Nexus Learn

Lecture 15: Hill Climbing and Beam Search

Lecture Slides CS-511 Artificial Intelligence 8 min read Updated 8 Nov 2026

↓ Download slides (PowerPoint)

Recap of Lecture 14 and Homework Check

LAST LECTURE · 5 MIN

Today: A* can keep millions of nodes in memory. What if we keep only ONE state and the path does not matter?

Today's Plan (50 minutes)

ROADMAP

0-5

Recap of Lecture 14 and homework check

S02

5-8

Plan and learning outcomes

S03-S04

8-15

Module 1: Local search and the state-space landscape

S05-S07

15-27

Module 2: Hill climbing on a track, 8-queens and the 8-puzzle

S08-S12

27-35

Module 3: Beam search

S13-S15

35-43

Class activity: climb the foggy track; count attacking queens

S16

43-48

Common mistakes and recap quiz

S17-S18

48-50

Homework and next lecture

S19

Learning Outcomes (SLOs)

BY THE END OF THIS LECTURE YOU WILL BE ABLE TO

SLO 15.1

Explain local search and the landscape terms: global and local maximum, plateau, shoulder, ridge

→ CLO3 · C2 Understand

SLO 15.2

Apply steepest-ascent hill climbing to a landscape and to 8-queens, and fix it with sideways moves and random restarts

→ CLO3 · C3 Apply

SLO 15.3

Apply beam search with width k and compare it with hill climbing and A*

→ CLO3 · C3 Apply

  • Course Learning Outcome(s) served
  • CLO3: Apply various techniques and algorithms of AI used in general problem solving, optimization problems, constraint satisfaction problems, and game programming.

Slide 5

MODULE 1

Local Search

When only the final state matters

Path Search vs Local Search

MODULE 1 · IDEA

Path search (L10-L14)

Local search (today)

Examples

Answer = a path of actions. Keeps a frontier. Can be optimal, needs memory.

Answer = a good final state. Keeps one state. Tiny memory, no guarantee.

8-queens, class timetables, tubewell or tower placement, machine settings.

The State-Space Landscape

MODULE 1 · LANDSCAPE

Fog: the hiker sees only the next step - exactly what hill climbing sees.

Slide 8

MODULE 2

Hill Climbing

Always take the steepest step uphill; stop when no step goes up

Hill Climbing on the Foggy Track

MODULE 2 · ALGORITHM

STEEPEST-ASCENT HILL CLIMBING

  • def hill_climb(state):
  • while True:
  • best = best neighbour of state
  • if value(best) <= value(state):
  • return state # top
  • state = best

Memory

Stops

Only the current state - O(1).

At a peak, a local peak or a flat area.

REAL OUTPUT: L15/TRACK.PY

  • start 1 ( 40 m): path [1, 2, 3] -> stops at 3 (120 m) stuck (local maximum or flat)
  • start 5 ( 80 m): path [5, 4, 3] -> stops at 3 (120 m) stuck (local maximum or flat)
  • start 12 (250 m): path [12] -> stops at 12 (250 m) stuck (local maximum or flat)
  • start 14 (230 m): path [14, 13] -> stops at 13 (250 m) stuck (local maximum or flat)
  • start 16 (220 m): path [16, 17, 18] -> stops at 18 (340 m) GLOBAL maximum

8-Queens: Count the Attacking Pairs

MODULE 2 · 8-QUEENS

REAL OUTPUT: L15/QUEENS.PY

  • start h = 17
  • . . . . . . . .
  • . . . . . . . .
  • . . . . . . . .
  • . . . Q . . . .
  • Q . . . Q . . .
  • . Q . . . Q . Q
  • . . Q . . . Q .
  • . . . . . . . .
  • step 1: h = 12 state (4, 5, 6, 3, 4, 2, 6, 5)
  • step 2: h = 8 state (4, 5, 1, 3, 4, 2, 6, 5)
  • step 3: h = 5 state (4, 5, 1, 3, 4, 2, 7, 5)
  • step 4: h = 3 state (4, 0, 1, 3, 4, 2, 7, 5)
  • step 5: h = 1 state (4, 0, 1, 3, 6, 2, 7, 5)
  • stopped: h = 1 - every neighbour is worse or equal

State

One queen per column: (4, 5, 6, 3, 4, 5, 6, 5) - row of each queen.

Neighbour

Move one queen up or down its column: 56 neighbours.

h to minimise

Pairs of queens that attack. Solved when h = 0.

How Often Does It Work? And How to Fix It

MODULE 2 · VARIANTS

REAL OUTPUT: L15/QUEENS_STATS.PY (8-QUEENS, RANDOM STARTS)

  • steepest ascent solved 15.4% | avg steps: success 3.9, stuck 3.1
  • with up to 100 sideways moves solved 94.0% | avg steps: success 18.7, stuck 57.9
  • random-restart hill climbing: solved 50 of 50 puzzles, average 8.0 restarts each

Hill Climbing on the 8-Puzzle

MODULE 2 · EVIDENCE

REAL OUTPUT: L15/PUZZLE.PY (FIRST LINE OF EACH BOARD: HILL CLIMBING)

  • 8 1 3 / 4 _ 2 / 7 6 5 A*: 14 moves | hill climbing: stuck, moves made 2, h = 8
  • beam k=1: fails | k=2: 34 moves | k=5: 62 moves | k=10: 30 moves | k=50: 14 moves
  • 7 2 4 / 5 _ 6 / 8 3 1 A*: 20 moves | hill climbing: stuck, moves made 2, h = 12
  • beam k=1: fails | k=2: fails | k=5: 22 moves | k=10: 20 moves | k=50: 20 moves
  • 8 6 7 / 2 5 4 / 3 _ 1 A*: 31 moves | hill climbing: stuck, moves made 1, h = 20
  • beam k=1: fails | k=2: fails | k=5: 43 moves | k=10: 39 moves | k=50: 39 moves

Why stuck? on the way to the goal, tiles must sometimes move away from home (h goes up first). Hill climbing never steps downhill.

A* vs hill climbing: A* keeps a frontier and can come back; hill climbing keeps one state and cannot.

Slide 13

MODULE 3

Beam Search

Keep the k best, not just one

Beam Search: a Torch Beam over the Tree

MODULE 3 · IDEA

BEAM SEARCH (BEAM_PUZZLE IN LOCALSEARCH.PY)

  • level = [start]
  • while level:
  • if goal in level: return
  • children = all children of level
  • sort children by h
  • level = children[:k] # keep k best

Torch beam

Memory

Used in

Lights only the k most promising nodes of each level.

At most k x b nodes per level - fixed, not exponential.

Speech recognition, machine translation (keep k best partial sentences).

Beam Search on the 8-Puzzle

MODULE 3 · EVIDENCE

REAL OUTPUT: L15/PUZZLE.PY (SECOND LINE OF EACH BOARD: BEAM SEARCH)

  • 8 1 3 / 4 _ 2 / 7 6 5 A*: 14 moves | hill climbing: stuck, moves made 2, h = 8
  • beam k=1: fails | k=2: 34 moves | k=5: 62 moves | k=10: 30 moves | k=50: 14 moves
  • 7 2 4 / 5 _ 6 / 8 3 1 A*: 20 moves | hill climbing: stuck, moves made 2, h = 12
  • beam k=1: fails | k=2: fails | k=5: 22 moves | k=10: 20 moves | k=50: 20 moves
  • 8 6 7 / 2 5 4 / 3 _ 1 A*: 31 moves | hill climbing: stuck, moves made 1, h = 20
  • beam k=1: fails | k=2: fails | k=5: 43 moves | k=10: 39 moves | k=50: 39 moves

Activity: Climb the Foggy Track; Count the Queens

CLASS ACTIVITY · PAIRS · 8 MIN

Pairs, 6 min + 2 min. Track heights (positions 0-20): 0, 40, 90, 120, 110, 80, 70, 100, 150, 210, 260, 250, 250, 250, 230, 190, 220, 300, 340, 310, 260. 4-queens: state[c] = row of the queen in column c.

Hill climbing from position 7: path and final height?

4-queens state (1, 3, 0, 3): how many attacking pairs (h)?

From position 15: where does it stop, and why?

4-queens state (0, 0, 0, 0): h = ?

List ALL starting positions that reach the global maximum.

Which variant would rescue the hiker who started at 15?

Common Mistakes

WATCH OUT

WRONG IDEA

CORRECT IDEA

✗ Hill climbing finds the best state.

✓ Only a peak near the start - often a local maximum (15.4% on 8-queens).

✗ Hill climbing keeps a frontier like A*.

✓ It keeps one state and never goes back.

✗ A flat area is a peak.

✓ A plateau has no uphill direction; sideways moves can cross it.

✗ Beam search with k = 1 is BFS.

✓ k = 1 is like hill climbing; k = infinity is BFS.

✗ A bigger beam always gives a shorter answer.

✓ Not always: k = 5 gave 62 moves, k = 10 gave 30.

Recap Quiz

CHECK YOUR UNDERSTANDING · 3 MIN

  • Q1. Hill climbing keeps in memory:
  • (a) The whole tree (b) The frontier (c) Only the current state (d) All visited states
  • Q2. A state better than all its neighbours but not the best overall is a:
  • (a) Plateau (b) Local maximum (c) Global maximum (d) Ridge
  • Q3. For 4-queens state (0, 0, 0, 0), the number of attacking pairs is:
  • (a) 4 (b) 3 (c) 6 (d) 0
  • Q4. Beam search with width k = 1 behaves most like:
  • (a) BFS (b) A* (c) Hill climbing (d) IDDFS

Homework and Next Lecture

BEFORE THE NEXT CLASS

  • Task 1: Run hill climbing on the foggy track from positions 0, 7 and 15. Write each path and say: global maximum, local maximum or plateau?
  • Task 2: 8-queens state (3, 2, 1, 4, 3, 2, 1, 2): count the attacking pairs h.
  • Task 3: Using the beam table (S15), explain why k = 1 always fails and why k = 50 still gave 39 moves on the 31-move board.
  • Prepare: Mid-term covers L01-L16. Revise the search traces (BFS, DFS, UCS, greedy, A*).

NEXT LECTURE

  • Lecture 16:
  • Simulated Annealing and Parallel Search

← All CS-511 material