Adjacency matrices and their powers
A network can be stored as a square table of 0s and 1s. What does multiplying that table by itself count, and which questions about the network does that arithmetic answer?
Where you are 4 in Graphs, vertices and edges
Why this exists
A drawing of a graph suits the eye, not a computer. A table with a row and a column for each vertex, holding 1 where two vertices are adjacent and 0 elsewhere, stores the same graph exactly. The surprise is what happens when that table is multiplied by itself with the ordinary rule for matrices: the entries of the powers count walks. Questions about reach, distance and triangles become arithmetic.
Definition
Graphs are as in Degree, the handshake lemma and complete graphs: G = (V, E) is simple, uv is an edge and \deg(v) a degree. Walks, their length, “joined”, “connected” and the distance d(u, v) are those of Walks, paths, cycles and connected graphs; directed graphs D = (V, A), arcs (u, v), \deg^+(v) and \deg^-(v) are those of Weighted and directed graphs.
List the vertices of G in an order v_1, v_2, \ldots, v_n. The adjacency matrix of G is the n \times n matrix A = [a_{ij}] with
For a directed graph D = (V, A) the adjacency matrix is written M = [m_{ij}] here, so that the letter A keeps naming the arc set: m_{ij} = 1 if (v_i, v_j) is an arc and 0 otherwise. Row i belongs to the tail, column j to the head.
Products are those of Matrix multiplication: (XY)_{ij} = \sum_{m} x_{im}y_{mj}, row i of X times column j of Y. A^k is the product of k copies of A, and A^0 = I_n, the identity matrix. The trace \operatorname{tr}(X) of a square matrix is the sum of its diagonal entries. A triangle in G is a cycle of length 3: three vertices that are pairwise adjacent.
Intuition
Row i of A marks the neighbors of v_i with 1s. In the product
a term is 1 exactly when v_m is a neighbor of both v_i and v_j, so the sum counts the ways to go from v_i to some v_m and on to v_j: walks of length 2. Each further factor of A adds one more step.
Results
1. Reading the matrix. For a simple graph, A is symmetric (a_{ij} = a_{ji}, see The transpose and symmetric matrices) with zero diagonal; row i and column i each sum to \deg(v_i); all entries together sum to 2\lvert E \rvert. For a directed graph, M has zero diagonal, row i sums to \deg^+(v_i), column i to \deg^-(v_i), all entries to the number of arcs, and M need not be symmetric.
2. Relabeling. Listing the vertices in the order v_{\sigma(1)}, \ldots, v_{\sigma(n)}, for a permutation \sigma of 1, \ldots, n, gives the matrix B with b_{ij} = a_{\sigma(i)\sigma(j)}: rows and columns are permuted together. So G_1 \cong G_2 (Graph isomorphism: when two drawings are the same graph) exactly when their vertices can be ordered to make the matrices equal, and every \operatorname{tr}(A^k) is an invariant.
3. Powers count walks. For every integer k \ge 0, the entry (A^k)_{ij} is the number of walks of length k from v_i to v_j. Likewise (M^k)_{ij} counts directed walks of length k.
4. Consequences. Let G be a simple graph with n vertices.
- (a) (A^2)_{ii} = \deg(v_i), and for i \ne j, (A^2)_{ij} is the number of common neighbors of v_i and v_j.
- (b) \operatorname{tr}(A^3) is 6 times the number of triangles in G.
- (c) d(v_i, v_j) is the smallest k \ge 0 with (A^k)_{ij} \ne 0, and \infty if there is none. With M, the same gives the length of a shortest directed walk.
- (d) In S = I + A + A^2 + \cdots + A^{n-1}, the entry s_{ij} is positive exactly when v_i and v_j are joined. So G is connected exactly when no entry of S is 0.
Why it is true
Result 1. Proof. v_iv_j and v_jv_i are the same edge, so a_{ij} = a_{ji}, and no vertex is adjacent to itself, so a_{ii} = 0. Row i has a 1 for each neighbor of v_i, so it sums to \deg(v_i), and by symmetry so does column i. The row sums add to the sum of the degrees, which is 2\lvert E \rvert by the handshake lemma. In M, each arc puts one 1 in its tail’s row and its head’s column. \square
Result 3. Proof. By induction on k. For k = 0, A^0 = I, and the only walk of length 0 from v_i is (v_i): one walk to v_j when i = j, none otherwise, matching the entries of I.
Suppose the statement holds for some k \ge 0. Since A^{k+1} = A^k A,
Sort the walks of length k + 1 from v_i to v_j by their next-to-last vertex v_m. Such a walk is a walk of length k from v_i to v_m followed by the step v_mv_j, which is allowed exactly when a_{mj} = 1. By the induction hypothesis there are (A^k)_{im} choices for the first part, so (A^k)_{im}a_{mj} walks have next-to-last vertex v_m. Each walk has exactly one next-to-last vertex, so adding over m counts every walk once. The directed case is the same with arcs (v_m, v_j). \square
Result 2. Proof. In the new order, position i holds v_{\sigma(i)}, so row i, column j records whether v_{\sigma(i)} and v_{\sigma(j)} are adjacent. An isomorphism is a matching of vertices that keeps adjacency, which says exactly that the matrices agree once the vertices of G_2 are listed as the images of v_1, \ldots, v_n. By Result 3, (B^k)_{ii} counts closed walks of length k at v_{\sigma(i)}, which is (A^k)_{\sigma(i)\sigma(i)}: the diagonal of B^k is that of A^k reordered, and the traces agree. \square
Result 4. Proof. (a) A walk (v_i, x, v_j) is a choice of a common neighbor x; when i = j every neighbor of v_i qualifies.
(b) (A^3)_{ii} counts closed walks (v_i, x, y, v_i). Consecutive vertices are adjacent and there are no loops, so x \ne v_i, y \ne x, y \ne v_i: the three are different and pairwise adjacent, a triangle through v_i. Conversely a triangle \{v_i, x, y\} gives exactly two such walks, one in each direction. Adding over i counts each triangle at each of its 3 vertices, twice, so \operatorname{tr}(A^3) = 6 \cdot (\text{number of triangles}).
(c) d(v_i, v_j) is the least length of a walk from v_i to v_j, and by Result 3 a walk of length k exists exactly when (A^k)_{ij} \ne 0.
(d) Entries of powers are never negative, so s_{ij} > 0 exactly when some (A^k)_{ij} > 0 with k \le n - 1. If v_i and v_j are joined, a shortest walk between them is a path (Result 1 of Walks, paths, cycles and connected graphs), with at most n vertices and so length at most n - 1. If they are not joined, there is no walk and s_{ij} = 0. \square
Worked examples
The matrix of a graph
Fikriye keeps track of her badminton club’s doubles games: Figure 1 joins two of five players A, B, C, D, E when they have played as partners. Write the adjacency matrix in the order A, B, C, D, E and read off who has had the most partners.
SolutionThe edges are AB, AC, BC, BD, CD, DE. Row A has 1s in columns B and C, row B in columns A, C, D, and so on:
Each edge appears twice, once in each endpoint’s row, so the matrix is symmetric; the diagonal is 0 because nobody partners themself. By Result 1 the row sums 2, 3, 3, 3, 1 are the degrees, so B, C and D have had three partners each. Check: 2 + 3 + 3 + 3 + 1 = 12 = 2 \cdot 6, twice the number of edges.
Squaring: common partners
Compute A^2 for Example 1. How many walks of length 2 run from B to C, and what does the entry in row A, column E say?
SolutionBy Result 4(a), entry (i, j) of A^2 counts common neighbors. Intersecting the neighbor sets \{B, C\}, \{A, C, D\}, \{A, B, D\}, \{B, C, E\}, \{D\} in pairs, or multiplying row by column, gives
So (A^2)_{BC} = 2: the walks (B, A, C) and (B, D, C), one through each common partner. The diagonal repeats the degrees, since a closed walk (v, x, v) goes out to a neighbor and straight back. And (A^2)_{AE} = 0 says A and E have no partner in common.
Cubing: triangles and distance
Compute A^3 for Example 1, count the triangles, list the walks of length 3 from B to D, and find d(A, E).
SolutionMultiplying A^2 by A,
The trace is 2 + 4 + 4 + 2 + 0 = 12, so by Result 4(b) there are 12 / 6 = 2 triangles, \{A, B, C\} and \{B, C, D\}. The diagonal agrees: B and C lie on two each, A and D on one, E on none.
A walk (B, x, y, D) needs x a neighbor of B and y a common neighbor of x and D. Taking x = A, C, D in turn: (B, A, B, D), (B, A, C, D); (B, C, B, D); (B, D, B, D), (B, D, C, D), (B, D, E, D). That is 6 walks, matching (A^3)_{BD} = 6, and only (B, A, C, D) is a path.
The entry in row A, column E is 0 in I, A and A^2, and 2 in A^3. By Result 4(c), d(A, E) = 3, along (A, B, D, E) and (A, C, D, E).
A round robin
A tournament is a directed graph with exactly one arc between each two vertices. Four new club members, 1 to 4, play each other once, and an arc runs from winner to loser: 1 beat 2 and 3, 2 beat 3 and 4, 3 beat 4, and 4 beat 1 (Figure 2). Find M, the win and loss counts, and M + M^2. Which players reach every other player through a chain of at most two wins?
In the order 1, 2, 3, 4,
By Result 1 the row sums 2, 2, 1, 1 are the wins and the column sums 1, 1, 2, 2 the losses; each list adds to 6 = \binom{4}{2}, one per match. M is not symmetric: m_{12} = 1 but m_{21} = 0. Row i of M^2 is the sum of the rows of M for the players i beat (row 1 is row 2 plus row 3), so
By Result 3, (M^2)_{14} = 2 counts the chains (1, 2, 4) and (1, 3, 4). An off-diagonal entry of M + M^2 is positive exactly when a directed walk of length 1 or 2 joins the two players. Rows 1, 2 and 4 have no off-diagonal 0, so those players reach everyone. Row 3 has 0 in column 2: player 3 beat only 4, who beat only 1, so d(3, 2) = 3 along (3, 4, 1, 2).
Two matrices, two graphs
Fikriye receives two graphs on the vertices 1 to 6 as matrices only:
Every row of each sums to 2. Are the graphs isomorphic? Is each connected?
SolutionEqual degrees settle nothing, so compare the traces of the cubes, which Result 2 makes invariants. In X every 1 links a vertex of \{1, 2, 3\} to one of \{4, 5, 6\}, so each step of a walk changes side, every closed walk has even length, and \operatorname{tr}(X^3) = 0. In Y, rows 1, 4, 5 have their 1s only in columns 1, 4, 5, and these three are pairwise adjacent; likewise 2, 3, 6. Each vertex lies on one triangle, so each diagonal entry of Y^3 is 2 and \operatorname{tr}(Y^3) = 12. The traces differ: not isomorphic.
A walk in Y starting at 1 never leaves \{1, 4, 5\}, so (Y^k)_{12} = 0 for every k, the row 1, column 2 entry of I + Y + \cdots + Y^5 is 0, and by Result 4(d) Y is disconnected: two triangles. Following the 1s of X gives the closed walk (1, 4, 2, 5, 3, 6, 1) through all six vertices, so X is connected, a relabeled C_6.
Boundaries
- Powers count walks, not paths: of the 6 walks in Example 3 only one is a path. No power of A counts paths in general; see Hamiltonian paths and cycles.
- The matrix depends on the vertex order. Equal invariants can show two graphs differ but never prove them the same (Exercise 16); proving isomorphism needs an ordering that makes the matrices equal, out of n!.
- Result 4(b) needs a simple undirected graph. For a directed graph, \operatorname{tr}(M^3) is 3 times the number of directed 3-cycles (Exercise 14), and with loops or repeated edges the count fails.
- Squaring a weight table does not give cheapest routes: a product adds over middle vertices where a cheapest route takes a minimum. See Finding a shortest path.
Where this goes next
Treating a graph as a matrix opens linear algebra to it. The eigenvalues of A govern how fast the number of walks grows, the theme of Matrix powers, dynamical systems and steady states. The matrix-tree theorem, stated here without proof, counts the spanning trees of a connected graph as a determinant built from A and the degrees. Exercise 13 is a first step toward Bipartite graphs, whose matrices have the block shape of X in Example 5.
Exercises
Building and reading matrices
Exercises 1, 2 and 4 to 7 use the graph H on 1, 2, 3, 4, 5 with edges 12, 13, 15, 23, 34, 45.
- Write the adjacency matrix of H. Read the degrees from it, and check that the entries add to twice the number of edges.
- Fikriye relabels H, calling vertex 1 by the name 4 and vertex 4 by the name 1. Write the new matrix.
- A directed graph D on 1, 2, 3, 4 has the arcs (1, 2), (2, 3), (3, 1), (3, 4), (4, 2). Write M and read off each out-degree and in-degree.
Counting walks with powers
- Compute the square of the matrix of H and check its diagonal against the degrees.
- How many walks of length 2 run from 3 to 5 in H? List them. How many walks of length 3 run from 1 to 5?
- Use the trace of the cube of the matrix of H to count the triangles of H, and name them.
- Use I + A + A^2, for the matrix A of H, to show that every distance in H is at most 2.
- For D in Exercise 3, compute M^2 and M^3, and use them to find d(1, 2), d(2, 1) and d(1, 4) along directed walks.
- Graphs on 1, \ldots, 5 have the edges 13, 35, 25, 24 (G_1) and 12, 14, 24, 35 (G_2). Show that they have the same degree sequence but are not isomorphic, decide which is disconnected, and find the smallest k with a nonzero entry in row 1, column 4 of the kth power of the matrix of G_1.
- Let A be the matrix of K_4. Show that A^2 = 2A + 3I, write A^3 in the form pA + qI, and use it to count the walks of length 3 between two different vertices and the triangles.
Reasoning
- Prove that a simple graph contains a cycle of length 4 exactly when some off-diagonal entry of A^2 is at least 2.
- Prove that the sum of all the entries of A^2 is \sum_{v \in V} \deg(v)^2.
- Prove that if G has no cycle of odd length, then (A^k)_{ii} = 0 for every odd k and every i.
- Prove that for a directed graph, \operatorname{tr}(M^3) is 3 times the number of directed cycles of length 3, and check it on D from Exercise 3.
- The path graph P_n has vertices 1, \ldots, n and edges \{i, i + 1\} for 1 \le i \le n - 1. Use it to show that the sum in Result 4(d) cannot in general stop at A^{n-2}.
- Refute: “Two graphs with the same degree sequence and the same value of \operatorname{tr}(A^3) are isomorphic.”
Answers
- \begin{bmatrix}0&1&1&0&1\\1&0&1&0&0\\1&1&0&1&0\\0&0&1&0&1\\1&0&0&1&0\end{bmatrix}, in the order 1, \ldots, 5. Degrees 3, 2, 3, 2, 2, and 3 + 2 + 3 + 2 + 2 = 12 = 2 \cdot 6.
- Swap rows 1 and 4 and also columns 1 and 4 (Result 2): \begin{bmatrix}0&0&1&0&1\\0&0&1&1&0\\1&1&0&1&0\\0&1&1&0&1\\1&0&0&1&0\end{bmatrix}. The old edge 15 is now 45, the 1 in row 4, column 5.
- M = \begin{bmatrix}0&1&0&0\\0&0&1&0\\1&0&0&1\\0&1&0&0\end{bmatrix}. Out-degrees (row sums) 1, 1, 2, 1; in-degrees (column sums) 1, 2, 1, 1; each list adds to 5 arcs.
- A^2 = \begin{bmatrix}3&1&1&2&0\\1&2&1&1&1\\1&1&3&0&2\\2&1&0&2&0\\0&1&2&0&2\end{bmatrix}, from the neighbor sets \{2, 3, 5\}, \{1, 3\}, \{1, 2, 4\}, \{3, 5\}, \{1, 4\}. The diagonal is the degree list, by Result 4(a).
- (A^2)_{35} = 2: the walks (3, 1, 5) and (3, 4, 5). Row 1 of A^2 times column 5 of A is 3 + 2 = 5: (1, 2, 1, 5), (1, 3, 1, 5), (1, 3, 4, 5), (1, 5, 1, 5), (1, 5, 4, 5).
- The diagonal of A^3 is 2, 2, 2, 0, 0, so the trace is 6 and there is 6 / 6 = 1 triangle, \{1, 2, 3\}.
- The non-adjacent pairs \{1, 4\}, \{2, 4\}, \{2, 5\}, \{3, 5\} have entries 2, 1, 1, 2 in A^2. So I + A + A^2 has no zero entry, and by Result 4(c) every distance is at most 2.
- M^2 = \begin{bmatrix}0&0&1&0\\1&0&0&1\\0&2&0&0\\0&0&1&0\end{bmatrix}, M^3 = \begin{bmatrix}1&0&0&1\\0&2&0&0\\0&0&2&0\\1&0&0&1\end{bmatrix}. d(1, 2) = 1; d(2, 1) = 2, along (2, 3, 1); d(1, 4) = 3, along (1, 2, 3, 4), since the (1, 4) entry is 0 in M and M^2.
- Both degree sequences are (2, 2, 2, 1, 1). The cube of the matrix of G_1 has trace 0 and that of G_2 trace 6: G_2 has the triangle \{1, 2, 4\} and G_1 none, so they are not isomorphic. In G_2, rows 1, 2, 4 have 1s only in columns 1, 2, 4, so no power is nonzero in row 1, column 3: G_2 is disconnected. For G_1 the entry is 0 for k = 1, 2, 3 and 1 for k = 4; G_1 is the path (1, 3, 5, 2, 4), and d(1, 4) = 4.
- Each diagonal entry of A^2 is a degree, 3, and two different vertices have 2 common neighbors, so A^2 = 2A + 3I. Then A^3 = 2A^2 + 3A = 2(2A + 3I) + 3A, which is 7A + 6I in the form asked. So 7 walks of length 3 join two different vertices, and \operatorname{tr}(A^3) = 4 \cdot 6 = 24 gives 24 / 6 = 4 = \binom{4}{3} triangles.
- Proof. If (A^2)_{ij} \ge 2 with i \ne j, then v_i and v_j have two different common neighbors x and y (Result 4(a)). Neither is v_i or v_j, since no vertex is its own neighbor, so (v_i, x, v_j, y, v_i) is a cycle of length 4. Conversely a cycle (a, b, c, d, a) makes b and d two common neighbors of a \ne c. \square
- Proof. By Result 3 the sum counts all walks (u, x, w) of length 2. For a middle vertex x, u and w are any neighbors of x, chosen independently and possibly equal: \deg(x)^2 walks. Adding over x gives the claim. \square
- Proof. If (A^k)_{ii} > 0 for an odd k, Result 3 gives a closed walk of odd length k at v_i, and by Result 5 of Walks, paths, cycles and connected graphs it contains a cycle of odd length, a contradiction. \square
- Proof. (M^3)_{ii} counts directed walks (v_i, x, y, v_i). With no loops, x \ne v_i, y \ne x, y \ne v_i: a directed cycle of length 3 starting at v_i. Arcs fix the direction, so each such cycle is counted once at each of its 3 vertices. \square For D the diagonal of M^3 is 1, 2, 2, 1, and 6 = 3 \cdot 2 for the cycles (1, 2, 3, 1) and (2, 3, 4, 2).
- P_n is connected and d(1, n) = n - 1, so by Result 4(c) (A^k)_{1n} = 0 for every k \le n - 2, and I + A + \cdots + A^{n-2} has a zero in row 1, column n.
- Counterexample: the path P_6, and a 4-cycle with a separate edge. Both have degree sequence (2, 2, 2, 2, 1, 1) and no triangle, so \operatorname{tr}(A^3) = 0 for both. An isomorphism carries walks to walks and so keeps connectedness; P_6 is connected and the other is not.