YI YANG
YI YANG

Reputation: 133

Adjacency matrix deleting vertex

I am trying to implement the adjacency matrix in Java.

I am using an ArrayList for storing the vertex which is a letter label. And I build the matrix based on vertex index in ArrayList.

Let's say the undirected graph is

A-B

Store A, B in the array list.

And the matrix should be

[[false,true],[true,false]]

May I ask what should I do to delete a vertex?

Currently, I removed all the edges from the vertex, for the next step, I have 2 ideas

  1. Set value to null in the ArrayList based on vertex index.
  2. Remove the vertex from ArrayList based on its index and then change the 2d matrix boolean array. If this is the way, what should I change there, deleting row and column?

Yeah, I'll implement bfs or some function like calculating the shorted path between 2 vertexes as well.

Thanks for any suggestions and any learning sources provided.

Here is my code:

    public class AdjMatrix <T extends Object>
    {
        // List to store all vertexs
        private ArrayList<T> vertList;

        // graph in 2d array
        private boolean[][] adjMatrix;

        public AdjMatrix() {
            vertList = new ArrayList<T>();
            adjMatrix = new boolean[4000][4000];
        } 

        public void addVertex(T vertLabel) {
            vertList.add(vertLabel);
        } 

        public void addEdge(T srcLabel, T tarLabel) {
            int srcIndex = vertList.indexOf(srcLabel);
            int tarIndex = vertList.indexOf(tarLabel);
            adjMatrix[srcIndex][tarIndex] = true;
            adjMatrix[tarIndex][srcIndex] = true;
        } 

        public ArrayList<T> neighbours(T vertLabel) {

            ArrayList<T> neighbours = new ArrayList<T>();
            int vertIndex = vertList.indexOf(vertLabel);
            int[] vertEdges = adjMatrix[vertIndex];
            for(int i = 0; i < vertEdges.length; i++){
                if(vertEdges[i]){
                    neighbours.add(vertList.get(i));
                }
            }
            return neighbours;
        } 

        public void removeVertex(T vertLabel) {
            // what should I do there?
            ArrayList<T> neighbours = neighbours(vertLabel);
            for (T neighbour: neighbours
                 ) {
                removeEdge(vertLabel,neighbour);
            }
        }

        public void removeEdge(T srcLabel, T tarLabel) {
            int srcIndex = vertList.indexOf(srcLabel);
            int tarIndex = vertList.indexOf(tarLabel);
            adjMatrix[srcIndex][tarIndex] = false;
            adjMatrix[tarIndex][srcIndex] = false;       
        } 

        public void printVertices(PrintWriter os) {
            String result = "";
            for (T vert :
                    vertList) {
                result += vert + " ";
            }
            os.println(result);
        } 

        public void printEdges(PrintWriter os) {
            for (T vert :
                    vertList) {
                ArrayList<T> neighbours = neighbours(vert);
                for (T neighbour :
                        neighbours) {
                    os.println(vert + " " + neighbour);
                }
            }
        }
    }

Upvotes: 0

Views: 2111

Answers (1)

Paul Janssens
Paul Janssens

Reputation: 722

What do you mean 'delete'?

If you really want to delete-delete, just delete the row and column for the node deleted, and renumber the remaining nodes.

Upvotes: 1

Related Questions