Presentation is loading. Please wait.

Presentation is loading. Please wait.

The n queens problem Many solutions to a classic problem: On an n x n chess board, place n queens so no queen threatens another.

Similar presentations


Presentation on theme: "The n queens problem Many solutions to a classic problem: On an n x n chess board, place n queens so no queen threatens another."— Presentation transcript:

1 The n queens problem Many solutions to a classic problem: On an n x n chess board, place n queens so no queen threatens another

2 Minimal example – 4 queens 4 x 4 Board Queen threatens next piece in any row, column or diagonal Problem: Place 4 queens so no queen is threatened

3 What kind of problem? Design state space for search nodes – how to represent state edges – what transformations to neighbouring state e.g., state: positions of four queens edge: move any queen to new position

4 Possible state spaces Every state has four queens: Neighbour state has one queen in different position

5 Start state is undecided: Random? Guess based on knowledge? Form of graph – all states are potential solutions Edges for neighbour relationship are undirected formulation

6 How many states?  Any queen anywhere: 16 4 = 65536(n 2 ) n  Queens on different squares: 16x15x14x13 = 43680n 2 !/(n 2 -n)!  Queens in separate columns: 4 4 = 256n n  Queens in separate cols, rows: 4x3x2x1 = 24n!

7 1. Complete state formulation What actions, branching factor  Move a queen anywhere: 4x16 = 64n x n 2  Move queen to open space: 4x12 = 48n x (n 2 -n)  Move queen in column: 4x3 = 12n x (n-1)  Exchange rows of two queens: 4x3/2 = 6n(n-1)/2

8 Fitness function: no queen threatened Operationalizing Heuristics for evaluating states: how (relatively) bad is the threat situation?  Number of unthreatened queens  Total pairwise threats Depends on the representation; e.g., need to count column threats?

9 Finding solution: local search repeat create random state find local optimum based on fitness function heuristic (e.g.,max number of unthreatened queens) until fitness function satisfied

10 Partial-solution spaces Every state has 0,1,2,3 or 4 queens Edges lead to states with one more queen

11 Start state is fixed: Empty board Form of graph – hierarchical, directed, multi-partite Actions/changes are directed edges that add one more queen into destination state Partial state formulation

12 How many states?  Any queen anywhere: 16 0 +16 1 +16 2 +16 3 +16 4  i=0,n (n 2 ) i = 69905 (c/w 65536)  Any queen on empty square: 47296 (43680)  i=0,n n 2 !/(n 2 -n)!  Queens in separate columns: 341 (256)  i=0,n n i  Queens in separate rows/cols: 40 (24)  i=1,n n!/(n-i)!

13 How many neighbours: branching factor  Place a queen anywhere: 4x4 = 16n 2  Place queen on open space: 12 < b ≤ 16n(n-1)<b≤n 2  Place queen in column: 4n  Place queen in col, row: 0 < b ≤ 4 0 < b ≤ n Partial state formulation

14 Heuristics for evaluating states: is there a threat? e.g., Total pairwise threats:0 0 Number of unthreatened spaces1 2 Partial state formulation

15 Search is globally controlled – spanning tree - Better for ‘optimizing’ - finding multiple solutions and choosing best Partial formulation

16 Complete / incremental? How many states? Branching factor? Heuristic evaluation of fitness? Example algorithm from Peter Alfeld http://www.apl.jhu.edu/~hall/NQueens.html Analyze this algorithm

17 Example algorithm from Peter Alfeld http://www.apl.jhu.edu/~hall/NQueens.html Complete / incremental? How many states? Branching factor? Heuristic evaluation? Incremental O( Σ i= 0,n n i ) n No conflict

18 An O(n) algorithmic solution ACM SIGART Bulletin, 2(2), page 22-24, 1991 -finds one arrangement of queens solution by Marty Hall based on this algorithm: O(n) algorithm


Download ppt "The n queens problem Many solutions to a classic problem: On an n x n chess board, place n queens so no queen threatens another."

Similar presentations


Ads by Google