Local Search & Hill Climbing

GATE Relevance: Medium

Prerequisites

🎯 Learning Objectives

  • Understand the local search paradigm: no path, just optimize
  • Implement steepest-ascent hill climbing
  • Recognize problems: local maxima, plateaus, ridges
  • Apply variants: first-choice, stochastic, random-restart
  • Solve the 8-queens problem using hill climbing

1. The Local Search Paradigm

All search algorithms we have studied so far — BFS, DFS, A*, IDA*, RBFS — are systematic searches. They explore the state space methodically, remember paths, and can guarantee optimality and completeness. But many real-world problems don’t need a path; they just need a good state.

Local Search vs Systematic Search

Systematic search: Find a path from start to goal. Remembers explored states. Guarantees completeness.
Local search: Operate on a single “current state” and move to neighboring states. No path memory. No completeness guarantee. But uses O(1) memory (constant space).

Local search algorithms are ideal for:

  • Optimization problems — find the state that maximizes/minimizes an objective function
  • Constraint satisfaction — find a state satisfying all constraints (e.g., 8-queens)
  • Very large state spaces — where systematic search is infeasible

1.1 Objective Function Landscape

Visualize the state space as a landscape. Each state has an “elevation” equal to its objective function (or heuristic) value. The goal is to find the global maximum (or minimum, depending on formulation).

  Elevation (objective value)
  ^
  |     /\        /\
  |    /  \  /\  /  \     ← Global maximum
  |   /    \/  \/    \
  |  /   plateau      \   ← Local maximum
  | / shoulder          \
  |/________________________→ State space

Key landscape features:

  • Global maximum — the best state overall
  • Local maximum — better than all neighbors, but not the global best
  • Plateau (flat) — a flat area where all neighbors have equal value
  • Ridge — a narrow elevated area that is hard to navigate
  • Shoulder — a plateau with an uphill edge

2. Steepest-Ascent Hill Climbing

The simplest variant. At each step, evaluate all neighbors and move to the one with the highest value. If no neighbor is better than the current state, stop.

2.1 Pseudocode

function STEEPEST-ASCENT-HILL-CLIMBING(problem):
    current = problem.initial_state

    loop:
        neighbor = highest-valued successor of current

        if VALUE(neighbor) ≤ VALUE(current):
            return current    // current is a local maximum

        current = neighbor

This is also called greedy local search because it always takes the steepest uphill step. It never looks ahead and never backtracks.

⚠ Greedy ≠ Optimal

Steepest-ascent hill climbing finds a local maximum, which may or may not be the global maximum. It is incomplete for optimization — there is no guarantee of finding the best solution.

2.2 Problems with Steepest Ascent

ProblemDescriptionWhat Happens
Local maximaA peak higher than neighbors but not the global bestAlgorithm stops prematurely
PlateausFlat area; no uphill neighbor existsAlgorithm gets stuck (random walk may help)
RidgesSequence of local maxima not directly connectedAlgorithm oscillates or stops at ridge edge

3. Hill Climbing Variants

3.1 First-Choice Hill Climbing

Instead of evaluating all neighbors, generate successors randomly and accept the first one that improves the current state.

function FIRST-CHOICE-HILL-CLIMBING(problem):
    current = problem.initial_state

    loop:
        // Generate random successors until one is better
        successor = random successor of current

        if VALUE(successor) > VALUE(current):
            current = successor
        else:
            // try again (with a limit on attempts)
            if too_many_failures:
                return current

Advantage: Much faster per step when the number of successors is very large (e.g., thousands of neighbors). No need to evaluate them all.

3.2 Stochastic Hill Climbing

Choose randomly among all uphill neighbors, with probability proportional to the steepness of the uphill move. This is less greedy — it doesn’t always pick the steepest move, which can help avoid some local maxima.

function STOCHASTIC-HILL-CLIMBING(problem):
    current = problem.initial_state

    loop:
        uphill_neighbors = {n : VALUE(n) > VALUE(current)}
        if uphill_neighbors is empty:
            return current

        // Select with probability proportional to improvement
        current = WEIGHTED-RANDOM-CHOICE(uphill_neighbors)

3.3 Random-Restart Hill Climbing

Run steepest-ascent hill climbing multiple times from random initial states. Return the best solution found across all restarts.

function RANDOM-RESTART-HILL-CLIMBING(problem, max_restarts):
    best = null

    for i = 1 to max_restarts:
        current = RANDOM-STATE(problem)
        result = STEEPEST-ASCENT-HILL-CLIMBING(current)

        if best is null OR VALUE(result) > VALUE(best):
            best = result

    return best

Why Random Restart Works

If each run has probability p of finding the global maximum, then the probability of not finding it after k restarts is (1−p)k. This decreases exponentially! For example, if p = 0.14 (as in 8-queens), then after 6 restarts the probability of success is 1 − (0.86)6 ≈ 0.60, and after 20 restarts it is ≈ 0.96.

3.4 Comparison of Variants

VariantEvaluates per stepEscapes local maxima?CompletenessBest for
Steepest AscentAll neighborsNoIncompleteSmall neighbor sets
First-ChoiceOne (random)NoIncompleteLarge neighbor sets
StochasticAll uphillSlightlyIncompleteRough landscapes
Random-RestartAll (per run)Yes (probabilistic)Complete w.p.1Most problems

4. 8-Queens: Hill Climbing Step by Step

The 8-Queens problem: place 8 queens on an 8×8 board so that no two queens attack each other. We use the number of attacking pairs as our cost function (to minimize) — equivalently, we maximize the negative.

4.1 State Representation

Each state is an array of 8 numbers: [c₁, c₂, …, c₈] where ci is the row of the queen in column i. This guarantees one queen per column. A neighbor is obtained by moving one queen to a different row in its column.

Number of neighbors per state: 8 × 7 = 56 (each of 8 queens can move to 7 other rows).

4.2 Evaluation Function

h(state) = number of pairs of queens attacking each other (directly or indirectly). Goal: h = 0.

4.3 Walkthrough

Example: Hill Climbing on 8-Queens
Initial state: [5, 6, 7, 4, 2, 1, 3, 8]
  . . . . . . . Q     (column 8, row 8)
  . . . . . . Q .     (column 7, row 3? — let's use 1-indexed)
  ...

Attacking pairs (h): Let's compute for a concrete small case.

Suppose h = 17 (many conflicts).

Step 1: Evaluate all 56 neighbors.
  Best neighbor: move queen in col 3 from row 7 to row 2.
  New h = 12.  Improvement: 17 → 12.  Accept.

Step 2: Evaluate all 56 neighbors of new state.
  Best neighbor: move queen in col 5 from row 2 to row 6.
  New h = 8.  Accept.

Step 3: Best neighbor reduces h to 5. Accept.

Step 4: Best neighbor reduces h to 3. Accept.

Step 5: Best neighbor reduces h to 1. Accept.

Step 6: Best neighbor has h = 1 (no improvement). STUCK at local minimum!
  h = 1 means one attacking pair remains.

The steepest-ascent hill climber got stuck at h = 1, a local minimum (not the global minimum h = 0).

4.4 Statistics for 8-Queens

Empirical results from Russell & Norvig (random initial states, 8-queens):

MetricSteepest AscentSteepest + Sideways moves
Success rate~14%~94%
Average steps (success)~4~21
Average steps (failure)~3~64

Allowing sideways moves (accepting neighbors with equal h, up to a limit like 100) dramatically improves success rate from 14% to 94%.

4.5 Random Restart for 8-Queens

With random-restart and p ≈ 0.14 per trial:

  • Expected restarts to solve: 1/0.14 ≈ 7
  • Expected total steps: 7 × 4 (success steps) ≈ 28 + extra failure steps
  • With sideways: expected restarts = 1/0.94 ≈ 1.06 — almost always first try!

5. Random Restart: Probability Analysis

Let p be the probability that a single hill-climbing run reaches the global optimum.

  • Probability of failure in one run: (1 − p)
  • Probability of failure in k runs: (1 − p)k
  • Probability of success in k runs: 1 − (1 − p)k
  • Expected number of runs to find optimum: 1 / p (geometric distribution)
Quick Calculation

For 8-queens with steepest ascent: p = 0.14.

P(success in 10 runs) = 1 − (0.86)10 = 1 − 0.221 = 0.779 (77.9%)

P(success in 25 runs) = 1 − (0.86)25 = 1 − 0.021 = 0.979 (97.9%)

Expected restarts: 1/0.14 ≈ 7.1 restarts

This analysis shows that random-restart hill climbing is complete with probability 1: as the number of restarts → ∞, the probability of success → 1. This makes it surprisingly effective despite the simplicity of each individual run.

Summary

  • Local search operates on a single current state — no path, O(1) memory.
  • Steepest-ascent hill climbing greedily moves to the best neighbor; gets stuck at local maxima.
  • First-choice variant is faster per step for large neighbor sets.
  • Stochastic variant introduces randomness in selection among uphill moves.
  • Random-restart is the most robust: repeats from random starts, complete with probability 1.
  • Key problems: local maxima, plateaus, ridges — but sideways moves and restarts mitigate these.
  • For 8-queens: steepest ascent succeeds ~14% of the time; with sideways moves, ~94%.

References

  • Russell & Norvig, Artificial Intelligence: A Modern Approach, 4th Ed. — Section 4.1.