Professional Documents
Culture Documents
Algo Mod10 Backtracking
Algo Mod10 Backtracking
ISSAM ALHADID
25/3/2019
General Method.
Generic Problem Formulation.
n-Queens Problem.
Hamiltonian Cycles.
Backtracking is a general algorithm for
finding all (or some) solutions to some
computational problems, that incrementally
builds candidates to the solutions, and
abandons each partial candidate
(“backtracks”) as soon as it determines that
the candidate cannot possibly be completed
to a valid solution
Backtracking is an algorithmic-technique for
solving problems recursively by trying to
build a solution incrementally, one piece at a
time, removing those solutions that fail to
satisfy the constraints of the problem at any
point of time
This is better than naive approach
(generating all possible combinations of
digits and then trying every combination one
by one) as it drops a set of permutations
whenever it backtracks.
The idea is that we can build a solution step
by step using recursion; if during the process
we realize that is not going to be a valid
solution, then we stop computing that
solution and we return back to the step
before (backtrack).
This search is often facilitated by a tree
organization (permutation tree) for the
solution space.
Backtracking algorithm is based on
systematic search of the solution space based
on:
◦ Tree organization of the solution space.
◦ Depth first node generation search of the tree.
◦ Use of bounding function to limit the search
Problem State: Each node in a tree defines a
problem state.
State space: All paths from the root to other
nodes define the state space of the problem.
Solution states: Solution states are those
problem states S for which the path from the
root to S defines a tuple in the solution space.
Answer states: Answer states are those problem
states S for which the path from the root to S
defines a tuple which is the member of the set of
solutions (i.e. satisfies implicit constraints).
Tree organization of the solution space: is state
space tree.
If we represent solution space in the form of a
tree then the tree is referred as the state space
tree.
Once the state space tree has been conceived
(designed) for any problem, this problem may
be solved systematically generating the
problem states, determining which of these are
solution states and finally determining which
solution states are answer states.
Live node: A node which has been generated
and all of whose children have not yet been
generated is live node.
E-node: The live nodes whose children are
currently being generated is called E-node
(node being expanded)
Dead node: It is a generated node that is
either not to be expanded further or one for
which all of its children has been generated.
Bounding function: It will be used to kill live
nodes without generating all their children
Depth first node generation search of the
tree: Viewing backtracking as depth-first
search …. Depth-first search (DFS) is
algorithm starts at the root (top) node of a
tree and goes as far as it can down a given
branch (path), then backtracks until it finds
an unexplored path, and then explores it.
In order to apply the backtracking method, the
desired solution must be expressible as an n-tuple
(x1, …, xn), where the xi are chosen from some finite
set Si.
The problem to be solved calls for finding one
vector which maximizes or minimizes or satisfies a
criterion function P(x1, …, xn).
Sometimes it seeks all such vectors which satisfy P.
Suppose mi is the size of set Si, then there are m =
m1 m2 … mn n-tuples which are possible candidates
for satisfying the function P.
Many problems solved using backtracking require all
solutions satisfy a set of constraints.
These constraints are divided into two categories:
explicit and implicit.
Explicit constraints are rules which restrict each xi to
take on values only from a given set.
Common examples of explicit constraints are:
◦ xi ≥ 0 or Si = {all nonnegative real numbers}
◦ xi = 0 or 1 or Si = {0, 1}
Implicit constraints These are the rules which determine
which of the tuples in the solution space satisfies the
criterion functions.
◦ Example.1: No two queens should in same row, same column
or diagonal position.
◦ Example.2: No two xi ’s can be same
n-Queens Problem
Example: 4-Queens Problem
n-Queens Algorithm
n-Queens: Time & Space
The n-queens problem is a generalization of
the 4-queens problem.
Problem Statement for n-queens problem:
◦ n-queens are to be placed on a n × n chessboard
so that no two “attack” (that is no two queens are
on the same row, column, or diagonal).
Thus, the solution space consists of all n!
permutations of the n-tuple (1, 2, …, n).
Problem Statement:
◦ A classical combinatorial problem is to place four
queens on an 4 × 4 chessboard so that no two
“attack”, that is no two of them are on the same
row, column or diagonal.
Assumptions:
◦ Let us number the rows and columns of the
chessboard 1 through 4 as shown in Figure 1.
◦ The queens are numbered 1 through 4.
◦ Since each queen must be on different row, we
assume queen i is placed on row i.
Figure 1: One solution to the 4-queens problem