Search HoldMyTwist

Browse all posts

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.

Episodes

Computing Basics

    Robot holds a map beside the portable aqua monitor. Duck points toward the parade bandstand.
    Robot traces route S to A to C to D on the map. This route has three links.
    Duck counts three link tokens along that route. Each map link counts as one equal step for this problem.
    The map has five stops S, A, B, C and D. Its undirected links are S-A, S-B, A-C, B-D and C-D.
    Robot marks start S as seen and puts it in the queue. No other stop is marked yet.
    Robot opens empty pincers beside two settled queue counters; Duck watches from inside the CRT. The exact graph below shows seen S, A and B, with queue A then B.
    Robot points with empty pincers at two settled counters. Duck points from inside the CRT. The exact graph shows seen S, A, B and C, with queue B then C.
    Robot steadies a tray holding two counters while Duck gestures inside the CRT. The exact graph shows all five nodes seen, with queue C then D.
    Robot removes C and checks its neighbor D. D is already marked and already queued, so no duplicate D is added.
    The cycle S-A-C-D-B-S is visible on the map. Robot points to marked stops rather than adding them again.
    Robot removes D. All its neighbors are already seen and no waiting stop remains. The queue is empty.
    Robot follows D’s discovery link back through B to S. The route S-B-D has two links, while S-A-C-D has three.
    Duck moves the start flag from S to A on the same map. D remains the destination. The old seen marks are cleared for a fresh search.
    Starting A is marked and queued, then removed. Its neighbors S and C are marked and queued in that order, each one link from A.
    The unchanged graph remains visible and the second search has not revealed its answer.
    In the fresh search, D is discovered through C at distance two. Robot highlights A-C-D. The alternative A-S-B-D has three links.
    Duck stretches one map link into a much longer ribbon while leaving the number of links unchanged.
    Robot holds equal link tokens beside unequal road lengths.
    Robot folds the map after identifying the two-link original route S-B-D. Duck points toward the bandstand from the monitor.
    Robot pushes the monitor trolley toward the parade bandstand. Duck looks pleased inside the screen.

    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)