We are given a set S = {a1, a2, ..., an} of n proposed activities that wish to use a resource that only one activity can use at a time. Each activity ai has a start time si and a finish time fi, where 0 ≤ si ≤ fi ≤ ∞. Activity ai takes place in the half-open time interval [si, fi). Activities ai and aj are compatible if the intervals [si, fi) and [sj, fj) do not overlap, i.e., if si ≥ fk or sj ≥ fi.
Activity Selection Problem: Select a maximum-size subset of mutually compatible activities, assuming that the activities are sorted in monotonically increasing order of finish time:
f1 ≤ f2 ≤ ... ≤ fn
Let Sk = {ai ∈ S : si ≥ fk} be the set of activities that start after ak finishes.
Theorem Consider any nonempty subproblem Sk, and let am be an activity in Sk with the earliest finish time. Then am is included in some maximum-size subset of mutually compatible activities of Sk.
Prefix Codes: Each code has a unique prefix
What is desirable about prefix codes?
Lemma Let C be an alphabet in which each character c ∈ C has frequency c.freq. Let x and y be two characters in C having the lowest frequencies. Then there exists an optimal prefix code for C in which the codewords for x and y have the same length and differ only in the last bit.
Proof
Lemma Let C be a given alphabet with frequency c.freq defined for each character c ∈ C. Let x and y be two characters in C with minimum frequency. Let C' be the alphabet C with the characters x and y removed and a new character z added, so that C' = C − {x, y} ∪ {z}. Define f for C' as for C, except that z.freq = x.freq + y.freq. Let T' be any tree representing an optimal prefix code for the alphabet C'. Then the tree T, obtained from T' by replacing the leaf node for z with an internal node having x and y as children, represents and optimal prefix code for the alphabet C.