2.4 Matrix algebra and the product of adjacency matrices — Learn
Prerequisites: Section 2.3 — adjacency matrices; multiplying whole numbers.
Objectives: multiply two matrices; explain what a power of an adjacency matrix counts; read the number of walks of a given length off a matrix power.
By the end of this section you can answer a question about routes by arithmetic on a matrix, without looking at the drawing at all.
Multiplying two matrices
To multiply matrix by matrix , take a row of and a column of : multiply the pairs, then add.
Here runs over every column of the first matrix, which is every row of the second.
The entry in row , column of the product is row of the first matrix times column of the second.
Example 1. Work out .
- Row 1, column 1:
- Row 1, column 2:
- Row 2, column 1:
- Row 2, column 2:
Matrix multiplication is not commutative: and are usually different, so the order matters. (Here is the same, which is a special case.)
What a power of an adjacency matrix counts
Now the idea the whole section is for.
The walk-counting rule. In an adjacency matrix , the entry in row , column of is the number of walks of length from vertex to vertex .
A walk of length uses edges, and vertices may repeat.
Why it is true. Look at first. A walk of length 2 from to goes for exactly one middle vertex . The walk exists when is joined to and is joined to — that is, when and , so their product is . The product of the two matrices adds that product over every possible middle vertex :
So it counts exactly the middle vertices that work, which is exactly the number of walks. The same argument repeats for , and for every higher power.
Example 2. A network has vertices and edges , , , , so
Then
Read three facts off it.
- The entry in row , column is , so there are 2 walks of length 2 from back to : and .
- The entry in row , column is , so there is exactly 1 walk of length 2 from to : . There is no edge , so you must go through a middle vertex, and only is joined to both.
- The diagonal entries count the walks that return to where they started. A vertex of degree has such walks of length 2, because it can go out along any edge and back along the same one. has degree , and indeed .
Higher powers
The rule holds for every power.
- counts walks of length 1 — the edges themselves.
- counts walks of length 2.
- counts walks of length .
Example 3. For the same ,
so there are 4 walks of length 3 from to , and 1 from to .
Two useful checks:
- The total of all the entries of is the number of walks of length in the whole network, starting anywhere and ending anywhere.
- The entry on the diagonal of is the degree of that vertex. If it is not, the matrix has been multiplied wrongly.
Progress check
Back to the chapter problem. Take your seven-room adjacency matrix from Section 2.3 and work out .
Use it to answer one question that the drawing makes awkward: how many ways are there to travel from one particular room to another using exactly two cable runs? Read the entry in that row and column. Then check one entry against the drawing by hand, to be sure the rule is doing what it claims.
Keep and . Section 2.8 searches the weighted version of this network for the shortest route, which is a different question from how many routes there are.
You should now be able to
- multiply two matrices, and say why the order matters;
- state the walk-counting rule and explain why it is true for ;
- use a power of an adjacency matrix to count walks of a given length;
- use the diagonal of to check your multiplication;
- say why the rule counts walks and not paths, and why that matters when you read a question.