L7 Greedy-Algorithms
L7 Greedy-Algorithms
Greedy Algorithms
CSE373: Design and Analysis of Algorithms
Greedy Algorithm
• Solves an optimization problem
• For many optimization problems, greedy algorithm can be used. (not always)
• Greedy algorithm for optimization problems typically go through a
sequence of steps, with a set of choices at each step.
• Current choice does not depend on evaluating potential future
choices or pre-solving repeatedly occurring subproblems (a.k.a.,
overlapping subproblems).
• With each step, the original problem is reduced to a smaller
problem.
• Greedy algorithm always makes the choice that looks best at the
moment.
• It makes a locally optimal choice in the hope that this choice will lead
to a globally optimal solution.
Finding the global minima of a function
• start from an arbitrary value of x
• If f(x+1) < f(x) then set x = x+1
•Otherwise if f(x-1) < f(x) then set x = x-1
• Continue until changing x doesn’t decrease f(x)
A
f(x) A f(x) B
B
C
2
3
0 1 2 3 4 5 6 7 8 9
Time
Activity Selection a.k.a. Interval Scheduling Problem
Here are a set of start and finish times
i 1 2 3 4 5 6 7 8 9 10 11
Sj 1 3 0 5 3 5 6 8 8 2 12
fj 4 5 6 7 9 9 10 11 12 14 16
RECURSIVE-ACTIVITY-SELECTOR(s,
RECURSIVE-ACTIVITY-SELECTOR(s,f,f,i)i)
//Solves
//Solvesthe
thesubproblem
subproblemSSi+1,n = {ai+1, ai+2, …, an} of the problem Si,n ={ai,ai+1,…,an}, i.e.,
i+1,n = {ai+1, ai+2, …, an} of the problem Si,n ={ai,ai+1,…,an}, i.e.,
//computes
//computesmutually
mutuallycompatible
compatibleactivities
activitiesininthe
theset
setSSi+1,n given thataai ∈
giventhat
i+1,n ∈AAi,n ::solution
i i,nsolutionset
setofofSSi,n
i,n
1.1. nn==s.length
s.length
2.2. mm==i+1 i+1
3.3. while
whilemm≤≤nnandands[m]
s[m]<<f[i]
f[i] //skip
//skipall
allactivities
activitieswhich
whichare
areincompatible
incompatiblewith
withi i
4.4. mm==mm++11
5.5. ifif mm≤≤nn //if
//ifan
anactivity
activityisisfound
foundininSSi+1,n which
whichisiscompatible
compatiblewith
withi i
i+1,n
6.6. return
return{a
{amm}}UURECURSIVE-ACTIVITY-SELECTOR(s,
RECURSIVE-ACTIVITY-SELECTOR(s,f,f,m)
m) //add
//addaammtotosolution
solutionset
set
6.6. else returnØØ
elsereturn
• Running time is (n)
• Assuming that the activities have already been sorted by finish times,
• the running time of the call RECURSIVE-ACTIVITY-SELECTOR is (n)
• Because, over all recursive calls, each activity is examined exactly once in
the while loop test of line 3.
1
5
Activities
10
11
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
Time
1
5
Activities
10
11
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
Time
1
5
Activities
10
11
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
Time
1
5
Activities
10
11
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
Time
1
5
Activities
10
11
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
Time
1
5
Activities
10
11
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
Time
1
5
Activities
10
11
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
Time
1
5
Activities
10
11
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
Time
Properties of Greedy Problems
How can one tell if a greedy algorithm will be able to solve an
optimization problem i.e., whether a problem is a “Greedy problem”?
There is no way in general. But there are 2 ingredients exhibited by
most Greedy Problems (problems solvable via a greedy algorithm):
1. Greedy Choice Property: there is an optimal solution that contains
the first greedy choice
• It implies that a globally optimal solution can be arrived at by making a
locally optimal (Greedy) choice.
• So we can make whatever choice seems best at this moment (i.e., greedy
choice) and then solve the subproblem that results after the choice is made.
• Thus, a greedy strategy usually progresses in a top-down fashion, making
one greedy choice after another, iteratively reducing each given problem
instance to a smaller one.
2. Optimal Sub Structure Property: Optimal solution of a problem
contains within it optimal solution of its subproblem(s)
• It implies that the optimal solution of subproblem(s) can be combined
together to obtain the optimal solution of the problem itself
Greedy Choice Property Illustration
i 1 2 3 4 5 6 7 8 9 10 11
Sj 1 3 0 5 3 5 6 8 8 2 12
fj 4 5 6 7 9 9 10 11 12 14 16
• An optimal solution which does not begin with activity 1 is: A={2,4,9,11}
• Let B = A – {2} U {1} = {1,4,9,11}; B is an optimal solution, too. Why?
• Activity 2 doesn’t clash with 4,9, or 11. Activity 1 finishes before 2 finishes and
as such 1 cannot clash with 4,9, or 11, either. Also, activities 4,9, and 11 do not
clash with each other (otherwise they wouldn’t be members of A).
• Hence B = {1,4,9,11} is a valid solution. Since |B| = |A|, B is an optimal solution.
Optimal Substructure Property Illustration
i 1 2 3 4 5 6 7 8 9 10 11
Sj 1 3 0 5 3 5 6 8 8 2 12
fj 4 5 6 7 9 9 10 11 12 14 16
(1)&(2) => f1 sj for all j ∈ A-{k} => activity 1 doesn’t clash with other activities in B
Also, activities in A-{k} do not clash with each other (Since, A is a valid solution)
Hence activities in B are mutually compatible. Also, |B| = |A|
Thus, B is an optimal solution which contains activity 1 (the first greedy choice)
Optimal Substructure Property of Activity Selection Problem
“knapsack”
Solution: P
• 1 ml of 5 50$
Items: •
1 2 3 4 5 2 ml of 3 40$
• 6 ml of 4 30$
Weight: 4 ml 8 ml 2 ml 6 ml 1 ml • 1 ml of 2 4$
Benefit: $12 $32 $40 $30 $50 10 ml •Total Profit:124$
Value: 3 4 20 5 50
($ per ml)
The Fractional Knapsack Problem
Given: A set S of n items, with each item i having
bi - a positive benefit
wi - a positive weight
Goal: Choose items with maximum total benefit but with weight at most W.
If we are allowed to take fractional amounts, then this is the fractional knapsack
problem.
In this case, we let xi denote the amount we take of item i
Objective: maximize
Constraint: b (x / w )
iS
i i i
x
iS
i W ,0 xi wi
The Fractional Knapsack Algorithm
Greedy choice: Keep taking item with highest value (benefit to weight ratio)
Since
b ( x / w ) (b / w ) x
iS
i i i
iS
i i i
Algorithm fractionalKnapsack(S, W)
Input: set S of items w/ benefit bi and weight wi; max. weight W
Output: amount xi of each item i to maximize benefit w/ weight at most W
Price 7 6 9 6 12 10 3
a b c d e f
Frequency (thousands) 45 13 12 16 9 5
3 bits needed
a b c d e f
Frequency (thousands) 45 13 12 16 9 5
Variable-Length Codes
E.g.: Data file containing 100,000 characters
a b c d e f
Frequency (thousands) 45 13 12 16 9 5
E.g.:
a = 0, b = 101, c = 100, d = 111, e = 1101, f = 1100
Idea: f: 5 e: 9 c: 12 b: 13 d: 16 a: 45
Start with a set of |C| leaves
At each step, merge the two least frequent objects: the frequency of the new
node = sum of two frequencies
Use a min-priority queue Q, keyed on f to identify the two least frequent
objects
Constructing a Huffman tree
Alg.: HUFFMAN(C)
Running time: O(nlgn)
1. n C
2. Q C
3. for i 1 to n – 1 O(n)
4. do allocate a new node z
5. left[z] x EXTRACT-MIN(Q)
6. right[z] y EXTRACT-MIN(Q) O(lgn)
7. f[z] f[x] + f[y]
8. INSERT (Q, z)
9. return EXTRACT-MIN(Q)
Example
f: 5 e: 9 c: 12 b: 13 d: 16 a: 45 c: 12 b: 13 14 d: 16 a: 45
0 1
f: 5 e: 9
14 d: 16 25 a: 45 25 30 a: 45
0 1 0 1 0 1 0 1
f: 5 e: 9 c: 12 b: 13 c: 12 b: 13 14 d: 16
0 1
f: 5 e: 9
a: 45 55 0 100 1
0 1
a: 45 55
25 30 0 1
0 1 0 1
25 30
c: 12 b: 13 14 d: 16 0 1 0 1
0 1 c: 12 b: 13 d: 16
14
f: 5 e: 9 0 1
f: 5 e: 9
Huffman encoding/decoding using Huffman tree
Encoding:
• Construct Huffman tree using the previous algorithm
•Traverse the Huffman tree to assign a code to each leaf
(representing an input character )
• Use these codes to encode the file
Decoding:
• Construct Huffman tree using the previous algorithm
• Traverse the Huffman tree starting from root node according
to the bits you encounter until you reach a leaf node at which
point you output the character represented by that leaf node &
then go back to the root node.
• Continue in this fashion until all the bits in the file are read.
Huffman Coding Practice
A file has the following characters along with their
frequencies:
a: 20, b: 30, c: 78, d: 12, e: 27, f: 67 g: 5, h: 80.