Before we
proceed, below are the terms we will
be using frequently.
- Code Table: Maintains the Codes & their Values.
- Index Stream: The I/P given to the Encoder.
- Index Buffer: It will be the list of characters we are currently looking at.
- Code Stream: This will be the list of codes generated as O/P.
Compression ALGORITHM
- Initialize Code Table
- Always start by sending a Clear Code (CC) to the code stream.
- Read first index from index stream. This value is now the value for the index buffer
- <LOOP POINT>
- Get the next index from the index stream to the index buffer. We will call this index, K
- Is index buffer + K in our Code Table?
- Yes:
- add K to the end of the index buffer
- No:
- Add a row for index buffer + K into our Code Table with the next smallest code
- Output the code for just the index buffer to our code steam
- Index buffer is set to K
- K is set to nothing
- If there are more indexes, return to LOOP POINT
- Output code for contents of index buffer
- Output End-Of-Information (EOI) Code
Now let us understand using an example.
- · Suppose our I/P Stream is: ABABCDEFABCDCDEFAB
- · Set index stream to point to first character of I/P Stream.
- · Initialize the CODE Table.
CODE
TABLE:
CODE
|
VALUE
|
.
.
|
.
.
|
A
|
#65
|
B
|
#66
|
C
|
#67
|
D
|
#68
|
E
|
#69
|
F
|
#70
|
.
.
|
.
.
|
ASCII value at 255
|
#255
|
CC
|
#256
|
EOI
|
#257
|
Working Procedure of the Algorithm:
WORKING
PROCEDURE:
STEP
|
ACTION
|
INDEX STREAM (K)
|
INDEX BUFFER
|
INDEX BUFFER + K
|
NEW CODE TABLE ENTRY
|
CODE STREAM
|
0
|
init
|
A
|
-
|
-
|
-
|
#256
|
1
|
Read
|
B
|
A
|
A
+ B
|
-
|
-
|
2
|
Not
Found
|
-
|
B
|
-
|
#258
- AB
|
#65
|
3
|
Read
|
A
|
B
|
B
+ A
|
-
|
-
|
4
|
Not
Found
|
-
|
A
|
-
|
#259
– BA
|
#66
|
5
|
Read
|
B
|
A
|
A
+ B
|
-
|
-
|
6
|
Found
|
-
|
A
+ B
|
-
|
-
|
-
|
7
|
Read
|
C
|
A
+ B
|
A
+ B + C
|
-
|
-
|
8
|
Not
Found
|
-
|
C
|
-
|
#260
- ABC
|
#258
|
9
|
Read
|
D
|
C
|
C
+ D
|
-
|
-
|
10
|
Not
Found
|
-
|
D
|
-
|
#261
- CD
|
#67
|
11
|
Read
|
E
|
D
|
D
+ E
|
-
|
-
|
12
|
Not
Found
|
-
|
E
|
-
|
#262
- DE
|
#68
|
13
|
Read
|
F
|
E
|
E
+ F
|
-
|
-
|
14
|
Not
Found
|
-
|
F
|
-
|
#263
- EF
|
#69
|
15
|
Read
|
A
|
F
|
F
+ A
|
-
|
-
|
16
|
Not
Found
|
-
|
A
|
-
|
#264
- FA
|
#70
|
17
|
Read
|
B
|
A
|
A
+ B
|
-
|
-
|
18
|
Read
|
C
|
A
+ B
|
A
+ B + C
|
-
|
-
|
19
|
Read
|
D
|
A
+ B + C
|
A
+ B + C + D
|
-
|
-
|
20
|
Not
Found
|
-
|
D
|
-
|
#265
- ABCD
|
#260
|
21
|
Read
|
C
|
D
|
D
+ C
|
-
|
-
|
22
|
Not
Found
|
-
|
C
|
-
|
#266
- DC
|
#68
|
23
|
Read
|
D
|
C
|
C
+ D
|
-
|
-
|
24
|
Read
|
E
|
C
+ D
|
C
+ D + E
|
-
|
-
|
25
|
Not
Found
|
-
|
E
|
-
|
#267
- CDE
|
#261
|
26
|
Read
|
F
|
E
|
E
+ F
|
-
|
-
|
27
|
Read
|
A
|
E
+ F
|
E
+ F + A
|
-
|
-
|
28
|
Not
Found
|
A
|
-
|
#268
- EFA
|
#263
|
|
29
|
Read
|
B
|
A
|
A
+ B
|
-
|
-
|
30
|
Read
|
NULL
|
-
|
-
|
-
|
#258
|
31
|
-
|
-
|
-
|
-
|
-
|
#257
|
We have already initialized the CODE Table. We start by doing two
things: we O/P the Clear Code (#256) to the Code Stream,
and we read the first character from
the Index Stream, A, into the Index Buffer.
Now, we enter the main LOOP. We read the next index in the Index Stream, B, into K [Step 1]. Next we see if we have a
record for the Index Buffer + K in
the Code Table. In this case, we
looking for AB. Currently, our CODE Table doesn’t happen to have AB. Now we actually add a new row in
our CODE Table that does contain
this value AB. The next available
code is #258, we will let #258 be AB. Now we send the code for the value in Index Buffer, A, to the Code Stream. Hence, the Code will be #65. This is the CODE we O/P. We now RESET the Index Buffer to the value in K
& K becomes nothing [Step 2].
We
continue by reading next index into K
[Step 3]. Now K is A & the Index Buffer is B. Again we look to see if there is a value in our CODE Table for Buffer + K, BA. No there
is none. So, we again add this into CODE
Table & O/P the CODE for B in Code Stream [Step 4].
We
again read next index buffer into K
[Step 5]. Now K is B & the Index Buffer is A. We look into our CODE Table for AB. We found the Match.
So, we add K to the end of Index Buffer & clear out K. Now Index Buffer is AB [Step 5].
Similarly,
we continue until the Index Stream
is EMPTY.
The Final Code Stream would look like this:
#256 #65 #66 #258 #67 #68 #69 #70 #260 #68 #261 #263
#258 #257
This is only 14 CODEs versus the 18 that would be required without
compression. Larger the Size of Data to Compress, higher will be the Compress
Rate because the Probability of Matches found increases sufficiently.