SlideShare a Scribd company logo
1 of 13
KNAPSACK PROBLEM
Submitted To: Submitted By:
Mr. K.S. BUMRAH AMAN SHARMA
KNAPSACK PROBLEM
Suppose:
Given items of different values and volumes, find the most valuable set of
items that fit in a knapsack of fixed volume.
KNAPSACK PROBLEM
Now the problem is that If we have a knapsack that only can hold 20 kg
of weight. Which items to be select that volume doesn't exceeds 20 kg
but have the high value?
KNAPSACK PROBLEM
In this case the problem is known as 0/1 knapsack problem because we
will either select an item or either reject in item.
0 1
Brute Force approach to solve this problem
• First we will create all possible outcomes of the items.
• Then we will select the best one which weight doesn’t exceeds from
20 kg but have the maximum value.
Clock Frame Radio Jar Book PC
Value $175 $90 $20 $50 $10 $200
Weight 10 kg 9 kg 4 kg 2 kg 1 kg 20 kg
(1) We can select
Total weight = 20 kg
Total value = 275 $
Total weight = 17 kg
Total value = 255 $
Total weight = 16 kg
Total value = 170 $
Total weight = 20 kg
Total value = 200 $
Now after having all combinations of items we can say that option no 1
is best to choose.
Because in that option we have exactly 20 kg of weight which the
knapsack can hold with the highest volume.
Total weight = 20 kg
Total value = 275 $
0-1 Knapsack problem
•Given a knapsack with maximum capacity W, and a set S consisting of
n items
•Each item i has some weight wi and benefit value bi (all wi , bi and W
are integer values)
•Problem: How to pack the knapsack to achieve maximum total value
of packed items?
•Solution: Dynamic Programming.
Fractional Knapsack Problem
• We are given n objects and a knapsack. Object i has a weight wi
and the knapsack has a capacity m.
• If a fraction xi, 0<=xi <=1, of object i is placed into the knapsack,
then a profit pi xi is earned.
• The objective is to obtain a filling of the knapsack that maximizes
the total profit earned.
Greedy Algorithm gives the optimal solution
• Greedy-fractional-knapsack (w, v, W)
1. for i =1 to n
2. do x[i] =0
3. weight = 0
4. while weight < W
5. do i = best remaining item
6. if weight + w[i] ≤ W
7. then x[i] = 1
8. weight = weight + w[i]
9. else
10. x[i] = (w - weight) / w[i]
11. weight = W
12. return x
Analysis
•If the items are already sorted into decreasing order of vi / wi , then
the while-loop takes a time in O(n);
•Therefore, the total time including the sort is in O(n log n).
•If we keep the items in heap with largest vi/wi at the root. Then
creating the heap takes O(n) time
•while-loop now takes O(log n) time (since heap property must be
restored after the removal of root)
Knapsack problem

More Related Content

Similar to 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.pptQurbanAli72
 
Knapsack problem algorithm, greedy algorithm
Knapsack problem algorithm, greedy algorithmKnapsack problem algorithm, greedy algorithm
Knapsack problem algorithm, greedy algorithmHoneyChintal
 
knapsack problem
knapsack problemknapsack problem
knapsack problemAdnan Malak
 
Knapsack problem and Memory Function
Knapsack problem and Memory FunctionKnapsack problem and Memory Function
Knapsack problem and Memory FunctionBarani Tharan
 
Knapsack Problem (DP & GREEDY)
Knapsack Problem (DP & GREEDY)Knapsack Problem (DP & GREEDY)
Knapsack Problem (DP & GREEDY)Ridhima Chowdhury
 
Knapsack Dynamic
Knapsack DynamicKnapsack Dynamic
Knapsack DynamicParas Patel
 
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
 
Greedy Algorithm - Knapsack Problem
Greedy Algorithm - Knapsack ProblemGreedy Algorithm - Knapsack Problem
Greedy Algorithm - Knapsack ProblemMadhu Bala
 
Presentation of knapsack
Presentation of knapsackPresentation of knapsack
Presentation of knapsackGaurav Dubey
 
4 greedy methodnew
4 greedy methodnew4 greedy methodnew
4 greedy methodnewabhinav108
 
Knapsack problem using dynamic programming
Knapsack problem using dynamic programmingKnapsack problem using dynamic programming
Knapsack problem using dynamic programmingkhush_boo31
 
0/1 DYNAMIC PROGRAMMING KNAPSACK PROBLEM
0/1 DYNAMIC PROGRAMMING KNAPSACK PROBLEM0/1 DYNAMIC PROGRAMMING KNAPSACK PROBLEM
0/1 DYNAMIC PROGRAMMING KNAPSACK PROBLEMMrunal Patil
 
Greedy method1
Greedy method1Greedy method1
Greedy method1Rajendran
 
Greedy+Day-3.pptx
Greedy+Day-3.pptxGreedy+Day-3.pptx
Greedy+Day-3.pptxAkswant
 

Similar to Knapsack problem (20)

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
 
Knapsack problem algorithm, greedy algorithm
Knapsack problem algorithm, greedy algorithmKnapsack problem algorithm, greedy algorithm
Knapsack problem algorithm, greedy algorithm
 
Knapsack dp
Knapsack dpKnapsack dp
Knapsack dp
 
knapsack problem
knapsack problemknapsack problem
knapsack problem
 
Knapsack problem and Memory Function
Knapsack problem and Memory FunctionKnapsack problem and Memory Function
Knapsack problem and Memory Function
 
Knapsack Problem (DP & GREEDY)
Knapsack Problem (DP & GREEDY)Knapsack Problem (DP & GREEDY)
Knapsack Problem (DP & GREEDY)
 
12 Greeddy Method
12 Greeddy Method12 Greeddy Method
12 Greeddy Method
 
Knapsack Dynamic
Knapsack DynamicKnapsack Dynamic
Knapsack Dynamic
 
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 ...
 
Greedy Algorithm - Knapsack Problem
Greedy Algorithm - Knapsack ProblemGreedy Algorithm - Knapsack Problem
Greedy Algorithm - Knapsack Problem
 
Dynamic programming
Dynamic programmingDynamic programming
Dynamic programming
 
Presentation of knapsack
Presentation of knapsackPresentation of knapsack
Presentation of knapsack
 
4 greedy methodnew
4 greedy methodnew4 greedy methodnew
4 greedy methodnew
 
Knapsack problem
Knapsack problemKnapsack problem
Knapsack problem
 
Knapsack
KnapsackKnapsack
Knapsack
 
Dynamic Programming
Dynamic ProgrammingDynamic Programming
Dynamic Programming
 
Knapsack problem using dynamic programming
Knapsack problem using dynamic programmingKnapsack problem using dynamic programming
Knapsack problem using dynamic programming
 
0/1 DYNAMIC PROGRAMMING KNAPSACK PROBLEM
0/1 DYNAMIC PROGRAMMING KNAPSACK PROBLEM0/1 DYNAMIC PROGRAMMING KNAPSACK PROBLEM
0/1 DYNAMIC PROGRAMMING KNAPSACK PROBLEM
 
Greedy method1
Greedy method1Greedy method1
Greedy method1
 
Greedy+Day-3.pptx
Greedy+Day-3.pptxGreedy+Day-3.pptx
Greedy+Day-3.pptx
 

More from garishma bhatia

More from garishma bhatia (19)

Peephole optimization techniques
Peephole optimization techniquesPeephole optimization techniques
Peephole optimization techniques
 
Vm migration techniques
Vm migration techniquesVm migration techniques
Vm migration techniques
 
Denial of service
Denial of serviceDenial of service
Denial of service
 
Iot + cloud
Iot + cloudIot + cloud
Iot + cloud
 
WORKFLOW OF THE PROCESS IN SPM
 WORKFLOW OF THE PROCESS IN SPM WORKFLOW OF THE PROCESS IN SPM
WORKFLOW OF THE PROCESS IN SPM
 
MULTILEVEL QUEUE SCHEDULING
MULTILEVEL QUEUE SCHEDULINGMULTILEVEL QUEUE SCHEDULING
MULTILEVEL QUEUE SCHEDULING
 
Constructor overloading & method overloading
Constructor overloading & method overloadingConstructor overloading & method overloading
Constructor overloading & method overloading
 
MULTIPLE ACCESS PROTOCOL COMPUTER NETWORKS
MULTIPLE ACCESS PROTOCOL COMPUTER NETWORKS MULTIPLE ACCESS PROTOCOL COMPUTER NETWORKS
MULTIPLE ACCESS PROTOCOL COMPUTER NETWORKS
 
BLOCKCHAIN TECHNOLOGY
BLOCKCHAIN TECHNOLOGYBLOCKCHAIN TECHNOLOGY
BLOCKCHAIN TECHNOLOGY
 
PRIM'S ALGORITHM
PRIM'S ALGORITHMPRIM'S ALGORITHM
PRIM'S ALGORITHM
 
Data types IN JAVA
Data types IN JAVAData types IN JAVA
Data types IN JAVA
 
Data Algorithms And Analysis
Data Algorithms And AnalysisData Algorithms And Analysis
Data Algorithms And Analysis
 
VEHICLE REGISTRATION PROJECT
VEHICLE REGISTRATION PROJECTVEHICLE REGISTRATION PROJECT
VEHICLE REGISTRATION PROJECT
 
WIFI TECH
WIFI TECHWIFI TECH
WIFI TECH
 
MATCHING GRAPH THEORY
MATCHING GRAPH THEORYMATCHING GRAPH THEORY
MATCHING GRAPH THEORY
 
Garishma se
Garishma seGarishma se
Garishma se
 
Garishma xcs
Garishma xcsGarishma xcs
Garishma xcs
 
Garishma maTHS
Garishma maTHSGarishma maTHS
Garishma maTHS
 
Garishma ec
Garishma ecGarishma ec
Garishma ec
 

Recently uploaded

Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service NashikCall Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service NashikCall Girls in Nagpur High Profile
 
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordCCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordAsst.prof M.Gokilavani
 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
UNIT-III FMM. DIMENSIONAL ANALYSIS
UNIT-III FMM.        DIMENSIONAL ANALYSISUNIT-III FMM.        DIMENSIONAL ANALYSIS
UNIT-III FMM. DIMENSIONAL ANALYSISrknatarajan
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130Suhani Kapoor
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escortsranjana rawat
 
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Call Girls in Nagpur High Profile
 
Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxupamatechverse
 
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Serviceranjana rawat
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCall Girls in Nagpur High Profile
 
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Dr.Costas Sachpazis
 
result management system report for college project
result management system report for college projectresult management system report for college project
result management system report for college projectTonystark477637
 
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSMANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSSIVASHANKAR N
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
Introduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxIntroduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxupamatechverse
 
SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )Tsuyoshi Horigome
 
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINEMANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINESIVASHANKAR N
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...Soham Mondal
 

Recently uploaded (20)

Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service NashikCall Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
 
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordCCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
 
UNIT-III FMM. DIMENSIONAL ANALYSIS
UNIT-III FMM.        DIMENSIONAL ANALYSISUNIT-III FMM.        DIMENSIONAL ANALYSIS
UNIT-III FMM. DIMENSIONAL ANALYSIS
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
 
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
 
Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptx
 
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
 
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
 
result management system report for college project
result management system report for college projectresult management system report for college project
result management system report for college project
 
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSMANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
 
Introduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxIntroduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptx
 
SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )
 
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINEMANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
 
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINEDJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
 

Knapsack problem

  • 1. KNAPSACK PROBLEM Submitted To: Submitted By: Mr. K.S. BUMRAH AMAN SHARMA
  • 2. KNAPSACK PROBLEM Suppose: Given items of different values and volumes, find the most valuable set of items that fit in a knapsack of fixed volume.
  • 3. KNAPSACK PROBLEM Now the problem is that If we have a knapsack that only can hold 20 kg of weight. Which items to be select that volume doesn't exceeds 20 kg but have the high value?
  • 4. KNAPSACK PROBLEM In this case the problem is known as 0/1 knapsack problem because we will either select an item or either reject in item. 0 1
  • 5. Brute Force approach to solve this problem • First we will create all possible outcomes of the items. • Then we will select the best one which weight doesn’t exceeds from 20 kg but have the maximum value. Clock Frame Radio Jar Book PC Value $175 $90 $20 $50 $10 $200 Weight 10 kg 9 kg 4 kg 2 kg 1 kg 20 kg
  • 6. (1) We can select Total weight = 20 kg Total value = 275 $ Total weight = 17 kg Total value = 255 $
  • 7. Total weight = 16 kg Total value = 170 $ Total weight = 20 kg Total value = 200 $
  • 8. Now after having all combinations of items we can say that option no 1 is best to choose. Because in that option we have exactly 20 kg of weight which the knapsack can hold with the highest volume. Total weight = 20 kg Total value = 275 $
  • 9. 0-1 Knapsack problem •Given a knapsack with maximum capacity W, and a set S consisting of n items •Each item i has some weight wi and benefit value bi (all wi , bi and W are integer values) •Problem: How to pack the knapsack to achieve maximum total value of packed items? •Solution: Dynamic Programming.
  • 10. Fractional Knapsack Problem • We are given n objects and a knapsack. Object i has a weight wi and the knapsack has a capacity m. • If a fraction xi, 0<=xi <=1, of object i is placed into the knapsack, then a profit pi xi is earned. • The objective is to obtain a filling of the knapsack that maximizes the total profit earned.
  • 11. Greedy Algorithm gives the optimal solution • Greedy-fractional-knapsack (w, v, W) 1. for i =1 to n 2. do x[i] =0 3. weight = 0 4. while weight < W 5. do i = best remaining item 6. if weight + w[i] ≤ W 7. then x[i] = 1 8. weight = weight + w[i] 9. else 10. x[i] = (w - weight) / w[i] 11. weight = W 12. return x
  • 12. Analysis •If the items are already sorted into decreasing order of vi / wi , then the while-loop takes a time in O(n); •Therefore, the total time including the sort is in O(n log n). •If we keep the items in heap with largest vi/wi at the root. Then creating the heap takes O(n) time •while-loop now takes O(log n) time (since heap property must be restored after the removal of root)