Algorithms
Introduction to Greedy Algorithms
cs333/cutler Greedy 1
The Greedy Technique(Method)
⃝ Make a greedy choice.
⃝ Prove that there exists an optimal solution(among
many) that uses greedy choice
⃝ Prove the optimal substructure property.
⃝ Counter example that shows that the greedy
algorithm does not provide an optimal solution.
⃝ Write down the Pseudocode of the algorithm
2
Elements of the Greedy
Strategy
Cast problem as one in which we make a
greedy choice and are left with one
subproblem to solve.
To show optimality:
1. Prove there is always an optimal
solution to original problem that
makes the greedy choice.
cs333/cutler Greedy 3
Elements of the Greedy
Strategy
2. Demonstrate that what remains is a
subproblem with property:
If we combine the optimal solution of the
subproblem with the greedy choice we
have an optimal solution to original
problem.
cs333/cutler Greedy 4
Activity Selection
⃝ Given a set S of n activities with start time
si and finish time fi of activity I
⃝ Find a maximum size subset A of compatible
activities (maximum number of activities).
⃝ Activities are compatible if they do not
overlap
⃝ Can you suggest a greedy choice?
cs333/cutler Greedy 5
Example
Activities 11 13
1
2 2 12
3 3 10
4
5 11 15
3 7
6
1 4
7
0 2
Time
0 1 2 3 4 5 cs333/cutler
6 7 8 9Greedy
10 11 12 13 14 15 6
Counter Example 1
• Select by start time
Activities
1 11 15
2
1 4
3
0 15
Time
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
cs333/cutler Greedy 7
Counter Example 2
• Select by minimum duration
Activities
1 8 15
2
1 8
3 7 9
Time
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
cs333/cutler Greedy 8
Select by finishing time
Activities
1 11 13
2 2 12
3 3 10
4
5 11 15
3 7
6
1 4
7
0 2
Time
0 1 2 3 4 5 cs333/cutler
6 7 8 9Greedy
10 11 12 13 14 15 9
Activity Selection
⃝ Assume without loss of generality that we
number the intervals in order of finish time. So
f1£...£fn.
⃝ Greedy choice: choose activity with minimum
finish time
⃝ The following greedy algorithm starts with
A={1} and then adds all compatible jobs.
(Theta(n))
⃝ Theta(nlogn) when including sort
cs333/cutler Greedy 10
Greedy-Activity-Selector(s,f)
1. n <- length[s] // number of activities
2. A <- {1}
3. j <- 1 //last activity added
4. for i <- 2 to n //select
5. if si >= fj then //compatible (feasible)
6. add {i} to A
7. j <- i //save new last activity
8. return A
cs333/cutler Greedy 11
Proof that greedy solution is
optimal
1. It is easy to see that there is an optimal solution to the problem
that makes the greedy choice.
Proof of 1.
Let A be an optimal solution. Let activity 1 be the greedy choice. If
1 A the proof is done. If 1 A, we will show that A’=A-{a}+{1} is
another optimal solution that includes 1.
Let a be the activity with minimum finish time in A.
Since activities are sorted by finishing time in the algorithm,
f(1) f(a). If f(1) s(a) we could add 1 to A and it could not be
optimal. So s(1) < f(a), and 1 and a overlap. Since f(1) f(a), if
we remove a and add 1 we get another compatible solution A’=A-
{a}+{1} and |A’|=|A|
cs333/cutler Greedy 12
Proof that greedy solution is
optimal
2. If we combine the optimal solution of the remaining subproblem with
the greedy choice we have an optimal solution to the original
problem.
Proof of 2.
Let activity 1 be the greedy choice.
Let S’ be the subset of activities that do not overlap with 1.
S’={i|i =1,…,n and si f(1)}.
Let B be an optimal solution for S’.
From the definition of S’, A’={1}+B is compatible, and a solution to
the original problem.
cs333/cutler Greedy 13
Proof that greedy solution is
optimal
2. If we combine the optimal solution of the remaining subproblem with
the greedy choice we have an optimal solution to the original
problem.
Proof of 2 continued.
The proof is by contradiction.
Assume that A’ is not an optimal solution to the original problem.
Let A be an optimal solution that contains 1.
So |A’|<|A|, and |A-{1}|>|A’-{1}|=|B|.
But A-{1} is also a solution to the problem of S’, contradicting the
assumption that B is an optimal solution to S’.
cs333/cutler Greedy 14