The primary topics in this part of the specialization are: greedy algorithms (scheduling, minimum spanning trees, clustering, Huffman codes) and dynamic programming (knapsack, sequence alignment, optimal search trees).

Loading...

From the course by Stanford University

Greedy Algorithms, Minimum Spanning Trees, and Dynamic Programming

441 ratings

The primary topics in this part of the specialization are: greedy algorithms (scheduling, minimum spanning trees, clustering, Huffman codes) and dynamic programming (knapsack, sequence alignment, optimal search trees).

From the lesson

Week 3

Huffman codes; introduction to dynamic programming.

- Tim RoughgardenProfessor

Computer Science

In this video we'll establish the correctness of Huffman's algorithm meaning

Â that that greedy algorithm always computes the prefix free binary code that minimizes

Â the average encoding length.

Â Let me also remind you of our expression for

Â the average encoding length in terms of tree capital T.

Â We'll be working with this expression quite a bit in this proof.

Â So we average over the symbols.

Â So we sum over the symbols I and the alphabet capital sigma.

Â We weight the various terms by the frequencies,

Â which are given as part of the input.

Â And remember that the encoding length that we're producing for a given symbol i

Â is exactly the same as the depth of the corresponding leaf in the tree, capital T.

Â So I quite like the proof of Huffman's theorem.

Â It's a cool proof, and it will give us an opportunity to revisit the themes that

Â we've been studying and proving the correctness of various greedy algorithms.

Â At a high level, we're going to proceed by induction,

Â induction on the size n of the alphabet sigma.

Â So there's a vague parallel you could draw with how we proved, say,

Â Dijkstra's algorithm correct, back in part one, where by induction we

Â showed that each successive iteration of the algorithm is correct,

Â assuming that the previous ones were.

Â But in the inductive step, we're going to use an exchange argument.

Â So to justify each of our mergers, we're going to argue that any optimal solution

Â can be massaged into one that looks more like ours without making it any worse.

Â And that's how we argue that each of our individual mergers is, in fact,

Â a reasonable idea, a good idea.

Â So more precisely, we're going to go by induction on the size n of the alphabet.

Â I'm going to assume,

Â to make the problem non-trivial, that the alphabet size is at least 2.

Â So, as in any proof by induction, we begin with a base case, and

Â then we do the inductive step, wherein we can assume the inductive hypothesis.

Â So, the base case is when we just have a two-letter alphabet.

Â If you go back to Huffman's algorithm,

Â you will see that it does the obvious thing in the base case.

Â It just outputs a tree that encodes one of the symbols with the letter zero, and

Â one of the symbols just with the bit one.

Â And that's the best you can do.

Â You need a bit to encode every symbol, and that tree uses exactly one bit for

Â each symbol so Huffman's algorithm is optimal in that trivial special case.

Â So for the inductive step, we focus on an arbitrary problem instance,

Â where the alphabet size is at least three.

Â Now, of course, whenever you're doing a proof by induction,

Â the thing you've really got going for you is the inductive hypothesis.

Â You assume that the assertion that you're trying to prove, in this case,

Â correctness of Huffman's algorithm, is true for all smaller values of n.

Â That is, we're going to assume that if we invoke the algorithm on any smaller input,

Â as, of course, we do, in this recursive algorithm, we get to assume that

Â the algorithm returns the correct solution to that smaller sub-problem.

Â So to understand how we're going to pass from the inductive hypothesis, so

Â this assumption that we're correct on all smaller inputs, to the inductive step,

Â the assertion that we're correct on the current input.

Â We need to take a closer look at how the original input, with its alphabet sigma,

Â can relate to the smaller sub-problem, with the smaller alphabet sigma prime,

Â with the two letters fused into one, which is the one that we solve correctly,

Â by assumption, recursively.

Â So recall our notation from the pseudocode for Huffman's algorithm.

Â What the algorithm does is take the two symbols with the smallest frequencies,

Â and let's call those two symbols a and b.

Â And it replaces those two symbols with a single symbol ab,

Â sort of a meta-symbol representing the presence of either a or b.

Â We also, in the quiz, discussed how the sensible frequency for

Â this new meta-symbol ab is the sum of the frequencies of a and b.

Â Last video, when we were developing our intuition for what a greedy algorithm for

Â successive bottom-up mergers might look like, we noticed that when you merge two

Â symbols a and b, what you're really doing is committing to outputting a tree at

Â the end of the day, at the end of your algorithm, in which the symbols a and

Â b appear as siblings, in which they have exactly the same parent.

Â Therefore, there's an exact correspondence,

Â one-on-one correspondence, between, on the one hand,

Â trees whose leaves are labeled with the symbols in sigma-prime.

Â So that there is no leaf labeled a, there's no leaf labeled b,

Â there's instead one labeled ab.

Â So there's a correspondence between those kinds of trees,

Â labels that the leaves correspond to sigma prime, and trees for

Â the original alphabet sigma, in which the symbols a and

Â b are siblings, in which they have exactly the same parent.

Â Given a tree as shown in the left picture, that is, given a tree for T prime,

Â the leaves labelled according to sigma prime, you can, and this is in fact

Â exactly what we do in Huffman's algorithm, split the leaf with the metal symbol a, b.

Â Make it an internal node, and give it two leaves with labels a and b.

Â That produces a tree of the form on the right.

Â Conversely, given a tree of the form on the right, that is,

Â its leaves are labeled according to sigma, and it just so happens that a and

Â b show up as leaves of that tree, you can produce a tree for

Â sigma prime just by contracting a and b together.

Â So, sucking up a and b into their parent, and labeling the parent ab.

Â So you can go back and forth between these two types of trees.

Â One arbitrary tree is for sigma prime and trees of a special kind for

Â sigma, trees in which a and b happen to be siblings.

Â This subset of trees for

Â sigma are important enough to be given its own notation.

Â So let me denote by capital X, sub ab, the trees with leaves labeled

Â according to sigma, in which it just so happens that a and b appear as siblings.

Â So that'll be some trees for

Â sigma, but not all of them, only the ones in which a and b are siblings.

Â So this correspondence between solutions to the smaller sub-problem and solutions

Â of a particular form for the original problem has an important property.

Â Namely, it preserves the objective function value,

Â it preserves the average encoding length.

Â Okay, that's not quite true, but it's almost true.

Â It's good enough for our purposes.

Â It preserves the average encoding length up to a fixed constant.

Â So let me show you the computation which demonstrates that point.

Â So consider any pair of matched trees, T prime and T.

Â And by matched, I just mean T prime is any old tree whose leaves are labeled

Â according to sigma prime.

Â And T is the tree you would obtain if you split the leaf

Â with meta label ab in the usual way.

Â You replace it with an internal node, and children with labels a and b.

Â So that's going to be the corresponding tree capital T.

Â So take any such matched pair, and

Â let's look at the difference between their average encoding lengths.

Â Now, remember the average encoding length of a tree is just a sum of the symbols of

Â the relevant alphabet.

Â And what we got going for us here is that sigma and

Â sigma prime are almost exactly the same, right?

Â So the only difference is sigma prime has the meta symbol ab,

Â whereas sigma has the individual symbols a and b.

Â Furthermore, the two trees t and t prime are almost exactly the same.

Â The only difference being that t prime has this leaf with the meta label ab,

Â whereas t has two corresponding nodes, one level down, with the labels a and b.

Â So, when we take the difference of these two sums,

Â everything cancels out, except that the tree t contributes two summands,

Â one for the leaf with label a, one for the leaf with label b.

Â And the t tree prime contributes, with a minus sign,

Â one summand, that corresponding to the leaf with label ab.

Â So, when the dust clears, and we cancel out all the terms,

Â what we're left is the term for the leaf a, and

Â with the frequency of a times the depth of the leaf a in the tree t.

Â A similar term for the leaf with label b in the tree t, and then with a minus sign,

Â the frequency of the meta label ab in the tree times its depth in the tree t prime.

Â But, we are certainly not done with our simplifications.

Â There is a strong relationship between the frequencies that we're staring at and

Â a strong relationship between these various depths.

Â Let's begin with the frequencies.

Â How did we define the frequency of the meta symbol ab?

Â Or, remember our quiz.

Â It made sense to define it as the sum of the frequencies of a and b.

Â What about the depths?

Â Well, you know, this symbol ab is at whatever depth it is in the tree T prime.

Â So, it's a depth 10, something like that.

Â Remember, T is obtained from T prime simply by splitting this leaf ab and

Â giving it two new children with symbols a and b.

Â So, if the meta symbol ab was at depth ten in T prime, then the leaves a and

Â b are going to be at depth 11 in T.

Â So, the depth is simply one more than whatever it was previously in

Â the tree T prime.

Â So these relationships are going to result in a second wave of cancellations.

Â So just to make that crystal clear, let's call the the depth of ab in T prime d.

Â So d is what I was calling ten earlier.

Â So a and b are both a depth d plus 1 in the tree T.

Â So the first term becomes the frequency of a times the depth, d plus 1.

Â The second term becomes the frequency of b, plus the depth, d plus 1.

Â And then the third term becomes the sum of the frequencies of a and

Â b times the depth d.

Â And, when the dust settles yet again, we are left with a constant, Pa + Pb.

Â So, the sums of two frequencies.

Â And one thing I want you to really understand is that the difference between

Â these two average encoding lengths is just some constant.

Â It does not depend on which trees we started with.

Â So if we choose a perfectly balance tree and

Â we do this difference, we get some constant like 0.1.

Â If we choose some totally different pair of trees that are really lopsided and

Â we take this difference, we still get 0.1, exactly the same thing.

Â So, this fulfills the promise I gave you earlier, that not only do we have this

Â natural correspondence between, on the one hand, trees labeled with sigma prime, and

Â trees of a certain type labeled according to sigma, namely those in which a and

Â b appear siblings, but the correspondence preserves average encoding length.

Â Okay, it doesn't quite preserve the average encoding length, but

Â it preserves it up to a universal constant, and

Â that's going to be good enough for our purposes, as we'll see in a sec.

Â Coursera provides universal access to the worldâ€™s best education,
partnering with top universities and organizations to offer courses online.