SlideShare a Scribd company logo
7
Most read
8
Most read
11
Most read
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

More Related Content

What's hot (20)

PPTX
knapsack problem
Adnan Malak
 
PPTX
Greedy Algorithm - Knapsack Problem
Madhu Bala
 
PPTX
Fractional knapsack class 13
Kumar
 
PPT
Greedy algorithms
Rajendran
 
PPTX
Dynamic Programming-Knapsack Problem
Amrita Yadav
 
PPTX
Greedy algorithms
sandeep54552
 
PPT
Recursion tree method
Rajendran
 
PPT
Knapsack problem
Vikas Sharma
 
PPTX
01 Knapsack using Dynamic Programming
Fenil Shah
 
DOCX
0-1 KNAPSACK PROBLEM
i i
 
PDF
Closest pair problems (Divide and Conquer)
Gem WeBlog
 
PDF
A greedy algorithms
Amit Kumar Rathi
 
PPT
Knapsack problem using dynamic programming
khush_boo31
 
PPTX
0/1 DYNAMIC PROGRAMMING KNAPSACK PROBLEM
Mrunal Patil
 
PPTX
greedy algorithm Fractional Knapsack
Md. Musfiqur Rahman Foysal
 
PPTX
Hamiltonian path
Arindam Ghosh
 
PPT
0/1 knapsack
Amin Omi
 
PPTX
Quick sort
amar kakde
 
PPTX
N queens using backtracking
srilekhagourishetty
 
PPT
Asymptotic notation
Dr Shashikant Athawale
 
knapsack problem
Adnan Malak
 
Greedy Algorithm - Knapsack Problem
Madhu Bala
 
Fractional knapsack class 13
Kumar
 
Greedy algorithms
Rajendran
 
Dynamic Programming-Knapsack Problem
Amrita Yadav
 
Greedy algorithms
sandeep54552
 
Recursion tree method
Rajendran
 
Knapsack problem
Vikas Sharma
 
01 Knapsack using Dynamic Programming
Fenil Shah
 
0-1 KNAPSACK PROBLEM
i i
 
Closest pair problems (Divide and Conquer)
Gem WeBlog
 
A greedy algorithms
Amit Kumar Rathi
 
Knapsack problem using dynamic programming
khush_boo31
 
0/1 DYNAMIC PROGRAMMING KNAPSACK PROBLEM
Mrunal Patil
 
greedy algorithm Fractional Knapsack
Md. Musfiqur Rahman Foysal
 
Hamiltonian path
Arindam Ghosh
 
0/1 knapsack
Amin Omi
 
Quick sort
amar kakde
 
N queens using backtracking
srilekhagourishetty
 
Asymptotic notation
Dr Shashikant Athawale
 

Similar to Knapsack Problem (DP & GREEDY) (20)

PPT
Knapsack problem and Memory Function
Barani Tharan
 
PPTX
Fractional Knapsack Problem
harsh kothari
 
PPTX
Knapsack Dynamic
Paras Patel
 
PPTX
5b.Greedy Technique - Fractional Knapsack+Coin change Problem.pptx
Suma Raj
 
PPTX
Greedy+Day-3.pptx
Akswant
 
PDF
12 Greeddy Method
Andres Mendez-Vazquez
 
PPTX
0 1 knapsack using branch and bound
Abhishek Singh
 
PDF
knapsackusingbranchandbound
hodcsencet
 
PPT
Knapsack prooblem using greedy based algoithm
RICHARDACHARYA
 
PPT
Elak1 greedy presentation for design and analysis of algorithms.ppt
Elakkiya Rajasekar
 
PPT
Design and analysis of Algorithms - Lecture 15.ppt
QurbanAli72
 
PPTX
Module 3_DAA (2).pptx
AnkitaVerma776806
 
PPT
AOA ppt.ppt
SaimaShaheen14
 
PDF
Comparative analysis-of-dynamic-and-greedy-approaches-for-dynamic-programming
Editor IJMTER
 
PPTX
Knapsack problem
garishma bhatia
 
PPTX
Knapsack problem
RacksaviR
 
PPTX
Greedy algo revision 2
maamir farooq
 
PPT
DynProg_Knapsack.ppt
Ruchika Sinha
 
PPT
lecture 25
sajinsc
 
PDF
Brief journey in the big world of Integer Programming with the Knapsack
🌳 Olivier Pirson — OPi 🇧🇪🇫🇷🇬🇧 🐧 👨‍💻 👨‍🔬
 
Knapsack problem and Memory Function
Barani Tharan
 
Fractional Knapsack Problem
harsh kothari
 
Knapsack Dynamic
Paras Patel
 
5b.Greedy Technique - Fractional Knapsack+Coin change Problem.pptx
Suma Raj
 
Greedy+Day-3.pptx
Akswant
 
12 Greeddy Method
Andres Mendez-Vazquez
 
0 1 knapsack using branch and bound
Abhishek Singh
 
knapsackusingbranchandbound
hodcsencet
 
Knapsack prooblem using greedy based algoithm
RICHARDACHARYA
 
Elak1 greedy presentation for design and analysis of algorithms.ppt
Elakkiya Rajasekar
 
Design and analysis of Algorithms - Lecture 15.ppt
QurbanAli72
 
Module 3_DAA (2).pptx
AnkitaVerma776806
 
AOA ppt.ppt
SaimaShaheen14
 
Comparative analysis-of-dynamic-and-greedy-approaches-for-dynamic-programming
Editor IJMTER
 
Knapsack problem
garishma bhatia
 
Knapsack problem
RacksaviR
 
Greedy algo revision 2
maamir farooq
 
DynProg_Knapsack.ppt
Ruchika Sinha
 
lecture 25
sajinsc
 
Brief journey in the big world of Integer Programming with the Knapsack
🌳 Olivier Pirson — OPi 🇧🇪🇫🇷🇬🇧 🐧 👨‍💻 👨‍🔬
 
Ad

Recently uploaded (20)

PPTX
Great-Books. Powerpoint presentation. files
tamayocrisgie
 
PPTX
Melbourne_Keynote_June_19_2013_without_photos.pptx
BryInfanteRayos
 
PPTX
Unit 1, 2 & 3 - Pharmacognosy - Defn_history_scope.pptx
bagewadivarsha2024
 
PPTX
Lesson 1-3(Learners' copy).pptxucspctopi
KrizeAnneCorneja
 
PDF
The Origin - A Simple Presentation on any project
RishabhDwivedi43
 
PDF
Planning the parliament of the future in greece – considerations for a data-d...
Dr. Fotios Fitsilis
 
PDF
Model Project Report_36DR_G&P.pdf for investors understanding
MeetAgrawal23
 
DOCX
How Digital Marketplaces are Empowering Emerging MedTech Brands
Ram Gopal Varma
 
PDF
Jotform Presentation Agents: Use Cases and Examples
Jotform
 
PDF
From Draft to DSN - How to Get your Paper In [DSN 2025 Doctoral Forum Keynote]
vschiavoni
 
PDF
Buy Verified Payoneer Accounts — The Ultimate Guide for 2025 (Rank #1 on Goog...
Buy Verified Cash App Accounts
 
PPTX
presentation on legal and regulatory action
raoharsh4122001
 
PPTX
INTRO-TO-EMPOWERMENT-TECHNOLGY grade 11 lesson
ReyAcosta8
 
PPTX
STURGEON BAY WI AG PPT JULY 6 2025.pptx
FamilyWorshipCenterD
 
PDF
The Impact of Game Live Streaming on In-Game Purchases of Chinese Young Game ...
Shibaura Institute of Technology
 
PDF
Buy Verified Coinbase Accounts — The Ultimate Guide for 2025 (Rank #1 on Goog...
Buy Verified Cash App Accounts
 
PDF
Jotform Presentation Agents: Features and Benefits
Jotform
 
PDF
The Family Secret (essence of loveliness)
Favour Biodun
 
DOC
STABILITY INDICATING METHOD DEVELOPMENT AND VALIDATION FOR SIMULTANEOUS ESTIM...
jmkeans624
 
PDF
Committee-Skills-Handbook---MUNprep.org.pdf
SatvikAgarwal9
 
Great-Books. Powerpoint presentation. files
tamayocrisgie
 
Melbourne_Keynote_June_19_2013_without_photos.pptx
BryInfanteRayos
 
Unit 1, 2 & 3 - Pharmacognosy - Defn_history_scope.pptx
bagewadivarsha2024
 
Lesson 1-3(Learners' copy).pptxucspctopi
KrizeAnneCorneja
 
The Origin - A Simple Presentation on any project
RishabhDwivedi43
 
Planning the parliament of the future in greece – considerations for a data-d...
Dr. Fotios Fitsilis
 
Model Project Report_36DR_G&P.pdf for investors understanding
MeetAgrawal23
 
How Digital Marketplaces are Empowering Emerging MedTech Brands
Ram Gopal Varma
 
Jotform Presentation Agents: Use Cases and Examples
Jotform
 
From Draft to DSN - How to Get your Paper In [DSN 2025 Doctoral Forum Keynote]
vschiavoni
 
Buy Verified Payoneer Accounts — The Ultimate Guide for 2025 (Rank #1 on Goog...
Buy Verified Cash App Accounts
 
presentation on legal and regulatory action
raoharsh4122001
 
INTRO-TO-EMPOWERMENT-TECHNOLGY grade 11 lesson
ReyAcosta8
 
STURGEON BAY WI AG PPT JULY 6 2025.pptx
FamilyWorshipCenterD
 
The Impact of Game Live Streaming on In-Game Purchases of Chinese Young Game ...
Shibaura Institute of Technology
 
Buy Verified Coinbase Accounts — The Ultimate Guide for 2025 (Rank #1 on Goog...
Buy Verified Cash App Accounts
 
Jotform Presentation Agents: Features and Benefits
Jotform
 
The Family Secret (essence of loveliness)
Favour Biodun
 
STABILITY INDICATING METHOD DEVELOPMENT AND VALIDATION FOR SIMULTANEOUS ESTIM...
jmkeans624
 
Committee-Skills-Handbook---MUNprep.org.pdf
SatvikAgarwal9
 
Ad

Knapsack Problem (DP & GREEDY)

  • 2. COURSE TITLE: ALGORITHM TOPIC: KNAPSACK PROBLEM (DP & GREEDY)
  • 4. 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:
  • 5. 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.
  • 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/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.
  • 8. 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
  • 9. 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.
  • 10. 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
  • 11. 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.
  • 12. 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)
  • 13. 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.
  • 14. ANY QUESTIONS ? THANK YOU EVERYONE