SlideShare a Scribd company logo
6
Most read
7
Most read
9
Most read
DEPT. OF IT & MCA
knapsack problem using
greedy approach
Group no: 13
Padmesh agrekar : 04
Revati jalnekar : 24
Akshay kamble : 28
Sonal kumar : 52
Ameyaa vaidya: 57
Guided by:
Prof. Neelam Chandolikar
Content
• Introduction to greedy algorithm
• Knapsack problem
• Example of fractional knapsack
• Example of 0/1 knapsack
• Algorithm and complexity of fractional knapsack
• Algorithm and complexity of 0/1knapsack
• Application of knapsack
Introduction
 A greedy algorithm is a simple, intuitive algorithm that is used in optimization problems.
 The algorithm makes the optimal choice at each step as it attempts to find the overall optimal way to
solve the entire problem.
 Greedy algorithms are quite successful in some problems, such as Huffman encoding which is used
to compress data, or Dijkstra's algorithm , which is used to find the shortest path through a graph.
 Steps for achieving a Greedy Algorithm are:
1. Feasible
2. Local optimal
3. unalterable
 A greedy algorithm works if a problem exhibits the following two properties:
1. Greedy Choice Property
2. Optimal substructure
 Applications of Greedy
Greedy approach is used to solve many problems, such as
1. Finding the shortest path between two vertices using Dijkstra’s algorithm.
2. Finding the minimal spanning tree in a graph using Prim’s /Kruskal’s algorithm, etc.
Knapsack problem
 Definition:
 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
Versions 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.
Example of fractional knapsack
 Consider n=5 and m=100
V(p) 20 30 66 40 60
W 10 20 30 40 50
v/w 2.0 1.5 2.2 1.0 1.2
After sorting 2 3 1 5 4
Take the items in sorted manner:
w=30+10+20+40 out of 50
value=66+20+30+.8*60=164
Example of 0/1 knapsack
 0-1knapsack Consider n=5 and m=15
Take the items in sorted manner
w=7+6+2=15
value=28+22+discard+discard+2=52
V(p) 18 8 2 22 28
w 5 4 2 6 7
v/w 3.60 2 1 3.66 4
After sorting 3 4 5 2 1
Algorithm of fractional knapsack
• for i=1 to n
• {
• compute pi/wi;
• }
• sort objects in non- increasing order p/w;
• for i=1 to n
• {
• if(m>0 && wi<=m)
• m = m - wi;
• P = P + pi;
• else
• break;
• }
• if(m>0)
• {
• P=P+ pi*(m/wi);
• }
complexity
 The complexity of the algorithm:
• If using a simple sort algorithm (selection, bubble…) then the complexity of the whole problem is
O(n2).
• If using quick sort or merge sort then the complexity of the whole problem is O(nlogn).
Algorithm and complexity of 0/1 knapsack
 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.
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.
 In many cases of resource allocation along with some constraint, the problem can be derived in a
similar way of Knapsack problem. Following is a set of example.
• Finding the least wasteful way to cut raw materials
• portfolio optimization
• Cutting stock problems
References:
• https://www.tutorialspoint.com/design_and_analysis_of_algorithms/design_and_analysis_of_algorithms_greedy_method.htm#:~:text=Greedy
%20algorithms%20build%20a%20solution,it%20gives%20an%20immediate%20benefit.&text=Hence%2C%20we%20can%20say%20that,of%20fi
nding%20global%20optimal%20solution.
• https://www.hackerearth.com/practice/algorithms/greedy/basics-of-greedy-algorithms/tutorial/
Knapsack problem using greedy approach

More Related Content

What's hot (20)

PPT
SINGLE-SOURCE SHORTEST PATHS
Md. Shafiuzzaman Hira
 
PPTX
Graph coloring using backtracking
shashidharPapishetty
 
PPT
Branch and bound
Dr Shashikant Athawale
 
PPT
0/1 knapsack
Amin Omi
 
PPTX
DAA-Floyd Warshall Algorithm.pptx
ArbabMaalik
 
PPTX
heap Sort Algorithm
Lemia Algmri
 
PPT
DESIGN AND ANALYSIS OF ALGORITHMS
Gayathri Gaayu
 
PPTX
Job sequencing with Deadlines
YashiUpadhyay3
 
PPT
Sum of subsets problem by backtracking 
Hasanain Alshadoodee
 
PPTX
Quick sort
Afaq Mansoor Khan
 
PDF
Bit pair recoding
Basit Ali
 
PPTX
01 Knapsack using Dynamic Programming
Fenil Shah
 
PPTX
Daa:Dynamic Programing
rupali_2bonde
 
PPTX
Binary search
AparnaKumari31
 
PPTX
Unit 4-booth algorithm
vishal choudhary
 
PPTX
The n Queen Problem
Sukrit Gupta
 
PPT
Dinive conquer algorithm
Mohd Arif
 
PPTX
Dynamic programming
Yıldırım Tam
 
PPT
Intermediate code generation (Compiler Design)
Tasif Tanzim
 
PPTX
Unit 4 sp macro
Deepmala Sharma
 
SINGLE-SOURCE SHORTEST PATHS
Md. Shafiuzzaman Hira
 
Graph coloring using backtracking
shashidharPapishetty
 
Branch and bound
Dr Shashikant Athawale
 
0/1 knapsack
Amin Omi
 
DAA-Floyd Warshall Algorithm.pptx
ArbabMaalik
 
heap Sort Algorithm
Lemia Algmri
 
DESIGN AND ANALYSIS OF ALGORITHMS
Gayathri Gaayu
 
Job sequencing with Deadlines
YashiUpadhyay3
 
Sum of subsets problem by backtracking 
Hasanain Alshadoodee
 
Quick sort
Afaq Mansoor Khan
 
Bit pair recoding
Basit Ali
 
01 Knapsack using Dynamic Programming
Fenil Shah
 
Daa:Dynamic Programing
rupali_2bonde
 
Binary search
AparnaKumari31
 
Unit 4-booth algorithm
vishal choudhary
 
The n Queen Problem
Sukrit Gupta
 
Dinive conquer algorithm
Mohd Arif
 
Dynamic programming
Yıldırım Tam
 
Intermediate code generation (Compiler Design)
Tasif Tanzim
 
Unit 4 sp macro
Deepmala Sharma
 

Similar to Knapsack problem using greedy approach (20)

PDF
Comparative analysis-of-dynamic-and-greedy-approaches-for-dynamic-programming
Editor IJMTER
 
PPTX
ppt 2.pptxlmkgngxjgdjgdjtxjgxjgxnvndjcgxjxjjc
ArunavaGhosh36
 
PPTX
Fractional knapsack problem
Learning Courses Online
 
PPTX
5b.Greedy Technique - Fractional Knapsack+Coin change Problem.pptx
Suma Raj
 
PDF
Module 2 - Greedy Algorithm Data structures and algorithm
farzanirani201402
 
PPTX
Fractional knapsack class 13
Kumar
 
PPTX
Module 3_Greedy Technique_2021 Scheme.pptx
RITIKKUMAR168218
 
PDF
12 Greeddy Method
Andres Mendez-Vazquez
 
PDF
Unit 3 - Greedy Method
MaryJacob24
 
PDF
Unit 3 greedy method
MaryJacob24
 
PPTX
Knapsack problem using Greedy method.pptx
Rajapriya82
 
PPTX
Ms nikita greedy agorithm
Nikitagupta123
 
PPTX
Knapsack
Karthik Chetla
 
PPTX
Greedy algo revision 2
maamir farooq
 
PPTX
Unit 3- Greedy Method.pptx
MaryJacob24
 
PPTX
greedy algorithm Fractional Knapsack
Md. Musfiqur Rahman Foysal
 
PDF
Approximation alogrithms
Mohsen Fatemi
 
PPTX
Greedy Method unit-2(Design and analysis of algorithms).pptx
shivani366010
 
PPTX
Presentation on Greedy Algorithm
Salim Hosen
 
PPT
Design and analysis of Algorithms - Lecture 15.ppt
QurbanAli72
 
Comparative analysis-of-dynamic-and-greedy-approaches-for-dynamic-programming
Editor IJMTER
 
ppt 2.pptxlmkgngxjgdjgdjtxjgxjgxnvndjcgxjxjjc
ArunavaGhosh36
 
Fractional knapsack problem
Learning Courses Online
 
5b.Greedy Technique - Fractional Knapsack+Coin change Problem.pptx
Suma Raj
 
Module 2 - Greedy Algorithm Data structures and algorithm
farzanirani201402
 
Fractional knapsack class 13
Kumar
 
Module 3_Greedy Technique_2021 Scheme.pptx
RITIKKUMAR168218
 
12 Greeddy Method
Andres Mendez-Vazquez
 
Unit 3 - Greedy Method
MaryJacob24
 
Unit 3 greedy method
MaryJacob24
 
Knapsack problem using Greedy method.pptx
Rajapriya82
 
Ms nikita greedy agorithm
Nikitagupta123
 
Knapsack
Karthik Chetla
 
Greedy algo revision 2
maamir farooq
 
Unit 3- Greedy Method.pptx
MaryJacob24
 
greedy algorithm Fractional Knapsack
Md. Musfiqur Rahman Foysal
 
Approximation alogrithms
Mohsen Fatemi
 
Greedy Method unit-2(Design and analysis of algorithms).pptx
shivani366010
 
Presentation on Greedy Algorithm
Salim Hosen
 
Design and analysis of Algorithms - Lecture 15.ppt
QurbanAli72
 
Ad

Recently uploaded (20)

PDF
Avatar for apidays apidays PRO June 07, 2025 0 5 apidays Helsinki & North 2...
apidays
 
PPTX
Aict presentation on dpplppp sjdhfh.pptx
vabaso5932
 
PDF
apidays Helsinki & North 2025 - API-Powered Journeys: Mobility in an API-Driv...
apidays
 
PDF
What does good look like - CRAP Brighton 8 July 2025
Jan Kierzyk
 
PDF
NIS2 Compliance for MSPs: Roadmap, Benefits & Cybersecurity Trends (2025 Guide)
GRC Kompas
 
PPTX
apidays Munich 2025 - Building an AWS Serverless Application with Terraform, ...
apidays
 
PDF
apidays Singapore 2025 - Trustworthy Generative AI: The Role of Observability...
apidays
 
PPTX
apidays Helsinki & North 2025 - API access control strategies beyond JWT bear...
apidays
 
PDF
apidays Helsinki & North 2025 - APIs in the healthcare sector: hospitals inte...
apidays
 
PDF
apidays Singapore 2025 - How APIs can make - or break - trust in your AI by S...
apidays
 
PPT
tuberculosiship-2106031cyyfuftufufufivifviviv
AkshaiRam
 
PDF
OOPs with Java_unit2.pdf. sarthak bookkk
Sarthak964187
 
PPTX
Exploring Multilingual Embeddings for Italian Semantic Search: A Pretrained a...
Sease
 
PPTX
apidays Singapore 2025 - The Quest for the Greenest LLM , Jean Philippe Ehre...
apidays
 
PPTX
apidays Munich 2025 - Building Telco-Aware Apps with Open Gateway APIs, Subhr...
apidays
 
PDF
Product Management in HealthTech (Case Studies from SnappDoctor)
Hamed Shams
 
PPTX
ER_Model_with_Diagrams_Presentation.pptx
dharaadhvaryu1992
 
PDF
Using AI/ML for Space Biology Research
VICTOR MAESTRE RAMIREZ
 
PPTX
b6057ea5-8e8c-4415-90c0-ed8e9666ffcd.pptx
Anees487379
 
PDF
apidays Singapore 2025 - Building a Federated Future, Alex Szomora (GSMA)
apidays
 
Avatar for apidays apidays PRO June 07, 2025 0 5 apidays Helsinki & North 2...
apidays
 
Aict presentation on dpplppp sjdhfh.pptx
vabaso5932
 
apidays Helsinki & North 2025 - API-Powered Journeys: Mobility in an API-Driv...
apidays
 
What does good look like - CRAP Brighton 8 July 2025
Jan Kierzyk
 
NIS2 Compliance for MSPs: Roadmap, Benefits & Cybersecurity Trends (2025 Guide)
GRC Kompas
 
apidays Munich 2025 - Building an AWS Serverless Application with Terraform, ...
apidays
 
apidays Singapore 2025 - Trustworthy Generative AI: The Role of Observability...
apidays
 
apidays Helsinki & North 2025 - API access control strategies beyond JWT bear...
apidays
 
apidays Helsinki & North 2025 - APIs in the healthcare sector: hospitals inte...
apidays
 
apidays Singapore 2025 - How APIs can make - or break - trust in your AI by S...
apidays
 
tuberculosiship-2106031cyyfuftufufufivifviviv
AkshaiRam
 
OOPs with Java_unit2.pdf. sarthak bookkk
Sarthak964187
 
Exploring Multilingual Embeddings for Italian Semantic Search: A Pretrained a...
Sease
 
apidays Singapore 2025 - The Quest for the Greenest LLM , Jean Philippe Ehre...
apidays
 
apidays Munich 2025 - Building Telco-Aware Apps with Open Gateway APIs, Subhr...
apidays
 
Product Management in HealthTech (Case Studies from SnappDoctor)
Hamed Shams
 
ER_Model_with_Diagrams_Presentation.pptx
dharaadhvaryu1992
 
Using AI/ML for Space Biology Research
VICTOR MAESTRE RAMIREZ
 
b6057ea5-8e8c-4415-90c0-ed8e9666ffcd.pptx
Anees487379
 
apidays Singapore 2025 - Building a Federated Future, Alex Szomora (GSMA)
apidays
 
Ad

Knapsack problem using greedy approach

  • 1. DEPT. OF IT & MCA knapsack problem using greedy approach Group no: 13 Padmesh agrekar : 04 Revati jalnekar : 24 Akshay kamble : 28 Sonal kumar : 52 Ameyaa vaidya: 57 Guided by: Prof. Neelam Chandolikar
  • 2. Content • Introduction to greedy algorithm • Knapsack problem • Example of fractional knapsack • Example of 0/1 knapsack • Algorithm and complexity of fractional knapsack • Algorithm and complexity of 0/1knapsack • Application of knapsack
  • 3. Introduction  A greedy algorithm is a simple, intuitive algorithm that is used in optimization problems.  The algorithm makes the optimal choice at each step as it attempts to find the overall optimal way to solve the entire problem.  Greedy algorithms are quite successful in some problems, such as Huffman encoding which is used to compress data, or Dijkstra's algorithm , which is used to find the shortest path through a graph.  Steps for achieving a Greedy Algorithm are: 1. Feasible 2. Local optimal 3. unalterable
  • 4.  A greedy algorithm works if a problem exhibits the following two properties: 1. Greedy Choice Property 2. Optimal substructure  Applications of Greedy Greedy approach is used to solve many problems, such as 1. Finding the shortest path between two vertices using Dijkstra’s algorithm. 2. Finding the minimal spanning tree in a graph using Prim’s /Kruskal’s algorithm, etc.
  • 5. Knapsack problem  Definition:  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
  • 6. Versions 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.
  • 7. Example of fractional knapsack  Consider n=5 and m=100 V(p) 20 30 66 40 60 W 10 20 30 40 50 v/w 2.0 1.5 2.2 1.0 1.2 After sorting 2 3 1 5 4 Take the items in sorted manner: w=30+10+20+40 out of 50 value=66+20+30+.8*60=164
  • 8. Example of 0/1 knapsack  0-1knapsack Consider n=5 and m=15 Take the items in sorted manner w=7+6+2=15 value=28+22+discard+discard+2=52 V(p) 18 8 2 22 28 w 5 4 2 6 7 v/w 3.60 2 1 3.66 4 After sorting 3 4 5 2 1
  • 9. Algorithm of fractional knapsack • for i=1 to n • { • compute pi/wi; • } • sort objects in non- increasing order p/w; • for i=1 to n • { • if(m>0 && wi<=m) • m = m - wi; • P = P + pi; • else • break; • } • if(m>0) • { • P=P+ pi*(m/wi); • }
  • 10. complexity  The complexity of the algorithm: • If using a simple sort algorithm (selection, bubble…) then the complexity of the whole problem is O(n2). • If using quick sort or merge sort then the complexity of the whole problem is O(nlogn).
  • 11. Algorithm and complexity of 0/1 knapsack  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. 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.  In many cases of resource allocation along with some constraint, the problem can be derived in a similar way of Knapsack problem. Following is a set of example. • Finding the least wasteful way to cut raw materials • portfolio optimization • Cutting stock problems