How does backtracking work in solving the n queens problem?

  • Context: Java 
  • Thread starter Thread starter FallenApple
  • Start date Start date
Click For Summary

Discussion Overview

The discussion centers around the implementation of backtracking in solving the n queens problem, specifically addressing how queens are placed and removed from the chessboard during recursive calls. Participants explore the mechanics of the algorithm, including the necessity of unplacing queens to allow for continued exploration of potential solutions.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant notes the requirement for placing n queens on an n×n chessboard without them attacking each other and questions the code's handling of queen placements after recursion.
  • Another participant suggests that a line of code to unplace a queen is necessary to ensure that the queen is removed after a recursive call, indicating that without it, the queen would remain placed.
  • There is a reiteration of the need for the unplacing code to be positioned correctly in relation to the if/else statements, emphasizing that it should occur after the entire if-else block to allow for continued solution exploration.
  • One participant concludes that backtracking occurs when the for loop finds no valid placements, leading to the necessity of removing the queen to explore other possibilities.
  • A reference to a book is made, suggesting it provides clarity on recursive algorithms relevant to the discussion.

Areas of Agreement / Disagreement

Participants express differing views on the placement of the unplacing code and its implications for the algorithm's functionality. There is no consensus on the exact implementation details, indicating ongoing debate regarding the correct approach to backtracking in this context.

Contextual Notes

Participants highlight the importance of code structure in recursive algorithms, particularly the placement of lines that manage queen placements and removals. The discussion reflects uncertainty about the implications of these placements on the algorithm's ability to find all solutions.

FallenApple
Messages
564
Reaction score
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
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.
 
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?
 
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
 
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.
 
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.
 

Similar threads

  • · Replies 11 ·
Replies
11
Views
5K
  • · Replies 20 ·
Replies
20
Views
9K
Replies
1
Views
2K
  • · Replies 17 ·
Replies
17
Views
7K
Replies
5
Views
2K
  • · Replies 107 ·
4
Replies
107
Views
20K
  • · Replies 39 ·
2
Replies
39
Views
7K
  • · Replies 65 ·
3
Replies
65
Views
12K