Greedy Algorithms
Greedy Algorithms
Greedy Technique
Constructs a solution to an optimization problem piece by
piece through a sequence of choices that are:
Defined by an
• feasible, i.e. satisfying the constraints objective function
and a set of
constraints
• Optimization Problem
– Problem with an objective function to either:
• Maximize some profit
• Minimize some cost
• Main Concept
– Divide the problem into multiple steps (sub-problems)
Total €0.50
Making change
Total €0.70
Making change
To make change for €0.74:
– Start with €0.50
– Add a €0.20
– Skip the €0.10 and the €0. 05 but add a €0.02
Total €0.72
Making change
Notice that each digit can be worked with
separately
– The maximum number of coins for any digit is
three
– Thus, to make change for anything less than
€1 requires at most six coins
– The solution is optimal
Anohter Example with TL
To make change for 9.78 TL using the given denominations of 50, 25, 10, 5,
and 1 Kurus:
We start with the largest coin, a 50 Kurus coin. We subtract its value from the
total:
Total value: 9.78 TL - 9.50 TL = 0.28 TL
Number of 50 Kurus coins: 1
The remaining amount is less than the value of the next largest coin, 25 Kurus.
So we move on to the next smallest coin, a 10 Kurus coin:
Total value: 0.28 TL - 2 * 10 Kurus = 0.08 TL
Number of 10 Kurus coins: 2
Next, we use a 5 Kurus coin:
Total value: 0.08 TL - 1 * 5 Kurus = 0.03 TL
Number of 5 Kurus coins: 1
Finally, we use 3 x 1 Kurus coins to reach the total of 9.78 TL:
Total value: 0.03 TL - 3 * 1 Kurus = 0 TL
Number of 1 Kurus coins: 3
Activity-Selection Problem
• Given a set of activities A1, A2, …An (E.g., talks or lectures)
• Each activity has a start time and end time
– Each Ai has (Si, Ei)
• Activities should use a common resource (E.g., Lecture hall)
• Objective: Maximize the number of “compatible” activities that use
the resource
– Cannot have two overlapping activities
A3 A7
A1
A2 A5
A8
A6
A4
Time dimension
Another Version Of The Same Problem
A3 A7
A1
A2 A5
A8
A6
A4
Time dimension
Greedy Algorithm
• Select the activity that ends first (smallest end time)
– Intuition: it leaves the largest possible empty space for
more activities
Is that an optimal
The greedy algorithm will select: answer?? Can we find
{A1, A4, A6, A7} a larger set?
Recursive Solution
Two arrays containing the start and end times The activity chosen
(Assumption: they are sorted based on end times) in the last call
Recursive-Activity-Selection(S, E, k, n)
m = k +1
While (m <= n) && ( S[m] < E[k]) Find the next activity starting
after the end of k
m++;
If (m <= n)
return {Am} U Recursive-Activity-Selection(S, E, m, n)
Else
return Φ
Iterative-Activity-Selection(S, E)
n = S.Length
List = {A1}
lastSelection = 1
For (i = 2 to n)
if (S[i] >= E[lastSelection])
List = List U {Ai}
lastSelection = i
End If
End Loop
Return List
Elements Of Greedy Algorithms
• Greedy-Choice Property
– At each step, we do a greedy (local optimal) choice
• Top-Down Solution
– The greedy choice is usually done independent of the sub-
problems
– Usually done “before” solving the sub-problem
• Optimal Substructure
– The global optimal solution can be composed from the local
optimal of the sub-problems
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
Interval scheduling
Each item i has some weight wi and benefit value vi (all wi , vi and W
are integer values)
• E.g.1:
Item 3 30 120
+
Item 2 50 50 50
20 100
Item 1 30
20 + 20 100
10 10 60
– 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
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
Fractional Knapsack
• E.g.1: 2/3 80
Of
Item 3 30 +
Item 2 50 50
20 100
Item 1 30
20 +
10 10 60
§ an integer n
§ positive value W.
Output:
§ Total profit
THE OPTIMAL KNAPSACK ALGORITHM
Initialization:
weight = 0; i = 1;
THE OPTIMAL KNAPSACK ALGORITHM
• Kruskal’s algorithm:
Created in 1957 by Joseph Kruskal
• Prim’s algorithm
Created by Robert C. Prim
Kruskal's Algorithm
Huffman code is
a = 000, b = 001, c = 010, d = 011, e = 1.
This is the optimum (minimum-cost) prefix code for this
distribution.