SlideShare a Scribd company logo
2
Most read
4
Most read
7
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

PPTX
Knapsack problem using greedy approach
PPTX
Knapsack Problem (DP & GREEDY)
PPTX
Greedy algorithms
PPTX
Fractional Knapsack Problem
PPTX
Knapsack Problem
PPTX
daa-unit-3-greedy method
PPTX
Knapsack problem algorithm, greedy algorithm
PPTX
knapsack problem
Knapsack problem using greedy approach
Knapsack Problem (DP & GREEDY)
Greedy algorithms
Fractional Knapsack Problem
Knapsack Problem
daa-unit-3-greedy method
Knapsack problem algorithm, greedy algorithm
knapsack problem

What's hot (20)

PPT
Greedy Algorihm
PPTX
Lecture optimal binary search tree
PPTX
Backtracking-N Queens Problem-Graph Coloring-Hamiltonian cycle
PPTX
8 queens problem using back tracking
PPT
0/1 knapsack
PPTX
Context free grammar
PPTX
NP completeness
PPTX
Strassen's matrix multiplication
PPTX
Single source Shortest path algorithm with example
PPT
BackTracking Algorithm: Technique and Examples
PPTX
Hashing Technique In Data Structures
PPTX
Webinar : P, NP, NP-Hard , NP - Complete problems
PPTX
The n Queen Problem
PPT
01 knapsack using backtracking
PPTX
8 queen problem
PPTX
Divide and conquer - Quick sort
PPT
Branch and bound
PPTX
01 Knapsack using Dynamic Programming
PPTX
Backtracking
Greedy Algorihm
Lecture optimal binary search tree
Backtracking-N Queens Problem-Graph Coloring-Hamiltonian cycle
8 queens problem using back tracking
0/1 knapsack
Context free grammar
NP completeness
Strassen's matrix multiplication
Single source Shortest path algorithm with example
BackTracking Algorithm: Technique and Examples
Hashing Technique In Data Structures
Webinar : P, NP, NP-Hard , NP - Complete problems
The n Queen Problem
01 knapsack using backtracking
8 queen problem
Divide and conquer - Quick sort
Branch and bound
01 Knapsack using Dynamic Programming
Backtracking
Ad

Viewers also liked (12)

PDF
Materi 4 penyelesaian spl tiga atau lebih variabel
PPTX
Greedy algorithms -Making change-Knapsack-Prim's-Kruskal's
DOCX
program pencarian data dengan bahasa C
PPTX
0 1 knapsack problem using dynamic programming
PPT
Knapsack problem using dynamic programming
PPT
DESIGN AND ANALYSIS OF ALGORITHMS
PPT
Genetic Algorithm based Approach to solve Non-Fractional (0/1) Knapsack Optim...
PPTX
Kruskal Algorithm
PPT
Knapsack problem
PPTX
Knapsack
PPTX
Greedy algorithm
PPT
Knapsack problem using fixed tuple
Materi 4 penyelesaian spl tiga atau lebih variabel
Greedy algorithms -Making change-Knapsack-Prim's-Kruskal's
program pencarian data dengan bahasa C
0 1 knapsack problem using dynamic programming
Knapsack problem using dynamic programming
DESIGN AND ANALYSIS OF ALGORITHMS
Genetic Algorithm based Approach to solve Non-Fractional (0/1) Knapsack Optim...
Kruskal Algorithm
Knapsack problem
Knapsack
Greedy algorithm
Knapsack problem using fixed tuple
Ad

Similar to Greedy Algorithm - Knapsack Problem (20)

PPTX
Fractional knapsack class 13
PPTX
Fractional knapsack problem
PDF
Comparative analysis-of-dynamic-and-greedy-approaches-for-dynamic-programming
PPT
Design and analysis of Algorithms - Lecture 15.ppt
PPTX
ppt 2.pptxlmkgngxjgdjgdjtxjgxjgxnvndjcgxjxjjc
PPTX
0-1 Knapsack Problem: Optimizing Value with Constraints
PPTX
Module 3_Greedy Technique_2021 Scheme.pptx
PPTX
Dynamic Programming-Knapsack Problem
PPT
Knapsack problem and Memory Function
PPT
DynProg_Knapsack.ppt
PPTX
5b.Greedy Technique - Fractional Knapsack+Coin change Problem.pptx
PPTX
0 1 knapsack using naive recursive approach and top-down dynamic programming ...
PPT
PPTX
Daa:Dynamic Programing
PDF
12 Greeddy Method
PPTX
0/1 DYNAMIC PROGRAMMING KNAPSACK PROBLEM
PPTX
Knapsack Problem Data Structure and Algorithm
PPT
Elak3 need of greedy for design and analysis of algorithms.ppt
PPTX
Ms nikita greedy agorithm
PPTX
Knapsack problem
Fractional knapsack class 13
Fractional knapsack problem
Comparative analysis-of-dynamic-and-greedy-approaches-for-dynamic-programming
Design and analysis of Algorithms - Lecture 15.ppt
ppt 2.pptxlmkgngxjgdjgdjtxjgxjgxnvndjcgxjxjjc
0-1 Knapsack Problem: Optimizing Value with Constraints
Module 3_Greedy Technique_2021 Scheme.pptx
Dynamic Programming-Knapsack Problem
Knapsack problem and Memory Function
DynProg_Knapsack.ppt
5b.Greedy Technique - Fractional Knapsack+Coin change Problem.pptx
0 1 knapsack using naive recursive approach and top-down dynamic programming ...
Daa:Dynamic Programing
12 Greeddy Method
0/1 DYNAMIC PROGRAMMING KNAPSACK PROBLEM
Knapsack Problem Data Structure and Algorithm
Elak3 need of greedy for design and analysis of algorithms.ppt
Ms nikita greedy agorithm
Knapsack problem

More from Madhu Bala (9)

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

Recently uploaded (20)

PDF
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
PPTX
Geodesy 1.pptx...............................................
PPTX
Artificial Intelligence
PPTX
additive manufacturing of ss316l using mig welding
PPTX
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
PPT
Mechanical Engineering MATERIALS Selection
PDF
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
PDF
PPT on Performance Review to get promotions
PDF
Embodied AI: Ushering in the Next Era of Intelligent Systems
PPT
Introduction, IoT Design Methodology, Case Study on IoT System for Weather Mo...
PPTX
Sustainable Sites - Green Building Construction
PDF
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
PDF
composite construction of structures.pdf
PPTX
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
PDF
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
PDF
Unit I ESSENTIAL OF DIGITAL MARKETING.pdf
PDF
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
PPTX
web development for engineering and engineering
PPTX
Safety Seminar civil to be ensured for safe working.
PPTX
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
Geodesy 1.pptx...............................................
Artificial Intelligence
additive manufacturing of ss316l using mig welding
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
Mechanical Engineering MATERIALS Selection
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
PPT on Performance Review to get promotions
Embodied AI: Ushering in the Next Era of Intelligent Systems
Introduction, IoT Design Methodology, Case Study on IoT System for Weather Mo...
Sustainable Sites - Green Building Construction
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
composite construction of structures.pdf
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
Unit I ESSENTIAL OF DIGITAL MARKETING.pdf
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
web development for engineering and engineering
Safety Seminar civil to be ensured for safe working.
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx

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