Skip to main content

Week 2: Dijkstra: when to stop

Tentative distances, settled nodes and relaxation; why the goal being discovered is not the goal being found.

1 March 2027

1011SAG
Symbolic graph: S→G costs 10; S→A→G costs 1+1=2. Discovering G first does not finish the search.

Last week we defined legal routes. Now we need a method that can find the cheapest one without guessing which street looks promising.

What you will learn

Model: a fixed directed graph with non-negative edge times. No battery or shared-resource constraint yet. A symbolic example comes first; the tutorial then offers kitchen → #03 on the canonical map.

Tentative is not final

The frontier, also called OPEN, contains routes waiting to be considered. best[v] is the lowest cost found so far to node v. Relaxing an edge u→v means checking whether reaching v through u is cheaper than its current best route.

candidate = best[u] + cost(u, v)
if candidate < best[v]:
    best[v] = candidate
    parent[v] = u
    put (v, candidate) into OPEN

A priority queue lets us remove the entry with the smallest cost. An old queue entry may remain after a better one is inserted; skip it if its cost exceeds best[v].

Why the first sight of G is misleading

Use S→G=10, S→A=1 and A→G=1. Costs are symbolic teaching units.

StepEntry removedChangeOPEN after the step
Start—best[S]=0S:0
1S:0Discover G at 10 and A at 1A:1, G:10
2A:1Improve G from 10 to 2; parent[G]=AG:2, G:10
3G:2Accept the goalStop

Stopping at step 1 returns 10. Removing G’s best entry returns 2. Following parents backwards gives G←A←S; reverse that list to report S→A→G.

Why we can trust that removal

Suppose a cheaper route to the removed node still existed. It would have to cross from an already processed node to a frontier node. With non-negative edges, that frontier route would have a smaller cost and would have been removed first. This contradicts the queue choice. Negative edges break this argument.

Stop when the goal’s non-stale entry is removed, not when the goal is first generated. An empty queue means no path in this graph. A search budget ending early means only that this run stopped searching.

Read and try

Read the Dijkstra section of Modern Robotics: Graph Search. In the tutorial, predict the table before enabling the wrong stop rule. Implement the same loop in mySearch; test source=goal, unreachable goal, an improved route and equal-cost alternatives. W3 keeps this loop and changes the queue priority.

Work through one example

Small symbolic route at the kitchen; non-negative costs, no energy constraint.

Can we stop when G is first discovered?

Symbolic directed costs: S→G = 10, S→A = 1, A→G = 1. Start at S with g(S) = 0.

Check the reasoning

After S, OPEN contains A:1 and G:10. Pop A and improve G to 2. Pop G at 2 to finish. Stopping when S first discovers G would incorrectly return 10.

Teaching team