Professional Documents
Culture Documents
2014 2015
Greedy Algorithms
Philip Dutr & Benedict Brown
Dept. of Computer Science, K.U.Leuven
At each step, take the largest possible bill or coin that does not
overshoot
a $5 bill
a $1 bill, to make $6
a 25 coin, to make $6.25
A 10 coin, to make $6.35
four 1 coins, to make $6.39
A 10 quatloo piece
Five 1 quatloo pieces (for a total of 15 quatloos)
this requires six coins
We schedule this activity first, but this leaves no room for the other 4
(and we want to optimize for the number of activities)
10
We schedule this activity first, but this leaves no room for the other 2
(and we want to optimize for the number of activities)
11
We schedule this activity first (fewest conflict), and we then only have place for 2 others
(and we want to optimize for the number of activities)
12
1st
13
2nd
3rd
4th
The activities do not overlap, so they are also sorted by start time.
15
File Compression
16
17
Run-Length Encoding
18
19
Huffman Coding
Morse code uses dots and dashes for letters, and gaps to end codes
Computers have only 1s and 0s (no gaps)
Prefix codes: No code is a prefix of any other code
20
21
Example: ovoviviparous
22
Proof of optimality
23
Proof of optimality
Lemma 2:
There exists an optimal subtree in which the two least frequent letters are
siblings at the maximum depth.
Proof:
24
Every leaf node has a sibling or the tree would not be full (lemma 1).
The two least frequent letters x and y must be at the maximum depth.
If not, there must be a letter w at maximum depth that occurs more frequently,
and that would not be optimal.
If x and y are not siblings, swap y with x's sibling. The number of bits for all
letters remains unchanged.
Proof of optimality
Base Case:
25
Proof of optimality
26
27