 Hello and welcome to this video lecture. I am Vinit Thribune from WIT Solapur and today we shall see how to solve the nqueens problem by backtracking approach. So in this video lecture we shall discuss what exactly is the nqueens problem, what constraints do we have in this problem and how backtracking approach helps us to find a solution to this particular problem. So at the end of this session you will be able to apply backtracking approach to solve nqueens problem. So what exactly is the problem? We are given n cross n chessboard and nqueens if it is a 8 cross 8 chessboard as we usually use so we will be given 8 queens if it is say for example a 4 cross 4 chessboard we will be given 4 queens and so on. The objective of the problem is to place the nqueens on the chessboard such that no 2 queens are in the same row, no 2 queens are in the same column and no 2 queens are in the same diagonal. So if you are well versed with the game of chess you know that a queen can traverse in any diagonal in any row and in any column from its current position. So our objective is that we have to place the nqueens in such a way that no queen attacks any other queen. So if you see in this given picture here that no queen is attacking any other queen. So how do we use the backtracking approach to find a solution to the nqueens problem? Basically as you know that in backtracking we generate the state space tree so we will generate the state space tree for all the possibilities of the position of queens. If any queen is attacking or will attack any other queen in the next step the further path is bounded and the solution path is backtracked. We continue with the solution path in the tree only if all queens are safe and no queen is attacking any other. So we take a 4 x 4 chess board and we take 4 queens and we will find out all the position of the 4 queens on the board and try to find a solution for this particular problem. So we start by placing the queens on the board from the first column and we will see how to place all the 4 queens on the particular given 4 x 4 chess board. So we start with the first queen, we place the first queen on the first block in the first column. Now we cannot place the second queen in the same column. So we move ahead and see for the next column we cannot also place the second queen in the diagonal from the first queen because it will make the constraint as false and these two queens can attack each other. So we move for the next block. Here both the queens are safe and this position is okay for the second queen for the moment. So we want to continue solving the problem so we want to search for the position of the next queen. The third queen cannot be placed in the first column. It cannot be placed in the second column also because it is being attacked by the second queen in the diagonal way. It cannot be placed in the third column also because both the queens are in the same same column and this position is also unsafe for both the queens since they are in the same diagonal. So what to do? Now we will be requiring to use the backtracking. Now since the third queen cannot be placed anywhere further because we just have four cross four chess board and we have seen all available four positions we backtrack the previous queen that is the position of the second queen to the next block because even this position is safe for the second queen. So we have backtracked our solution. Now we check for the safe position for the third queen. So the third queen cannot be placed in the first column because it is attacking the first queen. Here the second column is safe for the third queen because it is attacking no other queen so it is safe for this moment. Now we want to place the fourth queen so we start with the first column it is unsafe because it can attack two other queens. It is unsafe here as well because it is still attacking the second queen diagonally and the third queen it is attacking as they both are in the same column. This position is also unsafe because queen number three and queen four are in the same diagonal. Even this place or this position is unsafe because they are in the same column. Now since the fourth queen cannot be placed anywhere further and that is why and since backtracking of second queen is already done what we do is we backtrack the position of the previous that is the first queen to the next block. So now what we will do is we have backtracked the position of the first queen from the first column to the second column and now we are trying to find out the safe positions for the other three queens. Now second queen if we start from the first column it is unsafe position because it is diagonally in the same diagonal as the first queen. It is unsafe in the second column also similarly it is unsafe in the third column also because both of them are attacking each other diagonally but in the next block that is the fourth column the second queen is safe. So at this point in time we place it in the fourth column. Now if we start with the first column for the third queen it is absolutely safe here and at this point in time we place it in the first column and now we want to see which position out of the four blocks can accommodate the fourth queen. So first column is unsafe for the fourth queen, second column is also unsafe because it is attacking or will be attacked by all three queens, third column or third block is safe because it is not attacking any other queen and this is the final solution that we have arrived that the first queen will be placed in the second column or the second block, third queen in the fourth column, third queen in the first column and the fourth queen in the third column. So this is the final solution of the n queens problem. So this is the search space or the state space tree the complete state space tree that we have generated for this four cross four chess board requiring to place four queens. So if you see that all these positions will be checked and if the queen is attacking each other or is not safe it will be backtracked to the particular previous solution. So we will see the portion of the state space tree, this is the portion of the state space tree where we have arrived to our solution. So we saw that the first queen was placed in the second column, second queen was placed in the fourth column, third queen was placed in the first column and second or the fourth queen was placed in the third column. So this is our solution that we have arrived, this is the portion of the state space tree that is showing us the solution two four one three that is the position of the queens. So at this point in time I want you to pause the video and place the four queens on the board to find the solution to the end queens problem. We have already seen one solution to the four cross four chess board and how we place four queens on that board. I want you to find another solution to the same problem. Pause the video and solve the question. So if you have applied backtracking solution or backtracking approach to the problem you will come to find out that another solution to the same problem is three one four two where the first queen is placed in the third column or the third block. Second queen is placed in the first block or the first column. Third queen is placed in the fourth block or the fourth column and the fourth queen or the last queen is placed in the second column or the second block. This is the final solution to the problem. Thank you.