Warning: foreach() argument must be of type array|object, bool given in /var/www/html/web/app/themes/studypress-core-theme/template-parts/header/mobile-offcanvas.php on line 20

The basic intuition behind Huffman’s algorithm, that frequent blocks should have short encodings and infrequent blocks should have long encodings, is also at work in English, where typical words like I, you, is, and, to, from, and so on are short, and rarely used words like velociraptor are longer.

However, words like fire!, help!, and run! are short not because they are frequent, but perhaps because time is precious in situations where they are used.

To make things theoretical, suppose we have a file composed of m different words, with frequencies f1,...,fm. Suppose also that for the ithword, the cost per bit of encoding is ci. Thus, if we find a prefix-free code where the ithword has a codeword of length Ii, then the total cost of the encoding will be localid="1659078764835" fi·ci·li.

Show how to modify Huffman’s algorithm to find the prefix-free encoding of minimum total cost.

Short Answer

Expert verified

Combine the two trees with a short spectral region until you reach the root node. Then, with each character, verify the tree as well as traverse it to create the code word.

Step by step solution

01

Step 1: Minimum total cost of prefix-free encoding

Let's say we have a file containing "m" distinct terms and their accompanying frequency distributions. f1,f2,...fmand cost per bit for “ith” word is denoted as.

02

Step 2: Cost of prefix-free encoding

Huffman’s algorithm to find the cost of prefix-free encoding is given below:

cost=i=1mfili

Where,

f1” denotes as frequency distribution of the “ith” word.

Ii” denotes as length of the “ith” word.

The cost of prefix-free encoding can also be represented as,

cost=i=1mfili

Where,

fi.ci” denotes as frequency of the “ith” word.

Ii” denotes as length of the “ith” word.

03

Step 3: Minimum cost of prefix-free encoding

Here, Huffman developed a greedy algorithm to solve this problem and produces the minimum cost prefix code. The generated code is called as Huffman encoding.

Huffman encoding is an easy method.

• Each tree contains only one node, which corresponds to each letter.

• Join the pair of tree with the small range of frequency until to get the root node.

• Then, check the tree for each character and traverse the tree to write the code word.

o Here, “0” bit for left side traversal of tree.

o “1” bit for right side traversal of tree.

Note that the depth of the leaf in “ith” word in a tree “di” is equal to the length of the “ith” word “Ii” with that leaf.

Then, the minimum cost of prefix-free encoding can also be represented as,

cost=i=1mfidi

Where,

“localid="1658922807296" fi” denotes as frequency distribution of the“localid="1658922791600" ith” word.

“localid="1658922833155" di” denotes as depth of the“localid="1658922841716" ith” word.

Therefore, the Huffman encoding is considered as the minimum cost of prefix-free encoding.

For example:

Construction of the Huffman encoding tree:

Inputs are,

• Character A with frequency of 31%, character C with frequency of 20%, character G with frequency of 9%, and character T with frequency of 40%.

• Huffman encoding is shown below and frequencies are shown in square brackets.

From the Huffman encoding tree,

• Traverse the tree for character “A” from the root node is, 01 .

• Traverse the tree for character “C” from the root node is, 000 .

• Traverse the tree for character “G” from the root node is, 001 .

• Traverse the tree for character “T” from the root node is, 1 .

Hence, the Huffman encoding table is shown below:

Unlock Step-by-Step Solutions & Ace Your Exams!

  • Full Textbook Solutions

    Get detailed explanations and key concepts

  • Unlimited Al creation

    Al flashcards, explanations, exams and more...

  • Ads-free access

    To over 500 millions flashcards

  • Money-back guarantee

    We refund you if you fail your exam.

Over 30 million students worldwide already upgrade their learning with Vaia!

One App. One Place for Learning.

All the tools & learning materials you need for study success - in one app.

Get started for free

Most popular questions from this chapter

Design a linear-time algorithm for the following task.

Input: A connected, undirected graphG.

Question:Is there an edge you can remove fromGwhile still leavingGconnected?

Can you reduce the running time of your algorithm toO(V)?

Give a linear-time algorithm that takes as input a tree and determines whether it has a perfect matching: a set of edges that touches each node exactly once.

A feedback edge set of an undirected graph G(V,E) is a subset of edgesE'Ethat intersects every cycle of the graph. Thus, removing the edges will render the graph acyclic.

Give an efficient algorithm for the following problem:

Input: Undirected graph G(V,E) with positive edge weights we.

Output: A feedback edge set E'Eminimum total weight eE'we.

Give You are given a graphG=(V,E)with positive edge weights, and a minimum spanning tree T=(V,E)with respect to these weights; you may assume GandTare given as adjacency lists. Now suppose the weight of a particular edge eE'is modified fromw(e)to a new value w'(e). You wish to quickly update the minimum spanning tree T to reflect this change, without recomputing the entire tree from scratch. There are four cases. In each case give a linear-time algorithm for updating the tree.

(a) eE'and w'(e)>w(e) .

(b) role="math" localid="1658907878059" eE'and w'(e)>w(e) .

(c) role="math" localid="1658907882667" eE'and w'(e)>w(e) .

(d) role="math" localid="1658907887400" eE'and w'(e)>w(e) .

Let G=(V,E) be an undirected graph. Prove that if all its edge weights are distinct, then it has a unique minimum spanning tree

Show that if an undirected graph with n vertices has k connected components, then it has at least n - k edges.

See all solutions

Recommended explanations on Computer Science Textbooks

View all explanations

What do you think about this solution?

We value your feedback to improve our textbook solutions.

Study anywhere. Anytime. Across all devices.

Sign-up for free