Unix's LZW Compression Algorithm: How Does It Work?
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.
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.
Then, we’ll add this new phrase with this breaking character into our dictionary and assign it a new code.
For example, let’s say our input text is:
... app [cursor] appl ....
Our dictionary might look something like this:
{
...
"app": 324,
....
}
The next portion of text we come across is “appl” which isn’t currently in our dictionary.
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.
The output after this iteration might look something like this:
Dictionary: { … “app”: 324, “appl”: 325 …}
Output: … 324 …
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK