Algorithms - Greedy Algorithms
Algorithms - Greedy Algorithms
Amotz Bar-Noy
CUNY
Spring 2012
In Off-Line problems:
The whole input is known in advance.
Possible to do some preprocessing of the input.
Decisions are irrevocable.
Input:
Integer coin denominations dn > · · · > d2 > d1 = 1.
An integer amount to pay: A.
Implementation:
Coin-Changing(dn > · · · > d2 > d1 = 1)
for i = n downto 1
ni = bA/di c
A = A mod di = A − ni di
Return(N = nn + · · · + n2 + n1 )
Objective:
Find a solution that is polynomial only in n.
Probably impossible!?
Input:
A thief enters a store and finds n items I1 , . . . , In .
The value of item Ii is v (Ii ) and its weight is w(Ii ).
Both are positive integers.
The thief can carry at most weight W .
The thief either takes all of item Ii or doesn’t take item Ii .
Input:
A thief enters a store and finds n items I1 , . . . , In .
The value of item Ii is v (Ii ) and its weight is w(Ii ).
Both are positive integers.
The thief can carry at most weight W .
The thief either takes all of item Ii or doesn’t take item Ii .
Greedy criterion I: Order the items by their value from the most
expensive to the cheapest.
Greedy criterion II: Order the items by their weight from the
lightest to the heaviest.
Greedy criterion III: Order the items by their ratio of value over
weight from the largest ratio to the smallest ratio.
Objective:
Find a solution that is polynomial only in n.
Probably impossible!?
However, Greedy-by-Ratio produces “good” solutions.
Input:
Activities A1 , . . . , An that need the service of a common resource.
Activity Ai is associated with a time interval [si , fi ) for si < fi .
Ai needs the service from time si until just before time fi .
A graphical representation:
activities
A3
A2
A1
time
1 2 3 4 5 6 7 8
A graphical representation:
activities
A3
A2
A1
time
1 2 3 4 5 6 7 8
A1 A3
time
1 2 3 4 5 6 7 8
Add A to S.
Delete from R the activities that are not compatible with activity A.
Four criteria:
Prefer short activities.
Prefer activities intersecting few other activities.
Prefer activities that start earlier.
Prefer activities that terminate earlier.
Remarks:
All four criteria are static in their nature.
The second criterion has a dynamic version.
Preprocessing(A1 , . . . , An )
Sort the activities according to their finish time
Let this order be A1 , . . . , An (*i < j ⇒ fi ≤ fj *)
Greedy-Activity-Selector(A1 , . . . , An )
S = {A1 } (* A1 terminates the earliest *)
j = 1 (* Aj is the current selected activity *)
for i = 2 to n (* scan all the activities *)
if si ≥ fj (* check compatibility *)
then (* select Ai that is compatible with S *)
S = S ∪ {Ai }
j =i
else (* Ai is not compatible *)
Return(S)
Correctness: By definition.
Complexity:
The sorting can be done in O(n log n) time.
There are O(1) operations per each activity.
All together: O(n log n) + n · O(1) = O(n log n) time.
activities
A 11
A 10
A9
A8
A7
A6
A5
A4
A3
A2
A1
time
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
activities
A 11
A8
A4
A1
time
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Ai ∈
/ S ⇒ ∃Aj ∈ S that is not in T in which j < i.
activities
A 11
A 10
A9
A8
A7
A6
A5
A4
A3
A2
A1
time
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
activities
A 11
A 10
A9
A8
A7
A6
A5
A4
A3
A2
A1
time
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
activities
A 11
A 10
A9
A8
A7
A6
A5
A4
A3
A2
A1
time
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Input:
An alphabet of n symbols a1 , . . . , an .
A frequency fi for each symbol ai :
Pn
i=1 fi = 1.
A File F containing L symbols from the alphabet.
ai appears exactly ni = fi · L times in F.
Input:
An alphabet of n symbols a1 , . . . , an .
A frequency fi for each symbol ai :
Pn
i=1 fi = 1.
A File F containing L symbols from the alphabet.
ai appears exactly ni = fi · L times in F.
Output:
For symbol ai , 1 ≤ i ≤ n: A binary codeword wi of length `i .
A compressed (encoded) binary file F 0 of F.
Code I:
wa = 000, wb = 001, wc = 010, wd = 011, we = 100, wf = 101.
Length of encoded file is 300.
Code II:
wa = 0, wb = 101, wc = 100, wd = 111, we = 1101, wf = 1100.
Length of encoded file is 224
1 · 45 + 3 · 13 + 3 · 12 + 3 · 16 + 4 · 9 + 4 · 5 = 224.
100
0 1
a:45 55
0 0 1
25 30
0 1 0 1
f:5 e:9
1100 1101
Complexity Parameters:
f (x) the frequency of a leaf x.
Proof:
Let z be an internal node with only one child y.
There are 2 cases:
Case I: z is the root.
Case II: z is not the root.
z y
y
B C
B C
p p
z y
A A
y
B C
B C
In both cases:
`(x) of all the leaves in the sub-tree rooted at z is reduced by 1.
100
0 1
86 14
0 1 0
58 28 14
0 1 0 1 0 1
B(T ) = 300
100
0 1
86 14
0 1 0 1
58 28 e:9 f:5
0 1 0 1 10 11
B(T ) = 3 · 86 + 2 · 14 = 286
.......................................................................
f:5 e:9
f:5 e:9
.......................................................................
14 d:16 25 a:45
14 d:16 25 a:45
.......................................................................
25 30 a:45
f:5 e:9
25 30 a:45
f:5 e:9
.......................................................................
a:45 55
25 30
f:5 e:9
a:45 55
25 30
f:5 e:9
.......................................................................
100
a:45 55
25 30
f:5 e:9
http://www.cs.auckland.ac.nz/˜jmor159/PLDS210/huffman.html
Huffman(ha1 , f1 i , . . . , han , fn i)
Build-Queue({f1 , . . . , fn } , Q)
for i = 1 to n − 1 (* the combination loop *)
z =Allocate-Node() (* creating a new root *)
x = left(z) =Extract-Min(Q)
(* lightest tree is the left sub-tree *)
y = right(z) =Extract-Min(Q)
(* second lightest tree is the right sub-tree *)
f (z) = f (x) + f (y ) (* frequency of new root *)
Insert(Q, f (z)) (* inserting the new root to the queue *)
return Extract-Min(Q) (* last tree is the Huffman code *)
Let A be an alphabet.
x z
y w
z w x y
z z:f(x)+f(y)
x:f(x) y:f(y)
Proof by Induction:
The first lemma implies that the first greedy step is a first step
towards an optimal solution.
The second lemma justifies the inductive steps, applying again and
again the first lemma.