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

292 ratings

Stanford University

292 ratings

Course 3 of 4 in the Specialization Algorithms

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

To make sure that Huffman's greedy algorithm is clear,

Â let's go through a slightly larger, more complicated example.

Â So let's work with a six character alphabet.

Â Let's call the letters A, B, C, D, E, F, and let's assume

Â that we're given the weights 3, 2, 6 and 8, 2, 6 for these six characters.

Â Remember, this problem is well-defined even if the weights don't add up to 1.

Â If you prefer working with actual probabilities,

Â feel free to divide these six numbers by 27.

Â In the first step of Huffman's greedy algorithm,

Â we find the letters that have the smallest weights, the smallest frequencies.

Â So in this example, that would be the letters B and E,

Â both of them have weight 2.

Â Now what we do is we merge these two letters into a single meta letter,

Â in effect committing right now to having B and E be siblings in the final tree.

Â After this merger, our alphabet is down to five symbols, the symbols B and

Â E being replaced by the merged symbol BE.

Â And the weight of BE is the sum of the weights of B and E, namely, 4.

Â We can imagine our tree slowly taking shape through these iterations.

Â So after step 1, we know that B and E are going to be siblings and we know that just

Â A, C, D, and F are going to be leaves, that's all we know thus far.

Â In the next iteration, we again look for

Â the two symbols that have the smallest weight.

Â And here the smallest weight symbol is A.

Â It has weight 3.

Â And the runner-up is the merged symbol B sub E.

Â Its combined weight is 4, and that's second overall for these five symbols.

Â So in this step, we merge A with B E.

Â Now our alphabet is down to four symbols, the merged symbol, A, B, E,

Â which has cumulative weight 7, and then the original symbols, C, D, and F,

Â which have their original weights, 6, 8, and 6.

Â As far as our tree, we've now committed to the symbol A appearing as an uncle

Â of the siblings B and E.

Â And again, C, D,

Â and F, we just know they're leaves somewhere in the final tree.

Â In step three,

Â we're going to again pick the two symbols that have the smallest weights.

Â In this case, the two symbols with the smallest weight are C and

Â F, each of weight 6.

Â In our new alphabet, we still have our symbol ABE, it still has weight 7.

Â We still have the symbol D, it still has weight 8.

Â But now we have a new merged symbol CF, and its new weight is 12.

Â As far as our tree, in addition to the information we already knew,

Â we're now committing to having C and F be siblings in the final tree.

Â In step four, we merge the two symbols with the smallest weight.

Â So that would be ABE with its weight of seven and D with its weight of eight.

Â So this leaves us with only two symbols, ABDE and CF.

Â And now we know what both of these subtrees of the root of the final tree

Â are going to look like.

Â Now that we're down to two symbols,

Â the only thing we can do is fuse these two symbols into one,

Â fuse these two subtrees into a single one by uniting them under a common root.

Â That gives us the following final output of Huffman's algorithm.

Â What prefix-free code does this tree correspond to?

Â Well, as usual, let's label all of the left branches with zero and

Â all of the right branches with ones.

Â And now, as usual, the encoding of a character is just the symbols of zeros and

Â ones that you see when you traverse a path from the root to that leaf.

Â So for example, A will be encoded with 000,

Â B with 0010, C with 10, D with 01,

Â E with 0011, and F with 11.

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