Log inStart free
LESSON 3.C1 · 6 STAGES · 3 CONTROLS · 3 CHECKSPRO · PREVIEW

Four searches on one graph

Watch four searches cross one 12-node graph, and see what each one finds.


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.C1 · SEARCH BAY12 NODES · 16 EDGES · F TO D
01
Stage 1 of 6 · One graph
Each search walks this graph from F to D. It takes nodes from a frontier, each search in its own order.
Explain it simply

You see 12 nodes, the circles, joined by edges, the lines. Each number is the edge's weight, the cost to cross it. The grey bar is a wall, so no edge crosses it. Every search starts at F and looks for D. It keeps a list of nodes to visit next, called the frontier.

A grid of 12 nodes, A to L, with 16 edges. The start is F and the goal is D. BFS finds the path F, G, H, D, 3 edges at cost 7.

not reachedfrontier (ring), expanded, gedge weight, guess hpath foundextra work or costwall
graph · 12 nodes · 16 edges · F to Done loop · 4 orders1121311151112212ABCDgoalEFstartGHIJKLfrontier = [F]while frontier: u = take next if u == D: stop add u's neighboursBFSqueue · oldest firstDFSstack · newest firstDijkstralowest g firstA*lowest g + h firstcost(F→G→H→D) = 5 + 1 + 1 = 7graph · 12 nodes · 16 edges · F to Done loop · 4 orders1121311151112212ABCDgoalEFstartGHIJKLfrontier = [F]while frontier: u = take next if u == D: stop add u's neighboursBFSqueue · oldest firstDFSstack · newest firstDijkstralowest g firstA*lowest g + h firstcost(F→G→H→D) = 5 + 1 + 1 = 7
Toy: 4 × 3 grid · 12 nodes · start F · goal D
STAGE 1 / 6
MATH · STAGE 1HOVER A SYMBOL OR A SHAPE
cost(P)=∑(u,v)∈Pw(u,v)\text{cost}(\sy{P}{out}{P}) = \sum_{(u,v) \in P} \sy{w}{param}{w(u,v)}
cost(F ⁣→ ⁣G ⁣→ ⁣H ⁣→ ⁣D)=5+1+1=7\text{cost}(F\!\to\!G\!\to\!H\!\to\!D) = 5 + 1 + 1 = \sy{P}{out}{7}

A path costs the sum of its edge weights.

Common mix-ups

MYTHBFS always finds the cheapest path.

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

Predict, then reveal
3 CHECKS · PRO
Q01If every edge weight is 1, is the BFS path as cheap as the Dijkstra path?

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.