N-Queen Problem
Introduction
The N-Queen Problem is one of the most popular applications of the Backtracking technique. The objective is to place N queens on an N × N chessboard such that no two queens attack each other.
A queen can attack another queen if they are placed in the same row, column or diagonal. Therefore, the algorithm must place queens carefully while satisfying all these constraints.
Problem Statement
Place N queens on an N × N chessboard so that no two queens share the same row, column or diagonal.
Why Backtracking?
Whenever a queen is placed in an invalid position, the algorithm immediately removes it and tries another position. This "undo" operation is called Backtracking.
Rules of the N-Queen Problem
| Rule | Description |
|---|---|
| Same Row | No two queens can be placed in the same row. |
| Same Column | No two queens can be placed in the same column. |
| Main Diagonal | No two queens can attack each other diagonally. |
| Secondary Diagonal | Queens cannot share the opposite diagonal. |
Chessboard Illustration
Real-Life Example
Suppose four CCTV cameras are to be installed in a square hall. Each camera covers its entire row, column and both diagonals. The objective is to position the cameras so that no camera overlaps the coverage area of another camera. This is similar to the N-Queen Problem.
Solved Example (4-Queen Problem)
Consider a 4 × 4 chessboard. The algorithm places one queen in each row while checking whether the placement is safe.
| Row | Selected Column | Status |
|---|---|---|
| 1 | 2 | Safe |
| 2 | 4 | Safe |
| 3 | 1 | Safe |
| 4 | 3 | Safe (Solution Found) |
Final Solution
State Space Tree
The Backtracking algorithm explores possible queen placements using a State Space Tree.
N-Queen Algorithm
NQueen(Row)
Step 1 : If all queens are placed,
Display the solution and return.
Step 2 : For every column in the current row
Step 3 : Check whether placing the queen is safe.
Step 4 : If safe,
Place the queen.
Step 5 : Recursively place the queen in the next row.
Step 6 : If no solution is found,
Remove the queen (Backtrack).
Step 7 : Try the next column.
Dry Run of the Algorithm
The following steps illustrate the execution of the algorithm for the 4-Queen Problem.
| Step | Action | Result |
|---|---|---|
| 1 | Place Queen in Row 1, Column 2. | Safe |
| 2 | Place Queen in Row 2, Column 4. | Safe |
| 3 | Try Queen in Row 3, Column 2. | Rejected |
| 4 | Place Queen in Row 3, Column 1. | Safe |
| 5 | Place Queen in Row 4, Column 3. | Solution Found |
Complexity Analysis
| Parameter | Complexity | Remarks |
|---|---|---|
| Best Case | O(N) | Rare case where valid positions are found immediately. |
| Average Case | Problem Dependent | Depends on pruning efficiency. |
| Worst Case | O(N!) | Many invalid combinations are explored. |
| Space Complexity | O(N) | Recursive call stack. |
Advantages of the N-Queen Algorithm
- Produces an exact solution whenever one exists.
- Uses pruning to eliminate invalid placements.
- Demonstrates the power of Backtracking.
- Simple recursive implementation.
- Applicable to many constraint satisfaction problems.
Limitations of the N-Queen Algorithm
- Worst-case time complexity is factorial.
- Execution time increases rapidly with larger values of N.
- Requires recursive function calls.
- Not suitable for very large chessboards without optimization.
Applications of the N-Queen Problem
| Application | Description |
|---|---|
| Artificial Intelligence | Constraint satisfaction problems. |
| Scheduling | Conflict-free assignment of tasks. |
| VLSI Design | Component placement optimization. |
| Robotics | Search and planning algorithms. |
| Game Development | State space search problems. |
N-Queen Problem vs Graph Coloring
| N-Queen Problem | Graph Coloring |
|---|---|
| Places queens on a chessboard. | Assigns colours to graph vertices. |
| Constraint: Rows, Columns and Diagonals. | Constraint: Adjacent vertices cannot have the same colour. |
| Uses a chessboard representation. | Uses graph representation. |
| Solved using Backtracking. | Solved using Backtracking. |
Summary
The N-Queen Problem is a classical application of the Backtracking technique. It places queens one row at a time while checking row, column and diagonal constraints. Whenever an invalid position is encountered, the algorithm backtracks and tries another position. Although the worst-case complexity is O(N!), Backtracking significantly reduces the search space by pruning invalid branches.