How many ways can you get from Galway to Salthill if you must take exactly two buses?
You're going from Galway to Salthill on a model bus network. You have to take exactly two bus legs. How many choices do you have for the stop where you change buses?
Each vertex, or node, is a stop: G for Galway, O for Oranmore, M for Moycullen and S for Salthill. Each edge is a direct link. There are no arrows, so you can use each link both ways. The graph is undirected and connected: you can get between any two stops.
Use the order shown for both rows and columns. Read each entry from its row stop to its column stop. A one means draw a link; a zero means none. The two ones for Galway and Oranmore show the same link both ways. This matrix records links without weights such as travel times.
Start at G for Galway and aim for S for Salthill. Trace two edges through the orange stop, Oranmore. Then trace two edges through the blue stop, Moycullen. Each coloured stop gives one walk of exactly two edges because both legs exist.
You're leaving Galway and want to arrive in Salthill after exactly two legs. Which list gives all the stops where you could change buses? Pick one answer.
Multiply row G by column S, pairing entries in the order Galway, Oranmore, Moycullen, Salthill. Each pair tests the two legs through that stop. The products are zero, one in orange, one in blue, then zero. Add them: the G, S entry of A squared is two.
Now your destination is Oranmore. Keep the same service and start in Galway again. How many walks reach Oranmore in exactly two edges? Think about where each first edge leaves you, then pick an answer.
Service B has one directed arc, from Oranmore to Salthill. Galway and Moycullen are isolated, so B's network is disconnected. Use the same stop order in both matrices. In A B, take A first and B second. Only the orange product is one, so only the walk through Oranmore counts.
You might think you can't revisit Galway. But out through Oranmore and back is a two-edge walk, even though you could stay put. Galway through Oranmore to Salthill is a path with no repeated vertices. For a cycle, continue through Moycullen to Galway, repeating only the start at the end.