148. The adjacency matrix of a digraph
A convenient way for representing a digraph $G$ in a computer program is by means of its adjacency matrix.
148.1 Definition: adjacency matrix
The adjacency matrix of a digraph $G$ is a matrix of nonnegative integers whose entries are indexed by $G_0\times G_0$ and whose entry in the location indexed by the pair of nodes $\langle x,y\rangle$ is the number of arrows from $x$ to $y$.
148.1.1 Example For the left digraph in Figure 144.1 the adjacency matrix is
$$\begin{array}{c|cccc} & x & y & z & w \\ x & 0 & 2 & 1 & 0 \\ y & 0 & 1 & 0 & 0 \\ z & 1 & 1 & 0 & 0 \\ w & 0 & 0 & 0 & 0 \end{array}$$
148.1.2 Remark The adjacency matrix depends on the way the nodes are ordered; thus if you permute the nodes you get a different adjacency matrix for the same graph. Note that the adjacency matrix does not contain the information concerning the names of the arrows.
148.1.3 Exercise Draw the graph with this adjacency matrix:
$$\begin{array}{c|cccc} & 1 & 2 & 3 & 4 \\ 1 & 0 & 1 & 1 & 1 \\ 2 & 0 & 0 & 1 & 1 \\ 3 & 0 & 1 & 0 & 1 \\ 4 & 1 & 0 & 0 & 0 \end{array}$$
(Answer on page 251.)
148.1.4 Exercise Give the relational description of the digraph in Exercise 148.1.3. (Answer on page 251.)
148.1.5 Uses of the adjacency matrix You can use the adjacency matrix of a graph to determine properties of the graph:
(i) It is simple if no entry in the adjacency matrix is greater than 1.
(ii) It has no loops if the entries down the main diagonal (the one from upper left to lower right) are all 0.
(iii) The outdegree of a node is the sum over its row and the indegree is the sum over its column.
The adjacency matrix will be used in the next section to calculate which nodes can be reached from a given node.
148.1.6 Exercise Give the adjacency matrices of the digraphs in Figure 147.1. (Answer on page 251.)
148.1.7 Exercise Draw this digraph and give its adjacency matrix: The nodes are the numbers 1,2,3,4,6,12 and there is an arrow from $a$ to $b$ if and only if $a$ and $b$ have the same prime factors (in other words, for all primes $p$, $p\mid a\Leftrightarrow p\mid b$).
Pages 224–225 of the book (PDF).