Troubleshooting Index Error 7 in Decoding

In summary: So the decoder starts with the alphabet: dictionary[3] = {1:A} dictionary[4] = {2:B} J = 5decoder: input if input == J push last_first decode dictionary[prior input index] onto stack d[J] = {prior input index : last_first} last_first = first_byte[J] = first byte of stack output the stack J++ else decode dictionary[input] onto stack dictionary[J] = {prior input index : first_byte[input]} last_first = first_byte[J] = first byte of stack output the stack J++Encoding: input
  • #1
shivajikobardan
674
54
Homework Statement
LZW decoding-:There is no entry for index 7 in the dictionary while decoding, how do I fix this issue?
Relevant Equations
N/A
MSlVXdQfoEojqK2wHcAh-yp0zwNXv3BXb0bzrxk4Ys0JdZB1k2.png


sFS9ul_YJC-qN6tdNfY7SfOky9XEn3SJ-TSxkMDP4acvuaXT0g.png


Source of my knowledge-:

Encoding-:


Decoding-:


Other references-:


Question-: There is no entry for index 7 in the dictionary while decoding, how do I fix this issue?
 
Physics news on Phys.org
  • #2
The videos don't do a good job of explaining a LZW dictionary. Link to Wiki article:

https://en.wikipedia.org/wiki/Lempel–Ziv–Welch

Typically elements are not stored in the dictionary. For the OP's question the elements have values 1 (A) and 2(B). To keep things simple assume that elements and indexes are bytes with range 1 to 255.

The actual dictionary starts at index 3, since elements are usually not stored in the dictionary. Each dictionary entry consists of a prior index and an element, unless the prior index is less than 3, in which case its the first byte of a string. Each prior index represents a string. Note that decoding a dictionary entry will retrieve bytes in reverse order.

When the decoder encounters a code not yet in the dictionary, the last character for the new code equals the first character from the previous output. The decoder needs to save the first character of each output to implement this.

Code:
                 dictonary
rcvd decd     indx  code char string

   2    B
   1    A        3     2    1   BA
   1    A        4     1    1   AA
   3   BA        5     1    2   AB
   2    B        6     3    2  BAB
   7   BB        7     2    2   BB    new:  prior code = 2, prior first character = B = 2
   4   AA        8     7    1  BBA
   7   BB        9     4    2  AAB
   2    B       10     7    2  BBB

For a better example, string to be encoded: ABABABABABAB
Code:
    input   search  dictionary    output
    A        -
    B       A:B     d[3]=A:B       A
    A       B:A     d[4]=B:A       B
    B       A:B       3
    A       3:A     d[5]=3:A       3
    B       A:B       3
    A       3:A       5
    B       5:B     d[6]=5:B       5
    A       B:A       4
    B       4:B     d[7]=4:B       4
    A       B:A       4
    B       4:B       7
   eof                             7

Decoding:
Code:
input   dictionary    output
   A                  A
   B   d[3]=A:B=AB    B
   3   d[4]=B:A=BA    AB
   5   d[5]=3:A=ABA   ABA    last output first character = A
   4   d[6]=5:B=ABAB  BA
   7   d[7]=4:B=BAB   BAB    last output first character = B
 eof
 
Last edited:
  • #3
rcgldr said:
The videos don't do a good job of explaining a LZW dictionary. Link to Wiki article:

https://en.wikipedia.org/wiki/Lempel–Ziv–Welch

Typically elements are not stored in the dictionary. For the OP's question the elements have values 1 (A) and 2(B). To keep things simple assume that elements and indexes are bytes with range 1 to 255.

The actual dictionary starts at index 3, since elements are usually not stored in the dictionary. Each dictionary entry consists of a prior index and an element, unless the prior index is less than 3, in which case its the first byte of a string. Each prior index represents a string. Note that decoding a dictionary entry will retrieve bytes in reverse order.

When the decoder encounters a code not yet in the dictionary, the last character for the new code equals the first character from the previous output. The decoder needs to save the first character of each output to implement this.

Code:
                 dictonary
rcvd decd     indx  code char string

   2    B
   1    A        3     2    1   BA
   1    A        4     1    1   AA
   3   BA        5     1    2   AB
   2    B        6     3    2  BAB
   7   BB        7     2    2   BB    new:  prior code = 2, prior first character = B = 2
   4   AA        8     7    1  BBA
   7   BB        9     4    2  AAB
   2    B       10     7    2  BBB

For a better example, string to be encoded: ABABABABABAB
Code:
    input   search  dictionary    output
    A        -
    B       A:B     d[3]=A:B       A
    A       B:A     d[4]=B:A       B
    B       A:B       3
    A       3:A     d[5]=3:A       3
    B       A:B       3
    A       3:A       5
    B       5:B     d[6]=5:B       5
    A       B:A       4
    B       4:B     d[7]=4:B       4
    A       B:A       4
    B       4:B       7
   eof                             7

Decoding:
Code:
input   dictionary    output
   A                  A
   B   d[3]=A:B=AB    B
   3   d[4]=B:A=BA    AB
   5   d[5]=3:A=ABA   ABA    last output first character = A
   4   d[6]=5:B=ABAB  BA
   7   d[7]=4:B=BAB   BAB    last output first character = B
 eof
thank you for writing this long answer for me but i could not get most things..
 
Last edited:
  • #4
shivajikobardan said:
thank you for writing this long answer for me but i could not get most things..
Are you trying to create a program or just writing out the steps as you posted in your question?

Were you able to understand my encoding example?

Assuming a character is a byte, then each dictionary entry = {prior index : byte}.

Using my example, consider decoding d[6] onto a stack:
Code:
    d[6] = {5:B}      push B
    d[5] = {3:A}      push A
    d[3] = {A:B}      push B
    d[A] = {A}        push A
                      string = stack = ABAB

For the decoder. two arrays are used, one for dictionary entries, one for the first bytes of strings for those dictionary entries, a stack to store the bytes of a string in reverse order, a last_first variable that holds one byte, and J, an index into the dictionary, initialized to 3. The dictionary is initialized to the alphabet: d[A] = {A:}, d = {B:}. Instead of actually using dictionary entries for the alphabet, the code can just use the index if index is less than 3.

The first input is just a byte:
Code:
    last_first = input byte
    output input byte
After the first input, the dictionary is built on the fly and decoded to generate strings to output:
Code:
    decode dictionary[current input index] onto stack
    dictionary[J] = {prior input index : first_byte[current input index]}
    last_first = first_byte[J] = first byte of stack
    output the stack
    J++
If input == J, it's a special case:
Code:
    push last_first
    decode dictionary[prior input index] onto stack
    d[J] = {prior input index : last_first}
    last_first = first_byte[J] = first byte of stack
    output the stack
    J++
 
Last edited:

1. What is Index Error 7 in Decoding?

Index Error 7 in Decoding is a common error that occurs when trying to access an index or position in a sequence that does not exist. This can happen when decoding data or trying to access elements in a list or string.

2. What causes Index Error 7 in Decoding?

Index Error 7 in Decoding is typically caused by trying to access an index that is out of range. This can happen if the index is larger than the length of the sequence or if the sequence is empty.

3. How can I fix Index Error 7 in Decoding?

To fix Index Error 7 in Decoding, you will need to check the index you are trying to access and make sure it is within the range of the sequence. You can also check if the sequence is empty before trying to access an index. Additionally, you can use try and except statements to handle the error and prevent it from crashing your program.

4. Are there any common mistakes that can lead to Index Error 7 in Decoding?

Yes, some common mistakes that can lead to Index Error 7 in Decoding include using the wrong index or trying to access an index that does not exist in the sequence. It is important to double check your code and make sure you are using the correct index when accessing elements in a sequence.

5. How can I prevent Index Error 7 in Decoding from happening?

To prevent Index Error 7 in Decoding, it is important to carefully check and validate the indices you are using when accessing elements in a sequence. You can also use error handling techniques, such as try and except statements, to handle the error and prevent it from crashing your program.

Similar threads

Replies
1
Views
922
  • Engineering and Comp Sci Homework Help
Replies
1
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
999
  • Engineering and Comp Sci Homework Help
Replies
8
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Programming and Computer Science
Replies
0
Views
508
  • Engineering and Comp Sci Homework Help
Replies
10
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
920
  • Engineering and Comp Sci Homework Help
Replies
2
Views
825
Back
Top