Download presentation
Presentation is loading. Please wait.
Published byYulia Sudirman Modified over 5 years ago
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 = n2!/(n2-n)! Queens in separate columns: = nn Queens in separate cols, rows: 4x3x2x1 = n!
7
1. Complete state formulation
What actions, branching factor Move a queen anywhere: 4x16 = n x n2 Move queen to open space: 4x12 = n x (n2-n) Move queen in column: x3 = n x (n-1) Exchange rows of two queens: 4x3/2 = 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: i=0,n (n2)i = (c/w 65536) Any queen on empty square: 47296 (43680) i=0,n n2!/(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 = n2 Place queen on open space: < b ≤ 16 n(n-1)<b≤n2 Place queen in column: n Place queen in col, row: 0 < b ≤ < b ≤ n
14
Partial state formulation
Heuristics for evaluating states: is there a threat? e.g., Total pairwise threats: Number of unthreatened spaces
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
Analyze this algorithm Complete / incremental? How many states? Branching factor? Heuristic evaluation of fitness?
17
Example algorithm from Peter Alfeld <http://www. apl. jhu
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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.