You are on page 1of 3

LZW Algorithm:

There are two categories of compression techniques, lossy and lossless. Whilst each
uses different techniques to compress files, both have the same aim: To look for
duplicate data in the graphic (GIF for LZW) and use a much more compact data
representation. Lossless compression reduces bits by identifying and eliminating
statistical redundancy. No information is lost in lossless compression. On the other
hand, Lossy compression reduces bits by removing unnecessary or less important
information. Lossless compression methods include RLE (Run Length Encoding),
string-table compression, LZW (Lempel Ziff Welch), and zlib.
The LZW algorithm is a very common compression technique. This algorithm is typically
used in GIF and optionally in PDF and TIFF. Unix’s ‘compress’ command, among other
uses. It is lossless, meaning no data is lost when compressing. The algorithm is simple
to implement and has the potential for very high throughput in hardware
implementations. It is the algorithm of the widely used Unix file compression utility
compress and is used in the GIF image format.

To compress data using the LZW algorithm, a dictionary is initialized that contains all
possible characters, each with a unique code. The algorithm reads the data and
compares it to the dictionary to find matches. When a match is found, the algorithm
outputs the corresponding code and adds the new string to the dictionary.

To decompress the compressed data, the LZW algorithm reads the codes and uses
them to reconstruct the original data. The algorithm uses the same dictionary that was
used during compression and outputs the corresponding character for each code.

The LZW algorithm is frequently used in file compression applications, such as the GIF
and TIFF image file formats, and in Unix's compress and gzip utilities. Although the
LZW algorithm provides good compression for text files, its performance on binary files
may not be as good.

Code in python:

def lzw_compress(data):
# Initialize dictionary with all possible characters
dictionary = {chr(i): i for i in range(256)}
result = []
string = ""
for symbol in data:
# Add symbol to string if it exists in dictionary
if string + symbol in dictionary:
string += symbol
else:
# Output code for string and add new string to dictionary
result.append(dictionary[string])
dictionary[string + symbol] = len(dictionary)
string = symbol
# Output code for last string
if string:
result.append(dictionary[string])
return result

def lzw_decompress(data):
# Initialize dictionary with all possible characters
dictionary = {i: chr(i) for i in range(256)}
result = ""
string = chr(data.pop(0))
for code in data:
# Look up string for code in dictionary
if code in dictionary:
output = dictionary[code]
elif code == len(dictionary):
# Handle case for special string not in dictionary
output = string + string[0]
else:
raise ValueError("Invalid code")
result += output
# Add new string to dictionary
dictionary[len(dictionary)] = string + output[0]
string = output
return result

input_data = input("Please, input your data to send:" )


compressed_data = lzw_compress(input_data)
decompressed_data = lzw_decompress(compressed_data)
print("...Submitted by Shambhavi Adhikari...")
print("Input data: ", input_data)
print("Compressed data: ", compressed_data)
print("Decompressed data: ", decompressed_data)
Output:

You might also like