Preetpal Kaur Buttar Assistant Professor Department of Computer Science and Engineering, Sant Longowal Institute of Engineering and Technology, Longowal
Preetpal Kaur Buttar PCCS-621 (Design and Analysis of Algorithms): Chapter 6 1
0-1 Knapsack Problem
Preetpal Kaur Buttar PCCS-621 (Design and Analysis of Algorithms): Chapter 6 2
Problem Scenario • A thief is robbing a store • Can carry a maximal weight W into his knapsack • There are n items available in the store • The weight of ith item is wi and its profit is pi. • The items should be picked in such a way that the thief will carry those items for which he will gain maximum profit.
Preetpal Kaur Buttar PCCS-621 (Design and Analysis of Algorithms): Chapter 6 3
0-1 Knapsack • 0-1 Property: The thief cannot break an item into fractions, he can either pick the item or leave the item • pro[0…n – 1]: An array storing the profits of n items • wt[0…n – 1]: An array storing weights of n items • W: Maximum capacity of the knapsack • Objective: • Find out a subset of items which maximizes profit • Subject to the constraint that sum of weights of the items in this subset <= W • Profits and weights are positive numbers.
Preetpal Kaur Buttar PCCS-621 (Design and Analysis of Algorithms): Chapter 6 4
A Simple Solution • Consider all the subsets of items • Calculate the total weight and profit of all the subsets • Consider only those subsets whose total weight is smaller than W • From all such subsets, pick the maximum profit subset
Preetpal Kaur Buttar PCCS-621 (Design and Analysis of Algorithms): Chapter 6 5
Optimal Substructure • To consider all the subsets of items, there can be 2 cases for every item: • the item is included in the optimal subset • the item is not included in the optimal subset • Therefore, the maximum value that can be obtained from n items is the maximum of the following 2 values: • Maximum value obtained from n – 1 items and weight W (excluding the nth item) • Profit of nth item plus the maximum value obtained by n – 1 items and a weight W – weight of nth item (including the nth item) • If the weight of nth item > W, then nth item cannot be included and case 1 is the only possibility
Preetpal Kaur Buttar PCCS-621 (Design and Analysis of Algorithms): Chapter 6 6
Overlapping Subproblems KNAPSACK(W, wt[0…n – 1], pro[0…n – 1], n): if n == 0 or W == 0: // Best case return 0 // If weight of nth item > W, this item can’t be included in optimal solution if wt[n – 1] > W: return knapsack(W, wt, pro, n – 1) // Return the maximum of 2 cases // nth item included //nth item excluded else: return max(pro[n – 1] + knapsack(W – wt[n – 1], wt, pro, n – 1), knapsack(W, wt, pro, n – 1)
Preetpal Kaur Buttar PCCS-621 (Design and Analysis of Algorithms): Chapter 6 7
Overlapping Subproblems • The recursive function computes the same problems again and again. • Time complexity: O(2n) • Since the subproblems are computed again and again, the 0-1 Knapsack problem has the overlapping subproblems property.
Preetpal Kaur Buttar PCCS-621 (Design and Analysis of Algorithms): Chapter 6 8