Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 24

Backtracking

&
Branch and Bound
Backtracking

• This is problem solving strategy


• Is used to solve problems for which a sequence of objects is to be
selected from a set such that the sequence satisfies some constraint
• Traverses the state space using a depth-first search.
Backtracking

• Performs a depth-first traversal of a tree


• It is used when you have multiple solutions and you want all those
solutions.
• Applies to Non-optimization problems.
Cont..

• There are few problems that comes in backtracking


1)N Queen problem
2)Subset problem
3)Graph coloring problem
Example: N-Queens Problem-Backtracking

• Given an N x N sized chess board


• Objective: Place N queens on the board so that no
queens are in danger
• The problem is to place n queens on an n-by-n
chessboard such that no two queens attack each
other by being in the same row, same column or on
the same diagonal.
N-Queen Problem

1 2 3 4
1 queen 1
2 queen 2
3 queen 3
4 queen 4

Figure: Board for the four-queens problem.


6
• One option would be to generate a tree of every possible board layout
• This would be an expensive way to find a solution
Backtracking
• Backtracking prunes entire sub trees if
their root node is not a viable solution
• The algorithm will “backtrack” up the
tree to search for other possible
solutions
Sum of Subset Problem-Backtracking

• Subset sum problem is used to find the subsets of element that are
selected from a given set whose sum adds up to a given number.
Subset problem

• Problem: Given a set S = {s1, s2, . . . , sn} of n positive


integers. Find a subset of S whose sum is d. Example:
S = {1, 5, 7, 4, 3}, d = 12, then answer could be {5, 7} or
{4, 3, 5}.
• Root can be empty subset.
• Nodes at the ℓ-th level decide whether to add/not add si
to the sum.
• If current sum > d, then the sum is too high, and any
further search in this branch can be pruned.
• If current sum plus sum of remaining numbers is < d,
then sum is too small, and any further search in this
branch can be pruned.
• If sum = d, then done.
• Example: S = {3, 5, 6, 7}, d = 15.
Graph coloring problem- Backtracking

• Problem: Given an undirected graph G=(V,E), it is required to color


each vertex in V with one of the three colors, such that no two
neighboring vertices or adjacent vertices have the same color.
• There are 3^n possible coloring to color a graph with n vertices
3-coloring problem
Efficiency of Backtracking

• This given a significant advantage over an exhaustive search of the


tree for the average problem
• Worst case: Algorithm tries every path, traversing the entire search
space as in an exhaustive search
Branch and Bound

• This is a problem solving strategy.


• Where backtracking uses a depth-first search, the branch and bound
algorithm uses a breadth-first search.
• Branch and bound uses a queue as an auxiliary data structure
• It is useful for solving optimization problem.
The Branch and Bound Algorithm

• Starting by considering the root node and applying a lower-bounding


and upper-bounding procedure to it
• If the bounds match, then an optimal solution has been found and the
algorithm is finished
• If they do not match, then algorithm runs on the child nodes
Efficiency of Branch and Bound

• In many types of problems, branch and bound is faster due to the


use of a breadth-first search instead of a depth-first search
• The worst case scenario is the same, as it will still visit every node in
the tree
Thankyou

You might also like