Docsity

Data Compression - Algorithms and Applications in Java - Lecture Slides, Slides of Computer Science

Partial preview of the text

Download Data Compression - Algorithms and Applications in Java - Lecture Slides and more Slides Computer Science in PDF only on Docsity!

Data Compression

  • Reduce the size of data.

 Reduces storage space and hence storage cost.

  • Compression ratio = original data size/compressed data size  Reduces time to retrieve and transmit data.

Lossless And Lossy Compression

  • compressedData = compress(originalData)
  • decompressedData = decompress(compressedData)
  • When originalData = decompressedData, the

compression is lossless.

  • When originalData != decompressedData, the

compression is lossy.

Text Compression

• Lossless compression is essential.

  • Popular text compressors such as

zip and Unix’s compress are based

on the LZW (Lempel-Ziv-Welch)

method.

LZW Compression

  • Character sequences in the original text are

replaced by codes that are dynamically

determined.

  • The code table is not encoded into the

compressed text, because it may be

reconstructed from the compressed text during

decompression.

LZW Compression

  • Original text = abababbabaabbabbaabba
  • Compression is done by scanning the original text

from left to right.

  • Find longest prefix p for which there is a code in the

code table.

  • Represent p by its code pCode and assign the next

available code number to pc, where c is the next

character in the text that is to be compressed.

code key

a

b

LZW Compression

  • Original text = abababbabaabbabbaabba
  • p = a
  • pCode = 0
  • c = b
  • Represent a by 0 and enter ab into the code table.
  • Compressed text = 0

code key

a

b

ab

LZW Compression

• Original text = abababbabaabbabbaabba

  • Compressed text = 01

code key

a

b

ab

ba

  • p = ab
  • pCode = 2
  • c = a
  • Represent ab by 2 and enter aba into the code table.
  • Compressed text = 012

aba

LZW Compression

• Original text = abababbabaabbabbaabba

  • Compressed text = 012

code key

a

b

ab

ba

  • p = ab
  • pCode = 2
  • c = b
  • Represent ab by 2 and enter abb into the code table.
  • Compressed text = 0122

aba

abb

LZW Compression

• Original text = abababbabaabbabbaabba

  • Compressed text = 01223

code key

a

b

ab

ba

  • p = ba
  • pCode = 3
  • c = a
  • Represent ba by 3 and enter baa into the code table.
  • Compressed text = 012233

aba

abb

bab

baa

LZW Compression

• Original text = abababbabaabbabbaabba

  • Compressed text = 012233

code key

a

b

ab

ba

  • p = abb
  • pCode = 5
  • c = a
  • Represent abb by 5 and enter abba into the code

table.

  • Compressed text = 0122335

aba

abb

bab

baa

abba

LZW Compression

  • Original text = abababbabaabbabbaabba
  • Compressed text = 01223358

code key

a

b

ab

ba

  • p = abba
  • pCode = 8
  • c = null
  • Represent abba by 8.
  • Compressed text = 012233588

aba

abb

bab

baa

abba

abbaa

Code Table Representation

  • Dictionary.

 Pairs are (key, element) = (key,code).  Operations are : get(key) and put(key, code)

  • Limit number of codes to 2 12.
  • Use a hash table.

 Convert variable length keys into fixed length keys.  Each key has the form pc, where the string p is a key that is already in the table.  Replace pc with (pCode)c.

code key

a

b

ab

ba

aba

abb

bab

baa

abba

abbaa

LZW Decompression

  • Original text = abababbabaabbabbaabba
  • Compressed text = 012233588
  • Convert codes to text from left to right.
  • 0 represents a.
  • Decompressed text = a
  • pCode = 0 and p = a.
  • p = a followed by next text character (c) is entered

into the code table.

code key

a

b

LZW Decompression

  • Original text = abababbabaabbabbaabba
  • Compressed text = 012233588
  • 1 represents b.
  • Decompressed text = ab
  • pCode = 1 and p = b.
  • lastP = a followed by first character of p is entered

into the code table.

code key

a

b

ab