Lecture 4
Lecture 4
Algorithms
Greedy Algorithms
Slides derived from Dr. Wael Abulsadat lecture notes CSEN 707 Winter 2016
Greedy Algorithms
For an optimization problem maximize (or minimize)
2
Counting Money Problem
The problem is to count a certain amount of
money, using the fewest possible bills and coins
(after all who wants to carry too much change in
his pockets)
Available bills and coins
1¢ , 10¢ , 25¢ , $1 , $5, $10, $20, $50, $100
3
Counting Money Problem
The greedy approach select the highest bill/coin
that would not lead to exceeding the desired
amount of money (the intuition is that this should
decrease the number of bills/coins required)
Available bills and coins
1¢ , 10¢ , 25¢ , $1 , $5, $10, $20, $50, $100,
Desired $7.47
$5+$1+$1+25¢+10¢ +10¢+1¢ +1¢
Eights bills and coins
4
Counting Money Problem
Assume a hypothetical monetary system with bills
of 1, 7, 10
How to count 15 from these bills
Greedy Method 10+1+1+1+1+1 (Six bills)
NOT OPTIMAL
An OPTIMAL solution is 7+7+1 (Three bills)
5
Optimality of the Greedy Method
The greedy method does not necessary lead to
the optimal solution
6
Scheduling Problem
You have to run nine jobs, with running times of
3, 5, 6, 10, 11, 14, 15, 18, and 20 minutes
You have 3 processors on which you can run
these jobs
7
Scheduling Problem
Two choices we need to make
Which is the next job to schedule and
Which processor to assign this job to
8
Longest-to-Shortest Job Sequence
20, 18, 15, 14, 11, 10, 6 , 5, 3
20 10 3
18 11 5
15 14 6
Sol=36
9
Shortest-to-Longest Job Sequence
3, 5, 6, 10, 11, 14, 15, 18, 20
3 10 15
5 11 18
6 14 20
Sol=40
10
Optimal Solution
3, 5, 6, 10, 11, 14, 15, 18, 20
20 14
18 11 5
15 10 6 3
Sol=34
11