11
The n- queen problem Prepared by:: SUSHANT GOEL (b090010291) SUKRIT GUPTA (b090010285)

The n Queen Problem

Embed Size (px)

Citation preview

Page 1: The n Queen Problem

The n-queen problem

Prepared by::SUSHANT GOEL(b090010291)SUKRIT GUPTA(b090010285)

Page 2: The n Queen Problem

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

Page 3: The n Queen Problem

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

Page 4: The n Queen Problem

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

Page 5: The n Queen Problem

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

Page 6: The n Queen Problem

Example:-1 1

. . 21

2. . . .

12

. 3

12

3. . 4

1. . . 2

112

3. . . .

Page 7: The n Queen Problem

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

Page 8: The n Queen Problem

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

Page 9: The n Queen Problem

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]=IIf(k=n) then write (x[1:n]);else Nqueens(k+1,n);

}}

}

Page 10: The n Queen Problem

• 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 diagonalthen return false;

Return true;}

Page 11: The n Queen Problem

Thank you