BFS and DFS

The order you expand neighbour states decides breadth-first vs depth-first search.

BFS order 1 2 3 4 5 DFS order 1 2 5 3 4
BFS expands level by level; DFS dives down one branch (numbers = visit order).
Missionaries and cannibals

State $(m,c,b)$ = missionaries and cannibals on the left bank, and whether the boat is on the left. Goal $(0,0,0)$. Illegal if cannibals outnumber missionaries on either bank.

BFS and DFS both explore the same graph but visit states in different orders — BFS tends to find a shallow solution first.

Key takeaways

  • BFS = layer by layer; shortest unweighted path.
  • DFS = deep trails first.
  • Good state representation is half the work.