Backtracking: Depth-First Search N-Queens Problem Hamiltonian Circuits
Backtracking: Depth-First Search N-Queens Problem Hamiltonian Circuits
Depth-FirstSearch
N-QueensProblem
HamiltonianCircuits
Backtracking
functionbacktrack(currentdepth)
ifsolutionisvalid
return/printthesolution
else
foreachelementfromA[]sourcearray
letX[currentdepth]element
ifpossiblecandidate(currentdepth+1)
backtrack(currentdepth+1)
endif
endfor
endif
endfunction
http://www.devarticles.com/c/a/Development-Cycles/The-Backtracking-Algorithm-Technique/
Depth-First Tree Search
procedure depth_first_tree_search(v:node)
u : node;
begin
for each child u of v loop 1
depth_first_tree_search(u);
end loop; 2
11
end depth_first_tree_search;
3 10 12
We will use the convention of choosing nodes in
13
a left-to-right order (or alphabetical if labeled).
4
14 15 18
5 9
16 17
6
7 8
Depth-First Search
6
10
14
15
Backtracking Implementation
void main()
Can you see how this code {
checks for "attacking" queens? char ch;
printf("Enter the size of NxN chessboard: ");
scanf("%d",&n);
printf("\nThe solution: ");
back(0);
}
Hamiltonian Circuits Problem
A Hamiltonian circuit or tour of a graph is a path that starts at a given vertex, visits
each vertex in the graph exactly once, and ends at the starting vertex. Some graphs do
not contain Hamiltonian circuits.
v1 v2 v3 v1 v2 v3
v4 v5 v6 v4 v5 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.
Backtracking in a State Space Tree
v1 v2 v3 v4 1
state space tree
2 5
v5 v6 v7 v8
5 7 2 6
graph
6 7 3
3 4 8 :
:
3 8 4
:
:
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 mini-
maxing, 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.
Minimax Game Tree
We will assume that a large positive value is good for the Player 1. To determine Player 1's
next move, we will search the possible moves for both players assuming that each player
will make the best possible move. Ply 1 is Player 2's move so we will want to return the
minimum value from Ply 2 into each Ply 1 node.
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...
MAX +1 Ply 0
MIN -3 +1 -3 -2 Ply 1
-3 +2 +1 +3 -1 -3 -2 +1
Alpha-Beta Pruning Rule
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
-1 max
be propagated all the way to B, then
it will lose to one of As alternative
moves.
-1 -3 -4 min
So in either case, f(X) will have no
influence in determining the next
move, so we can stop evaluating its
children.
max -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