15.04.2018 Views

programming-for-dummies

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Lossless Data Compression Algorithms 439<br />

Dictionary encoding<br />

Run-length encoding is a simple algorithm that works well with redundant<br />

characters grouped together but doesn’t work as well with redundant data<br />

scattered throughout. An alternative to RLE is dictionary coding. The basic<br />

idea behind dictionary coding is to replace large data chunks with much<br />

smaller data chunks. Suppose you had the following text:<br />

See Dick. See Jane.<br />

You could replace the redundant text See by a simple code, such as 1, and<br />

create a new string:<br />

1 Dick. 1 Jane.<br />

Now you could replace Dick with 1 code and Jane with another code, such<br />

as 2 and 3 respectively, to create a compressed version, like this:<br />

1 2. 1 3.<br />

Uncompressing this data means replacing the codes with the actual data,<br />

using a dictionary. Each code refers to the actual data, so looking up 1<br />

retrieves the See string, 2 retrieves Dick, and 3 retrieves Jane, as shown in<br />

Figure 4-2.<br />

Figure 4-2:<br />

Uncompressing<br />

data<br />

requires<br />

using a<br />

dictionary to<br />

replace<br />

codes with<br />

actual data.<br />

1 See<br />

2 Dick<br />

3 Jane<br />

1 2 1 3<br />

Book IV<br />

Chapter 4<br />

If you know the data you want to compress ahead of time, such as condensing<br />

the entire contents of an encyclopedia on a DVD, you can optimize the dictionary<br />

to create the smallest codes, which represent the most common data chunks,<br />

such as the word the. In most cases, you don’t know the data to compress ahead<br />

of time, so you need an algorithm that can analyze data, create a dictionary on<br />

the fly, and then compress data using that dictionary. Three popular dictionary<br />

encoding algorithms include LZ77, LZ78, and LZW.<br />

Data Compression<br />

Algorithms

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!