这是我对algorithm described by wikipedia的解释
首先,创建一个函数,将两个集合作为输入并生成一组所有悬空后缀:
SET findSuffixes( SET a, SET b )
{
SET result = {}
for each item x in a
for each item y in b
{
if x is a prefix of y
result.additem( y - x )
else if y is a prefix of x
result.additem( x - y )
}
return result
}
并创建一个函数来检查任何代码字是否出现在给定的集合中:
BOOL containsCodeword( SET a, SET codewords )
并创建一个判断两个集合是否相同的函数:
BOOL setIsEqualToSet( SET a, SET b )
那么算法是这样的
(A) SET codewords = { 0, 01, 0111, 01111, 11110 }
(B) SET current = findSuffixes( codewords, codewords )
(C) LIST_OF_SETS previous = {}
forever
{
(D) if ( containsCodeword( current, codewords) )
then the code is not uniquely decodable, done
for ( each set x in previous )
(E) if ( setIsEqualToSet( current, x )
then the code is uniquely decodable, done
(F) previous.additem( current )
(G) current = findSuffixes( current, codewords )
}
这是一个有效的例子:
(A) codewords = { 0, 01, 0111, 01111, 11110 }
(B) findSuffixes( codewords, codewords )
0 - 0 = ignored since the result is empty
01 - 0 = 1
0111 - 0 = 111
01111 - 0 = 1111
11110 - 0 = rejected since 11110 doesn't start with 0
0 - 01 = rejected
01 - 01 = ignored
0111 - 01 = 11
01111 - 01 = 111
11110 - 01 = rejected
0 - 0111 = rejected
01 - 0111 = rejected
0111 - 0111 = ignored
01111 - 0111 = 1
11110 - 0111 = rejected
0 - 01111 = rejected
01 - 01111 = rejected
0111 - 01111 = rejected
01111 - 01111 = ignored
11110 - 01111 = rejected
0 - 11110 = rejected
01 - 11110 = rejected
0111 - 11110 = rejected
01111 - 11110 = rejected
11110 - 11110 = ignored
After removing duplicates, current = { 1, 11, 111, 1111 }
(D) current does not contain any of the codewords
(E) the previous list is empty, so no set in the previous list matches current
(F) at this point the set { 1, 11, 111, 1111 } is added to the previous list
(G) findsuffixes( current, codewords )
there are 40 cases to consider
20 cases where a member of the current set is removed from a member of codewords
20 cases where a member of codewords is removed from a member of the current set
most cases are rejected, the only interesting cases are
11110 - 1 = 1110
11110 - 11 = 110
11110 - 111 = 10
11110 - 1111 = 0
So current = { 1110, 110, 10, 0 }
(D) current does contain one of the codewords, so the code in not uniquely decodable