You are on page 1of 11

Dynamic Programming

PCCS-621 (Design and Analysis of Algorithms)


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


Solving 0-1 Knapsack Problem Using DP
Index wt profit wt→ 0 1 2 3 4 5 6 7
profit↓
0 1 1
0 (0)
1 4 4
1 (1)
2 5 5
2 (4)
3 7 7
3 (5)
4 (7)

• W=7 • Two choices:


• Boundary cases: • Exclude i
• No items, W > 0 • DP[i][j] = DP[i – 1][j]
• Include i
• W=0
• DP[i][j] = pro[i – 1] + dp[i – 1][j –
wt[i – 1]]
• DP[i][j] = max(include i, exclude i)
Preetpal Kaur Buttar PCCS-621 (Design and Analysis of Algorithms): Chapter 6 9
Algorithm
KNAPSACK(W, wt[0…n – 1], pro[0…n – 1], n):
dp[n+1][W+1]
for i in 0 to n:
for j in 0 to W:
if i == 0 or j == 0:
dp[i][j] = 0
elif wt[i – 1] > j:
dp[i][j] = dp[i – 1][j]
else:
dp[i][j] = max(pro[i – 1] + dp[i – 1][j – wt[i – 1]], dp[i – 1][j]
return dp[n][W]

Preetpal Kaur Buttar PCCS-621 (Design and Analysis of Algorithms): Chapter 6 10


Complexity Analysis
• Runtime: O(n*W)

Preetpal Kaur Buttar PCCS-621 (Design and Analysis of Algorithms): Chapter 6 11

You might also like