0% found this document useful (0 votes)
18 views

M2 Backtracking - N Queen Problem

The N Queen Problem is a classic backtracking problem where the goal is to place N queens on an N x N chessboard such that no two queens threaten each other. The document describes the process of solving the problem for N = 4, detailing the placement and backtracking steps involved. It also includes algorithms for checking valid placements and solving the N Queen Problem, with a time complexity of O(N!).

Uploaded by

luckymlcvl
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

M2 Backtracking - N Queen Problem

The N Queen Problem is a classic backtracking problem where the goal is to place N queens on an N x N chessboard such that no two queens threaten each other. The document describes the process of solving the problem for N = 4, detailing the placement and backtracking steps involved. It also includes algorithms for checking valid placements and solving the N Queen Problem, with a time complexity of O(N!).

Uploaded by

luckymlcvl
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

N queen Problem

Wednesday, January 4, 2023 5:24 PM

What is N Queen Problem?


• N Queen problem is the classical example of backtracking.
• N Queen problem is defined as “given N X N chess board, arrange N queens in such
a way that no two queens attack each other by being in same row, column or
diagonal.
• For N = 1, this is trivial case. For N = 2 and N = 3, solution is not possible. So we start with
N = 4 and we will generalize it for N queens
Problem:
• Given 4 x 4 chessboard, arrange four queens in a way, such that no two queens attack
each other. That is, no two queens are placed in the same row, column, or diagonal.
• Example: 4 Queen Problem using 4 X 4 Chessboard
• We have to arrange four queens, Q1, Q2, Q3 and Q4 in 4 x 4 chess board. We will put ith
queen in ith row.
• Let us start with position (1, 1). Q1 is the only queen, so there is no issue. partial solution is
<1>
• We cannot place Q2 at positions (2, 1) or (2, 2). Position (2, 3) is acceptable. partial
solution is <1, 3>.
• Next, Q3 cannot be placed in position (3, 1) as Q1 attacks her. And it cannot be placed at

Bracktracking Page 1.1


• Next, Q3 cannot be placed in position (3, 1) as Q1 attacks her. And it cannot be placed at
(3, 2), (3, 3) or (3, 4) as Q2 attacks her. There is no way to put Q3 in third row. Hence, the
algorithm backtracks and goes back to the previous solution and readjusts the position of
queen Q2. Q2 is moved from positions (2, 3) to (2, 4). Partial solution is <1, 4>
• Now, Q3 can be placed at position (3, 2). Partial solution is <1, 4, 3>.
• Queen Q4 cannot be placed anywhere in row four. So again, backtrack to the previous
solution and readjust the position of Q3. Q3 cannot be placed on (3, 3) or(3, 4). So the
algorithm backtracks even further.
• All possible choices for Q2 are already explored, hence the algorithm goes back to partial
solution <1> and moves the queen Q1 from (1, 1) to (1, 2). And this process continues until
a solution is found.

Bracktracking Page 1.7


Algorithm - isValid(board, row, col) Algorithm - solveNQueen(board, col)
• Input: The chess board, row and the column of the Input − The chess board, the col where the queen is
board. trying to be placed.
• Output − True when placing a queen in row and place Output − The position matrix where queens are placed.
position is a valid or not.
BEGIN
BEGIN
IF all columns are filled, THEN
IF there is a queen at the left of current col, THEN
RETURN true
RETURN false
FOR each row of the board, DO
IF there is a queen at the left upper diagonal, THEN
IF isValid(board, i, col), THEN
RETURN false
set queen at place (i, col) in the board
IF there is a queen at the left lower diagonal, THEN
IF solveNQueen(board, col+1) = true, THEN
RETURN false;
RETURN true
RETURN true //otherwise it is valid place
OTHERWISE remove queen from place (i, col) from
END board.
DONE
RETURN FALSE

Bracktracking Page 1.13


RETURN FALSE
END

Time Complexity Analysis


• The isvalid method takes O(n) time
• For each invocation of loop in solvnqueen, it runs for O(n) time
• The isvalid condition is present in the loop and also calls solvenqueen which is recursive
• Adding this up, the recurrence relation is:
𝟐

• solving the above recurrence by iteration or recursion tree, the time complexity of the
nQueen problem is = O(N!)

Bracktracking Page 1.19

You might also like