ADA N Queens
description
Transcript of ADA N Queens
N-Queens Problemfor Design and Analysis of Algorithms
Pritha P| Shilpa RK
The problem● Consider an n by n chessboard. Is it possible
to place n queens on the board without the queens threatening one another?
● Two queens cannot be in the sameo rowo columno diagonal
Explicit constraints
If n is the number of queens, or size of the chessboard,● solution space S={1, 2, 3...n}● solution vector is an n-tuple {x1, x2,
x3...xn} where xi indicates column number
Implicit constraints
All xi values are distinct to prevent two Queens being in the same column and competing for dominance.
Solution by backtrackingWhat is backtracking?● Depth-first generation with a bounding
functionKey terms:● live node● E node● dead node● bounding function
4 Queens problemFor simplicity, we consider a 4x4 chessboard:We are given: ● 4 queens Q Q Q Q ● 4x4 chessboard
o X X X X X X X X X X X X X X X X
Walk-through of solution● This is how the chessboard looks:
o X X X X X X X X X X X X X X X X
● We need to fit 4 queens Q on here
Walk-through of solution● Start by placing a queen in the first column
o Q1 X X X X X X X X X X X X X X X
● Proceed to the next row, try placing Q2 in the first column
Walk-through of solution● This raises a conflict, so move Q2 one step
to the righto Q1 X X X
X Q2 X X X X X X X X X X
● Even this threatens Q1, move once to the righto Q1 X X X
X X Q2 X X X X X X X X X
Walk-through of solution● Next row; Q3 cannot be placed without
threatening Q1 and Q2● Backtrack to row2, shift Q2 once to right:
o Q1 X X X X X X Q2 X X X X X X X X
● Now, in row3, keep shifting Q3 right from column till a comfortable place is reached for it
Walk-through of solution● Now we have
o Q1 X X X X X X Q2 X Q3 X X X X X X
● In row4, we find there is no safe spot for Q4● Backtrack all the way to Q1, shift it right
o X Q1 X X X X X Q2X Q3 X X X X X X
Walk-through of solution● Q1 is threatened by Q3, but safe from Q2, so
we focus on row3● Place Q3 in column1
o X Q1 X X X X X Q2Q3 X X X X X X X
[It’s almost over, now!]
Walk-through of solution● On to row4, starting from column1, shift Q4
right till it reaches a safe spot:● X Q1 X X
X X X Q2Q3 X X X X X Q4 X
● This is the final solution. No queen competes with any other, they’re all safe from attack.
This is basically what we justaccomplished
Pictorial of the solution
State-space treeThe state-space tree becomes easier to understand once we’ve already examined the solution.
State-space treeEach node is representedas n(i,j)
8 Queen’s problem8 Queen’s problem, solved!
Fundamental solutions of 8 Queens problem
Pseudocode● To mark feasible spots for their Majesties, the
Queensboolean validPosition( k ) for i = 0 to k – 1 do /*If two queens are on the same row or same diagonal*/ if ( Qi = Qk ) OR ( abs( Qi – Qk ) = abs( i – k ) )
return false return true
Pseudocode● Now, we actually put the Queens in their
respective places
placeQueens( N ) Q0 = 0 k = 0 /*Start with Q0 on row 0 */
Pseudocodewhile ( k < N ) do while( ( k < N ) AND ( validPosition( k ) is false ) ) do Qk = Qk + 1 /*Advance this queen one row */
Pseudocodeif( ( k = N – 1 ) AND ( Qk < N ) ) /*All queens are validly placed*/ print solution ( Q0, .. , QN-1 ) STOP else if ( ( k < N – 1 ) AND ( Qk < N ) ) k = k + 1 /* Not done yet; Now try to place the next queen*/ Qk = 0 else /*The positions of the first k queens cannot possible leadto a solution. So, we must backtrack.*/k = k – 1
Pseudocodeif( k < 0 ) /*We have not found any position for the first queen that could lead to a solution :( */print “no solution possible” STOP else Qk = Qk + 1 /*Advance this queen (the one we backtracked to) one more space*/
Applications● parallel memory storage schemes● VLSI testing● traffic control● deadlock prevention● The problem is also illustrative of more
practical problems whose solutions are permutations.o One such problem is the travelling salesperson
problem
References ● Algorithm Analysis and Design. (n.d.). Retrieved from http://www.academic.marist.edu/~jzbv/algorithms/
● Broeg, R. (n.d.). Pseudocode for N-Queens problem. Retrieved from
http://www.wou.edu/~broegb/Cs345/nqueens.pdf
● Levitin, A. (n.d.). Coping with the Limitations of Algorithm Power. In Introduction to the Design and Analysis of
Algorithms. Pearson.
● The n-Queens Problem. (n.d.). INFORMS Transactions on Education.
● Nandagopalan, S. (n.d.). Back-Tracking and Branch-and-Bound. In Analysis and Design of Algorithms. Sapna.● Letavec, C. and J. Ruggiero (2002), "The n-Queens Problem" INFORMS Transactions on Education, Vol. 2, No.
3, http://ite.informs.org/Vol2No3/LetavecRuggiero/