Eedy Algorithms
Eedy Algorithms
Greedy algorithms
ece.uwaterloo.ca
[email protected]
Algorithm Design
Greedy algorithms
Greedy algorithms
Making change
Making change
Total €0.50
Greedy algorithms
7
Making change
Total €0.70
Greedy algorithms
8
Making change
Total €0.72
Greedy algorithms
9
Making change
Making change
Making change
Making change
Making change
Definition
Optimal example
Optimal example
if ( m == 0 || r < m ) {
greedy( r, coins, rep2, n );
int sum1 = 0; for ( int k = 0; k < n; ++k ) sum1 += rep1[k];
int sum2 = 0; for ( int k = 0; k < n; ++k ) sum2 += rep2[k];
return m;
}
Jeffrey Shallit, What this Country Needs is an 18¢ Piece.
Greedy algorithms
20
Unfeasible example
Unfeasible example
Unfeasible example
Neither 1 nor 2 fits into the first empty square, so we fill it with 3
Greedy algorithms
23
Unfeasible example
Unfeasible example
Unfeasible example
Unfeasible example
Suppose you want to cycle through n cities without visiting the same
city twice
The Traveling Salesman Problem
– It is possible to go from any one city to another
Linear programming
Linear programming
Linear programming
Optimal substructure
Near-optimal algorithms
We have seen:
– A greedy change algorithm which works under certain conditions
– Prim’s and Dijkstra’s algorithms which are greedy and find the optimal
solution
– A naïve greedy algorithm which attempts (and fails) to solve Sudoku
– The nearest neighbor algorithm is unlikely to find the optimal solution
Next, we will see a greedy algorithm which finds a feasible, but not
necessarily an optimal, solution
Greedy algorithms
33
Project management
0/1 knapsack problem
Situation:
– The next cycle for a given product is 26 weeks
– We have ten possible projects which could be completed in that time,
each with an expected number of weeks to complete the project and an
expected increase in revenue
A 15 210
B 12 220
C 10 180
D 9 120
E 8 160
F 7 170
G 5 90
H 4 40
J 3 60
K 1 10
Greedy algorithms
36
Project management
0/1 knapsack problem
Let us first try to find an optimal schedule by trying to be as
productive as possible during the 26 weeks:
– we will start with the projects in order from most time to least time, and
at each step, select the longest-running project which does not put us
over 26 weeks
– we will be able to fill in the gaps with the smaller projects
Greedy algorithms
37
Project management
0/1 knapsack problem
Greedy-by-time (make use of all 26 wks):
– Project A: 15 wks
– Project C: 10 wks Product Completion Expected Revenue
– Project J: 1 wk ID Time (wks) (1000 $)
A 15 210
Total time: 26 wks B 12 220
C 10 180
Expected revenue: D 9 120
$400 000 E 8 160
F 7 170
G 5 90
H 4 40
I 3 60
J 1 10
Greedy algorithms
38
Project management
0/1 knapsack problem
Next, let us attempt to find an optimal schedule by starting with the
most :
– we will start with the projects in order from most time to least time, and
at each step, select the longest-running project which does not put us
over 26 weeks
– we will be able to fill in the gaps with the smaller projects
Greedy algorithms
39
Project management
0/1 knapsack problem
Greedy-by-revenue (best-paying projects):
– Project B: $220K
– Project C: $180K Product Completion Expected Revenue
– Project H: $ 60K ID Time (wks) (1000 $)
– Project K: $ 10K B 12 220
A 15 210
Total time: 26 wks C 10 180
F 7 170
Expected revenue: E 8 160
$470 000
D 9 120
G 5 90
J 3 60
H 4 40
K 1 10
Greedy algorithms
40
Project management
0/1 knapsack problem
Unfortunately, either of these techniques focuses on projects which
have high projected revenues or high run times
– The run time is Q(n ln(n)) — the time required to sort the list
– Later, we will see a dynamic program for finding an optimal solution with
one additional constraint
Greedy algorithms
45
Project management
0/1 knapsack problem
Of course, in reality, there are numerous other factors affecting
projects, including:
– Flexible deadlines (if a delay by a week would result in a significant
increase in expected revenue, this would be acceptable)
– Probability of success for particular projects
– The requirement for banner projects
• Note that greedy-by-revenue-density had none of the larger projects
Process scheduling
Process scheduling
Multiprogramming
– running processes in batches
Cooperative multitasking/time-sharing
– processes voluntarily (through a system command) give up the
processor
– this requires careful programming...good luck!
– some of you may remember Windows 3.1
Greedy algorithms
48
Process scheduling
Preemptive multitasking/time-sharing
– the operating system controls access to the processor
– processes may be preempted (time slices, interrupts)
Real Time
– addition of priorities, guarantees, etc.
Greedy algorithms
49
Process scheduling
Suppose we want to minimize the total wait time for the processes
to complete
Greedy algorithms
50
Process scheduling
Process scheduling
Process scheduling
Process scheduling
http://www.ehow.com/
Greedy algorithms
54
Process scheduling
Process scheduling
Process scheduling
( N k 1)t
k 1
ik
This is constant
This may change:
3×7+4×5<3×5+4×7
Greedy algorithms
57
Process scheduling
kt
k 1
ik
j ti j tik k j tik
– The first term does not depend on the order of t or t , but the second
ij ik
does
– (k – j) > 0 and to maximize the second term, we require t t
ik ij
Thus must be true for all pairs, thus ti1 ti2 ti3 tiN
Greedy algorithms
58
Process scheduling
Process scheduling
Process scheduling
Process scheduling
Process scheduling
The total wait time is 156 ms and therefore the average wait time is
156 ms/9 = 17.333 ms
Greedy algorithms
63
Process scheduling
Process scheduling
Interval scheduling
Interval scheduling
The first thought may be to always run that process that is next
ready to run
– A little thought, however, quickly demonstrates that this fails
– The worst case would be to only run 1 out of n possible processes when
n – 1 processes could have been run
Greedy algorithms
67
Interval scheduling
Interval scheduling
Process Interval
Consider the following list of 12 processes
together with the time interval during which A 5 – 8
they must be run B 10 – 13
– Find the optimal schedule with the earliest- C 6 – 9
deadline-first greedy algorithm D 12 – 15
E 3 – 7
F 8 – 11
G 1 – 6
H 8 – 12
J 3 – 5
K 2 – 4
L 11 – 16
M 10 – 15
Greedy algorithms
69
Interval scheduling
Process Interval
In order to simplify this, sort the processes
on their end times K 2 – 4
J 3 – 5
G 1 – 6
E 3 – 7
A 5 – 8
C 6 – 9
F 8 – 11
H 8 – 12
B 10 – 13
D 12 – 15
M 10 – 15
L 11 – 16
Greedy algorithms
70
Interval scheduling
Process Interval
To begin, choose Process K
K 2 – 4
J 3 – 5
G 1 – 6
E 3 – 7
A 5 – 8
C 6 – 9
F 8 – 11
H 8 – 12
B 10 – 13
D 12 – 15
M 10 – 15
L 11 – 16
Greedy algorithms
71
Interval scheduling
Process Interval
At this point, Process J, G and E can no
longer be run K 2 – 4
J 3 – 5
G 1 – 6
E 3 – 7
A 5 – 8
C 6 – 9
F 8 – 11
H 8 – 12
B 10 – 13
D 12 – 15
M 10 – 15
L 11 – 16
Greedy algorithms
72
Interval scheduling
Process Interval
Next, run Process A
K 2 – 4
J 3 – 5
G 1 – 6
E 3 – 7
A 5 – 8
C 6 – 9
F 8 – 11
H 8 – 12
B 10 – 13
D 12 – 15
M 10 – 15
L 11 – 16
Greedy algorithms
73
Interval scheduling
Process Interval
We can no longer run Process C
K 2 – 4
J 3 – 5
G 1 – 6
E 3 – 7
A 5 – 8
C 6 – 9
F 8 – 11
H 8 – 12
B 10 – 13
D 12 – 15
M 10 – 15
L 11 – 16
Greedy algorithms
74
Interval scheduling
Process Interval
Next, we can run Process F
K 2 – 4
J 3 – 5
G 1 – 6
E 3 – 7
A 5 – 8
C 6 – 9
F 8 – 11
H 8 – 12
B 10 – 13
D 12 – 15
M 10 – 15
L 11 – 16
Greedy algorithms
75
Interval scheduling
Process Interval
This restricts us from running
Processes H, B and M K 2 – 4
J 3 – 5
G 1 – 6
E 3 – 7
A 5 – 8
C 6 – 9
F 8 – 11
H 8 – 12
B 10 – 13
D 12 – 15
M 10 – 15
L 11 – 16
Greedy algorithms
76
Interval scheduling
Process Interval
The next available process is D
K 2 – 4
J 3 – 5
G 1 – 6
E 3 – 7
A 5 – 8
C 6 – 9
F 8 – 11
H 8 – 12
B 10 – 13
D 12 – 15
M 10 – 15
L 11 – 16
Greedy algorithms
77
Interval scheduling
Process Interval
The prevents us from running Process L
– We are therefore finished K 2 – 4
J 3 – 5
G 1 – 6
E 3 – 7
A 5 – 8
C 6 – 9
F 8 – 11
H 8 – 12
B 10 – 13
D 12 – 15
M 10 – 15
L 11 – 16
Greedy algorithms
78
Process Interval
We have scheduled four processes
– The selection may not be unique K 2 – 4
J 3 – 5
G 1 – 6
E 3 – 7
Once the processes are sorted, the run time A 5 – 8
is linear—we simply look ahead to find the C 6 – 9
next process that can be run F 8 – 11
– Thus, the run time is the run time of sorting H 8 – 12
the B 10 – 13
D 12 – 15
M 10 – 15
L 11 – 16
Greedy algorithms
79
Process Interval
For example, we could have chosen
Process L K 2 – 4
J 3 – 5
G 1 – 6
E 3 – 7
Process Interval
We could add weights to the individual
processes K 2 – 4
J 3 – 5
G 1 – 6
E 3 – 7
A 5 – 8
– The weights could be the duration of
the processes—maximize processor usage C 6 – 9
– The weights could be revenue gained from F 8 – 11
the performance—maximize revenue H 8 – 12
B 10 – 13
We will see an efficient algorithm in the D 12 – 15
topic on dynamic programming M 10 – 15
L 11 – 16
Greedy algorithms
81
Summary
References
Wikipedia, http://en.wikipedia.org/wiki/Algorithm_design
These slides are provided for the ECE 250 Algorithms and Data Structures course. The
material in it reflects Douglas W. Harder’s best judgment in light of the information available to
him at the time of preparation. Any reliance on these course slides by any party for any other
purpose are the responsibility of such parties. Douglas W. Harder accepts no responsibility for
damages, if any, suffered by any party as a result of decisions made or actions based on these
course slides for any other purpose than that for which it was intended.