N-Queens Problem & Utility Functions

Introduction to Local Search Concepts

Understanding optimization through the classic N-Queens problem (n=4)
Back to SE444 Course

The N-Queens Problem

The N-Queens problem is a classic constraint satisfaction and optimization problem. The goal is to place n queens on an n×n chessboard such that no two queens can attack each other.

Queens Attack Rules:

  • Horizontal: Queens attack along rows
  • Vertical: Queens attack along columns
  • Diagonal: Queens attack along both diagonals
Key Insight: We're working with n=4, so we need to place 4 queens on a 4×4 board with no conflicts. This gives us exactly one queen per row and per column.
4
Queens to Place
16
Board Squares
2
Valid Solutions

Interactive 4×4 Board

Click on squares to place or remove queens. Watch how the utility function changes!

Light Square
Dark Square
Conflict
Safe

Utility Function Concept

A utility function (or objective function) measures how "good" a particular state is. In the N-Queens problem, we can define utility in several ways:

Our Utility Function:

Utility = -Current Conflicts
Higher utility = Better state
  • Current Conflicts: Count of attacking queen pairs
  • Perfect State: Utility = 0 (no conflicts)
  • Worst State: Utility = -6 (maximum conflicts)
  • Key Principle: Higher utility always indicates better state
Why Negative Utilities?

Utility = 0 represents the optimal state (no conflicts).
Negative values represent penalty for each conflict.
This formulation clearly shows that any conflict is worse than none.

0
Current Utility
Empty board: No conflicts detected. Utility = 0
State Representation
Format: Row position of each queen in its column
Example: (2, 0, 3, 1) means Queen in Col 0→Row 2, Col 1→Row 0, etc.
( - , - , - , - )
Place queens to see state representation
0
Current Conflicts
0
Queens Placed

Example States

Here are different board states and their corresponding utility values:

Key Concepts for Local Search

State Representation

Each state is represented as (r₀, r₁, r₂, r₃) where rᵢ is the row position of the queen in column i. This compact representation makes it easy to evaluate and modify states.

State Space

Each possible arrangement of queens represents a state. The state space contains all possible configurations.

Optimization Landscape

The utility function creates a landscape where we search for peaks (high utility states).

Local vs Global Optima

Local search algorithms might get stuck in local maxima instead of finding the global optimum.

Neighborhood Moves

Small changes to the current state (moving one queen) define the neighborhood for local search.

Ready for Local Search!

Now that you understand the N-Queens problem and utility functions, you're ready to explore local search algorithms like Hill Climbing, Simulated Annealing, and Genetic Algorithms that can solve this problem efficiently.