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

437 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

So this set of lectures will be our final application of the greedy algorithm

Â design paradigm.

Â It's going to be to an application in compression.

Â Specifically, I'll show you a greedy algorithm for

Â constructing a certain kind of prefix-free binary codes know as Huffman codes.

Â So we're going to spend this video just sort of setting the stage.

Â So let's begin by just defining a binary code.

Â So a binary code is just a way to write down symbols from

Â some general alphabet in a manner that computers can understand.

Â That is, it's just a function mapping each symbol from an alphabet,

Â capital sigma, to a binary string, a sequence of zeroes and ones.

Â So this alphabet capital sigma could be any number of things but, you know,

Â as a simple example, you could imagine it's the letters a through z,

Â say in lowercase, plus maybe the space character and some punctuation.

Â So maybe, for a set of size 32 overall.

Â And if you have 32 symbols you need to encode in binary, well,

Â an obvious way to do it is, there happens to be 32 different binary strings of

Â length five, so why not just use one of each of those for your symbols.

Â So this would be a fixed length code, in the sense we're using the exactly the same

Â number of bits, namely five, to encode each of the symbols of our alphabet.

Â This is pretty similar to what's going on with ASCII codes.

Â And of course, it's a mantra of this class to ask,

Â when can we do better than the obvious solution?

Â So in this context, when can we do better than the fixed length codes?

Â Sometimes we can, in the important case when some symbols are much more likely to

Â appear than others.

Â In that case,

Â we can encode information using fewer bits by deploying variable length codes.

Â And this is, in fact, a very practical idea.

Â Variable length codes are used all the time in practice.

Â One example is in coding MP3 audio files.

Â So if you look up the standard for MP3 encoding,

Â there's this initial phase which you do analog-to-digital conversion.

Â But then, once you're in the digital domain, you do apply Huffman Codes,

Â what I'm going to teach you in these videos,

Â to compress the length of the files even further.

Â And of course as you know, compression, especially lossless compression,

Â like Huffman codes, is a good thing.

Â You want to download a file, you want it to happen as fast as possible.

Â Well, you want to make the file as small as possible.

Â So a new issue arises when you pass from fixed-length codes

Â to variable length codes.

Â So let me illustrate that with a simple example.

Â Suppose our alphabet sigma is just four characters, A, B, C, D.

Â So the obvious fixed length encoding of these characters would

Â just be 00, 01, 10, and 11.

Â Well, suppose you wanted to use fewer bits,

Â and wanted to use a variable length encoding, an obvious idea would be to try

Â to get away with only one bit for a couple of these characters.

Â So, suppose instead of using a double 0 for A, we just use a single 0.

Â And instead of using a double one for D we just use a single one.

Â So that's only fewer bits.

Â So that seems like that can only be better.

Â But now, here's the question.

Â Suppose, someone handed you an encoded transmission consisting of the digits 001.

Â What would have been the original sequence of symbols

Â that led to that encoded version?

Â All right, so the answer is D.

Â There is not enough information to know what 001 was

Â supposed to be an encoding of.

Â The reason is is that having passed to a variable length encoding,

Â there is now ambiguity.

Â There is more than one sequence of original symbols that could have led,

Â under this encoding, to the output 001.

Â Specifically, answers A and C would both lead to 001.

Â The letter A would give you a zero, the letter B would give you a 01.

Â So that would give you 001.

Â On the other hand, AAD would also give you 001.

Â So there's no way to know.

Â Contrast this with fixed-length encoding.

Â If you're given a sequence of bits with a fixed length code,

Â of course you know where one letter ends and the next one begins.

Â For example, if every symbol was encoded with 5 bits, you would just read 5 bits.

Â You would know what symbol it was, you would read the next 5 bits, and so on.

Â With variable length codes, without further precautions,

Â it's unclear where one symbol starts and the next one begins.

Â So that's an additional issue we have to make sure to take care of with variable

Â length codes.

Â So to solve this problem, that with variable length codes and without further

Â precautions, it's unclear where one symbol ends and where the next one begins,

Â we're going to insist that our variable length codes are prefix free.

Â So what this means is when we encode a bunch of symbols,

Â we're going to make sure that for each pair of symbols i and

Â j from the original alphabet sigma, the corresponding encodings will have

Â the property that neither one is a prefix of the other.

Â So going back to the previous slide,

Â you'll see that that example was not prefix free.

Â For example, we used zero, was a prefix of zero one, that led to ambiguity.

Â Similarly, one, the encoding for d, was a prefix of 10, the encoding for

Â c, and that also leads to an ambiguity.

Â So if you don't have prefixes for each other, and

Â we'll develop this more shortly, then there's no ambiguity.

Â Then there's a unique way to decode, to reconstruct what the original sequence of

Â symbols was, given just the zeros and ones.

Â So lest you think this is too strong a property, certainly interesting and

Â useful variable length codes exist that satisfy the prefix-free property.

Â So one simple example, again just to encode the letters A, B, C, D.

Â We can get away with encoding the symbol A just using a single bit,

Â just using a zero.

Â Now, of course, to be prefix free,

Â it better be the case that our encodings of B and C and D all start with the bit 1.

Â Otherwise we're not prefix free.

Â But we can get away with that, so let's encode a B with a one and

Â then a zero, and now both C and

Â D better have the property that they start neither with 0 nor with 10.

Â That is, they better start with 11, but let's just encode c using 110 and

Â D using 111.

Â So that would be a variable length code.

Â The number of bits varies between one and three, but it is prefix free.

Â And again, the reason we might want to do this,

Â the reason we might want to use a variable-length encoding, is to take

Â advantage of non-uniform frequencies of symbols from a given alphabet.

Â So let me show you a concrete example of the benefits you can get from these kinds

Â of codes on the next slide.

Â So let's continue with just our four-symbol alphabet, A, B, C, and D.

Â And let's suppose we have good statistics in our application domain about

Â exactly how frequent each of these symbols are.

Â So, in particular, let's assume we know that A is by far the most likely symbol.

Â Let's say 60% of the symbols are going to be As,

Â whereas 25% are Bs, 10% are Cs, and 5% are Ds.

Â So why would you know the statistics?

Â Well, in some domains you're just going to have a lot of expertise.

Â In genomics you're going to know the usual frequencies of As, Cs, Gs and Ts.

Â For something like an mp3 file, well, you can literally just take an intermediate

Â version of the file after you've done the analog to digital transformation, and

Â just count the number of occurrences of each of the symbols.

Â And then you know exact frequencies, and then you're good to go.

Â So let's compare the performance of the obvious fixed length code,

Â where we used 2 bits for each of the 4 characters, with that of the variable

Â length code that's also prefix-free that we mentioned on the previous slide.

Â And we're going to measure the performance of these codes by looking, on average,

Â how many bits do you need to encode a character.

Â Where the average is over the frequencies of the four different symbols.

Â So for the fixed-length encoding, of course, it's two bits per symbol.

Â We don't even need the average.

Â Just whatever the symbol is, it uses exactly two bits.

Â So what about the variable length encoding that's shown on the right in pink?

Â How many bits, on average, for an average character, given these frequencies of

Â the different symbols, are needed to encode a character of the alphabet sigma?

Â Okay, so the correct answer is the second one.

Â It's, on average, 1.55 bits per character.

Â So what's the computation?

Â Well, 60% of the time, it's going to use only 1 bit, and

Â that's where the big savings comes from.

Â 1 bit is all that's needed whenever we see an A, and most of the characters are As.

Â We don't do too bad when we see a B either, which is 25% of the time.

Â We're only using 2 bits for each B.

Â Now it is true that Cs and Ds, we're paying a price.

Â We've having to use 3 bits for each of those, but there aren't very many.

Â Only 10% of the time is it a C and 5% of the time is it a D.

Â And if you add up the result,

Â that's taking the average over the simple frequencies, we get the result of 1.55.

Â So this example draws our attention to a very neat algorithmic opportunity.

Â So namely, given a alphabet and frequencies of the symbols,

Â which in general are not uniform,

Â we now know that the obvious solution fixed length codes need not be optimal.

Â We can improve upon them using variable length prefix-free codes.

Â So the computation you probably want to solve is which one is the best?

Â How do we get optimal compression?

Â Which variable length code gives us the minimum average encoding

Â length of a symbol from this alphabet?

Â So Huffman codes are the solution to that problem.

Â We'll start developing them in the next video.

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