2

Unix's LZW Compression Algorithm: How Does It Work?

 3 years ago
source link: https://hackernoon.com/unixs-lzw-compression-algorithm-how-does-it-work-cp65347h
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Unix's LZW Compression Algorithm: How Does It Work?

Now, we’ll iterate through the rest of the text — character by character — building up a phrase / character sequence. If the inclusion of a new character creates a phrase that doesn’t currently exist in our dictionary, we will ignore this breaking character and output the code that matches the previous state of our character sequence.

0 reactions

Then, we’ll add this new phrase with this breaking character into our dictionary and assign it a new code.

0 reactions

For example, let’s say our input text is:

0 reactions
... app [cursor] appl ....

Our dictionary might look something like this:

0 reactions
{
  ...
  "app": 324,
  ....
}

The next portion of text we come across is “appl” which isn’t currently in our dictionary.

0 reactions

So, we’ll output the code for the existing phrase match of “app“ and then we’ll add “appl” to our dictionary with a new code.

0 reactions

The output after this iteration might look something like this:

0 reactions
Dictionary: { … “app”: 324, “appl”: 325 …} 
Output: … 324 …

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK