Design and Analysis of
Algorithm
Muhammad Umer Mehmood
Knapsack Problem with dynamic programming
Introduction
• The Knapsack Problem is a famous Dynamic Programming Problem
that falls in the optimization category.
• It derives its name from a scenario where, given a set of items with
specific weights and assigned values, the goal is to maximize
the value in a knapsack while remaining within the weight constraint.
Introduction
• Example:
• The value in the Bear’s Knapsack can be maximized by adding Honey
Comb 2 and 3 to it.
• Total Weight = 150gms + 350gms = 500gms
• Total Value = 150gms + 450gms = 600gms
Introduction
• Example 2:
• Marry wants to carry some fruits in her knapsack and maximize the
profit she makes. She should pick them such that she minimizes
weight ( <= bag’s capacity) and maximize the value.
Introduction
Items: 𝐴𝑝𝑝𝑙𝑒, 𝑂𝑟𝑎𝑛𝑔𝑒, 𝐵𝑎𝑛𝑎𝑛𝑎, 𝑀𝑒𝑙𝑜𝑛
Weights: 2,3,1,4
Profits: 5,4,3,7
Knapsack Capacity: 5
Fruits Pick by Marry:
Fractional Knapsack Problem
An efficient solution to use the Greedy Approach.
The basic idea of the greedy approach is to calculate the
ratio profit/weight for each item and sort the item on the basis of this
ratio. Then take the item with the highest ratio and add as much as we
can (can be the whole element or a fraction of it).
Fractional Knapsack Problem
Let’s consider an example:
Objects (O): 1, 2, 3, 4, 5, 6, 7
Profit P : 10, 5, 15, 7, 6, 18, 3
Weight (W): 2, 3, 5, 7, 1, 4, 1
Knapsack Capacity m : 15
Fractional Knapsack Problem
O 1 2 3 4 5 6 7
P 10 5 15 7 6 18 3
W 2 3 5 7 1 4 1
𝑷Τ𝑾 5 1.6 3 1 6 4.5 3
𝑋 = 𝑋1 , 𝑋2 , 𝑋3 , 𝑋4 , 𝑋5 , 𝑋6 , 𝑋7
0≤X≤1
Fractional Knapsack Problem
O 1 2 3 4 5 6 7
P 10 5 15 7 6 18 3
W 2 3 5 7 1 4 1
𝑷Τ𝑾 5 1.6 3 1 6 4.5 3
15 – 01 = 14
14 – 02 = 12
12 – 04 = 08 𝑋 = 𝑋1 , 𝑋2 , 𝑋3 , 𝑋4 , 𝑋5 , 𝑋6 , 𝑋7
15 Kg 1 2 1 0 1 1 1
08 – 05 = 03
3
03 – 01 = 02
02 – 02 = 00
Fractional Knapsack Problem
O 1 2 3 4 5 6 7
P 10 5 15 7 6 18 3
W 2 3 5 7 1 4 1
𝑷Τ𝑾 5 1.6 3 1 6 4.5 3
𝑋 = 𝑋1 , 𝑋2 , 𝑋3 , 𝑋4 , 𝑋5 , 𝑋6 , 𝑋7
1 2 1 0 1 1 1
3
2
𝑋𝑖 𝑊𝑖 = 1 ∗ 2 + ∗ 3 + 1 ∗ 5 + 0 ∗ 7 + 1 ∗ 1 + 1 ∗ 4 + 1 ∗ 1
3
=2+2+5+0+1+4+1
= 15
Fractional Knapsack Problem
O 1 2 3 4 5 6 7
P 10 5 15 7 6 18 3
W 2 3 5 7 1 4 1
𝑷Τ𝑾 5 1.6 3 1 6 4.5 3
𝑋 = 𝑋1 , 𝑋2 , 𝑋3 , 𝑋4 , 𝑋5 , 𝑋6 , 𝑋7
1 2 1 0 1 1 1
3
2
𝑋𝑖 𝑃𝑖 = 1 ∗ 10 + ∗ 5 + 1 ∗ 15 + 0 ∗ 7 + 1 ∗ 6 + 1 ∗ 18 + 1 ∗ 3
3
= 10 + 3.33 + 15 + 0 + 6 + 18 + 3
= 55.33
Fractional Knapsack Problem
O 1 2 3 4 5 6 7
P 10 5 15 7 6 18 3
W 2 3 5 7 1 4 1
𝑷Τ𝑾 5 1.6 3 1 6 4.5 3
Constraint: Objective:
𝑋𝑖 𝑊𝑖 ≤ 𝑚 𝑚𝑎𝑥 𝑋𝑖 𝑃𝑖
0/1 Knapsack Problem
The 0/1 Knapsack Problem is solved by dynamic programming
The idea of the 0/1 Knapsack problem means items are either
completely or no items are filled in a Knapsack.
0/1 Knapsack Problem
Let’s consider an example:
Objects (O): 1, 2, 3, 4
Profit P : 1, 2, 5, 6,
Weight (W): 2, 3, 4, 5
Knapsack Capacity m : 8
0/1 Knapsack Problem Knapsack Capacity (m)
0 1 2 3 4 5 6 7 8
P W O
1 2 1
2 3 2
5 4 3
6 5 4
Tabulation Method
0/1 Knapsack Problem Knapsack Capacity (m)
0 1 2 3 4 5 6 7 8
P W O 0 0 0 0 0 0 0 0 0
1 2 1 0
2 3 2 0
5 4 3
0
6 5 4 0
0/1 Knapsack Problem Knapsack Capacity (m)
0 1 2 3 4 5 6 7 8
P W O 0 0 0 0 0 0 0 0 0
1 2 1 0 0 1 1 1 1 1 1
1
0
0
0
0/1 Knapsack Problem Knapsack Capacity (m)
0 1 2 3 4 5 6 7 8
P W O 0 0 0 0 0 0 0 0 0
1 2 1 0 0 1 1 1 1 1 1
1
2 3 2 0 0 1 2 2 3 3 3 3
0
0
0/1 Knapsack Problem Knapsack Capacity (m)
0 1 2 3 4 5 6 7 8
P W O 0 0 0 0 0 0 0 0 0
1 2 1 0 0 1 1 1 1 1 1
1
2 3 2 0 0 1 2 2 3 3 3 3
5 4 3 0 0 1 2 5 5 6 7 7
0
0/1 Knapsack Problem Knapsack Capacity (m)
0 1 2 3 4 5 6 7 8
P W O 0 0 0 0 0 0 0 0 0
1 2 1 0 0 1 1 1 1 1 1
1
2 3 2 0 0 1 2 2 3 3 3 3
5 4 3 0 0 1 2 5 5 6 7 7
6 5 4 0 0 1 2 5
V 𝑖, 𝑤 = 𝑚𝑎𝑥 𝑉 𝑖 − 1, 𝑤 , 𝑉 𝑖 − 1, 𝑤 − 𝑤 𝑖 + 𝑃 𝑖
V 4,1 = 𝑚𝑎𝑥 𝑉 4 − 1, 1 , 𝑉 4 − 1,1 − 5 + 6
V 4,1 = 𝑚𝑎𝑥 𝑉 3, 1 , 𝑉 3, −4 + 6
0/1 Knapsack Problem Knapsack Capacity (m)
0 1 2 3 4 5 6 7 8
P W O 0 0 0 0 0 0 0 0 0
1 2 1 0 0 1 1 1 1 1 1
1
2 3 2 0 0 1 2 2 3 3 3 3
5 4 3 0 0 1 2 5 5 6 7 7
6 5 4 0 0 1 2 5 6
V 4,5 = 𝑚𝑎𝑥 𝑉 4 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 7 , 2 + 6
V 4,8 = 𝑚𝑎𝑥 7 , 8 = 8
0/1 Knapsack Problem Knapsack Capacity (m)
0 1 2 3 4 5 6 7 8
P W O 0 0 0 0 0 0 0 0 0
1 2 1 0 0 1 1 1 1 1 1
1
2 3 2 0 0 1 2 2 3 3 3 3
5 4 3 0 0 1 2 5 5 6 7 7
6 5 4 0 0 1 2 5 6 6 7 8
𝑋1 , 𝑋2 , 𝑋3 , 𝑋4 8–6=2
0 1 0 1 2–2=0
0/1 Knapsack Problem
Sets Method
Let’s consider an example:
Objects (O): 1, 2, 3, 4
Profit P : 1, 2, 5, 6,
Weight (W): 2, 3, 4, 5
Knapsack Capacity m : 8
0/1 Knapsack Problem
Prepare the set of Order Pair of Profit and Weight (P, W)
• 𝑆 0 = 0,0 , 𝑆10 = 1,2
• 𝑆1 = 0,0 , 1,2 , 𝑆11 = 2,3 , 3,5
• 𝑆2 = 0,0 , 1,2 , 2,3 , 3,5 , 𝑆12 = 5,4 , 6,6 , 7,7 , 8,9
• 𝑆 3 = 0,0 , 1,2 , 2,3 , 3,5 , 5,4 , 6,6 , 7,7 , 8,9 , 𝑆13 =
5,4 , 6,6 , 7,7 , 8,9
0/1 Knapsack Problem
• 𝑆2 = 0,0 , 1,2 , 2,3 , 3,5 , 𝑆12 = 5,4 , 6,6 , 7,7
• 𝑆3 = 0,0 , 1,2 , 2,3 , 3,5 , 5,4 , 6,6 , 7,7 ,
• 𝑆 3 = 0,0 , 1,2 , 2,3 , 5,4 , 6,6 , 7,7 , 𝑆13 =
6,5 , 7,7 , 8,8 , 11,9 , 12,11 , 13,12
• 𝑆4 =
0,0 , 1,2 , 2,3 , 5,4 , 6,6 , 7,7 6,5 , 7,7 , 8,8 , 11,9 , 12,11 , 13,12
• 𝑆4 = 0,0 , 1,2 , 2,3 , 5,4 , 6,6 , 7,7 8,8