SlideShare a Scribd company logo
2
Most read
7
Most read
13
Most read
Knapsack problem
M.Madhu Bala
Mphil (CS)
OPTIMIZATION PROBLEM (Cont.)
 An optimization problem:
 Given a problem instance, a set of constraints and an
objective function.
 Find a feasible solution for the given instance.
 either maximum or minimum depending on the
problem being solved.
 constraints specify the limitations on the required
solutions.
SOLUTION FOR OPTIMIZATION PROBLEM
For some optimization problems,
• Dynamic Programming is “overkill”
• Greedy Strategy is simpler and more efficient
.
DYNAMIC PROGRAMMING VS GREEDY
Dynamic Programming Greedy Algorithm
At each step, the choice is
determined based on
solutions of sub problems.
At each step, we quickly make a
choice that currently looks best.
A local optimal (greedy) choice
Sub-problems are solved
first.
Greedy choice can be made first
before solving further sub-
problems.
Bottom-up approach Top-down approach
Can be slower,
more complex
Usually faster,
simpler
 Characteristics of greedy algorithm:
 make a sequence of choices
 each choice is the one that seems best so far, only
depends on what's been done so far
 choice produces a smaller problem to be solved
GREEDY METHOD
PHASES OF GREEDY ALGORITHM
 A greedy algorithm works in phases.
 At each phase:
 takes the best solution right now, without regard for
future consequences
 choosing a local optimum at each step, and end up at a
global optimum solution.
KNAPSACK PROBLEM
There are two version of knapsack problem
1. 0-1 knapsack problem:
 Items are indivisible. (either take an item or not)
 can be solved with dynamic programming.
2. Fractional knapsack problem:
 Items are divisible. (can take any fraction of an item)
 It can be solved in greedy method
0-1 KNAPSACK PROBLEM:
 A thief robbing a store finds n items.
 ith item: worth vi value of item and wi weight of item
 W, wi, vi are integers.
 He can carry at most W pounds.
FRACTIONAL KNAPSACK PROBLEM:
 A thief robbing a store finds n items.
 ith item: worth vi value of item and wi weight of item
 W, wi, vi are integers.
 He can carry at most W pounds.
 He can take fractions of items.
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
Initialization:
 Sort the n objects from large to small based on their ratios vi / wi .
 We assume the arrays w[1..n] and v[1..n] store the respective
weights and values after sorting.
 initialize array x[1..n] to zeros.
 weight = 0; i = 1;
THE OPTIMAL KNAPSACK ALGORITHM
while (i  n and weight < W) do
if weight + w[i]  W then
x[i] = 1
else
x[i] = (W – weight) / w[i]
weight = weight + x[i] * w[i]
i++
THE OPTIMAL KNAPSACK ALGORITHM
KNAPSACK - EXAMPLE
Problem:
n = 3
W= 20
(v1, v2, v3) = (25, 24, 15)
(w1, w2, w3) = (18, 15, 10)
Solution:
 Optimal solution:
 x1 = 0
 x2 = 1
 x3 = 1/2
 Total profit = 24 + 7.5 = 31.5
KNAPSACK - EXAMPLE
THANK YOU

More Related Content

What's hot (20)

PPTX
Asymptotic Notations
Rishabh Soni
 
PPT
Sum of subsets problem by backtracking 
Hasanain Alshadoodee
 
PPTX
0 1 knapsack using branch and bound
Abhishek Singh
 
PPT
0/1 knapsack
Amin Omi
 
PDF
Daa notes 1
smruti sarangi
 
PPT
Minimum spanning tree
Hinal Lunagariya
 
PPTX
Analysis and Design of Algorithms
Bulbul Agrawal
 
PPTX
Stressen's matrix multiplication
Kumar
 
PPT
Knapsack problem
Vikas Sharma
 
PPT
Divide and conquer
Dr Shashikant Athawale
 
PPTX
Lecture optimal binary search tree
Divya Ks
 
PPT
Greedy Algorithm
Waqar Akram
 
PPTX
Divide and Conquer - Part 1
Amrinder Arora
 
PPT
Greedy algorithms
Rajendran
 
PPT
Greedy Algorihm
Muhammad Amjad Rana
 
PPTX
Prim's algorithm
Pankaj Thakur
 
PPT
Divide and Conquer
Dr Shashikant Athawale
 
PPTX
All pair shortest path
Arafat Hossan
 
PPTX
Greedy Algorithms
Amrinder Arora
 
PPTX
Strassen's matrix multiplication
Megha V
 
Asymptotic Notations
Rishabh Soni
 
Sum of subsets problem by backtracking 
Hasanain Alshadoodee
 
0 1 knapsack using branch and bound
Abhishek Singh
 
0/1 knapsack
Amin Omi
 
Daa notes 1
smruti sarangi
 
Minimum spanning tree
Hinal Lunagariya
 
Analysis and Design of Algorithms
Bulbul Agrawal
 
Stressen's matrix multiplication
Kumar
 
Knapsack problem
Vikas Sharma
 
Divide and conquer
Dr Shashikant Athawale
 
Lecture optimal binary search tree
Divya Ks
 
Greedy Algorithm
Waqar Akram
 
Divide and Conquer - Part 1
Amrinder Arora
 
Greedy algorithms
Rajendran
 
Greedy Algorihm
Muhammad Amjad Rana
 
Prim's algorithm
Pankaj Thakur
 
Divide and Conquer
Dr Shashikant Athawale
 
All pair shortest path
Arafat Hossan
 
Greedy Algorithms
Amrinder Arora
 
Strassen's matrix multiplication
Megha V
 

Viewers also liked (12)

PDF
Materi 4 penyelesaian spl tiga atau lebih variabel
radar radius
 
PPTX
Greedy algorithms -Making change-Knapsack-Prim's-Kruskal's
Jay Patel
 
DOCX
program pencarian data dengan bahasa C
kir yy
 
PPTX
0 1 knapsack problem using dynamic programming
Maher Alshammari
 
PPT
Knapsack problem using dynamic programming
khush_boo31
 
PPT
DESIGN AND ANALYSIS OF ALGORITHMS
Gayathri Gaayu
 
PPT
Genetic Algorithm based Approach to solve Non-Fractional (0/1) Knapsack Optim...
International Islamic University
 
PPTX
Kruskal Algorithm
Snehasis Panigrahi
 
PPTX
Knapsack
Karthik Chetla
 
PPT
01 knapsack using backtracking
mandlapure
 
PPTX
Greedy algorithm
Caisar Oentoro
 
PPT
Knapsack problem using fixed tuple
Mohanlal Sukhadia University (MLSU)
 
Materi 4 penyelesaian spl tiga atau lebih variabel
radar radius
 
Greedy algorithms -Making change-Knapsack-Prim's-Kruskal's
Jay Patel
 
program pencarian data dengan bahasa C
kir yy
 
0 1 knapsack problem using dynamic programming
Maher Alshammari
 
Knapsack problem using dynamic programming
khush_boo31
 
DESIGN AND ANALYSIS OF ALGORITHMS
Gayathri Gaayu
 
Genetic Algorithm based Approach to solve Non-Fractional (0/1) Knapsack Optim...
International Islamic University
 
Kruskal Algorithm
Snehasis Panigrahi
 
Knapsack
Karthik Chetla
 
01 knapsack using backtracking
mandlapure
 
Greedy algorithm
Caisar Oentoro
 
Knapsack problem using fixed tuple
Mohanlal Sukhadia University (MLSU)
 
Ad

Similar to Greedy Algorithm - Knapsack Problem (20)

PPTX
Fractional knapsack class 13
Kumar
 
PPTX
Fractional knapsack problem
Learning Courses Online
 
PDF
Comparative analysis-of-dynamic-and-greedy-approaches-for-dynamic-programming
Editor IJMTER
 
PPT
Design and analysis of Algorithms - Lecture 15.ppt
QurbanAli72
 
PPTX
ppt 2.pptxlmkgngxjgdjgdjtxjgxjgxnvndjcgxjxjjc
ArunavaGhosh36
 
PPTX
knapsack problem
Adnan Malak
 
PPTX
Module 3_Greedy Technique_2021 Scheme.pptx
RITIKKUMAR168218
 
PPTX
Dynamic Programming-Knapsack Problem
Amrita Yadav
 
PPT
Knapsack problem and Memory Function
Barani Tharan
 
PPT
DynProg_Knapsack.ppt
Ruchika Sinha
 
PPTX
5b.Greedy Technique - Fractional Knapsack+Coin change Problem.pptx
Suma Raj
 
PPTX
0 1 knapsack using naive recursive approach and top-down dynamic programming ...
Abhishek Singh
 
PPT
Lec37
Nikhil Chilwant
 
PPTX
01 Knapsack using Dynamic Programming
Fenil Shah
 
PPTX
Daa:Dynamic Programing
rupali_2bonde
 
PDF
12 Greeddy Method
Andres Mendez-Vazquez
 
PPTX
0/1 DYNAMIC PROGRAMMING KNAPSACK PROBLEM
Mrunal Patil
 
PPTX
Knapsack Problem Data Structure and Algorithm
jp0770930
 
PPT
Elak3 need of greedy for design and analysis of algorithms.ppt
Elakkiya Rajasekar
 
PPTX
Ms nikita greedy agorithm
Nikitagupta123
 
Fractional knapsack class 13
Kumar
 
Fractional knapsack problem
Learning Courses Online
 
Comparative analysis-of-dynamic-and-greedy-approaches-for-dynamic-programming
Editor IJMTER
 
Design and analysis of Algorithms - Lecture 15.ppt
QurbanAli72
 
ppt 2.pptxlmkgngxjgdjgdjtxjgxjgxnvndjcgxjxjjc
ArunavaGhosh36
 
knapsack problem
Adnan Malak
 
Module 3_Greedy Technique_2021 Scheme.pptx
RITIKKUMAR168218
 
Dynamic Programming-Knapsack Problem
Amrita Yadav
 
Knapsack problem and Memory Function
Barani Tharan
 
DynProg_Knapsack.ppt
Ruchika Sinha
 
5b.Greedy Technique - Fractional Knapsack+Coin change Problem.pptx
Suma Raj
 
0 1 knapsack using naive recursive approach and top-down dynamic programming ...
Abhishek Singh
 
01 Knapsack using Dynamic Programming
Fenil Shah
 
Daa:Dynamic Programing
rupali_2bonde
 
12 Greeddy Method
Andres Mendez-Vazquez
 
0/1 DYNAMIC PROGRAMMING KNAPSACK PROBLEM
Mrunal Patil
 
Knapsack Problem Data Structure and Algorithm
jp0770930
 
Elak3 need of greedy for design and analysis of algorithms.ppt
Elakkiya Rajasekar
 
Ms nikita greedy agorithm
Nikitagupta123
 
Ad

More from Madhu Bala (10)

PPTX
Internet of Things (IoT)
Madhu Bala
 
PPTX
Digital logic
Madhu Bala
 
PPTX
Operating system
Madhu Bala
 
PPTX
GRAPH APPLICATION - MINIMUM SPANNING TREE (MST)
Madhu Bala
 
PPTX
Divide and conquer - Quick sort
Madhu Bala
 
PPTX
GPRS Technology
Madhu Bala
 
PPTX
Algorithm - Introduction
Madhu Bala
 
PPTX
4G technology
Madhu Bala
 
PPTX
Data structure - Graph
Madhu Bala
 
PPTX
Smoothing Filters in Spatial Domain
Madhu Bala
 
Internet of Things (IoT)
Madhu Bala
 
Digital logic
Madhu Bala
 
Operating system
Madhu Bala
 
GRAPH APPLICATION - MINIMUM SPANNING TREE (MST)
Madhu Bala
 
Divide and conquer - Quick sort
Madhu Bala
 
GPRS Technology
Madhu Bala
 
Algorithm - Introduction
Madhu Bala
 
4G technology
Madhu Bala
 
Data structure - Graph
Madhu Bala
 
Smoothing Filters in Spatial Domain
Madhu Bala
 

Recently uploaded (20)

PPTX
fatigue in aircraft structures-221113192308-0ad6dc8c.pptx
aviatecofficial
 
PDF
Pressure Measurement training for engineers and Technicians
AIESOLUTIONS
 
PPTX
2025 CGI Congres - Surviving agile v05.pptx
Derk-Jan de Grood
 
PPTX
Numerical-Solutions-of-Ordinary-Differential-Equations.pptx
SAMUKTHAARM
 
PDF
3rd International Conference on Machine Learning and IoT (MLIoT 2025)
ClaraZara1
 
PPTX
澳洲电子毕业证澳大利亚圣母大学水印成绩单UNDA学生证网上可查学历
Taqyea
 
PPTX
Water Resources Engineering (CVE 728)--Slide 4.pptx
mohammedado3
 
PDF
Viol_Alessandro_Presentazione_prelaurea.pdf
dsecqyvhbowrzxshhf
 
PDF
MODULE-5 notes [BCG402-CG&V] PART-B.pdf
Alvas Institute of Engineering and technology, Moodabidri
 
PPTX
Distribution reservoir and service storage pptx
dhanashree78
 
PPTX
Biosensors, BioDevices, Biomediccal.pptx
AsimovRiyaz
 
PPTX
Worm gear strength and wear calculation as per standard VB Bhandari Databook.
shahveer210504
 
PDF
Reasons for the succes of MENARD PRESSUREMETER.pdf
majdiamz
 
PPT
Testing and final inspection of a solar PV system
MuhammadSanni2
 
PDF
AI TECHNIQUES FOR IDENTIFYING ALTERATIONS IN THE HUMAN GUT MICROBIOME IN MULT...
vidyalalltv1
 
PPTX
MODULE 05 - CLOUD COMPUTING AND SECURITY.pptx
Alvas Institute of Engineering and technology, Moodabidri
 
PDF
Electrical Engineer operation Supervisor
ssaruntatapower143
 
PDF
Halide Perovskites’ Multifunctional Properties: Coordination Engineering, Coo...
TaameBerhe2
 
PPTX
Mechanical Design of shell and tube heat exchangers as per ASME Sec VIII Divi...
shahveer210504
 
PDF
methodology-driven-mbse-murphy-july-hsv-huntsville6680038572db67488e78ff00003...
henriqueltorres1
 
fatigue in aircraft structures-221113192308-0ad6dc8c.pptx
aviatecofficial
 
Pressure Measurement training for engineers and Technicians
AIESOLUTIONS
 
2025 CGI Congres - Surviving agile v05.pptx
Derk-Jan de Grood
 
Numerical-Solutions-of-Ordinary-Differential-Equations.pptx
SAMUKTHAARM
 
3rd International Conference on Machine Learning and IoT (MLIoT 2025)
ClaraZara1
 
澳洲电子毕业证澳大利亚圣母大学水印成绩单UNDA学生证网上可查学历
Taqyea
 
Water Resources Engineering (CVE 728)--Slide 4.pptx
mohammedado3
 
Viol_Alessandro_Presentazione_prelaurea.pdf
dsecqyvhbowrzxshhf
 
MODULE-5 notes [BCG402-CG&V] PART-B.pdf
Alvas Institute of Engineering and technology, Moodabidri
 
Distribution reservoir and service storage pptx
dhanashree78
 
Biosensors, BioDevices, Biomediccal.pptx
AsimovRiyaz
 
Worm gear strength and wear calculation as per standard VB Bhandari Databook.
shahveer210504
 
Reasons for the succes of MENARD PRESSUREMETER.pdf
majdiamz
 
Testing and final inspection of a solar PV system
MuhammadSanni2
 
AI TECHNIQUES FOR IDENTIFYING ALTERATIONS IN THE HUMAN GUT MICROBIOME IN MULT...
vidyalalltv1
 
MODULE 05 - CLOUD COMPUTING AND SECURITY.pptx
Alvas Institute of Engineering and technology, Moodabidri
 
Electrical Engineer operation Supervisor
ssaruntatapower143
 
Halide Perovskites’ Multifunctional Properties: Coordination Engineering, Coo...
TaameBerhe2
 
Mechanical Design of shell and tube heat exchangers as per ASME Sec VIII Divi...
shahveer210504
 
methodology-driven-mbse-murphy-july-hsv-huntsville6680038572db67488e78ff00003...
henriqueltorres1
 

Greedy Algorithm - Knapsack Problem

  • 2. OPTIMIZATION PROBLEM (Cont.)  An optimization problem:  Given a problem instance, a set of constraints and an objective function.  Find a feasible solution for the given instance.  either maximum or minimum depending on the problem being solved.  constraints specify the limitations on the required solutions.
  • 3. SOLUTION FOR OPTIMIZATION PROBLEM For some optimization problems, • Dynamic Programming is “overkill” • Greedy Strategy is simpler and more efficient .
  • 4. DYNAMIC PROGRAMMING VS GREEDY Dynamic Programming Greedy Algorithm At each step, the choice is determined based on solutions of sub problems. At each step, we quickly make a choice that currently looks best. A local optimal (greedy) choice Sub-problems are solved first. Greedy choice can be made first before solving further sub- problems. Bottom-up approach Top-down approach Can be slower, more complex Usually faster, simpler
  • 5.  Characteristics of greedy algorithm:  make a sequence of choices  each choice is the one that seems best so far, only depends on what's been done so far  choice produces a smaller problem to be solved GREEDY METHOD
  • 6. PHASES OF GREEDY ALGORITHM  A greedy algorithm works in phases.  At each phase:  takes the best solution right now, without regard for future consequences  choosing a local optimum at each step, and end up at a global optimum solution.
  • 7. KNAPSACK PROBLEM There are two version of knapsack problem 1. 0-1 knapsack problem:  Items are indivisible. (either take an item or not)  can be solved with dynamic programming. 2. Fractional knapsack problem:  Items are divisible. (can take any fraction of an item)  It can be solved in greedy method
  • 8. 0-1 KNAPSACK PROBLEM:  A thief robbing a store finds n items.  ith item: worth vi value of item and wi weight of item  W, wi, vi are integers.  He can carry at most W pounds.
  • 9. FRACTIONAL KNAPSACK PROBLEM:  A thief robbing a store finds n items.  ith item: worth vi value of item and wi weight of item  W, wi, vi are integers.  He can carry at most W pounds.  He can take fractions of items.
  • 10. 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
  • 11. Initialization:  Sort the n objects from large to small based on their ratios vi / wi .  We assume the arrays w[1..n] and v[1..n] store the respective weights and values after sorting.  initialize array x[1..n] to zeros.  weight = 0; i = 1; THE OPTIMAL KNAPSACK ALGORITHM
  • 12. while (i  n and weight < W) do if weight + w[i]  W then x[i] = 1 else x[i] = (W – weight) / w[i] weight = weight + x[i] * w[i] i++ THE OPTIMAL KNAPSACK ALGORITHM
  • 13. KNAPSACK - EXAMPLE Problem: n = 3 W= 20 (v1, v2, v3) = (25, 24, 15) (w1, w2, w3) = (18, 15, 10)
  • 14. Solution:  Optimal solution:  x1 = 0  x2 = 1  x3 = 1/2  Total profit = 24 + 7.5 = 31.5 KNAPSACK - EXAMPLE