Course page
CS 486/686
Heuristic Search

Lecture 3

RN 3.5 · PM 3.6–3.7

Search Uncertainty Decisions Learning

Learning goals

  • Motivate heuristic search
  • Trace LCFS, GBFS, A*
  • Analyze: space, time, completeness, optimality
  • Design and dominate admissible heuristics
  • Prune the search space

Which state is closer to the goal?

State A
5
3
8
7
6
2
4
1
State B
1
2
3
4
5
7
8
6
Goal
1
2
3
4
5
6
7
8

B is just one move away. Humans see this instantly — uninformed search does not.

Uninformed vs heuristic

Uninformed

  • Treats every state the same
  • No idea which is closer to goal

Heuristic

  • Estimates distance to goal
  • Expands the most promising state

Two quantities at every node \(n\)

S n G g(n) — cost so far h(n) — estimated cost to goal f(n) = g(n) + h(n)

Solid blue: known. Dashed green: estimated. Red: total estimate.

What makes a good heuristic?

Problem-specific
0 +
Non-negative
0
\(h(\text{goal}) = 0\)
Cheap to compute

Three algorithms, one knob

Frontier is a priority queue. The three algorithms differ in the priority key.

LCFS
\(g(n)\)
cheapest-path-so-far
GBFS
\(h(n)\)
looks-closest-to-goal
A*
\(g(n) + h(n)\)
total estimated cost

The trace graph

All four traces use this graph. Edge labels are costs. Note that F has two parents (E and D).

1 1 9 1 1 1 1 9 S B C E D H F G

Optimal path: S → B → D → G with total cost 11.

Lowest-cost-first search (LCFS)

  • Frontier = priority queue, key = \(g(n)\)
  • Pop the cheapest path so far

a.k.a. Uniform-cost search (UCS) in RN, or Dijkstra's algorithm in algorithms textbooks.

Trace: LCFS

Path notation SBE: 2 = path S→B→E with cumulative cost 2. Ties broken alphabetically.

Frontier: (S: 0) pop S → (SB: 1, SC: 1) pop SB → (SC: 1, SBE: 2, SBD: 10) pop SC → (SBE: 2, SCH: 2, SBD: 10) pop SBE → (SCH: 2, SBD: 10, SBEF: 11) pop SCH (leaf) → (SBD: 10, SBEF: 11) pop SBD → (SBDF: 11, SBDG: 11, SBEF: 11) pop SBDF (leaf) → (SBDG: 11, SBEF: 11) pop SBDG — goal! cost = 11
S 1 1 1 B C 2 9 1 E D 3 1 H 4 9 F 5 6 1 1 G 7 8 G

LCFS — properties

Space\(O(b^d)\) exponential
Time\(O(b^d)\) exponential
Complete?Yes finite branching, edge cost ≥ ε > 0
Optimal?Yes under the same conditions

Greedy best-first search (GBFS)

  • Frontier = priority queue, key = \(h(n)\)
  • Pop the most promising path (lowest \(h\))

Trace: GBFS

Path notation SBD: 1 = path with \(h(D) = 1\). Heuristic values shown in green.

Frontier: (S: 8) pop S → (SC: 3, SB: 7) pop SC → (SB: 7, SCH: 100) pop SB → (SBD: 1, SBE: 4, SCH: 100) pop SBD → (SBDG: 0, SBE: 4, SCH: 100) pop SBDG — goal! cost = 11
S h=8 1 B h=7 C h=3 2 H h=100 3 E h=4 D h=1 4 G h=0 5 G

GBFS — properties

Space\(O(b^m)\) exponential
Time\(O(b^m)\) exponential
Complete?No can get stuck following a misleading heuristic
Optimal?No greedy choice may be wrong

Intuition: what each algorithm prioritises

LCFSGBFS
Frontier sorted by g(n) — cost so far h(n) — estimated cost to goal
Closest uninformed cousin BFS with weights. When every edge costs 1, LCFS = BFS exactly. DFS guided by h. Chases one direction aggressively.
Search shape A cost-wavefront that grows outward from the start. A narrow probe that dives toward whatever looks closest.

Keep these two pictures in mind — the guarantees on the next slide follow directly from them.

Intuition: what guarantees follow

LCFSGBFS
Complete? Yes — wavefront eventually reaches every reachable state. No — low-\(h\) cycles can trap it.
Optimal? Yes — first pop of \(G\) has the smallest \(g\). No — \(h\) alone can prefer a costly “looks-close” detour.

Worst-case time / space: LCFS is \(O(b^d)\); GBFS is \(O(b^m)\).

GBFS — not complete

A low-\(h\) cycle can trap GBFS forever — it never explores the path that actually reaches G.

1 1 1 1 S A B G h=5 h=1 (looks close!) h=10 h=0 S ↔ A ↔ S ↔ A …

Pop order: SA (h=1), SAS (h=5), SASA (h=1), … SB (h=10) is always outranked — G never reached.

GBFS — not optimal

Even when GBFS finds the goal, the path can be far from cheapest.

1 20 3 4 4 S A B C G h=15 h=2 h=10 h=4 h=0 GBFS path: S→A→G cost = 21 Optimal: S→B→C→G cost = 11

A* search

  • Frontier = priority queue, key = \(f(n) = g(n) + h(n)\)
  • Pop the path with smallest total estimated cost

A* combines LCFS's cost-so-far with GBFS's goal-pull.

Trace: A*

Path notation SBE: 6 = path with \(f = g+h = 6\). g in blue, h in green.

Frontier: (S: 8) pop S → (SC: 4, SB: 8) pop SC → (SB: 8, SCH: 102) pop SB → (SBE: 6, SBD: 11, SCH: 102) pop SBE → (SBD: 11, SBEF: 17, SCH: 102) pop SBD → (SBDG: 11, SBDF: 17, SBEF: 17, SCH: 102) pop SBDG — goal! cost = 11
S h=8 1 1 1 B h=7 C h=3 2 1 H h=100 3 9 1 E h=4 D h=1 4 9 F h=6 5 1 1 G h=0 6 G

A* — properties

Space\(O(b^m)\) worst case; often much better
Time\(O(b^m)\) worst case; often much better
Complete?Yes under the usual finite-branching condition
Optimal?Yes if \(h\) is admissible

Admissible heuristic

n goal true cost = 10 h(n) = 8 (an estimate)
Admissible: \(h(n) \le\) true cost from \(n\) to any goal, for every \(n\). Never over-estimates.
Theorem. If \(h\) is admissible, A* returns an optimal path.

A* optimality — proof sketch

Assume A* returns cost \(C^*\), but the true optimum is \(C' < C^*\).

  1. Some node \(N\) on the optimal path is still on the frontier at termination.
  2. Admissibility \(\Rightarrow f(N) = g(N) + h(N) \le C'\).
  3. So \(f(N) \le C' < C^*\) — A* would have popped \(N\) first. Contradiction. \(\blacksquare\)

A* is optimally efficient

No other optimal algorithm using the same \(h\) expands fewer nodes.

Manhattan distance

Sum of grid distances of each tile to its goal position.

Initial
5
3
8
7
6
2
4
1
Goal
1
2
3
4
5
6
7
8
tile 12345678
distance 43122022

Manhattan(start) = 4 + 3 + 1 + 2 + 2 + 0 + 2 + 2 = 16

Misplaced tile count

Number of tiles not yet in their goal position.

Initial
5
3
8
7
6
2
4
1
Goal
1
2
3
4
5
6
7
8

Red = misplaced. Green = already at goal.

Misplaced(start) = 7

Both heuristics are admissible.

Recipe: relax the problem

A: tile B: adjacent + empty drop me!
  1. Relax the problem by dropping constraints.
  2. Solve the relaxed problem (without search).
  3. That cost is an admissible heuristic for the original.

An easier problem gives a lower bound — safe to use as an estimate.

Quick check: heuristic from relaxation

Q. Drop the "B is empty" constraint: a tile can move from A to B if A and B are adjacent. Which heuristic does this give?

  1. Manhattan distance
  2. Misplaced tile count
  3. Another heuristic not listed
A — Manhattan distance. Without the "B must be empty" constraint, each tile slides freely along the grid to its goal — cost = number of grid steps = Manhattan distance.

Dominating heuristics

Definition. \(h_2\) dominates \(h_1\) if \(h_2(n) \ge h_1(n)\) for all \(n\), and \(h_2(n) > h_1(n)\) for some \(n\).
Theorem. If \(h_2\) dominates \(h_1\), A* with \(h_2\) expands no more nodes than A* with \(h_1\). Higher = better — as long as still admissible.

Manhattan vs Misplaced

Q. On our example state, Manhattan = 16 and Misplaced = 7. Which heuristic dominates the other in general?

  1. Manhattan dominates Misplaced
  2. Misplaced dominates Manhattan
  3. Neither dominates the other
A — Manhattan dominates. Each misplaced tile contributes at least 1 to both heuristics; Manhattan adds the actual grid distance, so Manhattan \(\ge\) Misplaced, and strictly \(>\) whenever any tile is more than one step from its goal.

Cycle pruning

Don't follow a path that revisits a node already on the current path.

When expanding path \(\langle n_0, \ldots, n_k \rangle\):
  1. For each neighbour n of nk:
  2. If n already appears in <n0, …, nk>skip.
  3. Else add <n0, …, nk, n> to the frontier.
A B C skip

Check is linear in path length. Saves DFS from infinite loops.

Multi-path pruning

Don't expand a node we've already expanded (across different paths). Cycle pruning is a special case.

Maintain an explored set, initialized to { }.

Pop path \(\langle n_0, \ldots, n_k \rangle\). Then:
  1. If nkexploredskip.
  2. Add nk to explored.
  3. If nk is the goal → return the path.
  4. Else push each neighbour-extension to the frontier.
S p p' n second path to n: skip

Caveats of multi-path pruning

  • Repeats still pushed — skipped on pop
  • Trades memory (explored set) for time
  • May sacrifice optimality (see next slides)

LCFS + multi-path pruning

Q. LCFS with multi-path pruning — can it discard the optimal solution?

  1. Yes, it can
  2. No, never
B — No. First pop of any node already has the smallest \(g\); later paths to it can only be costlier. Safe to prune.

A* + multi-path pruning

Q. A* with admissible \(h\) + multi-path pruning — can it discard the optimal?

  1. Yes, it can
  2. No, never
A — Yes, it can. A* orders by \(f = g + h\), so the first pop of \(n\) need not have the smallest \(g\). A cheaper \(g(n)\) found later is then discarded. Counterexample next.

A* + multi-path: counterexample

Optimal path is S→C→E→G (cost 23). With multi-path pruning, A* returns S→B→E→G (cost 25) instead.

Frontier & Explored: Frontier: (S)   Explored: { } pop S → Frontier: (SB:4, SC:21, SD:22)   Explored: {S} pop SB → Frontier: (SBE:9, SC:21, SD:22)   Explored: {S, B} pop SBE → Frontier: (SC:21, SD:22, SBEG:25)   Explored: {S, B, E} pop SC → Frontier: (SCE:7, SD:22, SBEG:25)   Explored: {S, B, E, C} pop SCE → E already explored → SKIP (the optimal path is lost here!)
S 1 1 2 2 C h=20 B h=2 D h=20 2 3 E h=4 3 20 G h=0 4 2 SKIP — optimal lost

Consistent (monotone) heuristic

Definition. \(h\) is consistent if for every edge \(m \to n\):

\(h(m) \le \text{cost}(m, n) + h(n)\)

i.e., the heuristic decreases by no more than the edge cost as we move toward the goal.

Theorem. If \(h\) is consistent, A* with multi-path pruning is optimal.

Most natural heuristics (Manhattan, straight-line distance, …) are consistent.

Where A* can mis-prune

Why does LCFS need no extra condition, but A* does?

LCFS — sorts by \(g(n)\):

  • first pop of \(n\) already has the smallest \(g(n)\) — safe.

A* — sorts by \(f = g + h\):

  • \(h(n)\) cancels at \(n\), but differs across prefixes \(p, p'\);
  • a big \(h\)-gap can pop the larger-\(g\) path first → \(n\) committed to a worse prefix.

How consistency restores the guarantee

Bound \(h\)-change along every edge \(p \to n\):

\(h(p) - h(n) \le c(p, n)\)  ⇔  \(h(p) \le c(p, n) + h(n)\)

— that's exactly consistency.

Chain of consequences:

  • \(f\) is non-decreasing along every path
  • first pop of \(n\) has the smallest \(g\) — LCFS guarantee, restored
  • ∴ multi-path pruning is safe

Five search algorithms

Algorithm Frontier key Space Time Complete? Optimal?
DFSLIFO (stack)\(O(bm)\)\(O(b^m)\)No cyclesNo
BFSFIFO (queue)\(O(b^d)\)\(O(b^d)\)YesYes unit cost
LCFS\(g(n)\)\(O(b^d)\)\(O(b^d)\)YesYes
GBFS\(h(n)\)\(O(b^m)\)\(O(b^m)\)NoNo
A*\(g(n)+h(n)\)\(O(b^m)\)\(O(b^m)\)YesYes admissible \(h\)

Learning goals (recap)

  • Motivate heuristic search
  • Trace LCFS, GBFS, A*
  • Analyze space, time, completeness, optimality
  • Design and dominate admissible heuristics
  • Prune the search space (cycle, multi-path, consistency)