The matrix is symmetric with a zero diagonal
For a simple undirected graph. Symmetry is the relation being symmetric; the zero diagonal is the absence of loops.
The same graph stored two ways. The matrix answers "are these adjacent" instantly and costs n² space; the list is compact and iterates neighbours quickly. And Aᵏ counts walks.
Core ideas
The choice is a trade between space and which operation is fast, and for sparse graphs it is not close.
For a simple undirected graph. Symmetry is the relation being symmetric; the zero diagonal is the absence of loops.
Most real graphs have |E| far below n². A road network with a million junctions has a few million edges and a matrix would need 10¹² entries.
Aᵏ[i][j] is the number of walks of length exactly k, because matrix multiplication sums over the intermediate vertex.
The material
Both structures, their costs, and the one theorem about powers.
The n × n matrix A with A[i][j] = 1 when vertices i and j are adjacent and 0 otherwise. For a simple undirected graph it is symmetric with zero diagonal.
For each vertex, the list of its neighbours. Total space Θ(n + |E|), since each edge appears in two lists.
The O and Θ in the table are the notation of “Divide-and-Conquer Recurrences”, in Induction and Recursion: Θ(n + |E|) means proportional to n + |E| up to a constant factor, and O(1) means bounded by a constant. “Big-O, Big-Omega and Big-Theta” in Algorithms and Complexity makes both precise; here they are read as "linear" and "constant".
The traversals of “Breadth-First and Depth-First Search” iterate neighbours constantly, so an adjacency list gives breadth-first search a running time of Θ(n + |E|) while a matrix gives Θ(n²). On a sparse graph that is the difference between linear and quadratic.
The matrix wins when the graph is dense, when adjacency tests dominate, or when the linear algebra of the next theorem is wanted.
For a graph with adjacency matrix A, the entry Aᵏ[i][j] is the number of walks of length exactly k from vertex i to vertex j. A walk may repeat vertices and edges.
By induction on k. For k = 1 the claim is the definition of A.
Assume it for k. A walk of length k+1 from i to j is a walk of length k from i to some t, followed by an edge from t to j. Summing over t gives Σ_t A^k[i][t] · A[t][j], which is exactly A^{k+1}[i][j] by the definition of matrix multiplication.
□
The proof is the definition of matrix multiplication read as a combinatorial statement: the sum over the middle index is the sum over the intermediate vertex.
A³[v][v] counts closed walks of length 3 from v back to itself, and each such walk is a triangle traversed from v. Each triangle gives 6 closed walks — three starting vertices, two directions — so the number of triangles is (Σ_v A³[v][v]) / 6, one sixth of the trace of A³.
That is a genuine algorithm: matrix multiplication counts triangles in O(n^ω) where ω < 2.4 is the matrix multiplication exponent, which beats the obvious O(n³) enumeration. It is one of the standard examples of algebra outperforming combinatorics.
Interactive
Your graph, the course's algorithms
Method
Ask which operation dominates.
|E| near n² is dense and favours the matrix; |E| near n is sparse and favours lists, usually decisively.
Repeated adjacency tests favour the matrix; repeated neighbour iteration favours lists.
n² entries is impossible past a few tens of thousands of vertices. Real large graphs are always stored as lists.
Aᵏ answers walk-counting questions directly, and nothing else does so cleanly.
C₄ with vertices 1–2–3–4–1.
The zeros in A² at adjacent positions are the bipartiteness of “Bipartite Graphs” showing up in the algebra: in a bipartite graph every walk of even length ends on the same side it started, so it can never end at a neighbour.
For a road network of a million junctions with an average of three roads each, state the space each representation needs: |E| = 1.5 million, so the lists hold 2|E| = 3 million entries against the matrix's 10¹² — a factor of about 300 000. Then say which queries each makes cheap.
Something to change about this lesson? Write it down, tick it off when it is handled, and export the list when you want to act on it. Kept in this browser only: nothing is sent anywhere, and nobody is notified.
No recommendations yet for this lesson.