0% found this document useful (0 votes)
4 views10 pages

n Queens Problem

The N-Queens problem involves placing N queens on an N × N chessboard so that no two queens threaten each other, typically solved using backtracking. The algorithm recursively attempts to place queens in safe positions, backtracking when necessary, with a worst-case time complexity of O(N!) and space complexity of O(N). Applications of this problem include artificial intelligence, puzzle solving, and scheduling problems.

Uploaded by

vabinaya394
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views10 pages

n Queens Problem

The N-Queens problem involves placing N queens on an N × N chessboard so that no two queens threaten each other, typically solved using backtracking. The algorithm recursively attempts to place queens in safe positions, backtracking when necessary, with a worst-case time complexity of O(N!) and space complexity of O(N). Applications of this problem include artificial intelligence, puzzle solving, and scheduling problems.

Uploaded by

vabinaya394
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

N-QUEENS PROBLEM

prepared by: Aswini J (ES24CS14)


Definition:
• The N-Queens problem is the task of placing N queens on an N × N
chessboard so that no two queens threaten each other, typically solved using
the backtracking technique.
• Place N queens on an N × N chessboard such that:
No two queens are in the same row
No two queens are in the same column
No two queens are in the same diagonal
• Backtracking technique:
Backtracking is an algorithmic technique used to solve
problems recursively.
• Algorithm Steps:

Step 1: Start from the first row.


Step 2: Try placing the queen in each column.
Step 3: Check if the position is safe.
Step 4: If safe, place the queen and move to the next row.
Step 5: If not safe, try the next column.
Step 6: If all columns fail, backtrack
Example:
• For n = 1, the problem has a trivial solution

• For n = 2, it is easy to see that there is no solution to place 2 queens in 2


× 2 chessboard.

• For n = 3, it is easy to see that there is no solution to place 3 queens


in 3 × 3 chessboard
For n = 4, There is solution to place 4 queens in 4 × 4 chessboard. the four-queens
problem solved by the backtracking technique.

Step 1: Start with the empty board

Step 2: Place queen 1 in the first possible position of its row, which is in column 1 of
row 1.
Step 3: place queen 2, after trying unsuccessfully columns 1 and 2, in the first
acceptable position for it, which is square (2, 3), the square in row 2 and column 3.

Step 4: This proves to be a dead end because there is no acceptable position for
queen 3. So, the algorithm backtracks and puts queen 2 in the next possible
position at (2, 4)

Step 5: Then queen 3 is placed at (3, 2), which proves to be another dead end.
Step 6: The algorithm then backtracks all the way to queen 1 and moves it to
(1, 2).

Step 7: The queen 2 goes to (2, 4). Step 8: The queen 3 goes to (3, 1).

Step 9: The queen 3 goes to (4, 3). This is a solution to the problem.
The state-space tree diagram of this search:
Time Complexity:

Worst Case Time Complexity: O(N!)


Because all possible placements may be checked.

Space Complexity:

O(N) for storing queen positions.

Applications:

Artificial Intelligence
Puzzle solving
Constraint satisfaction problems
Scheduling problems
Thank You

You might also like