CS : Algorithms LZW Dt Compression
Dt Compression Reduce the size of dt Reduces storge spce nd hence storge cost. Reduces time to retrieve nd trnsmit dt. Compression rtio = originl dt size / compressed dt size
Lossless nd Lossy Compression compresseddt = compress(originldt) decompresseddt = decompress(compresseddt) Lossless compression originldt = decompresseddt Lossy compression originldt!= decompresseddt
Lossless nd 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 codes tht re dynmiclly determined. The code tle is not encoded 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 6 chrcter ASCII set. The chrcters in the lphet re ssigned code numers eginning t The initil code tle is: code
LZW Compression code Originl text = Compression is done y scnning the originl text from left to right. Find longest prefix p for which there is code in the code tle. Represent p y its code pcode nd ssign the next ville code numer to pc, where c is the next chrcter in the text tht is to e compressed.
LZW Compression code Originl text = p = pcode = c = Represent y nd enter into the code tle. Compressed text =
Originl text = Compressed text = p = pcode = c = Represent y nd enter into the code tle. Compressed text =
Originl text = Compressed text = p = pcode = c = Represent y nd enter into the code tle. Compressed text =
Originl text = Compressed text = p = pcode = c = Represent y nd enter into the code tle. Compressed text =
6 Originl text = Compressed text = p = pcode = c = Represent y nd enter into the code tle. Compressed text =
6 7 Originl text = Compressed text = p = pcode = c = Represent y nd enter into the code tle. Compressed text =
6 7 8 Originl text = Compressed text = p = pcode = c = Represent y nd enter into the code tle. Compressed text =
6 7 8 9 Originl text = Compressed text = p = pcode = 8 c = Represent y 8 nd enter into the code tle. Compressed text = 8
6 7 8 9 Originl text = Compressed text = 8 p = pcode = 8 c = null Represent y 8. Compressed text = 88
Code Tle Representtion code 6 7 8 9 Dictionry. Pirs re (, element) = (, code). Opertions re: get() nd put(, code) Limit numer of codes 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 code 6 7 8 9 code 6 7 8 9 8
LZW Decompression code Originl text = Compressed text = 88 Convert codes to text from left to right. represents. Decompressed text = pcode = nd p =. p = followed y next text chrcter (c) is entered into the code tle.
LZW Decompression code Originl text = Compressed text = 88 represents. Decompressed text = pcode = nd p =. lstp = followed y first chrcter of p is entered into the code tle.
Originl text = Compressed text = 88 represents. Decompressed text = pcode = nd p =. lstp = followed y first chrcter of p is entered into the code tle.
Originl text = Compressed text = 88 represents Decompressed text =. pcode = nd p =. lstp = followed y first chrcter of p is entered into the code tle.
Originl text = Compressed text = 88 represents Decompressed text =. pcode = nd p =. lstp = followed y first chrcter of p is entered into the code tle.
6 Originl text = Compressed text = 88 represents Decompressed text =. pcode = nd p =. lstp = followed y first chrcter of p is entered into the code tle.
6 7 Originl text = Compressed text = 88 represents Decompressed text =. pcode = nd p =. lstp = followed y first chrcter of p is entered into the code tle.
6 7 8 Originl text = Compressed text = 88 8 represents??? When code is not in the tle (then, it is the lst one entered), nd its is lstp followed y first chrcter of lstp lstp = So 8 represents
6 7 8 9 Originl text = Compressed text = 88 8 represents Decompressed text =. pcode = 8 nd p =. lstp = followed y first chrcter of p is entered into the code tle.
Code Tle Representtion code 6 7 8 9 Dictionry. Pirs re (, element) = (code, wht the code represents) = (code, codekey). Opertions re : get() nd put(, code) Keys re integers,,, Use D rry codetle. codetle[code] = codekey. Ech code hs the form pc, where the string p is code tht is lredy in the tle Replce pc with (pcode)c
Time Complexity Compression 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.