solving n+k queens using dancing links

52
Solving N+k Queens Using Dancing Links Matthew Wolff Matthew Wolff CS 499c CS 499c May 3, 2006 May 3, 2006

Upload: elwyn

Post on 07-Jan-2016

34 views

Category:

Documents


0 download

DESCRIPTION

Matthew Wolff CS 499c May 3, 2006. Solving N+k Queens Using Dancing Links. Agenda. Motivation Definitions Problem Definition Solved Problems with Results Future Work. Motivation. NASA EPSCoR grant Began working with Chatham and Skaggs in November - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: 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

Page 2: Solving N+k Queens Using Dancing Links

AgendaAgenda

MotivationMotivationDefinitionsDefinitionsProblem DefinitionProblem DefinitionSolved Problems with ResultsSolved Problems with ResultsFuture WorkFuture Work

Page 3: Solving N+k Queens Using Dancing Links

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!

Page 4: Solving N+k Queens Using Dancing Links

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

8 Queens Example8 Queens Example

http://www.jsomers.com/nqueen_demo/nqueens.htmlhttp://www.jsomers.com/nqueen_demo/nqueens.html

Page 6: Solving N+k Queens Using Dancing Links

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.

Page 7: Solving N+k Queens Using Dancing Links

Fundamental Solutions for 8 Fundamental Solutions for 8 QueensQueens

http://mathworld.wolfram.com/QueensProblem.html

Page 8: Solving N+k Queens Using Dancing Links

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 9: Solving N+k Queens Using Dancing Links

Recursion in ArtRecursion in Art

Page 10: Solving N+k Queens Using Dancing Links

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 11: Solving N+k Queens Using Dancing Links

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 12: Solving N+k Queens Using Dancing Links

Backtracking ExampleBacktracking Example

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

Page 13: Solving N+k Queens Using Dancing Links

4 Queens Backtracking Example4 Queens Backtracking Example

Solved by iterating over all solutions, using Solved by iterating over all solutions, using backtrackingbacktracking

Page 14: Solving N+k Queens Using Dancing Links

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

Page 15: Solving N+k Queens Using Dancing Links

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

Page 16: Solving N+k Queens Using Dancing Links

8x8 Board, 1 Pawn8x8 Board, 1 Pawn

Page 17: Solving N+k Queens Using Dancing Links

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 18: Solving N+k Queens Using Dancing Links

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 19: Solving N+k Queens Using Dancing Links

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

Page 20: Solving N+k Queens Using Dancing 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

Page 21: Solving N+k Queens Using Dancing Links

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

Page 22: Solving N+k Queens Using Dancing Links

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

Page 23: Solving N+k Queens Using Dancing Links

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

Page 24: Solving N+k Queens Using Dancing Links

Pawn Placements for Parallel N+1 Pawn Placements for Parallel N+1 Queens SolutionQueens Solution

Page 25: Solving N+k Queens Using Dancing Links

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

Page 26: Solving N+k Queens Using Dancing Links

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

Page 27: Solving N+k Queens Using Dancing 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

Page 28: Solving N+k Queens Using Dancing Links

““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 29: Solving N+k Queens Using Dancing Links

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

Page 30: Solving N+k Queens Using Dancing Links

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 31: Solving N+k Queens Using Dancing Links

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 32: Solving N+k Queens Using Dancing Links

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

Page 33: Solving N+k Queens Using Dancing Links

Mapping the Chess BoardMapping the Chess Board

Page 34: Solving N+k Queens Using Dancing Links

The Amazing TechniColor Chess The Amazing TechniColor Chess BoardBoard

Page 35: Solving N+k Queens Using Dancing Links

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

Page 36: Solving N+k Queens Using Dancing Links

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}

Page 37: Solving N+k Queens Using Dancing Links

1x1 Universe: Before1x1 Universe: Before

Page 38: Solving N+k Queens Using Dancing Links

1x1 Universe: After1x1 Universe: After

Page 39: Solving N+k Queens Using Dancing Links

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

Page 40: Solving N+k Queens Using Dancing Links

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 41: Solving N+k Queens Using Dancing Links

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

Page 42: Solving N+k Queens Using Dancing Links

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

Page 43: Solving N+k Queens Using Dancing Links

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!

Page 44: Solving N+k Queens Using Dancing Links

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 45: Solving N+k Queens Using Dancing Links

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 46: Solving N+k Queens Using Dancing Links

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 47: Solving N+k Queens Using Dancing Links

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 48: Solving N+k Queens Using Dancing Links

N+k ResultsN+k Results

Still running in Lappin 241L… Still running in Lappin 241L… Maybe next week Maybe next week

Page 49: Solving N+k Queens Using Dancing Links

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.

Page 50: Solving N+k Queens Using Dancing Links

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.

Page 51: Solving N+k Queens Using Dancing Links

Questions?Questions?

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

Page 52: Solving N+k Queens Using Dancing Links

ReferencesReferences