0% found this document useful (0 votes)
2 views8 pages

Huffman Coding Solution

Uploaded by

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

Huffman Coding Solution

Uploaded by

Sam Wilson
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Huffman Coding Solution

Uploaded image
answer the question

To solve this Huffman coding problem, we'll follow these steps:

Given Frequencies

Character Frequency
a 10

e 15
i 12

o 3
u 4

s 13

t 1

Step 1: Build the Huffman Tree

We combine the two nodes with the smallest frequencies until only one node remains. Let’s denote each
node as `(char, freq)`:

1. Combine `t(1)` and `o(3)` → `to(4)`


2. Combine `u(4)` and `to(4)` → `uto(8)`
3. Combine `a(10)` and `i(12)` → `ai(22)`

4. Combine `e(15)` and `s(13)` → `es(28)`


5. Combine `uto(8)` and `ai(22)` → `utoai(30)`

6. Combine `utoai(30)` and `es(28)` → `utoaies(58)`


7. Combine `utoaies(58)` with nothing left → Done (root of tree)

Now let’s assign binary digits (0 = left, 1 = right), starting from the root and working down the tree to get
the codes.

Step 2: Assign Huffman Codes

(Example structure shown; the actual tree could have multiple valid Huffman codes, but here’s one
possibility.)
Let’s assign:

Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 1/8
`utoaies(58)`

Left → `utoa(30)`

Left → `uto(8)`
Left → `t(1)` → 000

Right → `o(3)` → 001


Right → `u(4)` → 01
Right → `ai(22)`

Left → `a(10)` → 100


Right → `i(12)` → 101

Right → `es(28)`
Left → `s(13)` → 110

Right → `e(15)` → 111

Final Huffman Codes

Character Frequency Code


t 1 000

o 3 001
u 4 01

a 10 100
i 12 101
s 13 110

e 15 111

Step 3: Average Code Length


1 × 3 + 3 × 3 + 4 × 2 + 10 × 3 + 12 × 3 + 13 × 3 + 15 × 3 174
Avg. Code Length = = = 3 bits
58 58
​ ​

Step 4: Total Length of Encoded Message

Total Length = ∑(frequency × code length) = 174 bits

✅ Final Answers:
a. Huffman Codes:

a: 100

e: 111

i: 101

Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 2/8
o: 001

u: 01

s: 110
t: 000

b.

Average Code Length: 3 bits

Total Length of Huffman Encoded Message: 174 bits

Would you like a diagram of the Huffman tree as well?

yes

Image created

Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 3/8
explain the solution with long, step-by-step solution

Sure! Let’s walk through the Huffman coding problem in a long, detailed, and step-by-step manner:

Problem Statement Recap


You are given the following characters with their frequencies:

Character Frequency

a 10
e 15
i 12
o 3

Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 4/8
Character Frequency

u 4
s 13
t 1
We are to:

a. Determine the Huffman Code for each character and the average code length

b. Compute the total length of the Huffman encoded message

Step-by-Step Huffman Coding Algorithm


Step 1: List Characters in Priority Queue (Min-Heap)

Each character is treated as a leaf node with a frequency (weight). Start by creating nodes for each
character and insert them into a priority queue sorted by frequency.

less

Initial Min-Heap (frequency-based):


t:1, o:3, u:4, a:10, i:12, s:13, e:15

Step 2: Build the Huffman Tree

We build the tree by repeatedly combining the two nodes with the lowest frequencies into a new
node whose frequency is the sum of the two. This new node is inserted back into the heap.

Iteration 1:

Remove: `t(1)` and `o(3)`

Combine into node: `to(1+3 = 4)`

css

Heap: u(4), to(4), a(10), i(12), s(13), e(15)

Iteration 2:

Remove: `to(4)` and `u(4)`


Combine into node: `uto(4+4 = 8)`

css

Heap: a(10), i(12), s(13), e(15), uto(8)

Iteration 3:

Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 5/8
Remove: `a(10)` and `i(12)`
Combine into node: `ai(10+12 = 22)`

css

Heap: s(13), e(15), uto(8), ai(22)

Iteration 4:

Remove: `s(13)` and `e(15)`


Combine into node: `se(13+15 = 28)`

css

Heap: uto(8), ai(22), se(28)

Iteration 5:

Remove: `uto(8)` and `ai(22)`

Combine into node: `utoai(8+22 = 30)`

css

Heap: se(28), utoai(30)

Iteration 6:

Remove: `se(28)` and `utoai(30)`

Combine into final root node: `root(58)`

csharp

Huffman Tree complete with total weight = 58

Step 3: Assign Binary Huffman Codes

Now that we have the tree, we assign binary codes:

Left edge = 0

Right edge = 1

We traverse the tree from the root to each leaf and record the path as that character's code.
Let’s trace the full path:

root(58)

Left: `utoai(30)` → prefix `0`

Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 6/8
Left: `uto(8)` → prefix `00`

Left: `t(1)` → `000`


Right: `o(3)` → `001`

Right: `u(4)` → `01`


Right: `ai(22)` → prefix `1`

Left: `a(10)` → `100`

Right: `i(12)` → `101`


Right: `se(28)` → prefix `11`

Left: `s(13)` → `110`

Right: `e(15)` → `111`

Step 4: Final Huffman Codes

Character Frequency Huffman Code

t 1 000
o 3 001

u 4 01
a 10 100

i 12 101

s 13 110
e 15 111

Step 5: Calculate Average Code Length

To calculate the average code length, we use:

∑(frequency × code length)


Avg. Code Length =
Total Frequency

Let's compute the numerator:

t: 1 × 3 =3
o: 3 × 3 =9
u: 4 × 2 =8
a: 10 × 3 = 30
i: 12 × 3 = 36
s: 13 × 3 = 39
e: 15 × 3 = 45

Sum = 3 + 9 + 8 + 30 + 36 + 39 + 45 = 170

Total Frequency = 1 + 3 + 4 + 10 + 12 + 13 + 15 = 58

7
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 7/8
170
Average Code Length = ≈ 2.93 bits
58

Step 6: Total Length of Huffman Encoded Message

This is the total number of bits used for encoding all characters:

Total Bits = ∑(frequency × code length) = 170 bits

✅ Final Answers:
a. Huffman Codes:

a → 100

e → 111
i → 101

o → 001

u → 01
s → 110

t → 000

b.

Average Code Length ≈ 2.93 bits


Total Encoded Message Length = 170 bits

Would you like help solving a similar problem or a Python implementation to generate Huffman codes
automatically?

Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 8/8

You might also like