DSA_Backtracking_Branch_Bound_Lec34-36
DSA_Backtracking_Branch_Bound_Lec34-36
Backtracking
● Exhaustive search is required for some problems.
● Backtracking is a more intelligent variation of this
approach.
● Main idea is to construct solutions one component at a
time and evaluate each partially constructed candidates in
following way:
– If a partially constructed solution can be developed,
continue.
– If there is no legitimate option for the next component,
backtrack to replace the last component of the partially
constructed solution with its next option
Backtracking
● We build a state-space tree for a backtracking algorithm
which is usually constructed in the depth-first search way.
● If our algorithm reaches a complete solution to the
problem, we stop (if just one solution is required) or we
continue searching for other possible solutions (if there
are).
● The advantage of this method is that if it is reallized that
the partial vector in the state-space tree can no longer lead
to the optimal solution then all previous steps can be
ignored and we backtrack to pervious step.
– It drastically reduces the number of searches compared to
exhaustive or brute force strategy. (We will look in examples)
Sum of Subsets Problem
● Find a subset of a given set A = {a1, . . . , an }
of n positive integers whose sum is equal to a
given positive integer D.
● For example A = {11, 13, 24, 7} and SUM = 31,
there are two solutions: {11, 13, 7} and {24, 7}.
– We can represent this solution in terms of indices
of these items as well e.g., {1,2,4} and {3,4}.
● Some instances of this problem may have no
solutions.
Sum of Subsets Problem
● It is convenient to sort the set’s elements in
increasing order.
● In the state space tree at a node, If sum is not
equal to D, we can terminate the node as non-
promising if either of the following two
inequalities holds:
– s+ai+1 > D (the sum is too large)
– s+Σ(j=i+1 to n) (aj) < D (the sum is too small)
Sum of Subsets Problem
● Example: For A = {3,5,6,7} and D = 15, the
solution: {3, 5, 7}.
N-queens Problem
● We are given a chessboard of size NxN, and we need to place
N queens so that no queen attacks the other queens. Queen
can attack in three directions: vertical, horizontal and diagonal.
● Lets us look at the statespace tree for a 4-Queen problem (N=4)
N-queens Problem
● We are given a chessboard of size NxN, and we need to place N
queens so that no queen attacks the other queens. Queen can attack
in three directions: vertical, horizontal and diagonal.
● We can see the enormous state-space for the 4-queen problem can
lead to a very intractable brute-force or exhaustive search strategy.
● Let us see how backtracking works on a 4 queen problem.
● We start with the root node and the path in the search space is ().
● Now we generate children in ascending order starting with 2, and path
is now (1)
– This signify placing queen 1 on column 1.
● Now node 3 is generated and immediately killed (why? Because on
second column we cannot place a queen.)
● Now we generate node 8 and path becomes (1,3) as you can see on
next slide.
N-queens Problem
● Node 8 won't lead to a solution as all child will
get killed as evident from the next slide and we
backtrack.
● We now move to path (1,4) representing first
st
queen is placed on 1 column and second
queen is placed on 4th column.
● In this manner we proceed to find the
backtracking solution to 4-queen problem and
in turn the general n-queen problem.
N-Queens Problem
Exercise Problem: Graph Coloring
● Given an undirected graph and a number m, determine if the graph
can be colored with at most m colors such that no two adjacent
vertices of the graph are colored with same color.
● Coloring of graph means assignment of colors to all vertices.
● This Problem is also called as m-colorability decision problem
● Applications:
– Making exam schedule for a university
● a graph where every vertex is a subject and an edge between two
vertices mean there is a common student.
– Mobile Radio Frequency Assignment
● How to assign frequencies to mobile towers, such that frequencies
assigned to all towers at the same location must be different.
(mobile towers are vertices and edge betweem them represent that
they are in range of each other)
● What is the minimum number of frequencies needed?
– Map Coloring
● Geographical maps of countries or states where no two adjacent
cities cannot be assigned same color.
Branch and Bound
● In Backtracking, we cut off a branch of the problem’s state-
space tree as soon as we can deduce that it cannot lead to a
solution
● This idea can be strengthened further if we deal with an
optimization problem.
● An optimization problem seeks to minimize or maximize some
objective function like
– a tour length in TSP,
– Value of items selected in knapsack subject to some constraints.
● An optimal solution is a feasible solution with the best value of
the objective function
Branch and Bound
● Compared to backtracking, branch-and-bound requires two
additional items:
1. 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
2. the value of the best solution seen so far
Branch and Bound
● In general, we terminate a search path for any one of the
following three reasons: