Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
SlideShare a Scribd company logo
The n-queen
problem
Prepared by::
SUSHANT GOEL
(b090010291)
SUKRIT GUPTA
(b090010285)
Introduction
• N-Queens dates back to the 19th century
(studied by Gauss)
• Classical combinatorial problem, widely used
as a benchmark because of its simple and regular structure
• Problem involves placing N queens on an N  N chessboard
such that no queen can attack any other
• Benchmark code versions include finding the first solution and
finding all solutions
The n-queen problem
• What is its input??
• Ans. Positive integer n
• What is its task??
• Ans. Place n queens on an n by n chessboard so that no two
queens attack each other (on same row, column, diagonal), or
report that this is impossible.
• Solving particular problem for the different values of
n=1,2,3,4.
• Pure brute force search
• 16 squares on a 4 by 4 chessboard
• Leads to 16 * 15 * 14 * 13 = 43,680 possible solutions
For the larger values of n
n=8
• At most one queen per row: 88 = 16,777,216
• Early backtracking: 2057 nodes total
• Time to find first solution: 114 nodes
n=12
• At most one queen per row: 1212
• Early backtracking: 856,189 nodes total
• Time to find first solution: 262 nodes
N-Queens Solutions
• Various approaches to the problem
• Brute force
• Local search algorithms
• Backtracking
• Divide and conquer approach
• Permutation generation
• Mathematical solutions
• Graph theory concepts
Example:-
1 1
. . 2
1
2
. . . .
1
2
. 3
1
2
3
. . 4
1
. . . 2
11
2
3
. . . .
The backtracking method
• A given problem has a set of constraints and possibly an
objective function
• The solution optimizes an objective function, and/or is
feasible.
• We can represent the solution space for the problem
using a
• The root of the tree represents 0 choices,
• Nodes at depth 1 represent first choice
• Nodes at depth 2 represent the second choice, etc.
• In this tree a path from a root to a leaf represents a
candidate solution
Backtracking approach
• One of the only approaches that guarantees a solution,
though it can be slow
• Can be seen as a form of intelligent depth-first search
• Complexity of backtracking typically rises exponentially
with problem size
• Good test case for performance analysis of RC systems,
as the problem is complex even for small data size
• Traditional processors provide a suboptimal platform for
this iterative application due to serial nature of their
processing pipelines
• Tremendous speedups achieved by adding parallelism at
the logic level via RC
Algorithm
• AlgorithmNQueens(k,n)
• //Using backtracking,this procedure prints all possible
//placements of n queens on an n*n
//chessboard so that they are non attacking
{
for(i=1 to n ) do
{
if Place(k,i) then
{
x[k]=I
If(k=n) then write (x[1:n]);
else Nqueens(k+1,n);
}
}
}
• Algorythm Place(k,i)
{
For j=1 to k-1 do
if((x[j]=i) //same colum or (Abs(x[j]-i)=Abs(j-k)))//same
diagonal
then return false;
Return true;
}
Thank you

More Related Content

The n Queen Problem

  • 1. The n-queen problem Prepared by:: SUSHANT GOEL (b090010291) SUKRIT GUPTA (b090010285)
  • 2. Introduction • N-Queens dates back to the 19th century (studied by Gauss) • Classical combinatorial problem, widely used as a benchmark because of its simple and regular structure • Problem involves placing N queens on an N  N chessboard such that no queen can attack any other • Benchmark code versions include finding the first solution and finding all solutions
  • 3. The n-queen problem • What is its input?? • Ans. Positive integer n • What is its task?? • Ans. Place n queens on an n by n chessboard so that no two queens attack each other (on same row, column, diagonal), or report that this is impossible. • Solving particular problem for the different values of n=1,2,3,4. • Pure brute force search • 16 squares on a 4 by 4 chessboard • Leads to 16 * 15 * 14 * 13 = 43,680 possible solutions
  • 4. For the larger values of n n=8 • At most one queen per row: 88 = 16,777,216 • Early backtracking: 2057 nodes total • Time to find first solution: 114 nodes n=12 • At most one queen per row: 1212 • Early backtracking: 856,189 nodes total • Time to find first solution: 262 nodes
  • 5. N-Queens Solutions • Various approaches to the problem • Brute force • Local search algorithms • Backtracking • Divide and conquer approach • Permutation generation • Mathematical solutions • Graph theory concepts
  • 6. Example:- 1 1 . . 2 1 2 . . . . 1 2 . 3 1 2 3 . . 4 1 . . . 2 11 2 3 . . . .
  • 7. The backtracking method • A given problem has a set of constraints and possibly an objective function • The solution optimizes an objective function, and/or is feasible. • We can represent the solution space for the problem using a • The root of the tree represents 0 choices, • Nodes at depth 1 represent first choice • Nodes at depth 2 represent the second choice, etc. • In this tree a path from a root to a leaf represents a candidate solution
  • 8. Backtracking approach • One of the only approaches that guarantees a solution, though it can be slow • Can be seen as a form of intelligent depth-first search • Complexity of backtracking typically rises exponentially with problem size • Good test case for performance analysis of RC systems, as the problem is complex even for small data size • Traditional processors provide a suboptimal platform for this iterative application due to serial nature of their processing pipelines • Tremendous speedups achieved by adding parallelism at the logic level via RC
  • 9. Algorithm • AlgorithmNQueens(k,n) • //Using backtracking,this procedure prints all possible //placements of n queens on an n*n //chessboard so that they are non attacking { for(i=1 to n ) do { if Place(k,i) then { x[k]=I If(k=n) then write (x[1:n]); else Nqueens(k+1,n); } } }
  • 10. • Algorythm Place(k,i) { For j=1 to k-1 do if((x[j]=i) //same colum or (Abs(x[j]-i)=Abs(j-k)))//same diagonal then return false; Return true; }