SlideShare a Scribd company logo
1 of 9
KNAPSACK
PROBLEM ALGORIT
HM
Department of Masters Of Computer Science(MCA)
Central university of Karnataka
Presented by: Prajjaval Rao Chintal
2018MCA09
WHAT ?
• GIVEN WEIGHTS AND VALUES OF N ITEMS, WE NEED TO PUT THESE ITEMS IN A
KNAPSACK OF CAPACITY W TO GET THE MAXIMUM TOTAL VALUE IN THE
KNAPSACK.
TYPES
• 0-1 KNAPSACK PROBLEM
In the 0-1 knapsack problem, we are not allowed to break items. We either
take the whole item or don’t take it.
• FRACTIONAL KNAPSACK
In fractional knapsack, we can break items for maximizing the total value
of knapsack. This problem in which we can break an item is also called the
fractional knapsack problem.
THE OPTIMAL KNAPSACK ALGORITHM
EXAMPLE
0-1 KNAPSACK
Take B and C
Total weight =20+30=50
Total value
=100+120=220
FRACTIONAL KNAPSACK
Take A,B and 2/3rd of C
Total weight =10+20+(30* 2/3)=50
Total value =60+100+(120* 2/3)=240
GREEDY APPROACH
• The basic idea of the greedy approach is to calculate the ratio value/weight for
each item.
• Sort the item on basis of this ratio.
• Then take the item with the highest ratio and add them until we can’t add the
next item as a whole.
• At the end add the next item as much (fraction) as we can.
GREEDY APPROACH SOLUTION
Ratio=
6
Ratio=
5
Ratio=
4
1. calculate the ratio
value/weight.
2. Sort (descending) item on basis
of ratio.
3. Take item with highest ratio
and add to knapsack until we
cant add the next item as
whole.
4. At the end add the next item
Capacity left =40
Value = 60
Capacity left =20
Value = 160
Take 2/3rd of C
Weight = 2/3 *30 =20
Value = 2/3 *120 =80
Capacity left =0
Value = 240
THE OPTIMAL KNAPSACK ALGORITHM
• INPUT: AN INTEGER N
• Positive values wi and vi such that 1 <= i <= n
• Positive value W.
• OUTPUT:
• N values of xi such that 0 <= xi <=1
• Total profit
GREEDY KNAPSACK ALGORITHM
Algorithm: Greedy-Fractional-Knapsack (w[1..n], p[1..n], W)
for i = 1 to n
do x[i] = 0 // X is ratio of items
weight = 0 //remaining weight of knapsack
for i = 1 to n
if weight + w[i] ≤ W //W is total capacity of bag
then x[i] = 1
weight = weight + w[i] //w[i] is the weight of item
else
x[i] = (W - weight) / w[i]
weight = W
break
return x
Time complexity
• If the provided items are already sorted into a decreasing order of then the
whileloop takes a time in O(n).
• As main time taking step is sorting, the whole problem can be solved in
O(n log n) only.
• It can be O(n^2),O(n^3) anything greater than O(nlogn).
• BUT we usually consider the tightest upper bound that is O(nlogn).
Thank you

More Related Content

What's hot

0/1 knapsack
0/1 knapsack0/1 knapsack
0/1 knapsackAmin Omi
 
Knapsack problem using dynamic programming
Knapsack problem using dynamic programmingKnapsack problem using dynamic programming
Knapsack problem using dynamic programmingkhush_boo31
 
01 Knapsack using Dynamic Programming
01 Knapsack using Dynamic Programming01 Knapsack using Dynamic Programming
01 Knapsack using Dynamic ProgrammingFenil Shah
 
Fractional knapsack class 13
Fractional knapsack class 13Fractional knapsack class 13
Fractional knapsack class 13Kumar
 
Travelling salesman dynamic programming
Travelling salesman dynamic programmingTravelling salesman dynamic programming
Travelling salesman dynamic programmingmaharajdey
 
0 1 knapsack using branch and bound
0 1 knapsack using branch and bound0 1 knapsack using branch and bound
0 1 knapsack using branch and boundAbhishek Singh
 
0-1 KNAPSACK PROBLEM
0-1 KNAPSACK PROBLEM0-1 KNAPSACK PROBLEM
0-1 KNAPSACK PROBLEMi i
 
Divide and conquer - Quick sort
Divide and conquer - Quick sortDivide and conquer - Quick sort
Divide and conquer - Quick sortMadhu Bala
 
Travelling Salesman Problem
Travelling Salesman ProblemTravelling Salesman Problem
Travelling Salesman ProblemDaniel Raditya
 
Greedy algorithms -Making change-Knapsack-Prim's-Kruskal's
Greedy algorithms -Making change-Knapsack-Prim's-Kruskal'sGreedy algorithms -Making change-Knapsack-Prim's-Kruskal's
Greedy algorithms -Making change-Knapsack-Prim's-Kruskal'sJay Patel
 
BackTracking Algorithm: Technique and Examples
BackTracking Algorithm: Technique and ExamplesBackTracking Algorithm: Technique and Examples
BackTracking Algorithm: Technique and ExamplesFahim Ferdous
 

What's hot (20)

0/1 knapsack
0/1 knapsack0/1 knapsack
0/1 knapsack
 
greedy algorithm Fractional Knapsack
greedy algorithmFractional Knapsack greedy algorithmFractional Knapsack
greedy algorithm Fractional Knapsack
 
Knapsack problem using dynamic programming
Knapsack problem using dynamic programmingKnapsack problem using dynamic programming
Knapsack problem using dynamic programming
 
Greedy algorithms
Greedy algorithmsGreedy algorithms
Greedy algorithms
 
01 Knapsack using Dynamic Programming
01 Knapsack using Dynamic Programming01 Knapsack using Dynamic Programming
01 Knapsack using Dynamic Programming
 
Backtracking
BacktrackingBacktracking
Backtracking
 
Tsp branch and-bound
Tsp branch and-boundTsp branch and-bound
Tsp branch and-bound
 
Fractional knapsack class 13
Fractional knapsack class 13Fractional knapsack class 13
Fractional knapsack class 13
 
Travelling salesman dynamic programming
Travelling salesman dynamic programmingTravelling salesman dynamic programming
Travelling salesman dynamic programming
 
0 1 knapsack using branch and bound
0 1 knapsack using branch and bound0 1 knapsack using branch and bound
0 1 knapsack using branch and bound
 
0-1 KNAPSACK PROBLEM
0-1 KNAPSACK PROBLEM0-1 KNAPSACK PROBLEM
0-1 KNAPSACK PROBLEM
 
Divide and conquer - Quick sort
Divide and conquer - Quick sortDivide and conquer - Quick sort
Divide and conquer - Quick sort
 
Knapsack
KnapsackKnapsack
Knapsack
 
Travelling Salesman Problem
Travelling Salesman ProblemTravelling Salesman Problem
Travelling Salesman Problem
 
Branch and bound
Branch and boundBranch and bound
Branch and bound
 
Greedy algorithm
Greedy algorithmGreedy algorithm
Greedy algorithm
 
Greedy algorithms -Making change-Knapsack-Prim's-Kruskal's
Greedy algorithms -Making change-Knapsack-Prim's-Kruskal'sGreedy algorithms -Making change-Knapsack-Prim's-Kruskal's
Greedy algorithms -Making change-Knapsack-Prim's-Kruskal's
 
Prim's algorithm
Prim's algorithmPrim's algorithm
Prim's algorithm
 
BackTracking Algorithm: Technique and Examples
BackTracking Algorithm: Technique and ExamplesBackTracking Algorithm: Technique and Examples
BackTracking Algorithm: Technique and Examples
 
Traveling Salesman Problem
Traveling Salesman Problem Traveling Salesman Problem
Traveling Salesman Problem
 

Similar to Optimal Knapsack Algorithm

0 1 knapsack using naive recursive approach and top-down dynamic programming ...
0 1 knapsack using naive recursive approach and top-down dynamic programming ...0 1 knapsack using naive recursive approach and top-down dynamic programming ...
0 1 knapsack using naive recursive approach and top-down dynamic programming ...Abhishek Singh
 
Design and analysis of Algorithms - Lecture 15.ppt
Design and analysis of Algorithms - Lecture 15.pptDesign and analysis of Algorithms - Lecture 15.ppt
Design and analysis of Algorithms - Lecture 15.pptQurbanAli72
 
knapsackusingbranchandbound
knapsackusingbranchandboundknapsackusingbranchandbound
knapsackusingbranchandboundhodcsencet
 
Knapsack Dynamic
Knapsack DynamicKnapsack Dynamic
Knapsack DynamicParas Patel
 
Knapsack problem and Memory Function
Knapsack problem and Memory FunctionKnapsack problem and Memory Function
Knapsack problem and Memory FunctionBarani Tharan
 
module3_Greedymethod_2022.pdf
module3_Greedymethod_2022.pdfmodule3_Greedymethod_2022.pdf
module3_Greedymethod_2022.pdfShiwani Gupta
 
Comparative analysis-of-dynamic-and-greedy-approaches-for-dynamic-programming
Comparative analysis-of-dynamic-and-greedy-approaches-for-dynamic-programmingComparative analysis-of-dynamic-and-greedy-approaches-for-dynamic-programming
Comparative analysis-of-dynamic-and-greedy-approaches-for-dynamic-programmingEditor IJMTER
 
0/1 DYNAMIC PROGRAMMING KNAPSACK PROBLEM
0/1 DYNAMIC PROGRAMMING KNAPSACK PROBLEM0/1 DYNAMIC PROGRAMMING KNAPSACK PROBLEM
0/1 DYNAMIC PROGRAMMING KNAPSACK PROBLEMMrunal Patil
 
Presentation of knapsack
Presentation of knapsackPresentation of knapsack
Presentation of knapsackGaurav Dubey
 
Data structure and algorithm
Data structure and algorithmData structure and algorithm
Data structure and algorithmvanmathy1
 
Module 3_Greedy Technique_2021 Scheme.pptx
Module 3_Greedy Technique_2021 Scheme.pptxModule 3_Greedy Technique_2021 Scheme.pptx
Module 3_Greedy Technique_2021 Scheme.pptxRITIKKUMAR168218
 
A Survey- Knapsack Problem Using Dynamic Programming
A Survey- Knapsack Problem Using Dynamic ProgrammingA Survey- Knapsack Problem Using Dynamic Programming
A Survey- Knapsack Problem Using Dynamic ProgrammingEditor IJCTER
 
Optimization problems
Optimization problemsOptimization problems
Optimization problemsRuchika Sinha
 
Unit 3- Greedy Method.pptx
Unit 3- Greedy Method.pptxUnit 3- Greedy Method.pptx
Unit 3- Greedy Method.pptxMaryJacob24
 

Similar to Optimal Knapsack Algorithm (20)

Knapsack dp
Knapsack dpKnapsack dp
Knapsack dp
 
0 1 knapsack using naive recursive approach and top-down dynamic programming ...
0 1 knapsack using naive recursive approach and top-down dynamic programming ...0 1 knapsack using naive recursive approach and top-down dynamic programming ...
0 1 knapsack using naive recursive approach and top-down dynamic programming ...
 
Knapsack problem
Knapsack problemKnapsack problem
Knapsack problem
 
Design and analysis of Algorithms - Lecture 15.ppt
Design and analysis of Algorithms - Lecture 15.pptDesign and analysis of Algorithms - Lecture 15.ppt
Design and analysis of Algorithms - Lecture 15.ppt
 
knapsackusingbranchandbound
knapsackusingbranchandboundknapsackusingbranchandbound
knapsackusingbranchandbound
 
12 Greeddy Method
12 Greeddy Method12 Greeddy Method
12 Greeddy Method
 
Knapsack Dynamic
Knapsack DynamicKnapsack Dynamic
Knapsack Dynamic
 
Knapsack problem and Memory Function
Knapsack problem and Memory FunctionKnapsack problem and Memory Function
Knapsack problem and Memory Function
 
module3_Greedymethod_2022.pdf
module3_Greedymethod_2022.pdfmodule3_Greedymethod_2022.pdf
module3_Greedymethod_2022.pdf
 
DSA
DSADSA
DSA
 
Comparative analysis-of-dynamic-and-greedy-approaches-for-dynamic-programming
Comparative analysis-of-dynamic-and-greedy-approaches-for-dynamic-programmingComparative analysis-of-dynamic-and-greedy-approaches-for-dynamic-programming
Comparative analysis-of-dynamic-and-greedy-approaches-for-dynamic-programming
 
Unit 3-Greedy Method
Unit 3-Greedy MethodUnit 3-Greedy Method
Unit 3-Greedy Method
 
Module 3_DAA (2).pptx
Module 3_DAA (2).pptxModule 3_DAA (2).pptx
Module 3_DAA (2).pptx
 
0/1 DYNAMIC PROGRAMMING KNAPSACK PROBLEM
0/1 DYNAMIC PROGRAMMING KNAPSACK PROBLEM0/1 DYNAMIC PROGRAMMING KNAPSACK PROBLEM
0/1 DYNAMIC PROGRAMMING KNAPSACK PROBLEM
 
Presentation of knapsack
Presentation of knapsackPresentation of knapsack
Presentation of knapsack
 
Data structure and algorithm
Data structure and algorithmData structure and algorithm
Data structure and algorithm
 
Module 3_Greedy Technique_2021 Scheme.pptx
Module 3_Greedy Technique_2021 Scheme.pptxModule 3_Greedy Technique_2021 Scheme.pptx
Module 3_Greedy Technique_2021 Scheme.pptx
 
A Survey- Knapsack Problem Using Dynamic Programming
A Survey- Knapsack Problem Using Dynamic ProgrammingA Survey- Knapsack Problem Using Dynamic Programming
A Survey- Knapsack Problem Using Dynamic Programming
 
Optimization problems
Optimization problemsOptimization problems
Optimization problems
 
Unit 3- Greedy Method.pptx
Unit 3- Greedy Method.pptxUnit 3- Greedy Method.pptx
Unit 3- Greedy Method.pptx
 

Recently uploaded

Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentInMediaRes1
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatYousafMalik24
 
CELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptxCELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptxJiesonDelaCerna
 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfMahmoud M. Sallam
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfFraming an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfUjwalaBharambe
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Celine George
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxOH TEIK BIN
 
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxHistory Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxsocialsciencegdgrohi
 
Meghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentMeghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentInMediaRes1
 
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...M56BOOKSTORE PRODUCT/SERVICE
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17Celine George
 
Painted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaPainted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaVirag Sontakke
 
Types of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxTypes of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxEyham Joco
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for BeginnersSabitha Banu
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTiammrhaywood
 

Recently uploaded (20)

Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media Component
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice great
 
CELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptxCELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptx
 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdf
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfFraming an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptx
 
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxHistory Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
Meghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentMeghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media Component
 
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17
 
Painted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaPainted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of India
 
Types of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxTypes of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptx
 
ESSENTIAL of (CS/IT/IS) class 06 (database)
ESSENTIAL of (CS/IT/IS) class 06 (database)ESSENTIAL of (CS/IT/IS) class 06 (database)
ESSENTIAL of (CS/IT/IS) class 06 (database)
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for Beginners
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
 

Optimal Knapsack Algorithm

  • 1. KNAPSACK PROBLEM ALGORIT HM Department of Masters Of Computer Science(MCA) Central university of Karnataka Presented by: Prajjaval Rao Chintal 2018MCA09
  • 2. WHAT ? • GIVEN WEIGHTS AND VALUES OF N ITEMS, WE NEED TO PUT THESE ITEMS IN A KNAPSACK OF CAPACITY W TO GET THE MAXIMUM TOTAL VALUE IN THE KNAPSACK.
  • 3. TYPES • 0-1 KNAPSACK PROBLEM In the 0-1 knapsack problem, we are not allowed to break items. We either take the whole item or don’t take it. • FRACTIONAL KNAPSACK In fractional knapsack, we can break items for maximizing the total value of knapsack. This problem in which we can break an item is also called the fractional knapsack problem. THE OPTIMAL KNAPSACK ALGORITHM
  • 4. EXAMPLE 0-1 KNAPSACK Take B and C Total weight =20+30=50 Total value =100+120=220 FRACTIONAL KNAPSACK Take A,B and 2/3rd of C Total weight =10+20+(30* 2/3)=50 Total value =60+100+(120* 2/3)=240
  • 5. GREEDY APPROACH • The basic idea of the greedy approach is to calculate the ratio value/weight for each item. • Sort the item on basis of this ratio. • Then take the item with the highest ratio and add them until we can’t add the next item as a whole. • At the end add the next item as much (fraction) as we can.
  • 6. GREEDY APPROACH SOLUTION Ratio= 6 Ratio= 5 Ratio= 4 1. calculate the ratio value/weight. 2. Sort (descending) item on basis of ratio. 3. Take item with highest ratio and add to knapsack until we cant add the next item as whole. 4. At the end add the next item Capacity left =40 Value = 60 Capacity left =20 Value = 160 Take 2/3rd of C Weight = 2/3 *30 =20 Value = 2/3 *120 =80 Capacity left =0 Value = 240
  • 7. THE OPTIMAL KNAPSACK ALGORITHM • INPUT: AN INTEGER N • Positive values wi and vi such that 1 <= i <= n • Positive value W. • OUTPUT: • N values of xi such that 0 <= xi <=1 • Total profit
  • 8. GREEDY KNAPSACK ALGORITHM Algorithm: Greedy-Fractional-Knapsack (w[1..n], p[1..n], W) for i = 1 to n do x[i] = 0 // X is ratio of items weight = 0 //remaining weight of knapsack for i = 1 to n if weight + w[i] ≤ W //W is total capacity of bag then x[i] = 1 weight = weight + w[i] //w[i] is the weight of item else x[i] = (W - weight) / w[i] weight = W break return x Time complexity • If the provided items are already sorted into a decreasing order of then the whileloop takes a time in O(n). • As main time taking step is sorting, the whole problem can be solved in O(n log n) only. • It can be O(n^2),O(n^3) anything greater than O(nlogn). • BUT we usually consider the tightest upper bound that is O(nlogn).