Lecture 6 (Greedy Approach) - Partial - Knapsack
Lecture 6 (Greedy Approach) - Partial - Knapsack
1. Optimization Problem
2. Greedy Algorithm.
3. Fractional knapsack Problem.
Optimization Problems
• An optimization problem is one in which you want to find, not just a solution, but the
best solution
Example
• Graph coloring optimization problem given an undirected graph, G= (V,E), what is the minimum
number of colors required to assign “colors” to each vertex in such a way that no two adjacent
vertices have the same color
• Path optimization problem: Find the shortest path(s) from u to v in a given graph G.
Find all the
a b shortest paths
from a to d:
a →b →d
a →c →d
d
c
Greedy Algorithm
o
Licensed under CSE 221: Algorithms
W=20 B 15 10 kg
C 24 15 kg
Fractional knapsack problem
Item Benefit Weight
A 25 18 kg
B 15 10 kg
C 24 15 kg
27 /
Fractional knapsack problem
28 /
Fractional knapsack problem
11 /
Licensed under CSE 221: Algorithms
38
Greedy algorithms
12 /
Licensed under CSE 221: Algorithms
38
Value
Item Benefit Weight index
A 140 2 kg 70
B 200 1 kg 200
C 150 5 kg 30
D 240 3 kg 80
Value
Item Benefit Weight index
B 200 1 kg 200
D 240 3 kg 80
A 140 2 kg 70
C 150 5 kg 30
Maximum 5
weight: kg 30 /
Greedy algorithms
Value
Item Benefit Weight index Chosen
B 200 1 kg 200 0 kg
D 240 3 kg 80 0 kg
A 140 2 kg 70 0 kg
C 150 5 kg 30 0 kg
Maximum 5 Remainin 5 Benefi 0
weight: kg g: kg t: kg
30 /
Greedy algorithms
Value
Item Benefit Weight index Chosen
B 200 1 kg 200 1 kg
D 240 3 kg 80 0 kg
A 140 2 kg 70 0 kg
C 150 5 kg 30 0 kg
Maximum 5 Remainin 4 Benefi 200
weight: kg g: kg t: kg
30 /
Greedy algorithms
Value
Item Benefit Weight index Chosen
B 200 1 kg 200 1 kg
D 240 3 kg 80 3 kg
A 140 2 kg 70 0 kg
C 150 5 kg 30 0 kg
Maximum 5 Remainin 1 Benefi 440
weight: kg g: kg t: kg
30 /
Greedy algorithms
Value
Item Benefit Weight index Chosen
B 200 1 kg 200 1 kg
D 240 3 kg 80 3 kg
A 140 2 kg 70 1 kg
C 150 5 kg 30 0 kg
Maximum 5 Remainin 0 Benefi 510
weight: kg g: kg t: kg
30 /
Greedy algorithms
Value
Item Benefit Weight index Chosen
B 200 1 kg 200 1 kg
D 240 3 kg 80 3 kg
A 140 2 kg 70 1 kg
C 150 5 kg 30 0 kg
Maximum 5 Remainin 0 Benefi 510
weight: kg g: kg t: kg
30 /
Fractional knapsack greedy algorithm
Fractional knapsack algorithm
Running time:
Given a collection S of n items, such that each item i has a benefit b i
and weight wi, we can construct a maximum-benefit subset of S,
allowing for fractional amounts, that has a total weight W in
O(nlogn) time. (how?)
Use heap-based priority queue to store S
Removing the item with the highest value takes O(logn) time
In the worst case, need to remove all items
Exercise
Assume that we have a knapsack with max weight capacity, W = 16.
our objective is to fill the knapsack with items such that the benefit
(value or profit) is maximum.
Consider the following items and their associated weight and value
ITEM WEIGHT VALUE
i1 6 6
i2 10 2
i3 3 1
i4 5 8
i5 1 3
i6 3 5
Conclusion
38 /
Books
Fundamental of Computer Algorithms, Ellis Horowitz, Sartaj Sahni, Sanguthevar Rajasekaran (HSR)
References
https://www.tutorialspoint.com/data_structures_algorithms/greedy_algorithms.htm
https://www.geeksforgeeks.org/fractional-knapsack-problem/