Title: N-Queens
1N-Queens
- Algorithm Analyses
- Implementations
Vlad Furash Steven Wine
2Background
- Problem surfaced in 1848 by chess player Max
Bezzel as 8 queens (regulation board size) - Premise is to place N queens on N x N board so
that they are non-attacking - A queen is one of six different chess pieces
- It can move forward back, side to side and to
its diagonal and anti-diagonals - The problem given N, find all solutions of queen
sets and return either the number of solutions
and/or the patterned boards.
3Basic Algorithm
- Generate a list of free cells on the next row.
- If no free cells, backtrack (step 2 of previous
row) - Place a queen on next free cell proceed with
step 1 for next row - If no next row, proceed to step 3
- At this point, you have filled all rows, so
count/store as a solution
4Basic Algorithm (example)
F F F F F
Q
Q
F F F
Q
Q
5Basic Algorithm (example)
Q
Q
F
Q
Q
Q
Q
Q
Q
F
Q
Q
Q
Q
6Basic Algorithm (example)
Q
Q
Q
Q
F
Q
Q
Q
Q
Q
DONE
7Optimizing the Algorithm
- Further look aheadnot just next row
- Keep track of total free spaces per row
- If any row ahead has a zero count, backtrack
- Jump to other rows
- To prevent unnecessary future backtracks, jump to
rows with fewest free spots and place queens
there first - Cell blocking
- Prevention of placing queens on cells that would
lead to repeating of previously found solution
and/or putting the board in an unsolvable
situation
8Optimizing the Algorithm (ex)
5 F F F F F
5 F F F F F
5 F F F F F
5 F F F F F
5 F F F F F
- Q
2 - - - F F
3 F - F - F
3 F - F F -
4 F - F F F
- Q
- Q
1 F - - - -
2 F - F - -
3 F - F - F
- Q
- Q
- Q
1 - - F - -
1 - - - - F
- Q
- Q
- Q
- Q
- Q
9Optimizing the Algorithm (ex)
- Q
- Q
- Q
- Q
2 - F - - - - F -
2 - F - - - - - F
0 - - - - - - - -
2 - F - - - F - -
example of when to backtrack due to looking
ahead (row 7)
10Possible Solution Set
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
All possible solutions for N5
11Possible Solution Set
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
Only 2 unique solutions for N5 (notice
transformations reflections)
12Rivin-Zabith algorithm Dynamic programming
solution to the n-queens problem
13Introductory concepts
- A Line is a set of all squares that make up a
row, column, or diagonal. If one of the squares
in a line has a queen in it then the line is
considered closed.
There are n(rows) n(columns) 4n
2(diagonals) 6n 2 lines on NN board.
Examples of lines
- A Configuration C is a placement of at most n
queens on NN board is considered feasible if no
two queens attack each other. - A completion of a configuration is placement of
remaining queens that results in a solution.
14Fundamental propositions
- Proposition 1 Two feasible configurations with
the same number of closed lines contain the same
number of queens. - Main Theorem If two feasible configurations have
the same set of closed lines, then completion of
one configuration is also a completion of the
other configuration.
15Algorithm overview
- Perform a breadth first search on the set of
feasible configurations, while checking if
combining every line set with the next line set
yields a line set that has already been found. - After the algorithm iterates sum the counter for
every lines set, which contains closed lines for
all rows and all columns to find number of
solutions.
16Algorithm pseudocode
- Set QUEUE ltØ,1gt. QUEUE is a set of structures
ltS, igt, where S is a set of closed lines and i is
the counter for an equivalence class. - For every unexamined square, choose a square and
let T be a set of lines containing the square. - For every set ltS, igt ? QUEUE, s.t. S n TØ, DO
- If ltS U T, jgt ? QUEUE, for some j, replace j with
i j, - Otherwise add ltS U T, igt to QUEUE.
- Return QUEUE
17Time and Space Complexity
- If there are p possible closed lines, need to
store 2p equivalence classes, which is the size
of QUEUE. Size of an element in QUEUE is at most
O(n2), size of the board. Overall space
complexity is O(n22p). Size of p is bounded by 6n
2. O(n264n) - Running time is also O(n264n), since there are n2
squares and algorithm iterates though at most 2p
equivalence classes.