33
Solving N+k Queens Using Dancing Links Matthew A. Wolff Matthew A. Wolff Morehead State University Morehead State University May 19, 2006 May 19, 2006

Solving N+k Queens Using Dancing Links Matthew A. Wolff Morehead State University May 19, 2006

Embed Size (px)

Citation preview

Page 1: Solving N+k Queens Using Dancing Links Matthew A. Wolff Morehead State University May 19, 2006

Solving N+k Queens Using Dancing Links

Matthew A. WolffMatthew A. Wolff

Morehead State UniversityMorehead State University

May 19, 2006May 19, 2006

Page 2: Solving N+k Queens Using Dancing Links Matthew A. Wolff Morehead State University May 19, 2006

AgendaAgenda

MotivationMotivationTermsTermsProblem DefinitionProblem DefinitionTiming Results for various N+1 ProgramsTiming Results for various N+1 ProgramsFuture WorkFuture Work

Page 3: Solving N+k Queens Using Dancing Links Matthew A. Wolff Morehead State University May 19, 2006

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

Page 4: Solving N+k Queens Using Dancing Links Matthew A. Wolff Morehead State University May 19, 2006

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

Page 5: Solving N+k Queens Using Dancing Links Matthew A. Wolff Morehead State University May 19, 2006

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

Page 6: Solving N+k Queens Using Dancing Links Matthew A. Wolff Morehead State University May 19, 2006

Recursion in ArtRecursion in Art

Page 7: Solving N+k Queens Using Dancing Links Matthew A. Wolff Morehead State University May 19, 2006

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));

}}

Page 8: Solving N+k Queens Using Dancing Links Matthew A. Wolff Morehead State University May 19, 2006

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));

}}}}

Page 9: Solving N+k Queens Using Dancing Links Matthew A. Wolff Morehead State University May 19, 2006

Backtracking ExampleBacktracking Example

Output: A – B – D – E – H – I – C – F - GOutput: A – B – D – E – H – I – C – F - G

Page 10: Solving N+k Queens Using Dancing Links Matthew A. Wolff Morehead State University May 19, 2006

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

Page 11: Solving N+k Queens Using Dancing Links Matthew A. Wolff Morehead State University May 19, 2006

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.

Page 12: Solving N+k Queens Using Dancing Links Matthew A. Wolff Morehead State University May 19, 2006

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

Page 13: Solving N+k Queens Using Dancing Links Matthew A. Wolff Morehead State University May 19, 2006

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

Page 14: Solving N+k Queens Using Dancing Links Matthew A. Wolff Morehead State University May 19, 2006

““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

Page 15: Solving N+k Queens Using Dancing Links Matthew A. Wolff Morehead State University May 19, 2006

Visualization of “The Universe”Visualization of “The Universe”

Page 16: Solving N+k Queens Using Dancing Links Matthew A. Wolff Morehead State University May 19, 2006

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.

Page 17: Solving N+k Queens Using Dancing Links Matthew A. Wolff Morehead State University May 19, 2006

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

Page 18: Solving N+k Queens Using Dancing Links Matthew A. Wolff Morehead State University May 19, 2006

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

Page 19: Solving N+k Queens Using Dancing Links Matthew A. Wolff Morehead State University May 19, 2006

Mapping the Chess BoardMapping the Chess Board

Page 20: Solving N+k Queens Using Dancing Links Matthew A. Wolff Morehead State University May 19, 2006

The Amazing TechniColor Chess The Amazing TechniColor Chess BoardBoard

Page 21: Solving N+k Queens Using Dancing Links Matthew A. Wolff Morehead State University May 19, 2006

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

Page 22: Solving N+k Queens Using Dancing Links Matthew A. Wolff Morehead State University May 19, 2006

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}

Page 23: Solving N+k Queens Using Dancing Links Matthew A. Wolff Morehead State University May 19, 2006

1x1 Universe: Before1x1 Universe: Before

Page 24: Solving N+k Queens Using Dancing Links Matthew A. Wolff Morehead State University May 19, 2006

1x1 Universe: After1x1 Universe: After

Page 25: Solving N+k Queens Using Dancing Links Matthew A. Wolff Morehead State University May 19, 2006

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?

Page 26: Solving N+k Queens Using Dancing Links Matthew A. Wolff Morehead State University May 19, 2006

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

Page 27: Solving N+k Queens Using Dancing Links Matthew A. Wolff Morehead State University May 19, 2006

Example of Rank “Scan”Example of Rank “Scan”

Page 28: Solving N+k Queens Using Dancing Links Matthew A. Wolff Morehead State University May 19, 2006

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

Page 29: Solving N+k Queens Using Dancing Links Matthew A. Wolff Morehead State University May 19, 2006

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

Page 30: Solving N+k Queens Using Dancing Links Matthew A. Wolff Morehead State University May 19, 2006

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

Page 31: Solving N+k Queens Using Dancing Links Matthew A. Wolff Morehead State University May 19, 2006

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, ||

Page 32: Solving N+k Queens Using Dancing Links Matthew A. Wolff Morehead State University May 19, 2006

Questions?Questions?

Thank you!Thank you!Dr. ChathamDr. ChathamDr. DoyleDr. DoyleMr. SkaggsMr. Skaggs

Page 33: Solving N+k Queens Using Dancing Links Matthew A. Wolff Morehead State University May 19, 2006

ReferencesReferences