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

Slides:



Advertisements
Similar presentations
Informed search algorithms
Advertisements

1 Finite Constraint Domains. 2 u Constraint satisfaction problems (CSP) u A backtracking solver u Node and arc consistency u Bounds consistency u Generalized.
BackTracking Algorithms
Types of Algorithms.
Techniques for Dealing with Hard Problems Backtrack: –Systematically enumerates all potential solutions by continually trying to extend a partial solution.
Announcements Assignment #4 is due tonight. Last lab program is going to be assigned this Wednesday. ◦ A backtracking problem.
B ACKTRACK SEARCH ALGORITHM. B ACKTRACKING Suppose you have to make a series of decisions, among various choices, where You don’t have enough information.
Eight queens puzzle. The eight queens puzzle is the problem of placing eight chess queens on an 8×8 chessboard such that none of them are able to capture.
Page 1 Recursion and Exhaustion Yip Lik Yau. Page 2 Why Exhaustion?  Many problems can be solved by brute force in a reasonable amount of time if the.
© The McGraw-Hill Companies, Inc., Chapter 2 The Complexity of Algorithms and the Lower Bounds of Problems.
Dynamic Programming Dynamic Programming is a general algorithm design technique for solving problems defined by recurrences with overlapping subproblems.
Backtracking COP Backtracking  Backtracking is a technique used to solve problems with a large search space, by systematically trying and eliminating.
Backtracking What is backtracking?
Backtracking.
2 -1 Chapter 2 The Complexity of Algorithms and the Lower Bounds of Problems.
3 -1 Chapter 3 The Greedy Method 3 -2 The greedy method Suppose that a problem can be solved by a sequence of decisions. The greedy method has that each.
November 10, 2009Introduction to Cognitive Science Lecture 17: Game-Playing Algorithms 1 Decision Trees Many classes of problems can be formalized as search.
CS420 lecture ten BACKTRACK. Solution vectors In optimization problems, or more general in search problems, a set of choices are to be made to arrive.
CHAPTER 4 Searching. Algorithm Binary Search This algorithm searches for the value key in the nondecreasing array L[i],..., L[j]. If key is found,
Ch 13 – Backtracking + Branch-and-Bound
Backtracking.
1 Adversary Search Ref: Chapter 5. 2 Games & A.I. Easy to measure success Easy to represent states Small number of operators Comparison against humans.
Data Structures Using C++ 2E Chapter 6 Recursion.
© The McGraw-Hill Companies, Inc., Chapter 3 The Greedy Method.
Vlad Furash & Steven Wine.  Problem surfaced in 1848 by chess player Max Bezzel as 8 queens (regulation board size)  Premise is to place N queens on.
Artificial Intelligence Lecture 9. Outline Search in State Space State Space Graphs Decision Trees Backtracking in Decision Trees.
Data Structures Using C++ 2E Chapter 6 Recursion.
Formal Description of a Problem In AI, we will formally define a problem as –a space of all possible configurations where each configuration is called.
Backtracking. N-Queens The object is to place queens on a chess board in such a way as no queen can capture another one in a single move –Recall that.
Lecture 5: Backtracking Depth-First Search N-Queens Problem Hamiltonian Circuits.
Recursion: Backtracking
Back Tracking Project Due August 11, 1999 N-queens: –A classic puzzle for chess buffs is the N- Queens problem. Simply stated: is it possible to place.
Design and Analysis of Algorithms - Chapter 111 How to tackle those difficult problems... There are two principal approaches to tackling NP-hard problems.
Chapter 12 Coping with the Limitations of Algorithm Power Copyright © 2007 Pearson Addison-Wesley. All rights reserved.
Brought to you by Max (ICQ: TEL: ) March 12, 2005 Recursion and Exhaustion.
Contents of Chapter 7 Chapter 7 Backtracking 7.1 The General method
Two Dimensional Arrays
Data Structures Using C++ 2E1 Recursion and Backtracking: DFS Depth first search (a way to traverse a tree or graph) Backtracking can be regarded as a.
Algorithmics - Lecture 131 LECTURE 13: Backtracking.
Hard Problems Some problems are hard to solve.  No polynomial time algorithm is known.  E.g., NP-hard problems such as machine scheduling, bin packing,
COPING WITH THE LIMITATIONS OF ALGORITHM POWER
Hard Problems Sanghyun Park Fall 2002 CSE, POSTECH.
Types of Algorithms. 2 Algorithm classification Algorithms that use a similar problem-solving approach can be grouped together We’ll talk about a classification.
Ch22.Branch and Bound.
Introduction to State Space Search
Search in State Spaces Problem solving as search Search consists of –state space –operators –start state –goal states A Search Tree is an efficient way.
Graph Connectivity This discussion concerns connected components of a graph. Previously, we discussed depth-first search (DFS) as a means of determining.
Analysis & Design of Algorithms (CSCE 321)
Chapter 13 Backtracking Introduction The 3-coloring problem
February 11, 2016Introduction to Artificial Intelligence Lecture 6: Search in State Spaces II 1 State-Space Graphs There are various methods for searching.
Unit – 5: Backtracking For detail discussion, students are advised to refer the class discussion.
CSG3F3/ Desain dan Analisis Algoritma
BackTracking CS255.
Depth-First Search N-Queens Problem Hamiltonian Circuits
Intro to Computer Science II
CSCI 104 Backtracking Search
Data Structures and Algorithms
Fundamentals of Programming II Backtracking with Stacks
BACKTRACKING- GENERAL METHOD
Design and Analysis of Algorithm
Sit-In Lab 1 Ob-CHESS-ion
Exact Solutions to NP-Complete Problems
Back Tracking.
Analysis and design of algorithm
adapted from Recursive Backtracking by Mike Scott, UT Austin
Haskell Tips You can turn any function that takes two inputs into an infix operator: mod 7 3 is the same as 7 `mod` 3 takeWhile returns all initial.
Presented By: David Miller
Backtracking and Branch-and-Bound
Announcements Assignment #4 is due tonight. Last lab program is going to be assigned this Wednesday. ◦ A backtracking problem.
Presentation transcript:

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 7 th 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.chessqueens

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 e c 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 e c 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 Columns Rows 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

Required solution When N = 4, Empty board Step 1 Step 2 Backtracking Step 3 Step 4 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) (2, 4, 1, 3) Another possible solution for this problem is (3,1,4,2). SOLUTION

FOR, N = 8 Step 1 Step 2 Step 3 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 Row 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

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 |

ALGORITHM PLACE (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

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) then 2) 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

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 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 Unique: Distinct:

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