Dt Compression Reduce the size of dt. Reduces storge spce nd hence storge cost. Compression rtio = originl dt size / compressed dt size Reduces time to trnsmit nd retrieve dt. Reduces the storge requirement. (prticulrly useful in emedded systems, network ridges, routers etc.) Adpted from Shni s Dt Structures nd Applictions slides.
Lossless And Lossy Compression compresseddt = compress(originldt) decompresseddt = decompress(compresseddt) When originldt = decompresseddt, the compression is lossless. When originldt!= decompresseddt, the compression is lossy.
Lossless And Lossy Compression Lossy compressors generlly otin much higher compression rtios thn do lossless compressors. Sy vs.. Lossless compression is essentil in pplictions such s text file compression. Lossy compression is cceptle in mny imging pplictions. In video trnsmission, slight loss in the trnsmitted video is not noticed y the humn eye.
Text Compression Lossless compression is essentil. Populr text compressors such s zip nd Unix s compress re sed on the LZW (Lempel-Ziv-Welch) method.
LZW Compression Chrcter sequences in the originl text re replced y s tht re dynmiclly determined. The tle is not end into the compressed text, ecuse it my e reconstructed from the compressed text during decompression.
LZW Compression Assume the letters in the text re limited to {, }. In prctice, the lphet my e the 56 chrcter ASCII set. The chrcters in the lphet re ssigned numers eginning t. The initil tle is:
LZW Compression Originl text = Compression is done y scnning the originl text from left to right. Find longest prefix p for which there is in the tle. Represent p y its pcode nd ssign the next ville numer to pc, where c is the next chrcter in the text tht is to e compressed.
LZW Compression Originl text = p = pcode = c = Represent y nd enter into the tle. Compressed text =
LZW Compression Originl text = Compressed text = p = pcode = c = Represent y nd enter into the tle. Compressed text =
LZW Compression Originl text = Compressed text = p = pcode = c = Represent y nd enter into the tle. Compressed text =
LZW Compression 5 Originl text = Compressed text = p = pcode = c = Represent y nd enter into the tle. Compressed text =
LZW Compression 5 6 Originl text = Compressed text = p = pcode = c = Represent y nd enter into the tle. Compressed text =
LZW Compression 5 6 7 Originl text = Compressed text = p = pcode = c = Represent y nd enter into the tle. Compressed text =
LZW Compression 5 6 7 8 Originl text = Compressed text = p = pcode = 5 c = Represent y 5 nd enter into the tle. Compressed text = 5
LZW Compression 5 6 7 8 9 Originl text = Compressed text = 5 p = pcode = 8 c = Represent y 8 nd enter into the tle. Compressed text = 58
LZW Compression 5 6 7 8 9 Originl text = Compressed text = 58 p = pcode = 8 c = null Represent y 8. Compressed text = 588
Code Tle Representtion 5 6 7 8 9 Dictionry. Pirs re (, element) = (,). Opertions re : get() nd put(, ) Limit numer of s to. Use hsh tle. Convert vrile length s into fixed length s. Ech hs the form pc, where the string p is tht is lredy in the tle. Replce pc with (pcode)c.
Code Tle Representtion 5 6 7 8 9 5 6 7 8 9 5 8
Implementtion of LZW lgorithm void Compress() {// Lempel-Ziv-Welch compressor. ChinHshTle<element, long> h(d); element e; for (int i = ; i < lph; i++) {// initilize e. = i; e. = i; h.insert(e); } int used = lph; // s used // input nd compress unsigned chr c; in.get(c); long p = c; // prefix
Implementtion of LZW lgorithm if (!in.eof()) {// file length is > do {// process rest of file in.get(c); if (in.eof()) rek; // finished long k = (p << ByteSize) + c; // see if for k in dictionry if (h.serch(k, e)) p = e.; // yes else {// k not in tle output(p); if (used < s) // crete new {e. = used++; e. = (p << ByteSize) c; h.insert(e);} p = c;} } while (true); output(p); if (sttus) {c = LeftOver << excess; out.put(c);} } out.close(); in.close(); }
LZW Decompression Originl text = Compressed text = 588 Convert s to text from left to right. represents. Decompressed text = pcode = nd p =. p = followed y next text chrcter (c) is entered into the tle.
LZW Decompression Originl text = Compressed text = 588 represents. Decompressed text = pcode = nd p =. lstp = followed y first chrcter of p is entered into the tle.
LZW Decompression Originl text = Compressed text = 588 represents. Decompressed text = pcode = nd p =. lstp = followed y first chrcter of p is entered into the tle.
LZW Decompression Originl text = Compressed text = 588 represents Decompressed text =. pcode = nd p =. lstp = followed y first chrcter of p is entered into the tle.
LZW Decompression 5 Originl text = Compressed text = 588 represents Decompressed text =. pcode = nd p =. lstp = followed y first chrcter of p is entered into the tle.
LZW Decompression 5 6 Originl text = Compressed text = 588 represents Decompressed text =. pcode = nd p =. lstp = followed y first chrcter of p is entered into the tle.
LZW Decompression 5 6 7 Originl text = Compressed text = 588 5 represents Decompressed text =. pcode = 5 nd p =. lstp = followed y first chrcter of p is entered into the tle.
LZW Decompression 5 6 7 8 Originl text = Compressed text = 588 8 represents??? When is not in the tle, its is lstp followed y first chrcter of lstp. lstp = So 8 represents.
LZW Decompression 5 6 7 8 9 Originl text = Compressed text = 588 8 represents Decompressed text =. pcode = 8 nd p =. lstp = followed y first chrcter of p is entered into the tle.
Code Tle Representtion 5 6 7 8 9 Dictionry. Pirs re (, element) = (, wht the represents) = (, Key). Opertions re : get() nd put(, ) Keys re integers,,, Use D rry Tle. Tle[] = Key. Ech hs the form pc, where the string p is tht is lredy in the tle. Replce pc with (pcode)c.
Compression. Time Complexity O(n) expected time, where n is the length of the text tht is eing compressed. Decompression. O(n) time, where n is the length of the decompressed text.