Tutorials › Linear Algebra › Counting Network Walks with Matrix Powers

Applications in Networks, Economics and Data · Tutorial 932 of 1000

Counting Network Walks with Matrix Powers

Use matrix multiplication to count fixed-length walks, interpret the entries, and check when a matrix power counts walks rather than weighted totals.

Intermediate 10 min read

What You'll Learn

  • Define walks of a given length with edge choices distinguished when edges are parallel.
  • Prove why the entry in row i, column j of an adjacency-matrix power counts walks from i to j.
  • Compute exact-length walk counts in directed and undirected networks.
  • Obtain total walk counts by summing entries of a matrix power.
  • Avoid confusing walks of exactly k steps with walks of at most k steps.
  • Distinguish counts from weighted sums when using a weighted adjacency matrix.

Walks and Adjacency Matrices

A network records which transitions are possible. Its adjacency matrix records those transitions numerically, and matrix powers combine them across several steps. The key interpretation is precise: an entry of the \(k\)th power counts walks of exactly \(k\) edges between two vertices. This works for directed and undirected networks, provided that the matrix entries represent edge counts and that parallel edges are treated as distinct choices.

Consider a finite network with vertices labelled \(1,\dots,n\). In a directed network, an edge from \(i\) to \(j\) is oriented from \(i\) toward \(j\). For an undirected network, each edge can be traversed in either direction. A loop is an edge from a vertex to itself. If parallel edges are present, they are distinct edges with the same endpoints.

Definition: A walk of length \(k\) from vertex \(i\) to vertex \(j\) consists of vertices \(v_0,\dots,v_k\) and a choice of a traversed edge at each step, with \(v_0=i\), \(v_k=j\), and the chosen edge joining \(v_{r-1}\) to \(v_r\) in an allowed direction for every \(r=1,\dots,k\). Thus, when parallel edges are available for a step, choosing different edges gives different walks, even if the vertex sequence is the same. A walk may revisit vertices or edges. A walk of length \(0\) from \(i\) to \(j\) exists exactly when \(i=j\); it is the empty walk.

For a network in which we count walks, define the adjacency matrix \(\mathbf{A}=[a_{ij}]\in\mathbb{R}^{n\times n}\) by letting \(a_{ij}\) be the number of directed edges from \(i\) to \(j\). In an undirected network, count edges joining \(i\) and \(j\); the resulting matrix is symmetric. In particular, \(a_{ii}\) counts loops at \(i\). With no parallel edges, each entry is either \(0\) or \(1\). The row index is the starting vertex and the column index is the destination.

Here the entries must be edge counts, not arbitrary edge weights. If an adjacency matrix instead stores general numerical weights, its powers combine products of weights along walks; the entries are then weighted totals, not necessarily numbers of walks. For a counting matrix with parallel edges, each available edge is one separate choice, as the definition above requires.

Why a Matrix Product Adds One Step

The multiplication rule explains the walk-count interpretation. The \((i,j)\) entry of a product \(\mathbf{B}\mathbf{C}\) is \(\sum_{r=1}^n b_{ir}c_{rj}\). For adjacency matrices, the intermediate index \(r\) specifies the vertex reached after the first step. There are \(a_{ir}\) choices of edge for that first step and, when the entries of \(\mathbf{A}^{k}\) count length-\(k\) walks, \((\mathbf{A}^{k})_{rj}\) choices for the remaining steps. Multiplying counts the combinations through \(r\), and summing includes every possible intermediate vertex.

Theorem (Walk-counting theorem): Let \(\mathbf{A}\in\mathbb{R}^{n\times n}\) be the adjacency matrix of a finite directed or undirected network, where each entry is the number of edges between the corresponding vertices in the indicated direction. For every integer \(k\geq 0\), the entry \((\mathbf{A}^{k})_{ij}\) equals the number of walks of length exactly \(k\) from \(i\) to \(j\), with different choices of parallel edges counted as different walks. For \(k=0\), use \(\mathbf{A}^{0}=\mathbf{I}_n\).

Proof. We use induction on \(k\). For \(k=0\), \(\mathbf{A}^{0}=\mathbf{I}_n\), whose \((i,j)\) entry is \(1\) if \(i=j\) and \(0\) otherwise. These are exactly the numbers of length-\(0\) walks by the definition. For \(k=1\), \((\mathbf{A})_{ij}=a_{ij}\), which is the number of available edges from \(i\) to \(j\), so it counts length-\(1\) walks, including separate choices among parallel edges.

Now suppose for some \(k\geq 1\) that \((\mathbf{A}^{k})_{rj}\) counts length-\(k\) walks from \(r\) to \(j\), for every pair of vertices \(r,j\). By matrix multiplication,

$$ (\mathbf{A}^{k+1})_{ij} =\sum_{r=1}^{n} a_{ir}(\mathbf{A}^{k})_{rj}. $$

For each \(r\), a walk counted by the product \(a_{ir}(\mathbf{A}^{k})_{rj}\) is formed by choosing one of the \(a_{ir}\) edges from \(i\) to \(r\), then choosing one of the length-\(k\) walks from \(r\) to \(j\). This gives a walk of length \(k+1\) from \(i\) to \(j\). Conversely, every length-\((k+1)\) walk from \(i\) to \(j\) has a uniquely specified first edge and a uniquely specified remaining length-\(k\) walk. Its first edge ends at exactly one vertex \(r\), so it is counted in exactly one term of the sum. Thus the sum counts every such walk exactly once, proving the claim for \(k+1\). The induction proves the theorem for all \(k\geq 0\). \(\square\)

Worked Counts in Directed Networks

Worked Example: Parallel Edges Give Distinct Walks

Consider three vertices with two distinct edges from \(1\) to \(2\), one edge from \(2\) to \(3\), one edge from \(1\) to \(3\), and one edge from \(3\) to \(1\). Find the number of length-\(2\) walks from \(1\) to \(3\).

With rows as starting vertices and columns as destinations, the adjacency matrix is

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

To compute \(\mathbf{A}^2\), write its rows as combinations of the rows of \(\mathbf{A}\). The first row of \(\mathbf{A}\) has two edges to vertex \(2\) and one to vertex \(3\), so the first row of \(\mathbf{A}^2\) is twice row \(2\) of \(\mathbf{A}\), plus row \(3\) of \(\mathbf{A}\). The other rows are obtained in the same way:

$$ \begin{aligned} \text{row }1 &: 2(0,0,1)+(1,0,0)=(1,0,2),\\ \text{row }2 &: (1,0,0),\\ \text{row }3 &: (0,2,1). \end{aligned} $$
$$ \mathbf{A}^2= \begin{bmatrix} 1&0&2\\ 1&0&0\\ 0&2&1 \end{bmatrix}. $$

Therefore \((\mathbf{A}^2)_{13}=2\). Both walks go from \(1\) to \(2\) and then from \(2\) to \(3\), but they differ in which of the two parallel edges from \(1\) to \(2\) they use. The direct edge from \(1\) to \(3\) does not create a length-\(2\) walk to \(3\), because no edge from \(3\) to \(3\) is present.

Check: multiplying row \(1\) of \(\mathbf{A}\) by column \(3\) of \(\mathbf{A}\) gives \(0\cdot1+2\cdot1+1\cdot0=2\), matching the two edge-distinguished walks. If walks were defined only as vertex sequences, both would have the single sequence \((1,2,3)\); that definition would fail to match the matrix entry.

Worked Example: A Directed Network and Three Steps

Suppose a directed network has edges \(1\to2\), \(1\to3\), \(2\to3\), and \(3\to1\). Find all length-\(3\) walk counts.

Its adjacency matrix and its rows are

$$ \mathbf{A}= \begin{bmatrix} 0&1&1\\ 0&0&1\\ 1&0&0 \end{bmatrix}, \qquad r_1=(0,1,1),\quad r_2=(0,0,1),\quad r_3=(1,0,0). $$

In \(\mathbf{A}^2=\mathbf{A}\mathbf{A}\), row \(1\) is \(r_2+r_3\), row \(2\) is \(r_3\), and row \(3\) is \(r_1\). Hence

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

For \(\mathbf{A}^3=\mathbf{A}^2\mathbf{A}\), row \(1\) is \(r_1+r_3=(1,1,1)\), row \(2\) is \(r_1=(0,1,1)\), and row \(3\) is \(r_2+r_3=(1,0,1)\). Therefore

$$ \mathbf{A}^3= \begin{bmatrix} 1&1&1\\ 0&1&1\\ 1&0&1 \end{bmatrix}. $$

For instance, \((\mathbf{A}^3)_{12}=1\): there is exactly one length-\(3\) walk from \(1\) to \(2\), namely \(1\to3\to1\to2\). The entry \((\mathbf{A}^3)_{33}=1\) counts \(3\to1\to2\to3\).

Check: row \(1\) of \(\mathbf{A}^2\) is \((1,0,1)\). Multiplying it by column \(2\) of \(\mathbf{A}\), which is \((1,0,0)^{\top}\), gives \(1\cdot1+0\cdot0+1\cdot0=1\), confirming the \((1,2)\) entry of \(\mathbf{A}^3\).

Undirected Networks and Total Counts

For an undirected network, each edge can be traversed in either direction. Its adjacency matrix is symmetric: \(a_{ij}=a_{ji}\). Matrix powers still count walks by the same theorem; symmetry adds a useful relation between walks in opposite directions.

Proposition: If \(\mathbf{A}\) is the adjacency matrix of a finite undirected network, then for every \(k\geq 0\), \((\mathbf{A}^{k})_{ij}=(\mathbf{A}^{k})_{ji}\).

Proof. Reversing the order of the vertices and traversed edges in any length-\(k\) walk from \(i\) to \(j\) gives a length-\(k\) walk from \(j\) to \(i\). Because every edge in an undirected network can be traversed in either direction, the reversed sequence is allowed. Reversing again recovers the original walk, so this correspondence is a bijection. It preserves distinct choices of parallel edges, since the same chosen edges are used in reverse order. The two sets of walks therefore have the same number of elements. By the walk-counting theorem, those numbers are \((\mathbf{A}^{k})_{ij}\) and \((\mathbf{A}^{k})_{ji}\), respectively. For \(k=0\), the equality also follows from the identity matrix. \(\square\)

Worked Example: Walks on a Three-Vertex Path

Take the undirected path with edges joining \(1\) to \(2\) and \(2\) to \(3\). Calculate the number of length-\(3\) walks from \(1\) to \(2\).

The adjacency matrix has rows \(r_1=(0,1,0)\), \(r_2=(1,0,1)\), and \(r_3=(0,1,0)\). Multiplying \(\mathbf{A}\) by itself, row \(1\) of \(\mathbf{A}^2\) is \(r_2\), row \(2\) is \(r_1+r_3\), and row \(3\) is \(r_2\):

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

Now multiply \(\mathbf{A}^2\mathbf{A}\). Row \(1\) is row \(1\) of \(\mathbf{A}\) plus row \(3\) of \(\mathbf{A}\), giving \((0,2,0)\). Row \(2\) is twice row \(2\) of \(\mathbf{A}\), giving \((2,0,2)\). Row \(3\) is again \((0,2,0)\). Thus

$$ \mathbf{A}^3= \begin{bmatrix} 0&2&0\\ 2&0&2\\ 0&2&0 \end{bmatrix}. $$

There are two length-\(3\) walks from \(1\) to \(2\): \(1\to2\to1\to2\) and \(1\to2\to3\to2\). The entry \((\mathbf{A}^3)_{12}=2\) agrees with this count, and the symmetric entry \((\mathbf{A}^3)_{21}=2\) agrees with the proposition.

Check: row \(1\) of \(\mathbf{A}^2\) is \((1,0,1)\). Its product with column \(2\) of \(\mathbf{A}\), \((1,0,1)^{\top}\), is \(1\cdot1+0\cdot0+1\cdot1=2\), as required.

Corollary (Total walks): For a finite network with adjacency matrix \(\mathbf{A}\), the number of length-\(k\) walks starting at vertex \(i\), with any destination, is the sum of the entries in row \(i\) of \(\mathbf{A}^k\). The total number of length-\(k\) walks in the network is the sum of all entries of \(\mathbf{A}^k\).

Proof. For each destination \(j\), the walk-counting theorem says that \((\mathbf{A}^k)_{ij}\) counts walks from \(i\) to \(j\). Walks from \(i\) have exactly one destination, so adding these counts over \(j=1,\dots,n\) counts each walk starting at \(i\) exactly once. Adding that row sum over all starting vertices counts every walk in the network exactly once, since each walk has exactly one starting vertex. \(\square\)

Exact Length, Not “At Most”

The power \(\mathbf{A}^k\) counts walks of exactly \(k\) steps. It does not include shorter walks automatically. For example, the identity matrix \(\mathbf{A}^0\) counts only the length-\(0\) walks; it does not count the length-\(1\) walks recorded by \(\mathbf{A}\). If a question asks for walks of lengths from \(0\) through \(k\), the relevant matrix is the sum \(\mathbf{I}_n+\mathbf{A}+\cdots+\mathbf{A}^k\): each power contributes the counts for its own exact length.

A common error is to read the adjacency entry \(a_{ij}=2\) for two parallel edges as one walk because there is only one vertex sequence \((i,j)\). That calculation uses a definition that forgets which edge was traversed. Under the edge-choice definition, there are two length-\(1\) walks, one for each edge, and the matrix entry correctly equals \(2\). A separate error is to interpret \((\mathbf{A}^2)_{ij}\) as the number of walks of length at most \(2\). It counts only length \(2\); shorter lengths must be added as separate powers.

These interpretations are useful whenever a question asks how many ways a network can be traversed in a fixed number of steps. The matrix power combines all possible intermediate vertices without requiring the walks to be listed individually. For small networks, direct multiplication makes the count visible; for larger powers, the same rule applies, with each entry retaining its exact meaning.

Check Your Understanding

Use the edge-choice definition of a walk and the row-source, column-destination convention in each question.

  1. A directed network has three parallel edges from \(1\) to \(2\) and two edges from \(2\) to \(3\). How many length-\(2\) walks go from \(1\) to \(3\)?
  2. For an adjacency matrix \(\mathbf{A}\), what does \((\mathbf{A}^0)_{ij}\) count when \(i=j\), and when \(i\ne j\)?
  3. Why does the matrix product sum over intermediate vertices when counting length-\((k+1)\) walks?
  4. In the undirected three-vertex path example, how many length-\(2\) walks go from \(1\) to \(3\)? Read the answer from \(\mathbf{A}^2\) and describe the walk.
  5. If a problem asks for walks of lengths \(1\), \(2\), or \(3\), which matrix expression combines those counts?

Test yourself: Linear Algebra final exam practice

Ready to check what you know? Work a full-length practice final, then open the complete worked solutions.

Want all 1,114 practice problems?

This tutorial covers one topic. Calc Pass covers the full Calculus AB & BC curriculum (the core of Calculus I and II): full review, every practice question, and step-by-step solutions to each one.

QR code to download the Calc Pass app

Get the Calc Pass App

Scan to download and practice on the go, with instant scoring.

Calc Pass Volume 1: 1,114 Practice Questions book cover

Vol. 1: 1,114 Questions

The complete practice question bank, organized by AB & BC topic.

View on Amazon
Calc Pass Volume 2: Solutions Manual book cover

Vol. 2: Solutions Manual

Fully worked solutions to every question in Volume 1.

View on Amazon