KEMBAR78
Knapsack Problem (DP & GREEDY) | PPTX
WELCOME
TO
OUR
PRESENTATION
COURSE TITLE: ALGORITHM
TOPIC: KNAPSACK PROBLEM
(DP & GREEDY)
CONTENTS
Introduction
Explanation & Example
Algorithm & Complexity
Application
Knapsack problem states that: Given a set of items,
each with a weight and a profit, determine the
number of each item to include in a collection so that
the total weight is less than or equal to a given limit
and the total profit is as large as possible.
Definition:
Version of knapsack:
There are two versions of knapsack problem:
1. 0/1 Knapsack Problem: Items are indivisible;
you either take them or not. And it is solved
using Dynamic Programming(DP).
2. Fractional Knapsack Problem: Items are
divisible; you can take any fraction of an item.
And it is solved using Greedy Algorithm.
Explanation:
Necklace
Weight: 1kg
Value: 3000tk
Camera
Weight: 2kg
Value: 1000tk
Laptop
Weight: 3kg
Value: 20000tk
Vase
Weight: 4kg
Value: 4000tk
Knapsack
Weight: 7kg
????
example of 0/1 knapsack:
DP approach to solve 0/1 knapsack problem:
 Identify the smaller sub-problems.
 Recursively define the value of an optimal solution in terms
of solutions to smaller problems.
Initial condition:
B[0,w]=0 for w>=0
B[i,0]=0 for i>=0
Recursive condition:
B[k,w]= B[k-1,w]; if w<wk
max{B[k-1,w], B[k-1, w-wk]+bk}; else
Example: Number of n=5, capacity m=6 & the weights and profits ar
given below:Weight(wk) 1 4 3 2 5
Profit(bk) 3 6 4 4 6
Now find the chosen item with maximum profit.
Solution:
Step 1: Identify the smaller sub-problems.
Step 2: Use the initial and recursive condition to find the maximum prof
Step 3: Use recursion to find the chosen item.
0 0 0 0 0 0 0
0 3 3 3 3 3 3
0 3 4 7 7 7 7
0 3 4 7 7 8 11
0 3 4 7 7 9 11
0 3 4 7 7 9 11
W 0 1 2 3 4 5 6i
0
1
2
3
4
5
(1,3)
(2,4)
(3,4)
(4,6)
(5,6)
Maximum
Profit
Ans : Maximum Profit=11. Item Taken :
Weight 1 2 3
Profit 3 4 4
Example of fractional knapsack:
Example: Number of items n=3, Capacity m=45
&
the weights and profits are given below:Weight (wi) 20 20 20
Profit (pi) 40 60 50
Now find out the fraction of chosen items with maximum p
Greedy approach to solve
Fractional knapsack problem:
Find the unit ui using the formula ui=pi/wi.
Find the fraction of the items xi that will be taken
in order to get maximum profit.
SOLUTION:
Step 1: Find the unit ui.
Step 2: Sort the item in descending order of ui.
Step 3: Find the maximum profit & the fraction xi of the items.
wi 20 20 20
pi 60 50 40
ui=pi/xi 3 2.5 2
xi 1 1 1/4
Therefore, maximum profit=(60*1)+(50*1)+40*1/4)=120.
Ans: Maximum Profit =120.
Taken fraction of the item:
Weight 20 20 20
Profit 60 50 40
Fraction 1 1 1/4
Algorithm& Complexityof 0/1 knapsack (DP):
Algorithm:
Knapsack_DP(n,W)
1. for w = 1 to m
2. do B[0,w] 0
3. for i = 1 to n
4. do B[i,0] 0
5. for w = 1 to m
6. do if (Wi < = w && B[i-1,w-Wi] +bk > B[i-1,w]
7. then B[i,w] B[i-1,w-Wi]+bk
8. else B[i,w] B[i-1,w]
Complexity:
Clearly the dynamic programming algorithm for the knapsack
problem has a time complexity of O(n.w) .
Where , n= the number of items &
w= the capacity of the knapsack.
Algorithm& Complexity of FRACTIONAL
knapsack(GREEDY):
Algorithm:
1. for i=1 to n
2. do x[i]=0.0
3. for i=1 to n
4. {
5. if(w[i]>m) then break
6. x[i]=1.0; m=m-w[i]
7. }
8. if(i<=n) then x[i]= m/w[i]
Complexity:
First sort according to profit to weight ratio time required : n log n
Then we need one for loop to find out maximum profit and for that
time needed is: n .
Overall Time complexity : n log n + n = O(n log n)
Application of knapsack:
Knapsack problems appear in real
world decision making processes in a
wide variety of fields, such as finding
the least wasteful way to cut raw
materials , selection of
investment and selection of assets.
ANY QUESTIONS ?
THANK YOU
EVERYONE

Knapsack Problem (DP & GREEDY)

  • 1.
  • 2.
    COURSE TITLE: ALGORITHM TOPIC:KNAPSACK PROBLEM (DP & GREEDY)
  • 3.
  • 4.
    Knapsack problem statesthat: Given a set of items, each with a weight and a profit, determine the number of each item to include in a collection so that the total weight is less than or equal to a given limit and the total profit is as large as possible. Definition:
  • 5.
    Version of knapsack: Thereare two versions of knapsack problem: 1. 0/1 Knapsack Problem: Items are indivisible; you either take them or not. And it is solved using Dynamic Programming(DP). 2. Fractional Knapsack Problem: Items are divisible; you can take any fraction of an item. And it is solved using Greedy Algorithm.
  • 6.
    Explanation: Necklace Weight: 1kg Value: 3000tk Camera Weight:2kg Value: 1000tk Laptop Weight: 3kg Value: 20000tk Vase Weight: 4kg Value: 4000tk Knapsack Weight: 7kg ????
  • 7.
    example of 0/1knapsack: DP approach to solve 0/1 knapsack problem:  Identify the smaller sub-problems.  Recursively define the value of an optimal solution in terms of solutions to smaller problems. Initial condition: B[0,w]=0 for w>=0 B[i,0]=0 for i>=0 Recursive condition: B[k,w]= B[k-1,w]; if w<wk max{B[k-1,w], B[k-1, w-wk]+bk}; else Example: Number of n=5, capacity m=6 & the weights and profits ar given below:Weight(wk) 1 4 3 2 5 Profit(bk) 3 6 4 4 6 Now find the chosen item with maximum profit.
  • 8.
    Solution: Step 1: Identifythe smaller sub-problems. Step 2: Use the initial and recursive condition to find the maximum prof Step 3: Use recursion to find the chosen item. 0 0 0 0 0 0 0 0 3 3 3 3 3 3 0 3 4 7 7 7 7 0 3 4 7 7 8 11 0 3 4 7 7 9 11 0 3 4 7 7 9 11 W 0 1 2 3 4 5 6i 0 1 2 3 4 5 (1,3) (2,4) (3,4) (4,6) (5,6) Maximum Profit Ans : Maximum Profit=11. Item Taken : Weight 1 2 3 Profit 3 4 4
  • 9.
    Example of fractionalknapsack: Example: Number of items n=3, Capacity m=45 & the weights and profits are given below:Weight (wi) 20 20 20 Profit (pi) 40 60 50 Now find out the fraction of chosen items with maximum p Greedy approach to solve Fractional knapsack problem: Find the unit ui using the formula ui=pi/wi. Find the fraction of the items xi that will be taken in order to get maximum profit.
  • 10.
    SOLUTION: Step 1: Findthe unit ui. Step 2: Sort the item in descending order of ui. Step 3: Find the maximum profit & the fraction xi of the items. wi 20 20 20 pi 60 50 40 ui=pi/xi 3 2.5 2 xi 1 1 1/4 Therefore, maximum profit=(60*1)+(50*1)+40*1/4)=120. Ans: Maximum Profit =120. Taken fraction of the item: Weight 20 20 20 Profit 60 50 40 Fraction 1 1 1/4
  • 11.
    Algorithm& Complexityof 0/1knapsack (DP): Algorithm: Knapsack_DP(n,W) 1. for w = 1 to m 2. do B[0,w] 0 3. for i = 1 to n 4. do B[i,0] 0 5. for w = 1 to m 6. do if (Wi < = w && B[i-1,w-Wi] +bk > B[i-1,w] 7. then B[i,w] B[i-1,w-Wi]+bk 8. else B[i,w] B[i-1,w] Complexity: Clearly the dynamic programming algorithm for the knapsack problem has a time complexity of O(n.w) . Where , n= the number of items & w= the capacity of the knapsack.
  • 12.
    Algorithm& Complexity ofFRACTIONAL knapsack(GREEDY): Algorithm: 1. for i=1 to n 2. do x[i]=0.0 3. for i=1 to n 4. { 5. if(w[i]>m) then break 6. x[i]=1.0; m=m-w[i] 7. } 8. if(i<=n) then x[i]= m/w[i] Complexity: First sort according to profit to weight ratio time required : n log n Then we need one for loop to find out maximum profit and for that time needed is: n . Overall Time complexity : n log n + n = O(n log n)
  • 13.
    Application of knapsack: Knapsackproblems appear in real world decision making processes in a wide variety of fields, such as finding the least wasteful way to cut raw materials , selection of investment and selection of assets.
  • 14.