Tutorials › Graph Theory (Undergraduate) › Closed Walks, Traces, And Triangle Counts

Adjacency and Laplacian Matrices · Tutorial 619 of 799

Closed Walks, Traces, And Triangle Counts

Use adjacency-matrix powers to count closed walks and derive a precise formula for the number of triangles in a simple graph.

Intermediate 10 min read

What You'll Learn

  • Interpret diagonal entries of an adjacency-matrix power as closed-walk counts.
  • Relate the trace of a matrix power to the total number of closed walks of that length.
  • Compute length-two and length-three closed walks in small graphs.
  • Prove that every length-three closed walk in a simple graph comes from a triangle.
  • Apply the trace formula to calculate triangle counts and avoid common counting errors.

From Walks to a Single Number

An adjacency-matrix power records many walk counts at once. Its diagonal entries focus on walks that return to their starting vertex, while the trace adds those counts over all possible starting vertices. For length three, the resulting total has a particularly simple interpretation: each triangle contributes six closed walks, one for each choice of starting vertex and direction.

Throughout, \(G=(V,E)\) is a finite simple undirected graph. A walk of length \(k\) is a sequence \(v_0,v_1,\dots,v_k\) in which consecutive vertices are adjacent; vertices and edges may repeat. It is closed if \(v_k=v_0\). A triangle is a set of three distinct vertices with all three edges between them.

Definition: If the vertices of \(G\) are ordered as \(v_1,\dots,v_n\), its adjacency matrix \(\mathbf A=\mathbf A(G)\) has entry \(A_{ij}=1\) when \(v_i v_j\in E\), and \(A_{ij}=0\) otherwise. The trace of an \(n\)-by-\(n\) matrix \(M\) is \(\operatorname{tr}(M)=\sum_{i=1}^n M_{ii}\).

Since the graph is simple and undirected, \(\mathbf A\) is symmetric and has zero diagonal. The ordering of vertices affects how the matrix is displayed, but not the walk counts or trace formulas below.

Diagonal Entries and Closed Walks

Proposition: For every nonnegative integer \(k\), the entry \((\mathbf A^k)_{ij}\) is the number of walks of length \(k\) from \(v_i\) to \(v_j\). In particular, \((\mathbf A^k)_{ii}\) counts closed walks of length \(k\) starting at \(v_i\), and \(\operatorname{tr}(\mathbf A^k)\) counts all such walks over all starting vertices.

Proof. For \(k=0\), \(\mathbf A^0\) is the identity matrix: its \(ij\) entry is \(1\) if \(i=j\) and \(0\) otherwise. This agrees with the number of length-zero walks from \(v_i\) to \(v_j\), since such a walk stays at its one starting vertex. Now suppose the claim holds for \(k\). Matrix multiplication gives \((\mathbf A^{k+1})_{ij}=\sum_{\ell=1}^n(\mathbf A^k)_{i\ell}A_{\ell j}\). For each \(\ell\), the first factor counts walks of length \(k\) from \(v_i\) to \(v_\ell\), and the second factor is \(1\) exactly when \(v_\ell v_j\) is an edge. Thus the sum counts exactly the walks formed by taking a length-\(k\) walk to \(v_\ell\) and then the final edge to \(v_j\). This proves the claim by induction. Setting \(i=j\) gives the closed-walk interpretation of the diagonal, and summing those entries gives the trace interpretation. \(\square\)

This result counts walks, not paths: a walk is allowed to repeat a vertex or edge. For instance, a walk of length two can travel along an edge and immediately return along the same edge. The zero diagonal of \(\mathbf A\) means there are no length-one closed walks in a simple graph; the length-zero case is different, since each vertex has one length-zero walk starting and ending there.

Corollary: In a finite simple undirected graph, \((\mathbf A^2)_{ii}=\deg(v_i)\), and \(\operatorname{tr}(\mathbf A^2)=2|E|\).

Proof. A length-two walk from \(v_i\) back to itself chooses a neighbor \(v_j\) of \(v_i\), then returns along the same edge. There is exactly one such walk for each neighbor, so the diagonal entry is \(\deg(v_i)\). Summing gives \(\operatorname{tr}(\mathbf A^2)=\sum_{v\in V}\deg(v)\). Each edge contributes one to the degree of each of its two endpoints, so this sum is \(2|E|\). \(\square\)

Worked Example: Closed Walks in a Path

Let \(G=(V,E)\) have \(V=\{1,2,3,4\}\) and \(E=\{12,23,34\}\), the path \(P_4\). In the order \(1,2,3,4\), its adjacency matrix is

$$ \mathbf A= \begin{pmatrix} 0&1&0&0\\ 1&0&1&0\\ 0&1&0&1\\ 0&0&1&0 \end{pmatrix}. $$

Multiplying \(\mathbf A\) by itself gives

$$ \mathbf A^2= \begin{pmatrix} 1&0&1&0\\ 0&2&0&1\\ 1&0&2&0\\ 0&1&0&1 \end{pmatrix}. $$

For example, the diagonal entry \((\mathbf A^2)_{22}=2\) counts the two walks \(2,1,2\) and \(2,3,2\). The diagonal entries are \(1,2,2,1\), so \(\operatorname{tr}(\mathbf A^2)=1+2+2+1=6\). The graph has three edges, and the independent check gives \(2|E|=2\cdot3=6\).

To check length three, the formula \((\mathbf A^3)_{ii}=\sum_j(\mathbf A^2)_{ij}A_{ji}\) sums the length-two counts over neighbors \(j\) of \(i\). For vertex 1 the only neighbor is 2, and \((\mathbf A^2)_{12}=0\), so \((\mathbf A^3)_{11}=0\). For vertex 2, its neighbors are 1 and 3, and \((\mathbf A^2)_{21}=(\mathbf A^2)_{23}=0\), giving \((\mathbf A^3)_{22}=0\). For vertex 3, the corresponding entries at neighbors 2 and 4 are \((\mathbf A^2)_{32}=0\) and \((\mathbf A^2)_{34}=0\), giving \((\mathbf A^3)_{33}=0\). For vertex 4, its only neighbor is 3 and \((\mathbf A^2)_{43}=0\), so \((\mathbf A^3)_{44}=0\). Thus the trace is zero, consistent with the absence of triangles in this path.

Why the Cube Counts Triangles

The trace of \(\mathbf A^3\) counts all closed walks of length three, with a walk counted separately for every starting vertex and order in which it is traversed. In a simple graph, each such walk must use three distinct vertices: if two consecutive vertices in the three-step closed sequence were equal, one of its steps would be a loop, which is not allowed. If the first and third vertices were equal, the final step back to the starting vertex would be a loop. Therefore the three vertices are distinct, and all three edges between them are present. They form a triangle.

Theorem: For every finite simple undirected graph \(G\), if \(T(G)\) is its number of triangles, then \(\operatorname{tr}(\mathbf A(G)^3)=6T(G)\).

Proof. By the proposition, the trace counts every closed walk of length three. The preceding argument shows that every such walk visits three distinct vertices and therefore traverses a triangle. Conversely, each triangle with vertices \(a,b,c\) gives exactly six closed walks of length three: there are three choices for the starting vertex, and from each starting vertex there are two directions around the triangle. These six walks are distinct as sequences of vertices. Also, a closed walk determines its three vertices and hence determines its triangle, so walks arising from different triangles are not counted together. Thus each triangle contributes exactly six to the trace, and no other closed walks contribute. Therefore \(\operatorname{tr}(\mathbf A^3)=6T(G)\). \(\square\)

A useful way to compute the diagonal entries of \(\mathbf A^3\) is to first calculate \(\mathbf A^2\). The multiplication rule gives \((\mathbf A^3)_{ii}=\sum_j(\mathbf A^2)_{ij}A_{ji}\), so only neighbors \(v_j\) of \(v_i\) contribute. In a simple undirected graph, \((\mathbf A^2)_{ij}\) counts the common neighbors of \(v_i\) and \(v_j\): a length-two walk from \(v_i\) to \(v_j\) chooses exactly such a common neighbor. This makes the trace calculation particularly concrete.

Worked Example: The Triangle \(K_3\)

Let \(V=\{1,2,3\}\) and \(E=\{12,13,23\}\). The adjacency matrix and its square are

$$ \mathbf A= \begin{pmatrix} 0&1&1\\ 1&0&1\\ 1&1&0 \end{pmatrix}, \qquad \mathbf A^2= \begin{pmatrix} 2&1&1\\ 1&2&1\\ 1&1&2 \end{pmatrix}. $$

For vertex 1, the neighbors are 2 and 3, so \((\mathbf A^3)_{11}=(\mathbf A^2)_{12}+(\mathbf A^2)_{13}=1+1=2\). For vertex 2, the neighbors are 1 and 3, giving \((\mathbf A^3)_{22}=(\mathbf A^2)_{21}+(\mathbf A^2)_{23}=1+1=2\). For vertex 3, the neighbors are 1 and 2, giving \((\mathbf A^3)_{33}=(\mathbf A^2)_{31}+(\mathbf A^2)_{32}=1+1=2\). Hence \(\operatorname{tr}(\mathbf A^3)=2+2+2=6\), and the theorem gives \(T(G)=6/6=1\).

Check: The only three-element subset of \(V\) is \(\{1,2,3\}\), and all three of its possible edges are in \(E\), so there is exactly one triangle. Its six closed walks are the three rotations of \(1,2,3,1\) and the three rotations in the opposite direction.

Worked Example: Two Triangles Sharing an Edge

Let \(V=\{1,2,3,4\}\) and \(E=\{12,13,23,14,24\}\). The two triangles are expected to be \(123\) and \(124\), which share edge \(12\). The adjacency matrix and its square are

$$ \mathbf A= \begin{pmatrix} 0&1&1&1\\ 1&0&1&1\\ 1&1&0&0\\ 1&1&0&0 \end{pmatrix}, \qquad \mathbf A^2= \begin{pmatrix} 3&2&1&1\\ 2&3&1&1\\ 1&1&2&2\\ 1&1&2&2 \end{pmatrix}. $$

The diagonal of \(\mathbf A^2\) is \(3,3,2,2\), matching the degrees of vertices \(1,2,3,4\). For the cube, sum the relevant entries of each row of \(\mathbf A^2\), using only neighbors. Vertex 1 has neighbors 2, 3, and 4, so \((\mathbf A^3)_{11}=2+1+1=4\). Vertex 2 has neighbors 1, 3, and 4, so \((\mathbf A^3)_{22}=2+1+1=4\). Vertex 3 has neighbors 1 and 2, so \((\mathbf A^3)_{33}=1+1=2\). Vertex 4 has neighbors 1 and 2, so \((\mathbf A^3)_{44}=1+1=2\). Therefore \(\operatorname{tr}(\mathbf A^3)=4+4+2+2=12\), and the formula gives \(T(G)=12/6=2\).

Check: The four possible triples are \(123,124,134,234\). The first two have all three edges, while \(134\) and \(234\) each require the missing edge \(34\). Thus there are exactly two triangles, agreeing with the trace calculation.

Common Counting Pitfall

Do not divide the trace by three. In the \(K_3\) example, the trace is 6, and dividing by 3 would give 2, although there is only one triangle. The trace counts walks with both a starting vertex and a direction specified. Each triangle has three choices of starting vertex and two choices of direction, so the correct divisor is \(3\cdot2=6\).

There is a related but different count using common neighbors. If one sums the number of common neighbors over all edges, each triangle is counted once for each of its three edges. That sum is therefore three times the number of triangles. It has a different divisor because it counts edge-triangle incidences, not rooted, directed closed walks. Keeping track of exactly what is being counted is essential whenever a matrix sum is translated into a graph count.

The trace is useful because it compresses a count over all starting vertices into one matrix quantity. For length two it recovers twice the number of edges; for length three it recovers six times the number of triangles. The same walk-counting interpretation applies to other powers, although longer closed walks can revisit vertices and edges, so their traces generally count more varied configurations than triangles.

Check Your Understanding

Use the definitions and counting arguments above to answer these questions.

  1. For a finite simple undirected graph, explain why \((\mathbf A^2)_{ii}\) equals the degree of vertex \(v_i\).
  2. Let \(V=\{1,2,3\}\) and \(E=\{12,23\}\). What is \(\operatorname{tr}(\mathbf A^3)\), and why?
  3. Why does each triangle give six, rather than three, closed walks of length three?
  4. For a graph with three triangles, what is \(\operatorname{tr}(\mathbf A^3)\)? State the theorem used.
  5. Explain why summing common-neighbor counts over edges uses a different divisor from the trace formula.