0% found this document useful (0 votes)
19 views25 pages

Lecture 6 (Greedy Approach) - Partial - Knapsack

Uploaded by

tusarrahamanaiub
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views25 pages

Lecture 6 (Greedy Approach) - Partial - Knapsack

Uploaded by

tusarrahamanaiub
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 25

Greedy Algorithm

Course Code: CSC2211 Course Title: Algorithms

Dept. of Computer Science


Faculty of Science and Technology

Lecturer No: 06 Week No: 06 Semester: Summer 22-23


Lecturer: Pritam Khan Boni; [email protected]
Lecture Outline

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

• Solves an optimization problem


• For many optimization problems, greedy algorithm can be used. (not
always)
• Greedy algorithm for optimization problems typically go through a
sequence of steps, with a set of choices at each step. Current choice
does not depend on evaluating potential future choices or pre-solving
repeatedly occurring subproblems (a.k.a., overlapping subproblems).
With each step, the original problem is reduced to a smaller problem.
• Greedy algorithm always makes the choice that looks best at the
moment.
• It makes a locally optimal choice in the hope that this choice will lead
to a globally optimal solution.
Optimal Solution

What is an Optimal Solution?


o Given a problem, more than one solution
exist
5/

o
Licensed under CSE 221: Algorithms

One of the solution is the best based on


38

some given constraints, that solution is


called the optimal solution
What is Global Optimal Solution?
o Optimal Solution to the main problem

What is local Optimal


Example of Greedy Algorithms

• Activity Selection Problem


• Coin Changing Problem
• Job Scheduling Problem
• Fractional Knapsack Problem
• Dijkstra’s Shortest Path Problem
• Minimum Spanning Tree Problem
Fractional knapsack problem
Fractional knapsack problem
Item Benefit Weight
Capacity A 25 18 kg

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

Licensed under CSE 221: Algorithms

27 /
Fractional knapsack problem

Licensed under CSE 221: Algorithms

28 /
Fractional knapsack problem

11 /
Licensed under CSE 221: Algorithms
38
Greedy algorithms

Fractional knapsack in action

12 /
Licensed under CSE 221: Algorithms
38

Item Benefit Weight


A 140 2 kg
B 200 1 kg
C 150 5 kg
D 240 3 kg

Calculate benefit/kg – the value


index.
Greedy algorithms

Fractional knapsack in action

CSE 221: Algorithms

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

Sort by non-increasing value


index. 30 /
Greedy algorithms

Fractional knapsack in action

Licensed under CSE 221: Algorithms

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

Fractional knapsack in action

Licensed under CSE 221: 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

Fractional knapsack in action

Licensed under CSE 221: 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

Fractional knapsack in action

Licensed under CSE 221: 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

Fractional knapsack in action

Licensed under CSE 221: 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

Fractional knapsack in action

Licensed under CSE 221: 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

Licensed under CSE 221: Algorithms

38 /
Books

Introduction to Algorithms, Thomas H. Cormen, Charle E. Leiserson,


Ronald L. Rivest, Clifford Stein (CLRS).

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/

You might also like