Backtracking
Md. Rezaul Karim
CSEDU
Features of Backtracking
• Backtracking is more intelligent variation of brute force approach
• The principal idea is to construct solutions one component at a time
and evaluate such partially constructed candidates as follows.
• If a partially constructed solution can be developed further
without violating the problem’s constraints, it is done by taking
the first remaining legitimate option for the next component
• If there is no legitimate option for the next component, no
alternatives for any remaining component need to be considered.
• In this case, the algorithm backtracks to replace the last
component of the partially constructed solution with its next
option.
State-SpaceTree
• It is convenient to implement this kind of processing by constructing a tree
of choices being made, called the state-space tree.
• Its root represents an initial state before the search for a solution begins.
• The nodes of the first level in the tree represent the choices made for the
first component of a solution, the nodes of the second level represent the
choices for the second component, and so on.
• A node in a state-space tree is said to be promising if it corresponds to a
partially constructed solution that may still lead to a complete solution;
otherwise, it is called nonpromising
• Leaves represent either nonpromising dead ends or complete solutions
found by the algorithm
State-SpaceTree
• A state-space tree for a backtracking algorithm is constructed in the
manner of depth first search.
• If the current node is promising, its child is generated by adding the first
remaining legitimate option for the next component of a solution, and the
processing moves to this child
• If the current node turns out to be nonpromising, the algorithm backtracks
to the node’s parent to consider the next possible option for its last
component; if there is no such option, it backtracks one more level up the
tree, and so on
• Finally, if the algorithm reaches a complete solution to the problem, it
either stops (if just one solution is required) or continues searching for
other possible solutions
Four-Queens Problem
Hamiltonian Circuit
Branch and Bound
• Deals with optimization problem
• A feasible solution is a point in the problem’s search space that
satisfies all the problem’s constraints (e.g., a Hamiltonian circuit in
the traveling salesman problem or a subset of items whose total
weight does not exceed the knapsack’s capacity in the knapsack
problem),
• An optimal solution is a feasible solution with the best value of the
objective function (e.g., the shortest Hamiltonian circuit or the most
valuable subset of items that fit the knapsack).
Branch and bound
• Additional two item
• A way to provide, for every node of a state-space tree, a bound on the best value of
the objective function on any solution that can be obtained by adding further
components to the partially constructed solution represented by the node
• The value of the best solution seen so far
• If this information is available, we can compare a node’s bound value with
the value of the best solution seen so far.
• If the bound value is not better than the value of the best solution seen so
far—i.e., not smaller for a minimization problem and not larger for a
maximization problem—the node is nonpromising and can be terminated
(some people say the branch is “pruned”)
• Indeed, no solution obtained from it can yield a better solution than the one
already available. This is the principal idea of the branch-and-bound technique.
• we terminate a search path at the current node in a state-space tree of a
branch-and-bound algorithm for any one of the following three reasons:
• The value of the node’s bound is not better than the value of the best solution
seen so far.
• The node represents no feasible solutions because the constraints of the
problem are already violated.
• The subset of feasible solutions represented by the node consists of a single
point (and hence no further choices can be made)—in this case, we compare
the value of the objective function for this feasible solution with that of the
best solution seen so far and update the latter with the former if the new
solution is better.