Discrete Mathematics and Combinatorics

Adjacency Matrices and Counting Walks in a Graph

Turn a network into a grid of 0s and 1s, then use matrix powers to count walks, find distances and detect triangles.

Key takeaways

  • The adjacency matrix of a graph has a 1 in row i, column j when vertex i is joined to vertex j, and a 0 otherwise.
  • The (i, j) entry of the kth power of the matrix counts the walks of length k from i to j.
  • A walk may repeat vertices and edges, so these powers do not count simple paths.
  • The diagonal of the square gives vertex degrees, and the diagonal of the cube counts triangles.
  • The eigenvalues of the matrix determine the number of closed walks of every length.

If you write a graph as a square table of 0s and 1s and multiply that table by itself k times, the entry in row i and column j tells you exactly how many walks of length k lead from vertex i to vertex j. Ordinary matrix multiplication does the counting for you.

This is one of the most useful bridges between pictures and algebra. Once a network is a matrix, questions about connections, distances and loops become questions about numbers that a computer handles well.

Building the matrix

A graph is a set of points, called vertices, joined by links, called edges. Number the vertices 1 to n. The adjacency matrix A is the n × n table in which the entry A(i, j) is 1 if there is an edge between i and j, and 0 if not.

Take a graph with four vertices and four edges: 1–2, 1–3, 2–3 and 3–4. It is a triangle with a tail.

        1  2  3  4
    1   0  1  1  0
A = 2   1  0  1  0
    3   1  1  0  1
    4   0  0  1  0

For an undirected graph with no edge from a vertex to itself, the matrix is symmetric and has zeros down the main diagonal. For a directed graph, put a 1 in A(i, j) only when there is an arrow from i to j, and the symmetry disappears.

Walks and why matrix multiplication counts them

A walk of length k is a sequence of k steps along edges. You may revisit vertices and reuse edges. A path, by contrast, never repeats a vertex.

The entry of A² in row i, column j is computed by running along row i and column j together:

A²(i, j) = A(i,1)·A(1,j) + A(i,2)·A(2,j) + ... + A(i,n)·A(n,j)

Each term is 1 exactly when both i–m and m–j are edges, for the middle vertex m. So the sum counts the two-step walks from i to j, one for each possible middle stop.

The same idea repeats. A walk of length k from i to j is a walk of length k − 1 from i to some vertex m, followed by an edge from m to j. Summing over m is exactly what multiplying A^(k−1) by A does. A short argument by induction turns this into a proof for every k.

The worked powers

Multiplying out gives:

         2  1  1  1                2  3  4  1
A²  =    1  2  1  1        A³  =   3  2  4  1
         1  1  3  0                4  4  2  3
         1  1  0  1                1  1  3  0

Read one entry to test it. A³ in row 1, column 3 is 4. The three-step walks from vertex 1 to vertex 3 are:

  1. 1, 2, 1, 3
  2. 1, 3, 1, 3
  3. 1, 3, 2, 3
  4. 1, 3, 4, 3

There are no others. Row 1, column 4 of A³ is 1, matching the single walk 1, 2, 3, 4.

What each power of the adjacency matrix reveals
A: direct links A 1 marks a single edge. A²: two steps Diagonal entries are the vertex degrees. A³: three steps Diagonal sum equals 6 times the triangle count. Aᵏ: k steps First nonzero power gives the distance between two vertices. A: direct links A 1 marks a single edge. A²: two steps Diagonal entries are the vertex degrees. A³: three steps Diagonal sum equals 6 times the triangle count. Aᵏ: k steps First nonzero power gives the distance between two vertices.

Computing one entry by hand

It helps to see a single entry built up. To get A³ in row 3, column 4, take row 3 of A², which is 1, 1, 3, 0, and column 4 of A, which is 0, 0, 1, 0.

A³(3, 4) = 1·0 + 1·0 + 3·1 + 0·0 = 3

Only the third term survives, because vertex 3 is the sole neighbor of vertex 4. The number 3 in that term counts the two-step walks from vertex 3 back to itself: out to 1, 2 or 4 and straight back. Each of those can then be extended by the final step to vertex 4.

This is the pattern behind every entry. The earlier power supplies the number of ways to reach each intermediate vertex, and the matrix A selects the intermediate vertices that have an edge to the destination.

If you want all walks of length at most 3, add the matrices: A + A² + A³. The entry in row 1, column 4 is then 0 + 1 + 1 = 2.

Degrees and triangles on the diagonal

A walk of length 2 from a vertex back to itself goes out along an edge and returns. So A²(i, i) is the degree of vertex i, the number of edges at it. In the example the diagonal of A² reads 2, 2, 3, 1.

The sum of the diagonal entries is called the trace. The trace of A² is 8, which is twice the number of edges. That is the handshaking lemma seen through a matrix.

A closed walk of length 3 must go around a triangle. Each triangle is counted once for each of 3 starting points and 2 directions, so

number of triangles = trace(A³) / 6

Here trace(A³) = 2 + 2 + 2 + 0 = 6, so there is exactly one triangle.

For longer cycles the formulas get messier, because closed walks of length 4 or more can double back without forming a cycle. You have to subtract those cases.

Distances and connectivity

The distance between two vertices is the length of the shortest walk joining them. That is the smallest k for which Aᵏ(i, j) is not zero. Vertices 1 and 4 are not adjacent, but A²(1, 4) = 1, so their distance is 2.

A graph on n vertices is connected if every pair has a walk of length at most n − 1. One test is to check that every entry of (I + A) raised to the power n − 1 is positive, where I is the identity matrix with 1s on the diagonal. In practice a direct search of the graph is faster, but the matrix statement is handy in proofs.

Eigenvalues and closed walks

An eigenvalue of A is a number λ for which some nonzero vector v satisfies Av = λv. A symmetric n × n matrix has n real eigenvalues, counted with repetition. The total number of closed walks of length k, which is the trace of Aᵏ, equals the sum of the kth powers of those eigenvalues.

Take the complete graph on n vertices, where every pair is joined. Its eigenvalues are n − 1 (once) and −1 (n − 1 times). So the number of closed walks of length k is

(n − 1)ᵏ + (n − 1)·(−1)ᵏ

For n = 3, k = 3:  2³ + 2·(−1)³ = 8 − 2 = 6

Six matches the single triangle walked from three starting points in two directions. The study of what eigenvalues reveal about networks is called spectral graph theory. It informs methods for splitting networks into communities and for estimating how fast a random walk mixes.

Counting things that are not obviously graphs

Many counting problems become walk counts. How many strings of 0s and 1s of length n have no two 1s in a row? Let one state mean "the last symbol was 0" and another "the last symbol was 1". From the first you may go to either state, and from the second only back to 0.

M =  1  1
     1  0

Powers of M produce Fibonacci numbers, and the count of valid strings of length 1, 2, 3 and 4 is 2, 3, 5 and 8. This is the same sequence you get from solving the linear recurrence a(n) = a(n−1) + a(n−2). The technique is often called the transfer matrix method.

How fast walk counts grow

For long walks, the largest eigenvalue takes over. If the graph is connected and has at least one odd cycle, the number of walks of length k between any two vertices grows roughly like a constant times λᵏ, where λ is the largest eigenvalue. That number lies between the average degree and the maximum degree of the graph. In a graph where every vertex has degree d, it equals d, matching the d choices at each step.

If a graph has no odd cycle, it is bipartite: its vertices split into two sides with every edge running across. Walks then alternate sides, so an entry of Aᵏ is zero for every k of the wrong parity. You can see this in a square with four vertices, where no walk of odd length returns to its start. The eigenvalues of a bipartite graph come in pairs λ and −λ, which makes all the odd closed walk counts cancel.

There is also a compact way to package every length at once. For a small enough number x, the sum I + xA + x²A² + x³A³ + ... equals the inverse of the matrix I − xA, a matrix version of the geometric series. The entries of that inverse are generating functions for walk counts.

Efficiency and common mistakes

To compute A^64, do not multiply 63 times. Square the matrix repeatedly: A², A⁴, A⁸, and so on, which takes only six multiplications.

Storage is the other issue. A network with a million vertices would need a trillion entries. Most are zero, so software stores only the nonzero ones or uses a list of neighbors for each vertex.

Watch out for these errors:

  • Reading powers as path counts. Walks may backtrack. Counting simple paths is much harder in general.
  • Multiplying entry by entry. Aᵏ means matrix multiplication, not raising each entry to a power.
  • Forgetting direction. In a directed graph, A(i, j) and A(j, i) are different questions.
  • Overlooking weights. If entries are multiplied along a walk and summed, weights can represent the number of parallel links or transition probabilities.

That last case leads to random walks, and from there to the PageRank idea.

Common questions

Do the vertex numbers matter?

Renumbering the vertices shuffles rows and columns in the same way. The matrix looks different, but its trace, its eigenvalues and all walk counts between corresponding vertices stay the same.

Do two graphs with the same eigenvalues have to be identical?

No. There are pairs of different graphs with identical eigenvalues, so the spectrum does not capture everything about a graph.

What background do I need?

Matrix multiplication is enough for everything about counting walks. Eigenvalues come from a first course in linear algebra and make the long-run behavior much clearer.

Written for 4Mathematics with AI writing tools and not reviewed line by line by a person. Check anything you rely on against a textbook or primary source. Report an error.

Keep reading

All 45 articles in Discrete Mathematics and Combinatorics →

Latest news on this topic