Could you tell which trains run directly between stations without seeing the map?
Suppose you're looking at a small model of an Irish Rail network. The map has been replaced by a table. Could you tell whether there's a direct train from one station to another, or whether you'd have to change?
Each station is a vertex, also called a node. Each arrow is a directed arc. Follow the arrows from A to B and then to C. That's a path of two arcs. Follow the last arrow from C back to A and you've made a cycle.
Follow the arrow from C to A. Find row C, where it starts, and column A, where it ends. Write one where they meet because there's one direct arc from C to A. The square table that holds all these counts is called an adjacency matrix.
In a different network, two arcs go from L to C, and one goes from C back to L. Find row L and column C. Which number belongs in that position? Pick an answer.
Use A, B, C for both rows and columns. Row A has one under B. If the journey takes six hundred seconds, that time is the arc's weight. Leave it out of the count. Row B has one under C. Row C has one under A. Stack the rows to form M.
This time you get the table first. The row and column order is R, P, Q, S. Only the third row is non-zero. It reads one, one, zero, zero. Which two arrows would you draw? Pick an answer.
Draw R, P, Q and S first. Row Q has a one under R and another under P. Draw Q to R, then Q to P. Every other entry is zero. S has no links, so the graph is disconnected even if you ignore the arrow directions.
Now R and Q share a single undirected edge. You can follow it either way, so there's a path between the two vertices and the graph is connected. Count the edge in both directions. With matching row and column order, the ones mirror each other across the main diagonal. The matrix is symmetric.
For A to C in M squared, take row A and column C. Multiply matching entries and add. Each product counts walks through one middle vertex. Only B gives one, so there's one two-arc walk. Repeat for every entry. A walk can revisit a vertex.
An exam question might ask you to use the adjacency matrix to find the number of walks of length two. For a larger graph, you can use Python with NumPy. Import NumPy, enter the rows in the given vertex order, then use the at sign to multiply M by itself.