Q1 — Search: BFS/A*/BnB

Search algorithms on a grid map

The Question

The figure shows a map on a uniform grid where each tile is 10x10 in size.

The start node is S and the goal node is G. The MoveGen function returns nodes in alphabetical order. Use Manhattan Distance as the heuristic function.

Tie-breaker: If several nodes have the same cost, use node labels to break the tie.

Node coordinates on the 10x10 grid (relative to origin):

  • S: (0, 20)
  • A: (20, 0)
  • B: (20, 20)
  • C: (40, 0)
  • D: (10, 40)
  • E: (30, 40)
  • F: (30, 60)
  • G: (50, 40)
#QuestionOfficial answer
1.1What is the path found by the Depth First Search algorithm? Enter the path as aS,A,C,G
1.2What is the path found by the Best First Search algorithm? Enter the path as a cS,D,F,G
1.3What is the path found by A* search algorithm? Enter the path as a comma separatS,B,E,G
1.4What is the path found by Branch-and-Bound search algorithm? Enter the path as aS,A,C,G
1.5For the given map, which algorithm finds the shortest path from S to G?B

The Topic in 60 Seconds

AlgorithmSorts frontier byIgnores
DFSdeepest node (dive alphabetically)all costs
Best Firsth(n)h(n)g(n)g(n)
A*f(n)=g(n)+h(n)f(n) = g(n) + h(n)nothing
Branch-and-Boundg(n)g(n) on whole pathsh(n)h(n)

Deep-dive: Depth First Search, Best First Search, A* Search, Branch and Bound.

Step 0 — Heuristic table

Manhattan units to G at (50, 40), scaled ×2 to match the edge-cost scale:

NodeΔx+Δy\Delta x + \Delta yhhNodeΔx+Δy\Delta x + \Delta yhh
S50 + 20 = 70140D40 + 0 = 4080
A30 + 40 = 70140E20 + 0 = 2040
B30 + 20 = 50100F20 + 20 = 4080
C10 + 40 = 50100G00

Any common positive scaling of these hh values produces the same picks below.

1.1 — Depth First Search → S,A,C,G

DFS dives into the alphabetically-first fresh neighbour and never re-enters a node already generated; the goal test fires the moment G is generated.

StepAction
1Expand S → MoveGen gives A, B, D (all new). OPEN = {A, B, D}. Dive into A
2At A: S closed, B already generated from S → skip. Fresh branch = C. Dive into C
3At C: A closed, E new, G new → generate G → goal test fires

Path: S → A → C → G. Note B and D sit unexpanded in OPEN forever — DFS never looks at them.

1.2 — Best First Search → S,D,F,G

Sort OPEN by hh only:

StepOPEN (node : hh)Pick
1S : 140S → A(140), B(100), D(80)
2D : 80, B : 100, A : 140D → F(80)
3F : 80, B : 100, A : 140F → E(40), G(0)
4G : 0, E : 40, B : 100, A : 140G — goal!

Path: S → D → F → G, cost 60+60+30+40=19060+60+30+40 = 190. Greedy on hh walks straight "towards" G geometrically — and straight into the expensive eastern route.

1.3 — A* Search → S,B,E,G

Sort OPEN by f=g+hf = g + h:

StepOPEN (node : ff)Pick
1S : 0+140 = 140S → A : 50+140 = 190, B : 40+100 = 140, D : 60+80 = 140
2B : 140, D : 140, A : 190B (tie B=D at 140 → labels) → A stays 190 (via B costs 210), D : 80+80 = 160, E : 90+40 = 130
3E : 130, D : 160, A : 190E → F : 120+80 = 200, G : 130+0 = 130, C : 150+100 = 250
4G : 130, D : 160, A : 190, F : 200, C : 250G — goal!

Path: S → B → E → G, cost 40+50+40=13040+50+40 = 130.

Interactive trace (labels show the current best ff):

1 / 5
h: S=140, A=140, B=100, C=100, D=80, E=40, F=80, G=0 OPEN = { S(g=0, f=0+140=140) }
expandingin OPEN (f = g+h)expandedreturned path

1.4 — Branch-and-Bound → S,A,C,G

Pop whole paths by gg; discard any path reaching a node already cheaper reached:

PopNew paths (path : gg)
S : 0SB : 40, SA : 50, SD : 60
SB : 40SBA : 70, SBD : 80, SBE : 90
SA : 50SAC : 70 (SAB : 80 discarded — B already at 40)
SD : 60SDF : 120 (B via D = 100 discarded)
SAC : 70 (tie with SBA at 70 → labels: S-A-C first)SACG : 100 ← goal queued
SBA : 70nothing survives (A@70 > 50, D@110 > 80)
SBD : 80SBDF : 140
SAB : 80nothing
SBE : 90SBEF : 120, SBEG : 130, SBEC : 150
SACG : 100 — cheapest left in OPEN is 120complete → optimal, stop

Path: S → A → C → G, cost 50+20+30=10050+20+30 = 100 ✓ the true shortest.

Answers, decoded

  • 1.5 → B (Branch-and-Bound). Enumerate every route: S-A-C-G = 100, S-B-E-G = 130, S-B-D-E-G = 160, S-B-E-C-G = 230, S-D-F-G = 190, S-D-B-E-G = 210… The shortest is 100. Best First returns 190 and A* returns 130 — both miss it because h(C)=100h(C) = 100 grossly overestimates the true remaining cost h∗(C)=30h^*(C) = 30 (inadmissible heuristic ⇒ no optimality guarantee). Only Branch-and-Bound sorts by real accumulated cost gg, so its first complete path is provably cheapest → keyed answer B.
  • Why DFS also lands on S,A,C,G here: pure alphabetical luck — the western letters come first, so the dive happens to ride the cheap route. No cost bookkeeping backs it up, which is why the key credits BnB alone.
  • Why A fails despite using costs:* one poisoned hh value is enough. With h(C)=100≫30h(C) = 100 \gg 30, every ff through C looks terrible (f(C)=250f(C) = 250), so G pops at 130 before the C-route can win.

Tricks & Tips

Answers: 1.1 S,A,C,G · 1.2 S,D,F,G · 1.3 S,B,E,G · 1.4 S,A,C,G · 1.5 B