0% found this document useful (0 votes)
39 views10 pages

HuffmanCoding Proof

This document discusses greedy algorithms and optimal prefix codes. It proves two lemmas: 1) In an optimal prefix code, the codes for the lowest frequency characters can differ only in the last bit. 2) If a code is optimal for an alphabet without two characters, it remains optimal when those characters are merged into a single new character. The proofs modify example code trees and use the optimality of existing codes to show the modified codes must also be optimal.

Uploaded by

f20210467
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
39 views10 pages

HuffmanCoding Proof

This document discusses greedy algorithms and optimal prefix codes. It proves two lemmas: 1) In an optimal prefix code, the codes for the lowest frequency characters can differ only in the last bit. 2) If a code is optimal for an alphabet without two characters, it remains optimal when those characters are merged into a single new character. The proofs modify example code trees and use the optimality of existing codes to show the modified codes must also be optimal.

Uploaded by

f20210467
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 10

Greedy Algorithms

Dr. Tathagata Ray


Professor, BITS Pilani, Hyderabad Campus
BITS Pilani [email protected]
Hyderabad Campus
Lemma

Let be an alphabet in which each character has frequency


. Let and be two characters in having the lowest
frequencies. Then there exists an optimal prefix code for
in which the codewords for and have the same length
and differ only in the last bit.

BITS Pilani, Hyderabad Campus


Proof

• (sketch)
• Take any tree representing an arbitrary optimal prefix
code.
• Modify to a new tree such that the characters and with
lowest frequencies appear as sibling leaves of
maximum depth in .
• Thus and will have the same length codeword only
differing in the last bit.
• Show that will also be an optimal prefix code.

BITS Pilani, Hyderabad Campus


Proof contd

• Let and be two characters that are sibling leaves of


maximum depth in .
• Assume and .
• Since characters and are with lowest frequencies.
• and .
• Note that because otherwise

BITS Pilani, Hyderabad Campus


T T’

x a

y y

a b x b

BITS Pilani, Hyderabad Campus


T’ T’’

a a

y b

x b x y

BITS Pilani, Hyderabad Campus


Proof contd.

• -

• Thus .
• Similarly we can shown .
• But we know that is the optimal tree and hence .
• Hence Thus is an optimal tree in which and will have the
same length codeword only differing in the last bit.

BITS Pilani, Hyderabad Campus


Lemma

Let be an alphabet in which each character has frequency


. Let and be two characters in having the lowest
frequencies. Let be the alphabet with the characters
and removed and a new character added, so that
Define for as for , except that Let be any tree
representing an optimal prefix code for the alphabet .
Then the tree , obtained from by replacing the leaf node
for with an internal node having and as children,
represents an optimal prefix code for the alphabet .

BITS Pilani, Hyderabad Campus


Proof

• Express cost of in terms of cost of .


• For each character we have , and hence .
• Also , we have

• Thus
• It means

BITS Pilani, Hyderabad Campus


Proof contd

• Now we assume that does not represent an optimal


prefix code for .
• Then there must exist another tree ’’ such that .
• Now from previous lemma, has and as siblings.
• Let be the tree where and replaced by their common
parent with frequency .
• Then
• This is a contradiction to the fact that represents an
optimal prefix code for .

BITS Pilani, Hyderabad Campus

You might also like