Incoding theory, theSardinas–Patterson algorithm is a classical algorithm for determining inpolynomial time whether a givenvariable-length code is uniquely decodable, named after August Albert Sardinas and George W. Patterson, who published it in 1953.[1] The algorithm carries out a systematic search for a string which admits two different decompositions into codewords. AsKnuth reports, the algorithm was rediscovered about ten years later in 1963 byFloyd, despite the fact that it was at the time already well known in coding theory.[2]
Consider the code. This code, which is based on an example by Berstel,[3] is an example of a code which is not uniquely decodable, since the string
can be interpreted as the sequence of codewords
but also as the sequence of codewords
Two possible decodings of this encoded string are thus given bycdb andbabe.
In general, a codeword can be found by the following idea: In the first round, we choose two codewords and such that is aprefix of, that is, for some "dangling suffix". If one tries first and, the danglingsuffix is. If we manage to find two sequences and of codewords such that, then we are finished: For then the string can alternatively be decomposed as, and we have found the desired string having at least two different decompositions into codewords.
In the second round, we try out two different approaches: the first trial is to look for a codeword that hasw as prefix. Then we obtain a new dangling suffixw, with which we can continue our search. If we eventually encounter a dangling suffix that is itself a codeword (or theempty word), then the search will terminate, as we know there exists a string with two decompositions. The second trial is to seek for a codeword that is itself a prefix ofw. In our example, we have, and the sequence1 is a codeword. We can thus also continue with as the new dangling suffix.
| Ambiguity from result | |||||||
|---|---|---|---|---|---|---|---|
| 1 | 1 | 1 | 0 | 0 | 1 | 1 | |
| d | b | ||||||
| a | F | b | since d = aF | ||||
| a | a | H | b | since F = aH | |||
| a | a | e | since e = Hb | ||||
| 1 | 1 | 1 | 0 | 0 | 1 | 1 | |
| Example run | |
|---|---|
| : | |
| a | 1 |
| b | 011 |
| c | 01110 |
| d | 1110 |
| e | 10011 |
| : | |
| F=a-1d | 110 |
| G=a-1e | 0011 |
| H=b-1c | 10 |
| : | |
| H=a-1F | 10 |
| I=a-1H | 0 |
| b=H-1e | 011 |
| J=I-1b | 11 |
| K=I-1c | 1110 |
The algorithm is described most conveniently usingquotients offormal languages. In general, for two sets of stringsD andN, the (left) quotient is defined as the residual words obtained fromD by removing some prefix inN. Formally,. Now let denote the (finite) set of codewords in the given code.
The algorithm proceeds in rounds, where we maintain in each round not only one dangling suffix as described above, but the (finite) set of all potential dangling suffixes. Starting with round, the set of potential dangling suffixes will be denoted by. The sets are definedinductively as follows:
. Here, the symbol denotes theempty word.
, for all.
The algorithm computes the sets in increasing order of. As soon as one of the contains a word fromC or the empty word, then the algorithm terminates and answers that the given code is not uniquely decodable. Otherwise, once a setequals a previously encountered set with, then the algorithm would enter in principle an endless loop. Instead of continuing endlessly, it answers that the given code is uniquely decodable.
See the left box for an example run of the algorithm on the given code; lower and upper case letters denote code and "dangling sugffix" strings, respectively. During the construction of, the code wordb is encountered (shown in red), and the algorithm stops. The right box indicates how the example string 1110011 can be shown to have multiple encodings (db,aae), using the equations that were collected during the algorithm run.
Since all sets are sets of suffixes of a finite set of codewords, there are only finitely many different candidates for. Since visiting one of the sets for the second time will cause the algorithm to stop, the algorithm cannot continue endlessly and thus must alwaysterminate. More precisely, the total number of dangling suffixes that the algorithm considers is at most equal to the total of the lengths of the codewords in the input, so the algorithm runs inpolynomial time as a function of this input length. By using asuffix tree to speed the comparison between each dangling suffix and the codewords, the time for the algorithm can be bounded by O(nk), wheren is the total length of the codewords andk is the number of codewords.[4] The algorithm can be implemented using apattern matching machine.[5] The algorithm can also be implemented to run on anondeterministic Turing machine that uses onlylogarithmic space; the problem of testing unique decipherability isNL-complete, so this space bound is optimal.[6]
A proof that the algorithm iscorrect, i.e. that it always gives the correct answer, is found in the textbooks bySalomaa[7] and by Berstel et al.[8]