Difference between revisions of "Information Theory/Compression According to Lempel, Ziv and Welch"

From LNTwww
 
(73 intermediate revisions by 5 users not shown)
Line 1: Line 1:
 
   
 
   
 
{{Header
 
{{Header
|Untermenü=Quellencodierung – Datenkomprimierung
+
|Untermenü=Source Coding - Data Compression
 
|Vorherige Seite=Allgemeine Beschreibung
 
|Vorherige Seite=Allgemeine Beschreibung
 
|Nächste Seite=Entropiecodierung nach Huffman
 
|Nächste Seite=Entropiecodierung nach Huffman
Line 7: Line 7:
  
  
==Statische und dynamische Wörterbuchtechniken ==  
+
==Static and dynamic dictionary techniques ==  
 
<br>
 
<br>
Viele Datenkomprimierungsverfahren verwenden Wörterbücher. Die Idee ist dabei die Folgende:  
+
Many data compression methods use dictionaries.&nbsp; The idea is the following:  
*Man konstruiere eine Liste der Zeichenmuster, die im Text vorkommen,  
+
*Construct a list of character patterns that occur in the text,  
*und codiere diese Muster als Indizes der Liste.  
+
*and encode these patterns as indices of the list.  
  
  
Besonders effizient ist diese Vorgehensweise, wenn sich bestimmte Muster im Text häufig wiederholen und dies bei der Codierung auch berücksichtigt wird. Hierbei unterscheidet man:
+
This procedure is particularly efficient if certain patterns are repeated frequently in the text and if this is also taken into account in the coding.&nbsp; A distinction is made here between
*Verfahren mit statischem Wörterbuch,
+
*procedures with static dictionary,
*Verfahren mit dynamischem Wörterbuch.
+
*procedures with dynamic dictionary.
  
  
 
<br><br>
 
<br><br>
$\text{(1) Verfahren mit statischem Wörterbuch}$
+
$\text{(1) &nbsp; Procedures with static dictionary}$
  
Ein statisches Wörterbuch ist nur für ganz spezielle Anwendungen sinnvoll, zum Beispiel für eine Datei der folgenden Form:
+
A static dictionary is only useful for very special applications, for example for a file of the following form:
  
[[File:P_ID2424__Inf_T_2_2_S1a.png|center|frame|Zu bearbeitende Datei in diesem Abschnitt]]
+
[[File:EN_Inf_T_2_2_S1a.png|center|frame|Data file&nbsp; (rubrics:&nbsp; "Name",&nbsp; "Vorname" &nbsp; &rArr;  &nbsp; "first name",&nbsp; "Wohnort" &nbsp; &rArr;  &nbsp; "residence")]]
  
Beispielsweise ergibt sich mit den Zuordnungen
+
For example, the assignments result in
 
      
 
      
 
:$$"\boldsymbol{\rm 0}" \hspace{0.05cm} \mapsto \hspace{0.05cm} \boldsymbol{\rm 000000} \hspace{0.05cm},\hspace{0.15cm} ... \hspace{0.15cm},\hspace{0.05cm}
 
:$$"\boldsymbol{\rm 0}" \hspace{0.05cm} \mapsto \hspace{0.05cm} \boldsymbol{\rm 000000} \hspace{0.05cm},\hspace{0.15cm} ... \hspace{0.15cm},\hspace{0.05cm}
 
"\boldsymbol{\rm 9}" \hspace{0.05cm} \mapsto \hspace{0.05cm} \boldsymbol{\rm 001001} \hspace{0.05cm},
 
"\boldsymbol{\rm 9}" \hspace{0.05cm} \mapsto \hspace{0.05cm} \boldsymbol{\rm 001001} \hspace{0.05cm},
"\hspace{-0.03cm}\_\hspace{-0.03cm}\_\hspace{0.03cm}" \hspace{0.1cm}{\rm (Blank)}\hspace{0.05cm} \mapsto \hspace{0.05cm} \boldsymbol{\rm 001010} \hspace{0.05cm},$$
+
"\hspace{-0.03cm}\_\hspace{-0.03cm}\_\hspace{0.03cm}" \hspace{0.1cm}{\rm (blank)}\hspace{0.05cm} \mapsto \hspace{0.05cm} \boldsymbol{\rm 001010} \hspace{0.05cm},$$
  
:$$"\hspace{-0.01cm}.\hspace{-0.01cm}" \hspace{0.1cm}{\rm (Punkt)}\hspace{0.05cm} \mapsto \hspace{0.05cm} \boldsymbol{\rm 001011} \hspace{0.05cm},
+
:$$"\hspace{-0.01cm}.\hspace{-0.01cm}" \hspace{0.1cm}{\rm (point)}\hspace{0.05cm} \mapsto \hspace{0.05cm} \boldsymbol{\rm 001011} \hspace{0.05cm},
"\hspace{-0.01cm},\hspace{-0.01cm}" \hspace{0.1cm}{\rm (Komma)}\hspace{0.05cm} \mapsto \hspace{0.05cm} \boldsymbol{\rm 001011} \hspace{0.05cm},
+
"\hspace{-0.01cm},\hspace{-0.01cm}" \hspace{0.1cm}{\rm (comma)}\hspace{0.05cm} \mapsto \hspace{0.05cm} \boldsymbol{\rm 001011} \hspace{0.05cm},
 
" {\rm end\hspace{-0.1cm}-\hspace{-0.1cm}of\hspace{-0.1cm}-\hspace{-0.1cm}line}\hspace{0.05cm} \mapsto \hspace{0.05cm} \boldsymbol{\rm 001101} \hspace{0.05cm},$$
 
" {\rm end\hspace{-0.1cm}-\hspace{-0.1cm}of\hspace{-0.1cm}-\hspace{-0.1cm}line}\hspace{0.05cm} \mapsto \hspace{0.05cm} \boldsymbol{\rm 001101} \hspace{0.05cm},$$
  
Line 48: Line 48:
 
"\boldsymbol{\rm ,\_\hspace{-0.03cm}\_Wohnort\hspace{-0.1cm}:\hspace{-0.05cm}\_\hspace{-0.03cm}\_}" \hspace{0.05cm} \mapsto \hspace{0.05cm} \boldsymbol{\rm 010010} \hspace{0.05cm},\hspace{0.15cm} ... \hspace{0.15cm}$$
 
"\boldsymbol{\rm ,\_\hspace{-0.03cm}\_Wohnort\hspace{-0.1cm}:\hspace{-0.05cm}\_\hspace{-0.03cm}\_}" \hspace{0.05cm} \mapsto \hspace{0.05cm} \boldsymbol{\rm 010010} \hspace{0.05cm},\hspace{0.15cm} ... \hspace{0.15cm}$$
  
für die mit jeweils sechs Bit pro Zeichen binär–quellencodierte erste Zeile des obigen Textes:
+
for the first line of the above text, binary source coded with six bits per character:
 
      
 
      
 
:$$\boldsymbol{010000} \hspace{0.15cm}\boldsymbol{100000} \hspace{0.15cm}\boldsymbol{100001} \hspace{0.15cm}\boldsymbol{100100} \hspace{0.15cm}\boldsymbol{101011} \hspace{0.3cm}
 
:$$\boldsymbol{010000} \hspace{0.15cm}\boldsymbol{100000} \hspace{0.15cm}\boldsymbol{100001} \hspace{0.15cm}\boldsymbol{100100} \hspace{0.15cm}\boldsymbol{101011} \hspace{0.3cm}
Line 73: Line 73:
  
 
{{BlaueBox|TEXT=
 
{{BlaueBox|TEXT=
$\text{Fazit:}$&nbsp;  
+
$\text{Conclusion:}$&nbsp;  
Bei dieser spezifischen Anwendung lässt sich die erste Zeile mit $14 · 6 = 84$ Bit darstellen.  
+
In this specific application the first line can be displayed with&nbsp; $14 \cdot 6 = 84$&nbsp; bits. <br>
*Dagegen würde man bei herkömmlicher Binärcodierung $39 · 7 = 273$ Bit benötigen, weil:
+
*In contrast, conventional binary coding would require&nbsp; $39 \cdot 7 = 273$&nbsp; bits <br> &nbsp; &nbsp; &nbsp; (because of the lowercase letters in the text, six bits per character are not sufficient here).  
*Aaufgrund der Kleinbuchstaben im Text reichen hier sechs Bit pro Zeichen nicht aus.  
+
*For the entire text, this results in&nbsp; $103 \cdot 6 = 618$&nbsp; bits versus&nbsp; $196 \cdot 7 = 1372$&nbsp; bits.  
*Für den gesamten Text ergeben sich $103 · 6 = 618$ Bit gegenüber $196 · 7 = 1372$ Bit.  
+
*However, the code table must also be known to the recipient.}}
*Allerdings muss die Codetabelle auch dem Empfänger bekannt sein.}}
 
  
  
 
<br><br>
 
<br><br>
$\text{(2) Verfahren mit dynamischem Wörterbuch}$
+
$\text{(2) &nbsp; Procedures with dynamic dictionary}$
  
Alle relevanten Komprimierungsverfahren arbeiten allerdings nicht mit statischem Wörterbuch, sondern mit ''dynamischen Wörterbüchern'', die erst während der Codierung sukzessive entstehen:
+
Nevertheless, all relevant compression methods do not work with static dictionaries, but with&nbsp; &raquo;'''dynamic dictionaries'''&laquo;, which are created successively only during the coding:
*Solche Verfahren sind flexibel einsetzbar und müssen nicht an die Anwendung adaptiert werden. Man spricht von ''universellen Quellencodierverfahren''.
+
*Such procedures are flexible and do not have to be adapted to the application. &nbsp; One speaks of &raquo;'''universal source coding procedures'''&laquo;.
*Dann genügt ein einziger Durchlauf, während bei mit statischem Wörterbuch die Datei vor dem Codiervorgang erst analysiert werden muss.
+
*A single pass is sufficient, whereas with a static dictionary the file must first be analyzed before the encoding process.
*An der Sinke wird das dynamische Wörterbuch in gleicher Weise generiert wie bei der Quelle. Damit entfällt die Übertragung des Wörterbuchs.
+
*At the sink, the dynamic dictionary is generated in the same way as for the source.&nbsp; This eliminates the need to transfer the dictionary.
  
  
[[File:P_ID2926__Inf_T_2_2_S1b_neu.png|frame|Auszug aus dem Hexdump eines natürlichen Bildes im BMP–Format]]
+
[[File:P_ID2926__Inf_T_2_2_S1b_neu.png|frame|Extract from the hexdump of a natural image in BMP format]]
 
{{GraueBox|TEXT=
 
{{GraueBox|TEXT=
$\text{Beispiel 1:}$&nbsp;  
+
$\text{Example 1:}$&nbsp;  
Die Grafik zeigt einen kleinen Ausschnitt von 80 Byte einer [[Digitalsignalübertragung/Anwendungen_bei_Multimedia–Dateien#Bilder_im_BMP.E2.80.93Format_.281.29|BMP–Datei]] in Hexadezimaldarstellung. Es handelt sich um die unkomprimierte Darstellung eines natürlichen Bildes.
+
The graphic shows a small section of&nbsp; $80$&nbsp; byte of a&nbsp; [[Digital_Signal_Transmission/Applications_for_Multimedia_Files#Images_in_BMP_format|$\text{BMP file}$]]&nbsp; in hexadecimal format.&nbsp; It is the uncompressed representation of a natural picture.
  
*Man erkennt, dass in diesem kleinen Ausschnitt einer Landschaftsaufnahme die Bytes '''FF''', '''55''' und '''47''' sehr häufig auftreten.  
+
*You can see that in this small section of a landscape image the bytes&nbsp; $\rm FF$,&nbsp; $\rm 55$&nbsp; and&nbsp; $\rm 47$&nbsp; occur very frequently.&nbsp; Data compression is therefore promising.  
*Eine Datenkomprimierung ist deshalb erfolgversprechend.  
+
*But since other parts of the file or other byte combinations dominate in other image contents, the use of a static dictionary would not be appropriate here.}}
*Da aber an anderen Stellen der 4 MByte–Datei oder bei anderem Bildinhalt andere Bytekombinationen dominieren,  
 
*wäre hier die Verwendung eines statischen Wörterbuchs nicht zielführend.}}
 
  
  
 +
[[File:P_ID2927__Inf_T_2_2_S1c_GANZ_neu.png|right|frame|Possible encoding of a simple graphic]]
 
{{GraueBox|TEXT=
 
{{GraueBox|TEXT=
$\text{Beispiel 2:}$&nbsp;  
+
$\text{Example 2:}$&nbsp;  
Bei einer künstlich erzeugten Grafik – zum Beispiel einem Formular – könnte man dagegen durchaus mit  statischem Wörterbuch arbeiten. Wir betrachten hier ein S/W–Bild mit 27 × 27 Pixeln, wobei die Zuordnung „Schwarz”  &nbsp; ⇒ &nbsp; '''0''' und „Weiß”  &nbsp; ⇒ &nbsp;  '''1''' vereinbart wurde.
+
For an artificially created graphic, for example a form, you could work with a static dictionary.  
  
[[File:P_ID2927__Inf_T_2_2_S1c_GANZ_neu.png|frame|Mögliche Codierung einer einfachen Grafik]]
+
We are looking at a&nbsp; "black & white"&nbsp; image with&nbsp; $27 × 27$&nbsp; pixels, where the mapping "black" &nbsp; ⇒ &nbsp; '''0'''&nbsp; and "white" &nbsp; ⇒ &nbsp; '''1'''&nbsp; has been agreed upon.
  
*Im oberen Bereich (schwarze Markierung) wird jede Zeile durch 27 Nullen beschrieben.
+
*At the top&nbsp; (black marker),&nbsp; each line is described by&nbsp; $27$&nbsp; zeros.
*In der Mitte (blaue Markierung) wechseln sich jeweils drei Nullen und drei Einsen ab.
+
*In the middle&nbsp; (blue marker),&nbsp; three zeros and three ones always alternate.
*Unten (rote Markierung) werden pro Zeile 25 Einsen durch zwei Nullen begrenzt.}}
+
*At the bottom&nbsp; (red marker),&nbsp; 25 ones per line are limited by two zeros.}}
  
 
 
 
 
==LZ77 – die Grundform der Lempel–Ziv–Algorithmen  ==  
+
==LZ77 - the basic form of the Lempel-Ziv algorithms ==  
 
<br>
 
<br>
Die wichtigsten Verfahren zur Datenkomprimierung mit dynamischem Wörterbuch gehen auf [https://de.wikipedia.org/wiki/Abraham_Lempel Abraham Lempel] und [https://de.wikipedia.org/wiki/Jacob_Ziv Jacob Ziv] zurück. Die gesamte Lempel–Ziv–Familie (im Folgenden verwenden wir hierfür kurz: LZ–Verfahren) kann wie folgt charakterisiert werden:
+
The most important procedures for data compression with a dynamic dictionary go back to&nbsp; [https://en.wikipedia.org/wiki/Abraham_Lempel $\text{Abraham Lempel}$]&nbsp; and&nbsp; [https://en.wikipedia.org/wiki/Jacob_Ziv $\text{Jacob Ziv}$].&nbsp; The entire Lempel-Ziv family&nbsp; (in the following we will use for this briefly: &nbsp; $\rm LZ$&nbsp; procedure)&nbsp; can be characterized as follows:
*Lempel–Ziv–Verfahren nutzen die Tatsache, dass in einem Text oft ganze Wörter – oder zumindest Teile davon – mehrfach vorkommen. Man sammelt alle Wortfragmente, die man auch als ''Phrasen'' bezeichnet, in einem ausreichend großen Wörterbuch.
+
*Lempel-Ziv methods use the fact that often whole words, or at least parts of them, occur several times in a text.&nbsp; One collects all word fragments, which are also called&nbsp; "phrases"&nbsp; in a sufficiently large dictionary.
*Im Gegensatz zur vorher entwickelten Entropiecodierung (z.B. von Shannon und Huffman) ist hier nicht die Häufigkeit einzelner Zeichen oder Zeichenfolgen die Grundlage der Komprimierung, so dass die LZ–Verfahren auch ohne Kenntnis der Quellenstatistik angewendet werden können.
+
*Contrary to the entropy coding developed some years before (by Shannon and Huffman), the frequency of single characters or character strings is not the basis of the compression here, so that the LZ procedures can be applied even without knowledge of the source statistics.
*LZ–Komprimierungsverfahren kommen dementsprechend mit einem einzigen Durchgang aus und auch der Quellensymbolumfang $M$ und die Symbolmenge $\{q_μ\}$ mit $μ = 1$, ... , $M$ muss nicht bekannt sein. Man spricht von universeller Quellencodierung (englisch: ''Universal Source Coding'').
+
*LZ compression accordingly manages with a single pass and also the source symbol set size&nbsp; $M$&nbsp; and the symbol set&nbsp; $\{q_μ\}$&nbsp; with&nbsp; $μ = 1$, ... , $M$&nbsp; does not have to be known.&nbsp; This is called&nbsp; &raquo;'''Universal Source Coding'''&laquo;.
  
  
Wir betrachten zunächst den Lempel–Ziv–Algorithmus in seiner ursprünglichen Form aus dem Jahre 1977, bekannt unter der Bezeichnung [https://de.wikipedia.org/wiki/LZ77 LZ77]. Dieser arbeitet mit einem Fenster, das sukzessive über den Text verschoben wird; man spricht auch von einem ''Sliding Window''. Die Fenstergröße $G$ ist dabei ein wichtiger Parameter, der das Komprimierungsergebnis entscheidend beeinflusst.
+
We first look at the Lempel-Ziv algorithm in its original form from 1977, known as&nbsp; [https://en.wikipedia.org/wiki/LZ77_and_LZ78#LZ77 $\rm LZ77$]:
 +
[[File:EN_Inf_T_2_2_S2a_v3.png|right|frame|Sliding window with LZ77 compression]]
 +
 +
*This works with a window that is successively moved over the text &nbsp; &rArr; &nbsp; one also speaks of a&nbsp; "sliding window".  
 +
*The window size&nbsp; $G$&nbsp; is an important parameter that decisively influences the compression result.
  
[[File:P_ID2426__Inf_T_2_2_S2a_neu.png|frame|Sliding–Window bei LZ77–Komprimierung]]
 
  
Die Grafik zeigt eine beispielhafte Belegung des ''Sliding Windows''. Dieses ist unterteilt in
 
*den Vorschaupuffer (blaue Hinterlegung) und
 
*den Suchpuffer (rote Hinterlegung, mit Positionen $P = 0$, ... , $7$  ⇒  $G = 8$).
 
  
 +
The graphic shows an example of the&nbsp; sliding windows.&nbsp; This is divided into
 +
*the&nbsp; "preview buffer"&nbsp; $($blue background),&nbsp; and
 +
*the&nbsp; "search buffer"&nbsp; $($red background, with the positions&nbsp; <br>$P = 0$, ... , $7$ &nbsp; ⇒ &nbsp; window size&nbsp; $G = 8)$.
  
Der bearbeitete Text umfasst die vier Worte '''Miss''', '''Mission''', '''Mississippi''' und '''Mistral''', jeweils getrennt durch einen Bindestrich. Zum betrachteten Zeitpunkt steht im Vorschaupuffer '''Mississi'''.
 
*Gesucht wird nun im Suchpuffer die beste Übereinstimmung  ⇒  die Zeichenfolge mit der maximalen Übereinstimmungslänge $L$. Diese ergibt sich für die Position $P = 7$ und die Länge $L = 5$ zu '''Missi'''.
 
*Dieser Schritt wird durch das ''Triple'' (7, 5, '''s''') ausgedrückt  ⇒  allgemein ( $P$, $L$, $Z$ ), wobei $Z$ = '''s''' das Zeichen angibt, das nicht mehr mit der gefundenen Zeichenfolge im Suchpuffer übereinstimmt.
 
*Anschließend wird das Fenster um $L + 1 = 6$ Zeichen nach rechts verschoben. Im Vorschaupuffer steht nun '''sippi–Mi''', im Suchpuffer '''n–Missis''' und die Codierung ergibt das Triple (2, 2, '''p''').
 
  
Im folgenden Beispiel werden die LZ77–Codier&ndash; und Decodier–Algorithmen genauer beschrieben.
+
The text consists of the words&nbsp; '''Miss''',&nbsp; '''Mission''',&nbsp; '''Mississippi'''&nbsp; and&nbsp; '''Mistral''', each separated by a hyphen.&nbsp; At the time under consideration the preview buffer contains&nbsp; '''Mississi'''.
 +
*Search now in the search buffer for the best match &nbsp; ⇒ &nbsp; the string with the maximum match length&nbsp; $L$.&nbsp; This is the result for position&nbsp; $P = 7$&nbsp; and length&nbsp; $L = 5$&nbsp; to&nbsp; '''Missi'''.
 +
*This step is expressed by the triple&nbsp; $(7,&nbsp; 5,&nbsp; $ '''s'''$)$ &nbsp; ⇒ &nbsp; general&nbsp; $(P, \ L, \ Z)$, where&nbsp; $Z =$&nbsp;'''s'''&nbsp; specifies the character that no longer matches the string found in the search buffer.
 +
*At the end the window is moved by&nbsp; $L + 1 = 6$&nbsp; character is moved to the right.&nbsp; In the preview buffer there is now&nbsp; '''sippi-Mi''',&nbsp; and in the search buffer&nbsp; '''n-Missis'''.&nbsp; <br>The encoding gives the triple&nbsp; $(2, 2,$&nbsp; '''p'''$)$.
 +
 
 +
 
 +
In the following example the LZ77 coding algorithm  is described in more detail.&nbsp; The decoding runs in a similar way.
 
 
 
 
{{GraueBox|
+
{{GraueBox|TEXT=
TEXT='''Beispiel 3''':&nbsp;  
+
$\text{Example 3:}$&nbsp;  
Wir betrachten die LZ77–Codierung des Strings '''ABABCBCBAABCABe''' entsprechend der folgenden Grafik. Die Eingangsfolge hat die Länge $N = 15$. Weiter wird vorausgesetzt:
+
We consider the LZ77 encoding of the string&nbsp; '''ABABCBCBAABCABe'''&nbsp; according to the following graphic.&nbsp; The input sequence has the length $N = 15$.&nbsp;
*Zeichen $Z ∈ \{$ '''A''', '''B''', '''C''', '''e''' $\}$, '''e''' entspricht ''end–of–file'' (Ende des Eingabe–Strings),
+
 
*Größe von Vorschau– und Suchpuffer jeweils $G = 4$ Position $P ∈ {0, 1, 2, 3}$.
+
Further is assumed:
 +
*For the characters apply&nbsp; $Z ∈ \{$ '''A''',&nbsp; '''B''',&nbsp; '''C''',&nbsp; '''e''' $\}$,&nbsp; where&nbsp; '''e'''&nbsp; corresponds to the&nbsp; "end-of-file"&nbsp; (end of the input string)
 +
*The size of the preview and search buffer are&nbsp; $G = 4$ &nbsp; &nbsp; position&nbsp; $P ∈ \{0, 1, 2, 3\}$.
 +
 
 +
[[File:EN_Inf_T_2_2_S2b.png|frame|To illustrate the LZ77 encoding]]
 +
<br>
 +
<u>Display of the encoding process</u>:
  
[[File:P_ID2427__Inf_T_2_2_S2b_neu.png|frame|Zur Verdeutlichung der LZ77–Codierung]]
+
<u>Step 1 and 2</u>: &nbsp; The characters&nbsp; '''A'''&nbsp; and&nbsp; '''B'''&nbsp; are encoded by the triple&nbsp; $(0, 0,&nbsp; $ '''A'''$)$&nbsp; and&nbsp; $(0, 0,&nbsp; $ '''B'''$)$, because they are not yet stored in the search buffer. &nbsp; Then move the sliding window by 1.
  
Hierzu einige Anmerkungen (''Hinweis'': Der Decodiervorgang läuft in vergleichbarer Weise ab):
+
<u>Step 3</u>: &nbsp; '''AB'''&nbsp; is masked by the search buffer and at the same time the still unknown character&nbsp; '''C'''&nbsp; is appended.&nbsp; After that the sliding window is moved three positions to the right.
* <u>Schritt 1 und 2</u>: Es werden die Zeichen '''A''' und '''B''' durch die Triple (0, 0, '''A''') und (0, 0, '''B''') codiert, da diese im Suchpuffer noch nicht abgelegt sind. Dann Verschiebung des Sliding Window um 1.
 
* <u>Schritt 3</u>: '''AB''' wird über den Suchpuffer maskiert und gleichzeitig das noch unbekannte Zeichen '''C''' angehängt. Danach wird das Sliding Window um drei Positionen nach rechts verschoben.
 
* <u>Schritt 4</u>: Hier wird gezeigt, dass der Suchstring '''BCB''' auch im Vorschaupuffer enden darf. Jetzt kann das Fenster um vier Positionen verschoben werden.
 
* <u>Schritt 5</u>: Es wird im Suchpuffer lediglich '''A''' gefunden und '''B''' abgehängt. Bei größerem Suchpuffer könnten dagegen '''ABC''' gemeinsam maskiert werden. Dazu müsste $G ≥ 7$ sein.
 
* <u>Schritt 6</u>: Ebenso muss das Zeichen '''C''' aufgrund des zu kleinen Puffers separat codiert werden. Da aber '''CA''' vorher noch nicht aufgetreten ist, würde $G = 7$ die Komprimierung nicht verbessern.
 
* <u>Schritt 7</u>: Mit der Berücksichtigung des end–of–file (e) gemeinsam mit '''AB''' aus dem Suchpuffer ist der Codiervorgang abgeschlossen.
 
  
 +
<u>Step 4</u>: &nbsp; This shows that the search string&nbsp; '''BCB'''&nbsp; may also end in the preview buffer.&nbsp; Now the window can be moved four positions to the right.
  
Vor der Übertragung müssen natürlich die angegebenen Triple noch binär codiert werden. Dabei benötigt man im vorliegenden Beispiel für
+
<u>Step 5</u>: &nbsp; Only&nbsp; '''A'''&nbsp; is found in the search buffer and&nbsp; '''B'''&nbsp; is dropped.&nbsp; If the search buffer is larger, however,&nbsp; '''ABC'''&nbsp; could be masked together.&nbsp; For this purpose must be&nbsp; $G ≥  7$.
*die Position $P ∈ \{0, 1, 2, 3\}$ zwei Bit (gelbe Hinterlegung),
 
*die Kopierlänge $L$ drei Bit (grün hinterlegt), so dass man auch $L = 7$ noch darstellen könnte,
 
*alle Zeichen mit jeweils zwei Bit (weiß hinterlegt), z.B. '''A''' &#8594; '''00''', '''B''' &#8594; '''01''', '''C''' &#8594; '''10''', '''e''' („end–of–file”) &#8594; '''11'''.
 
  
 +
<u>Step 6</u>: &nbsp; Likewise, the character&nbsp; '''C'''&nbsp; must be coded separately due to the buffer being too small.&nbsp; But since&nbsp; '''CA'''&nbsp; hasn't occurred before,&nbsp; $G = 7$&nbsp; would not improve the compression here.
  
Damit hat die LZ77–Ausgangsfolge eine Länge von 7 · 7 = 49 Bit, während die Eingangsfolge nur 15 · 2 = 30 Bit benötigt. Daraus erkennt man:<br>
+
<u>Step 7</u>: &nbsp; With the consideration of the end-of-file&nbsp; ('''e''')&nbsp; together with&nbsp; '''AB'''&nbsp; from the search buffer, the encoding process is finished.
:'''Eine LZ–Komprimierung macht nur bei großen Dateien Sinn'''.}}
 
  
  
==Die Lempel–Ziv–Variante LZ78 ==  
+
Before transmission, the specified triples must of course be binary coded.&nbsp; In this example
 +
*the position&nbsp; $P ∈ \{0,\ 1,\ 2,\ 3\}$&nbsp; needs two bits&nbsp; (yellow background in the table above),
 +
*the copy length&nbsp; $L$&nbsp; needs three bits&nbsp; (green background), so that also&nbsp; $L = 7$&nbsp; could still be displayed,
 +
*all characters need two bits&nbsp; (white background),&nbsp; for example&nbsp; '''A''' &#8594; '''00''',&nbsp; '''B''' &#8594; '''01''',&nbsp; '''C''' &#8594; '''10''',&nbsp; '''e''' ("end-of-file") &#8594; '''11'''.
 +
 
 +
 
 +
Thus the&nbsp; $\rm LZ77$&nbsp; output sequence has a length of&nbsp; $7 \cdot 7 = 49$&nbsp; bits, while the input sequence only needed&nbsp; $15 \cdot 2 = 30$&nbsp; bits.}}
 +
 
 +
 
 +
{{BlaueBox|TEXT=
 +
$\text{Conclusion:}$&nbsp; '''Lempel-Ziv compression only makes sense with large files !'''}}
 +
 
 +
 
 +
==The Lempel-Ziv variant LZ78 ==  
 
<br>
 
<br>
Der LZ77–Algorithmus erzeugt dann eine sehr ineffiziente Ausgabe, wenn sich häufigere Zeichenfolgen erst mit größerem Abstand wiederholen. Aufgrund der begrenzten Puffergröße $G$ des ''Sliding Window'' können solche Wiederholungen oft nicht erkannt werden.
+
The LZ77 algorithm produces very inefficient output if more frequent strings are repeated only with a larger distance.&nbsp; Such repetitions can often not be recognized due to the limited buffer size&nbsp; $G$&nbsp; of the sliding window.
  
Lempel und Ziv haben dieses Manko bereits ein Jahr nach der Veröffentlichung der ersten Version LZ77 korrigiert. Der Algorithmus LZ78 verwendet zur Komprimierung anstelle des lokalen Wörterbuchs (Suchpuffer) ein globales Wörterbuch. Bei entsprechender Wörterbuchgröße lassen sich somit auch solche Phrasen, die schon längere Zeit vorher im Text aufgetaucht sind, effizient komprimieren.
+
Lempel and Ziv corrected this shortcoming already one year after the release of the first version LZ77:
 +
*The algorithm&nbsp; $\rm LZ78$&nbsp; uses a global dictionary for compression instead of the local dictionary (search buffer).  
 +
*The size of the dictionary allows efficient compression of phrases that have been used for a long time before.
  
  
{{GraueBox|
+
{{GraueBox|TEXT=
TEXT='''Beispiel 4''':&nbsp;  
+
$\text{Example 4:}$&nbsp;  
Zur Erklärung des LZ78–Algorithmus betrachten wir die gleiche Folge '''ABABCBCBAABCABe''' wie für das LZ77–Beispiel auf der letzten Seite.
+
To explain the LZ78  algorithm we consider the same sequence&nbsp; '''ABABCBCBAABCABe'''&nbsp; as for the LZ77 $\text{Example 3}$.
*Die Grafik zeigt (mit roter Hinterlegung) das Wörterbuch mit Index $I&nbsp;$ (in Dezimal– und Binärdarstellung, Spalte 1 und 2) und dem entsprechenden Inhalt (Spalte 3), der zum Codierschritt $i&nbsp;$ eingetragen wird (Spalte 4). Bei LZ78 gilt sowohl für die Codierung als auch für die Decodierung stets $i = I$.
 
*In Spalte 5 findet man die formalisierte Coderausgabe (Index $I$, neues Zeichen $Z$). In der Spalte 6 ist die dazugehörige Binärcodierung angegeben mit vier Bit für den Index und der gleichen Zeichenzuordnung '''A''' &#8594; '''00''', '''B''' &#8594; '''01''', '''C''' &#8594; '''10''', '''e''' („end–of–file”) &#8594; '''11''' wie im Beispiel 3.
 
  
:[[File:Inf_T_2_2_S3_version2.png|frame|Generierung des Wörterbuchs und Ausgabe bei LZ78]]
+
[[File:EN_Inf_T_2_2_S3.png|frame|Generation of the dictionary and output at LZ78]]
  
*Zu Beginn (Schritt $i = 0$) ist das Wörterbuch (WB) leer bis auf den Eintrag '''ε''' (leeres Zeichen, nicht zu verwechseln mit dem Leerzeichen, das aber hier nicht verwendet wird) mit Index $I = 0$.
+
The graphic shows&nbsp; (with red background)&nbsp; the dictionary
*Im Schritt $i = 1$ findet man im Wörterbuch noch keinen verwertbaren Eintrag, und es wird (0, '''A''') ausgegeben ('''A''' folgt auf '''ε'''). Im Wörterbuch erfolgt der Eintrag '''A''' in Zeile $I = 1$ (abgekürzt 1:'''A''').
+
*with index&nbsp; $I&nbsp;$&nbsp; (in decimal and binary representation, column 1 and 2)&nbsp;
*Damit vergleichbar ist die Vorgehensweise im zweiten Schritt ($i$ = 2). Ausgegeben wird hier (0, '''B''') und ins Wörterbuch wird 2:'''B''' eingetragen.
+
*and the corresponding content (column 3),
*Da bei Schritt 3 bereits der Eintrag 1:'''A''' gefunden wird, können hier die Zeichen '''AB''' gemeinsam durch (1, '''B''') codiert werden und es wird der neue Wörterbucheintrag 3:'''AB''' vorgenommen.
+
*which is entered for coding step&nbsp; $i&nbsp;$&nbsp; (column 4).&nbsp; For LZ78 both encoding and decoding are always&nbsp; $i = I$.
*Nach Codierung und Eintrag des neuen Zeichens '''C''' in Schritt 4 wird im Schritt 5 das Zeichenpaar '''BC''' gemeinsam codiert  (2, '''C''') und in das Wörterbuch 5:'''BC''' eingetragen.
+
*In column 5 you find the formalized code output&nbsp; $($Index&nbsp; $I$,&nbsp; new character&nbsp; $Z)$.
*In Schritt 6 werden mit '''BA''' ebenfalls zwei Zeichen gemeinsam behandelt und in den beiden letzten Schritten jeweils drei, nämlich 7:'''ABC''' und 8:'''ABe'''. Die Ausgabe (3, '''C''') steht für „WB(3) + '''C”''' = '''ABC''' und die Ausgabe (3, '''e''') für '''ABe'''.
+
*In column 6 the corresponding binary coding is given with four bits for the index and the same character assignment&nbsp; '''A''' &#8594; '''00''',&nbsp; '''B''' &#8594; '''01''',&nbsp; '''C''' &#8594; '''10''',&nbsp; '''e''' ("end-of-file") &#8594; '''11'''&nbsp; as in&nbsp; $\text{Example 3}$.
 +
<br clear=all>
 +
*At the beginning&nbsp; (step $\underline{i = 0}$)&nbsp; the dictionary is&nbsp; (WB)&nbsp; empty except for the entry&nbsp; '''ε'''&nbsp; $($empty character, not to be confused with the space character, which is not used here$)$&nbsp; with index&nbsp; $I = 0$.
 +
*In the step&nbsp; $\underline{i = 1}$&nbsp; there is no usable entry in the dictionary yet, and it becomes&nbsp; ('''0,&nbsp; A''')&nbsp; output&nbsp; ('''A'''&nbsp; follows&nbsp; '''ε'''). &nbsp; In the dictionary, the entry&nbsp; '''A'''&nbsp; follows in line&nbsp; $I = 1$&nbsp; (abbreviated&nbsp; '''1: A''').
 +
*The procedure in the second step&nbsp; ($\underline{i = 2}$).&nbsp; The output is&nbsp; ('''0,&nbsp; B''')&nbsp; and the dictionary entry is&nbsp; '''2: B'''&nbsp;.
 +
*As the entry&nbsp; $\underline{i = 3}$&nbsp; is already found in step&nbsp; '''1: A'''&nbsp;, the characters&nbsp; '''AB'''&nbsp; can be coded together by&nbsp; ('''1, B''')&nbsp; and the new dictionary entry&nbsp; '''3: AB'''&nbsp; is made.
 +
*After coding and insertion of the new character&nbsp; '''C'''&nbsp; in step&nbsp; $\underline{i = 4}$&nbsp; the pair of characters&nbsp; '''BC'''&nbsp; is coded together &nbsp; &nbsp; ('''2, C''') and entered into the dictionary&nbsp; '''5: BC'''&nbsp; in step&nbsp; $\underline{i = 5}$&nbsp;.
 +
*In step&nbsp; $\underline{i = 6}$&nbsp; two characters are treated together with&nbsp; ''''6: BA'''&nbsp; and in the last two steps three each, namely&nbsp; '''7: ABC'''&nbsp; and&nbsp; '''8: ABe'''.  
 +
*The output&nbsp; (3, '''C''')&nbsp; in step&nbsp; $\underline{i = 7}$&nbsp; stands for&nbsp; "WB(3) + '''C''' = '''ABC''' &nbsp; and the output&nbsp; (3, '''e''')&nbsp; in step&nbsp; $\underline{i = 8}$&nbsp; for&nbsp; '''ABe'''.
  
  
Im Beispiel besteht somit die LZ78–Codesymbolfolge aus 8 · 6 = 48 Bit. Das Ergebnis ist vergleichbar mit LZ77 (49 Bit). Auf Details und Verbesserungen von LZ78 wird hier verzichtet. Hier verweisen wir auf den [[Informationstheorie/Komprimierung_nach_Lempel,_Ziv_und_Welch#Der_Lempel.E2.80.93Ziv.E2.80.93Welch.E2.80.93Algorithmus|LZW–Algorithmus]], der auf den nächsten Seiten beschrieben wird. Soviel nur vorneweg:
+
In this&nbsp; $\text{Example 4}$&nbsp; the LZ78 encoded sequence thus consists of&nbsp; $8 - 6 = 48$&nbsp; bit.&nbsp; The result is comparable to the LZ77-$\text{Example 3}$&nbsp; $(49$ bit$)$.}}
*Der Index $I$ wird hier einheitlich mit vier Bit dargestellt, wodurch das Wörterbuch auf 16 Einträge beschränkt ist. Durch eine [[Informationstheorie/Komprimierung_nach_Lempel,_Ziv_und_Welch|variable Bitanzahl]] für den Index kann man diese Einschränkung umgehen. Gleichzeitig erhält man so einen besseren Komprimierungsfaktor.
 
*Das Wörterbuch muss bei allen LZ–Varianten nicht übertragen werden, sondern wird beim Decoder in genau gleicher Weise erzeugt wie auf der Coderseite. Die Decodierung erfolgt bei LZ78 – nicht aber bei LZW – ebenfalls in analoger Weise wie die Codierung.
 
*Alle LZ–Verfahren sind asymptotisch optimal, das heißt, dass bei unendlich langen Folgen die mittlere Codewortlänge $L_{\rm M}$ pro Quellensymbol gleich der Quellenentropie $H$ist.
 
*Bei kurzen Folgen ist die Abweichung allerdings beträchtlich. Mehr dazu am [[Informationstheorie/Komprimierung_nach_Lempel,_Ziv_und_Welch#Quantitative_Aussagen_zur_asymptotischen_Optimalit.C3.A4t|Kapitelende]].}}
 
  
  
==Der Lempel–Ziv–Welch–Algorithmus  ==  
+
{{BlaueBox|TEXT=
 +
$\text{Conclusion:}$&nbsp; Details and improvements of LZ78 will be omitted here.&nbsp; Here we refer to the&nbsp; [[Information_Theory/Compression_According_to_Lempel,_Ziv_and_Welch#The_Lempel-Ziv-Welch_algorithm|$\text{LZW algorithm}$]], which will be described in the following sections.&nbsp; Only this much will be said now:
 +
*The index&nbsp; $I$&nbsp; is uniformly represented here with four bits, whereby the dictionary is limited to&nbsp; $16$&nbsp; entries.&nbsp; By a&nbsp; "variable number of bits"&nbsp; for the index one can bypass this limitation.&nbsp; At the same time one gets a better compression factor.
 +
*The dictionary does not have to be transmitted with all LZ variants, but is generated with the decoder in exactly the same way as on the encoder side.&nbsp; The decoding is also done with LZ78, but not with LZW, in the same way as the encoding.
 +
*All LZ procedures are asymptotically optimal, i.e., for infinitely long sequences the mean code word length&nbsp; $L_{\rm M}$&nbsp; per source symbol is equal to the source entropy&nbsp; $H$.
 +
*For short sequences, however, the deviation is considerable.&nbsp; More about this at the&nbsp; [[Information_Theory/Compression According to Lempel, Ziv and Welch#Quantitative statements on asymptotic optimality|"end of chapter"]].}}
 +
 
 +
 
 +
==The Lempel-Ziv-Welch algorithm ==  
 
<br>
 
<br>
Die heute gebräuchlichste Variante der Lempel–Ziv–Komprimierung wurde von [https://de.wikipedia.org/wiki/Terry_Welch Terry Welch] entworfen und 1983 veröffentlicht. Wir nennen diese den ''Lempel–Ziv–Welch–Algorithmus'', abgekürzt mit LZW. Ebenso wie LZ78 leichte Vorteile gegenüber LZ77 aufweist (wie zu erwarten – warum sonst hätte der Algorithmus modifiziert werden sollen?), hat LZW gegenüber LZ78 auch mehr Vorteile als Nachteile.
+
The most common variant of Lempel-Ziv compression used today was designed by&nbsp; [https://en.wikipedia.org/wiki/Terry_Welch $\text{Terry Welch}$]&nbsp; and published in 1983.&nbsp; In the following we refer to it as the&nbsp; "Lempel-Ziv-Welch-Algorithm", abbreviated as&nbsp; $\rm LZW$. &nbsp; Just as LZ78 has slight advantages over LZ77&nbsp; (as expected, why else would the algorithm have been modified?),&nbsp; LZW also has more advantages than disadvantages compared to LZ78.
 +
 
 +
[[File:EN_Inf_T_2_2_S4_v2.png|right|frame|LZW encoding of the sequence&nbsp; '''ABABCBCBAABCABe''']]
  
[[File:P_ID2430__Inf_T_2_2_S4_neu.png|frame|LZW–Codierung der Folge '''ABABCBCBAABCABe''']]
+
The graphic shows the coder output for the exemplary input sequence&nbsp; '''ABABCBCBAABCABe'''.&nbsp; On the right you can see the dictionary&nbsp; (highlighted in red), which is successively generated during LZW encoding.&nbsp; The differences to LZ78 can be seen in comparison to the graphic in the last section, namely:
 +
*For LZW, all characters occurring in the text are already entered at the beginning&nbsp; $(i = 0)$&nbsp; and assigned to a binary sequence, in the example with the indices&nbsp; $I = 0$, ... ,&nbsp; $I = 3$.&nbsp;
 +
*This also means that LZW requires some knowledge of the message source, whereas LZ78 is a&nbsp; "true universal encoding".
 +
*For LZW, only the dictionary index&nbsp; $I$&nbsp; is transmitted for each encoding step&nbsp; $i$&nbsp; while for LZ78 the output is the combination&nbsp; $(I$,&nbsp; $Z)$,&nbsp;where $Z$&nbsp; denotes the current new character. &nbsp;
 +
*Due to the absence of&nbsp; $Z$&nbsp; in the encoder output, LZW decoding is more complicated than with LZ78, as described in section&nbsp; [[Information_Theory/Compression_According_to_Lempel,_Ziv_and_Welch#Decoding_of_the_LZW_algorithm|"Decoding of the LZW algorithm"]].
 +
<br clear=all>
  
Die Grafik zeigt die Coderausgabe für die beispielhafte Eingangsfolge '''ABABCBCBAABCABe'''. Rechts dargestellt ist das Wörterbuch (rot hinterlegt), das bei der LZW–Codierung sukzessive entsteht. Die Unterschiede gegenüber LZ78 erkennt man im Vergleich zur Grafik auf der letzten Seite, nämlich:
+
{{GraueBox|TEXT=
*Bei LZW sind im Wörterbuch schon zu Beginn ($i = 0$) alle vorkommenden Zeichen eingetragen und einer Binärfolge zugeordnet, im Beispiel mit den Indizes $I = 0$, ... , $I = 3$.
+
$\text{Example 5:}$&nbsp; For this exemplary&nbsp; $\rm LZW$&nbsp; encoding, again the input sequence&nbsp; '''ABABCBCBAABCABe'''&nbsp; is assumed  as with "LZ77" and "LZ78".&nbsp; So the following description refers to the above graphic.
*Das bedeutet aber auch, dass bei LZW doch gewisse Kenntnisse über die Nachrichtenquelle vorhanden sein müssen, während LZ78 eine „echte universelle Codierung” darstellt.
 
*Bei LZW wird zu jedem Codierschritt $i$ nur ein Wörterbuchindex $I$ übertragen, während bei LZ78 die Kombination ($I$, $Z$) ausgegeben wird; $Z$ gibt dabei das aktuell neue Zeichen an.
 
*Aufgrund des Fehlens von $Z$ in der Coderausgabe ist die LZW–Decodierung komplizierter als bei LZ78, wie auf der Seite [[Informationstheorie/Komprimierung_nach_Lempel,_Ziv_und_Welch#Decodierung_des_LZW.E2.80.93Algorithmus|Decodierung des LZW&ndash;Algorithmus]] beschrieben.  
 
 
  
{{GraueBox|
+
<u>Step i = 0</u>&nbsp; (default): &nbsp; The allowed characters&nbsp; '''A''',&nbsp; '''B''',&nbsp; '''C'''&nbsp; and&nbsp; '''e'''&nbsp; ("end-of-file") are entered into the dictionary and assigned to the indices&nbsp; $I = 0$, ... , $I = 3$&nbsp;.
TEXT='''Beispiel 5''':&nbsp; Für diese beispielhafte LZW–Codierung wird wie bei &bdquo;LZ77&rdquo; und &bdquo;LZ78&rdquo; wieder die Eingangsfolge '''ABABCBCBAABCABe''' vorausgesetzt.
 
  
* <u>Schritt i = 0</u> (Vorbelegung): Die erlaubten Zeichen '''A''', '''B''', '''C''' und '''e''' („end–of–file”) werden in das Wörterbuch eingetragen und den Indizes $I = 0$, ... , $I = 3$ zugeordnet.
+
<u>Step i = 1</u>: &nbsp; The character&nbsp; '''A'''&nbsp; is encoded by the decimal index&nbsp; $I = 0$&nbsp; and its binary representation&nbsp; '''0000'''&nbsp; is transmitted. &nbsp; <br>Then the combination of the current character&nbsp; '''A'''&nbsp; and the following character&nbsp; '''B'''&nbsp; of the input sequence is stored in the dictionary under the index&nbsp; $I = 4$&nbsp;.
* <u>Schritt i = 1</u>: '''A''' wird durch den Dezimalindex $I = 0$ codiert und dessen Binärdarstellung '''0000''' übertragen. Anschließend wird ins Wörterbuch die Kombination aus dem aktuellen Zeichen '''A''' und dem nachfolgenden Zeichen '''B''' der Eingangsfolge unter dem Index $I = 4$ abgelegt.
 
* <u>Schritt i = 2</u>: Darstellung von '''B''' durch Index $I = 1$ bzw. '''0001''' (binär) sowie Wörterbucheintrag von '''BA''' mit Index $I = 5$.
 
* <u>Schritt i = 3</u>: Aufgrund des Wörterbucheintrags '''AB''' zum Zeitpunkt $i = 1$ ergibt sich der zu übertragende Index $I = 4$ (binär: '''0100'''). Danach wird ins Wörterbuch '''ABC''' neu eingetragen.
 
* <u>Schritt i = 8</u>: Hier werden die Zeichen '''ABC''' gemeinsam durch den Index $I = 6$ (binär: '''0110''') dargestellt und der Eintrag für '''ABCA''' vorgenommen.
 
  
 +
<u>step i = 2</u>: &nbsp; Representation of&nbsp; '''B'''&nbsp; by index&nbsp; $I = 1$&nbsp; or&nbsp; '''0001'''&nbsp; (binary)&nbsp; as well as dictionary entry of&nbsp; '''BA'''&nbsp; placed under index&nbsp; $I = 5$.
  
Mit der Codierung von '''e''' (EOF–Marke) ist der Codiervorgang nach zehn Schritten beendet. Bei LZ78 wurden nur acht Schritte benötigt. Es ist aber zu berücksichtigen:
+
<u>Step i = 3</u>: &nbsp; Because of the entry&nbsp; '''AB'''&nbsp; at time&nbsp; $i = 1$&nbsp; the index to be transmitted is&nbsp; $I = 4$&nbsp; (binary:&nbsp; '''0100''').&nbsp; New dictionary entry of&nbsp; '''ABC'''&nbsp; under&nbsp; $I = 6$.
*Der LZW–Algorithmus benötigt für die Darstellung dieser 15 Eingangssymbole nur 10 · 4 = 40 Bit gegenüber den 8 · 6 = 48 Bit bei LZ78. Vorausgesetzt ist für diese einfache Rechnung jeweils vier Bit zur Indexdarstellung.
 
*Sowohl bei LZW als auch bei LZ78 kommt man mit weniger Bit aus (nämlich mit 34 bzw. 42), wenn man berücksichtigt, dass zum Schritt $i = 1$ der Index nur mit zwei Bit codiert werden muss ($I ≤ 3$) und für $i = 2$ bis $i = 5$ drei Bit ausreichen ($I ≤ 7$).}}
 
  
 +
<u>Step i = 8</u>: &nbsp; Here the characters&nbsp; '''ABC'''&nbsp; are represented together by the index&nbsp; $I = 6$&nbsp; (binary:&nbsp; '''0110''')&nbsp; and the entry for&nbsp; '''ABCA'''&nbsp; is made.
  
Auf den beiden folgenden Seiten wird auf die variable Bitanzahl zur Indexdarstellung sowie auf die Decodierung von LZ78– und LZW–codierten Binärfolgen noch im Detail eingegangen.
+
With the encoding of&nbsp; '''e'''&nbsp; (EOF mark) the encoding process is finished after ten steps.&nbsp; With LZ78 only eight steps were needed.&nbsp; But it has to be considered:
 +
*The LZW algorithm needs only&nbsp; $10 \cdot 4 = 40$&nbsp; bit versus the&nbsp; $8 \cdot 6 = 48$&nbsp; bit for LZ78.&nbsp; Provided that for this simple calculation, four bits each are needed for index representation.
 +
*LZW as well as LZ78 require less bits&nbsp; $($namely &nbsp; $34$&nbsp; or &nbsp; $42)$, if one considers that for the step&nbsp; $i = 1$&nbsp; the index has to be encoded with two bits only&nbsp; $(I ≤ 3)$&nbsp; and for&nbsp; $2 ≤ i ≤ 5$&nbsp; three bits are sufficient&nbsp; $(I ≤ 7)$.}}
  
  
==Lempel–Ziv–Codierung mit variabler Indexbitlänge ==  
+
==Lempel-Ziv coding with variable index bit length ==  
 
<br>
 
<br>
Aus Gründen einer möglichst kompakten Darstellung betrachten wir nun nur noch Binärquellen mit dem Wertevorrat $\{$ '''A''', '''B''' $\}$. Auch das Abschlusszeichen '''end–of–file''' bleibt unberücksichtigt.
+
For reasons of a most compact representation, we now consider only binary sources with symbol set&nbsp; $\{$'''A''', '''B'''$\}$.&nbsp; The terminating character&nbsp; &raquo;'''end-of-file'''&laquo;&nbsp; is not considered, too.  
  
[[File:P_ID2432__Inf_T_2_2_S5_neu.png|frame|LZW–Codierung einer binären Eingangsfolge]]
+
[[File:EN_Inf_T_2_2_S5_vers2.png|right|frame|LZW encoding of a binary input sequence]]
  
Wir betrachten die LZW–Codierung anhand eines Bildschirmabzugs unseres interaktiven Flash–Moduls [[Lempel–Ziv–Algorithmen]]. Die Aussagen gelten aber in gleicher Weise für [[Informationstheorie/Komprimierung_nach_Lempel,_Ziv_und_Welch#Die_Lempel.E2.80.93Ziv.E2.80.93Variante_LZ78|LZ78]].
+
We demonstrate the LZW encoding by means of a screenshot of our interactive SWF module&nbsp; [[Applets:Lempel-Ziv-Welch|"Lempel-Ziv-Welch algorithms"]].  
*Beim ersten Codierschritt ($i = 1$) wird A mit 0 codiert. Danach erfolgt im Wörterbuch der Eintrag mit dem Index $I = 2$ und dem Inhalt '''AB'''.
 
*Da es bei Schritt $i = 1$  im Wörterbuch mit '''A''' und '''B''' nur zwei Einträge gibt, genügt ein Bit. Dagegen werden bei Schritt $i = 2$ und $i = 3$ für '''B'''  ⇒  '''01''' bzw. '''A'''  ⇒  '''00''' jeweils zwei Bit benötigt.
 
*Ab $i = 4$ erfolgt die Indexdarstellung mit drei Bit, ab $i = 8$ mit vier Bit und ab $i = 16$ mit fünf Bit. Hieraus lässt sich ein einfacher Algorithmus für die jeweilige Index–Bitanzahl $L(i)$ ableiten.
 
*Betrachten wir abschließend den Codierschritt $i = 18$. Hier wird die rot markierte Sequenz '''ABABB''', die zum Zeitpunkt $i = 11$ in das Wörterbuch eingetragen wurde (Index $I = 13$ ⇒ '''1101''') bearbeitet. Die Ausgabe lautet wegen $i ≥ 16$ aber nun '''01101''' (grüne Markierung).
 
  
Die Verbesserung durch variable Indexbitlänge ist auch bei LZ78 in gleicher Weise möglich.
+
*In the first encoding  step&nbsp; $(i = 1)$:&nbsp;  '''A'''&nbsp; &nbsp;⇒&nbsp; '''0'''.&nbsp; Afterwards the entry with index&nbsp; $I = 2$&nbsp; and&nbsp; content&nbsp; '''AB'''.
 +
*As there are only two dictionary entries in step&nbsp; $i = 1$&nbsp;  (for&nbsp; '''A'''&nbsp; and&nbsp; '''B'''&nbsp;) one bit is sufficient. &nbsp; For step&nbsp; $i = 2$&nbsp; and&nbsp; $i = 3$&nbsp; $($'''B''' &nbsp;⇒&nbsp; '''01'''&nbsp; and &nbsp; '''A''' &nbsp;⇒&nbsp; '''00'''$)$&nbsp; two bits are needed in each case.
 +
*Starting on &nbsp;$i = 4$&nbsp;, the index representation is done with three bits, then from &nbsp; $i = 8$&nbsp; with four bits and from&nbsp; $i = 16$&nbsp; with five bits.&nbsp; A simple algorithm for the respective index bit number&nbsp; $L(i)$&nbsp; can be derived.
 +
*Let's finally consider the encoding step&nbsp; $i = 18$.&nbsp; Here, the sequence&nbsp; '''ABABB'''&nbsp; marked in red, which was entered into the dictionary at time&nbsp; $i = 12$&nbsp; at time&nbsp; $($Index&nbsp; $I = 13$ ⇒ '''1101'''$)$&nbsp; is edited. &nbsp; However, the encoder output is now&nbsp; '''01101'''&nbsp;because of&nbsp; $i ≥ 16$&nbsp;  (green mark at the encoder output).
 +
 
 +
 
 +
The statements also apply to&nbsp; [[Information_Theory/Compression_According_to_Lempel,_Ziv_and_Welch#The_Lempel-Ziv_variant_LZ78|$\text{LZ78}$]].&nbsp; That is: &nbsp; '''With LZ78 a variable index bit length results in the same improvement as with  LZW'''.
  
 
 
 
 
==Decodierung des LZW–Algorithmus ==
+
==Decoding of the LZW algorithm ==
 +
<br>
 +
The decoder now displays the decoded output in the&nbsp; [[Information_Theory/Compression_According_to_Lempel,_Ziv_and_Welch#Lempel-Ziv_coding_with_variable_index_bit_length|"last section"]]&nbsp; as input sequence.&nbsp; The graphic shows that it is possible to uniquely decode this sequence even with variable index bit lengths. Please note:
  
Am Decoder liegt nun die auf der [[Informationstheorie/Komprimierung_nach_Lempel,_Ziv_und_Welch#Lempel.E2.80.93Ziv.E2.80.93Codierung_mit_variabler_Indexbitl.C3.A4nge|letzten Seite]] ermittelte Coder–Ausgabe als Eingangsfolge an. Die Grafik zeigt, dass es auch bei variabler Indexbitlänge möglich ist, diese Folge eindeutig zu decodieren.
+
#&nbsp;The decoder knows that in the first encoding step&nbsp; $(i = 1)$&nbsp; the index&nbsp; $I&nbsp;$ was encoded with only one bit, in the steps&nbsp; $i = 2$&nbsp; and&nbsp; $i = 3$&nbsp; with two bit, from &nbsp; $i = 4$&nbsp; with three bit, from&nbsp; $i = 8$&nbsp; with four bit, and so on.
 +
#&nbsp;The decoder generates the same dictionary as the encoder, but the dictionary entries are made one time step later.  
  
[[File:P_ID2433__Inf_T_2_2_S6_neu.png|frame|LZW–Decodierung einer binären Eingangsfolge]]
+
[[File:EN_Inf_T_2_2_S6.png|right|frame|LZW decoding of a binary input sequence]]
 +
<br>
 +
*At step&nbsp; $\underline{i = 1}$&nbsp; the adjacent symbol&nbsp; '''0'''&nbsp; is decoded as&nbsp; '''A'''. &nbsp; Likewise, the following results for step&nbsp; $\underline{i = 2}$&nbsp; from the preassignment of the dictionary and the two-bit representation agreed upon for this: &nbsp; '''01''' &nbsp; ⇒ &nbsp; '''B'''.
 +
*The entry of the line&nbsp; $\underline{I = 2}$&nbsp; $($content: &nbsp; '''AB'''$)$&nbsp; of the dictionary is therefore only made at the step&nbsp; $\underline{i = 2}$, while at the&nbsp; [[Information_Theory/Compression_According_to_Lempel,_Ziv_and_Welch#Lempel-Ziv_coding_with_variable_index_bit_length|$\text{encoding process}$]]&nbsp; this could already be done at the end of step&nbsp; $i = 1$.
 +
*Let us now consider the decoding for&nbsp; $\underline{i = 4}$. &nbsp; The index&nbsp; $\underline{I = 2}$&nbsp; returns the decoding result&nbsp; '''010''' &nbsp; ⇒ &nbsp; '''AB'''&nbsp; and in the next step&nbsp; $(\underline{i = 5})$&nbsp; the dictionary line&nbsp; $\underline{I = 5}$&nbsp; will be filled with&nbsp; '''ABA'''.&nbsp;
 +
*This time difference with respect to the dictionary entries can lead to decoding problems.&nbsp; For example, for step&nbsp; $\underline{i = 7}$&nbsp; there is no dictionary entry with index&nbsp; $\underline{I= 7}$.
 +
*What is to do in such a case as&nbsp; $(\underline{I = i})$?&nbsp; In this case you take the result of the previous decoding step&nbsp; $($here: &nbsp; '''BA'''&nbsp; for&nbsp; $\underline{i = 6})$&nbsp; and append the first character of this sequence at the end again. &nbsp; This gives the decoding result for&nbsp; $\underline{i = 7}$&nbsp; to&nbsp; '''111''' &nbsp; ⇒ &nbsp; '''BAB'''.
 +
*Naturally, it is unsatisfactory to specify only one recipe.&nbsp; In the&nbsp; [[Aufgaben:Aufgabe_2.4Z:_Nochmals_LZW-Codierung_und_-Decodierung|"Exercise 2.4Z"]]&nbsp; you should justify the procedure demonstrated here.&nbsp; We refer to the sample solution for this exercise.
  
Beim Decoder wird das gleiche Wörterbuch generiert wie beim Coder, doch erfolgen hier die Wörterbucheinträge einen Zeitschritt später. Weiter gilt:
 
*Dem Decoder ist bekannt, dass im ersten Codierschritt ($i = 1$) der Index $I&nbsp;$ mit nur einem Bit codiert wurde, in den Schritten $i = 2$ und $i = $ mit zwei Bit, ab $i = 4$ mit drei Bit, ab $i = 8$ mit vier Bit, usw.
 
*Zum Schritt $i = 1$ wird also '''0''' als '''A''' decodiert. Ebenso ergibt sich zum Schritt $i = 2$ aus der Vorbelegung des Wörterbuches und der vereinbarten Zwei–Bit–Darstellung: '''1''' ⇒ '''01''' ⇒ '''B'''.
 
*Der Eintrag der Zeile $I = 2$ (Inhalt: '''AB''') des Wörterbuchs erfolgt also erst zum Schritt $i = 2$, während beim [[Informationstheorie/Komprimierung_nach_Lempel,_Ziv_und_Welch#Lempel.E2.80.93Ziv.E2.80.93Codierung_mit_variabler_Indexbitl.C3.A4nge|Codiervorgang]] dies bereits am Ende von Schritt $i = 1$ geschehen konnte.
 
*Betrachten wir weiter die Decodierung für $i = 4$. Der Index $I =2$ liefert das Decodierergebnis '''AB''' und im nächsten Schritt ($i = 5$) wird die Wörterbuchzeile $I =5$ mit '''ABA''' belegt.
 
*Diese Zeitverschiebung hinsichtlich der WB–Einträge kann zu Decodierproblemen führen. Zum Beispiel gibt es zum Schritt $i = 7$ noch keinen Wörterbuch–Eintrag mit Index $I = 7$.
 
*Was ist in einem solchen Fall ($I = i$) zu tun? Man nimmt in diesem Fall das Ergebnis des vorherigen Decodierschrittes (hier: '''BA''' für $i = 6$) und fügt das erste Zeichen dieser Sequenz am Ende noch einmal an. Man erhält so das Decodierergebnis für $i = 7$ zu '''BAB'''.
 
*Natürlich ist es unbefriedigend, nur ein Rezept anzugeben. In der [[Aufgaben:2.04Z_LZW-Codierung-/-Decodierung|Aufgabe 2.4Z]] sollen Sie das Vorgehen selbst begründen. Wir verweisen hier auf die Musterlösung zu dieser Aufgabe.
 
  
 
+
With LZ78 decoding, the problem described here does not occur because not only the index&nbsp; $I&nbsp;$&nbsp; but also the current character&nbsp; $Z$&nbsp; is included in the encoding result and is transmitted.
Bei der LZ78–Decodierung tritt das hier geschilderte Problem nicht auf, da nicht nur der Index $I&nbsp;$, sondern auch das aktuelle Zeichen $Z$ im Codierergebnis enthalten ist und übertragen wird.
 
 
 
 
 
 
   
 
   
==Restredrundanz als Maß für die Effizienz von Codierverfahren  ==
+
==Remaining redundancy as a measure for the efficiency of coding methods==
 
<br>
 
<br>
Für den Rest dieses Kapitels gehen wir von folgenden Voraussetzungen aus:
+
For the rest of this chapter we assume the following prerequisites:
*Der ''Symbolumfang'' der Quelle (oder im übertragungstechnischen Sinne die Stufenzahl) sei $M$, wobei $M$ eine Zweierpotenz darstellt    $M = 2, 4, 8, 16$, ....
+
*The source symbol set size&nbsp; $($or in the transmission sense: &nbsp; the number of levels )&nbsp; is&nbsp; $M$, where&nbsp; $M$&nbsp; represents a power of two &nbsp; &nbsp; $M = 2, \ 4, \ 8, \ 16$, ....
*Die Quellenentropie sei $H$. Gibt es keine statistischen Bindungen zwischen den Symbolen, so gilt $H = H_0$, wobei $H_0 = \log_2 \ M$ den Entscheidungsgehalt angibt. Andernfalls gilt $H < H_0$.
+
*The source entropy is&nbsp; $H$.&nbsp; If there are no correlations between the symbols and if they are equally probable, then&nbsp; $H = H_0 = \log_2 \ M$.&nbsp; Otherwise, $H < H_0$ applies.
*Eine Symbolfolge der Länge $N$ wird quellencodiert und liefert eine binäre Codefolge der Länge $L$. Über die Art der Quellencodierung treffen wir vorerst keine Aussage.
+
*A symbol sequence of length&nbsp; $N$&nbsp; is source-coded and returns a binary encoded sequence of length&nbsp; $L$.&nbsp;
 +
*For the time being we do not make any statement about the type of source coding.
  
Nach dem [[Informationstheorie/Allgemeine_Beschreibung#Quellencodierungstheorem|Quellencodierungstheorem]] muss die mittlere Codewortlänge $L_{\rm M}$ größer oder gleich der Quellenentropie $H$ (in bit/Quellensymbol) sein. Das bedeutet
+
 
*für die Gesamtlänge der quellencodierten Binärfolge:
+
According to the&nbsp; [[Information_Theory/General_Description#Source_coding_theorem|$\text{Source Coding Theorem}$]]&nbsp; the average code word length&nbsp; $L_{\rm M}$&nbsp; must be greater than or equal to the source entropy&nbsp; $H$&nbsp; (in bit/source symbol).&nbsp; This means
 +
*for the total length of the source-coded binary sequence:
 
:$$L \ge N \cdot H \hspace{0.05cm},$$  
 
:$$L \ge N \cdot H \hspace{0.05cm},$$  
*für die relative Redundanz der Codefolge, im Folgenden kurz '''Restredundanz''' genannt wird:
+
*for the relative redundancy of the coded sequence, in the following called&nbsp; &raquo;'''residual redundancy'''&laquo;:
 
:$$r = \frac{L - N \cdot H}{L} \hspace{0.05cm}.$$
 
:$$r = \frac{L - N \cdot H}{L} \hspace{0.05cm}.$$
  
{{GraueBox|
+
{{GraueBox|TEXT=
TEXT='''Beispiel 6''':&nbsp;  
+
$\text{Example 6:}$&nbsp; If there were a&nbsp; "perfect source coding"&nbsp; for a redundancy-free binary source sequence&nbsp; $(M = 2,\ p_{\rm A} = p_{\rm B} = 0.5$,&nbsp; without memory$)$&nbsp; of length&nbsp; $N = 10000$, the encoder sequence would have also the length&nbsp; $L = 10000$.  
Gäbe es für eine redundanzfreie binäre Quellensymbolfolge ($M = 2, p_{\rm A} = p_{\rm B} = 0.5$, ohne statistische Bindungen ) der Länge $N = 10000$ eine ''perfekte Quellencodierung'', so hätte auch die Codefolge die Länge $L = 10000$. Ist bei einem Code das Ergebnis $L = N$ nie möglich, so bezeichnet man den Code als nichtperfekt.
 
*Für diese Nachrichtenquelle ist Lempel–Ziv nicht geeignet. Es wird stets $L > N$ gelten. Man kann es auch ganz lapidar ausdrücken: Die perfekte Quellencodierung ist hier gar keine Codierung.
 
*Eine redundante Binärquelle mit $p_{\rm A} = 0.89$, $p_{\rm B} = 0.11$ &nbsp; &nbsp; $H = 0.5$ könnte man mit einer perfekten Quellencodierung durch $L = 5000$ Bit darstellen, ohne dass wir hier sagen können, wie diese perfekte Quellencodierung aussieht.
 
*Bei einer Quaternärquelle ist $H > 1 \ \rm (bit/Quellensymbol)$ möglich, so dass auch bei perfekter Codierung stets $L > N$ sein wird. Ist die Quelle redundanzfrei (keine Bindungen, alle $M$ Symbole gleichwahrscheinlich), so hat sie die Entropie $H= 2 \ \rm (bit/Quellensymbol)$.
 
  
 +
<u>Consequence:</u> &nbsp; If in a code the result&nbsp; $L = N$&nbsp; is never possible, then this code is called&nbsp; "not&ndash;perfect".
 +
*Lempel-Ziv is not suitable for this redundancy-free message source.&nbsp; It will always be&nbsp; $L > N$.&nbsp; You can also put it quite succinctly like this: &nbsp; The perfect source coding here is "no coding at all".
 +
*A redundant binary source with &nbsp;$p_{\rm A} = 0.89$,&nbsp; $p_{\rm B} = 0.11$ &nbsp; ⇒ &nbsp; $H = 0.5$&nbsp; could be represented with a perfect source coding with&nbsp;$L = 5000$,&nbsp; without being able to say what this perfect source coding looks like.
 +
*For a quaternary source,&nbsp; $H > 1 \ \rm (bit/source\ symbol)$&nbsp; is possible, so that even with perfect source coding there will always be&nbsp; $L > N$.&nbsp; If the source is redundancy-free&nbsp; (no memory, all&nbsp; $M$&nbsp; symbols equally probable), it has the entropy&nbsp; $H= 2 \ \rm (bit/source\ symbol)$.
  
Bei allen diesen Beispielen für perfekte Quellencodierung wäre die relative Redundanz der Codefolge (also die Restredundanz) $r = 0$. Das heißt: Die Nullen und Einsen sind gleichwahrscheinlich und es bestehen keine statistischen Bindungen zwischen einzelnen Symbolen.<br>
 
'''Das Problem ist: Bei endlicher Folgenlänge $N$ gibt es keine perfekte Quellencodierung'''.}}
 
  
 +
For all these examples of perfect source coding, the relative redundancy of the encoded sequence&nbsp; ("residual redundancy")&nbsp; is&nbsp; $r = 0$.&nbsp; That is: &nbsp; The zeros and ones are equally probable and there are no correlations between the individual binary symbols.
 +
<br>
  
==Effizienz der Lempel–Ziv–Codierung  ==
+
'''The problem is: &nbsp; At finite sequence length&nbsp; $N$&nbsp; there is no perfect source code'''&nbsp;!}}
 +
 
 +
 
 +
==Efficiency of Lempel-Ziv coding ==
 
<br>
 
<br>
Von den Lempel–Ziv–Algorithmen weiß man (und kann diese Aussage sogar beweisen), dass sie '''asymptotisch optimal''' sind. Das bedeutet, dass die relative Redundanz der Codesymbolfolge
+
From the Lempel-Ziv algorithms we know&nbsp; (and can even prove this statement)&nbsp; that they are&nbsp; &raquo;'''asymptotically optimal'''&laquo;. &nbsp;
 +
*This means that the relative redundancy of the encoded sequence&nbsp; (here written as a function of the source symbol sequence length&nbsp; $N$: 
 
   
 
   
:$$r(N) = \frac{L(N) - N \cdot H}{L(N)}= 1 - \frac{ N \cdot H}{L(N)}\hspace{0.05cm}$$
+
:$$r(N) = \frac{L(N) - N \cdot H}{L(N)}= 1 - \frac{ N \cdot H}{L(N)}\hspace{0.05cm}.$$
  
(hier als Funktion der Quellensymbolfolgenlänge $N$ geschrieben) für große $N$ den Grenzwert $0$ liefert:
+
*For large&nbsp; $N$&nbsp; the limit value returns  "zero":
 
   
 
   
 
:$$\lim_{N \rightarrow \infty}r(N) = 0 \hspace{0.05cm}.$$
 
:$$\lim_{N \rightarrow \infty}r(N) = 0 \hspace{0.05cm}.$$
  
Was aber sagt die Eigenschaft „asymptotisch optimal” für praxisrelevante Folgenlängen aus? Nicht allzu viel, wie der nachfolgende Bildschirmabzug des Flash–Moduls [[Lempel–Ziv–Algorithmen]] zeigt. Die Kurven gelten für den [[Informationstheorie/Komprimierung_nach_Lempel,_Ziv_und_Welch#Der_Lempel.E2.80.93Ziv.E2.80.93Welch.E2.80.93Algorithmus|LZW–Algorithmus]]. Die Ergebnisse für [[Informationstheorie/Komprimierung_nach_Lempel,_Ziv_und_Welch#LZ77_.E2.80.93_die_Grundform_der_Lempel.E2.80.93Ziv.E2.80.93Algorithmen|LZ77]] und [[Informationstheorie/Komprimierung_nach_Lempel,_Ziv_und_Welch#Die_Lempel.E2.80.93Ziv.E2.80.93Variante_LZ78|LZ78]] sind aber nur geringfügig schlechter.
+
But what does the property&nbsp; "asymptotically optimal"&nbsp; say for practical sequence lengths?&nbsp; Not too much, as the following screenshot of our simulation tool&nbsp; [[Applets:Lempel-Ziv-Welch|"Lempel-Ziv algorithms"]]&nbsp; shows. &nbsp; All curves apply exactly only to the [[Information_Theory/Compression_According_to_Lempel,_Ziv_and_Welch#The_Lempel-Ziv-Welch_algorithm|$\text{LZW algorithm}$]].&nbsp; However, the results for&nbsp; [[Information_Theory/Compression_According_to_Lempel,_Ziv_and_Welch#LZ77_-_the_basic_form_of_the_Lempel-Ziv_algorithms|$\text{LZ77}$]]&nbsp; and&nbsp; [[Information_Theory/Compression_According_to_Lempel,_Ziv_and_Welch#The_Lempel-Ziv_variant_LZ78|$\text{LZ78}$]]&nbsp; are only slightly worse.
  
[[File:P_ID2450__Inf_T_2_2_S7b_neu.png|frame|Beispielhafte Verläufe von <i>L</i>(<i>N</i>) und <i>r</i>(<i>N</i>)]]
+
The three graphs show for different message sources the dependence of the following sizes on the source symbol sequence length&nbsp; $N$:
 +
*the required number&nbsp; $N \cdot \log_2 M$&nbsp;  of bits without source coding&nbsp; (black curves),
 +
*the required number&nbsp; $H \cdot N$&nbsp;  of bits with perfect source coding&nbsp; (gray dashed),
 +
*the required number&nbsp; $L(N)$&nbsp;  of bits for LZW coding&nbsp; (red curves after averaging),
 +
*the relative redundancy &nbsp; &rArr; &nbsp; residual redundancy &nbsp;$r(N)$&nbsp; in case of LZW coding (green curves).
  
Die drei Grafiken zeigen für verschiedene Nachrichtenquellen die Abhängigkeit folgender Größen von der Quellensymbolfolgenlänge $N$:
 
*die erforderliche Bitanzahl $N · \log_2 M$ ohne Quellencodierung (schwarze Kurven),
 
*die erforderliche Bitanzahl $H$ · $N$ bei perfekter Quellencodierung (grau–gestrichelt),
 
*die erforderliche Bitanzahl $L(N)$ bei LZW–Codierung (rote Kurven nach Mittelung),
 
*die relative Redundanz &nbsp; &rArr; &nbsp; Restredundanz $r(N)$ bei LZW–Codierung (grüne Kurven).
 
  
 +
[[File:EN_Inf_T_2_2_S7b.png|frame|Example curves of&nbsp; $L(N)$&nbsp; and&nbsp; $r(N)$]]
  
Die obere Grafik gilt für eine redundante Binärquelle: &nbsp; $M = 2, \hspace{0.1cm}p_{\rm A} = 0.89,\hspace{0.1cm} p_{\rm B} = 0.11 \hspace{0.15cm} \Rightarrow \hspace{0.15cm} H = 0.5 \ \rm bit/Quellensymbol$. Man erkennt:
+
$\underline{\text{Redundant binary source (upper graphic)} }$
*Die schwarze und die graue Kurve sind echte Gerade (nicht nur bei diesem Parametersatz).
+
:$$M = 2, \hspace{0.1cm}p_{\rm A} = 0.89,\hspace{0.1cm} p_{\rm B} = 0.11$$
*Die rote Kurve $L(N)$ zeigt eine leichte Krümmung (mit bloßem Auge schwer zu erkennen).
+
:$$\Rightarrow \hspace{0.15cm} H = 0.5 \ \rm bit/source\hspace{0.15cm} symbol\text{:}$$
*Wegen dieser Krümmung von $L(N)$ fällt die grüne Kurve $r(N) = 1 0.5 · N/L(N)$ leicht ab.
+
*The black and gray curves are true straight lines (not only for this parameter set).
*Abzulesen sind die Zahlenwerte $L(N = 10000) = 6800$ und $r(N = 10000) = 26.5\%$.
+
*The red curve&nbsp; $L(N)$&nbsp; is slightly curved&nbsp; (difficult to see with the naked eye).
 +
*Because of this curvature of&nbsp; $L(N)$&nbsp; the residual redundancy (green curve) drops slightly.
 +
:$$r(N) = 1 - 0.5 \cdot N/L(N).$$  
 +
*The numerical values can be read:
 +
:$$L(N = 10000) = 6800,\hspace{0.5cm}
 +
r(N = 10000) = 26.5\%.$$
  
  
Die mittlere Grafik gilt für gleichwahrscheinliche Binärsymbole: &nbsp;  $M = 2,\hspace{0.1cm} p_{\rm A} = p_{\rm B} = 0.5 \hspace{0.15cm} \Rightarrow \hspace{0.15cm} H = 1 \ \rm bit/Quellensymbol$:
+
$\underline{\text{Redundancy-free binary source (middle graphic)} }$
* Hier fallen die graue und die schwarze Gerade zusammen und die leicht gekrümmte rote Kurve liegt erwartungsgemäß darüber.  
+
:$$M = 2,\hspace{0.1cm} p_{\rm A} = p_{\rm B} = 0.5$$
*Obwohl hier die LZW–Codierung eine Verschlechterung bringt – erkennbar aus der Angabe $L(N = 10000) = 12330$, ist die relative Redundanz mit $r(N = 10000) = 18.9\%$ kleiner als bei der oberen Grafik.
+
:$$\Rightarrow \hspace{0.15cm} H = 1 \ \rm bit/source\hspace{0.15cm} symbol\text{:}$$
 +
* Here the gray and the black straight line coincide and the slightly curved red curve lies above it, as expected.  
 +
*Although the LZW coding brings a deterioration here, recognizable from the indication&nbsp; $L(N = 10000) = 12330$, the residual redundancy is smaller than in the upper graph:
 +
:$$r(N = 10000) = 18.9\%.$$
  
  
Die untere Grafik beschreibt eine redundante Quaternärquelle: &nbsp;  $M = 4,\hspace{0.1cm}p_{\rm A} = 0.7,\hspace{0.1cm} p_{\rm B} = p_{\rm C} = p_{\rm D} = 0.1 \hspace{0.15cm} \Rightarrow \hspace{0.15cm}  H \approx 1.357 \ \rm bit/Quellensymbol$:
 
*  Ohne Quellencodierung wären für $N = 10000$ Quaternärsymbole $20000 \ \rm Binärsymbole (Bit)$ erforderlich.
 
* Bei perfekter Quellencodierung ergäben sich $N \cdot H= 13570 \ \rm  Bit$.
 
* Mit der (nicht perfekten) LZW–Codierung benötigt man  $L(N = 10000) ≈ 16485 \ \rm  Bit$.
 
*Die relative Redundanz beträgt hier $r(N = 10000) ≈17.7\%$.
 
  
 +
$\underline{\text{Redundant quaternary source (lower graphic)} }$
 +
:$$M = 4,\hspace{0.1cm}p_{\rm A} = 0.7,\hspace{0.1cm} p_{\rm B} = p_{\rm C} = p_{\rm D} = 0.1$$
 +
:$$ \Rightarrow \hspace{0.15cm} H \approx 1.357 \ \rm bit/source\hspace{0.15cm} symbol\text{:}$$
 +
* Without source coding, for&nbsp; $N = 10000$&nbsp; quaternary symbols&nbsp; $20000$&nbsp; binary symbols (bits) would be required (black curve).
 +
* If source encoding was perfect, this would result in&nbsp; $N \cdot H= 13570$&nbsp; bits&nbsp; (gray curve).
 +
* With (imperfect) LZW encoding you need&nbsp; $L(N = 10000) ≈ 16485$&nbsp; bits&nbsp; (red curve).
 +
*The residual redundancy here is&nbsp; $r(N = 10000) ≈17.7\%$&nbsp; (green curve).
  
==Quantitative Aussagen zur asymptotischen Optimalität== 
+
 
 +
==Quantitative statements on asymptotic optimality== 
 
<br>
 
<br>
Die Ergebnisse der letzten Seite haben gezeigt, dass die relative Restredundanz $r(N = 10000)$ deutlich größer ist als der theoretisch versprochene Wert $r(N \to \infty) = 0$. Dieses praxisrelevante Ergebnis soll nun am Beispiel der redundanten Binärquelle mit $H = 0.5 \ \rm bit/Quellensymbol$ präzisiert werden.
+
The results in the last section have shown that the residual redundancy&nbsp; $r(N = 10000)$&nbsp; is significantly greater than the theoretically promised value&nbsp; $r(N \to \infty) = 0$.  
  
[[File:P_ID2443__Inf_T_2_2_S8_neu.png|frame|LZW–Restredundanz <i>r</i>(<i>N</i>) bei redundanter Binärquelle (<i>r</i> = 0.5) ]]
+
This practically relevant result shall now be clarified using the example of the redundant binary source with&nbsp; $H = 0.5 \ \rm bit/source\hspace{0.15cm} symbol$&nbsp; according to the middle graphic in the last section. However, we now consider for the source symbol sequence length values between&nbsp; $N=10^3$&nbsp; and&nbsp; $N=10^{12}$.
  
Die Grafik zeigt jeweils Simulationen mit $N = 1000$ Binärsymbolen, wobei sich nach Mittelung über 10 Versuchsreihen $r(N = 1000) ≈35.2\%$ ergibt. Unterhalb des gelben Punktes (im Beispiel bei $N ≈ 150$) bringt der LZW–Algorithmus sogar eine Verschlechterung. In diesem Bereich gilt nämlich $L$ > $N$.
+
[[File:EN_Inf_T_2_2_S8.png|frame|LZW residual redundancy&nbsp; $r(N)$&nbsp; with redundant binary source&nbsp; $(H = 0.5)$ ]]
 +
{{GraueBox|TEXT=
 +
$\text{Example 7:}$&nbsp; The graphic on the right shows simulation results with only &nbsp; $N = 1000$&nbsp; binary symbols.
 +
*After averaging over ten series of experiments the result is&nbsp; $r(N = 1000) ≈35.2\%$.  
 +
*Below the yellow dot&nbsp; $($in the example with&nbsp; $N ≈ 150)$&nbsp; the LZW algorithm even brings a deterioration.  
 +
*In this range, namely&nbsp; $L > N$, that is:&nbsp; "Red"&nbsp; is above&nbsp; "black".
  
[[File:P_ID2923__Inf_T_2_2_S8b_neu.png|frame|Einige Zahlenwerte zur Effizienz der LZW–Codierung]]
 
  
Die Tabelle fasst die Simulationsergebnisse für die redundante Binärquelle ($H = 0.5$) zusammen:
+
In the table below, the results for this redundant binary source&nbsp; $(H = 0.5)$&nbsp; are summarized
*In der Zeile 4 ist die Restredundanz $r(N)$ für verschiedene Folgenlängen  zwischen $N =1000$ und $N =50000$ angegeben. Man erkennt den nur langsamen Abfall mit steigendem $N$.
+
 
*Entsprechend Literaturangaben nimmt die Restredundanz mit $1/\lg(N)$ ab. In Zeile 5 sind die Ergebnisse einer empirischen Formel eingetragen (Anpassung für $N = 10000$):
+
[[File:EN_Inf_T_2_2_S8c.png|right|frame|Some numerical values for the efficiency of LZW coding]]
 +
 
 +
*The compression factor&nbsp; $K(N)= L(N)/N$&nbsp; decreases with increasing&nbsp; $N$&nbsp; only very slowly&nbsp; (line 3).
 +
*In line 4 the residual redundancy&nbsp; $r(N)$&nbsp; is given for different lengths between&nbsp; $N =1000$&nbsp; and&nbsp; $N =50000$.  
 +
*According to relevant literature this residual redundancy decreases proportionally to&nbsp; $\big[\hspace{0.05cm}\lg(N)\hspace{0.05cm}\big]^{-1}$.  
 +
*In line 5 the results of an empirical formula are entered $($adaptation for $N = 10000)$:
 
   
 
   
:$$r'(N) = {A}/{{\rm lg}\hspace{0.1cm}(N)}
+
:$$r\hspace{0.05cm}'(N) = \frac{A}{ {\rm lg}\hspace{0.1cm}(N)}\hspace{0.5cm}{\rm with}$$
\hspace{0.5cm}{\rm mit}\hspace{0.5cm} A = {r(N = 10000)} \cdot {{\rm lg}\hspace{0.1cm}10000} = 0.265 \cdot 4 = 1.06
+
:$$ A = {r(N = 10000)} \cdot { {\rm lg}\hspace{0.1cm}10000} = 0.265 \cdot 4 = 1.06
 
\hspace{0.05cm}.$$
 
\hspace{0.05cm}.$$
 
+
*You can see the good agreement between our simulation results&nbsp; $r(N)$&nbsp; and the rule of the thumb&nbsp; $r\hspace{0.05cm}′(N)$.  
 
+
*You can also see that the residual redundancy of the LZW algorithm for&nbsp; $N = 10^{12}$&nbsp; is still&nbsp; $8.8\%$&nbsp;.
Man erkennt die gute Übereinstimmung zwischen unseren Simulationsergebnissen $r(N)$, basierend auf unserem Interaktionsmodul [[Lempel–Ziv–Algorithmen]], und der Faustformel $r′(N)$. Man erkennt aber auch, dass für $N = 10^{12}$ die Restredundanz des LZW–Algorithmus noch immer 8.8% beträgt.
+
*For other sources, with other&nbsp; $A$&ndash;values you will get similar results.&nbsp; The principle process remains the same.&nbsp; See also&nbsp; [[Aufgaben:Exercise_2.5:_Residual_Redundancy_with_LZW_Coding|"Exercise 2.5"]]&nbsp; and&nbsp; [[Aufgaben:Aufgabe_2.5Z:_Komprimierungsfaktor_vs._Restredundanz|"Exercise 2.5Z"]].}}
 
 
Bei anderen Quellen erhält man mit anderen Zahlenwerten des Parameters $A$ ähnliche Ergebnisse. Der prinzipielle Kurvenverlauf bleibt aber gleich. Siehe auch [[Aufgaben:2.05_Relative_Restredundanz|Aufgabe A2.5]] und [[Aufgaben:2.05Z_LZW-Komprimierung|Aufgabe Z2.5]].
 
  
 
 
 
 
==Aufgaben zum Kapitel  ==
+
==Exercises for the chapter ==  
 
<br>
 
<br>
[[Aufgaben:2.3 Zur LZ78-Komprimierung|Aufgabe 2.3: Zur LZ78-Komprimierung]]
+
[[Aufgaben:Exercise_2.3:_About_the_LZ78_Compression|Exercise 2.3: About the LZ78 Compression]]
  
[[Aufgaben:2.3Z Zur LZ77-Codierung|Aufgabe 2.3Z: Zur LZ77-Codierung]]
+
[[Aufgaben:Exercise_2.3Z:_To_the_LZ77_Coding|Exercise 2.3Z: To the LZ77 Coding]]
  
[[Aufgaben:2.4 Zum LZW-Algorithmus|Aufgabe 2.4: Zum LZW-Algorithmus]]
+
[[Aufgaben:Exercise_2.4:_About_the_LZW_algorithm|Exercise 2.4: About the LZW algorithm]]
  
[[Aufgaben:2.4Z Nochmals LZW-Codierung und -Decodierung|Aufgabe 2.4Z: Nochmals LZW-Codierung und -Decodierung]]
+
[[Aufgaben:Exercise_2.4Z:_LZW_Coding_and_Decoding_again|Exercise 2.4Z: LZW Coding and Decoding again]]
  
[[Aufgaben:2.5 Relative Restredundanz|Aufgabe 2.5: Relative Restredundanz]]
+
[[Aufgaben:Exercise_2.5:_Residual_Redundancy_with_LZW_Coding|Exercise 2.5: Residual Redundancy with LZW Coding]]
  
[[Aufgaben:2.5Z Komprimierungsfaktor vs. Restredundanz|Aufgabe 2.5Z: Komprimierungsfaktor vs. Restredundanz]]
+
[[Aufgaben:Exercise_2.5Z:_Compression_Factor_vs._Residual_Redundancy|Exercise_2.5Z: Compression Factor vs. Residual Redundancy]]
  
  
 
{{Display}}
 
{{Display}}

Latest revision as of 19:05, 20 February 2023


Static and dynamic dictionary techniques


Many data compression methods use dictionaries.  The idea is the following:

  • Construct a list of character patterns that occur in the text,
  • and encode these patterns as indices of the list.


This procedure is particularly efficient if certain patterns are repeated frequently in the text and if this is also taken into account in the coding.  A distinction is made here between

  • procedures with static dictionary,
  • procedures with dynamic dictionary.




$\text{(1)   Procedures with static dictionary}$

A static dictionary is only useful for very special applications, for example for a file of the following form:

Data file  (rubrics:  "Name",  "Vorname"   ⇒   "first name",  "Wohnort"   ⇒   "residence")

For example, the assignments result in

$$"\boldsymbol{\rm 0}" \hspace{0.05cm} \mapsto \hspace{0.05cm} \boldsymbol{\rm 000000} \hspace{0.05cm},\hspace{0.15cm} ... \hspace{0.15cm},\hspace{0.05cm} "\boldsymbol{\rm 9}" \hspace{0.05cm} \mapsto \hspace{0.05cm} \boldsymbol{\rm 001001} \hspace{0.05cm}, "\hspace{-0.03cm}\_\hspace{-0.03cm}\_\hspace{0.03cm}" \hspace{0.1cm}{\rm (blank)}\hspace{0.05cm} \mapsto \hspace{0.05cm} \boldsymbol{\rm 001010} \hspace{0.05cm},$$
$$"\hspace{-0.01cm}.\hspace{-0.01cm}" \hspace{0.1cm}{\rm (point)}\hspace{0.05cm} \mapsto \hspace{0.05cm} \boldsymbol{\rm 001011} \hspace{0.05cm}, "\hspace{-0.01cm},\hspace{-0.01cm}" \hspace{0.1cm}{\rm (comma)}\hspace{0.05cm} \mapsto \hspace{0.05cm} \boldsymbol{\rm 001011} \hspace{0.05cm}, " {\rm end\hspace{-0.1cm}-\hspace{-0.1cm}of\hspace{-0.1cm}-\hspace{-0.1cm}line}\hspace{0.05cm} \mapsto \hspace{0.05cm} \boldsymbol{\rm 001101} \hspace{0.05cm},$$
$$"\boldsymbol{\rm A}" \hspace{0.05cm} \mapsto \hspace{0.05cm} \boldsymbol{\rm 100000} \hspace{0.05cm},\hspace{0.15cm} ... \hspace{0.15cm},\hspace{0.05cm} "\boldsymbol{\rm E}" \hspace{0.05cm} \mapsto \hspace{0.05cm} \boldsymbol{\rm 100100} \hspace{0.05cm}, \hspace{0.15cm} ... \hspace{0.15cm},\hspace{0.05cm} "\boldsymbol{\rm L}" \hspace{0.05cm} \mapsto \hspace{0.05cm} \boldsymbol{\rm 101011} \hspace{0.05cm},\hspace{0.15cm}"\boldsymbol{\rm M}" \hspace{0.05cm} \mapsto \hspace{0.05cm} \boldsymbol{\rm 101100} \hspace{0.05cm},$$
$$"\boldsymbol{\rm O}" \hspace{0.05cm} \mapsto \hspace{0.05cm} \boldsymbol{\rm 101110} \hspace{0.05cm},\hspace{0.15cm} ... \hspace{0.15cm},\hspace{0.05cm} "\boldsymbol{\rm U}" \hspace{0.05cm} \mapsto \hspace{0.05cm} \boldsymbol{\rm 110100} \hspace{0.05cm}, "\boldsymbol{\rm Name\hspace{-0.1cm}:\hspace{-0.05cm}\_\hspace{-0.03cm}\_}" \hspace{0.05cm} \mapsto \hspace{0.05cm} \boldsymbol{\rm 010000} \hspace{0.05cm},\hspace{0.05cm}$$
$$"\boldsymbol{\rm ,\_\hspace{-0.03cm}\_Vorname\hspace{-0.1cm}:\hspace{-0.05cm}\_\hspace{-0.03cm}\_}" \hspace{0.05cm} \mapsto \hspace{0.05cm} \boldsymbol{\rm 010001} \hspace{0.05cm},\hspace{0.05cm} "\boldsymbol{\rm ,\_\hspace{-0.03cm}\_Wohnort\hspace{-0.1cm}:\hspace{-0.05cm}\_\hspace{-0.03cm}\_}" \hspace{0.05cm} \mapsto \hspace{0.05cm} \boldsymbol{\rm 010010} \hspace{0.05cm},\hspace{0.15cm} ... \hspace{0.15cm}$$

for the first line of the above text, binary source coded with six bits per character:

$$\boldsymbol{010000} \hspace{0.15cm}\boldsymbol{100000} \hspace{0.15cm}\boldsymbol{100001} \hspace{0.15cm}\boldsymbol{100100} \hspace{0.15cm}\boldsymbol{101011} \hspace{0.3cm} \Rightarrow \hspace{0.3cm} \boldsymbol{(\rm Name\hspace{-0.1cm}:\hspace{-0.05cm}\_\hspace{-0.03cm}\_) \hspace{0.05cm}(A)\hspace{0.05cm}(B)\hspace{0.05cm}(E)\hspace{0.05cm}(L)}$$
$$\boldsymbol{010001} \hspace{0.15cm}\boldsymbol{101011}\hspace{0.15cm} \boldsymbol{100100} \hspace{0.15cm}\boldsymbol{101110} \hspace{0.3cm} \Rightarrow \hspace{0.3cm} \boldsymbol{(,\hspace{-0.05cm}\_\hspace{-0.03cm}\_\rm Vorname\hspace{-0.1cm}:\hspace{-0.05cm}\_\hspace{-0.03cm}\_) \hspace{0.05cm}(L)\hspace{0.05cm}(E)\hspace{0.05cm}(O)}$$
$$\boldsymbol{010010} \hspace{0.15cm}\boldsymbol{110100} \hspace{0.15cm}\boldsymbol{101011} \hspace{0.15cm}\boldsymbol{101100} \hspace{0.3cm}\Rightarrow \hspace{0.3cm} \boldsymbol{(,\hspace{-0.05cm}\_\hspace{-0.03cm}\_\rm Wohnort\hspace{-0.1cm}:\hspace{-0.05cm}\_\hspace{-0.03cm}\_) \hspace{0.05cm}(U)\hspace{0.05cm}(L)\hspace{0.05cm}(M)} \hspace{0.05cm} $$
$$\boldsymbol{001101} \hspace{0.3cm}\Rightarrow \hspace{0.3cm} ({\rm end\hspace{-0.1cm}-\hspace{-0.1cm}of\hspace{-0.1cm}-\hspace{-0.1cm}line}) \hspace{0.05cm}$$

$\text{Conclusion:}$  In this specific application the first line can be displayed with  $14 \cdot 6 = 84$  bits.

  • In contrast, conventional binary coding would require  $39 \cdot 7 = 273$  bits
          (because of the lowercase letters in the text, six bits per character are not sufficient here).
  • For the entire text, this results in  $103 \cdot 6 = 618$  bits versus  $196 \cdot 7 = 1372$  bits.
  • However, the code table must also be known to the recipient.




$\text{(2)   Procedures with dynamic dictionary}$

Nevertheless, all relevant compression methods do not work with static dictionaries, but with  »dynamic dictionaries«, which are created successively only during the coding:

  • Such procedures are flexible and do not have to be adapted to the application.   One speaks of »universal source coding procedures«.
  • A single pass is sufficient, whereas with a static dictionary the file must first be analyzed before the encoding process.
  • At the sink, the dynamic dictionary is generated in the same way as for the source.  This eliminates the need to transfer the dictionary.


Extract from the hexdump of a natural image in BMP format

$\text{Example 1:}$  The graphic shows a small section of  $80$  byte of a  $\text{BMP file}$  in hexadecimal format.  It is the uncompressed representation of a natural picture.

  • You can see that in this small section of a landscape image the bytes  $\rm FF$,  $\rm 55$  and  $\rm 47$  occur very frequently.  Data compression is therefore promising.
  • But since other parts of the file or other byte combinations dominate in other image contents, the use of a static dictionary would not be appropriate here.


Possible encoding of a simple graphic

$\text{Example 2:}$  For an artificially created graphic, for example a form, you could work with a static dictionary.

We are looking at a  "black & white"  image with  $27 × 27$  pixels, where the mapping "black"   ⇒   0  and "white"   ⇒   1  has been agreed upon.

  • At the top  (black marker),  each line is described by  $27$  zeros.
  • In the middle  (blue marker),  three zeros and three ones always alternate.
  • At the bottom  (red marker),  25 ones per line are limited by two zeros.


LZ77 - the basic form of the Lempel-Ziv algorithms


The most important procedures for data compression with a dynamic dictionary go back to  $\text{Abraham Lempel}$  and  $\text{Jacob Ziv}$.  The entire Lempel-Ziv family  (in the following we will use for this briefly:   $\rm LZ$  procedure)  can be characterized as follows:

  • Lempel-Ziv methods use the fact that often whole words, or at least parts of them, occur several times in a text.  One collects all word fragments, which are also called  "phrases"  in a sufficiently large dictionary.
  • Contrary to the entropy coding developed some years before (by Shannon and Huffman), the frequency of single characters or character strings is not the basis of the compression here, so that the LZ procedures can be applied even without knowledge of the source statistics.
  • LZ compression accordingly manages with a single pass and also the source symbol set size  $M$  and the symbol set  $\{q_μ\}$  with  $μ = 1$, ... , $M$  does not have to be known.  This is called  »Universal Source Coding«.


We first look at the Lempel-Ziv algorithm in its original form from 1977, known as  $\rm LZ77$:

Sliding window with LZ77 compression
  • This works with a window that is successively moved over the text   ⇒   one also speaks of a  "sliding window".
  • The window size  $G$  is an important parameter that decisively influences the compression result.


The graphic shows an example of the  sliding windows.  This is divided into

  • the  "preview buffer"  $($blue background),  and
  • the  "search buffer"  $($red background, with the positions 
    $P = 0$, ... , $7$   ⇒   window size  $G = 8)$.


The text consists of the words  MissMissionMississippi  and  Mistral, each separated by a hyphen.  At the time under consideration the preview buffer contains  Mississi.

  • Search now in the search buffer for the best match   ⇒   the string with the maximum match length  $L$.  This is the result for position  $P = 7$  and length  $L = 5$  to  Missi.
  • This step is expressed by the triple  $(7,  5,  $ s$)$   ⇒   general  $(P, \ L, \ Z)$, where  $Z =$ s  specifies the character that no longer matches the string found in the search buffer.
  • At the end the window is moved by  $L + 1 = 6$  character is moved to the right.  In the preview buffer there is now  sippi-Mi,  and in the search buffer  n-Missis
    The encoding gives the triple  $(2, 2,$  p$)$.


In the following example the LZ77 coding algorithm is described in more detail.  The decoding runs in a similar way.

$\text{Example 3:}$  We consider the LZ77 encoding of the string  ABABCBCBAABCABe  according to the following graphic.  The input sequence has the length $N = 15$. 

Further is assumed:

  • For the characters apply  $Z ∈ \{$ ABCe $\}$,  where  e  corresponds to the  "end-of-file"  (end of the input string)
  • The size of the preview and search buffer are  $G = 4$   ⇒   position  $P ∈ \{0, 1, 2, 3\}$.
To illustrate the LZ77 encoding


Display of the encoding process:

Step 1 and 2:   The characters  A  and  B  are encoded by the triple  $(0, 0,  $ A$)$  and  $(0, 0,  $ B$)$, because they are not yet stored in the search buffer.   Then move the sliding window by 1.

Step 3:   AB  is masked by the search buffer and at the same time the still unknown character  C  is appended.  After that the sliding window is moved three positions to the right.

Step 4:   This shows that the search string  BCB  may also end in the preview buffer.  Now the window can be moved four positions to the right.

Step 5:   Only  A  is found in the search buffer and  B  is dropped.  If the search buffer is larger, however,  ABC  could be masked together.  For this purpose must be  $G ≥ 7$.

Step 6:   Likewise, the character  C  must be coded separately due to the buffer being too small.  But since  CA  hasn't occurred before,  $G = 7$  would not improve the compression here.

Step 7:   With the consideration of the end-of-file  (e)  together with  AB  from the search buffer, the encoding process is finished.


Before transmission, the specified triples must of course be binary coded.  In this example

  • the position  $P ∈ \{0,\ 1,\ 2,\ 3\}$  needs two bits  (yellow background in the table above),
  • the copy length  $L$  needs three bits  (green background), so that also  $L = 7$  could still be displayed,
  • all characters need two bits  (white background),  for example  A00B01C10e ("end-of-file") → 11.


Thus the  $\rm LZ77$  output sequence has a length of  $7 \cdot 7 = 49$  bits, while the input sequence only needed  $15 \cdot 2 = 30$  bits.


$\text{Conclusion:}$  Lempel-Ziv compression only makes sense with large files !


The Lempel-Ziv variant LZ78


The LZ77 algorithm produces very inefficient output if more frequent strings are repeated only with a larger distance.  Such repetitions can often not be recognized due to the limited buffer size  $G$  of the sliding window.

Lempel and Ziv corrected this shortcoming already one year after the release of the first version LZ77:

  • The algorithm  $\rm LZ78$  uses a global dictionary for compression instead of the local dictionary (search buffer).
  • The size of the dictionary allows efficient compression of phrases that have been used for a long time before.


$\text{Example 4:}$  To explain the LZ78 algorithm we consider the same sequence  ABABCBCBAABCABe  as for the LZ77 $\text{Example 3}$.

Generation of the dictionary and output at LZ78

The graphic shows  (with red background)  the dictionary

  • with index  $I $  (in decimal and binary representation, column 1 and 2) 
  • and the corresponding content (column 3),
  • which is entered for coding step  $i $  (column 4).  For LZ78 both encoding and decoding are always  $i = I$.
  • In column 5 you find the formalized code output  $($Index  $I$,  new character  $Z)$.
  • In column 6 the corresponding binary coding is given with four bits for the index and the same character assignment  A00B01C10e ("end-of-file") → 11  as in  $\text{Example 3}$.


  • At the beginning  (step $\underline{i = 0}$)  the dictionary is  (WB)  empty except for the entry  ε  $($empty character, not to be confused with the space character, which is not used here$)$  with index  $I = 0$.
  • In the step  $\underline{i = 1}$  there is no usable entry in the dictionary yet, and it becomes  (0,  A)  output  (A  follows  ε).   In the dictionary, the entry  A  follows in line  $I = 1$  (abbreviated  1: A).
  • The procedure in the second step  ($\underline{i = 2}$).  The output is  (0,  B)  and the dictionary entry is  2: B .
  • As the entry  $\underline{i = 3}$  is already found in step  1: A , the characters  AB  can be coded together by  (1, B)  and the new dictionary entry  3: AB  is made.
  • After coding and insertion of the new character  C  in step  $\underline{i = 4}$  the pair of characters  BC  is coded together   ⇒   (2, C) and entered into the dictionary  5: BC  in step  $\underline{i = 5}$ .
  • In step  $\underline{i = 6}$  two characters are treated together with  '6: BA  and in the last two steps three each, namely  7: ABC  and  8: ABe.
  • The output  (3, C)  in step  $\underline{i = 7}$  stands for  "WB(3) + C = ABC   and the output  (3, e)  in step  $\underline{i = 8}$  for  ABe.


In this  $\text{Example 4}$  the LZ78 encoded sequence thus consists of  $8 - 6 = 48$  bit.  The result is comparable to the LZ77-$\text{Example 3}$  $(49$ bit$)$.


$\text{Conclusion:}$  Details and improvements of LZ78 will be omitted here.  Here we refer to the  $\text{LZW algorithm}$, which will be described in the following sections.  Only this much will be said now:

  • The index  $I$  is uniformly represented here with four bits, whereby the dictionary is limited to  $16$  entries.  By a  "variable number of bits"  for the index one can bypass this limitation.  At the same time one gets a better compression factor.
  • The dictionary does not have to be transmitted with all LZ variants, but is generated with the decoder in exactly the same way as on the encoder side.  The decoding is also done with LZ78, but not with LZW, in the same way as the encoding.
  • All LZ procedures are asymptotically optimal, i.e., for infinitely long sequences the mean code word length  $L_{\rm M}$  per source symbol is equal to the source entropy  $H$.
  • For short sequences, however, the deviation is considerable.  More about this at the  "end of chapter".


The Lempel-Ziv-Welch algorithm


The most common variant of Lempel-Ziv compression used today was designed by  $\text{Terry Welch}$  and published in 1983.  In the following we refer to it as the  "Lempel-Ziv-Welch-Algorithm", abbreviated as  $\rm LZW$.   Just as LZ78 has slight advantages over LZ77  (as expected, why else would the algorithm have been modified?),  LZW also has more advantages than disadvantages compared to LZ78.

LZW encoding of the sequence  ABABCBCBAABCABe

The graphic shows the coder output for the exemplary input sequence  ABABCBCBAABCABe.  On the right you can see the dictionary  (highlighted in red), which is successively generated during LZW encoding.  The differences to LZ78 can be seen in comparison to the graphic in the last section, namely:

  • For LZW, all characters occurring in the text are already entered at the beginning  $(i = 0)$  and assigned to a binary sequence, in the example with the indices  $I = 0$, ... ,  $I = 3$. 
  • This also means that LZW requires some knowledge of the message source, whereas LZ78 is a  "true universal encoding".
  • For LZW, only the dictionary index  $I$  is transmitted for each encoding step  $i$  while for LZ78 the output is the combination  $(I$,  $Z)$, where $Z$  denotes the current new character.  
  • Due to the absence of  $Z$  in the encoder output, LZW decoding is more complicated than with LZ78, as described in section  "Decoding of the LZW algorithm".


$\text{Example 5:}$  For this exemplary  $\rm LZW$  encoding, again the input sequence  ABABCBCBAABCABe  is assumed as with "LZ77" and "LZ78".  So the following description refers to the above graphic.

Step i = 0  (default):   The allowed characters  ABC  and  e  ("end-of-file") are entered into the dictionary and assigned to the indices  $I = 0$, ... , $I = 3$ .

Step i = 1:   The character  A  is encoded by the decimal index  $I = 0$  and its binary representation  0000  is transmitted.  
Then the combination of the current character  A  and the following character  B  of the input sequence is stored in the dictionary under the index  $I = 4$ .

step i = 2:   Representation of  B  by index  $I = 1$  or  0001  (binary)  as well as dictionary entry of  BA  placed under index  $I = 5$.

Step i = 3:   Because of the entry  AB  at time  $i = 1$  the index to be transmitted is  $I = 4$  (binary:  0100).  New dictionary entry of  ABC  under  $I = 6$.

Step i = 8:   Here the characters  ABC  are represented together by the index  $I = 6$  (binary:  0110)  and the entry for  ABCA  is made.

With the encoding of  e  (EOF mark) the encoding process is finished after ten steps.  With LZ78 only eight steps were needed.  But it has to be considered:

  • The LZW algorithm needs only  $10 \cdot 4 = 40$  bit versus the  $8 \cdot 6 = 48$  bit for LZ78.  Provided that for this simple calculation, four bits each are needed for index representation.
  • LZW as well as LZ78 require less bits  $($namely   $34$  or   $42)$, if one considers that for the step  $i = 1$  the index has to be encoded with two bits only  $(I ≤ 3)$  and for  $2 ≤ i ≤ 5$  three bits are sufficient  $(I ≤ 7)$.


Lempel-Ziv coding with variable index bit length


For reasons of a most compact representation, we now consider only binary sources with symbol set  $\{$A, B$\}$.  The terminating character  »end-of-file«  is not considered, too.

LZW encoding of a binary input sequence

We demonstrate the LZW encoding by means of a screenshot of our interactive SWF module  "Lempel-Ziv-Welch algorithms".

  • In the first encoding step  $(i = 1)$:  A   ⇒  0.  Afterwards the entry with index  $I = 2$  and  content  AB.
  • As there are only two dictionary entries in step  $i = 1$  (for  A  and  B ) one bit is sufficient.   For step  $i = 2$  and  $i = 3$  $($B  ⇒  01  and   A  ⇒  00$)$  two bits are needed in each case.
  • Starting on  $i = 4$ , the index representation is done with three bits, then from   $i = 8$  with four bits and from  $i = 16$  with five bits.  A simple algorithm for the respective index bit number  $L(i)$  can be derived.
  • Let's finally consider the encoding step  $i = 18$.  Here, the sequence  ABABB  marked in red, which was entered into the dictionary at time  $i = 12$  at time  $($Index  $I = 13$ ⇒ 1101$)$  is edited.   However, the encoder output is now  01101 because of  $i ≥ 16$  (green mark at the encoder output).


The statements also apply to  $\text{LZ78}$.  That is:   With LZ78 a variable index bit length results in the same improvement as with LZW.


Decoding of the LZW algorithm


The decoder now displays the decoded output in the  "last section"  as input sequence.  The graphic shows that it is possible to uniquely decode this sequence even with variable index bit lengths. Please note:

  1.  The decoder knows that in the first encoding step  $(i = 1)$  the index  $I $ was encoded with only one bit, in the steps  $i = 2$  and  $i = 3$  with two bit, from   $i = 4$  with three bit, from  $i = 8$  with four bit, and so on.
  2.  The decoder generates the same dictionary as the encoder, but the dictionary entries are made one time step later.
LZW decoding of a binary input sequence


  • At step  $\underline{i = 1}$  the adjacent symbol  0  is decoded as  A.   Likewise, the following results for step  $\underline{i = 2}$  from the preassignment of the dictionary and the two-bit representation agreed upon for this:   01   ⇒   B.
  • The entry of the line  $\underline{I = 2}$  $($content:   AB$)$  of the dictionary is therefore only made at the step  $\underline{i = 2}$, while at the  $\text{encoding process}$  this could already be done at the end of step  $i = 1$.
  • Let us now consider the decoding for  $\underline{i = 4}$.   The index  $\underline{I = 2}$  returns the decoding result  010   ⇒   AB  and in the next step  $(\underline{i = 5})$  the dictionary line  $\underline{I = 5}$  will be filled with  ABA
  • This time difference with respect to the dictionary entries can lead to decoding problems.  For example, for step  $\underline{i = 7}$  there is no dictionary entry with index  $\underline{I= 7}$.
  • What is to do in such a case as  $(\underline{I = i})$?  In this case you take the result of the previous decoding step  $($here:   BA  for  $\underline{i = 6})$  and append the first character of this sequence at the end again.   This gives the decoding result for  $\underline{i = 7}$  to  111   ⇒   BAB.
  • Naturally, it is unsatisfactory to specify only one recipe.  In the  "Exercise 2.4Z"  you should justify the procedure demonstrated here.  We refer to the sample solution for this exercise.


With LZ78 decoding, the problem described here does not occur because not only the index  $I $  but also the current character  $Z$  is included in the encoding result and is transmitted.


Remaining redundancy as a measure for the efficiency of coding methods


For the rest of this chapter we assume the following prerequisites:

  • The source symbol set size  $($or in the transmission sense:   the number of levels )  is  $M$, where  $M$  represents a power of two   ⇒   $M = 2, \ 4, \ 8, \ 16$, ....
  • The source entropy is  $H$.  If there are no correlations between the symbols and if they are equally probable, then  $H = H_0 = \log_2 \ M$.  Otherwise, $H < H_0$ applies.
  • A symbol sequence of length  $N$  is source-coded and returns a binary encoded sequence of length  $L$. 
  • For the time being we do not make any statement about the type of source coding.


According to the  $\text{Source Coding Theorem}$  the average code word length  $L_{\rm M}$  must be greater than or equal to the source entropy  $H$  (in bit/source symbol).  This means

  • for the total length of the source-coded binary sequence:
$$L \ge N \cdot H \hspace{0.05cm},$$
  • for the relative redundancy of the coded sequence, in the following called  »residual redundancy«:
$$r = \frac{L - N \cdot H}{L} \hspace{0.05cm}.$$

$\text{Example 6:}$  If there were a  "perfect source coding"  for a redundancy-free binary source sequence  $(M = 2,\ p_{\rm A} = p_{\rm B} = 0.5$,  without memory$)$  of length  $N = 10000$, the encoder sequence would have also the length  $L = 10000$.

Consequence:   If in a code the result  $L = N$  is never possible, then this code is called  "not–perfect".

  • Lempel-Ziv is not suitable for this redundancy-free message source.  It will always be  $L > N$.  You can also put it quite succinctly like this:   The perfect source coding here is "no coding at all".
  • A redundant binary source with  $p_{\rm A} = 0.89$,  $p_{\rm B} = 0.11$   ⇒   $H = 0.5$  could be represented with a perfect source coding with $L = 5000$,  without being able to say what this perfect source coding looks like.
  • For a quaternary source,  $H > 1 \ \rm (bit/source\ symbol)$  is possible, so that even with perfect source coding there will always be  $L > N$.  If the source is redundancy-free  (no memory, all  $M$  symbols equally probable), it has the entropy  $H= 2 \ \rm (bit/source\ symbol)$.


For all these examples of perfect source coding, the relative redundancy of the encoded sequence  ("residual redundancy")  is  $r = 0$.  That is:   The zeros and ones are equally probable and there are no correlations between the individual binary symbols.

The problem is:   At finite sequence length  $N$  there is no perfect source code !


Efficiency of Lempel-Ziv coding


From the Lempel-Ziv algorithms we know  (and can even prove this statement)  that they are  »asymptotically optimal«.  

  • This means that the relative redundancy of the encoded sequence  (here written as a function of the source symbol sequence length  $N$:
$$r(N) = \frac{L(N) - N \cdot H}{L(N)}= 1 - \frac{ N \cdot H}{L(N)}\hspace{0.05cm}.$$
  • For large  $N$  the limit value returns "zero":
$$\lim_{N \rightarrow \infty}r(N) = 0 \hspace{0.05cm}.$$

But what does the property  "asymptotically optimal"  say for practical sequence lengths?  Not too much, as the following screenshot of our simulation tool  "Lempel-Ziv algorithms"  shows.   All curves apply exactly only to the $\text{LZW algorithm}$.  However, the results for  $\text{LZ77}$  and  $\text{LZ78}$  are only slightly worse.

The three graphs show for different message sources the dependence of the following sizes on the source symbol sequence length  $N$:

  • the required number  $N \cdot \log_2 M$  of bits without source coding  (black curves),
  • the required number  $H \cdot N$  of bits with perfect source coding  (gray dashed),
  • the required number  $L(N)$  of bits for LZW coding  (red curves after averaging),
  • the relative redundancy   ⇒   residual redundancy  $r(N)$  in case of LZW coding (green curves).


Example curves of  $L(N)$  and  $r(N)$

$\underline{\text{Redundant binary source (upper graphic)} }$

$$M = 2, \hspace{0.1cm}p_{\rm A} = 0.89,\hspace{0.1cm} p_{\rm B} = 0.11$$
$$\Rightarrow \hspace{0.15cm} H = 0.5 \ \rm bit/source\hspace{0.15cm} symbol\text{:}$$
  • The black and gray curves are true straight lines (not only for this parameter set).
  • The red curve  $L(N)$  is slightly curved  (difficult to see with the naked eye).
  • Because of this curvature of  $L(N)$  the residual redundancy (green curve) drops slightly.
$$r(N) = 1 - 0.5 \cdot N/L(N).$$
  • The numerical values can be read:
$$L(N = 10000) = 6800,\hspace{0.5cm} r(N = 10000) = 26.5\%.$$


$\underline{\text{Redundancy-free binary source (middle graphic)} }$

$$M = 2,\hspace{0.1cm} p_{\rm A} = p_{\rm B} = 0.5$$
$$\Rightarrow \hspace{0.15cm} H = 1 \ \rm bit/source\hspace{0.15cm} symbol\text{:}$$
  • Here the gray and the black straight line coincide and the slightly curved red curve lies above it, as expected.
  • Although the LZW coding brings a deterioration here, recognizable from the indication  $L(N = 10000) = 12330$, the residual redundancy is smaller than in the upper graph:
$$r(N = 10000) = 18.9\%.$$


$\underline{\text{Redundant quaternary source (lower graphic)} }$

$$M = 4,\hspace{0.1cm}p_{\rm A} = 0.7,\hspace{0.1cm} p_{\rm B} = p_{\rm C} = p_{\rm D} = 0.1$$
$$ \Rightarrow \hspace{0.15cm} H \approx 1.357 \ \rm bit/source\hspace{0.15cm} symbol\text{:}$$
  • Without source coding, for  $N = 10000$  quaternary symbols  $20000$  binary symbols (bits) would be required (black curve).
  • If source encoding was perfect, this would result in  $N \cdot H= 13570$  bits  (gray curve).
  • With (imperfect) LZW encoding you need  $L(N = 10000) ≈ 16485$  bits  (red curve).
  • The residual redundancy here is  $r(N = 10000) ≈17.7\%$  (green curve).


Quantitative statements on asymptotic optimality


The results in the last section have shown that the residual redundancy  $r(N = 10000)$  is significantly greater than the theoretically promised value  $r(N \to \infty) = 0$.

This practically relevant result shall now be clarified using the example of the redundant binary source with  $H = 0.5 \ \rm bit/source\hspace{0.15cm} symbol$  according to the middle graphic in the last section. However, we now consider for the source symbol sequence length values between  $N=10^3$  and  $N=10^{12}$.

LZW residual redundancy  $r(N)$  with redundant binary source  $(H = 0.5)$

$\text{Example 7:}$  The graphic on the right shows simulation results with only   $N = 1000$  binary symbols.

  • After averaging over ten series of experiments the result is  $r(N = 1000) ≈35.2\%$.
  • Below the yellow dot  $($in the example with  $N ≈ 150)$  the LZW algorithm even brings a deterioration.
  • In this range, namely  $L > N$, that is:  "Red"  is above  "black".


In the table below, the results for this redundant binary source  $(H = 0.5)$  are summarized

Some numerical values for the efficiency of LZW coding
  • The compression factor  $K(N)= L(N)/N$  decreases with increasing  $N$  only very slowly  (line 3).
  • In line 4 the residual redundancy  $r(N)$  is given for different lengths between  $N =1000$  and  $N =50000$.
  • According to relevant literature this residual redundancy decreases proportionally to  $\big[\hspace{0.05cm}\lg(N)\hspace{0.05cm}\big]^{-1}$.
  • In line 5 the results of an empirical formula are entered $($adaptation for $N = 10000)$:
$$r\hspace{0.05cm}'(N) = \frac{A}{ {\rm lg}\hspace{0.1cm}(N)}\hspace{0.5cm}{\rm with}$$
$$ A = {r(N = 10000)} \cdot { {\rm lg}\hspace{0.1cm}10000} = 0.265 \cdot 4 = 1.06 \hspace{0.05cm}.$$
  • You can see the good agreement between our simulation results  $r(N)$  and the rule of the thumb  $r\hspace{0.05cm}′(N)$.
  • You can also see that the residual redundancy of the LZW algorithm for  $N = 10^{12}$  is still  $8.8\%$ .
  • For other sources, with other  $A$–values you will get similar results.  The principle process remains the same.  See also  "Exercise 2.5"  and  "Exercise 2.5Z".


Exercises for the chapter


Exercise 2.3: About the LZ78 Compression

Exercise 2.3Z: To the LZ77 Coding

Exercise 2.4: About the LZW algorithm

Exercise 2.4Z: LZW Coding and Decoding again

Exercise 2.5: Residual Redundancy with LZW Coding

Exercise_2.5Z: Compression Factor vs. Residual Redundancy