Upload
ben-mauger
View
214
Download
1
Tags:
Embed Size (px)
Citation preview
Solving N+k Queens Using Dancing Links
Matthew A. WolffMatthew A. Wolff
Morehead State UniversityMorehead State University
May 19, 2006May 19, 2006
AgendaAgenda
MotivationMotivationTermsTermsProblem DefinitionProblem DefinitionTiming Results for various N+1 ProgramsTiming Results for various N+1 ProgramsFuture WorkFuture Work
MotivationMotivation
NASA EPSCoR grant NASA EPSCoR grant (Subcontract # WKURF 516140-06-15)(Subcontract # WKURF 516140-06-15)Began 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 Spring '06 semesterbeginning of Spring '06 semesterSenior ProjectSenior Project
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
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
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.
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?
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 performanceDLX is supposedly quicker than a standard DLX is supposedly quicker than a standard
backtracking algorithmbacktracking algorithmMade 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 {R{Rww, 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
The Dance StepsThe Dance Steps
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
Dance Steps, cont.Dance Steps, cont.
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
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 Placements(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”
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, ||
Questions?Questions?
Thank you!Thank you!Dr. ChathamDr. ChathamDr. DoyleDr. DoyleMr. SkaggsMr. Skaggs
ReferencesReferences