N-Queens

1 / 17

# N-Queens - PowerPoint PPT Presentation

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.

## PowerPoint Slideshow about 'N-Queens' - wyatt

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 - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

### 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
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 , 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 є QUEUE, s.t. S ∩ T=∅, DO:
• If є QUEUE, for some j, replace j with i + j,