# Recognize Uniquely Decodable Codes

A set of strings $C$ is called a uniquely decodable code, if $C_{∗}$ has a unique factorization over $C$. Namely, for each element $s$ in $C_{∗}$, there exist a unique finite sequence ${s_{i}}_{i=1}$ of elements in $C$, such that $s_{1}…s_{m}=s$. We call the strings in $C$ a code string.

Let $C$ be a finite set of strings, decide if $C$ is a uniquely decodable code.

In general, the infinite version of the problem is undecidable. It is however decidable if $C$ is a regular language.

To solve this problem, we shall describe a formulation of Sardinas–Patterson algorithm, which combines the description of [1] and [2].

In the entire article, we assume the alphabet size is fixed.

For variable sized alphabet, let $σ$ be the number of distinct alphabet appeared in $C$. There is an extra factor of $σ$ or $gσ$ depending on if there exist a comparator for the alphabet.

Define $S(C)={v∣xv=c,c∈C}$, the set of suffixes of $C$. $I(C)={v∣c_{′}v=c,c_{′}=c,c_{′},c∈C}$, we call those initial suffix.

$C$ is uniquely decodable if and only if there is no $v∈I(C)$, such that $vs∈C_{∗}$ and $s∈C_{∗}$.

If there exist such $v$ so $vs,s∈C_{∗}$. $c_{′}v=c$ for some $c,c_{′}∈C$. Consider the string $cs=c_{′}(vs)$. It has at least two factorization, one start with $c$, the other start with $c_{′}$, and $c_{′}=c$.

Let no such $v$ exists. Consider some $u∈C_{∗}$. It can be written as $cs$ and $c_{′}s_{′}$ for $c,c_{′}∈C$, $s,s_{′}∈C_{∗}$. Assume there is more than one factorization, then $c_{′}=c$ and wlog $c_{′}v=c$. We arrive $vs=s_{′}∈C_{∗}$, a contradiction.

Consider a $G=(V,A)$ a directed graph. $V=S(C)$. There is an arc from $a$ to $b$ iff $ab=c$ or $cb=a$ for some $c∈C$.

$C$ is uniquely decodable code iff there is no path from a vertex in $I(C)$ to $ϵ$.

For any arc $uv$, if $uv=c∈C$, then label the arc $uv$ by $u$. Otherwise, label the arc with $c$ where $cv=u$. It’s then easy to see concatenate the labels on any walk from $v$ to $ϵ$ spells a string of the form $vs∈C_{∗}$ for $s∈C_{∗}$.

By induction, one can show that for a vertex $v$, there exist string $vs∈C_{∗}$, where $s∈C_{∗}$, if and only if there is a path from $v$ to $ε$.

In order to compute the arcs on the graph, we need to answer two questions.

- Is $u$ a prefix of $c$? For all $u∈S(C)$ and $c∈C$.
- Is $c$ a prefix of $u$? For all $u∈S(C)$ and $c∈C$.

Let $k=∣C∣$, $n=∑_{c∈C}∣c∣$. It is known that we can build a generalized suffix tree for $C$ in linear time.

For the first question, “Is $u$ a prefix of $c$?”, consider we constructed a suffix tree $T$ for $C$. Transversing the suffix tree $T(C)$ with a code string $c∈C$. Assume we have read a prefix $u$ of $c$, we can check if $u∈S(C)$ in constant time during the transversal. If $u∈S(C)$, then $c=uv$ where $v∈S(C)$. It will add an arc $uv$. We use $O(∣c∣)$ time to find all the arcs can be formed by answering the “Is $u$ a prefix of $c$”. In total, we can answer question 1 in $O(n)$ time.

For the second question, we can also use the same suffix tree. Transverse the suffix tree with a code string $c$. Find all the leaves in the subtree when the string $c$ ends. Those correspond to all strings $u∈S(C)$ such that $c$ is a prefix. Namely $u=cv$ where $v∈S(C)$. The algorithm add an arc $uv$. Note there might be many $u$ with this property, worst case $O(n)$. This step might run in $O(nk)$ time.

It’s not clear we can construct arcs between the vertices in constant time. The main difficulty lies in two suffix of two different code word might correspond to the same vertex in the graph. If we number the code words and name the suffixes by their length, then we can construct a $O(n)$ size table that maps each pair $(i,j)$, which represents the suffix of length $j$ in the $i$th string, to a vertex that represent the string. Simply build a trie for the reverse of the strings, and do some bookkeeping. This map allow us to add an edge in constant time.

The graph $G$ has at most $O(nk)$ arcs. We add a new vertex that has an arc to each initial vertices and apply a DFS from the new vertex. If it reaches the $ϵ$ vertex, we return true, else we return false.

There exist an algorithm to test if $C$ is a uniquely decodable code in $O(nk)$ time, where $k=∣C∣$ and $n=∑_{c∈C}∣c∣$.

I have an implementation in Haskell here.
Note it doesn’t run in exactly $O(nk)$
time because of the `Map`

takes $O(gn)$ time. It can, however, be easily
modified to run in $O(nk)$ time.

# References

**Jewels of stringology**, World Scientific, 2002.

**A fast test for unique decipherability based on suffix trees (corresp.)**, Information Theory, IEEE Transactions On. 28 (1982) 648–651 10.1109/TIT.1982.1056535.