Log inStart free
LESSON 3.11 · 6 STAGES · 3 CONTROLS · 3 CHECKSPRO · PREVIEW

Shortest paths: Dijkstra and A*

Watch Dijkstra spread out from F, then watch A* aim at D.


Pro lesson. Stage 1 is free to watch. Pro opens all 6 stages, 3 controls and 3 checks, in this lesson and every other.

Go Pro · $8.25/mo
EXPLAINER · 3.11 · PATH BAYFEWEST-EDGE ROUTE · 3 EDGES · COST 7
01
Stage 1 of 6 · Weighted graph
Each edge of the weighted graph has a weight, its cost. The fewest-edge route costs 7.
Explain it simply

You see 12 nodes joined by 16 edges; the grey bar is a wall. You want the cheapest route from F, the start, to D, the goal. Each edge has a weight, like minutes of travel. The fewest-edge route crosses F–G, with weight 5, so it costs 7.

A 4 by 3 grid of 12 nodes, A to L, with 16 edges. F is the start and D is the goal. The fewest-edge route F, G, H, D costs 7.

not reachedwaiting or popped node, gedge weight, guess hpath and its costlowered g, extra work or costwall
graphroute cost1121311151112212ABCDgoalEFstartGHIJKLfewest-edge route F→G→H→D5 + 1 + 1 = 73 edges · cost 7graphroute cost1121311151112212ABCDgoalEFstartGHIJKLfewest-edge route F→G→H→D5 + 1 + 1 = 73 edges · cost 7
Toy: 4 × 3 grid · 12 nodes · start F · goal D
STAGE 1 / 6
MATH · STAGE 1HOVER A SYMBOL OR A SHAPE
cost(P)=∑e∈Pw(e)\sy{cost}{out}{\mathrm{cost}}(P) = \sum_{e \in P} \sy{w}{param}{w}(e)
cost(F→G→H→D)=5+1+1=7\mathrm{cost}(F \to G \to H \to D) = 5 + 1 + 1 = 7

A path's cost is the sum of the weights of its edges.

Common mix-ups

MYTHThe first route that reaches a node is its shortest route.

The fix, and 1 more mix-up with the diagram to show them, come with Pro.

Predict, then reveal
3 CHECKS · PRO
Q01With h = 0, does A* pop fewer nodes than Dijkstra?

Make your guess, then test it in the diagram. All 3 checks come with Pro.

PRO LESSON · YOU WATCHED STAGE 1 OF 6
Unlock the full explainer
  • 5 more stages and 3 live controls
  • 2 mix-up cards and 3 predict-then-reveal checks
  • Watch it as a narrated video, in three voices
  • Every Pro lesson and capstone · 656 in total

$99 a year, or $15 a month. Cancel any time.