IIT Bombay Data Structures and Algorithms Prof Ajit
IIT Bombay Data Structures and Algorithms Prof. Ajit A. Diwan Prof. Ganesh Ramakrishnan Prof. Deepak B. Phatak Department of Computer Science and Engineering IIT Bombay Session: Huffman Coding Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay 1
Huffman Coding IIT Bombay • Assigns variable-length codes to input characters • Based on frequency of occurrence of each character • Smallest code is assigned to a character having highest frequency of occurrence • Applications in lossless data compression Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay 2
Huffman Coding IIT Bombay • Consider a file containing characters from ‘a’ to ‘f’ • Frequency of each character is denoted by the number of times they appear in the file Example Character Frequency A 15 B 10 C 12 D 18 E 55 F 16 Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay 3
Huffman Coding IIT Bombay • We wish to construct a ‘unique’ code for each element • We start by looking at a pair of ‘minimal’ frequency elements • We note that ‘B’ occurs 10 times, and ‘C’ 12 times Example Character Frequency A 15 B 10 C 12 D 18 E 55 F 16 Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay 4
Huffman Coding IIT Bombay Character Frequency a 15 b 10 c 12 d 18 e 55 f 16 22 0 b: 10 Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay 22 1 c: 12 5
Huffman Coding IIT Bombay Character Frequency a 15 b 10 c 12 d 18 e 55 f 16 31 22 31 0 a: 15 1 f: 16 0 b: 10 Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay 22 1 c: 12 6
Huffman Coding IIT Bombay Character Frequency a 15 b 10 c 12 d 18 e 55 f 16 31 22 40 40 31 0 a: 15 1 f: 16 0 1 d: 18 0 b: 10 Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay 22 1 c: 12 7
Huffman Coding IIT Bombay Character Frequency a 15 b 10 c 12 d 18 e 55 f 16 31 22 71 71 40 0 1 40 31 0 a: 15 1 f: 16 0 1 d: 18 0 b: 10 Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay 22 1 c: 12 8
Huffman Coding IIT Bombay Character Frequency a 15 b 10 c 12 d 18 e 55 f 16 126 31 22 71 126 40 1 0 e: 55 71 0 1 40 31 0 a: 15 1 f: 16 0 1 d: 18 0 b: 10 Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay 22 1 c: 12 9
Huffman Coding IIT Bombay • To Find a Code Word • Follow the path from root node to the leaf node e: 0 a: 100 f: 101 d: 110 b: 1110 c: 1111 Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay 126 1 0 e: 55 71 0 1 40 31 0 a: 15 1 f: 16 0 1 d: 18 0 b: 10 22 1 c: 12 10
Remarks on Huffman Coding IIT Bombay Huffman codes are type of prefix codes [1] If you see the code words generated in the previous slide, you will find that there is no whole code word in the system that is a prefix (initial segment) of any other code word in the system Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay 11
References IIT Bombay [1] Wikipedia article on Prefix Code(variable-length code), available at https: //en. wikipedia. org/wiki/Prefix_code, accessed on Aug 8, 2016. Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay 12
IIT Bombay Thank you Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay 13
- Slides: 13