0% found this document useful (0 votes)
2 views6 pages

Assignment 10

The document outlines an assignment focused on solving the N-Queens Problem using a backtracking algorithm, aiming to place N queens on an N×N chessboard without any two queens threatening each other. It explains the backtracking approach, key concepts, algorithm steps, and includes a code implementation for the solution. The assignment emphasizes understanding backtracking in constraint satisfaction problems and demonstrates the algorithm's efficiency for small values of N.

Uploaded by

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

Assignment 10

The document outlines an assignment focused on solving the N-Queens Problem using a backtracking algorithm, aiming to place N queens on an N×N chessboard without any two queens threatening each other. It explains the backtracking approach, key concepts, algorithm steps, and includes a code implementation for the solution. The assignment emphasizes understanding backtracking in constraint satisfaction problems and demonstrates the algorithm's efficiency for small values of N.

Uploaded by

adityaparade20
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

ASSIGNMENT NO.

10
Title: N-Queens Problem (Backtracking Algorithm)
Aim:
To place N queens on an N×N chessboard such that no two queens threaten
each other and implement the backtracking technique to solve this problem.
Relevant Course Objectives:
● To understand the concept of backtracking and its application in solving
constraint satisfaction problems.
● To develop problem-solving techniques using recursive algorithms for
optimization problems.
Theory:
N-Queens Problem:
● The N-Queens problem is a classic backtracking problem where the goal
is to place N queens on an N×N chessboard such that no two queens can
attack each other.
● A queen can attack another if they are on the same row, same column,
or the same diagonal.
Backtracking Approach:
● Backtracking is a recursive algorithm for solving constraint satisfaction
problems. It incrementally builds candidates for solutions and abandons
a candidate as soon as it determines that the candidate cannot lead to a
valid solution.
Key Concepts in N-Queens:
1. Row-wise Placement:
o The algorithm places queens one by one starting from the first
row and proceeds row by row.
2. Safe Position Check:
o For each queen placement in a row, check if placing the queen at
a particular column is safe by ensuring there are no queens
already placed in the same column, upper-left diagonal, or upper-
right diagonal.
3. Backtracking:
o If placing a queen in the current row results in no safe position for
queens in the next row, backtrack by removing the queen and
trying another column in the current row.
Algorithm Analysis:
● Time Complexity:
o The N-Queens problem has a time complexity of O(N!) because
for each row, there are N possibilities for placing a queen, and the
number of remaining possibilities reduces by one with each
subsequent row.
● Space Complexity:
o The space complexity is O(N²) for the chessboard and additional
space for storing the positions of queens.
Steps in N-Queens Algorithm:
1. Initialize the Chessboard:
o Create an N×N chessboard and initialize it with no queens placed.
2. Place Queens Row by Row:
o Start placing queens in the first row. For each row, attempt to
place a queen in each column.
3. Safe Position Check:
o For each queen placement, check if placing the queen in that
column would lead to a clash with any previously placed queens
by ensuring:
▪ There is no other queen in the same column.
▪ There is no other queen on the upper-left diagonal.
▪ There is no other queen on the upper-right diagonal.
4. Backtracking:
o If a safe position is found, place the queen and proceed to the
next row.
o If no safe position is found in the current row, backtrack to the
previous row and try a different column.
5. Recursive Placement:
o Recursively repeat the process for each row. If all queens are
successfully placed, a solution is found. If no valid solution is found
for a row, backtrack.
6. Output the Solution:
o Once all queens are successfully placed on the board without
clashes, output the solution (e.g., the board configuration).
Algorithm Steps:
1. Place Queen in a Row:
o Start with the first row.
o For each column in the row, check if placing a queen there is safe.
o If safe, place the queen and move to the next row.
2. Check for Clashes:
o Ensure that no other queens are placed in the same column or
diagonals by checking:
▪ Columns: No queen in the same column.
▪ Diagonals: No queen on the upper-left or upper-right
diagonals.
3. Backtrack if Necessary:
o If no valid position is found in a row, backtrack to the previous
row, remove the queen, and try another column.
4. Repeat until All Rows are Filled:
o Continue placing queens row by row, backtracking when
necessary, until all rows are filled with queens or no solution is
found.
Algorithm Analysis:
● Time Complexity:
o O(N!): The problem explores N positions in the first row, then N-1
positions in the second row, and so on.
● Space Complexity:
o O(N²): For storing the board and tracking queen placements.
Output:
● The output will be the N×N chessboard with queens placed such that no
two queens can attack each other.
Case Study Example:
If N=4, a possible solution is:
_Q__
___Q
Q___
__Q_
CODE:
//queen

#include <iostream>
using namespace std;

bool isSafe(int** board, int row, int col, int N) {


for (int i = 0; i < col; i++) {
if (board[row][i]) {
return false;
}
}

for (int i = row, j = col; i >= 0 && j >= 0; i--, j--) {


if (board[i][j]) {
return false;
}
}

for (int i = row, j = col; i < N && j >= 0; i++, j--) {


if (board[i][j]) {
return false;
}
}
return true;
}

bool solveNQUtil(int** board, int col, int N) {


if (col >= N) {
return true;
}

for (int i = 0; i < N; i++) {


if (isSafe(board, i, col, N)) {
board[i][col] = 1;

if (solveNQUtil(board, col + 1, N)) {


return true;
}

board[i][col] = 0;
}
}

return false;
}

void printSolution(int** board, int N) {


for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
cout << " " << board[i][j] << " ";
}
cout << endl;
}
}

bool solveNQ(int N) {

int** board = new int*[N];


for (int i = 0; i < N; i++) {
board[i] = new int[N]();
}

if (!solveNQUtil(board, 0, N)) {
cout << "Solution does not exist" << endl;
return false;
}

printSolution(board, N);

for (int i = 0; i < N; i++) {


delete[] board[i];
}
delete[] board;
return true;
}

int main() {
int N;
cout << "Enter the size of the board (N): ";
cin >> N;

solveNQ(N);
return 0;
}

OUTPUT:
Enter the size of the board (N): 4
0 0 1 0
1 0 0 0
0 0 0 1
0 1 0 0
Enter the size of the board (N): 3
Solution does not exist
Enter the size of the board (N): 5
1 0 0 0 0
0 0 0 1 0
0 1 0 0 0
0 0 0 0 1
0 0 1 0 0

Conclusion:
In this assignment, we solved the N-Queens Problem using a backtracking
algorithm. The solution was obtained by recursively placing queens on the
chessboard and checking for clashes. If a valid configuration was not found in
any row, the algorithm backtracked to try other positions. The backtracking
approach efficiently explored all possible solutions, ensuring that the queens
were placed in safe positions without being attacked. This assignment helped
in understanding the concept of backtracking and its application to constraint
satisfaction problems, providing an efficient method to solve the N-Queens
problem within a reasonable time for small values of N.

You might also like