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 1x1 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.

#QuestionOfficial answer
1.1What is the path found by the Best First Search algorithm? Enter the path as a comma separated listS,A,B,D,G
1.2What is the path found by A* search algorithm? Enter the path as a comma separated listS,C,D,G
1.3What is the path found by Branch-and-Bound search algorithm? Enter the path as a comma separated listS,E,F,H,I,G
1.4For the given map, which algorithm finds the shortest path from S to G?B
1.5What can you say about the heuristic function for the given graph?B

The Topic in 60 Seconds

AlgorithmSorts OPEN byIgnores
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) of the whole pathh(n)h(n)

All three pop the cheapest entry, generate children, and stop when they pop G — only the sorting key differs. Tie-break: smaller node label first.

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

Step 0 — Heuristic table (Manhattan distance to G, read off the grid)

NodehhNodehh
S18D6
A9E16
B6F8
C10H4
G0I2

Memorize two numbers before tracing: h(A)=9h(A) = 9 (small!) and h(E)=16h(E) = 16 (big). Everything that happens below is driven by them.

1.1 — Best First Search → S,A,B,D,G

Sort purely by hh. Costs of edges are ignored completely.

StepOPEN (node : hh)Pick
1S : 18S → A(9), C(10), E(16)
2A : 9, C : 10, E : 16A → B(6)
3B : 6, C : 10, E : 16B → D(6)
4D : 6, C : 10, E : 16D → G(0), H(4)
5G : 0, H : 4, C : 10, E : 16G — goal!

Path: S → A → B → D → G, cost 12+6+6+6=3012+6+6+6 = 30.

Best First chased the small hh values (9→6→6→09 \to 6 \to 6 \to 0) and never noticed it was paying huge gg costs — the S–A edge alone is 12.

1.2 — A* search → S,C,D,G

Sort by f(n)=g(n)+h(n)f(n) = g(n) + h(n).

StepOPEN (node : f=g+hf = g+h)Pick
1S : 0+180+18 = 18S → A : 12+912+9 = 21, C : 8+108+10 = 18, E : 4+164+16 = 20
2C : 18, E : 20, A : 21C → D : 14+614+6 = 20, B : 14+614+6 = 20
3B : 20, D : 20, E : 20, A : 21B (tie at 20 → labels) — generates nothing new
4D : 20, E : 20, A : 21D (tie at 20 → labels) → G : 20+020+0 = 20, H : 20+420+4 = 24
5G : 20, E : 20, A : 21, H : 24G — goal! (tie with E broken by label)

Path: S → C → D → G, cost 8+6+6=208+6+6 = 20.

Note the trap: E sits in OPEN with a tempting g=4g = 4, but h(E)=16h(E) = 16 inflates f(E)f(E) to 20, so A* never expands it. A* returned cost 20 while a cost-18 route exists — remember this for 1.4/1.5.

1.3 — Branch-and-Bound → S,E,F,H,I,G

Sort full paths by gg only. Extend paths until G is popped as the cheapest.

Pop #Popped (path : gg)New paths added
1S : 0SE : 4, SC : 8, SA : 12
2SE : 4SEF : 4+44+4 = 8
3SC : 8 (tie with SEF → label)SCB : 14, SCD : 14
4SEF : 8SEFC : 14, SEFH : 14
5SA : 12SAB : 12+612+6 = 18
6SCB : 14SCBD : 20
7SCD : 14SCDH : 20, SCDG : 20
8SEFC : 14SEFCB : 20, SEFCD : 20
9SEFH : 14SEFHI : 14+214+2 = 16, SEFHD : 20, SEFHG : 22
10SEFHI : 16SEFHIG : 16+216+2 = 18
11SAB : 18 (tie with SEFHIG → label)SABD : 24
12SEFHIG : 18 — reaches G; every other open path ≥ 20stop — optimal

Path: S → E → F → H → I → G, cost 4+4+6+2+2=184+4+6+2+2 = 18 ✓ the true shortest.

BnB never looked at any hh value, so the misleading heuristic could not hurt it. It popped cheap partial paths (SE at 4, SEF at 8) and let the cheap edges accumulate into the winning route.

1.4 — Who finds the shortest path? → B

Full route census:

RouteCost
S-E-F-H-I-G18 ← shortest
S-C-D-G20
S-E-F-H-G22
S-C-D-H-I-G24
S-C-B-D-G26
S-A-B-D-G30

Returned paths: Best First 30, A* 20, BnB 18. Only Branch-and-Bound returns the shortest → option B ✓.

1.5 — Admissibility → B (not admissible)

An admissible hh never overestimates the true remaining cost h∗(n)h^*(n).

Check the suspects against true cheapest distances:

Nodehhh∗h^* (true cheapest to G)Verdict
E16E-F-H-I-G = 4+6+2+24+6+2+2 = 14overestimates!
F8F-H-I-G = 10ok
C10C-D-G = 12ok
A9A-B-D-G = 18ok

h(E)=16>h∗(E)=14h(E) = 16 > h^*(E) = 14 → the heuristic overestimates at E → not admissible → B ✓. That single overestimate is exactly why A* deprioritized E (f = 20 instead of a true-deserved lower value) and settled for the cost-20 route while BnB collected the cost-18 one.

Interactive A* trace

Step through the A* run — watch how f(E)=20f(E)=20 keeps the truly cheap region frozen while the C-route finishes:

1 / 6
OPEN = { S(g=0, h=18, f=18) }
expandingin OPEN (f = g+h)expandedreturned path

Tricks & Tips

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