# N-Queens - PowerPoint PPT Presentation

1 / 17

N-Queens. Algorithm Analyses & Implementations. Vlad Furash & Steven Wine. Background. 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

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.

N-Queens

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

## N-Queens

Algorithm Analyses &

Implementations

### Background

• 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.

### Basic 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

DONE

### Optimizing the Algorithm

• Further look ahead—not just next row

• Keep track of total free spaces per row

• If any row ahead has a zero count, backtrack

• 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

### Optimizing the Algorithm (ex)

*example of when to backtrack

due to looking ahead (row 7)

### Possible Solution Set

All possible solutions for N=5

### Possible Solution Set

Only 2 unique solutions for N=5

(notice transformations & reflections)

## Rivin-Zabith algorithm Dynamic programming solution to the n-queens problem

### Introductory 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 N×N board.

Examples of lines

• A Configuration C is a placement of at mostn queens on N×N 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.

### Fundamental 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.

### Algorithm 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.

### Algorithm pseudocode

• Set QUEUE = {<∅,1>}. QUEUE is a set of structures <S, i>, 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 <S, i> є QUEUE, s.t. S ∩ T=∅, DO:

• If <S U T, j> є QUEUE, for some j, replace j with i + j,

• Otherwise add <S U T, i> to QUEUE.

• Return QUEUE

### Time 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 mostO(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.