Encoding Flashcards
(14 cards)
What is encoding?
Given a set of items n, encode them using an amount of characters
What is fixed length, fixed encoding?
The amount of bits used to encode each item is the same, and the characters used to encode is the same
How many bits are in a file with n-bit binary encoding?
n * the amount of items
What is variable length, fixed encoding?
The amount of bits used varies by how frequently an item appears in the set, but the characters used to encode are the same
What is an example of variable length, fixed encoding?
Morse Code
What is the most common form of variable length, variable encoding?
Huffman encoding
What are the key features of Huffman encoding?
No code is a prefix of another, it can used to compress any kind of file, the length and encoding depend on the file itself
What is required to decode something that has been Huffman encoded?
The Huffman tree that was used to encode it must be sent with the compressed file
What are the steps for Huffman encoding?
- Count the item frequency
- Sort items by frequency, least to most
- Build binary Huffman tree, starting with each item as a single node tree
- Combine the two trees with the smallest values, with the left child as the smaller, right as the larger, and parent as the sum of both
- Repeat until all only one tree remains
How do you use a Huffman tree to encode something?
Assign each “edge” of the tree a value (usually 0 and 1) and trace down the branches to each item. The values passed, in order, is the encoded item
What are the two approaches for implementing Huffman’s algorithm, and which is better?
MinHeap or 2 Queues
Same time complexity for both, 2 Queues easier to implement
How do you find the average number of bits per item?
Sum of the bits*freq for each item, by the sum of all the frequencies
How do you find the Compression Ratio for a Huffman encoded file?
F-A/F * 100
F = Bits for fixed representation
A = Average number of bits
What is one benefit of Huffman encoding?
It guarantees the smallest binary encoding possible