151. Directed walks and matrices

151.1 Theorem

If $G=(G_0,G_1,s,t)$ is a digraph with adjacency matrix $M$, then the number of directed walks of length $k$ from node $p$ to node $q$ is the $\langle p,q\rangle$th entry of $M^k$.

Proof This fact can be proved by induction on $k$. It is clear for $k=1$, since a directed walk of length 1 is just an arrow, and the $\langle p,q\rangle$th entry in $M^1=M$ is the number of arrows from $p$ to $q$ by definition.

Suppose it is true that for all nodes $p$ and $q$, the $\langle p,q\rangle$th entry of $M^k$ is the number of directed walks of length $k$ from $p$ to $q$. A directed walk of length $k+1$ from $p$ to $q$ is a directed walk of length $k$ from $p$ to some node $r$ followed by an arrow (directed walk of length 1) from $r$ to $q$. By the induction hypothesis, there are $(M^k)_{pr}$ directed walks of length $k$ from $p$ to $r$, and there are $M_{rq}$ arrows from $r$ to $q$. Hence the number of directed walks of length $k+1$ from $p$ to $q$ that consist of a directed walk of length $k$ from $p$ to $r$ followed by an arrow from $r$ to $q$ is $(M^k)_{pr}\times M_{rq}$. The total number of directed walks of length $k+1$ from $p$ to $q$ must be obtained by adding up this number $(M^k)_{pr}\times M_{rq}$ for each node $r$ of the

Walk with a loop: a walk with arrows a_(r-3), a_(r-2), a_(r-1), a_(r+s-1), a_(r+s+2), a_(r+s+3), containing a circuit a_r, a_(r+1), a_(r+2), ..., a_(r+s-2), a_(r+s-1), a_(r+s). Figure 151.1: Walk with loop.

digraph; in other words, if there are $n$ nodes in the digraph, the total number of walks is

$$\Sigma_{r=1}^{n}\Bigl((M^k)_{pr}\times M_{rq}\Bigr) \tag{151.1}$$

That sum, by formula (150.1), is the $\langle p,q\rangle$th entry of $M^{k+1}$, which is $M^k\times M$, and that is what we had to prove.

151.2 Reachability

Let $p$ and $q$ be nodes of a digraph $G$. One says that $q$ is reachable from $p$ if there is at least one directed walk of some length (possibly zero) from $p$ to $q$.

Since a directed walk of length $k$ touches $k+1$ nodes, it follows from the pigeonhole principle that a directed walk of length $n$ or more in a digraph $G$ with $n$ nodes must touch some node twice. Suppose such a walk $\langle a_1,\ldots,a_k\rangle$ touches a node $x$ twice; say arrow $a_r$ has source $x$ and arrow $a_{r+s}$ (with $s\geq 0$) has target $x$. Then the directed walk $\langle a_r,\ldots,a_{r+s}\rangle$ can be eliminated from the walk, as in Figure 151.1, giving

$$\langle a_1,\ldots,a_{r-1},a_{r+s+1},\ldots,a_k\rangle \tag{151.2}$$

from $p$ to $q$. (Note: if $r=1$ or $r+s=k$, the walk (151.2) has to be modified in an obvious way.)

Clearly, by successively eliminating circuits, one can replace the walk by a path (not just a walk) of length $<n$. This leads to:

151.3 Corollary

Let $G$ be a digraph as in Theorem 151.1 with $n$ nodes and matrix $M$. Then $q$ is reachable from $p$ if and only if the $\langle p,q\rangle$th entry of the matrix $K=I+M+M^2+\ldots+M^{n-1}$ is nonzero.

Proof If there is a directed walk from $p$ to $q$, then the argument before the statement of the Corollary shows that there must be one of length $n-1$ or less. This means that one of the matrices $M$, $M^2$, $\ldots$, $M^{n-1}$ has a nonzero $\langle p,q\rangle$th entry. Since all the entries in these matrices are nonnegative, this means that the $\langle p,q\rangle$th entry of $K$ is nonzero. Conversely, if that entry is nonzero it must be because the $\langle p,q\rangle$th entry in $M^i$ for some $i$ is nonzero.

151.3.1 Exercise Use matrix multiplication to find all the directed walks of length 1, 2, 3 and 4 that go from 1 to 3 in these digraphs:

Two digraphs. (a) nodes 1, 2, 3 with arrows a from 1 to 2, b and c from 2 to 1, d from 2 to 3, e from 1 to 3. (b) nodes 1, 2, 3, 4 with arrows 1 to 2, 1 to 3, 2 to 4, 3 to 4, 1 to 4, 4 to 1, 2 to 3, 3 to 2 (151.3)

(Answer on page 251.)

151.4 Definition: reachability matrix

The matrix

$$K=I+M+M^2+\ldots+M^{n-1}$$

is called the reachability matrix for the digraph $G$.

151.4.1 Exercise Calculate the reachability matrices for the digraphs in Figure 144.1, page 218. (Answer on page 251.)

151.4.2 Exercise Let $G$ be the digraph whose set of nodes is $\{1,2,3,4\}$, with an arrow from $a$ to $b$ if and only if $a$ is even and $b$ is 2 or 3. Find the reachability matrix of $G$ by counting paths and by direct addition and multiplication of matrices. (You may use Mathematica for the latter.)

151.4.3 Exercise Let $D$ be a digraph with adjacency matrix $M$. Show that $D$ is transitive (as defined in the preceding problem) if and only if

$$(M^2)_{ij}\neq 0\Rightarrow M_{ij}\neq 0$$

for all pairs $\langle i,j\rangle$.

Pages 228–230 of the book (PDF).