35

HISTORY The problem was originally proposed in 1848 by the chess player Max Bezzel, and over the years, many mathematicians, including Gauss have worked

Embed Size (px)

Citation preview

HISTORY

The problem was originally proposed in 1848 by the chess player Max Bezzel, and over the years, many mathematicians, including Gauss have worked on this puzzle. In 1874, S.Gunther proposed a method of finding solutions by using DETERMINANTS, and J.W.L Glaisher refined this approach.

This puzzle appeared in the popular early 1990’s computer game, THE 7th GUEST.

The eight queens puzzle is the problem of putting eight chess queens on an 8×8 chessboard such that none of them is able to capture any other using the standard chess queen's moves. The color of the queens is meaningless in this puzzle, and any queen is assumed to be able to attack any other. Thus, a solution requires that no two queens share the same row, column, or diagonal .

The 8 queen problem as an exercise in algorithm design

Finding all solutions to the eight queen puzzle is a good example of a simple but nontrivial problem. For this reason, it is often uses as an example problem for various programming technique, including non traditional approaches such as:

Constraint programming

Logic programming or genetic algorithms

Recursive algorithms

Brute force search algorithms

BACKTRACKING

Backtracking is a technique which is applied to the problem which are non-polynomial complete and the algorithm doesn't exist to give the best results. The key point of Backtracking is the binary choice Y/N. When a NO node is encountered this indicate the dead end and algorithm backtracks one step and tries for those path for which the choice is YES. The solution given by the backtracking method can be represented by an implicit graph.

In this method, we form the state space tree for the problem.

EXAMPLE:

To find Hamiltonian circuit in a closed graph using BACKTRACKING.

INPUT: A closed graph G=(V,E) having 5 vertices.

G = (V,E)

b

a

c

d

e

SOLUTION:

Initially we start our search with vertex ‘a’ this vertex becomes root of our implicit tree.

a ROOT

Next we choose vertex ‘b’ adjacent to ‘a’ as it comes first in the alphabetical order.

c d

a

b

b

a

c

d

e

Next vertex ‘c’ is selected which is adjacent to ‘b’.

c d

a

b

ec

b

a

c

d

e

Next vertex ‘d’ is selected which is adjacent to ‘’c’

c d

a

b

ec

d c

b

a

c

d

e

Next vertex selected that is adjacent to ‘d’ is vertex ‘e’. If we choose vertex ‘a’ then we do not get the Hamiltonian cycle.

c d

a

b

ec

d e

e

b

a

c

d

e

The vertex adjacent to ‘e’ are b, c, d but they are already visited, i.e. ‘e is the dead end. So, we backtrack one step.

c d

a

b

ec

d e

e

b

a

c

d

e

b

a

c

d

e

The vertex adjacent to ‘d’ are (e,c,a) from which ‘e’ is a Dead end. If we choose vertex ‘a’ then we do not get the Hamiltonian cycle. So again we backtrack one step. Here we select vertex ‘e’ adjacent to ‘c’.

b

a

c

d

e

c d

a

b

ec

d e

e

c d

a

b

ec

d e

e d

The vertex adjacent to ‘e’ are (b, c ,d). So, vertex ‘d’ is selected.

b

a

c

d

e

Now all the vertices has been selected

The vertex adjacent to ‘d’ are (a, c, e). So, we select vertex a to get the Hamiltonian cycle.

b

a

c

d

e

c d

a

b

ec

d e

e d

a

HAMILTONIAN CYCLE

Starting point

Final state space tree

THE 8 QUEEN PROBLEM

INPUT

QUEENS

Attacking ends

Attacking ends

Columns

Rows

1 2 3 4 5 6 7 81

234

5

67

8

CHESS BOARD

Generalization of 8 Queen Problem to N Queen Problem

Like 8*8 chess board is this possible to place N Queens in N*N board. It’s pretty easy to see that this is impossible if N is 2 or 3, and it’s reasonably straight forward to find solutions when N is 4,5,6 or 7. the problem begins to become difficult for manual solution precisely when N is 8.

Probably the fact that this number coincidently equals the dimensions of an ordinary chess board has contributed to the popularity of the problem.

Attacking ends

For example:When N =

2

1

1

2

1

2

1

23

1

12

1

2

3

12

3

4

Required solution

When N = 4,

Empty board

Step 1

Step 2

Backtracking

Step 3

Step 4Backtrack

Backtrack

Step 5

Step 6

Step 7

Step 8

As in Backtracking method the desired solution is expressed as an N-tuple (x1,x2,x3…..xn). Where xi is the column of the ith row where the ith queen is placed. In this case Si={x1,x2…..xn}, so the solution will be the permutation of the N-Tuple. Where Si is the bounding function.

So the final solution we get for this 4- Queen problem is 4-Tuple (2,4,1,3).

1

23

4 (2, 4, 1, 3)

1 2 3 4

1

2

3

4

Another possible solution for this problem is (3,1,4,2).

SOLUTION

FOR ,

N = 8Step 1

Step 2

Step 3

Step 4

Step 4

Step 5

Step 6

Backtrack

Step 3

Step 7

Step 8

Required solution

(1,5,8,6,3,7,4)

Consider the Queen at position A[4,2]. The diagonally attacking ends of this queen :

1) Diagonal from the upper left to the lower right are A[3,1], A[5,3], A[6,4], A[7,5] and A[8,6]

All these squares have same (row – column) value.

2) Diagonal from the upper right to the lower left are A[1,5], A[2,4], A[3,3], A[5,1]

All these squares have same (row + column) value .

Constraints for Non Attacking Ends

Columns

Row1 2 3 4 5 6 7 81

2345678

Suppose two queens are placed at positions (i,j) and (k,l) then they will lie on the same diagonal iff,

i-j = k-l OR i+j = k+l

i j k l

i j k l

Two Queens are in attacking end if they are in same row, same column and in the same diagonal.

Suppose two Queens are placed at positions (i, j) and (k ,l) respectively. Then implicit constraints will be,

Condition for row and

column:

i= k AND j= l

Conditions for diagonals:

i - j = k - l OR i + j = k + l

=>

j - l = i - k OR j – l = k – i

Two queens lie in on the same diagonal if and only if,

| j - l | = | i – k |

ALGORITHMPLACE (k , i)

//Returns TRUE if a queen can be placed in kth row and ith column. Otherwise it returns FALSE.

X{} is a global array whose first (k-1) values have been set. Abs (r) returns the absolute value of r.

{

for j:=1 to (k-1) do

if ( (x[j]=i) //Two in the same column

or (abs ( x[j] - i) = abs (j-k)) ) //Or in

the same diagonal

then return FALSE;

return TRUE;

}

Here,

k is the queen to be placed.

i is the column for which we checking.

j is the index of array x[].

NQueens (k, n)

//Using Backtracking, this procedure prints all possible placements of n Queens on an n*n chessboard so that they are non attacking.

{

for i:=1 to n do

{

if Place(k, i) then

{

x[k]:=i;

if (k=n) then write (x[i:n]);

else NQueens(k+1, n);

}

}

}

Here,

k is the queen to be placed.

i is the column for which we checking.

NQueens (k, n)

{

for i:=1 to n do

{

if Place(k, i) then

{

x[k]:=i;

if (k=n) then write (x[i:n]);

else NQueens(k+1, n);

}

}

}

n=8

k=1

i=1

2) Place(1,1)

{

for j=1 to (1-1) do

if x[1]=1 or x[1]-1=1-1

{(0=1 ) or |0-1| = 0}

both conditions false

Return (TRUE);

}

PLACE (k , i)

{

for j:=1 to (k-1) do

if ( (x[j]=i)

or (abs ( x[j] - i) = abs (j-k)) )

then return FALSE;

return TRUE;

}

1) Nqueens(1,8)

For i=1 to 8

if TRUE then

{

x[1]:=1;

If (k=n)……;

Else NQueens(1+1,8);

Initially,

X

J 1 2 3 4 5 6 7 8

1

PLACE (k , i)

{

for j:=1 to (k-1) do

if ( (x[j]=i)

or (abs ( x[j] - i) = abs (j-k)) )

then return FALSE;

return TRUE;

}

NQueens (k, n)

{

for i:=1 to n do

{

if Place(k, i) then

{

x[k]:=i;

if (k=n) then write (x[i:n]);

else NQueens(k+1, n);

}

}

}

n=8

k=2

1) NQueens(2,8)

i=1 to 8 do

{

if Place(2,1) then2) Place(2,1)

For j=1 to 1

if x[1]=1 or x[1]-1=1-2

( 1 = 1 or 1-1 = -1)

return (FALSE);

i=2

Now,

X

J 1 2 3 4 5 6 7 8

1

3) NQueens(2,8)

{

i=1 to 8 do

{

if Place(2,1) then

FALSE

{

not executed;

}

i++;

}4) NQueens(2,8)

{

i=2 to 8 do

{

if Place(2,2) then

6) Place(2,2)

For j=1 to 1

if x[1]=2 or x[1]-2=1-2

( 1 = 2 or 1-2 = 1-2)

return (FALSE);

5) NQueens(2,8)

{

i=2 to 8 do

{

if Place(2,2) then

{

not executed;

}

i++;

}

}

NQueens (k, n)

{

for i:=1 to n do

{

if Place(k, i) then

{

x[k]:=i;

if (k=n) then write (x[i:n]);

else NQueens(k+1, n);

}

}

}

PLACE (k , i)

{

for j:=1 to (k-1) do

if ( (x[j]=i)

or (abs ( x[j] - i) = abs (j-k)) )

then return FALSE;

return TRUE;

}

n=8

k=2

i=3

1) NQueens(2,8)

{

i=3 to 8 do

{

if Place(2,3) then

Initially,

X

J 1 2 3 4 5 6 7 8

1 3

n=8

k=3

i=1

2) Place(2,3)

For j=1 to 1

if x[1]=3 or x[1]-3=1-2

( 1 = 3 or 1-3 = 1-2)

return (TRUE);

3) NQueens(2,8)

{

i=3 to 8 do

{

if Place(2,3) then

TRUE

{

x[2] = 3;

if(2=8)…..;

else NQueens(2+1,8);

TIME COMPLEXITY

Time complexity of this algorithm is,

O(n-1) = O(n)

The complexity of Backtracking method depends on the number of nodes generated in the state space tree.

So the worst time complexity when, number of nodes in the space tree are,

1) 2^n

O[p(n) 2^n]

2) n!

O[q(n) n!]

where p(n) and q(n) are the polynomials in n.

COUNTING SOLUTIONS:

Following table gives the number of solutions for N Queens, both unique and distinct:

N 1 2 3 4 5 6 7 8

Unique: 1 0 0 1 2 1 6 12

Distinct: 1 0 0 2 10 4 40 92

SOLUTIONS OF 8 QUEEN PROBLEM

There are 12 unique solutions and 92 distinct solutions for this problem.

12 Unique solutions are as follows:

RELATED PROBLEMS:

Some other related problems are as follows:

32 knights or 14 bishops or 16 kings problem

The magic square problem

9 Queens and 1 pawn on 8*8 board problem