ADA N Queens

25
N-Queens Problem for Design and Analysis of Algorithms Pritha P| Shilpa RK

description

This was made as part of my presentation for my Design and Analysis of Algorithms class. It explains how the N Queens problem is solved using two methods-- backtracking, and mirroring.

Transcript of ADA N Queens

Page 1: ADA N Queens

N-Queens Problemfor Design and Analysis of Algorithms

Pritha P| Shilpa RK

Page 2: ADA N Queens

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

Page 3: ADA N Queens

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

Page 4: ADA N Queens

Implicit constraints

All xi values are distinct to prevent two Queens being in the same column and competing for dominance.

Page 5: ADA N Queens

Solution by backtrackingWhat is backtracking?● Depth-first generation with a bounding

functionKey terms:● live node● E node● dead node● bounding function

Page 6: ADA N Queens

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

Page 7: ADA N Queens

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

Page 8: ADA N Queens

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

Page 9: ADA N Queens

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

Page 10: ADA N Queens

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

Page 11: ADA N Queens

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

Page 12: ADA N Queens

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!]

Page 13: ADA N Queens

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.

Page 14: ADA N Queens

This is basically what we justaccomplished

Pictorial of the solution

Page 15: ADA N Queens

State-space treeThe state-space tree becomes easier to understand once we’ve already examined the solution.

Page 16: ADA N Queens

State-space treeEach node is representedas n(i,j)

Page 17: ADA N Queens

8 Queen’s problem8 Queen’s problem, solved!

Page 18: ADA N Queens

Fundamental solutions of 8 Queens problem

Page 19: ADA N Queens

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

Page 20: ADA N Queens

Pseudocode● Now, we actually put the Queens in their

respective places

placeQueens( N ) Q0 = 0 k = 0 /*Start with Q0 on row 0 */

Page 21: ADA N Queens

Pseudocodewhile ( k < N ) do while( ( k < N ) AND ( validPosition( k ) is false ) ) do Qk = Qk + 1 /*Advance this queen one row */

Page 22: ADA N Queens

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

Page 23: ADA N Queens

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*/

Page 24: ADA N Queens

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

Page 25: ADA N Queens

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/