Module 5 Notes of Engineering
Module 5 Notes of Engineering
Backtracking
The General Method
Sudeep Manohar
Department of Information Science and Engineering
JNNCE, Shivamogga
Backtracking – The general method
• The name backtrack was first coined by D H Lehmer in 1950
• 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 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
Initial state
First component of the solution
Promising node
• For example, for S = {1, 2, 5, 6, 8} and d = 9, there are two solutions {1, 2, 6} and
{1, 8}
b
c f
d e e
c
e d f
Dead end Dead end d
f
Dead end a Solution
After sorting
item weight value value/weight
1 4 40 10
2 7 42 6
3 5 25 5
4 3 12 4
Sudeep Manohar, Dept. of ISE, JNNCE, Shivamogga
Sudeep Manohar, Dept. of ISE, JNNCE, Shivamogga
Todo
Find the maximum value of the subset of items for the given Knapsack
instance
Weights = (2, 1, 3, 2)
Values = (12, 10, 20, 15)
W=5
Item 1 2 3 4
Weights 2 4 6 9
Profits 10 10 12 18
Weights 4 5 8 10 12
Profits 20 15 16 20 16
Item 1 2 3 4
Weights 2 4 6 9
Profits 10 10 12 18
Weights 4 5 8 10
Profits 20 15 16 20
• If the result of every operation is uniquely defined then such algorithms are called
deterministic algorithms
• Algorithms which contain operations whose outcome is not uniquely defined are
called non deterministic algorithms
• Consider the following functions