1 / 19

The n queens problem

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. Minimal example – 4 queens. Queen threatens next piece in any row, column or diagonal. 4 x 4 Board. Problem: Place 4 queens so no queen is threatened.

abner
Download Presentation

The n queens problem

An Image/Link below is provided (as is) to download presentation 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. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

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 Queen threatens next piece in any row, column or diagonal 4 x 4 Board 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. formulation Start state is undecided: • Random? • Guess based on knowledge? Form of graph – all states are potential solutions Edges for neighbour relationship are undirected

  6. formulation How many states? • Any queen anywhere: 164 = 65536 (n2)n • Queens on different squares: 16x15x14x13 = 43680 n2!/(n2-n)! • Queens in separate columns: 44 = 256 nn • Queens in separate cols, rows: 4x3x2x1 = 24 n!

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

  12. Partial state formulation How many states? Any queen anywhere: 160+161+162+163+164 i=0,n (n2)i = 69905 (c/w 65536) Any queen on empty square: 47296 (43680) i=0,nn2!/(n2-n)! Queens in separate columns: 341 (256) i=0,n ni Queens in separate rows/cols: 40 (24) i=1,n n!/(n-i)!

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

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

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

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

  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 ni) • 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

  19. Another puzzle:Does any subset of these numbers add to 50? {27, 25, 3, 12, 6, 15, 9, 30, 19, 21} Generalize the problem: S  I N = {n1, n2,…,nk}, ni I; ni≠nj if i≠j M  N   ni = S? M

More Related