Episode 11
The Short Way to the Bandstand
The robot and duck look through the map level by level and learn when the shortest path is discovered with a breadth first search.




















Episode transcript
Count links to the bandstand
START S. TARGET D. ONE LINK = ONE EQUAL STEP
Duck: What's the route with the fewest number of links?
Robot finds route from S via A to C to D on the map. There are 3 links.
Robot: Hmmm, this one is a nice looking one
For the purpose of this problem, one equal step is one map link.
Duck: Let us count the links, not trust the drawing.
There are five places S A B C D on the map. There are undirected edges on the map S-A, S-B, A-C, B-D, C-D.
LINKS: S-A, S-B, A-C, B-D, C-D
Robot: We are going to look one level at a time.
One level at a time
SEEN S. QUEUE [S]
Duck: Start at S.
Robot has seen A and B and queues A before B. Both A and B are one link from S.
DEQUEUE S → QUEUE [A,B]. DISTANCE A=B=1
Robot: A and B are one step away
Robot takes A from the queue, sees its already-seen neighbor S, then sees C through A.
DEQUEUE A → QUEUE [B,C]. SEEN C. PARENT[C]=A
Duck: C follows B.
Robot takes away B, has already seen S, finds D via B.
DEQUEUE B → QUEUE [C,D]. SEEN D. PARENT[D]=B
Robot: D is 2 links away from our start
Keep the search from circling
D is already marked and queued, so it does not add duplicate D.
DEQUEUE C → QUEUE [D]. D ALREADY SEEN
Duck: D is already marked.
MARK SEEN WHEN ENQUEUED; DO NOT ENQUEUE AGAIN
Robot: We do not need to queue them twice.
Robot takes D away. Robot sees all its neighbors. There is no waiting stop left. The queue is empty.
DEQUEUE D → EMPTY
Duck: That ends the search.
The path from S to B to D has 2 links. From S to A to C to D has 3 links.
BACKTRACK: D → B → S. ROUTE: S → B → D. TWO LINKS.
Robot: 2 links to the bandstand
Start from somewhere new
D is still the target. Previous seen marks are erased to restart the search.
FRESH SEARCH: START A. CLEAR OLD SEEN MARKS
Duck: Okay now start from A
FIRST LEVEL FROM A: S,C
Robot finds A-C-D. The other route A-S-B-D has 3 links.
A-C-D: 2 LINKS. A-S-B-D: 3 LINKS
Robot: A to C to D
Links and road lengths
Duck: what if one road is significantly longer?
FEWEST LINKS ≠ SHORTEST DISTANCE WHEN ROAD LENGTHS DIFFER
Robot: It doesn't necessarily mean the shortest path is the one with least number of links
Duck: There's our equal step map answer.
Robot: Let's go
Breadth-first search
GRAPH NODES: STOPS. GRAPH LINKS: CONNECTIONS BETWEEN STOPS.
Use a fifo queue. Flag a stop as seen when you put it in. Note which stop found it.
In a fifo (first-in, first-out) queue, new stops are added at the end of the queue after existing stops. Stops of one link are checked before stops of two links, stops of two links before stops of three links, etc. Neighbors are found for the stop, so the route to a new neighbor is one link more than for the current stop.
EQUAL UNWEIGHTED LINKS → FIRST DISCOVERY USES THE FEWEST LINKS
You can end a full search when queue is empty. Or if you've got one destination in mind, you can end when you find it.
QUEUE EMPTY + DESTINATION UNSEEN → NO ROUTE FROM THIS START
UNEQUAL DISTANCES OR COSTS: FEWEST LINKS IS NOT A SHORTEST-JOURNEY GUARANTEE
Try B to C
SAME UNDIRECTED MAP. START B. TARGET C. FROM B: VISIT S BEFORE D.
Answer
C is discovered in the time when D is being processed. So the search for the destination stops. A is left in the queue and is not processed.
QUEUE: [B] → [S,D] → [D,A] → [A,C]. DISCOVER C AND STOP.
BACKTRACK: C → D → B. ROUTE: B → D → C. TWO LINKS. ALTERNATIVE: B → S → A → C. THREE LINKS.
If there are 2 paths with the same number of links neighbor order may choose either path.
Sources
NIST Dictionary of Algorithms and Data Structures, breadth-first search (opens in a new tab)
Sedgewick and Wayne, Algorithms: BreadthFirstPaths (opens in a new tab)