IMPLEMENTATION OF CODING HUFFMAN Presented By Snehal Sable49
IMPLEMENTATION OF CODING HUFFMAN Presented By: Snehal Sable(49). Madhura Puranik(21). Kalyani Bothra(66). Dimple Bedmutha(12). 10/23/2021 IMPLEMENTATION OF HUFFMAN CODING 1
Purpose of Huffman Coding Proposed by Dr. David A. Huffman in 1952 �“A Method for the Construction of Minimum Redundancy Codes” � Applicable to many forms of data transmission �example: text files � 10/23/2021 IMPLEMENTATION OF HUFFMAN CODING 2
Huffman Coding Huffman coding is a technique used to compress files for transmission � Uses statistical coding �more frequently used symbols have shorter code words. �Less frequently used symbols have shorter code words. � Works well for text and fax transmissions � An Application of Binary Trees and Priority Queues � 10/23/2021 IMPLEMENTATION OF HUFFMAN CODING 3
Prefix code � The fixed-length character code that has characters places only at the leaves guarantees that any bit sequence can be decoded unambiguously � Prefix code - characters may have varying lengths as long as no character code is a prefix of another code � That means that characters can be only in leafs 10/23/2021 IMPLEMENTATION OF HUFFMAN CODING 4
10/23/2021 IMPLEMENTATION OF HUFFMAN CODING 5
Huffman encoding � Greedy Approach: �Sort characters by frequency. �Form two lowest weight nodes into a sub-tree. ○ Sub-tree weight = sum of weights of nodes. �Move new tree to correct place. � It constructs the optimal prefix code. 10/23/2021 IMPLEMENTATION OF HUFFMAN CODING 6
Huffman Coding Algorithm 1. Scan text to be compressed and tally occurrence of all characters. 2. Sort or prioritize characters based on number of occurrences in text. 3. Build Huffman code tree based on prioritized list. 4. Perform a traversal of tree to determine all code words. 5. Scan text again and create new file using the Huffman codes. 10/23/2021 IMPLEMENTATION OF HUFFMAN CODING 7
Huffman Encoding -Operation Initial sequence Sorted by frequency Combine lowest two into sub-tree Move it to correct place 10/23/2021 IMPLEMENTATION OF HUFFMAN CODING 8
Cont… After shifting sub-tree to its correct place. . . Combine next lowest pair Move sub-tree to correct place 10/23/2021 IMPLEMENTATION OF HUFFMAN CODING 9
Cont… Move the new tree to the correct place. . . Now the lowest two are the “ 14” sub-tree and D Combine and move to correct place 10/23/2021 IMPLEMENTATION OF HUFFMAN CODING 10
Cont… Move the new tree to the correct place. . . Now the lowest two are the “ 25” and “ 30” trees Combine and move to correct place 10/23/2021 IMPLEMENTATION OF HUFFMAN CODING 11
Cont… Combine last two trees A=0, B=101, C=100, D=111, E=1101, F=1100 10/23/2021 IMPLEMENTATION OF HUFFMAN CODING 12
Huffman Encoding - Time Complexity � Sort keys � Repeat n times �Form new sub-tree �Move sub-tree O(n log n) O(1) O(log n) (binary search) �Total � Overall 10/23/2021 O(n log n) IMPLEMENTATION OF HUFFMAN CODING 13
Huffman decoding -Operation Ø How do we decode a Huffman-encoded bit string? With these variable length strings, it's not possible to break up an encoded string of bits into characters!“ Ø The decoding procedure is deceptively simple. Starting with the first bit in the stream, one then uses successive bits from the stream to determine whether to go left or right in the decoding tree. When we reach a leaf of the tree, we've decoded a character, so we place that character onto the (uncompressed) output stream. The next bit in the input stream is the first bit of the next character. 10/23/2021 IMPLEMENTATION OF HUFFMAN CODING 14
HUFFMAN ENCODING - DECODING 10/23/2021 IMPLEMENTATION OF HUFFMAN CODING 15
FIXED LENGTH bits Char (cᵢ) A B C D E F TOTAL 10/23/2021 Freq Prefix Depth Cost (fᵢ) Code (dᵢ) =dᵢ*fᵢ 1 0000 4 4 2 0101 4 8 1 0100 4 4 3 0111 4 12 1 1101 4 4 2 1100 4 8 BITS = 40 IMPLEMENTATION OF HUFFMAN CODING 16
variable length bits Char (cᵢ) A B C D E F TOTAL 10/23/2021 Freq Prefix Depth Cost (fᵢ) Code (dᵢ) =dᵢ*fᵢ 1 0 1 1 2 101 3 6 1 100 3 3 3 111 3 9 1 1101 4 4 2 1100 4 8 BITS = 31 IMPLEMENTATION OF HUFFMAN CODING 17
References [1]. Mac. Kay, D. J. C. , Information Theory, Inference, and Learning Algorithms, Cambridge University Press, 2003. [2]. Wikipedia, http: //en. wikipedia. org/wiki/Huffman_coding [3]. Hu, Y. C. and Chang, C. C. , “A new lossless compression scheme based on Huffman coding scheme for image compression”. 10/23/2021 IMPLEMENTATION OF HUFFMAN CODING 18
THANK YOU!!!! 10/23/2021 IMPLEMENTATION OF HUFFMAN CODING 19
ANY QUERY? ? ? 10/23/2021 IMPLEMENTATION OF HUFFMAN CODING 20
- Slides: 20