MHB How Do You Calculate the Total Weight of a Huffman Code?

  • Thread starter Thread starter mathmari
  • Start date Start date
  • Tags Tags
    Code Weight
Click For Summary
To calculate the total weight of a Huffman code, determine the weighted path length from the root of the Huffman tree. This involves multiplying the length of each symbol's code by its frequency and summing these products. For example, the contribution of the letter 'a' is calculated as its code length (4) multiplied by its frequency (2), resulting in a total of 8. There is a suggestion that the initial Huffman tree may not be optimal, as another tree configuration could yield a lower total weight. Understanding these calculations is essential for achieving optimal data compression.
mathmari
Gold Member
MHB
Messages
4,984
Reaction score
7
Hey! 😊

We are given the following letters with the respective frequencies:
\begin{equation*}\begin{matrix}a/2 & b/4 & c/7 & d/6 & e/4 & f/5 & g/8 & h/10 & i/3 & j/11\end{matrix}\end{equation*}

For that I have applied the Huffman code and I got the following tree:

Huffman.JPG
Now it is asked for the total weight of the code. How do we calculate that? :unsure:
 
Physics news on Phys.org
mathmari said:
We are given the following letters with the respective frequencies:
\begin{equation*}\begin{matrix}a/2 & b/4 & c/7 & d/6 & e/4 & f/5 & g/8 & h/10 & i/3 & j/11\end{matrix}\end{equation*}

For that I have applied the Huffman code and I got the following tree:

Now it is asked for the total weight of the code. How do we calculate that?
Hey mathmari!

The total weight would be the weighted path length from the root.
The objective of the algorithm is to minimize the total weight, implying that compression is optimal. 🧐

Put differently, it is the length of the resulting code for each symbol multiplied by its frequency and then summed together.
So the contribution of $a$ is $4\times 2=8$, since $a$ is encoded by $0000$, which has length $4$ and it occurs $2$ times. 🤔

I think your tree is not optimal though. I found a different tree with a slightly lower total weight. (Sweating)
 
First trick I learned this one a long time ago and have used it to entertain and amuse young kids. Ask your friend to write down a three-digit number without showing it to you. Then ask him or her to rearrange the digits to form a new three-digit number. After that, write whichever is the larger number above the other number, and then subtract the smaller from the larger, making sure that you don't see any of the numbers. Then ask the young "victim" to tell you any two of the digits of the...

Similar threads

  • · Replies 18 ·
Replies
18
Views
1K
  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 4 ·
Replies
4
Views
4K
Replies
26
Views
3K
  • · Replies 8 ·
Replies
8
Views
1K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 17 ·
Replies
17
Views
1K
  • · Replies 9 ·
Replies
9
Views
2K
  • · Replies 7 ·
Replies
7
Views
2K