Graphs and Shortest Path

A table describes things: one row per customer, per product, per airport. Some questions are not about the things at all but about what lies between them. How do I get from Atlanta to Sydney, and with how many changes? Which pages can be reached from the home page? If this road closes, is the town cut off? None of these is a filter, a join, or a group-by. They are questions about connections, and the structure for them is a graph: a set of nodes and a set of edges between pairs of them, where an edge may have a direction and may carry a weight. Often the hardest step is the first — recognising that a word puzzle, a course catalogue, and an airline timetable are the same problem. An edge list is the natural form for a file and a poor one to ask; an adjacency list, a dictionary mapping each node to its neighbours, puts every node’s connections one lookup away, provided every node is a key.

The basic operation is a search that spreads outward from a start. Breadth-first search works in rings: everything one step away, then everything new that is two steps away. A queue produces that order by itself, and because nearer rings finish first, the first time a node is reached is by a route with the fewest steps. Three details decide whether it works: take from the front of the queue, skip what has been seen, and record a node when it joins the queue. One “came from” note per node recovers the path itself; shortest paths are not unique, and all of them are correct. The nodes a search reaches form a connected component, and looking at the components is one of the first things to do with any real network. Finally, fewest steps is not shortest distance. When edges have lengths the search must take the nearest node next, which is Dijkstra’s algorithm, and what “shortest” means becomes a modelling decision made by what is put on the edges.

Figure: Graph Traversal and Shortest Path

Listen

How Graph Algorithms Find Shortest Paths

Presentation

Read

Hands-on

Notebooks in 12-Graphs-Shortest-Path

Homework