Solving problems by searching

Comparing search strategies

Lists the completeness, optimality, and time and space complexity of each search strategy as on the slides, runs BFS, DFS, IDS, UCS, greedy best-first, A*, and weighted A* on one problem side by side, and computes node counts for given b, d, and m.

  • Lecture reference: Uninformed Search · slides 30–45
  • Lecture reference: Informed Search · slide 36
  • Lecture reference: Informed Search · slides 42–43

All search strategies

AlgorithmComplete?Optimal?Time complexitySpace complexity
YesIf all step costs are equalO(bᵈ) O(bᵈ)
NoNoO(bᵐ) O(bm)
YesIf all step costs are equalO(bᵈ) O(bd)
YesYesNumber of nodes with g(n) ≤ C*
NoNoWorst case: O(bᵐ)Best case: O(bd)
YesYes (if heuristic is admissible)Number of nodes with g(n) + h(n) ≤ C*
b
maximum branching factor of the search tree
d
depth of the optimal solution
m
maximum length of any path in the state space (may be infinite)
C*
cost of the optimal solution
g(n)
cost of the path from the start state to node n

Lecture reference: Uninformed Search · slide 45 Lecture reference: Informed Search · slide 42

Problem Romania: Arad to Bucharest

Tree search from Arad to Bucharest with the straight-line distance. DFS goes Arad, Sibiu, Arad, Sibiu, … until the limit stops it. UCS and A* return the route through Rimnicu Vilcea and Pitesti (418); BFS, IDS, greedy, and weighted A* the route through Fagaras (450).

Lecture reference: Informed Search · slides 8–22
Start
Arad
Goal
Bucharest
h(n)
Straight-line distance to Bucharest
b
4 (most successors of any state)
Cheapest
Arad → Sibiu → Rimnicu Vilcea → Pitesti → Bucharest (cost 418, d = 4)
Graph text

Settings

Repeated states

Every child goes on the frontier; states can repeat. Lecture reference: Solving Problems by Searching · slide 28

A run stops after taking 1,000 nodes off the frontier or generating 50,000 nodes. IDS tries depth limits 0 to 50. Successors in name order; the goal test runs when a node is taken off the frontier.

Nodes generated

  • BFS 40
  • DFS 3,501 stopped at the limit
  • IDS 30
  • UCS 132
  • Greedy 10
  • A* 16
  • Weighted A* 10

Outlined bars: no solution returned. Bars are scaled to the runs that found a solution; a gap near the end marks a bar cut short.

Results tree search · Romania: Arad to Bucharest

StrategyPathCostCheapest?Taken off the frontierExpandedGeneratedMax frontierOutcome
BFS Step throughBFS in the search toolArad → Sibiu → Fagaras → Bucharest450 No, +321615 4025Found Returned a solution.
Expansion orderof BFS

Arad, Sibiu, Timisoara, Zerind, Arad, Fagaras, Oradea, Rimnicu Vilcea, Arad, Lugoj, Arad, Oradea, Sibiu, Timisoara, Zerind, Bucharest

DFS Step throughDFS in the search tool–––1,0001,000 3,5012,501Stopped at the limit Stopped after taking 1,000 nodes off the frontier.
Expansion orderof DFS

Arad, Sibiu, Arad, Sibiu, Arad, Sibiu, Arad, Sibiu, Arad, Sibiu, Arad, Sibiu, Arad, Sibiu, Arad, Sibiu, Arad, Sibiu, Arad, Sibiu

IDS Step throughIDS in the search toolArad → Sibiu → Fagaras → Bucharest450 No, +32259 308Found Returned a solution.
Expansion orderof IDS

Arad | Arad, Sibiu, Timisoara, Zerind | Arad, Sibiu, Arad, Fagaras, Oradea, Rimnicu Vilcea, Timisoara, Arad, Lugoj, Zerind, Arad, Oradea | Arad, Sibiu, Arad

UCS Step throughUCS in the search toolArad → Sibiu → Rimnicu Vilcea → Pitesti → Bucharest418 lowest Yes5352 13280Found Returned a solution.
Expansion orderof UCS

Arad, Zerind, Timisoara, Sibiu, Oradea, Arad, Zerind, Rimnicu Vilcea, Zerind, Lugoj, Arad, Fagaras, Timisoara, Arad, Oradea, Sibiu, Oradea, Arad, Oradea, Sibiu

Greedy Step throughGreedy in the search toolArad → Sibiu → Fagaras → Bucharest450 No, +3243 fewest107Found Returned a solution.
Expansion orderof Greedy

Arad, Sibiu, Fagaras, Bucharest

A* Step throughA* in the search toolArad → Sibiu → Rimnicu Vilcea → Pitesti → Bucharest418 lowest Yes65 1611Found Returned a solution.
Expansion orderof A*

Arad, Sibiu, Rimnicu Vilcea, Fagaras, Pitesti, Bucharest

Weighted A* α = 2 Step throughWeighted A* in the search toolArad → Sibiu → Fagaras → Bucharest450 No, +3243 fewest107Found Returned a solution.
Expansion orderof Weighted A*

Arad, Sibiu, Fagaras, Bucharest

Cheapest? compares each path with the cheapest path in the graph. Generated counts the root and children that were not added; max frontier is the largest frontier size.

Lowest cost found: 418 (UCS, A*). Fewest nodes expanded: 3 (Greedy, Weighted A*). No solution returned: DFS.

Node counts

Inputs

Time nodes generated

BFS O(bᵈ)

Nodes in a b-ary tree of depth d Lecture reference: Uninformed Search · slide 31

1 + b + b² + … + bᵈ

= 1 + 10 + 10² + 10³ + 10⁴ + 10⁵

= 1 + 10 + 100 + 1,000 + 10,000 + 100,000

= 111,111

IDS O(bᵈ)

Level i is generated again in each of the d + 1 − i iterations that reach it Lecture reference: Uninformed Search · slide 38

(d+1)b⁰ + d b¹ + (d−1)b² + … + bᵈ

= 6·10⁰ + 5·10¹ + 4·10² + 3·10³ + 2·10⁴ + 10⁵

= 6 + 50 + 400 + 3,000 + 20,000 + 100,000

= 123,456

IDS / BFS = 1.11: 11% more nodes than BFS.

DFS O(bᵐ)

A solution at the maximum depth m: every node of a b-ary tree of depth m Lecture reference: Uninformed Search · slide 32

1 + b + b² + … + bᵐ

= 1 + 10 + 10² + … + 10⁹ + 10¹⁰

= 1 + 10 + 100 + … + 1,000,000,000 + 10,000,000,000

= 11,111,111,111

UCS O(bC*/ε)

Nodes with g(n) ≤ C* lie at depth k = ⌊C*/ε⌋ = 10 or less. Lecture reference: Uninformed Search · slide 44

1 + b + b² + … + bᵏ

= 1 + 10 + 10² + … + 10⁹ + 10¹⁰

= 1 + 10 + 100 + … + 1,000,000,000 + 10,000,000,000

= 11,111,111,111

k = 10 > d = 5: up to 100,000 times the BFS count.

Space nodes in memory

AlgorithmSpaceNodes
BFSO(bᵈ) bᵈ100,000
IDSO(bd) b·d + 151
DFSO(bm) b·m + 1101
UCSO(bC*/ε) bᵏ10,000,000,000

BFS keeps the whole last level on the frontier; DFS keeps the unexpanded siblings of each node on its path (b per level) plus the root; IDS is DFS with depth limit d.

A note on the complexity of search

The worst-case complexity of search is exponential in the length of the solution path, and the length of the solution path can be exponential in the number of “objects” in the problem. Example: towers of Hanoi. Lecture reference: Informed Search · slide 43

Moves in the shortest solution
2ⁿ − 1 = 2⁵ − 1 = 31
Search cost at that depth
O(bᵈ) with b = 3, d = 31: 3³¹ = 617,673,396,283,947 (15 digits)

b = 3: in any state the smallest disk can move to either other peg, and one move is possible between the two remaining pegs.

Typical search costs for the 8-puzzle

Average number of nodes expanded for different solution depths.

Algorithmd = 12d = 24
IDS3,644,035≈ 54,000,000,000
A*(h1)22739,135
A*(h2)731,641

h1: misplaced tiles; h2: total Manhattan distance.

Lecture reference: Informed Search · slide 36 Solve boards in the 8-puzzle tool

Questions from the slides

When is UCS equivalent to BFS?

Lecture reference: Uninformed Search · slide 45
Show answer

Can the complexity of UCS exceed the complexity of BFS?

Lecture reference: Uninformed Search · slide 45
Show answer

How to make DFS complete?

Lecture reference: Uninformed Search · slide 45
Show answer

When is DFS better than BFS?

Lecture reference: Uninformed Search · slide 45
Show answer