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

Experiment No. 8- Implement a Program for 8 Queens Problem (N- Queens Problem) Using Backtracking.docx

The document outlines an experiment to implement the 8 Queens Problem using Backtracking, aiming to analyze its complexity. Students will learn to compute the best-case and worst-case complexities of the problem, which involves placing N queens on an N x N chessboard without conflicts. The algorithm and theory behind Backtracking are discussed, concluding that the complexity of the N-Queens Problem is O(n!).

Uploaded by

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

Experiment No. 8- Implement a Program for 8 Queens Problem (N- Queens Problem) Using Backtracking.docx

The document outlines an experiment to implement the 8 Queens Problem using Backtracking, aiming to analyze its complexity. Students will learn to compute the best-case and worst-case complexities of the problem, which involves placing N queens on an N x N chessboard without conflicts. The algorithm and theory behind Backtracking are discussed, concluding that the complexity of the N-Queens Problem is O(n!).

Uploaded by

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

Experiment No.

: 8

Aim: Implement a program for 8 Queens Problem (N-Queens Problem) using


Backtracking
.
Objectives: To implement and analyze complexity of 8 Queens Problem (N- Queens Problem)
using Backtracking.

Outcomes: Students will be able to compute the best-case, worst-case complexity of 8 Queens
Problem (N- Queens Problem) using Backtracking.

Hardware / Software Required: Turbo C/ Notepad/ Eclipse/ IDEs/ code editors, and online
compilers.

Problem Definition:
In this problem N queens are to be placed on N x N chessboard
​ - No two queens must be in same Row,
​ - No two queens must be in same Column
​ - No two queens must be in same Diagonal
All the solutions to the n-queens problem can be represented as n tuples(x1,x2,….,xn) where xi
is the column on which queen i is placed. Solution space consists of n! Permutations of n
tuple (1,2,-----,n).

Theory:
Backtracking is the refinement of the brute force approach, which systematically searches for
a solution to a problem among all available options. It does so by assuming that the solutions
are represented by vectors (v1,v2,…..,vm) of values and by traversing the domains of vectors
until the solutions are found.

Algorithm:
Algorithm n_queen (row,n)
//Problem Description: Implementation of n queen Problem
//Input: no.of queens
for col←1 to n do

if (place (row, col)) then


​ ​ x(row)=col
​ ​ if (row = n) then
​ ​ ​ print x[1:n]
else
n_queen (row+1,n)
Algorithm place (row, col)
// it returns true if queen can be placed in proper place else return false
for j=1 to row-1
if (x(j) =col) //two are in same column
​ ​ OR abs(x(j)-col)=abs(j-row) //in same diagonal
return false
return true
Conclusion: Thus it is observed that the complexity of N-Queen is O(n!) because we are
selecting if we can put or not put a Queen at that place.

You might also like