Why is this code uniquely decodable?Designing a prefix code for integers?Name of a type of code similar to...

Can the Count of Monte Cristo's calculation of poison dosage be explained?

Crystal compensation for temp and voltage

Meaning of すきっとした

Do authors have to be politically correct in article-writing?

Charged enclosed by the sphere

Why is working on the same position for more than 15 years not a red flag?

Quenching swords in dragon blood; why?

Could quantum mechanics be necessary to analyze some biology scenarios?

Dilemma of explaining to interviewer that he is the reason for declining second interview

Finding the number of integers that are a square and a cube at the same time

Is the theory of the category of topological spaces computable?

Can a hotel cancel a confirmed reservation?

Can I retract my name from an already published manuscript?

Can a person refuse a presidential pardon?

Can the Assuming function be used with ContourPlot or DensityPlot?

It took me a lot of time to make this, pls like. (YouTube Comments #1)

Why didn't Eru and/or the Valar intervene when Sauron corrupted Númenor?

Is it a fallacy if someone claims they need an explanation for every word of your argument to the point where they don't understand common terms?

What are these green text/line displays shown during the livestream of Crew Dragon's approach to dock with the ISS?

How to use a mathematical expression as xticklable

ip vs ifconfig commands pros and cons

Why do neural networks need so many training examples to perform?

How to acknowledge an embarrassing job interview, now that I work directly with the interviewer?

When does coming up with an idea constitute sufficient contribution for authorship?



Why is this code uniquely decodable?


Designing a prefix code for integers?Name of a type of code similar to block codesIs Morse Code binary, ternary or quinary?What is bi-binary code?Is there any practical trick to mentally count in Gray code?Error correcting permutation codeHow to uniquely encode a vector of non-increasing positive integersImplementing a decoding algorithm for directed figure codesOptional prefix code for the naturalsError detection code supporting arithmetic













11












$begingroup$


Source alphabet: ${a, b, c, d, e, f}$



Code alphabet: ${0, 1}$




  • $acolon 0101$

  • $bcolon 1001$

  • $ccolon 10$

  • $dcolon 000$

  • $ecolon 11$

  • $fcolon 100$


I thought that for a code to be uniquely decodable, it had to be prefix-free.
But in this code, the codeword $c$ is the prefix of codeword $f$ for example, so it is not prefix-free. However my textbook tells me that its reverse is prefix free (I don't understand this), and therefore it is uniquely decodable. Can someone explain what this means, or why it is uniquely decodable? I know it satisfies Kraft's inequality, but that is only a necessary condition, not a sufficient condition.










share|cite|improve this question









New contributor




2000mroliver is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.







$endgroup$








  • 5




    $begingroup$
    Prefix-free implies uniquely decodable, but that it is not an "if and only if" statement. See, for example, here.
    $endgroup$
    – dkaeae
    17 hours ago












  • $begingroup$
    Okay I see, but my text book says this: Code A is uniquely decodable since its reverse it is prefixfree, so uniquely decodable Do you understand what they mean by its reverse?
    $endgroup$
    – 2000mroliver
    17 hours ago






  • 1




    $begingroup$
    Probably simply the code obtained by reversing all codewords.
    $endgroup$
    – dkaeae
    17 hours ago










  • $begingroup$
    and why does that imply uniquely decodable, I don't get it
    $endgroup$
    – 2000mroliver
    17 hours ago
















11












$begingroup$


Source alphabet: ${a, b, c, d, e, f}$



Code alphabet: ${0, 1}$




  • $acolon 0101$

  • $bcolon 1001$

  • $ccolon 10$

  • $dcolon 000$

  • $ecolon 11$

  • $fcolon 100$


I thought that for a code to be uniquely decodable, it had to be prefix-free.
But in this code, the codeword $c$ is the prefix of codeword $f$ for example, so it is not prefix-free. However my textbook tells me that its reverse is prefix free (I don't understand this), and therefore it is uniquely decodable. Can someone explain what this means, or why it is uniquely decodable? I know it satisfies Kraft's inequality, but that is only a necessary condition, not a sufficient condition.










share|cite|improve this question









New contributor




2000mroliver is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.







$endgroup$








  • 5




    $begingroup$
    Prefix-free implies uniquely decodable, but that it is not an "if and only if" statement. See, for example, here.
    $endgroup$
    – dkaeae
    17 hours ago












  • $begingroup$
    Okay I see, but my text book says this: Code A is uniquely decodable since its reverse it is prefixfree, so uniquely decodable Do you understand what they mean by its reverse?
    $endgroup$
    – 2000mroliver
    17 hours ago






  • 1




    $begingroup$
    Probably simply the code obtained by reversing all codewords.
    $endgroup$
    – dkaeae
    17 hours ago










  • $begingroup$
    and why does that imply uniquely decodable, I don't get it
    $endgroup$
    – 2000mroliver
    17 hours ago














11












11








11





$begingroup$


Source alphabet: ${a, b, c, d, e, f}$



Code alphabet: ${0, 1}$




  • $acolon 0101$

  • $bcolon 1001$

  • $ccolon 10$

  • $dcolon 000$

  • $ecolon 11$

  • $fcolon 100$


I thought that for a code to be uniquely decodable, it had to be prefix-free.
But in this code, the codeword $c$ is the prefix of codeword $f$ for example, so it is not prefix-free. However my textbook tells me that its reverse is prefix free (I don't understand this), and therefore it is uniquely decodable. Can someone explain what this means, or why it is uniquely decodable? I know it satisfies Kraft's inequality, but that is only a necessary condition, not a sufficient condition.










share|cite|improve this question









New contributor




2000mroliver is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.







$endgroup$




Source alphabet: ${a, b, c, d, e, f}$



Code alphabet: ${0, 1}$




  • $acolon 0101$

  • $bcolon 1001$

  • $ccolon 10$

  • $dcolon 000$

  • $ecolon 11$

  • $fcolon 100$


I thought that for a code to be uniquely decodable, it had to be prefix-free.
But in this code, the codeword $c$ is the prefix of codeword $f$ for example, so it is not prefix-free. However my textbook tells me that its reverse is prefix free (I don't understand this), and therefore it is uniquely decodable. Can someone explain what this means, or why it is uniquely decodable? I know it satisfies Kraft's inequality, but that is only a necessary condition, not a sufficient condition.







encoding-scheme






share|cite|improve this question









New contributor




2000mroliver is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|cite|improve this question









New contributor




2000mroliver is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|cite|improve this question




share|cite|improve this question








edited 9 hours ago









Yuval Filmus

194k14182347




194k14182347






New contributor




2000mroliver is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked 17 hours ago









2000mroliver2000mroliver

583




583




New contributor




2000mroliver is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





2000mroliver is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






2000mroliver is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.








  • 5




    $begingroup$
    Prefix-free implies uniquely decodable, but that it is not an "if and only if" statement. See, for example, here.
    $endgroup$
    – dkaeae
    17 hours ago












  • $begingroup$
    Okay I see, but my text book says this: Code A is uniquely decodable since its reverse it is prefixfree, so uniquely decodable Do you understand what they mean by its reverse?
    $endgroup$
    – 2000mroliver
    17 hours ago






  • 1




    $begingroup$
    Probably simply the code obtained by reversing all codewords.
    $endgroup$
    – dkaeae
    17 hours ago










  • $begingroup$
    and why does that imply uniquely decodable, I don't get it
    $endgroup$
    – 2000mroliver
    17 hours ago














  • 5




    $begingroup$
    Prefix-free implies uniquely decodable, but that it is not an "if and only if" statement. See, for example, here.
    $endgroup$
    – dkaeae
    17 hours ago












  • $begingroup$
    Okay I see, but my text book says this: Code A is uniquely decodable since its reverse it is prefixfree, so uniquely decodable Do you understand what they mean by its reverse?
    $endgroup$
    – 2000mroliver
    17 hours ago






  • 1




    $begingroup$
    Probably simply the code obtained by reversing all codewords.
    $endgroup$
    – dkaeae
    17 hours ago










  • $begingroup$
    and why does that imply uniquely decodable, I don't get it
    $endgroup$
    – 2000mroliver
    17 hours ago








5




5




$begingroup$
Prefix-free implies uniquely decodable, but that it is not an "if and only if" statement. See, for example, here.
$endgroup$
– dkaeae
17 hours ago






$begingroup$
Prefix-free implies uniquely decodable, but that it is not an "if and only if" statement. See, for example, here.
$endgroup$
– dkaeae
17 hours ago














$begingroup$
Okay I see, but my text book says this: Code A is uniquely decodable since its reverse it is prefixfree, so uniquely decodable Do you understand what they mean by its reverse?
$endgroup$
– 2000mroliver
17 hours ago




$begingroup$
Okay I see, but my text book says this: Code A is uniquely decodable since its reverse it is prefixfree, so uniquely decodable Do you understand what they mean by its reverse?
$endgroup$
– 2000mroliver
17 hours ago




1




1




$begingroup$
Probably simply the code obtained by reversing all codewords.
$endgroup$
– dkaeae
17 hours ago




$begingroup$
Probably simply the code obtained by reversing all codewords.
$endgroup$
– dkaeae
17 hours ago












$begingroup$
and why does that imply uniquely decodable, I don't get it
$endgroup$
– 2000mroliver
17 hours ago




$begingroup$
and why does that imply uniquely decodable, I don't get it
$endgroup$
– 2000mroliver
17 hours ago










3 Answers
3






active

oldest

votes


















11












$begingroup$

Your code has the property that if you reverse all codewords, then you get a prefix code. This implies that your code is uniquely decodable.



Indeed, consider any code $C = x_1,ldots,x_n$ whose reverse $C^R := x_1^R,ldots,x_n^R$ is uniquely decodable. I claim that $C$ is also uniquely decodable. This is because
$$
w = x_{i_1} ldots x_{i_m} text{ if and only if } w^R = x_{i_m}^R ldots x_{i_1}^R.
$$

In words, decompositions of $w$ into codewords of $C$ are in one-to-one correspondence with decompositions of $w^R$ into codewords of $C^R$. Since the latter are unique, so are the former.



Since prefix codes are uniquely decodable, it follows that the reverse of a prefix code is also uniquely decodable. This is the case in your example.



The McMillan inequality states that if $C$ is uniquely decodable then
$$
sum_{i=1}^n 2^{-|x_i|} leq 1.
$$

In other words, a uniquely decodable code satisfies Kraft's inequality. Therefore if all you're interested in is minimizing the expected codeword length, there is no reason to look beyond prefix codes.



Sam Roweis gives in his slides a nice example of a uniquely decodable code which is neither a prefix code nor the reverse of a prefix code:
$$
0,01,110.
$$

In order to show that this code is uniquely decodable, it suffices to show how to decode the first codeword of a word. If the word starts with a $1$, then the first codeword is $110$. If it is of the form $01^*$, then it must be either $0$ or $01$. Otherwise, there must be a prefix of the form $01^*0$. We now distinguish several cases:



$$
begin{array}{c|cccc}
text{prefix} & 00 & 010 & 0110 & 01110 \hline
text{codeword} & 0 & 01 & 0 & 01
end{array}
$$

Longer runs of $1$ cannot be decoded at all.






share|cite|improve this answer











$endgroup$









  • 1




    $begingroup$
    In seems that in the OP's example, we cannot decode the first codeword after a fixed amount of digits, there are infinitely many cases: 1001010101010101… can be either fcccccc… or caaa…, and we might need to wait until the end of the input to decide.
    $endgroup$
    – Bergi
    9 hours ago












  • $begingroup$
    This also happens for $1,10,00$.
    $endgroup$
    – Yuval Filmus
    9 hours ago










  • $begingroup$
    @Bergi It is always decodable for any finite amount of digits. There is always only one way to decode the encoding without any remainders. Any other attempt will end up with spare 1's or 0s. This is because the code is uniquely decodable if we read it tail first. In theory if something is uniquely decodable in one direction it makes no sense that there can be more than one solution in the other direction
    $endgroup$
    – slebetman
    7 hours ago





















1












$begingroup$

If I give you any message that you are supposed to decode, then you can do the following: Reverse the message, starting with the last bit instead of the first bit. Reverse the code words. Decode the message. Reverse the decoded string.



You can do that because after reversing the six code words, you get a prefix-free code: 1010, 1001, 01, 000, 11, 001 is prefix free.






share|cite|improve this answer









$endgroup$





















    0












    $begingroup$

    If prefix-free means what I think, the reverse of ‘a’ starts with 1, or 10, or 101, none of which is any other whole valid code.



    Therefore, if a message ends with 0101, it can only be an ‘a’ and you can apply similar logic to the preceding bit(s).



    However, what if there is no end to start from? Well, if the first bit is 1, you know it isn’t ‘a’ or ‘d’. The second bit will eliminate ‘e’ or {‘b’,’c’,’f’}. The third bit might bring it down to one choice, but if not, it is unique by the fourth bit.



    As soon as you get to a unique sequence, you restart the algorithm on the next bit.






    share|cite|improve this answer








    New contributor




    WGroleau is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.






    $endgroup$













      Your Answer





      StackExchange.ifUsing("editor", function () {
      return StackExchange.using("mathjaxEditing", function () {
      StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
      StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["$", "$"], ["\\(","\\)"]]);
      });
      });
      }, "mathjax-editing");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "419"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: false,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: null,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });






      2000mroliver is a new contributor. Be nice, and check out our Code of Conduct.










      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcs.stackexchange.com%2fquestions%2f105065%2fwhy-is-this-code-uniquely-decodable%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      11












      $begingroup$

      Your code has the property that if you reverse all codewords, then you get a prefix code. This implies that your code is uniquely decodable.



      Indeed, consider any code $C = x_1,ldots,x_n$ whose reverse $C^R := x_1^R,ldots,x_n^R$ is uniquely decodable. I claim that $C$ is also uniquely decodable. This is because
      $$
      w = x_{i_1} ldots x_{i_m} text{ if and only if } w^R = x_{i_m}^R ldots x_{i_1}^R.
      $$

      In words, decompositions of $w$ into codewords of $C$ are in one-to-one correspondence with decompositions of $w^R$ into codewords of $C^R$. Since the latter are unique, so are the former.



      Since prefix codes are uniquely decodable, it follows that the reverse of a prefix code is also uniquely decodable. This is the case in your example.



      The McMillan inequality states that if $C$ is uniquely decodable then
      $$
      sum_{i=1}^n 2^{-|x_i|} leq 1.
      $$

      In other words, a uniquely decodable code satisfies Kraft's inequality. Therefore if all you're interested in is minimizing the expected codeword length, there is no reason to look beyond prefix codes.



      Sam Roweis gives in his slides a nice example of a uniquely decodable code which is neither a prefix code nor the reverse of a prefix code:
      $$
      0,01,110.
      $$

      In order to show that this code is uniquely decodable, it suffices to show how to decode the first codeword of a word. If the word starts with a $1$, then the first codeword is $110$. If it is of the form $01^*$, then it must be either $0$ or $01$. Otherwise, there must be a prefix of the form $01^*0$. We now distinguish several cases:



      $$
      begin{array}{c|cccc}
      text{prefix} & 00 & 010 & 0110 & 01110 \hline
      text{codeword} & 0 & 01 & 0 & 01
      end{array}
      $$

      Longer runs of $1$ cannot be decoded at all.






      share|cite|improve this answer











      $endgroup$









      • 1




        $begingroup$
        In seems that in the OP's example, we cannot decode the first codeword after a fixed amount of digits, there are infinitely many cases: 1001010101010101… can be either fcccccc… or caaa…, and we might need to wait until the end of the input to decide.
        $endgroup$
        – Bergi
        9 hours ago












      • $begingroup$
        This also happens for $1,10,00$.
        $endgroup$
        – Yuval Filmus
        9 hours ago










      • $begingroup$
        @Bergi It is always decodable for any finite amount of digits. There is always only one way to decode the encoding without any remainders. Any other attempt will end up with spare 1's or 0s. This is because the code is uniquely decodable if we read it tail first. In theory if something is uniquely decodable in one direction it makes no sense that there can be more than one solution in the other direction
        $endgroup$
        – slebetman
        7 hours ago


















      11












      $begingroup$

      Your code has the property that if you reverse all codewords, then you get a prefix code. This implies that your code is uniquely decodable.



      Indeed, consider any code $C = x_1,ldots,x_n$ whose reverse $C^R := x_1^R,ldots,x_n^R$ is uniquely decodable. I claim that $C$ is also uniquely decodable. This is because
      $$
      w = x_{i_1} ldots x_{i_m} text{ if and only if } w^R = x_{i_m}^R ldots x_{i_1}^R.
      $$

      In words, decompositions of $w$ into codewords of $C$ are in one-to-one correspondence with decompositions of $w^R$ into codewords of $C^R$. Since the latter are unique, so are the former.



      Since prefix codes are uniquely decodable, it follows that the reverse of a prefix code is also uniquely decodable. This is the case in your example.



      The McMillan inequality states that if $C$ is uniquely decodable then
      $$
      sum_{i=1}^n 2^{-|x_i|} leq 1.
      $$

      In other words, a uniquely decodable code satisfies Kraft's inequality. Therefore if all you're interested in is minimizing the expected codeword length, there is no reason to look beyond prefix codes.



      Sam Roweis gives in his slides a nice example of a uniquely decodable code which is neither a prefix code nor the reverse of a prefix code:
      $$
      0,01,110.
      $$

      In order to show that this code is uniquely decodable, it suffices to show how to decode the first codeword of a word. If the word starts with a $1$, then the first codeword is $110$. If it is of the form $01^*$, then it must be either $0$ or $01$. Otherwise, there must be a prefix of the form $01^*0$. We now distinguish several cases:



      $$
      begin{array}{c|cccc}
      text{prefix} & 00 & 010 & 0110 & 01110 \hline
      text{codeword} & 0 & 01 & 0 & 01
      end{array}
      $$

      Longer runs of $1$ cannot be decoded at all.






      share|cite|improve this answer











      $endgroup$









      • 1




        $begingroup$
        In seems that in the OP's example, we cannot decode the first codeword after a fixed amount of digits, there are infinitely many cases: 1001010101010101… can be either fcccccc… or caaa…, and we might need to wait until the end of the input to decide.
        $endgroup$
        – Bergi
        9 hours ago












      • $begingroup$
        This also happens for $1,10,00$.
        $endgroup$
        – Yuval Filmus
        9 hours ago










      • $begingroup$
        @Bergi It is always decodable for any finite amount of digits. There is always only one way to decode the encoding without any remainders. Any other attempt will end up with spare 1's or 0s. This is because the code is uniquely decodable if we read it tail first. In theory if something is uniquely decodable in one direction it makes no sense that there can be more than one solution in the other direction
        $endgroup$
        – slebetman
        7 hours ago
















      11












      11








      11





      $begingroup$

      Your code has the property that if you reverse all codewords, then you get a prefix code. This implies that your code is uniquely decodable.



      Indeed, consider any code $C = x_1,ldots,x_n$ whose reverse $C^R := x_1^R,ldots,x_n^R$ is uniquely decodable. I claim that $C$ is also uniquely decodable. This is because
      $$
      w = x_{i_1} ldots x_{i_m} text{ if and only if } w^R = x_{i_m}^R ldots x_{i_1}^R.
      $$

      In words, decompositions of $w$ into codewords of $C$ are in one-to-one correspondence with decompositions of $w^R$ into codewords of $C^R$. Since the latter are unique, so are the former.



      Since prefix codes are uniquely decodable, it follows that the reverse of a prefix code is also uniquely decodable. This is the case in your example.



      The McMillan inequality states that if $C$ is uniquely decodable then
      $$
      sum_{i=1}^n 2^{-|x_i|} leq 1.
      $$

      In other words, a uniquely decodable code satisfies Kraft's inequality. Therefore if all you're interested in is minimizing the expected codeword length, there is no reason to look beyond prefix codes.



      Sam Roweis gives in his slides a nice example of a uniquely decodable code which is neither a prefix code nor the reverse of a prefix code:
      $$
      0,01,110.
      $$

      In order to show that this code is uniquely decodable, it suffices to show how to decode the first codeword of a word. If the word starts with a $1$, then the first codeword is $110$. If it is of the form $01^*$, then it must be either $0$ or $01$. Otherwise, there must be a prefix of the form $01^*0$. We now distinguish several cases:



      $$
      begin{array}{c|cccc}
      text{prefix} & 00 & 010 & 0110 & 01110 \hline
      text{codeword} & 0 & 01 & 0 & 01
      end{array}
      $$

      Longer runs of $1$ cannot be decoded at all.






      share|cite|improve this answer











      $endgroup$



      Your code has the property that if you reverse all codewords, then you get a prefix code. This implies that your code is uniquely decodable.



      Indeed, consider any code $C = x_1,ldots,x_n$ whose reverse $C^R := x_1^R,ldots,x_n^R$ is uniquely decodable. I claim that $C$ is also uniquely decodable. This is because
      $$
      w = x_{i_1} ldots x_{i_m} text{ if and only if } w^R = x_{i_m}^R ldots x_{i_1}^R.
      $$

      In words, decompositions of $w$ into codewords of $C$ are in one-to-one correspondence with decompositions of $w^R$ into codewords of $C^R$. Since the latter are unique, so are the former.



      Since prefix codes are uniquely decodable, it follows that the reverse of a prefix code is also uniquely decodable. This is the case in your example.



      The McMillan inequality states that if $C$ is uniquely decodable then
      $$
      sum_{i=1}^n 2^{-|x_i|} leq 1.
      $$

      In other words, a uniquely decodable code satisfies Kraft's inequality. Therefore if all you're interested in is minimizing the expected codeword length, there is no reason to look beyond prefix codes.



      Sam Roweis gives in his slides a nice example of a uniquely decodable code which is neither a prefix code nor the reverse of a prefix code:
      $$
      0,01,110.
      $$

      In order to show that this code is uniquely decodable, it suffices to show how to decode the first codeword of a word. If the word starts with a $1$, then the first codeword is $110$. If it is of the form $01^*$, then it must be either $0$ or $01$. Otherwise, there must be a prefix of the form $01^*0$. We now distinguish several cases:



      $$
      begin{array}{c|cccc}
      text{prefix} & 00 & 010 & 0110 & 01110 \hline
      text{codeword} & 0 & 01 & 0 & 01
      end{array}
      $$

      Longer runs of $1$ cannot be decoded at all.







      share|cite|improve this answer














      share|cite|improve this answer



      share|cite|improve this answer








      edited 17 hours ago

























      answered 17 hours ago









      Yuval FilmusYuval Filmus

      194k14182347




      194k14182347








      • 1




        $begingroup$
        In seems that in the OP's example, we cannot decode the first codeword after a fixed amount of digits, there are infinitely many cases: 1001010101010101… can be either fcccccc… or caaa…, and we might need to wait until the end of the input to decide.
        $endgroup$
        – Bergi
        9 hours ago












      • $begingroup$
        This also happens for $1,10,00$.
        $endgroup$
        – Yuval Filmus
        9 hours ago










      • $begingroup$
        @Bergi It is always decodable for any finite amount of digits. There is always only one way to decode the encoding without any remainders. Any other attempt will end up with spare 1's or 0s. This is because the code is uniquely decodable if we read it tail first. In theory if something is uniquely decodable in one direction it makes no sense that there can be more than one solution in the other direction
        $endgroup$
        – slebetman
        7 hours ago
















      • 1




        $begingroup$
        In seems that in the OP's example, we cannot decode the first codeword after a fixed amount of digits, there are infinitely many cases: 1001010101010101… can be either fcccccc… or caaa…, and we might need to wait until the end of the input to decide.
        $endgroup$
        – Bergi
        9 hours ago












      • $begingroup$
        This also happens for $1,10,00$.
        $endgroup$
        – Yuval Filmus
        9 hours ago










      • $begingroup$
        @Bergi It is always decodable for any finite amount of digits. There is always only one way to decode the encoding without any remainders. Any other attempt will end up with spare 1's or 0s. This is because the code is uniquely decodable if we read it tail first. In theory if something is uniquely decodable in one direction it makes no sense that there can be more than one solution in the other direction
        $endgroup$
        – slebetman
        7 hours ago










      1




      1




      $begingroup$
      In seems that in the OP's example, we cannot decode the first codeword after a fixed amount of digits, there are infinitely many cases: 1001010101010101… can be either fcccccc… or caaa…, and we might need to wait until the end of the input to decide.
      $endgroup$
      – Bergi
      9 hours ago






      $begingroup$
      In seems that in the OP's example, we cannot decode the first codeword after a fixed amount of digits, there are infinitely many cases: 1001010101010101… can be either fcccccc… or caaa…, and we might need to wait until the end of the input to decide.
      $endgroup$
      – Bergi
      9 hours ago














      $begingroup$
      This also happens for $1,10,00$.
      $endgroup$
      – Yuval Filmus
      9 hours ago




      $begingroup$
      This also happens for $1,10,00$.
      $endgroup$
      – Yuval Filmus
      9 hours ago












      $begingroup$
      @Bergi It is always decodable for any finite amount of digits. There is always only one way to decode the encoding without any remainders. Any other attempt will end up with spare 1's or 0s. This is because the code is uniquely decodable if we read it tail first. In theory if something is uniquely decodable in one direction it makes no sense that there can be more than one solution in the other direction
      $endgroup$
      – slebetman
      7 hours ago






      $begingroup$
      @Bergi It is always decodable for any finite amount of digits. There is always only one way to decode the encoding without any remainders. Any other attempt will end up with spare 1's or 0s. This is because the code is uniquely decodable if we read it tail first. In theory if something is uniquely decodable in one direction it makes no sense that there can be more than one solution in the other direction
      $endgroup$
      – slebetman
      7 hours ago













      1












      $begingroup$

      If I give you any message that you are supposed to decode, then you can do the following: Reverse the message, starting with the last bit instead of the first bit. Reverse the code words. Decode the message. Reverse the decoded string.



      You can do that because after reversing the six code words, you get a prefix-free code: 1010, 1001, 01, 000, 11, 001 is prefix free.






      share|cite|improve this answer









      $endgroup$


















        1












        $begingroup$

        If I give you any message that you are supposed to decode, then you can do the following: Reverse the message, starting with the last bit instead of the first bit. Reverse the code words. Decode the message. Reverse the decoded string.



        You can do that because after reversing the six code words, you get a prefix-free code: 1010, 1001, 01, 000, 11, 001 is prefix free.






        share|cite|improve this answer









        $endgroup$
















          1












          1








          1





          $begingroup$

          If I give you any message that you are supposed to decode, then you can do the following: Reverse the message, starting with the last bit instead of the first bit. Reverse the code words. Decode the message. Reverse the decoded string.



          You can do that because after reversing the six code words, you get a prefix-free code: 1010, 1001, 01, 000, 11, 001 is prefix free.






          share|cite|improve this answer









          $endgroup$



          If I give you any message that you are supposed to decode, then you can do the following: Reverse the message, starting with the last bit instead of the first bit. Reverse the code words. Decode the message. Reverse the decoded string.



          You can do that because after reversing the six code words, you get a prefix-free code: 1010, 1001, 01, 000, 11, 001 is prefix free.







          share|cite|improve this answer












          share|cite|improve this answer



          share|cite|improve this answer










          answered 6 hours ago









          gnasher729gnasher729

          11k1217




          11k1217























              0












              $begingroup$

              If prefix-free means what I think, the reverse of ‘a’ starts with 1, or 10, or 101, none of which is any other whole valid code.



              Therefore, if a message ends with 0101, it can only be an ‘a’ and you can apply similar logic to the preceding bit(s).



              However, what if there is no end to start from? Well, if the first bit is 1, you know it isn’t ‘a’ or ‘d’. The second bit will eliminate ‘e’ or {‘b’,’c’,’f’}. The third bit might bring it down to one choice, but if not, it is unique by the fourth bit.



              As soon as you get to a unique sequence, you restart the algorithm on the next bit.






              share|cite|improve this answer








              New contributor




              WGroleau is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
              Check out our Code of Conduct.






              $endgroup$


















                0












                $begingroup$

                If prefix-free means what I think, the reverse of ‘a’ starts with 1, or 10, or 101, none of which is any other whole valid code.



                Therefore, if a message ends with 0101, it can only be an ‘a’ and you can apply similar logic to the preceding bit(s).



                However, what if there is no end to start from? Well, if the first bit is 1, you know it isn’t ‘a’ or ‘d’. The second bit will eliminate ‘e’ or {‘b’,’c’,’f’}. The third bit might bring it down to one choice, but if not, it is unique by the fourth bit.



                As soon as you get to a unique sequence, you restart the algorithm on the next bit.






                share|cite|improve this answer








                New contributor




                WGroleau is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                Check out our Code of Conduct.






                $endgroup$
















                  0












                  0








                  0





                  $begingroup$

                  If prefix-free means what I think, the reverse of ‘a’ starts with 1, or 10, or 101, none of which is any other whole valid code.



                  Therefore, if a message ends with 0101, it can only be an ‘a’ and you can apply similar logic to the preceding bit(s).



                  However, what if there is no end to start from? Well, if the first bit is 1, you know it isn’t ‘a’ or ‘d’. The second bit will eliminate ‘e’ or {‘b’,’c’,’f’}. The third bit might bring it down to one choice, but if not, it is unique by the fourth bit.



                  As soon as you get to a unique sequence, you restart the algorithm on the next bit.






                  share|cite|improve this answer








                  New contributor




                  WGroleau is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.






                  $endgroup$



                  If prefix-free means what I think, the reverse of ‘a’ starts with 1, or 10, or 101, none of which is any other whole valid code.



                  Therefore, if a message ends with 0101, it can only be an ‘a’ and you can apply similar logic to the preceding bit(s).



                  However, what if there is no end to start from? Well, if the first bit is 1, you know it isn’t ‘a’ or ‘d’. The second bit will eliminate ‘e’ or {‘b’,’c’,’f’}. The third bit might bring it down to one choice, but if not, it is unique by the fourth bit.



                  As soon as you get to a unique sequence, you restart the algorithm on the next bit.







                  share|cite|improve this answer








                  New contributor




                  WGroleau is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.









                  share|cite|improve this answer



                  share|cite|improve this answer






                  New contributor




                  WGroleau is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.









                  answered 7 hours ago









                  WGroleauWGroleau

                  1011




                  1011




                  New contributor




                  WGroleau is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.





                  New contributor





                  WGroleau is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.






                  WGroleau is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.






















                      2000mroliver is a new contributor. Be nice, and check out our Code of Conduct.










                      draft saved

                      draft discarded


















                      2000mroliver is a new contributor. Be nice, and check out our Code of Conduct.













                      2000mroliver is a new contributor. Be nice, and check out our Code of Conduct.












                      2000mroliver is a new contributor. Be nice, and check out our Code of Conduct.
















                      Thanks for contributing an answer to Computer Science Stack Exchange!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      Use MathJax to format equations. MathJax reference.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcs.stackexchange.com%2fquestions%2f105065%2fwhy-is-this-code-uniquely-decodable%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      As a Security Precaution, the user account has been locked The Next CEO of Stack OverflowMS...

                      Список ссавців Італії Природоохоронні статуси | Список |...

                      Українські прізвища Зміст Історичні відомості |...