Difference between revisions of "Aufgaben:Exercise 2.3Z: About the LZ77 Coding"

From LNTwww
Line 3: Line 3:
 
}}
 
}}
  
[[File:P_ID2436__Inf_Z_2_3.png|right|frame|Sliding–Window mit&nbsp; $G = 4$&nbsp; und&nbsp; $G = 5$<br>(valid for "LZ77")]]
+
[[File:P_ID2436__Inf_Z_2_3.png|right|frame|Sliding–Window with&nbsp; $G = 4$&nbsp; and&nbsp; $G = 5$<br>(valid for "LZ77")]]
In der&nbsp; [[Aufgaben:2.3_Zur_LZ78-Komprimierung|Aufgabe 2.3]]&nbsp; sollten Sie&nbsp; <b>BARBARA&ndash;BAR</b>&nbsp; (String der Länge&nbsp; $11$, vier verschiedene Zeichen)&nbsp; mit dem LZ78&ndash;Algorithmus komprimieren.&nbsp;  
+
In &nbsp; [[Aufgaben:2.3_Zur_LZ78-Komprimierung|task 2.3]]&nbsp;, you were supposed to compress&nbsp; <b>BARBARA&ndash;BAR</b>&nbsp; (string of length&nbsp; $11$, four different characters)&nbsp; using the LZ78 algorithm.&nbsp;  
  
In dieser Aufgabe verwenden wir den gleichen Text zur Demonstration der LZ77&ndash;Komprimierung.&nbsp; Anzumerken ist:
+
In this task we use the same text to demonstrate LZ77 compression.&nbsp; To note:
* Während beim Nachfolger "LZ78" sukzessive ein globales Wörterbuch aufgebaut wird, verwendet "LZ77"   ein lokales Wörterbuch.
+
* While the successor "LZ78" successively builds a global dictionary, "LZ77" uses a local dictionary.
* Das LZ77&ndash;Verfahren arbeitet mit einem&nbsp; <i>Sliding Window</i>, das schrittweise über den Eingabetext verschoben wird.
+
* The LZ77 method works with a&nbsp; <i>Sliding Window</i> that is gradually shifted over the input text.
* Dieses "gleitende Fenster" ist unterteilt in den&nbsp; ''Vorschaupuffer''&nbsp; (in der Grafik blau hinterlegt) und den&nbsp; ''Suchpuffer''&nbsp; (rote Hinterlegung).&nbsp; Beide Puffer haben je eine Größe von&nbsp; $G$&nbsp; Speicherplätzen.
+
* This "sliding window" is divided into the&nbsp; ''preview buffer''&nbsp; (highlighted in blue in the graphic) and the&nbsp; ''search buffer''&nbsp; (highlighted in red).&nbsp; Both buffers have a size of&nbsp; $G$&nbsp; memory locations each.
* Jeder Codierschritt&nbsp; $i$&nbsp; wird durch ein Zahlentriple&nbsp; $(P,\ L,\ Z)$&nbsp; charakterisiert.&nbsp; Hierbei sind&nbsp; $P$&nbsp; und&nbsp; $L$&nbsp; Integergrößen und&nbsp; $Z$&nbsp; ein Character.&nbsp; Übertragen werden die Binärdarstellungen von&nbsp; $P$,&nbsp; $L$&nbsp; und&nbsp; $Z$.
+
* Each coding step&nbsp; $i$&nbsp; is characterised by a number triple&nbsp; $(P,\ L,\ Z)$&nbsp;.&nbsp; Here,&nbsp; $P$&nbsp; and&nbsp; $L$&nbsp; are integers and&nbsp; $Z$&nbsp; is a character.&nbsp; The binary representations of&nbsp; $P$,&nbsp; $L$&nbsp; and&nbsp; $Z$.
* Nach der Übertragung wird das&nbsp; <i>Sliding Window</i>&nbsp; um eine oder mehrere Positionen nach rechts verschoben und es beginnt der nächste Codierschritt&nbsp; $i + 1$.
+
* After transmission, the&nbsp; <i>Sliding Window</i>&nbsp; is shifted one or more positions to the right and the next coding step&nbsp; $i + 1$ begins.
  
  
Die obere Grafik zeigt die Anfangsbelegung mit der Puffergröße&nbsp; $G = 4$&nbsp; zu den Zeitpunkten&nbsp; $i = 1$&nbsp; sowie&nbsp; $i = 4$.  
+
The upper diagram shows the initial allocation with the buffer size&nbsp; $G = 4$&nbsp; at the times&nbsp; $i = 1$&nbsp; as well as&nbsp; $i = 4$.  
*Zum Zeitpunkt&nbsp; $i = 1$&nbsp; ist der Suchpuffer leer, so dass die Coderausgabe&nbsp; $(0, 0,$ <b>B</b>$)$&nbsp; lautet.  
+
*At time&nbsp; $i = 1$&nbsp;, the search buffer is empty, so the coder output is&nbsp; $(0, 0,$ <b>B</b>$)$&nbsp;.  
*Nach der Verschiebung um eine Position beinhaltet der Suchpuffer ein&nbsp; <b>B</b>, aber keinen String, der mit&nbsp; <b>A</b>&nbsp; anfängt.&nbsp; Das zweite Zahlentriple ist somit&nbsp; $(0, 0,$ <b>A</b>$)$.  
+
*After the shift by one position, the search buffer contains a&nbsp; <b>B</b>, but no string that begins with&nbsp; <b>A</b>&nbsp;.&nbsp; The second number triple is therefore&nbsp; $(0, 0,$ <b>A</b>$)$.  
*Die Ausgabe für&nbsp; $i = 3$&nbsp; lautet&nbsp; $(0, 0,$ <b>R</b>$)$, da im Suchpuffer auch jetzt keine mit&nbsp; <b>R</b>&nbsp; beginnende Zeichenfolge zu finden ist.
+
*The output for&nbsp; $i = 3$&nbsp; is&nbsp; $(0, 0,$ <b>R</b>$)$, since there is no string beginning with&nbsp; <b>R</b>&nbsp; in the search buffer even now.
  
  
Die Momentaufnahme zum Zeitpunkt&nbsp; $i = 4$&nbsp; ist ebenfalls in der Grafik angegeben.&nbsp; Gesucht ist nun die Zeichenfolge im Suchpuffer, die mit dem Vorschautext&nbsp; <b>BARA</b>&nbsp; am besten übereinstimmt.&nbsp; Übertragen wird wieder ein Zahlentriple&nbsp; $(P,\ L,\ Z)$, aber nun mit folgender Bedeutung:
+
The snapshot at time&nbsp; $i = 4$&nbsp; is also shown in the graphic.&nbsp; The search is now for the character string in the search buffer that best matches the preview text&nbsp; <b>BARA</b>&nbsp;.&nbsp; A number triple&nbsp; $(P,\ L,\ Z)$, is transmitted again, but now with the following meaning:
* $P$&nbsp; gibt die Position im (roten) Suchpuffer an, bei der die Übereinstimmung beginnt.&nbsp; Die&nbsp; $P$&ndash;Werte der einzelnen Speicherplätze kann man der Grafik entnehmen.
+
* $P$&nbsp; indicates the position in the (red) search buffer where the match starts.&nbsp; The&nbsp; $P$& values of the individual memory locations can be seen in the graphic.
* $L$&nbsp; bezeichnet die Anzahl der Zeichen im Suchpuffer, die beginnend bei&nbsp; $P$&nbsp; mit dem aktuellen String im Vorschaupuffer übereinstimmen.
+
* $L$&nbsp; denotes the number of characters in the search buffer that, starting at&nbsp; $P$&nbsp; , match the current string in the preview buffer.
* $Z$&nbsp; bezeichnet schließlich das erste Zeichen im Vorschaupuffer, das sich vom gefundenen Übereinstimmungs&ndash;String  im Suchpuffer unterscheidet.
+
* Finally $Z$&nbsp; denotes the first character in the preview buffer that differs from the matching string found in the search buffer.
  
  
Je größer der LZ77&ndash;Parameter&nbsp; $G$&nbsp; ist, um so leichter findet man eine möglichst lange Übereinstimmung.&nbsp; In der Teilaufgabe&nbsp; '''(4)'''&nbsp; werden Sie feststellen, dass die LZ77&ndash;Codierung mit&nbsp; $G = 5$&nbsp; ein besseres Ergebnis liefert als diejenige mit&nbsp; $G = 4$.
+
The larger the LZ77 parameter&nbsp; $G$&nbsp;, the easier it is to find the longest possible match.&nbsp; In subtask&nbsp; '''(4)'''&nbsp; you will notice that the LZ77 encoding with&nbsp; $G = 5$&nbsp; gives a better result than the one with&nbsp; $G = 4$.
* Aufgrund der späteren Binärdarstellung von&nbsp; $P$ wird&nbsp; man allerdings&nbsp; $G$&nbsp; stets als Zweierpotenz wählen, so dass&nbsp; $G$&nbsp; mit&nbsp; $\log_2 \ P$&nbsp; Bit darstellbar ist&nbsp; $(G = 8$ &nbsp; &#8658; &nbsp; dreistellige Binärzahl&nbsp; $P)$.  
+
* However, because of the later binary representation of&nbsp; $P$,&nbsp; , one will always choose&nbsp; $G$&nbsp; as a power of two, so that&nbsp; $G$&nbsp; can be represented with&nbsp; $\log_2 \ P$&nbsp; bits&nbsp; $(G = 8$ &nbsp; &#8658; &nbsp; three-digit binary number&nbsp; $P)$.  
*Das heißt, ein&nbsp; <i>Sliding Window</i>&nbsp; mit&nbsp; $G = 5$&nbsp; hat eher einen geringen Praxisbezug.
+
*This means that a&nbsp; <i>Sliding Window</i>&nbsp; with&nbsp; $G = 5$&nbsp; has rather little practical relevance.
  
  
  
  
 
+
''Hints:''  
''Hinweise:''  
+
*The task belongs to the chapter&nbsp; [[Information_Theory/Komprimierung_nach_Lempel,_Ziv_und_Welch|Compression according to Lempel, Ziv and Welch]].
*Die Aufgabe gehört zum  Kapitel&nbsp; [[Information_Theory/Komprimierung_nach_Lempel,_Ziv_und_Welch|Komprimierung nach Lempel, Ziv und Welch]].
+
*In particular, reference is made to the page&nbsp; [[Information_Theory/Komprimierung_nach_Lempel,_Ziv_und_Welch#LZ77_-_the_basic_form_of_the_Lempel-Ziv_algorithms|LZ77 &ndash; the basic form of the Lempel-Ziv algorithms]]&nbsp;.
*Insbesondere wird auf die Seite&nbsp; [[Information_Theory/Komprimierung_nach_Lempel,_Ziv_und_Welch#LZ77_.E2.80.93_die_Grundform_der_Lempel.E2.80.93Ziv.E2.80.93Algorithmen|LZ77 &ndash; die Grundform der Lempel-Ziv-Algorithmen]]&nbsp; Bezug genommen.
+
*The original literature&nbsp; [LZ77]&nbsp; on this method is: <br>Ziv, J.; Lempel, A.: ''A Universal Algorithm for Sequential Data Compression.'' In: IEEE Transactions on Information Theory, no. 3, vol. 23, 1977, p. 337–343.
*Die Originalliteratur&nbsp; [LZ77]&nbsp; zu diesem Verfahren lautet: <br>Ziv, J.; Lempel, A.: ''A Universal Algorithm for Sequential Data Compression.'' In: IEEE Transactions on Information Theory, no. 3, vol. 23, 1977, p. 337–343.
 
 
   
 
   
*Die&nbsp; [[Aufgaben:2.3_Zur_LZ78-Komprimierung|Aufgabe 2.3]]&nbsp; sowie die&nbsp; [[Aufgaben:Aufgabe_2.4:_Zum_LZW-Algorithmus|Aufgabe 2.4]]&nbsp; behandeln andere Lempel&ndash;Ziv-Verfahren in ähnlicher Weise.
+
*&nbsp; [[Aufgaben:2.3_Zur_LZ78-Komprimierung|Task 2.3]]&nbsp; and&nbsp; [[Aufgaben:Aufgabe_2.4:_Zum_LZW-Algorithmus|task 2.4]]&nbsp; deal with other Lempel&ndash;Ziv methods in a similar way.
 +
 
  
  
===Fragebogen===
+
===Questions===
  
 
<quiz display=simple>
 
<quiz display=simple>
{Wie lautet die LZ77&ndash;Ausgabe mit&nbsp; $G = 4$&nbsp; beim Schritt&nbsp; $i = 4$?
+
{What is the LZ77 output with&nbsp; $G = 4$&nbsp; at step&nbsp; $i = 4$?
 
|type="()"}
 
|type="()"}
 
- $(0, 0,$ <b>B</b>$)$,
 
- $(0, 0,$ <b>B</b>$)$,
Line 52: Line 52:
  
  
{Welche Aussage gilt für die gleiche Puffergröße&nbsp; $G = 4$&nbsp; beim Schritt&nbsp; $i = 5$?
+
{Which statement is true for the same buffer size&nbsp; $G = 4$&nbsp; at step&nbsp; $i = 5$?
 
|type="[]"}
 
|type="[]"}
+ Im Suchpuffer steht&nbsp; <b>BARA</b>.
+
+ The search buffer contains&nbsp; <b>BARA</b>.
+ Im Vorschaupuffer steht&nbsp; <b>&ndash;BAR</b>.
+
+ The preview buffer says&nbsp; <b>&ndash;BAR</b>.
- Die Ausgabe lautet&nbsp; $(0, 0,$ <b>A</b>$)$.
+
- The output is&nbsp; $(0, 0,$ <b>A</b>$)$.
  
  
{Nach welchem Schritt&nbsp; $i_{\rm Ende}$&nbsp; ist die Codierung mit&nbsp; $G = 4$&nbsp; beendet?
+
{After which step&nbsp; $i_{\rm Ende}$&nbsp; is the coding with&nbsp; $G = 4$&nbsp; finished?
 
|type="{}"}
 
|type="{}"}
 
$i_{\rm Ende} \ = \ $ { 9 }  
 
$i_{\rm Ende} \ = \ $ { 9 }  
  
  
{Nun gelte&nbsp; $G = 5$.&nbsp; Nach welchem Schritt&nbsp; $i_{\rm Ende}$&nbsp; ist nun die Codierung  beendet?
+
{Now let&nbsp; $G = 5$.&nbsp; After which step&nbsp; $i_{\rm Ende}$&nbsp; is the coding finished?
 
|type="{}"}
 
|type="{}"}
 
$i_{\rm Ende} \ = \ $ { 6 }  
 
$i_{\rm Ende} \ = \ $ { 6 }  
  
  
{Welche Vorteile hat LZ78 gegenüber LZ77 bei "sehr großen" Dateien?
+
{What advantages does LZ78 have over LZ77 for "very large" files?
 
|type="[]"}
 
|type="[]"}
+ Man findet häufiger bereits abgelegte Phrasen im Wörterbuch.
+
+ One finds more often already stored phrases in the dictionary.
- Pro Codierschritt müssen weniger Bit übertragen werden.
+
- Fewer bits have to be transferred per coding step.
  
  
Line 78: Line 78:
 
</quiz>
 
</quiz>
  
===Musterlösung===
+
===Solution===
 
{{ML-Kopf}}
 
{{ML-Kopf}}
 
[[File:P_ID2438__Inf_Z_2_3c.png|right|frame|Beispiel zum LZ77–Algorithmus mit&nbsp; $G = 4$]]
 
[[File:P_ID2438__Inf_Z_2_3c.png|right|frame|Beispiel zum LZ77–Algorithmus mit&nbsp; $G = 4$]]

Revision as of 16:06, 9 July 2021

Sliding–Window with  $G = 4$  and  $G = 5$
(valid for "LZ77")

In   task 2.3 , you were supposed to compress  BARBARA–BAR  (string of length  $11$, four different characters)  using the LZ78 algorithm. 

In this task we use the same text to demonstrate LZ77 compression.  To note:

  • While the successor "LZ78" successively builds a global dictionary, "LZ77" uses a local dictionary.
  • The LZ77 method works with a  Sliding Window that is gradually shifted over the input text.
  • This "sliding window" is divided into the  preview buffer  (highlighted in blue in the graphic) and the  search buffer  (highlighted in red).  Both buffers have a size of  $G$  memory locations each.
  • Each coding step  $i$  is characterised by a number triple  $(P,\ L,\ Z)$ .  Here,  $P$  and  $L$  are integers and  $Z$  is a character.  The binary representations of  $P$,  $L$  and  $Z$.
  • After transmission, the  Sliding Window  is shifted one or more positions to the right and the next coding step  $i + 1$ begins.


The upper diagram shows the initial allocation with the buffer size  $G = 4$  at the times  $i = 1$  as well as  $i = 4$.

  • At time  $i = 1$ , the search buffer is empty, so the coder output is  $(0, 0,$ B$)$ .
  • After the shift by one position, the search buffer contains a  B, but no string that begins with  A .  The second number triple is therefore  $(0, 0,$ A$)$.
  • The output for  $i = 3$  is  $(0, 0,$ R$)$, since there is no string beginning with  R  in the search buffer even now.


The snapshot at time  $i = 4$  is also shown in the graphic.  The search is now for the character string in the search buffer that best matches the preview text  BARA .  A number triple  $(P,\ L,\ Z)$, is transmitted again, but now with the following meaning:

  • $P$  indicates the position in the (red) search buffer where the match starts.  The  $P$& values of the individual memory locations can be seen in the graphic.
  • $L$  denotes the number of characters in the search buffer that, starting at  $P$  , match the current string in the preview buffer.
  • Finally $Z$  denotes the first character in the preview buffer that differs from the matching string found in the search buffer.


The larger the LZ77 parameter  $G$ , the easier it is to find the longest possible match.  In subtask  (4)  you will notice that the LZ77 encoding with  $G = 5$  gives a better result than the one with  $G = 4$.

  • However, because of the later binary representation of  $P$,  , one will always choose  $G$  as a power of two, so that  $G$  can be represented with  $\log_2 \ P$  bits  $(G = 8$   ⇒   three-digit binary number  $P)$.
  • This means that a  Sliding Window  with  $G = 5$  has rather little practical relevance.



Hints:

  •   Task 2.3  and  task 2.4  deal with other Lempel–Ziv methods in a similar way.


Questions

1

What is the LZ77 output with  $G = 4$  at step  $i = 4$?

$(0, 0,$ B$)$,
$(2, 1,$ A$)$,
$(2, 3,$ A$)$.

2

Which statement is true for the same buffer size  $G = 4$  at step  $i = 5$?

The search buffer contains  BARA.
The preview buffer says  –BAR.
The output is  $(0, 0,$ A$)$.

3

After which step  $i_{\rm Ende}$  is the coding with  $G = 4$  finished?

$i_{\rm Ende} \ = \ $

4

Now let  $G = 5$.  After which step  $i_{\rm Ende}$  is the coding finished?

$i_{\rm Ende} \ = \ $

5

What advantages does LZ78 have over LZ77 for "very large" files?

One finds more often already stored phrases in the dictionary.
Fewer bits have to be transferred per coding step.


Solution

Beispiel zum LZ77–Algorithmus mit  $G = 4$

(1)  Richtig ist der Lösungsvorschlag 3.

  • Im Vorschaupuffer steht zum betrachteten Zeitpunkt  $i = 4$  die Zeichenfolge  BARA.
  • Im Suchpuffer steht in den letzten drei Stellen  BAR:
$$P = 2\hspace{0.05cm},\hspace{0.2cm}L = 3\hspace{0.05cm},\hspace{0.2cm}Z = \boldsymbol{\rm A}\hspace{0.05cm}.$$


(2)  Richtig sind die beiden ersten Lösungsvorschläge:

  • Der Bindestrich findet sich zum Zeitpunkt  $i = 5$  nicht im Suchpuffer.
  • Ausgegeben wird  $(0, 0,$ $)$.


(3)  Die obere Grafik zeigt das  Sliding–Window  und die Coderausgabe zu den Zeiten  $i>5$.

  • Nach  $i = 9$  Codierschritten ist der Codiervorgang unter Berücksichtigung von  eof  beendet   ⇒   $\underline{i_{\rm Ende} = 9}$.


Beispiel zum LZ77–Algorithmus mit  $G = 5$

(4)  Bei größerer Puffergröße  $(G = 5$  anstelle von  $G = 4)$  ist die Codierung schon nach dem 6. Codierschritt abgeschlossen   ⇒   $\underline{i_{\rm Ende} = 6}$.

  • Ein Vergleich der beiden Grafiken zeigt, dass sich für  $G = 5$  gegenüber  $G = 4$  bis einschließlich  $i = 5$  nichts ändert.
  • Aufgrund des größeren Puffers lässt sich aber nun  BAR  gemeinsam mit  eof  (end-of-file) in einem einzigen Schritt codieren, während mit  $G = 4$  hierfür vier Schritte notwendig waren.


(5)  Richtig ist nur die Aussage 1.

  • Ein Nachteil von LZ77 ist das lokale Wörterbuch.  Eigentlich schon bekannte Phrasen können nicht für die Datenkomprimierung verwendet werden, wenn sie mehr als  $G$  Zeichen vorher im Text aufgetreten sind.  Dagegen sind bei LZ78 alle Phrasen im globalen Wörterbuch abgelegt.
  • Richtig ist zwar, dass bei LZ78 nur Pärchen  $(I, \ Z)$  übertragen werden müssen, während bei LZ77 jeder Codierschritt durch ein Triple  $(P, \ L, \ Z)$  gekennzeichnet ist.  Das bedeutet aber noch nicht, dass pro Codierschritt auch weniger Bit übertragen werden müssen.
  • Betrachten wir beispielhaft die Puffergröße  $G = 8$.  Bei LZ77 muss man dann  $P$  mit drei Bit und  $L$  mit vier Bit dargestellen.  Beachten Sie bitte, dass die gefundene Übereinstimmung zwischen Vorschaupuffer und Suchpuffer auch im Vorschaupuffer enden darf.
  • Das neue Zeichen  $Z$  benötigt bei LZ78 genau die gleiche Bitanzahl wie bei LZ77 (nämlich zwei Bit), wenn man wie hier vom Symbolumfang  $M = 4$  ausgeht.
  • Die Aussage 2 wäre nur dann richtig, wenn  $N_{\rm I}$  kleiner wäre als  $N_{\rm P}+ N_{\rm L}$, beispielsweise  $N_{\rm I} = 6$. Das würde aber bedeuten, dass man die Wörterbuchgröße auf  $I = 2^6 = 64$  begrenzen müsste.  Dies reicht für große Dateien nicht aus.
  • Unsere Überschlagsrechnung basiert allerdings auf einer einheitlichen Bitanzahl für den Index  $I$.  Mit variabler Bitanzahl für den Index kann man etliche Bit einsparen, indem man  $I$  nur mit so vielen Bit überträgt, wie es für den Codierschritt  $i$  erforderlich ist.
  • Prinzipiell ändert das aber nichts an der Beschränkung der Wörterbuchgröße, was bei großen Dateien stets zu Problemen führen wird.