Can the longest line on a road network show a quicker trip?
You're leaving Galway and want to get to Tuam. You could take the direct road or go through Athenry. A diagram can make one road look much longer than another. What would you need to know before calling one route quicker?
Start with a sketch map. Find Galway, Athenry and Tuam. You can go from Galway straight to Tuam or take one road to Athenry and another from there to Tuam. At this stage, just look at which towns have a direct road between them.
Replace Galway, Athenry and Tuam with dots G, A and T. Each dot is a vertex, or node, and each road becomes an edge. Follow G to A to T. That's a path with no repeated vertex. Take the edge back to G to complete a cycle.
Look at the link joining G and A. What does that single edge tell you about Galway and Athenry? Pick the statement you can justify from the graph alone.
Read the sample weights: Galway to Athenry, twenty-five minutes; Athenry to Tuam, twenty; Tuam to Galway, thirty-five. What does each weight measure? It gives the travel time in minutes. The longest line shows twenty-five minutes, less than the shorter line's thirty-five. Use the labels to compare travel times.
Each arrow shows a one-way road, so this graph is directed. A directed edge is called an arc. Oranmore, marked O, has no links, so the graph is disconnected even if you ignore the arrows. Our earlier graph was undirected, with two-way roads, and connected, with a path between every pair of towns.
Call the three-town adjacency matrix M. Rows and columns list towns in the same order. In Galway's row, zero means no link to itself, and ones mark links to Athenry and Tuam. To redraw the graph, place the three vertices, then join pairs with a one, drawing each undirected edge once.
To square M, multiply matching entries from a row and a column, then add. Row G with column A gives one: only going via T works. Row G with column G gives two ways back. Repeat for every entry. The result counts two-edge walks, where vertices can repeat.
Suppose only the two-way roads from Galway to Athenry and from Athenry to Tuam remain. In the new adjacency matrix, what do you get when you multiply row G by column G? Pick the entry for G to G in the squared matrix.
Add Oranmore as a fourth town with no roads in this model. Enter the matrix in NumPy and use the at sign to multiply it by itself. If asked to interpret the first row, second column of the product, say there's one two-road walk from Galway to Athenry.