Professional Documents
Culture Documents
Greedy Algorithms
1
Optimization Problems
• Some problems can have many possible/ feasible solutions
with each solution having a specific cost. We wish to find the
best solution with the optimal cost.
• Maximization problem finds a solution with maximum cost
• Minimization problem finds a solution with minimum cost
2
Optimization Problems
• Two common techniques:
• Greedy Algorithms (local)
• Make the greedy choice and THEN
• Solve sub-problem arising after the choice is made
• The choice we make may depend on previous choices, but
not on solutions to sub-problems
• Top down solution, problems decrease in size
• Dynamic Programming (global)
• We make a choice at each step
• The choice depends on solutions to sub-problems
3
• Bottom up solution, smaller to larger sub-problems
Greedy Algorithm
• A greedy algorithm works in phases. At each phase:
• makes the best choice available right now, without regard for future
consequences
• hopes to end up at a global optimum by choosing a local optimum at
each step. For some problem, it works
5
Greedy Algorithm
• Greedy choice property: A globally optimal solution can be arrived at
by making a locally optimal (greedy) choice
• Make whatever choice seems best at the moment and then solve the
sub-problem arising after the choice is made
• The choice made by a greedy algorithm may depend on choices so far,
but it cannot depend on any future choices or on the solutions to sub-
problems
6
Huffman Codes
7
Huffman Codes
Computer Data Encoding:
How do we represent data in binary?
Historical Solution:
Fixed length codes.
8
American Standard Code for Information
Interchange (ASCII): Example
Encode: AABCAA
A A B C A A
1000001 1000001 1000010 1000011 1000001 1000001
9
Assuming an ℓ bit fixed length code and a file of n characters
Total space usage in bits: nℓ bits.
Variable Length Codes
Idea: In order to save space, use less bits for frequent characters and
more bits for rare characters.
Example: suppose we have 3 symbols: { A, B, C } and in a file we have
1,000,000 characters. Need 2 bits for a fixed length code and a total of
2,000,000 bits.
Suppose the frequency distribution of the characters is:
C B A
500 500 999,000
11
How do we Decode?
In the variable length code, we use an idea called Prefix code, where
no code is a prefix of another.
Example: A = 0
B = 10
C = 11
None of the above codes is a prefix of another.
AAAB B C C C B C B AA C C
How to construct Huffman code?
• Construct a variable length code for a given file with the
following properties:
• Prefix code.
• Using shortest possible codes.
• Efficient.
• Idea:
• Consider a binary tree, with:
• 0 meaning a left turn 0 1
• 1 meaning a right turn.
A
Consider the paths from the root 0 1
to each of the leaves A, B, C, D:
B
A:0 0 1
B : 10 13
C : 110 C D
D : 111
Observe:
1. This is a prefix code, since each of the leaves has a path
ending in it, without continuation.
2. If the tree is full then we are not “wasting” bits.
3. If we make sure that the more frequent symbols are closer to
the root then they will have a smaller code.
0 1
A
0 1
B
0 1
14
C D
Greedy Algorithm:
1. Consider all pairs: <frequency, symbol>.
2. Choose the two lowest frequencies, and make them brothers, with the
root having the combined frequency.
3. Iterate.
15
Greedy Algorithm Example:
Alphabet: A, B, C, D, E, F
Frequency table:
F E D C B A
60 50 40 30 20 10
17
Algorithm Run:
X 30 C 30 D 40 E 50 F 60
A 10 B 20
18
Algorithm Run:
Y 60 D 40 E 50 F 60
X 30 C 30
A 10 B 20
19
Algorithm Run:
D 40 E 50 Y 60 F 60
X 30 C 30
A 10 B 20
20
Algorithm Run:
Z 90 Y 60 F 60
D 40 E 50 X 30 C 30
A 10 B 20
21
Algorithm Run:
Y 60 F 60 Z 90
X 30 C 30 D 40 E 50
A 10 B 20
22
Algorithm Run:
W 120 Z 90
Y 60 F 60 D 40 E 50
X 30 C 30
A 10 B 20
23
Algorithm Run:
Z 90 W 120
D 40 E 50 Y 60 F 60
X 30 C 30
A 10 B 20
24
Algorithm Run:
V 210
0 1
Z 90 W 120
0 1 1
0
D 40 E 50 Y 60 F 60
1
0
X 30 C 30
0 1
A 10 B 20 25
The Huffman encoding:
V 210
A: 1000 0 1
B: 1001
Z 90
C: 101 1
W 120
0 0 1
D: 00
E: 01 D 40 E 50 Y 60 F 60
0 1
F: 11
X 30 C 30
0 1
A 10 B 20
26
File Size: 10x4 + 20x4 + 30x3 + 40x2 + 50x2 + 60x2 =
40 + 80 + 90 + 80 + 100 + 120 = 510 bits
Note the savings:
Frequency table:
F E D C B A
5 9 16 12 13 45
F:5 E:9
Letter to be encoded A B C D E F
Frequency (thousands) 45 13 12 16 9 5
Fixed-length code 000 001 010 011 100 101
Variable-length code 0 101 100 111 1101 1100
30
Activity Selection Problem
31
Activity Selection Problem ASP
Input: Set A = {a1, a2, ..., an} of n activities, where activity ai is the half-
open interval [si, fi), where si and fi are the start and finish time.
i 1 2 3 4 5 6 7 8 9 10 11
si 1 3 0 5 3 5 6 8 8 2 12
fi 4 5 6 7 8 9 10 11 12 13 14
2 6
34
1 5 7
3 4 8
A Recursive Greedy Algorithm
• The greedy algorithm first sorts the activities on the finish times and
them picks them in order, such that each new activity picked is
compatible with the one picked previously. This algorithm solves
the ASP in O(n lg n).
Recursive-Activity-Selector (s, f, i, j)
1 m←i+1
2 while m < j and sm < fi
3 do m ← m + 1
4 if m < j
5 then
return {am} Recursive-Activity-Selector (s, f, m, j)
6 else return Ø 35
Example: A Recursive Greedy Algorithm
i 0 1 2 3 4 5 6 7 8 9 10 11
si - 1 3 0 5 3 5 6 8 8 2 12
fi 0 4 5 6 7 8 9 10 11 12 13 14
36
Example: A Recursive Greedy Algorithm
a1
a0
time
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
i = 0,
j = n + 1 = 12
m←i+1←0+1=1
m < j (1 < 12) and s1 < f0 (But 1>0)
if m < j (1 < 12)
return {a1} Recursive-Activity-Selector (s, f, 1,12) 37
Example: A Recursive Greedy Algorithm
a2
a1
time
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
i = 1,
m←i+1←1+1=2
a3
a1
time
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
39
Example: A Recursive Greedy Algorithm
a4
a1
time
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
a5
a1 a4
time
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
i = 4,
m←i+1←4+1=5
a6
a1 a4
time
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
42
Example: A Recursive Greedy Algorithm
a7
a1 a4
time
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
43
Example: A Recursive Greedy Algorithm
a8
a1 a4
time
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
a9
a1 a4 a8
time
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
i = 8,
m←i+1←8+1=9
a10
a1 a4 a8
time
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
46
Example: A Recursive Greedy Algorithm
a11
a1 a4 a8
time
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
m < j (11 < 12) and s11 < f8 (But 12 > 11)
if m < j (11 < 12)
return {a11} Recursive-Activity-Selector (s, f, 11,12)
47
Example: A Recursive Greedy Algorithm
a1 a4 a8 a11
time
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
i = 11,
m ← i + 1 ← 11 + 1 = 12
49
Counting Money Problem
50
Counting Money Problem
Suppose you want to count out a certain amount of money,
using the fewest possible bills and coins
51
For US money, the greedy algorithm always gives optimum solution
Counting Money Problem
Greedy algorithm (C, N)
1. sort coins so C1 C2 . . . Ck
2. S = ;
3. Change = 0
4. i=1 \\ Check for next coin
5. while Change N do \\ all most valuable coins
6. if Change + Ci ≤ N then
7. Change = Change + Ci
8. S = S {Ci}
9. else i = i+1
52
Counting Money Problem
In Pakistan, our currency notes are
C1 = 5000, C2 = 1000, C3 = 500, C4 = 100,
C5 = 50, C6 = 20 , C7 = 10
A better solution would be to use two 7 kron pieces and one 1 kron
piece, requiring only tree coins
54
The greedy algorithm results in a solution, but not in an optimum
solution.
Bin Packing Problem
55
Approximate Bin Packing Problem
We are given n items of sizes S1, S2,…..,Sn. All sizes satisfy 0 < Si <= 1.
Suppose we have item list with sizes 0.2, 0.5, 0.4, 0.7, 0.1, 0.3, 0.8.
Optimal packing of these sizes is
0.3 0.5
0.8
0.1
0.7
0.4 56
0.2
B1 B2 B3
Approximate Bin Packing Problem
Algorithm 1: Next Fit
When processing any item, we check to see whether it fits in the
same bin as the last item; if it does it is placed there; otherwise a
new bin is created.
void NextFit ( )
{ read item1;
while ( read item2 ) {
if ( item2 can be packed in the same bin as item1 )
place item2 in the bin;
else
create a new bin for item2;
item1 = item2;
} /* end-while */
}
57
【 Theorem 】 Let M be the optimal number of bins required to pack a list
I of items. Then next fit never uses more than 2M bins. There exist
sequences such that next fit uses 2M – 2 bins.
Approximate Bin Packing Problem
Next Fit for 0.2, 0.5, 0.4, 0.7, 0.1, 0.3, 0.8 is
0.1
0.5 0.8
0.7
0.2 0.4 0.3
B1 B2 B3 B4 B5
58
Approximate Bin Packing Problem
Algorithm 2: First Fit
• We scan the bins in order and place the new item in the first bin
that is large enough to hold it. A new bin is created only when the
result of previous placements have left no other alternative.
void FirstFit ( )
{ while ( read item ) {
scan for the first bin that is large enough for item;
if ( found )
place item in that bin;
else
create a new bin for item;
} /* end-while */
}
First Fit for 0.2, 0.5, 0.4, 0.7, 0.1, 0.3, 0.8 is
0.1
0.5 0.3
0.8
0.7
0.2 0.4
B1 B2 B3 B4
60
Approximate Bin Packing Problem
Algorithm 3: Best Fit
• Instead of placing a new item in the first spot that is found. It is placed in the
tightest spot among all bins
Best Fit for 0.2, 0.5, 0.4, 0.7, 0.1, 0.3, 0.8 is
0.1
0.3
0.5 0.8
0.7
0.2 0.4
B1 B2 B3 B4 61
62
Approximate Bin Packing Problem
Offline Algorithm
• Best Fit Decreasing for 0.8, 0.7, 0.5, 0.4, 0.3, 0.2, 0.1 is
0.2 0.1
0.3
0.4
0.8 0.7 0.5
B1 B2 B3
63