Algorithm: Design and Analysis
Algorithm: Design and Analysis
Greedy Algorithm
Dr. Khaled W. Mahmoud
xxx
1
Agenda
Introduction
Problems
1. Knapsack
2. Activity Selection
2
Introduction to Greedy Algorithm
Simple approach
Used for optimization problems
Idea:
3
Introduction to Greedy Algorithm
Top down solution, problems decrease in size
Greedy algorithms don’t always yield an
optimal solution
4
Introduction: Example
Making Change Example
Problem: Find the minimum # of quarters(25), dimes
5
Introduction: Example
Amount = 92¢
25¢ 25¢ 25¢ 25¢ 25¢ 25¢ 25¢
25¢ 25¢ 25¢
10¢ 10¢ 10¢ 10¢ 10¢ 10¢ 10¢
10¢ 10¢ 10¢
5¢ 5¢ 5¢ 5¢ 5¢ 5¢ 5¢ 5¢
5¢ 5¢
1¢ 1¢ 1¢ 1¢ 1¢ 1¢ 1¢ 1¢
1¢ 1¢
6
Introduction: Example
Problem:
◦ Find the minimum # of 4, 3, and 1 cent coins to make up 6
cents.
Greedy Choice:
◦ Start by grabbing a 4 coin.
Consequences:
◦ 4+1+1 = 6 solution but not optimal
◦ 3+3=6 better solution
8
Fractional knapsack problem
Greedy property: Pick the item with maximum value
per weight
v1 v2 vn
... 30
w1 w2 wn --- JD80
Item 3 20
+
Item 2
50 20 JD100
Item 1 30
20 +
10 10 JD60
10
Fractional Knapsack Problem: Alg
Running Time:
◦ O(n lg n) for sorting items by value per pound
◦ O(n) cost for “For Loop”
◦ Overall cost = O(n lg n)
Fractional Knapsack - Example
Item Weight Value v/w Weight limit W = 10
i w v
1 2 2 1
2 4 3 0.75
3 3 3 1
4 5 6 1.2
5 2 4 2
6 6 9 1.5
Fractional Knapsack - Example
item Weight Value v/w Weight limit: 10
i w v ◦ Load=0, profit=0
5 2 4 2 Take item 5
6 6 9 1.5 ◦ 2 Kg, jd 4
Take item 6
4 5 6 1.2
◦ 8 Kg, jd 13 (4+6*1.5)
1 2 2 1 Take 2 Kg of item 4
3 3 3 1 ◦ 10 Kg, jd 15.4
(13+2*1.2)
2 4 3 0.75
Problems
The Fractional knapsack problem
The 0-1 knapsack problem
Activity Selection problem
14
0-1 knapsack problem: Greedy Sol
Greedy property: Pick the item with maximum value per weight
Item 3
Item 2
30 50 50
Item 1 20 jd100
20
10 +
JD100 JD120 10 jd60 30 jd120
JD 60
Item 3
20 jd100
Item 2
+
30 50
Item 1
20
10 30 jd120
JD 60 JD100 JD120
This greedy property gives the optimal solution for this instance with
value equal to jd 220.
16
0-1 knapsack problem: Greedy Sol
Greedy property: Pick the item with maximum value
Item 3
Item 2
10 jd60
50 50 50
Item 1 50
+
20 jd120
10 20 jd100
JD 60 JD100 JD120
jd120 jd160
JD6/KG JD5/KG JD 4/KG
This greedy property does not gives the optimal solution for this
instance.
17
Problems
The fractional knapsack problem
The 0-1 knapsack problem
Activity Selection problem
18
Activity Selection Problem
Problem: Schedule n activities that require exclusive
use of a common resource
◦ find max # of non-overlapping activities
Problem Description
◦ S = {a1, . . . , an} – set of activities
◦ ai needs resource during period [si , fi)
◦ si = start time and fi = finish time of activity ai
◦ 0 si < f i <
◦ Activities ai and aj are compatible if the intervals [si , fi) and
[sj, fj) do not overlap
19
Activity Selection Problem: Example
Possible Greedy strategy is select event with minimum
duration i 1 2 3 4 5 6 7 8 9
si 2 2 4 1 5 8 9 11 13
fi 3 5 7 8 9 10 11 14 16
Di 1 3 3 7 4 2 2 3 3
i 1 2 3 4 5 6 7 8 9
si 2 2 4 1 5 8 9 11 13
fi 3 5 7 17 9 10 11 14 16
21
Activity Selection Problem: Example
Possible Greedy strategy is select event Conflicting
with the Fewest Other Events
Not working: Counter Example
22
Activity Selection Problem : Example
Possible Greedy strategy is select event with minimum finish
time i 1 2 3 4 5 6 7 8 9
si 2 2 4 1 5 8 9 11 13
fi 3 5 7 8 9 10 11 14 16
23
Activity Selection Problem
Greedy strategies:
• Minimum Duration: Not Optimal
• Minimum Finish Time: Optimal
• Maximum Start Time: Optimal
• Minimum Start Time : Not Optimal
24
Activity Selection Problem: Alg
// Using minimum finish time greedy algorithm strategy.
Activity selection(s, f, n)
Sort activities in both arrays by minimum finish time.
A = A U {a1}
k=1 s: array of start time.
f: array of finish time.
for i=2……..n n: number of the activities.
if s[i] >= f[k] A: Solution Set contains the selected activities
k: the index of the last taken activity.
A = A U {ai}
k=i
Return A
25
Activity Selection Problem: Alg
It takes the start and finish times of the
activities , represented as arrays s and f
The variable k indexes the most recent
addition to A
Since the activities are sorted by finish time,
28
Now prove optimal substructure.
◦ Once we make the first greedy choice, now the problem is S’ = {i S: si
≥ f1) , with optimal solution A' = A - {1}.
◦ This solution must be optimal.
◦ If B' solves S' with more activities than A', adding activity 1 to B' makes
it bigger than A, which is a contradiction.
◦ Therefore, greedy choice yields an optimal solution.
29
When does Greedy Work?
Two key ingredients:
1. Optimal sub-structure
◦ An optimal solution to the entire problem
contains within it optimal solutions to sub-
problems (this is also true of dynamic
programming)
2. Greedy choice property