Title: Solving N k Queens Using Dancing Links
1Solving Nk Queens Using Dancing Links
- Matthew Wolff
- CS 499c
- May 3, 2006
2Agenda
- Motivation
- Definitions
- Problem Definition
- Solved Problems with Results
- Future Work
3Motivation
- NASA EPSCoR grant
- Began working with Chatham and Skaggs in November
- Doyle added DLX (Dancing Links) at beginning of
semester - New to me (and the rest of the team, I think)
- A lot more work!
4Category of Problems
- 8 Queens
- 8 attacking queens on an 8x8 chess board
- N Queens
- N attacking queens on an NxN chess board
- N1 Queens
- N1 attacking queens on an NxN chess board
- 1 Pawn used to block two or more attacking queens
- Nk Queens
- Nk attacking queens on an NxN chess board
- k Pawns used to block numerous attacking queens
58 Queens Example
- http//www.jsomers.com/nqueen_demo/
nqueens.html
6Solutions
- Solutions A class of Queen placements such
that no two Queens can attack each other. - Fundamental Solutions A class of solutions
such that all members of the class are simply
rotations or reflections of one another. - Given the set of solutions, a set of fundamental
solutions can be generated. And vice versa - The fundamental solutions are a subset of all
solutions.
7Fundamental Solutions for 8 Queens
http//mathworld.wolfram.com/QueensProblem.html
8Recursion
- "To understand recursion, one must first
understand recursion" -- Tina Mancuso - A function is recursive if it can be called
while active (on the stack). - i.e. It calls itself
9Recursion in Art
10Recursion in Computer Science
- // precondition n gt 0// postcondition n! is
returnedfactorial (int n) if (n 1) or (n
0) return 1 else return
(nfactorial(n-1))
11Backtracking
- An example of backtracking is used in a
depth-first search in a binary tree - Let t be a binary tree
- depthfirst(t) if (t is not empty) access
root item of t depthfirst(left(t)) depthfi
rst(right(t))
12Backtracking Example
- Output A B D E H I C F - G
134 Queens Backtracking Example
- Solved by iterating over all solutions, using
backtracking
14N Queens
- Extend to N board
- Similar to 8 Queens
- Use a more general board of size NxN
- Same algorithm as 8 Queens
15N1 Queens
- What happens when you add a pawn?
- For a large enough board, we can add an extra
Queen - Slightly more complex
- Another loop over Pawn placements
- More checking for fundamental solutions
168x8 Board, 1 Pawn
17Main Focus Nk Queens
- Why?
- Instead of focusing on specific solutions (N1,
N2, ...), we will be able to solve any general
statement (Nk) of the Queens Problem. - Implementing a solution is rigorous and utilizes
many important techniques in computer science
such as parallel algorithm development,
recursion, and backtracking
18Chatham, Fricke, Skaggs
- Proved Nk queens can be placed on an NxN board
with k pawns.
19What did I do?
- Translate Chathams Python Code (for N1) into a
sequential C program - Modify sequential C code to run in Parallel
with MPI - Design and implement the Nk Queens solution
- (Iterative)k (Recursive)N No.
- Dancing Links
20N1 Sequential Solution
- Optimized to exploit the geometry of the problem
- Pawns may not be placed in first or last column
or row - Pawns are only placed on roughly 1/8 of the board
(in a wedge shape) - The Need for Speed
- Even with optimizations, program can run for days
for large N - Roughly 6x faster than Python
21N1 Results
22Python versus C
23N1 Parallel Solution
- Almost exactly the same as Sequential except
- For-loop over Pawn Placements is distributed
over p processors - Evidence suggests that more solutions are found
when the Pawns are near the center of the chess
board - More solutions implies more computations, thus
more time - Pawns are specially numbered for more optimization
24Pawn Placements for Parallel N1 Queens Solution
25N1 Queens, Parallel vs. Sequential C
26NK what to do?
- Nk presents a very large problem
- 1 Pawn meant an extra for loop around everything
- k Pawns would imply k for loops around everything
- Dynamic for loops? Thats Unpossible Ralph
Wiggum - Search for a better way
- Dancing Links
27Why Dancing Links?
- Structure Algorithm
- Comprehendible (Open for Debate)
- Increased performance
- Parallel computing is utilized mainly for
performance advantages so, why run a sub-par
algorithm (backtracking) when the goal is to
achieve the quickest run-time? - Made popular by Knuth via his circa 2000 article
28The Universe
- Multi-Dimensional structure composed of circular,
doubly linked-lists - Each row and column is a circular, doubly
linked-list
29Visualization of The Universe
30The Header node
- The root node of the entire structure
- Members
- Left pointer
- Right pointer
- Name (H)
- Size Number of Column Headers in its row.
31Column Headers
- Column Headers are nodes linked horizontally with
the Header node - Members
- Left pointer
- Right pointer
- Up pointer
- Down pointer
- Name (Rw, Fx, Ay, or Bz)
- Size the number of Column Objects linked
vertically in their column
32Column Objects
- Grouped in two ways
- All nodes in the same column are members of the
same Rank, File, or Diagonal on the chess board - Linked horizontally in sets of 4
- Rw, Fx, Ay, or Bz
- Each set represents a space on the chess board
- Same members as Column Headers, but with an
additional top pointer which points directly to
the Column Header
33Mapping the Chess Board
34The Amazing TechniColor Chess Board
35Dance, Dance Revolution
- The entire algorithm is based off of two simple
ideas - Cover remove an item
- Node.right.left Node.left
- Node.left.right Node.right
- Uncover insert the item back
- Node.right.left Node
- Node.left.right Node
36The Latest Dance Craze
- void search(k) if (header.right header)
finished else c choose_column() cover(c)
r c.down while (r ! c) j
r.right while (j ! r) cover(j.top) j
j.right place next queen search(k1)
c r.top j r.left while (j !
r) uncover(j.top) j j.left
completed search(k) uncover(c) finished
371x1 Universe Before
381x1 Universe After
39The Aha! Moment
- N Queens worked, now what?
- Nk Queens hmm
- What needs to be modified?
- Do I have to start from scratch?!?!
- .
- Nope ?
- As it turns out, the way the universe is built is
the only needed modification to go from N Queens
to Nk Queens
40Modifying for Nk Queens
- 1 Pawn will cut its row, column, and diagonal
into 2 separate pieces - Just add these 4 new Column Headers to the
universe, along with their respective Column
Objects - k Pawns will cut their rows, columns, and
diagonals into. ? separate pieces. - Still need to add these extra Column Headers, but
how many are there and how many Column Objects
are in each?
41It Slices, It Dices
- Find ALL valid Pawn Placements
- Wolffs Theorem
- (N-2)2 choose k lots of combinations
- Then build 4 NxN arrays
- One for each Rank, File, and Diagonal
- Scan through arrays
- For Ranks scan horizontally (Files vertically,
Diagonals diagonally) - Reach the end or a Pawn, increment 1
42Example of Rank Scan
43And now for the moment youve all been waiting
for!
- DRUM ROLL!.
- GRAPHS AND STUFF!
44N1 QueensVarying Language, Algorithm
45N1 Queens Parallel Backtracking vs. DLX
46N1 QueensSequential DLX vs. Parallel DLX
47Interesting TidbitSequential DLX vs. Parallel
C
48Nk Results
- Still running in Lappin 241L
- Maybe next week ?
49Further Work
- Finish module that will properly count the
fundamental solutions - Since run-times will decrease over time (newer
processors, etc), compare amount of updates to
the structure to see if Dancing Links is actually
doing less work, which would explain the decrease
in run-time.
50Future Work (Project)
- Find a more efficient way to account for k Pawns
in the universe - Using Dancing Links itself?
- Find patterns so parallelization can be done
efficiently, similar to N1 specific parallel
program - Find more results for larger values of N and k
- May involve use of Genetic Algorithms
- Domination Problem?
- Fewest number of Queens to cover entire chess
board.
51Questions?
- Thank you!
- Dr. Chatham
- Dr. Doyle
- Mr. Skaggs
52References