SlideShare a Scribd company logo
1 of 15
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

daa-unit-3-greedy method
daa-unit-3-greedy methoddaa-unit-3-greedy method
daa-unit-3-greedy method
hodcsencet
 
Time and space complexity
Time and space complexityTime and space complexity
Time and space complexity
Ankit Katiyar
 

What's hot (20)

Np cooks theorem
Np cooks theoremNp cooks theorem
Np cooks theorem
 
Matrix chain multiplication
Matrix chain multiplicationMatrix chain multiplication
Matrix chain multiplication
 
daa-unit-3-greedy method
daa-unit-3-greedy methoddaa-unit-3-greedy method
daa-unit-3-greedy method
 
BackTracking Algorithm: Technique and Examples
BackTracking Algorithm: Technique and ExamplesBackTracking Algorithm: Technique and Examples
BackTracking Algorithm: Technique and Examples
 
Brute force method
Brute force methodBrute force method
Brute force method
 
Graph coloring problem
Graph coloring problemGraph coloring problem
Graph coloring problem
 
Branch and bound
Branch and boundBranch and bound
Branch and bound
 
Knapsack Problem (DP & GREEDY)
Knapsack Problem (DP & GREEDY)Knapsack Problem (DP & GREEDY)
Knapsack Problem (DP & GREEDY)
 
The n Queen Problem
The n Queen ProblemThe n Queen Problem
The n Queen Problem
 
Complexity analysis in Algorithms
Complexity analysis in AlgorithmsComplexity analysis in Algorithms
Complexity analysis in Algorithms
 
P, NP, NP-Complete, and NP-Hard
P, NP, NP-Complete, and NP-HardP, NP, NP-Complete, and NP-Hard
P, NP, NP-Complete, and NP-Hard
 
Graph coloring using backtracking
Graph coloring using backtrackingGraph coloring using backtracking
Graph coloring using backtracking
 
Time and space complexity
Time and space complexityTime and space complexity
Time and space complexity
 
Dijkstra's Algorithm
Dijkstra's Algorithm Dijkstra's Algorithm
Dijkstra's Algorithm
 
Sum of subset problem.pptx
Sum of subset problem.pptxSum of subset problem.pptx
Sum of subset problem.pptx
 
Job sequencing with Deadlines
Job sequencing with DeadlinesJob sequencing with Deadlines
Job sequencing with Deadlines
 
SINGLE-SOURCE SHORTEST PATHS
SINGLE-SOURCE SHORTEST PATHS SINGLE-SOURCE SHORTEST PATHS
SINGLE-SOURCE SHORTEST PATHS
 
Topological Sorting
Topological SortingTopological Sorting
Topological Sorting
 
Backtracking
BacktrackingBacktracking
Backtracking
 
Greedy algorithm
Greedy algorithmGreedy algorithm
Greedy algorithm
 

Viewers also liked

0 1 knapsack problem using dynamic programming
0 1 knapsack problem using dynamic programming0 1 knapsack problem using dynamic programming
0 1 knapsack problem using dynamic programming
Maher Alshammari
 
DESIGN AND ANALYSIS OF ALGORITHMS
DESIGN AND ANALYSIS OF ALGORITHMSDESIGN AND ANALYSIS OF ALGORITHMS
DESIGN AND ANALYSIS OF ALGORITHMS
Gayathri Gaayu
 
01 knapsack using backtracking
01 knapsack using backtracking01 knapsack using backtracking
01 knapsack using backtracking
mandlapure
 

Viewers also liked (13)

Materi 4 penyelesaian spl tiga atau lebih variabel
Materi 4 penyelesaian spl tiga atau lebih variabelMateri 4 penyelesaian spl tiga atau lebih variabel
Materi 4 penyelesaian spl tiga atau lebih variabel
 
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
 
program pencarian data dengan bahasa C
program pencarian data dengan bahasa Cprogram pencarian data dengan bahasa C
program pencarian data dengan bahasa C
 
0 1 knapsack problem using dynamic programming
0 1 knapsack problem using dynamic programming0 1 knapsack problem using dynamic programming
0 1 knapsack problem using dynamic programming
 
Knapsack problem using dynamic programming
Knapsack problem using dynamic programmingKnapsack problem using dynamic programming
Knapsack problem using dynamic programming
 
DESIGN AND ANALYSIS OF ALGORITHMS
DESIGN AND ANALYSIS OF ALGORITHMSDESIGN AND ANALYSIS OF ALGORITHMS
DESIGN AND ANALYSIS OF ALGORITHMS
 
Genetic Algorithm based Approach to solve Non-Fractional (0/1) Knapsack Optim...
Genetic Algorithm based Approach to solve Non-Fractional (0/1) Knapsack Optim...Genetic Algorithm based Approach to solve Non-Fractional (0/1) Knapsack Optim...
Genetic Algorithm based Approach to solve Non-Fractional (0/1) Knapsack Optim...
 
Kruskal Algorithm
Kruskal AlgorithmKruskal Algorithm
Kruskal Algorithm
 
Knapsack problem
Knapsack problemKnapsack problem
Knapsack problem
 
Knapsack
KnapsackKnapsack
Knapsack
 
01 knapsack using backtracking
01 knapsack using backtracking01 knapsack using backtracking
01 knapsack using backtracking
 
Greedy algorithm
Greedy algorithmGreedy algorithm
Greedy algorithm
 
Knapsack problem using fixed tuple
Knapsack problem using fixed tupleKnapsack problem using fixed tuple
Knapsack problem using fixed tuple
 

Similar to Greedy Algorithm - 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
QurbanAli72
 
Mastering Greedy Algorithms: Optimizing Solutions for Efficiency"
Mastering Greedy Algorithms: Optimizing Solutions for Efficiency"Mastering Greedy Algorithms: Optimizing Solutions for Efficiency"
Mastering Greedy Algorithms: Optimizing Solutions for Efficiency"
22bcs058
 
knapsackusingbranchandbound
knapsackusingbranchandboundknapsackusingbranchandbound
knapsackusingbranchandbound
hodcsencet
 
lecture 26
lecture 26lecture 26
lecture 26
sajinsc
 
Lecture34
Lecture34Lecture34
Lecture34
farazch
 
Parallel_Algorithms_In_Combinatorial_Optimization_Problems.ppt
Parallel_Algorithms_In_Combinatorial_Optimization_Problems.pptParallel_Algorithms_In_Combinatorial_Optimization_Problems.ppt
Parallel_Algorithms_In_Combinatorial_Optimization_Problems.ppt
dakccse
 

Similar to Greedy Algorithm - Knapsack Problem (20)

Dynamic programming
Dynamic programmingDynamic programming
Dynamic programming
 
Fractional Knapsack Problem
Fractional Knapsack ProblemFractional Knapsack Problem
Fractional Knapsack Problem
 
Greedymethod
GreedymethodGreedymethod
Greedymethod
 
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
 
Mastering Greedy Algorithms: Optimizing Solutions for Efficiency"
Mastering Greedy Algorithms: Optimizing Solutions for Efficiency"Mastering Greedy Algorithms: Optimizing Solutions for Efficiency"
Mastering Greedy Algorithms: Optimizing Solutions for Efficiency"
 
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
 
knapsackusingbranchandbound
knapsackusingbranchandboundknapsackusingbranchandbound
knapsackusingbranchandbound
 
lect
lectlect
lect
 
lecture 26
lecture 26lecture 26
lecture 26
 
lect
lectlect
lect
 
Lecture34
Lecture34Lecture34
Lecture34
 
Lecture34
Lecture34Lecture34
Lecture34
 
lec
leclec
lec
 
DynamicProgramming.pptx
DynamicProgramming.pptxDynamicProgramming.pptx
DynamicProgramming.pptx
 
Optimization problems
Optimization problemsOptimization problems
Optimization problems
 
Greedy method1
Greedy method1Greedy method1
Greedy method1
 
Approximation alogrithms
Approximation alogrithmsApproximation alogrithms
Approximation alogrithms
 
12 Greeddy Method
12 Greeddy Method12 Greeddy Method
12 Greeddy Method
 
Parallel_Algorithms_In_Combinatorial_Optimization_Problems.ppt
Parallel_Algorithms_In_Combinatorial_Optimization_Problems.pptParallel_Algorithms_In_Combinatorial_Optimization_Problems.ppt
Parallel_Algorithms_In_Combinatorial_Optimization_Problems.ppt
 
Parallel_Algorithms_In_Combinatorial_Optimization_Problems.ppt
Parallel_Algorithms_In_Combinatorial_Optimization_Problems.pptParallel_Algorithms_In_Combinatorial_Optimization_Problems.ppt
Parallel_Algorithms_In_Combinatorial_Optimization_Problems.ppt
 

More from Madhu Bala (10)

Internet of Things (IoT)
Internet of Things (IoT)Internet of Things (IoT)
Internet of Things (IoT)
 
Digital logic
Digital logicDigital logic
Digital logic
 
Operating system
Operating systemOperating system
Operating system
 
GRAPH APPLICATION - MINIMUM SPANNING TREE (MST)
GRAPH APPLICATION - MINIMUM SPANNING TREE (MST)GRAPH APPLICATION - MINIMUM SPANNING TREE (MST)
GRAPH APPLICATION - MINIMUM SPANNING TREE (MST)
 
Divide and conquer - Quick sort
Divide and conquer - Quick sortDivide and conquer - Quick sort
Divide and conquer - Quick sort
 
GPRS Technology
GPRS TechnologyGPRS Technology
GPRS Technology
 
Algorithm - Introduction
Algorithm - IntroductionAlgorithm - Introduction
Algorithm - Introduction
 
4G technology
4G technology4G technology
4G technology
 
Data structure - Graph
Data structure - GraphData structure - Graph
Data structure - Graph
 
Smoothing Filters in Spatial Domain
Smoothing Filters in Spatial DomainSmoothing Filters in Spatial Domain
Smoothing Filters in Spatial Domain
 

Recently uploaded

Online crime reporting system project.pdf
Online crime reporting system project.pdfOnline crime reporting system project.pdf
Online crime reporting system project.pdf
Kamal Acharya
 
Microkernel in Operating System | Operating System
Microkernel in Operating System | Operating SystemMicrokernel in Operating System | Operating System
Microkernel in Operating System | Operating System
Sampad Kar
 

Recently uploaded (20)

SLIDESHARE PPT-DECISION MAKING METHODS.pptx
SLIDESHARE PPT-DECISION MAKING METHODS.pptxSLIDESHARE PPT-DECISION MAKING METHODS.pptx
SLIDESHARE PPT-DECISION MAKING METHODS.pptx
 
Lab Manual Arduino UNO Microcontrollar.docx
Lab Manual Arduino UNO Microcontrollar.docxLab Manual Arduino UNO Microcontrollar.docx
Lab Manual Arduino UNO Microcontrollar.docx
 
Raashid final report on Embedded Systems
Raashid final report on Embedded SystemsRaashid final report on Embedded Systems
Raashid final report on Embedded Systems
 
Theory for How to calculation capacitor bank
Theory for How to calculation capacitor bankTheory for How to calculation capacitor bank
Theory for How to calculation capacitor bank
 
Online crime reporting system project.pdf
Online crime reporting system project.pdfOnline crime reporting system project.pdf
Online crime reporting system project.pdf
 
Diploma Engineering Drawing Qp-2024 Ece .pdf
Diploma Engineering Drawing Qp-2024 Ece .pdfDiploma Engineering Drawing Qp-2024 Ece .pdf
Diploma Engineering Drawing Qp-2024 Ece .pdf
 
Microkernel in Operating System | Operating System
Microkernel in Operating System | Operating SystemMicrokernel in Operating System | Operating System
Microkernel in Operating System | Operating System
 
Software Engineering - Modelling Concepts + Class Modelling + Building the An...
Software Engineering - Modelling Concepts + Class Modelling + Building the An...Software Engineering - Modelling Concepts + Class Modelling + Building the An...
Software Engineering - Modelling Concepts + Class Modelling + Building the An...
 
Linux Systems Programming: Semaphores, Shared Memory, and Message Queues
Linux Systems Programming: Semaphores, Shared Memory, and Message QueuesLinux Systems Programming: Semaphores, Shared Memory, and Message Queues
Linux Systems Programming: Semaphores, Shared Memory, and Message Queues
 
Filters for Electromagnetic Compatibility Applications
Filters for Electromagnetic Compatibility ApplicationsFilters for Electromagnetic Compatibility Applications
Filters for Electromagnetic Compatibility Applications
 
Geometric constructions Engineering Drawing.pdf
Geometric constructions Engineering Drawing.pdfGeometric constructions Engineering Drawing.pdf
Geometric constructions Engineering Drawing.pdf
 
Insurance management system project report.pdf
Insurance management system project report.pdfInsurance management system project report.pdf
Insurance management system project report.pdf
 
BORESCOPE INSPECTION for engins CFM56.pdf
BORESCOPE INSPECTION for engins CFM56.pdfBORESCOPE INSPECTION for engins CFM56.pdf
BORESCOPE INSPECTION for engins CFM56.pdf
 
Module-III Varried Flow.pptx GVF Definition, Water Surface Profile Dynamic Eq...
Module-III Varried Flow.pptx GVF Definition, Water Surface Profile Dynamic Eq...Module-III Varried Flow.pptx GVF Definition, Water Surface Profile Dynamic Eq...
Module-III Varried Flow.pptx GVF Definition, Water Surface Profile Dynamic Eq...
 
The battle for RAG, explore the pros and cons of using KnowledgeGraphs and Ve...
The battle for RAG, explore the pros and cons of using KnowledgeGraphs and Ve...The battle for RAG, explore the pros and cons of using KnowledgeGraphs and Ve...
The battle for RAG, explore the pros and cons of using KnowledgeGraphs and Ve...
 
Intelligent Agents, A discovery on How A Rational Agent Acts
Intelligent Agents, A discovery on How A Rational Agent ActsIntelligent Agents, A discovery on How A Rational Agent Acts
Intelligent Agents, A discovery on How A Rational Agent Acts
 
Involute of a circle,Square, pentagon,HexagonInvolute_Engineering Drawing.pdf
Involute of a circle,Square, pentagon,HexagonInvolute_Engineering Drawing.pdfInvolute of a circle,Square, pentagon,HexagonInvolute_Engineering Drawing.pdf
Involute of a circle,Square, pentagon,HexagonInvolute_Engineering Drawing.pdf
 
Research Methodolgy & Intellectual Property Rights Series 1
Research Methodolgy & Intellectual Property Rights Series 1Research Methodolgy & Intellectual Property Rights Series 1
Research Methodolgy & Intellectual Property Rights Series 1
 
Artificial Intelligence Bayesian Reasoning
Artificial Intelligence Bayesian ReasoningArtificial Intelligence Bayesian Reasoning
Artificial Intelligence Bayesian Reasoning
 
Lesson no16 application of Induction Generator in Wind.ppsx
Lesson no16 application of Induction Generator in Wind.ppsxLesson no16 application of Induction Generator in Wind.ppsx
Lesson no16 application of Induction Generator in Wind.ppsx
 

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