DAA Unit 6
DAA Unit 6
DAA Unit 6
Algorithms
(CSC-314)
B.Sc. CSIT
Unit-6: Backtracking
Introduction
●
The term backtracking suggests that if the current
solution is not suitable, then backtrack and try other
solutions. Thus, recursion is used in this approach.
●
This approach is used to solve problems that have
multiple solutions. If you want an optimal solution, you
must go for dynamic programming.
●
Backtracking is an algorithmic technique where the goal
is to get all solutions to a problem using the brute force
approach.
●
It consists of building a set of all the solutions
incrementally. Since a problem would have constraints,
the solutions that fail to satisfy them will be removed.
●
Here,
●
Green is the start point, blue is the intermediate point, red are
points with no feasible solution, dark green is end solution.
●
Here, when the algorithm propagates to an end to check if it is a
solution or not, if it is then returns the solution otherwise
backtracks to the point one step behind it to find track to the
next point to find solution.
●
Problem: You want to find all the possible ways of
arranging 2 boys and 1 girl on 3 benches. Constraint:
Girl should not be on the middle of the bench.
?
Design and Analysis of Algorithms (CSC-314)
Unit-6: Backtracking
Example Backtracking Approach
●
Solution: There are a total of 3! = 6 possibilities. We
will try all the possibilities and get the possible solutions.
We recursively try all the possibilities.
●
All the possibilities are:
●
Subset sum problem is the problem of finding a subset
such that the sum of elements equal a given number.
●
The backtracking approach generates all permutations
in the worst case but in general, performs better than
the recursive approach towards subset sum problem.
●
A subset A of n positive integers and a value sum is
given, find whether or not there exists any subset of the
given set, the sum of whose elements is equal to the
given value of sum.
●
In 0/1 Knapsack Problem,
Statement: A thief has a bag or knapsack that can contain maximum weight W of his
loot. There are n items and the weight of ith item is Wi and it worth Vi . An amount of
item can be put into the bag is 0 or 1 i.e. x i is 0 or 1. Here the objective is to collect the
items that maximize the total profit earned.
Thank You!