How does backtracking work in solving the n queens problem?

  • Java
  • Thread starter FallenApple
  • Start date
In summary, the conversation discusses the n queens problem, which involves placing n chess queens on an n x n chessboard without them attacking each other. The conversation also touches on the use of backtracking in finding solutions for this problem. There is a suggestion to add a line to unplace a queen after the recursive call, which would allow the program to continue searching for solutions.
  • #1
FallenApple
566
61
I'm watching a vid on the n queens problem. Basically I have find out how to put n queens on a chess board of size n without them attacking each other. It's a rather famous puzzle.

"The eight queens puzzle is the problem of placing eight chess queens on an 8×8 chessboard so that no two queens threaten each other. Thus, a solution requires that no two queens share the same row, column, or diagonal. The eight queens puzzle is an example of the more general n queens problem of placing n non-attacking queens on an n×n chessboard, for which solutions exist for all natural numbers n with the exception of n=2 and n=3."

https://en.wikipedia.org/wiki/Eight_queens_puzzle
Nqueen1.png


here there is no positions after the second level of the recursion. So it makes sense that after the third level of the recursion is popped from the stack, the second level will just continue the for loop.But why is the previous placement replaced?

Nqueen2.png


He mentions that when the backtrack happens, the queen is placed on the 4th column. But that is only possible if the previous placement is nullifed. But how? I see nothing in the code that can erase the result of board[2][3]==2.
 
Technology news on Phys.org
  • #2
Even a youtube commenter got this:

Shouldn't there be a line to unplace a queen like this: board[row][column] = false; Otherwise, the queen will never be unplaced after the recursive call.

answer yes, there should be such a line, which should come after the if/else statement. You could only skip it if the board[][] array is a local variable which would be passed by value to NQueensBackTrack(). Adding and later removing the queensl should be faster.
 
  • #3
willem2 said:
Even a youtube commenter got this:

Shouldn't there be a line to unplace a queen like this: board[row][column] = false; Otherwise, the queen will never be unplaced after the recursive call.

answer yes, there should be such a line, which should come after the if/else statement. You could only skip it if the board[][] array is a local variable which would be passed by value to NQueensBackTrack(). Adding and later removing the queensl should be faster.
So its right after the if else statement on the same level as it but not within it?
 
  • #4
FallenApple said:
So its right after the if else statement on the same level as it but not within it?

It seems the progam continues after "Print board" to find other solutions, so you should remove the queen always, so the program can continue, so it should not be in the else block, but after the entire if-else
 
  • #5
willem2 said:
It seems the progam continues after "Print board" to find other solutions, so you should remove the queen always, so the program can continue, so it should not be in the else block, but after the entire if-else

That makes sense. So the backtrack happens when the for loop finds nothing. If the for loop keeps finding something, eventually it will just hit print board, and never returns to the earlier call to reset the queen as removed.
 
  • #6
For an explanation of this problem see Wirth, Algorithms + Data Structures = Programs (1976), Section 3.5. The whole of chapter 3 deals with recursive algorithms with great clarity.
 

1. What is the n queens problem?

The n queens problem is a classic mathematical puzzle that involves placing n queens on an n x n chessboard in such a way that no two queens are attacking each other. In other words, no two queens can be in the same row, column, or diagonal.

2. What is backtracking?

Backtracking is a problem-solving algorithm that involves trying different solutions to a problem and backtracking or undoing a solution if it is found to be incorrect or leads to a dead end. It is commonly used in computer science and mathematical problem solving.

3. How does backtracking work in solving the n queens problem?

In the n queens problem, backtracking works by recursively trying different positions for each queen on the board. If a queen can be placed without attacking any other queen, it is left in that position. If not, the algorithm backtracks and tries a different position for the previous queen. This process continues until a solution is found or all possible combinations have been exhausted.

4. What is the time complexity of backtracking in solving the n queens problem?

The time complexity of backtracking in solving the n queens problem is O(n!), where n is the number of queens. This means that the time taken to solve the problem increases exponentially with the number of queens, making it impractical for large values of n.

5. Are there any other techniques for solving the n queens problem?

Yes, there are other techniques for solving the n queens problem, such as using a heuristic algorithm or using bit manipulation. However, backtracking is the most commonly used approach due to its simplicity and wide applicability to other problems.

Similar threads

Replies
20
Views
4K
  • Math POTW for University Students
Replies
1
Views
2K
  • Linear and Abstract Algebra
Replies
17
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
1K
  • Math Proof Training and Practice
4
Replies
107
Views
15K
  • Math Proof Training and Practice
3
Replies
83
Views
17K
  • Math Proof Training and Practice
2
Replies
38
Views
6K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
7K
  • STEM Academic Advising
Replies
10
Views
4K
  • MATLAB, Maple, Mathematica, LaTeX
2
Replies
39
Views
4K
Back
Top