Professional Documents
Culture Documents
dynamic programming
applicable to some problems (e.g., the knapsack problem)
backtracking
eliminates some unnecessary cases from consideration
yields solutions in reasonable time for many instances but worst case is
still exponential
branch-and-bound
further refines the backtracking idea for optimization problems
Backtracking & Branch and Bound
Improvement over exhaustive search
It construct one candidate solution at a time and evaluate
the partially constructed solution; if no potential values of
the remaining components can lead to a solution, the
remaining is not generated.
It uses state space tree.
It terminates a node as soon as it can be guaranteed that no
solution to the problem can be obtained by considering
choices that corresponds to the descendants.
Comparison
1 2 3 4
1 queen 1
2 queen 2
3 queen 3
4 queen 4
State-Space Tree of the 4-Queens Problem
Hamiltonian circuit problem
In the mathematical field of graph theory, a Hamiltonian
path is a path in an undirected graph which visits each
vertex exactly once. A Hamiltonian cycle (or Hamiltonian
circuit) is a cycle in an undirected graph which visits each
vertex exactly once and also returns to the starting vertex.
Determining whether such paths and cycles exist in graphs
is the Hamiltonian path problem which is NP-complete.
Example: Hamiltonian Circuit Problem
a b
c f
d e
Hamiltonian Circuit Problem
It starts at vertex a.
3 adjacent nodes can be visited, tie is resolved, we selected
b.
From b, algorithm proceeds c, then d and then e and finally
f, which is an dead end.
Then Backtrack from f to e, then e to d, then d to c, which
provides first alternate to pursue. From c we can go to e
and f, which is also dead end, backtrack to e and from e we
can go to d.
Backtrack to b, the alternate is f,e,c,d and then reach a.
Sum of Subset problem
Problem statement: Let, S = {S1, ..Sn} be a set of n
positive integers, then we have to find a subset whose sum is
equal to given positive integer d. For example, for
S = {1, 2, 5, 6, 8} and d = 9, there are two solutions: {1, 2, 6}
and {1, 8}.
It is always convenient to sort the sets elements in
ascending order. That is
S1 S2 S3 Sn
The state space tree can be constructed as a binary tree as
in fig. for instance S = {3, 5, 6, 7} and d=15.
Steps:
Step 1: The root of the tree represents the starting point, with no decisions about the
Given elements.
Step 2: Its left and right children represent, inclusion and exclusion of S1 in the set being
Sought.
Step 3: Going to the left from a node of the first level corresponds to inclusion of S2,
While going to right corresponds to exclusion.
Step 4: A path from the root to a node at the ith level of the tree indicates which of the
first i numbers have been included in the subsets represented by that node.
Step 5: We record the value of s, the sum of these numbers in the node. If s is equal to
d, we have a solution to the problem and stop. If all the solutions need to be
found, continue by backtracking to the nodes parent. If s is not equal to d, we
can terminate the node as nonpromising if either of the two equaities holds:
s + si+1 > d (the sums too large)
n
s '+ sj < d
j =i +1
0
with 3 w/o 3
3 0
with 5 w/o 5 with 5 w/o 5
8 3 5 0
with 6 w/o 6 with 6 w/o 6 with 6 w/o 6 X
0+13<15
14 8 9 3 11 5
X with 7 w/o 7 X X X X
14+7>15 9+7>15 3+7<15 11+7>14 5+7<15
15 8
solution X
8<15