Lecture 5: Backtracking: Depth-First Search N-Queens Problem Hamiltonian Circuits
Lecture 5: Backtracking: Depth-First Search N-Queens Problem Hamiltonian Circuits
Backtracking
Backtracking is closely related to the brute-force problem-solving method in
which the solution space is scanned, but with the additional condition that only the possible candidate solutions are considered.
What is meant by possible solutions and how these are differentiated from the impossible ones are issues specific to the problem being solved.
function backtrack(current depth) if solution is valid return / print the solution else for each element from A[ ] source array let X[current depth] element if possible candidate (current depth + 1) backtrack(current depth + 1) end if end for end if end function
http://www.devarticles.com/c/a/Development-Cycles/The-Backtracking-Algorithm-Technique/
end loop;
end depth_first_tree_search;
3
11 12 13
We will use the convention of choosing nodes in a left-to-right order (or alphabetical if labeled).
4
10
14 15 18 5 6 7 8 9 16 17
Depth-First Search
Depth-First traversal is a type of backtracking in a graph. If we use an alpha-numeric order for node traversal we can define a unique ordering of the nodes encountered in a connected graph.
A
B E G D
F H I
AB AC AD AE BA BG CA CF DA DF DH EA EG EH
FC FD FH F I GB GE GH HD HE HF HG H I I F IH
Starting at node A we can traverse every other node in a depth-first order, making sure that we do not enter any node more than once. ABGEHDFCI
We move forward from A to C and then we have to backtrack to F and move forward to I.
Backtracking Technique
Backtracking is used to solve problems in which a feasible solution is needed rather than an optimal one, such as the solution to a maze or an arrangement of squares in the 15-puzzle. Backtracking problems are typically a sequence of items (or objects) chosen from a set of alternatives that satisfy some criterion.
6 10 14 15
Backtracking Implementation
Backtracking is a modified depth-first search of the solution-space tree. In the case of the maze the start location is the root of a tree, that branches at each point in the maze where there is a choice of direction.
N-Queens Problem
The problem of placing N queens on an NxN chessboard in such a way that no two of them are "attacking" each other, is a classic problem used to demonstrate the backtracking method. A simple brute-force method would be to try placing the first queens on the first square, followed by the second queen on the first available square, scanning the chessboard in a row-column manner. A more efficient backtracking approach is to note that each queen must be in its own column and row. This reduces the search from (N2)! to N!.
Can you see how this code checks for "attacking" queens?
void main() { char ch; printf("Enter the size of NxN chessboard: "); scanf("%d",&n); printf("\nThe solution: "); back(0); }
v1 v4
v2 v5
v3 v6
v1 v4
v2 v5
v3 v6
A state space tree for this problem is as follows. Put the starting vertex at level 0 in the tree, call this the zero'th vertex on the path. At level 1, consider each vertex other than the starting vertex as the first vertex after the starting one. At level 2, consider each of these vertices as the second vertex, and so on. You may now backtrack in this state space tree.
vertices.
Sample Problem
v1
v2
v3
v4
1
state space tree
2
v5 v6
graph
v7
v8 5 6 3 3 4 8 : : 8 4 7 2 7 : : 6 3
Game Trees
The state-space tree showing all legal moves of both players starting from some valid game state is called the game tree. We can define a function that estimates the value of any game state relative to one of the players. For example, a large positive value can mean that this is a good move for Player 1, while a large negative value would represent a good move for Player 2. The computer plays the game by expanding the game tree to some arbitrary depth and then bringing back values to the current game state node.
Ply 0
current node
Ply 1
-3
+2
+1
+3
-1
-3
-2
+1
Mini-Max a definition
A program starts with the current game state and generates all legal moves...all legal responses to these moves...and so on until a fixed depth is reached. At each leaf node, an evaluation function is applied which assigns a numerical score to that board position. These scores are then ``backed up'' by a process called minimaxing, which is simply the assumption that each side will choose the line of play most favorable to it at all times. If positive scores favor Player 1, then Player 1 picks the move of maximum score and Player 2 picks the move of minimum score.
Ply 0 is the Player 1's move so we choose the maximum of the Ply 1 values. So the best move for Player 1 results in at least a +1 return value...
+1 Ply 0
MAX
MIN
-3
+1
-3
-2
Ply 1
-3
+2
+1
+3
-1
-3
-2
+1
If A is an ancestor of X, where A is a max node and X is a min node, then whenever Beta(X) < Alpha(A), we know that if f(X) is good enough to be propagated all the way to B, then it will lose to one of As alternative moves.
-1
-1
max
-3
-4
So in either case, f(X) will have no influence in determining the next move, so we can stop evaluating its children.
max
min
-1
+2
-3
-4
Similarly, if Y is a max node and a descendant of B, then we can prune Y whenever Alpha(Y) > Beta(B).
-1 -2 -3 +2 -1 -3 -4 -3 +3 +4 -4 -5 +4 +5
Summary
Backtracking is... an efficient means of implementing brute-force search inherently depth-first to be considered when any solution will do N-Queens Problem Hamiltonian Circuits Game Trees MiniMax and Alpha-Beta Pruning