How can matrices store the connections in a network and count routes between vertices?
Represent networks with adjacency matrices and use matrix powers to count walks of a given length between vertices.
How to build an adjacency matrix from a network, read direct connections, and use powers of the matrix to count the number of two-step and longer walks between vertices.
Reviewed by: AI editorial process; not yet individually human-reviewed
Have a quick question? Jump to the Q&A page
Jump to a section
What this dot point is asking
You must build an adjacency matrix from a graph, interpret its entries, and use or higher powers to count multi-step connections.
Building an adjacency matrix
A network (graph) has vertices joined by edges. Its adjacency matrix is square, with one row and one column per vertex. Each entry counts the edges joining that pair of vertices.
For an undirected network the matrix is symmetric (a link from to is also a link from to ). For a directed network the entry in row , column counts only edges pointing from to , so the matrix need not be symmetric.
Counting two-step walks with matrix powers
A walk of length 2 uses two edges, passing through one intermediate vertex. The entry of in row , column counts exactly these two-edge walks from to .
Reading the diagonal and the row sums
The diagonal entries of count two-step walks that return to the starting vertex. In the example each diagonal entry is 2, because each town can reach two others and come straight back, giving two such loops.
Row sums of are also useful: in an undirected network the sum of row gives the degree of vertex , the number of edges meeting it.
Direct plus indirect connections
A common task is to add matrices to combine route lengths. For example, gives the number of routes of length 1 or 2 between each pair of vertices, useful for asking "in how many ways can I get from P to R in at most two steps?"