Graph Theory37 sections · 1633 units
Open in Course

Matrix - Visual Example

Seeing the Grid

Nodes: 11, 22, 33. Edges: (11-22) and (22-33). Here is what the matrix looks like:

  1 2 3
1 [ 0 1 0 ]
2 [ 1 0 1 ]
3 [ 0 1 0 ]

Notice the matrix is symmetric. Position M[1][2] equals position M[2][1] because if node 11 connects to node 22, then node 22 connects to node 11. This symmetry only holds for undirected graphs. For directed graphs, the matrix is not symmetric.

The diagonal is all zeros because no node connects to itself. If self-loops existed, you would see ones on the diagonal. You can read any cell to immediately answer "Is there an edge between ii and jj?" That lookup takes O(1)O(1) time, which is the main advantage of this representation.