Professional Documents
Culture Documents
CHAPTER 1
INTRODUCTION
1.1 GENERAL INTRODUCTION
Chess composer Max Bezzel published the eight queens puzzle in 1848. Franz Nauck
published the first solutions in 1850.Nauck also extended the puzzle to the n queens problem,
with n queens on a chessboard of n × n squares.
Since then, many mathematicians, including Carl Friedrich Gauss, have worked on both the
eight queens puzzle and its generalized n-queens version. In 1874, S. Gunther proposed a
method using determinants to find solutions.J.W.L. Glaisher refined Gunther's approach.
In 1972, Edsger Dijkstra used this problem to illustrate the power of what he called
structured programming. He published a highly detailed description of a depth-first
backtracking algorithm.
The classic example of the use of backtracking is the eight queens puzzle, that asks for all
arrangements of eight chessqueens on a standard chessboard so that no queen attacks any
other. In the common backtracking approach, the partial candidates are arrangements of k
queens in the first k rows of the board, all in different rows and columns. Any partial solution
that contains two mutually attacking queens can be abandoned.
The problem can be quite computationally expensive, as there are 4,426,165,368 (i.e., 64C8)
possible arrangements of eight queens on an 8×8 board, but only 92 solutions. It is possible
to use shortcuts that reduce computational requirements or rules of thumb that avoids brute-
force computational techniques. For example, by applying a simple rule that constrains each
queen to a single column (or row), though still considered brute force, it is possible to reduce
the number of possibilities to 16,777,216 (that is, 88 ) possible combinations. Generating
permutations further reduces the possibilities to just 40,320 (that is, 8!), which are then
checked for diagonal attacks.
Martin Richards published a program to count solutions to the n- queens problem using
bitwise operations.[3] . However, this solution has already been published by Zongyan Qiu
Department of MCA, NHCE 2018-2019 1
N QUEEN PROBLEM 1NH18MCA37
o Chess variations
Related problems can be asked for chess variations such as shogi. For
instance, the n+k dragon kings problem asks to place k shogi
paws and n+k mutually nonattacking dragon kings on an n×n shogi
board.[6]
o Permutation matrix
In mathematics, a permutation matrix can be regarded geometrically as
a set of n points lying on the squares of a n×n chessboard, such that
each row or column contains only one point. Thus, an order-
n permutation matrix is a solution to an n-rooks puzzle.
Department of MCA, NHCE 2018-2019 2
N QUEEN PROBLEM 1NH18MCA37
o Nonstandard boards
Pólya studied the n queens problem on a toroidal ("donut-shaped")
board and showed that there is a solution on an n×n board if and only
if n is not divisible by 2 or 3.[7] In 2009 Pearson and Pearson
algorithmically populated three-dimensional boards (n×n×n)
with n2 queens, and proposed that multiples of these can yield
solutions for a four-dimensional version of the puzzle.[8][better source needed]
o Domination
Given an n×n board, the domination number is the minimum number
of queens (or other pieces) needed to attack or occupy every square.
For n = 8 the queen's domination number is 5.
o Queens and other pieces
Variants include mixing queens with other pieces; for example,
placing m queens and m knights on an n×n board so that no piece
attacks another[9] or placing queens and pawns so that no two queens
attack each other.[10][better source needed]
o Magic squares
In 1992, Demirörs, Rafraf, and Tanik published a method for
converting some magic squares into n-queens solutions, and vice
versa.[11]
o Latin squares
In an n×n matrix, place each digit 1 through n in n locations in the
matrix so that no two instances of the same digit are in the same row
or column.
o Exact cover
Consider a matrix with one primary column for each of the n ranks of
the board, one primary column for each of the n files, and one
secondary column for each of the 4n − 6 nontrivial diagonals of the
board. The matrix has n2 rows: one for each possible queen placement,
and each row has a 1 in the columns corresponding to that square's
rank, file, and diagonals and a 0 in all the other columns. Then
the n queens problem is equivalent to choosing a subset of the rows of
The n-queens problem is proposed for the first time in 1850 by Carl Gauss. It is to
determine a placement of n queens on an n × n chessboard, such that no two queens can
attack each other [3]. This problem falls in a special class of problems well
known as NP hard, whose solution cannot be found
out in polynomial time. Let’s consider the 8-queen problem, which is computationally
very expensive since the total number of possible arrangements queen is 64! / (56! x 8!)
~ 4.4 x 1 and the total number of possible solutions are 92 [1]. But there exists only 12
unique solutions. There are some solutions that are to be the same and can be obtained one
from the other by taking rotations or symmetry. The n-queen problem follows the same rules
as in 8-queen problem with N queens and an n x n chessboard. Current knowledge of
total number of solutions of n-queens problem can be viewed from the Table 1.
A Novel Quantum N-Queens Solver Algorithm and its Simulation and Application to
Satellite Communication Using IBM Quantum Experience
The backtracking algorithm enumerates a set of partial candidates that, in principle, could be
completed in various ways to give all the possible solutions to the given problem. The
completion is done incrementally, by a sequence of candidate extension steps.
Conceptually, the partial candidates are represented as the nodes of a tree structure, the
potential search tree. Each partial candidate is the parent of the candidates that differ from it
by a single extension step; the leaves of the tree are the partial candidates that cannot be
extended any further.
The backtracking algorithm traverses this search tree recursively, from the root down, in
depth-first order. At each node c, the algorithm checks whether c can be completed to a valid
solution. If it cannot, the whole sub-tree rooted at c is skipped (pruned). Otherwise, the
algorithm (1) checks whether c itself is a valid solution, and if so reports it to the user; and
(2) recursively enumerates all sub-trees of c. The two tests and the children of each node are
defined by user-given procedures.
Therefore, the actual search tree that is traversed by the algorithm is only a part of the
potential tree. The total cost of the algorithm is the number of nodes of the actual tree times
the cost of obtaining and processing each node. This fact should be considered when
choosing the potential search tree and implementing the pruning test.
The N Queen is the problem of placing N chess queens on an N×N chessboard so that no two
queens attack each other. For example, following are two solutions for 4 Queen problem.
CHAPTER 2
REVIEW OF LITERATURE
Backtracking Strategy
Backtracking algorithms are applicable for NP-Complete problems. Priestley and
Ward [1] presented the details about the backtracking and its applications. They have
clearly explained the preliminaries of the algorithm and gave a clear picture of the 8-
queen puzzle. The solution for the problem could be achieved through tree structure
representation of the choices. The proposed procedure could reduce the number of
test cases to a sum of 15,720. The time complexity was reduced by reducing test
cases. Pre –analysis was used to reduce the test cases. They used bush pruning
technique for further improvement. Finally hybrid approach of pre-analysis and bust
pruning gave better result.
Ginsberg [2] introduced dynamic backtracking algorithm but that does not solve
constraint satisfaction problem dynamically. Gerald and Thomas [3] proposed some
alteration to support a dynamic constraint satisfaction. But this method suffers due to
heavy time complexities.
John Gaschnig [12] proposed a fast backtracking algorithm which is reduced its
computational complexities by eliminating redundant tests. He tried to exploit space-
time trade off maximum in his algorithm. According to John, it is hard to eliminate all
redundant tests in computationally efficient manner. But it is possible to eliminate all
redundant test cases with heuristic approach. In [13], Jordan Bell and Brett Stevens
discussed the computational approaches of n queen’s problem. In this survey paper
they have given concentration for different approaches and results for the same
problem.
Erbak and Tanik given a detailed study of algorithms used for n-queens problem in
[14]. They grouped the total algorithms in different categories based on the outcome
of the algorithms. As per their view they are three types of algorithms based on their
outcomes. Some of them generate all solutions and others are produce fundamental or
subset of total number of solutions. Brute-force trial and error and backtracking are
examples of algorithms which generate complete solutions. The algorithms based on
group properties of results, symmetric elimination and test-based produce only basic
solutions.
CHAPTER 3
SYSTEM CONFIGURATION
It does not need any additional hardware and software to operate the program, the following
requirements should strongly maintained
4. 20 MB of memory space
CHAPTER 4
MODULE DESCRIPTION
4.1 MODULE 1
N QUEEN
Input:
NxN Matrix
Process:
Using Backtracking Algorithm searching the location of queen
Output:
Displaying the possibility queen in the matrix
CHAPTER 5
SYSTEM DESIGN
5.1 DFD / UML Diagrams
NxN Matrix
N QUEEN
DFD
The Data Flow Diagram shows the flow of data through a system. It views the system
as a function that transform the input to the desired output, it contains the number of
symbols that is use to represent the system. DFD describes about the modules and
their attributes in such a manner that each and every one can understand it easily.
Dot_Table
Retrive
RetriveNodes
Nodes
EnterNodes
Enter Nodes Update
UpdateNodes
Nodes
Retrive Nodes
Display Display
User
DISPLAY
Update Nodes
Department of MCA, NHCE 2018-2019 14
N QUEEN PROBLEM 1NH18MCA37
CHAPTER 6
SYSTEM IMPLEMENTATION
6.1 IMPLEMENTATION
N Queen problem implementation generally consists of placing queens in a particular
row and column and continuing to place the next queen in next row and column without
violating the same row, same column and same diagonal constraint. The initial permutation
consists of placing a queen in first row. There are n columns in the first row offering a
possible of n combinations in first row. For every column in the first row there are n columns
that are available for placing a queen in the second row raising the number of combinations
in the second row to n*n combinations. At nth row there are nn combinations. This can be
reduced to a minimum by using the following constraints No two queens can be placed in the
same row No two queens can be placed in the same column No two queens can be placed in
the same diagonal The general algorithm is to place the queen and move till the nth row and
backtrack. This can be optimized by checking for the constraints when a column is assigned
for every row. So while traversing the recursion tree the constraints are applied at that level
before proceeding to the children. When the constraints are put we can decide whether the
current node from root accepts or violates all the constraints. If the node is accepted the
control shifts to finding a suitable column for the next row. When there is violation in
constraints the node at that level can never lead to any valid solutions and so the tree is
pruned at that node by returning to the parent to consider the next column assignment. This
leads to reduction in the computing needed.
6.2ALGORITHM
Algorithm:
1. Place the queens column wise, start from the left most column
2. If all queens are placed.
1. return true and print the solution matrix.
3. Else
1. Try all the rows in the current column.
6.3 CODING
#include<stdio.h>
#include<conio.h>
#include<math.h>
char a[10][10];
int n;
//print the new position of queen
void printqueen()
{
int i, j;
printf("\n");
}
/*checking the posiblity of the queen
can be placeing at the current position*/
int posiblity(int row, int col)
{
int i, tc;
for (i = 0; i < n; i++) {
tc = getprequeen(i);
//check in the all direction and in diagnal
if (col == tc || abs(row - i) == abs(col - tc))
return 0;
//if equal then no queen can place at this position
}
return 1;
}
if (row < n)
{
for (i = 0; i < n; i++)
{
if (posiblity(row, i))
{
Department of MCA, NHCE 2018-2019 17
N QUEEN PROBLEM 1NH18MCA37
a[row][i] = 'Q';
//check next posiable queen otherwise backtraking
nqueen(row + 1);
a[row][i] = '.';
}
}
}
else
{
if(n==m)
{
nqueen(0);
Department of MCA, NHCE 2018-2019 18
N QUEEN PROBLEM 1NH18MCA37
}
else
{
printf("there should be square matrix");
}
getch();
return (0);
6.4 APPLICATION
Matrix Equal
Possible Output 1:
Possible Output 2:
Possible Output 3:
Possible Output 4:
CHAPTER 7
SYSTEM TESTING
Testing is the activity where the errors remaining from all the previous phases must
be detected. Hence, testing performs a very critical role for ensuring quality.
During testing, the software to be tested is executed with a set of test cases, and the
behavior of the system for the test cases is evaluated to determine if the system is performing
as expected. Clearly, the success of testing in revealing errors depends critically on the test
cases.
Testing is the major quality control measure used during the software development its
basic function is to detect errors in the software.
Thus a goal of testing is to uncover requirement, design, and coding errors in the
programs.
Level of testing:
Testing is done different level which includes the following:
1. Unit testing
2. Integration testing
3. System testing
4. Acceptance testing
Unit testing:
It is a starting point of testing; in this module is testing separately and
is performed by the coder himself simultaneously along with the coding of
the module.
Integration testing:
The modules are gradually integrated into subsystem, which are then
integrated to eventually from the entire system. During integration of module
integration testing is performed to detect design errors by focusing on testing
the interconnection between the modules.
System testing:
After the system put together, system testing is performed here the
system is testing against the system requirements.
CHAPTER 8
RESULTS & DISCUSSION
The eight queens puzzle has 92 distinct solutions. If solutions that differ only by the
symmetry operations of rotation and reflection of the board are counted as one, the puzzle
has 12 solutions. These are called fundamental solutions; representatives of each are
shown…below.
A fundamental solution usually has eight variants (including its original form) obtained by
rotating 90, 180, or 270° and then reflecting each of the four rotational variants in a mirror in
a fixed position. However, should a solution be equivalent to its own 90° rotation (as happens
to one solution with five queens on a 5×5 board), that fundamental solution will have only
two variants (itself and its reflection). Should a solution be equivalent to its own 180°
rotation (but not to its 90° rotation), it will have four variants (itself and its reflection, its 90°
rotation and the reflection of that). If n > 1, it is not possible for a solution to be equivalent to
its own reflection because that would require two queens to be facing each other. Of the 12
fundamental solutions to the problem with eight queens on an 8×8 board, exactly one
(solution 12 below) is 10 equal to its own 180° rotation, and none is equal to its 90° rotation;
thus, the number of distinct solutions is 11×8 + 1×4 = 92 (where the 8 is derived from four
90° rotational positions and their reflections, and the 4 is derived from two 180° rotational
positions and their reflections)
8.1 CONCLUSION
Finding all solutions to the eight queens puzzle is a good example of a simple
but nontrivial problem. For this reason, it is often used as an example problem for various
programming techniques, including nontraditional approaches such as constraint
programming, logic programming or genetic algorithms. Most often, it is used as an example
of a problem that can be solved with a recursive algorithm, by phrasing the n queens problem
inductively in terms of adding a single queen to any solution to the problem of placing n−1
queens on an n-by-n chessboard.
Department of MCA, NHCE 2018-2019 28
N QUEEN PROBLEM 1NH18MCA37
In N-Queen, as N value increases the time of processing also takes more time
.This happens when we try to display all the possible solutions.
Of course, we could make it much faster if we wanted to only find one solution
instead of all of them: not more than a few milliseconds for board sizes up to 50
Various techniques like brute force, permutations, greedy methods which can be used
to solve n queens problem but among all backtracking with recursion is very efficient. Since
backtracking with recursion is useful to solve the problem where the iteration is more
prominent in finding the solutions, it is used to solve n queens problem
The graphical simulation assists learners in understanding the solutions possible for n
queens problem using backtracking. The graphical interface is very efficient and enables the
users to visualize every solution of n queens problem.
8.2 LIMITATIONS
The major advantage of the backtracking algorithm is the abillity to find and count all
the possible solutions rather than just one while offering decent speed. In fact this is
the reason it is so widely used. Also one can easily produce a parallel version of the
backtracking algorithm increasing speed several times just by starting multiple threads
with different starting positions of the first queens.
CHAPTER 9
REFERENCES
9.1 TEXT REFERENCE