next up previous contents
Next: About this document ... Up: Greedy Algorithms Previous: Greedy Algorithms   Contents

Huffman Coding: Introduction [209]




\begin{picture}(560,245)(0,580)
\thicklines\put( 30,740){\circle{10}}
\put( 90,7...
...ut(490,650){\makebox(0,0)[lb]{\raisebox{0pt}[0pt][0pt]{\twlrm 0}}}
\end{picture}



Encoding is First Part of Data File: huff.dat

  7 3 A 6 4 B 5 4 C 52 6 D 22 5 E 12 4 F 14 5 G 15 5 H 0 0
I 1 3 J 0 0 K 0 0 L 17 5 M 10 4 N 0 3 O 4 4 P 53 6 Q 0 0
R 23 5 S 9 4 T 27 5 U 16 5 V 0 0 W 0 0 X 0 0 Y 0 0 Z 0 0

Letter, Bits, Number of Bits

For example: A is 0110 (=6) and is 4 bits long

Instead of using 7 bits/character, now it is reduced: file compression



Huffman: huff [210]

% gcc -o huff huff.c                                   > tcc huff.c

Message is Second Part of Data File: huff.dat

01101111001001101011010110001110011110011101110111001000011
11111110110100111010111001111100000110100010010110110010110
11110000100100100001111111011011110100010000011010011010001
11100010000101001011100101111110100011101110101001110111001

% huff huff.dat
A SIMPLE STRING TO BE ENCODED USING A MINIMAL NUMBER OF BITS
Savings: 21.3%



Huffman: huff.c [211]

#define N 27
#define BITS_PER_LETTER 5
int code[N], len[N], letter[N];

void read_and_write_message() { 
int k,bit;
  char chbit;
  int decimal = 0, length = 0;
  while (fscanf(fp,"%c",&chbit) != EOF) 
    if (chbit != '\n') {
      length++; bit = chbit - '0';
      /* compute: decimal (base 10) =  sum of bits (base 2)          */
      /* example:      52 = 110100; hint: double the old value       */
      decimal = 2*decimal + bit;
      /* for all possible letters                                    */ 
      for (k=0; k<=N-1; k++)
        /* if code is decimal and len is length then printf letter   */ 
        /* example: if code[3]=52 and len[3]=6 then printf letter[3] */
        if ((code[k] == decimal) && (len[k] == length)) {
          printf("%c",letter[k]);
          decimal = 0; length = 0;
          break; 
        }
    }
}



Ted Billard 2001-10-25