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)

PPT
Knapsack problem
Vikas Sharma
 
PPT
0/1 knapsack
Amin Omi
 
PPTX
Knapsack
Karthik Chetla
 
PPTX
Fractional knapsack class 13
Kumar
 
PPTX
DAA-Floyd Warshall Algorithm.pptx
ArbabMaalik
 
PPTX
01 Knapsack using Dynamic Programming
Fenil Shah
 
PPT
Greedy Algorithm
Waqar Akram
 
PPTX
Dynamic Programming-Knapsack Problem
Amrita Yadav
 
PPTX
Job sequencing with Deadlines
YashiUpadhyay3
 
PPT
Lec 17 heap data structure
Sajid Marwat
 
PPT
Greedy Algorihm
Muhammad Amjad Rana
 
PPT
Knapsack problem using dynamic programming
khush_boo31
 
PDF
P, NP, NP-Complete, and NP-Hard
Animesh Chaturvedi
 
PPTX
The n Queen Problem
Sukrit Gupta
 
PPTX
0 1 knapsack using branch and bound
Abhishek Singh
 
PPTX
asymptotic notation
SangeethaSasi1
 
PPT
SINGLE-SOURCE SHORTEST PATHS
Md. Shafiuzzaman Hira
 
PPTX
Daa:Dynamic Programing
rupali_2bonde
 
PPT
Divide and conquer
Dr Shashikant Athawale
 
PPTX
Graph coloring using backtracking
shashidharPapishetty
 
Knapsack problem
Vikas Sharma
 
0/1 knapsack
Amin Omi
 
Knapsack
Karthik Chetla
 
Fractional knapsack class 13
Kumar
 
DAA-Floyd Warshall Algorithm.pptx
ArbabMaalik
 
01 Knapsack using Dynamic Programming
Fenil Shah
 
Greedy Algorithm
Waqar Akram
 
Dynamic Programming-Knapsack Problem
Amrita Yadav
 
Job sequencing with Deadlines
YashiUpadhyay3
 
Lec 17 heap data structure
Sajid Marwat
 
Greedy Algorihm
Muhammad Amjad Rana
 
Knapsack problem using dynamic programming
khush_boo31
 
P, NP, NP-Complete, and NP-Hard
Animesh Chaturvedi
 
The n Queen Problem
Sukrit Gupta
 
0 1 knapsack using branch and bound
Abhishek Singh
 
asymptotic notation
SangeethaSasi1
 
SINGLE-SOURCE SHORTEST PATHS
Md. Shafiuzzaman Hira
 
Daa:Dynamic Programing
rupali_2bonde
 
Divide and conquer
Dr Shashikant Athawale
 
Graph coloring using backtracking
shashidharPapishetty
 

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

PPTX
Fractional knapsack problem
Learning Courses Online
 
PPTX
5b.Greedy Technique - Fractional Knapsack+Coin change Problem.pptx
Suma Raj
 
PDF
Knapsack dp
Vivek Rathi
 
PPTX
Knapsack problem using Greedy method.pptx
Rajapriya82
 
PDF
12 Greeddy Method
Andres Mendez-Vazquez
 
PDF
Comparative analysis-of-dynamic-and-greedy-approaches-for-dynamic-programming
Editor IJMTER
 
PPTX
Knapsack Dynamic
Paras Patel
 
PPT
Design and analysis of Algorithms - Lecture 15.ppt
QurbanAli72
 
PPT
Knapsack problem and Memory Function
Barani Tharan
 
PPTX
Module 3_Greedy Technique_2021 Scheme.pptx
RITIKKUMAR168218
 
PPT
AOA ppt.ppt
SaimaShaheen14
 
PPTX
Knapsack problem
garishma bhatia
 
PPTX
ppt 2.pptxlmkgngxjgdjgdjtxjgxjgxnvndjcgxjxjjc
ArunavaGhosh36
 
PPT
DynProg_Knapsack.ppt
Ruchika Sinha
 
PPT
Lec37
Nikhil Chilwant
 
PPT
Elak3 need of greedy for design and analysis of algorithms.ppt
Elakkiya Rajasekar
 
PPTX
0 1 knapsack using naive recursive approach and top-down dynamic programming ...
Abhishek Singh
 
PDF
knapsackusingbranchandbound
hodcsencet
 
PPTX
Knapsack algoritm
Pritam Golder
 
PPTX
Knapsack problem
RacksaviR
 
Fractional knapsack problem
Learning Courses Online
 
5b.Greedy Technique - Fractional Knapsack+Coin change Problem.pptx
Suma Raj
 
Knapsack dp
Vivek Rathi
 
Knapsack problem using Greedy method.pptx
Rajapriya82
 
12 Greeddy Method
Andres Mendez-Vazquez
 
Comparative analysis-of-dynamic-and-greedy-approaches-for-dynamic-programming
Editor IJMTER
 
Knapsack Dynamic
Paras Patel
 
Design and analysis of Algorithms - Lecture 15.ppt
QurbanAli72
 
Knapsack problem and Memory Function
Barani Tharan
 
Module 3_Greedy Technique_2021 Scheme.pptx
RITIKKUMAR168218
 
AOA ppt.ppt
SaimaShaheen14
 
Knapsack problem
garishma bhatia
 
ppt 2.pptxlmkgngxjgdjgdjtxjgxjgxnvndjcgxjxjjc
ArunavaGhosh36
 
DynProg_Knapsack.ppt
Ruchika Sinha
 
Elak3 need of greedy for design and analysis of algorithms.ppt
Elakkiya Rajasekar
 
0 1 knapsack using naive recursive approach and top-down dynamic programming ...
Abhishek Singh
 
knapsackusingbranchandbound
hodcsencet
 
Knapsack algoritm
Pritam Golder
 
Knapsack problem
RacksaviR
 
Ad

Recently uploaded (17)

PDF
Leveraging the Power of Jira Dashboard.pdf
siddharthshukla742740
 
PPTX
presentation on legal and regulatory action
raoharsh4122001
 
PPTX
Great-Books. Powerpoint presentation. files
tamayocrisgie
 
PDF
Buy Verified Coinbase Accounts — The Ultimate Guide for 2025 (Rank #1 on Goog...
Buy Verified Cash App Accounts
 
PDF
Buy Verified Payoneer Accounts — The Ultimate Guide for 2025 (Rank #1 on Goog...
Buy Verified Cash App Accounts
 
PPTX
STURGEON BAY WI AG PPT JULY 6 2025.pptx
FamilyWorshipCenterD
 
PPTX
some leadership theories MBA management.pptx
rkseo19
 
PDF
The Impact of Game Live Streaming on In-Game Purchases of Chinese Young Game ...
Shibaura Institute of Technology
 
PDF
From Draft to DSN - How to Get your Paper In [DSN 2025 Doctoral Forum Keynote]
vschiavoni
 
PDF
The Family Secret (essence of loveliness)
Favour Biodun
 
PPTX
AI presentation for everyone in every fields
dodinhkhai1
 
PPTX
Inspired by VeinSense: Supercharge Your Hackathon with Agentic AI
ShubhamSharma2528
 
PDF
The Origin - A Simple Presentation on any project
RishabhDwivedi43
 
PPTX
BARRIERS TO EFFECTIVE COMMUNICATION.pptx
shraddham25
 
PPTX
2025-07-06 Abraham 06 (shared slides).pptx
Dale Wells
 
PPTX
Pastor Bob Stewart Acts 21 07 09 2025.pptx
FamilyWorshipCenterD
 
PPTX
Presentationexpressions You are student leader and have just come from a stud...
BENSTARBEATZ
 
Leveraging the Power of Jira Dashboard.pdf
siddharthshukla742740
 
presentation on legal and regulatory action
raoharsh4122001
 
Great-Books. Powerpoint presentation. files
tamayocrisgie
 
Buy Verified Coinbase Accounts — The Ultimate Guide for 2025 (Rank #1 on Goog...
Buy Verified Cash App Accounts
 
Buy Verified Payoneer Accounts — The Ultimate Guide for 2025 (Rank #1 on Goog...
Buy Verified Cash App Accounts
 
STURGEON BAY WI AG PPT JULY 6 2025.pptx
FamilyWorshipCenterD
 
some leadership theories MBA management.pptx
rkseo19
 
The Impact of Game Live Streaming on In-Game Purchases of Chinese Young Game ...
Shibaura Institute of Technology
 
From Draft to DSN - How to Get your Paper In [DSN 2025 Doctoral Forum Keynote]
vschiavoni
 
The Family Secret (essence of loveliness)
Favour Biodun
 
AI presentation for everyone in every fields
dodinhkhai1
 
Inspired by VeinSense: Supercharge Your Hackathon with Agentic AI
ShubhamSharma2528
 
The Origin - A Simple Presentation on any project
RishabhDwivedi43
 
BARRIERS TO EFFECTIVE COMMUNICATION.pptx
shraddham25
 
2025-07-06 Abraham 06 (shared slides).pptx
Dale Wells
 
Pastor Bob Stewart Acts 21 07 09 2025.pptx
FamilyWorshipCenterD
 
Presentationexpressions You are student leader and have just come from a stud...
BENSTARBEATZ
 
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