Solving N+k Queens Using Dancing Links
description
Transcript of Solving N+k Queens Using Dancing Links
Solving N+k Queens Using Dancing Links
Matthew WolffMatthew Wolff
CS 499cCS 499c
May 3, 2006May 3, 2006
AgendaAgenda
MotivationMotivationDefinitionsDefinitionsProblem DefinitionProblem DefinitionSolved Problems with ResultsSolved Problems with ResultsFuture WorkFuture Work
MotivationMotivation
NASA EPSCoR grantNASA EPSCoR grantBegan working with Chatham and Skaggs in Began working with Chatham and Skaggs in
NovemberNovemberDoyle added DLX (Dancing Links) at Doyle added DLX (Dancing Links) at
beginning of semesterbeginning of semesterNew to me (and the rest of the team, I think)New to me (and the rest of the team, I think)A lot more work!A lot more work!
Category of ProblemsCategory of Problems
8 Queens8 Queens 8 attacking queens on an 8x8 chess board8 attacking queens on an 8x8 chess board
N QueensN Queens N attacking queens on an NxN chess boardN attacking queens on an NxN chess board
N+1 QueensN+1 Queens N+1 attacking queens on an NxN chess boardN+1 attacking queens on an NxN chess board 1 Pawn used to block two or more attacking queens1 Pawn used to block two or more attacking queens
N+k QueensN+k Queens N+k attacking queens on an NxN chess boardN+k attacking queens on an NxN chess board k Pawns used to block numerous attacking queensk Pawns used to block numerous attacking queens
8 Queens Example8 Queens Example
http://www.jsomers.com/nqueen_demo/nqueens.htmlhttp://www.jsomers.com/nqueen_demo/nqueens.html
SolutionsSolutions
SolutionsSolutions – A class of Queen placements such – A class of Queen placements such that no two Queens can attack each other.that no two Queens can attack each other.
Fundamental Solutions Fundamental Solutions – A class of solutions – A class of solutions such that all members of the class are simply such that all members of the class are simply rotations or reflections of one another.rotations or reflections of one another. Given the set of solutions, a set of fundamental Given the set of solutions, a set of fundamental
solutions can be generated. And vice versasolutions can be generated. And vice versa The fundamental solutions are a subset of all The fundamental solutions are a subset of all
solutions.solutions.
Fundamental Solutions for 8 Fundamental Solutions for 8 QueensQueens
http://mathworld.wolfram.com/QueensProblem.html
RecursionRecursion
"To "To understandunderstand recursionrecursion, one must first , one must first understandunderstand recursionrecursion" -- Tina Mancuso " -- Tina Mancuso
““A function is recursive if it can be called A function is recursive if it can be called while active (on the stack).”while active (on the stack).”i.e. It calls itselfi.e. It calls itself
Recursion in ArtRecursion in Art
Recursion in Computer ScienceRecursion in Computer Science
// precondition: n >= 0// precondition: n >= 0// postcondition: n! is returned// postcondition: n! is returnedfactorial (int n) {factorial (int n) {
if (n == 1) or (n == 0)if (n == 1) or (n == 0)return 1;return 1;
elseelsereturn (n*factorial(n-1));return (n*factorial(n-1));
}}
BacktrackingBacktracking
An example of backtracking is used in a An example of backtracking is used in a depth-first search in a binary tree:depth-first search in a binary tree:Let Let t be a binary treet be a binary treedepthfirst(t) {depthfirst(t) {
if (t is not empty) {if (t is not empty) {access root item of t;access root item of t;depthfirst(left(t));depthfirst(left(t));depthfirst(right(t));depthfirst(right(t));
}}}}
Backtracking ExampleBacktracking Example
Output: A – B – D – E – H – I – C – F - GOutput: A – B – D – E – H – I – C – F - G
4 Queens Backtracking Example4 Queens Backtracking Example
Solved by iterating over all solutions, using Solved by iterating over all solutions, using backtrackingbacktracking
N QueensN Queens
Extend to N boardExtend to N boardSimilar to 8 QueensSimilar to 8 QueensUse a more general board of size NxNUse a more general board of size NxN
Same algorithm as 8 QueensSame algorithm as 8 Queens
N+1 QueensN+1 Queens
What happens when you add a pawn?What happens when you add a pawn?For a large enough board, we can add an For a large enough board, we can add an
extra Queenextra QueenSlightly more complexSlightly more complex
Another loop over Pawn placementsAnother loop over Pawn placementsMore checking for fundamental solutionsMore checking for fundamental solutions
8x8 Board, 1 Pawn8x8 Board, 1 Pawn
Main Focus: N+k QueensMain Focus: N+k Queens
Why?Why?Instead of focusing on specific solutions (N+1, Instead of focusing on specific solutions (N+1,
N+2, ...), we will be able to solve any general N+2, ...), we will be able to solve any general statement (N+k) of the “Queens Problem.”statement (N+k) of the “Queens Problem.”
Implementing a solution is rigorous and Implementing a solution is rigorous and utilizes many important techniques in utilizes many important techniques in computer science such as parallel algorithm computer science such as parallel algorithm development, recursion, and backtrackingdevelopment, recursion, and backtracking
Chatham, Fricke, SkaggsChatham, Fricke, Skaggs
Proved N+k queens can be placed on an Proved N+k queens can be placed on an NxN board with k pawns.NxN board with k pawns.
What did I do?What did I do?
Translate Chatham’s Python Code (for N+1) into Translate Chatham’s Python Code (for N+1) into a sequential C++ programa sequential C++ program
Modify sequential C++ code to run in Parallel Modify sequential C++ code to run in Parallel with MPIwith MPI
Design and implement the N+k Queens solutionDesign and implement the N+k Queens solution(Iterative)(Iterative)kk * (Recursive) * (Recursive)NN = No. = No.Dancing LinksDancing Links
N+1 Sequential SolutionN+1 Sequential Solution
Optimized to exploit the geometry of the Optimized to exploit the geometry of the problemproblemPawns may not be placed in first or last Pawns may not be placed in first or last
column or rowcolumn or rowPawns are only placed on roughly 1/8 of the Pawns are only placed on roughly 1/8 of the
board (in a wedge shape)board (in a wedge shape)
The Need for SpeedThe Need for SpeedEven with optimizations, program can run for Even with optimizations, program can run for
days for large Ndays for large NRoughly 6x faster than PythonRoughly 6x faster than Python
N+1 ResultsN+1 ResultsN Solutions Fundamental
Solutions
6 2 16
7 3 20
8 16 128
9 52 396
10 286 2288
11 1403 11152
12 8214 65172
13 54756 437848
14 389833 3118664
15 2923757 23387448
Python versus C++Python versus C++
1.0E-03
1.0E-02
1.0E-01
1.0E+00
1.0E+01
1.0E+02
1.0E+03
1.0E+04
6 7 8 9 10 11 12 13
Python C++,backtrack
N+1 Parallel SolutionN+1 Parallel Solution
Almost exactly the same as Sequential Almost exactly the same as Sequential except:except:For-loop over Pawn Placements is distributed For-loop over Pawn Placements is distributed
over p processorsover p processorsEvidence suggests that more solutions are Evidence suggests that more solutions are
found when the Pawns are near the center of found when the Pawns are near the center of the chess boardthe chess boardMore solutions implies more computations, thus More solutions implies more computations, thus
more timemore timePawns are specially numbered for more Pawns are specially numbered for more
optimizationoptimization
Pawn Placements for Parallel N+1 Pawn Placements for Parallel N+1 Queens SolutionQueens Solution
N+1 Queens, N+1 Queens, Parallel vs. Sequential C++Parallel vs. Sequential C++
1.0E-03
1.0E-01
1.0E+01
1.0E+03
1.0E+05
6 7 8 9 10 11 12 13 14 15
C++,backtrack C++, BT, ||
N+K – what to do?N+K – what to do?
N+k presents a very large problemN+k presents a very large problem1 Pawn meant an extra for loop around 1 Pawn meant an extra for loop around
everythingeverythingk Pawns would imply k for loops around k Pawns would imply k for loops around
everythingeverythingDynamic for loops? Dynamic for loops?
“That’s Unpossible” – Ralph Wiggum“That’s Unpossible” – Ralph Wiggum
Search for a better way…Search for a better way…Dancing LinksDancing Links
Why “Dancing Links?”Why “Dancing Links?”
Structure & AlgorithmStructure & AlgorithmComprehendible (Open for Debate…)Comprehendible (Open for Debate…)
Increased performanceIncreased performanceParallel computing is utilized mainly for Parallel computing is utilized mainly for
performance advantages… so, why run a sub-performance advantages… so, why run a sub-par algorithm (backtracking) when the goal is par algorithm (backtracking) when the goal is to achieve the quickest run-time?to achieve the quickest run-time?
Made popular by Knuth via his circa 2000 Made popular by Knuth via his circa 2000 articlearticle
““The Universe”The Universe”
Multi-Dimensional structure composed of Multi-Dimensional structure composed of circular, doubly linked-listscircular, doubly linked-lists
Each row and column is a circular, doubly Each row and column is a circular, doubly linked-listlinked-list
Visualization of “The Universe”Visualization of “The Universe”
The Header nodeThe Header node
The “root” node of the entire structureThe “root” node of the entire structureMembers:Members:
Left pointerLeft pointerRight pointerRight pointerName (H)Name (H)Size: Number of “Column Headers” in its row.Size: Number of “Column Headers” in its row.
Column HeadersColumn Headers
Column Headers are nodes linked horizontally Column Headers are nodes linked horizontally with the Header nodewith the Header node
Members:Members:Left pointerLeft pointerRight pointerRight pointerUp pointerUp pointerDown pointerDown pointerName (RName (Rww, F, Fxx, A, Ayy, or B, or Bzz))Size: the number of “Column Objects” linked vertically Size: the number of “Column Objects” linked vertically
in their column in their column
Column ObjectsColumn Objects
Grouped in two ways:Grouped in two ways:All nodes in the same column are members of All nodes in the same column are members of
the same Rank, File, or Diagonal on the chess the same Rank, File, or Diagonal on the chess boardboard
Linked horizontally in sets of 4 Linked horizontally in sets of 4 {{RRww, F, Fxx, A, Ayy, or B, or Bzz}}Each set represents a space on the chess boardEach set represents a space on the chess board
Same members as Column Headers, but Same members as Column Headers, but with an additional “top pointer” which with an additional “top pointer” which points directly to the Column Headerpoints directly to the Column Header
Mapping the Chess BoardMapping the Chess Board
The Amazing TechniColor Chess The Amazing TechniColor Chess BoardBoard
Dance, Dance: RevolutionDance, Dance: Revolution
The entire algorithm is based off of two simple The entire algorithm is based off of two simple ideas:ideas:
CoverCover: remove an item: remove an itemNode.right.left = Node.leftNode.right.left = Node.leftNode.left.right = Node.right Node.left.right = Node.right
UncoverUncover: insert the item back: insert the item backNode.right.left = NodeNode.right.left = NodeNode.left.right = Node Node.left.right = Node
The Latest Dance CrazeThe Latest Dance Craze
void search(k):void search(k):if (header.right == header) {finished}if (header.right == header) {finished}else else
c = choose_column()c = choose_column()cover(c)cover(c)r = c.downr = c.downwhile (r != c)while (r != c)
j = r.rightj = r.rightwhile (j != r)while (j != r)
cover(j.top)cover(j.top)j = j.rightj = j.right
# place next queen# place next queensearch(k+1)search(k+1)c = r.topc = r.topj = r.leftj = r.leftwhile (j != r)while (j != r)
uncover(j.top)uncover(j.top)j = j.leftj = j.left
# completed search(k)# completed search(k)uncover(c)uncover(c){finished}{finished}
1x1 Universe: Before1x1 Universe: Before
1x1 Universe: After1x1 Universe: After
The “Aha!” MomentThe “Aha!” Moment
N Queens worked, now what?N Queens worked, now what?N+k Queens… hmmN+k Queens… hmm
What needs to be modified?What needs to be modified?Do I have to start from scratch?!?!Do I have to start from scratch?!?!……..Nope Nope
As it turns out, the way the universe is built is the only As it turns out, the way the universe is built is the only needed modification to go from N Queens to N+k needed modification to go from N Queens to N+k QueensQueens
Modifying for N+k QueensModifying for N+k Queens
1 Pawn will cut its row, column, and 1 Pawn will cut its row, column, and diagonal into 2 separate piecesdiagonal into 2 separate piecesJust add these 4 new Column Headers to the Just add these 4 new Column Headers to the
universe, along with their respective Column universe, along with their respective Column ObjectsObjects
k Pawns will cut their rows, columns, and k Pawns will cut their rows, columns, and diagonals into…. ? separate pieces.diagonals into…. ? separate pieces.Still need to add these extra Column Headers, Still need to add these extra Column Headers,
but how many are there and how many but how many are there and how many Column Objects are in each?Column Objects are in each?
It Slices, It Dices…It Slices, It Dices…
Find Find ALLALL valid Pawn Placements valid Pawn PlacementsWolff’s Theorem:Wolff’s Theorem:
(N-2)(N-2)22 choose k = lots of combinations choose k = lots of combinations
Then build 4 NxN arraysThen build 4 NxN arraysOne for each Rank, File, and DiagonalOne for each Rank, File, and Diagonal
““Scan” through arrays:Scan” through arrays:For Ranks: scan horizontally (Files: vertically, For Ranks: scan horizontally (Files: vertically,
Diagonals: diagonally)Diagonals: diagonally)Reach the end or a Pawn, increment 1Reach the end or a Pawn, increment 1
Example of Rank “Scan”Example of Rank “Scan”
And now for the moment you’ve all And now for the moment you’ve all been waiting for!been waiting for!
DRUM ROLL!DRUM ROLL!
….….
GRAPHS AND STUFF!GRAPHS AND STUFF!
N+1 QueensN+1 QueensVarying Language, AlgorithmVarying Language, Algorithm
1.0E-03
1.0E-02
1.0E-01
1.0E+00
1.0E+01
1.0E+02
1.0E+03
1.0E+04
1.0E+05
6 7 8 9 10 11 12 13 14 15
Python C++,backtrack C++, DLX
N+1 QueensN+1 Queens Parallel Backtracking vs. DLX Parallel Backtracking vs. DLX
1.0E-03
1.0E-02
1.0E-01
1.0E+00
1.0E+01
1.0E+02
1.0E+03
1.0E+04
1.0E+05
6 7 8 9 10 11 12 13 14 15 16
C++, BT, || C++ || DLX
N+1 QueensN+1 QueensSequential DLX vs. Parallel DLXSequential DLX vs. Parallel DLX
1.0E-03
1.0E-02
1.0E-01
1.0E+00
1.0E+01
1.0E+02
1.0E+03
1.0E+04
1.0E+05
6 7 8 9 10 11 12 13 14 15 16
C++, DLX C++ || DLX
Interesting Tidbit:Interesting Tidbit:Sequential DLX vs. Parallel C++Sequential DLX vs. Parallel C++
1.0E-03
1.0E-01
1.0E+01
1.0E+03
1.0E+05
6 7 8 9 10 11 12 13 14 15 16
C++, DLX C++, BT, ||
N+k ResultsN+k Results
Still running in Lappin 241L… Still running in Lappin 241L… Maybe next week Maybe next week
Further WorkFurther Work
Finish module that will properly count the Finish module that will properly count the fundamental solutionsfundamental solutions
Since run-times will decrease over time Since run-times will decrease over time (newer processors, etc), compare amount (newer processors, etc), compare amount of updates to the structure to see if of updates to the structure to see if Dancing Links is actually doing less work, Dancing Links is actually doing less work, which would explain the decrease in run-which would explain the decrease in run-time.time.
Future Work (Project)Future Work (Project)
Find a more efficient way to account for k Pawns Find a more efficient way to account for k Pawns in the universein the universe Using Dancing Links itself?Using Dancing Links itself? Find patterns so parallelization can be done Find patterns so parallelization can be done
efficiently, similar to N+1 specific parallel programefficiently, similar to N+1 specific parallel program
Find more results for larger values of N and kFind more results for larger values of N and k May involve use of Genetic AlgorithmsMay involve use of Genetic Algorithms
Domination Problem?Domination Problem? Fewest number of Queens to cover entire chess Fewest number of Queens to cover entire chess
board.board.
Questions?Questions?
Thank you!Thank you!Dr. ChathamDr. ChathamDr. DoyleDr. DoyleMr. SkaggsMr. Skaggs
ReferencesReferences