Nexus Learn

Lecture 15: Hill Climbing and Beam Search

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

↓ Download notes (Word)

On this page
  1. Learning Outcomes
  2. 1. Local Search
  3. 2. Hill Climbing
  4. 3. Beam Search
  5. 4. Common Mistakes
  6. Word Bank
  7. Self-Check Questions

Learning Outcomes

SLO

After this lecture you will be able to...

CLO

Bloom

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

§1.1 Path search vs local search

Slide L15-S06

BFS, UCS, greedy and A* (L10-L14) return a PATH and keep a frontier in memory. In many problems only the final state matters: 8-queens, a class timetable, the best place for a tubewell. LOCAL SEARCH keeps only one current state, looks at its neighbours (states one small change away) and moves to a better one. It needs almost no memory and works in huge spaces, but it does not guarantee the best answer. The objective function is the value to maximise (height, profit) or the cost to minimise (number of clashes).

§1.2 The state-space landscape

Slide L15-S07

Imagine every state as a position and its objective value as a height. Our foggy hill track (positions 0-20, heights in metres):

position: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20

height: 0 40 90 120 110 80 70 100 150 210 260 250 250 250 230 190 220 300 340 310 260

Term

Meaning

On our track

Global maximum

The highest state of all

Position 18 (340 m)

Local maximum

Higher than its neighbours, not the highest

Position 3 (120 m), 10 (260 m)

Plateau

A flat area: neighbours have the same value

Positions 11-13 (250 m)

Shoulder

A plateau with an uphill exit

11-13 lead on to 10 (260 m)

Ridge

A narrow line of local maxima in many dimensions

Hard to climb one step at a time

2. Hill Climbing

§2.1 The algorithm

Slide L15-S09

Steepest-ascent hill climbing moves to the best neighbour if it is better than the current state and stops otherwise. Like a hiker in fog who can only see the next step.

def hill_climb_1d(start, track=TRACK):

"""Steepest ascent on the track: look at left and right neighbour, move to the higher one if it is higher."""

pos, path = start, [start]

while True:

nbrs = [p for p in (pos - 1, pos + 1) if 0 <= p < len(track)]

best = max(nbrs, key=lambda p: track[p])

if track[best] <= track[pos]: return pos, path

pos = best; path.append(pos)

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

The result depends completely on the starting point: only starts 16-20 reach the global maximum.

§2.2 8-queens

Slide L15-S10

State: one queen per column, state[c] = row of the queen in column c. Neighbour: move one queen within its column (56 neighbours). h = number of pairs of queens attacking each other (same row or diagonal); we minimise h, solved when h = 0.

def attacks(s):

n, h = len(s), 0

for a in range(n):

for b in range(a + 1, n):

if s[a] == s[b] or abs(s[a] - s[b]) == b - a: h += 1

return h

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

The search stopped at h = 1: a local minimum after 5 steps.

§2.3 Success rates and variants

Slide L15-S11

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

Variant

Idea

Fixes

Sideways moves

Allow equal-value moves (limited)

Plateaus: 15.4% to 94.0% solved

Random restart

Start again from a random state when stuck

Local maxima: all 50 solved

Stochastic

Random uphill move

Sometimes better peaks

First-choice

First better neighbour found

Very many neighbours

§2.4 Hill climbing on the 8-puzzle

Slide L15-S12

With h = Manhattan distance hill climbing gets stuck after 1 or 2 moves on every board below (first line of each board): to reach the goal, tiles must sometimes move away from home first, so h goes up before it comes down.

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

§3.1 The idea

Slide L15-S14

Beam search expands the tree level by level like BFS but keeps only the k best nodes (smallest h) of each level; k is the beam width - like a torch that lights only part of the road. k = 1 behaves like hill climbing; k = infinity is BFS. Memory is at most k x b nodes per level. Local beam search does the same with k complete states: pool all their neighbours and keep the k best. Beam search is used in speech recognition and machine translation.

def beam_puzzle(start, k, h=h_manhattan, max_depth=200):

"""Beam search: breadth-first level by level, but keep only the k best states (by h) at each level."""

level, parent, depth = [start], {start: None}, 0

while level and depth < max_depth:

if GOAL in level:

n, s = 0, GOAL

while parent[s]: s = parent[s]; n += 1

return n, depth, len(parent)

children = []

for s in level:

for _, c in moves(s):

if c not in parent: parent[c] = s; children.append(c)

children.sort(key=lambda c: (h(c), c))

level = children[:k]; depth += 1

return None, depth, len(parent)

§3.2 Results on the 8-puzzle

Slide L15-S15

Second line of each board in §2.4: k = 1 always fails, k = 2 fails on two boards, and k = 50 finds the optimal 14 and 20 moves but 39 on the 31-move board. Beam search is neither complete nor optimal, and a bigger k is not always better.

Hill climbing

Beam search

A*

Keeps

1 state

k per level

Whole frontier

Complete

No

No

Yes

Optimal

No

No

Yes (admissible h)

Memory

O(1)

O(kb)

Exponential

4. Common Mistakes

Wrong idea

Correct idea

Hill climbing finds the best state.

Often only a local maximum (15.4% on 8-queens).

Hill climbing keeps a frontier.

One state; it never goes back.

A plateau is a peak.

No uphill direction; sideways moves can cross it.

Beam with k = 1 is BFS.

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

A bigger beam always helps.

k = 5 gave 62 moves, k = 10 gave 30.

Word Bank

Word

Simple meaning

Example

Local search

Improve one state step by step

Move one queen at a time

Objective function

Value to maximise or minimise

Height; attacking pairs

Local maximum

A small peak, not the highest

120 m at position 3

Plateau

Flat area

250 m at 11-13

Random restart

Start again from a random state

About 8 restarts for 8-queens

Beam width

Number of states kept per level

k = 50

Self-Check Questions

  1. When is local search better than path search? (see §1.1)
  2. Name the landscape features on the track. (see §1.2)
  3. Run hill climbing on the track from position 7. (see §2.1)
  4. Count the attacking pairs of (0, 0, 0, 0) in 4-queens. (see §2.2)
  5. How do sideways moves and random restarts help? (see §2.3)
  6. Why does beam search with k = 1 fail? (see §3.2)

Reading: Russell & Norvig (4e) 4.1.1-4.1.3; Luger (6e) 4.1, 4.4 (beam search).

← All CS-511 material