You are on page 1of 52

Modified by Dr.

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

 Expressed as an 4-tuple, the solution is (2, 4, 1, 3).


 All solutions to the 4-queens problem can be
represented as 4-tuples (x1, x2, x3, x4), where
xi is the column on which queen i is placed.
 The explicit constraints using this
formulation are Si = {1, 2, 3, 4}, 1 ≤ i ≤ n.
 Therefore, the solution space consists of 44
4-tuples.
 The implicit constraints for this problem are
that no two xi’s can be the same (that is all
queens must be on different columns) and
 no two queens can be on the same diagonal.
 The first constraint implies that all solutions
are permutations of the 4-tuple (1, 2, 3, 4).
 This realization reduces the size of the
solution space from 44 tuples to 4! tuples.
 Figure 2: Tree organization of 4-queens solution space, where
nodes are numbered as in depth-first search.
 At each internal node in the state space trees
of 4-queens example the solution space is
partitioned into disjoint sub-solution spaces.
 For all of the state space trees, the solution
space will be partitioned into disjoint sub-
solution spaces at each internal node.
 Example:
◦ At node 1 of Figure 2 the solution space is
partitioned into four disjoint sets.
◦ Sub-trees 2, 18, 34, and 50 respectively represent
all elements of the solution space with x1=1, 2, 3,
and 4.
◦ At node 2 the sub-solution space with x1=1 is
further partitioned into three disjoint sets.
◦ Sub-tree 3 (at node 3) represents all solutions
space elements with x1=1 and x2=2.
 Once a state space tree has been formed for any
problem, this problem may be solved by
generating the problem states, determining
which of these are solution states and finally
determining which solution states are answer
states.
 There are two methods to generate the problem
states:
◦ Depth-First generation – FILO stack
◦ Breadth-First generation – FIFO queue
 Both methods begin with root node and generate
other nodes.
 In both methods we will have a list of live
nodes.
 In depth-first generation method, as soon as
a new child C of the current E-node R is
generated, this child will become the new E-
node, and R will become the E-node again
when the sub-tree C has been fully explored.
 Paper soln.
 The n-queens algorithm consists of two
procedures:
◦ PLACE(k): returns true if a queen can be placed in
kth row and X(k)th column, also if there is no other
queen on same diagonal. Otherwise, it returns
false.
◦ NQUEENS(n): using backtracking this procedure
prints all possible placements of n queens on an
n×n chessboard, so that they are non-attacking.
1. Place the first queen in the left upper corner of the
table.
2. Save the attacked positions.
3. Move to the next queen (which can only be placed to
the next line).
4. Search for a valid position. If there is one go to step 8.
5. There is not a valid position for the queen. Delete it
(the x coordinate is 0).
6. Move to the previous queen.
7. Go to step 4.
8. Place it to the first valid position.
9. Save the attacked positions.
10. If the queen processed is the last stop otherwise go to
step 3.
 Hamiltonian Cycles Problem
 Example: Hamiltonian Cycles
 Hamiltonian Cycles Algorithm
 Hamiltonian Cycles: Time & Space
; the start point is the
End point (Cycle)
≠0
 Exercise:
 Find both time and space for both
procedures: NEXTVALUE(k) and
HAMILTONIAN(k).

You might also like