SlideShare a Scribd company logo
KNAPSACK
PROBLEM
The knapsack problem states that −
given a set of items, holding weights
and profit values, one must
determine the subset of the items to
be added in a knapsack such that, the
total weight of the items must not
exceed the limit of the knapsack and
its total profit value is maximum.
0-1 Knapsack Problem: Optimizing Value with Constraints
LIST OF
CONTENTS
Introduction
Fractional knapsack
problem using greedy
method
Steps involved to solve
the problem
Example
problem
Pseudocode
INTROduction
Given the weights and profits of N items, in the
form of {profit, weight} put these items in a
knapsack of capacity W to get the maximum total
profit in the knapsack. In Fractional Knapsack, we
can break items for maximizing the total value of
the knapsack.
MAXIMUM
PROFIT/WEIGH
T RATIO
THE THREE POSSIBLE APPROACHES
MAXIMUM
PROFIT
MINIMUM
WEIGHT
AN EFFICIENT
SOLUTION IS TO
USE THE GREEDY
APPROACH.
The basic idea of the greedy approach is to calculate
the ratio profit/weight for each item and sort the item
on the basis of this ratio. Then take the item with the
highest ratio and add them as much as we can (can be
the whole element or a fraction of it).
This will always give the maximum profit because, in
each step it adds an element such that this is the
maximum possible profit for that much weight.
• CALCULATE THE RATIO
(PROFIT/WEIGHT) FOR EACH
ITEM.
• DO THE FOLLOWING FOR EVERY ITEM I IN THE
SORTED ORDER:
1.If the weight of the current item is less than or equal
to the remaining capacity then add the value of that
item into the result
2.Else add the current item as much as we can and
break out of the loop.
Follow the given steps to solve the problem
• SORT ALL THE ITEMS IN
DECREASING ORDER OF
THE RATIO.
• INITIALIZE RES = 0,
CURR_CAP = GIVEN_CAP.
• RETURN RES.
STEPS
MAX. PROFIT=60
GreedyKnapsack(m, n):
//P[1..n]: Profits of items
//W[1..n]: Weights of items
//x[1..n]: Fraction of items to be taken
//m: Capacity of the knapsack
Sort items such that P[i]/W[i] is in decreasing order
x :=0
u := m
p := 0
for i := 1 to n do
if W[i] <= u then
x[i] := 1
u := u - W[i]
p := p + P[i]
else
x[i] := u / W[i]
p := p + P[i] * x[i]
break
return x, p
P
S
E
U
D
O
C
O
D
E
THANK
YOU

More Related Content

PPTX
0 1 knapsack using branch and bound
PPTX
Knapsack problem using Greedy method.pptx
PPTX
Knapsack problem algorithm, greedy algorithm
PPTX
Fractional Knapsack Problem
PPTX
Knapsack Problem (DP & GREEDY)
PDF
knapsackusingbranchandbound
PPTX
5b.Greedy Technique - Fractional Knapsack+Coin change Problem.pptx
PPTX
Knapsack problem using greedy approach
0 1 knapsack using branch and bound
Knapsack problem using Greedy method.pptx
Knapsack problem algorithm, greedy algorithm
Fractional Knapsack Problem
Knapsack Problem (DP & GREEDY)
knapsackusingbranchandbound
5b.Greedy Technique - Fractional Knapsack+Coin change Problem.pptx
Knapsack problem using greedy approach

Similar to 0-1 Knapsack Problem: Optimizing Value with Constraints (20)

PDF
12 Greeddy Method
PPTX
Knapsack
PPTX
Greedy Algorithm - Knapsack Problem
PPTX
Fractional knapsack problem
PPT
Knapsack problem and Memory Function
PPTX
Dynamic Programming-Knapsack Problem
PPTX
Fractional knapsack class 13
PPT
Knapsack problem
PPTX
ppt 2.pptxlmkgngxjgdjgdjtxjgxjgxnvndjcgxjxjjc
PPTX
Module 3_Greedy Technique_2021 Scheme.pptx
PDF
Knapsack dp
PPTX
Data structure and algorithm
PDF
Comparative analysis-of-dynamic-and-greedy-approaches-for-dynamic-programming
PPT
DynProg_Knapsack.ppt
PPTX
01 Knapsack using Dynamic Programming
PPTX
Greedy algo revision 2
PPTX
Greedy algorithms
PPTX
0 1 knapsack using naive recursive approach and top-down dynamic programming ...
PPTX
daa-unit-3-greedy method
PPTX
Knapsack problem
12 Greeddy Method
Knapsack
Greedy Algorithm - Knapsack Problem
Fractional knapsack problem
Knapsack problem and Memory Function
Dynamic Programming-Knapsack Problem
Fractional knapsack class 13
Knapsack problem
ppt 2.pptxlmkgngxjgdjgdjtxjgxjgxnvndjcgxjxjjc
Module 3_Greedy Technique_2021 Scheme.pptx
Knapsack dp
Data structure and algorithm
Comparative analysis-of-dynamic-and-greedy-approaches-for-dynamic-programming
DynProg_Knapsack.ppt
01 Knapsack using Dynamic Programming
Greedy algo revision 2
Greedy algorithms
0 1 knapsack using naive recursive approach and top-down dynamic programming ...
daa-unit-3-greedy method
Knapsack problem
Ad

Recently uploaded (20)

PPTX
Safety Seminar civil to be ensured for safe working.
PDF
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
PDF
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
PDF
Embodied AI: Ushering in the Next Era of Intelligent Systems
PPTX
Construction Project Organization Group 2.pptx
PDF
Digital Logic Computer Design lecture notes
DOCX
573137875-Attendance-Management-System-original
PDF
composite construction of structures.pdf
PPTX
CH1 Production IntroductoryConcepts.pptx
PDF
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
PDF
Automation-in-Manufacturing-Chapter-Introduction.pdf
PDF
R24 SURVEYING LAB MANUAL for civil enggi
PPTX
Current and future trends in Computer Vision.pptx
PDF
Operating System & Kernel Study Guide-1 - converted.pdf
PDF
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf
PDF
Well-logging-methods_new................
PPTX
UNIT-1 - COAL BASED THERMAL POWER PLANTS
PPTX
bas. eng. economics group 4 presentation 1.pptx
PPTX
Foundation to blockchain - A guide to Blockchain Tech
PDF
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
Safety Seminar civil to be ensured for safe working.
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
Embodied AI: Ushering in the Next Era of Intelligent Systems
Construction Project Organization Group 2.pptx
Digital Logic Computer Design lecture notes
573137875-Attendance-Management-System-original
composite construction of structures.pdf
CH1 Production IntroductoryConcepts.pptx
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
Automation-in-Manufacturing-Chapter-Introduction.pdf
R24 SURVEYING LAB MANUAL for civil enggi
Current and future trends in Computer Vision.pptx
Operating System & Kernel Study Guide-1 - converted.pdf
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf
Well-logging-methods_new................
UNIT-1 - COAL BASED THERMAL POWER PLANTS
bas. eng. economics group 4 presentation 1.pptx
Foundation to blockchain - A guide to Blockchain Tech
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
Ad

0-1 Knapsack Problem: Optimizing Value with Constraints

  • 2. The knapsack problem states that − given a set of items, holding weights and profit values, one must determine the subset of the items to be added in a knapsack such that, the total weight of the items must not exceed the limit of the knapsack and its total profit value is maximum.
  • 4. LIST OF CONTENTS Introduction Fractional knapsack problem using greedy method Steps involved to solve the problem Example problem Pseudocode
  • 5. INTROduction Given the weights and profits of N items, in the form of {profit, weight} put these items in a knapsack of capacity W to get the maximum total profit in the knapsack. In Fractional Knapsack, we can break items for maximizing the total value of the knapsack.
  • 6. MAXIMUM PROFIT/WEIGH T RATIO THE THREE POSSIBLE APPROACHES MAXIMUM PROFIT MINIMUM WEIGHT
  • 7. AN EFFICIENT SOLUTION IS TO USE THE GREEDY APPROACH. The basic idea of the greedy approach is to calculate the ratio profit/weight for each item and sort the item on the basis of this ratio. Then take the item with the highest ratio and add them as much as we can (can be the whole element or a fraction of it). This will always give the maximum profit because, in each step it adds an element such that this is the maximum possible profit for that much weight.
  • 8. • CALCULATE THE RATIO (PROFIT/WEIGHT) FOR EACH ITEM. • DO THE FOLLOWING FOR EVERY ITEM I IN THE SORTED ORDER: 1.If the weight of the current item is less than or equal to the remaining capacity then add the value of that item into the result 2.Else add the current item as much as we can and break out of the loop. Follow the given steps to solve the problem • SORT ALL THE ITEMS IN DECREASING ORDER OF THE RATIO. • INITIALIZE RES = 0, CURR_CAP = GIVEN_CAP. • RETURN RES. STEPS
  • 10. GreedyKnapsack(m, n): //P[1..n]: Profits of items //W[1..n]: Weights of items //x[1..n]: Fraction of items to be taken //m: Capacity of the knapsack Sort items such that P[i]/W[i] is in decreasing order x :=0 u := m p := 0 for i := 1 to n do if W[i] <= u then x[i] := 1 u := u - W[i] p := p + P[i] else x[i] := u / W[i] p := p + P[i] * x[i] break return x, p P S E U D O C O D E