Trying to understand Adjacency Matrix Representation of an Graph

Hi trying to understand toString method from the following code, It’s clear to me that to represent graph in string format we ‘re iterating through vertex and array, but what’s logic behind it? How it’s exactly working? Any help would be appreciated
: …


This content originally appeared on DEV Community and was authored by ganeshkulkarni123

Hi trying to understand toString method from the following code, It's clear to me that to represent graph in string format we 're iterating through vertex and array, but what's logic behind it? How it's exactly working? Any help would be appreciated
: public class AdjMatrixGraph {

private int V; // number of vertices in Graph
private int E; // number of edges in Graph
private int[][] adjMatrix;

public AdjMatrixGraph(int nodes) {
    this.V = nodes;
    this.E = 0;
    this.adjMatrix = new int[nodes][nodes];
}

public void addEdge(int u, int v) {
    adjMatrix[u][v] = 1;
    adjMatrix[v][u] = 1; // because it is an undirected graph
    E++;
}

public String toString() {
    StringBuilder sb = new StringBuilder();
    sb.append(V + " vertices, " + E + " edges " + "\n");
    for(int v = 0; v < V; v++) {
        sb.append(v + ": ");
        for(int w : adjMatrix[v]) {
            sb.append(w + " ");
        }
        sb.append("\n");
    }
    return sb.toString();
}

public static void main(String[] args) {
    AdjMatrixGraph g = new AdjMatrixGraph(4);
    g.addEdge(0, 1);
    g.addEdge(1, 2);
    g.addEdge(2, 3);
    g.addEdge(3, 0);
    System.out.println(g);
}

}


This content originally appeared on DEV Community and was authored by ganeshkulkarni123


Print Share Comment Cite Upload Translate Updates
APA

ganeshkulkarni123 | Sciencx (2021-06-05T10:33:26+00:00) Trying to understand Adjacency Matrix Representation of an Graph. Retrieved from https://www.scien.cx/2021/06/05/trying-to-understand-adjacency-matrix-representation-of-an-graph/

MLA
" » Trying to understand Adjacency Matrix Representation of an Graph." ganeshkulkarni123 | Sciencx - Saturday June 5, 2021, https://www.scien.cx/2021/06/05/trying-to-understand-adjacency-matrix-representation-of-an-graph/
HARVARD
ganeshkulkarni123 | Sciencx Saturday June 5, 2021 » Trying to understand Adjacency Matrix Representation of an Graph., viewed ,<https://www.scien.cx/2021/06/05/trying-to-understand-adjacency-matrix-representation-of-an-graph/>
VANCOUVER
ganeshkulkarni123 | Sciencx - » Trying to understand Adjacency Matrix Representation of an Graph. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/06/05/trying-to-understand-adjacency-matrix-representation-of-an-graph/
CHICAGO
" » Trying to understand Adjacency Matrix Representation of an Graph." ganeshkulkarni123 | Sciencx - Accessed . https://www.scien.cx/2021/06/05/trying-to-understand-adjacency-matrix-representation-of-an-graph/
IEEE
" » Trying to understand Adjacency Matrix Representation of an Graph." ganeshkulkarni123 | Sciencx [Online]. Available: https://www.scien.cx/2021/06/05/trying-to-understand-adjacency-matrix-representation-of-an-graph/. [Accessed: ]
rf:citation
» Trying to understand Adjacency Matrix Representation of an Graph | ganeshkulkarni123 | Sciencx | https://www.scien.cx/2021/06/05/trying-to-understand-adjacency-matrix-representation-of-an-graph/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.