N queens using backtracking

3,119 views 13 slides Apr 01, 2019
Slide 1
Slide 1 of 13
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13

About This Presentation

N queens problem using backtracking


Slide Content

Presented by : G.SRILEKHA B16CS045 Counsellor : B.SRINIVAS sir N-Queens problem

contents Problem statement History Backtracking Data flow diagram Algorithm Output Advantages over other methods Conclusion

Problem Statement The N Queen is the problem of placing N chess queens on an N×N chessboard so that no two queens attack each other. That means , no two queens are placed in same row or in same column or diagonal to each other.

History Chess composer Max Bezzel  published the eight queens puzzle in 1848.Franz Nauck  published the first solutions in 1850.Nauck also extended the puzzle to the  n  queens problem, with  n  queens on a chessboard of  n  ×  n  squares. Since then, many mathematicians, including Carl Friedrich Gauss, have worked on both the eight queens puzzle and its generalized  n -queens version. In 1874,  S.Gunther  proposed a method using determinants to find solutions.J.W.L . Glaisher  refined Gunther's approach.

BACKTRACKING Backtracking is finding the solution of a problem whereby the solution depends on the previous steps taken. Thus, the general steps of backtracking are: start with a sub-solution check if this sub-solution will lead to the solution or not If not, then come back and change the sub-solution and continue again

start t P lace a Queen and check for next queen Is possible or not Backtrack and again check other column stop N o Yes FLOW CHART

ALGORITHM Place the queens column wise, start from the left most column If all queens are placed. return true and print the solution. Else Try all the rows in the current column. Check if queen can be placed here safely if yes mark the current cell in solution matrix as 1 and try to solve the rest of the problem recursively. If placing the queen in above step leads to the solution return true. If placing the queen in above step does not lead to the solution , BACKTRACK, mark the current cell in solution matrix as 0 and return false. If all the rows are tried and nothing worked, return false and print NO SOLUTION.

Output ( if n=4)

If we continue the process , we get another solution as : First Solution

TIME TAKEN FOR EACH INTEGER N OF A QUEEN

Advantages over other methods   The major advantage of the backtracking algorithm is the abillity to find and count all the possible solutions rather than just one while offering decent speed. In fact this is the reason it is so widely used. Also one can easily produce a parallel version of the backtracking algorithm increasing speed several times just by starting multiple threads with different starting positions of the first queens.  

Conclusion In N-Queen, as N value increases the time of processing also takes more time .This happens when we try to display all the possible solutions. Of course, we could make it much faster if we wanted to only find one solution instead of all of them: not more than a few milliseconds for board sizes up to 50.

THANK YOU !!