Abstract
We consider the problem of constructing prefix-free codes in which a designated symbol, a space, can only appear at the end of codewords. We provide a linear-time algorithm to construct almost-optimal codes with this property, meaning that their average length differs from the minimum possible by at most one. We obtain our results by uncovering a relation between our class of codes and the class of one-to-one codes. Additionally, we derive upper and lower bounds to the average length of optimal prefix-free codes with a space in terms of the source entropy.
1. Introduction
Modern natural languages achieve the unique parsability of written texts by inserting a special character (i.e., a space) between words [1] (See [2] for a few exceptions to this rule). Classical Information Theory, instead, studies codes that achieve the unique parsability of texts by imposing diverse combinatorial properties on the codeword set: e.g., the prefix property, unique decipherability, etc. [3]. With respect to the efficiency of such codes (usually measured via the average number of code symbols per source symbol), it is well known that the Shannon entropy of the information source constitutes a fundamental lower bound for it. On the other hand, if one drops the property of the unique parsability of code messages into individual codewords, and simply requires that different source symbols be encoded with different codewords, one can obtain codes (known as one-to-one codes) with efficiency below the source Shannon entropy (although not too much below; see, e.g., [4,5]).
Jaynes [6] took the approach of directly studying source codes in which a designated character of the code alphabet is exclusively used as a word delimiter. More precisely, Jaynes studied the possible decrease of the noiseless channel capacity (see [7], p. 8) associated with any code that uses a designated symbol as an end-of-codeword mark, as compared with the noiseless channel capacity of an unconstrained code. Quite interestingly, Jaynes proved that the decrease of the noiseless channel capacity of codes with an end-of-codeword mark becomes negligible, as the maximum codeword length increases.
In this paper, we study the problem of constructing prefix-free codes where a specific symbol (referred to as a ‘space’) can only be positioned at the end of codewords. We refer to this kind of prefix code as prefix codes ending with a space. We develop a linear-time algorithm that constructs ‘almost’-optimal codes with this characteristic, in the sense that the average length of the constructed codes is at most one unit longer than the shortest possible average length of any prefix-free code in which the space can appear only at the end of codewords. We prove this result by highlighting a connection between our type of codes and the well-known class of one-to-one codes. We also provide upper and lower limits of the average length of optimal prefix codes ending with a space, expressed in terms of the source entropy and the cardinality of the code alphabet.
The paper is structured as follows. In Section 2, we illustrate the relationships between prefix codes ending with a space and one-to-one codes. Specifically, we prove that, from one-to-one codes, one can easily construct prefix codes ending with a space, and we give an upper bound on the average length of the constructed codes. Successively, we show that, if we remove all the spaces from the codewords of prefix codes ending with a space, one obtains a one-to-one code. This result allows us to prove that the average length of our prefix codes ending with a space differs from the minimum possible by at most one. In Section 3 and Section 4, we derive upper and lower bounds on the average length of optimal prefix codes ending with a space in terms of the source entropy and the cardinality of the code alphabet.
2. Relations between One-to-One Codes and Prefix Codes Ending with a Space
Let be the set of source symbols, be a probability distribution on the set S (that is, is the probability of source symbol ), and be the code alphabet. We denote by the set of all non-empty sequences on the code alphabet , , and by the set of all non-empty k-ary sequences that ends with the special symbol ⊔, i.e., the space symbol.
A prefix-free code ending with a space is a one-to-one mapping:
in which no codeword is a prefix of another codeword , for any , .
A k-ary one-to-one code (see [4,5,8,9,10,11] and the references therein quoted) is a bijective mapping from S to the set of all non-empty sequences over the alphabet , .
The average length of an arbitrary code for the set of source symbols , with probabilities , is , where is the number of alphabet symbols in the codeword associated with the source symbol .
Without loss of generality, we assume that probability distribution is ordered, that is . Under this assumption, it is apparent that the best one-to-one code proceeds by assigning the shortest codeword (e.g., in the binary case, codeword 0) to the highest probability source symbol , the next shortest codeword 1 to the source symbol , the codeword 00 to , the codeword 01 to , and so on.
An equivalent approach for constructing an optimal one-to-one code, which we will use later, proceeds as follows: Let us consider the first n non-empty k-ary strings according to the radix order [12] (that is, the k-ary strings are ordered by length and, for equal lengths, ordered according to the lexicographic order). We assign the strings to the symbols in S by increasing the string length and, for equal lengths, by inverse order according to the lexicographic order. For example, in the binary case, we assign the codeword 1 to the highest probability source symbol , the codeword 0 to the source symbol , the codeword 11 to , the codeword 10 to , and so on. Therefore, one can see that, in the general case of a k-ary code alphabet, , an optimal one-to-one code of minimal average length assigns a codeword of length to the i-th symbol , where is given by:
Moreover, the codewords of an optimal k-ary one-to-one code can be represented as the nodes of a k-ary tree of maximum depth , where, for each node v, the k-ary string (codeword) associated with v is obtained by concatenating all the labels in the path from the root of the tree to v.
It is evident that, if we apply the above encoding to a sequence of source symbols, the obtained binary sequence is not uniquely parsable in terms of individual codewords. Let us see how one can recover unique parsability by appending a space ⊔ to judiciously chosen codewords of an optimal one-to-one code. To gain insight, let us consider the following example. Let be the set of source symbols, and and let us assume that the code alphabet is . Under the standing hypothesis that , one has that the best prefix-free code C one can obtain by the procedure of appending a space ⊔ to codewords of the optimal one-to-one code for S is the following:
Observe that we started from the codewords of the optimal one-to-one code constructed according to the second procedure previously described. Moreover, note that the codewords associated with symbols , and necessitate the space character ⊔ at their end; otherwise, the unique parsability of some encoded sequences of source symbols would not be guaranteed. On the other hand, the codewords associated with symbols , and do not necessitate the space character ⊔. Indeed, the codeword set
satisfies the prefix-free condition (i.e., no codeword is a prefix of any other); therefore, it guarantees the unique parsability of any coded message in terms of individual codewords.
The idea of the above example can be generalized, as shown in the following lemma.
Lemma 1.
Let be the set of source symbols and , , be a probability distribution on S. Let be the -ary code alphabet. We can construct a prefix-free code , in , such that its average length satisfies
where is the average length of an optimal one-to-one code and .
Proof.
Under the standing hypothesis that the probabilities of the source symbols are ordered from the largest to the smallest, we show how to construct a prefix-free code—by appending the special character ⊔ to the end of (some) codewords of an optimal one-to-one code for S—having the average length upper bounded by (2).
Among the class of all the prefix-free codes that one can obtain by appending the character ⊔ to the end of (some) codewords of an optimal one-to-one code for S, we aim to construct the one with the minimum average length. Therefore, we have to ensure that, in the k-ary tree representation of the code, the following basic condition holds: For any pair of nodes and , , associated with the symbols and , the depth of the node is not smaller than the depth of the node . In fact, if it were otherwise, the average length of the code could be improved.
Therefore, by recalling that is the height of the k-ary tree associated with an optimal one-to-one code, we have that the prefix-free code of the minimum average length that one can obtain by appending the special character ⊔ to the end of (some) codewords of an optimal one-to-one code for S assigns a codeword of length to the i-th symbol , where is given by:
We stress that the obtained prefix-free code is not necessarily a prefix-free code of minimum average length. Now, we justify the expression (4). First, since the probabilities are ordered in non-increasing fashion, the codeword lengths of the code are ordered in non-decreasing fashion, that is . Therefore, in the k-ary tree representation of the code, it holds the desired basic condition: For any pair of nodes and , , associated with the symbols and , the depth of the node is smaller than or equal to the depth of the node .
Furthermore, we need to append the space character only to the k-ary strings that are the prefix of some others. Therefore, let us consider the first n non-empty k-ary strings according to the radix order [12], in which, we recall, the k-ary strings are ordered by length and, for equal lengths, ordered according to the lexicographic order. We have that the number of strings that are a prefix of some others is exactly . One obtains this number by seeing the strings as corresponding to nodes in a k-ary tree with labels on the edges. The number of strings that are a prefix of some others (among the n strings) is exactly equal to the number of internal nodes (except the root) in such a tree. This number of internal nodes is equal to , where N is the total number of nodes that, in our case, is equal to (i.e., N counts also the root of the tree).
Moreover, starting from the optimal one-to-one code constructed according to our second method, that is by assigning k-ary strings to the symbols by increasing length and, for equal lengths, by inverse order according to the lexicographic order, one can verify that the internal nodes are associated with the codewords of the symbols , for i that goes from 1 to , and from to .
In fact, since the height of the k-ary tree is and since all the levels of the tree, except the last two, are full, we need to append the space to all symbols from 1 to . While on the second-to-last level, we have to append the space only to the remaining internal nodes associated with the symbols , where i goes from to . Those remaining nodes are exactly, among all the nodes in the second-to-last level, the ones associated with the symbols that have smaller probabilities. Thus, we obtain (4).
Summarizing, we can construct a prefix-free code , in time, with lengths defined as in (4), starting from an optimal one-to-one code. Thus:
□
Note that, from Lemma 1, we obtain that the average length of any optimal (i.e., of minimum average length) prefix-free code ending with a space is upper bounded by the Formula (2). Furthermore, we have an upper bound on the average length of the optimal prefix-free codes ending with a space in terms of the average length of optimal one-to-one codes.
We can also derive a lower bound on the average length of optimal prefix-free codes ending with a space in terms of the average length of optimal one-to-one codes. For such a purpose, we need two intermediate results. We first recall that, given a k-ary code C, its codewords can be represented as nodes in a k-ary tree with labels on the edges. Indeed, for each node v, the k-ary string (codeword) associated with v can be obtained by concatenating all the labels in the path from the root of the tree to v. We also recall that, in prefix-free codes, the codewords correspond to the node leaves of the associated tree, while in one-to-one codes, the codewords may correspond also to the internal nodes of the associated tree.
Lemma 2.
Let be the set of source symbols, and let , , be a probability distribution on S. There exists an optimal prefix-free code ending with a space such that the following property holds: For any internal node v (except the root) of the tree representation of C, if we denote by w the k-ary string associated with the node v, then the string belongs to the codeword set of C.
Proof.
Let C be an arbitrary optimal prefix-free code ending with a space. Let us assume that, in the tree representation of C, there exists an internal node v whose associated string w is such that does not belong to the codeword set of C. Since v is an internal node, there is at least a leaf x, which is a descendant of v, whose associated string is the codeword of some symbol . We modify the encoding, by assigning the codeword to the symbol . The new encoding is still prefix-free, and its average length can only decrease since the length of the newly assigned codeword to cannot be greater than the previous one. We can repeat the argument for all internal nodes that do not satisfy the property stated in the lemma to complete the proof. □
Lemma 3.
Let be an arbitrary prefix-free code, then the code one obtains from C by removing the space ⊔ from each codeword of C is a one-to-one code.
Proof.
The proof is straightforward. Since C is prefix-free, it holds that, for any pair , with , the codeword is not a prefix of and vice versa. Therefore, since D is obtained from C by removing the space, we have four cases:
- and : then since ;
- and : then since is not a prefix of and vice versa;
- and : then since is not a prefix of ;
- and : then since is not a prefix of .
Therefore, for any pair , with , , and D is a one-to-one code. □
We can now derive a lower bound on the average length of optimal prefix-free codes with space in terms of the average length of optimal one-to-one codes.
Lemma 4.
Let be the set of source symbols, and let , , be a probability distribution on S, then the average of an optimal prefix-free code satisfies
where is the average length of an optimal k-ary one-to-one code on S.
Proof.
From Lemma 2, we know that there exists an optimal prefix-free code C with a space in which exactly codewords contain the space character at the end. Let be the set of indices associated with the symbols whose codeword contains the space. Moreover, from Lemma 3, we know that the code D obtained by removing the space from C is a one-to-one code. Putting it all together, we obtain that
From (6), we have that
□
We notice that the difference between the expression (2) and the lower bound (5) is, because of (3), less than
therefore, the prefix-free codes ending with a space that we construct in Lemma 1 have an average length that differs from the minimum possible by at most one. Moreover, since both the upper bound (3) and the lower bound (5) are easily computable, we can determine the average length of an optimal prefix-free code with a tolerance at most of one. One can also see that the left-hand side of (7) is, often, much smaller than one.
In the following sections, we will focus on providing upper and lower bounds on the average length of k-ary optimal one-to-one codes in terms of the k-ary Shannon entropy of the source distribution . Because of Lemmas 1 and 4, this will give us the corresponding upper and lower bounds on the average length of optimal prefix-free codes ending with a space.
3. Lower Bounds on the Average Length
In this section, we provide lower bounds on the average length of the optimal one-to-one code and, subsequently, thanks to Lemma 4, on the average length of the optimal prefix-free code with a space. For technical reasons, it will be convenient to consider one-to-one codes that make use of the empty word , that is one-to-one mappings . It is easy to see (cf. (1)) that the optimal one-to-one code that makes use of the empty word assigns to the i-th symbol a codeword of length given by:
where k is the cardinality of the code alphabet.
Thus, by denoting by the average length of the optimal one-to-one code that does not make use of the empty word and with the average length of the optimal one-to-one code that does use it, we obtain the following relation:
Our first result is a generalization of the lower bound to the average length of the optimal one-to-one codes presented in [5], from the binary case to the general case of k-ary alphabets, . Our proof technique differs from that of [5] since we are dealing with a set of source symbols of bounded cardinality (in [5], the authors considered the case of a numerable set of source symbols).
Lemma 5.
Let be the set of source symbols and be a probability distribution on S, with . The average length of the optimal one-to-one code satisfies
where .
Proof.
The proof is an adaptation of Alon et al.’s proof [4] from the binary case to the -ary case.
We recall that the optimal one-to-one code (i.e., whose average length achieves the minimum ) has codeword lengths given by:
For each , let us define the quantities as
It holds that Let Y be a random variable that takes values in according to the probability distribution , that is
From (10), we have
By applying the entropy grouping rule ([3], Ex. 2.27) to the distribution , we obtain
We now derive an upper bound to in terms of the expected value .
To this end, let us consider an auxiliary random variable with the same distribution of Y, but with values ranging from 1 to (instead of from 0 to ). It is easy to verify that .
Let be a positive number, whose value will be chosen later. We obtain that
By substituting with in the obtained inequality
we obtain
Since is decreasing in , and because , we obtain:
By applying (14) to (12) and since , we obtain
From (11), we have that ; moreover, from the inequality (28) of Lemma 7 (proven in the next Section 4), we know that
Hence, since the function is increasing in z, we can apply (16) to upper-bound the term
to obtain the following inequality:
Rewriting (17), we finally obtain
and that concludes our proof. □
By bringing into play the size of the largest mass in addition to the entropy, Lemma 5 can be improved, as shown in the following result.
Lemma 6.
Let be the set of source symbols and , , be a probability distribution on S. The average length of the optimal one-to-one code has the following lower bounds:
- 1.
- If ,
- 2.
- ifwhere .
Proof.
The proof is the same as the proof of Lemma 5. However, we change two steps in the demonstration.
First, since
is decreasing in and , we have
Hence, by applying (20) to the right-hand side of (13), we obtain
Now, by applying (21) (instead of (14)) to (12) and since , we obtain
Here, instead of applying the upper bound:
of Lemma 7 to the right-hand side of (22), we apply the improved version:
proven in Lemma 8 of the Section 4. Then, we simply need to rewrite the inequality, concluding the proof. □
Thanks to Lemma 4 and the Formula (9), the above lower bounds on can be applied to derive our main results for prefix-free codes with a space, as shown in the following theorems.
Theorem 1.
The average length of an optimal prefix-free code with space satisfies
Proof.
From Lemma 4 and the Formula (9), we have
By applying the lower bound (10) of Lemma 5 to (24), we obtain (23). □
Analogously, by exploiting (the possible) knowledge of the maximum source symbol probability value, we have the following result.
Theorem 2.
The average length of the optimal prefix-free code with space has the following lower bounds:
- 1.
- If :
- 2.
- If :
Proof.
From Lemma 4 and the Formula (9), we have
By applying the lower bounds (18) and (19) of Lemma 6 to (27), we obtain (25) or (26) according to the value of the maximum source symbol probability. □
4. Upper Bounds on the Average Length
In this section, we will first derive upper bounds on the average length of optimal one-to-one codes. Successively, we will provide corresponding upper bounds on the average length of optimal prefix-free codes ending with a space.
We start by extending the result obtained in [13] from the binary case to the k-ary case, .
Lemma 7.
Let be the set of source symbols and , , be a probability distribution on S. The average length of the optimal one-to-one code satisfies
Proof.
Under the standing hypothesis that , it holds that
We recall that the length of the i-th codeword of the optimal one-to-one code D is equal to
Therefore, from (29), we can upper bound each length as
Hence, by applying (31) to the average length of D, we obtain
This concludes our proof. □
By exploiting the knowledge of the maximum probability value of , we generalize the upper bound in [5] from to arbitrary .
Lemma 8.
Let be the set of source symbols and , , be a probability distribution on S. The average length of the optimal one-to-one code satisfies
Proof.
Let us prove first that the length of an optimal one-to-one code satisfies the inequality:
Indeed, by recalling that , we can write as follows:
where the last inequality holds since
We note that the function is increasing in j. Therefore, it reaches the minimum at , where it takes the value
for any . Thus, (34) holds as we claimed.
Let us now show that
Since the distribution is ordered in a non-increasing fashion, from (29) and (34), we have
Therefore, (35) holds.
To conclude the proof, it remains to prove that
By observing that for any , it holds that
we obtain:
Therefore, (36) holds as well.
From (35) and (36), since
we obtain
and
Now, it is easy to verify that for , proving the Lemma. □
Thanks to the result of Lemma 1 and to the Formula (9), the upper bounds obtained above can be used to derive our upper bounds on the average length of optimal prefix-free codes with space, as shown in the following theorems.
Theorem 3.
The average length of an optimal prefix-free code with space satisfies
where .
Proof.
From Lemma 1 and the Formula (9), we have
By applying the upper bound (28) on of Lemma 7 to (40), we obtain (38). □
Theorem 4.
The average length of an optimal prefix-free code with space satisfies
Proof.
From Lemma 1 and the Formula (9) and by recalling that , we have
We apply the upper bound (33) on of Lemma 8 to (42). That gives us (41). □
Remark 1.
One can estimate how much the average length of optimal prefix-free codes ending with a space differs from the minimum average length of unrestricted optimal prefix-free codes on the alphabet , that is optimal prefix-free codes in which the special symbol ⊔ is not constrained to appear at the end of the codewords, only.
Let be the set of source symbols and be a probability distribution on S. Let us denote by an optimal prefix-free code ending with a space for S and by an optimal prefix-free code without the restriction of where the space can occur. Clearly, , since the more constrained optimal code has an average length less than . Therefore,
Since , we have that, as the cardinality of the code alphabet increases, the constraint that the space can appear only at the end of codewords becomes less and less influential.
5. Concluding Remarks
In this paper, we have introduced the class of prefix-free codes where a specific symbol (referred to as a “space") can only appear at the end of codewords. We have proposed a linear-time algorithm to construct “almost"-optimal codes with this characteristic, and we have shown that their average length is at most one unit longer than the minimum average length of any prefix-free code in which the space can appear only at the end of codewords. We have proven this result by highlighting a connection between our type of codes and the well-known class of one-to-one codes. We have also provided upper and lower limits of the average length of optimal prefix-free codes ending with a space, expressed in terms of the source entropy and the cardinality of the code alphabet.
We leave open the problem of providing an efficient algorithm to construct optimal prefix-free codes ending with a space. It seems that there is no easy way to modify the classical Huffman greedy algorithm to solve our problem. It is possible that the more powerful dynamic programming approach could be useful to provide an optimal solution to the problem, as done in [14] for optimal binary codes ending with ones. This will be the subject of future investigations.
Author Contributions
All authors contributed equally to this work. All authors have read and agreed to the published version of the manuscript.
Funding
This research received no external funding.
Institutional Review Board Statement
Not applicable.
Informed Consent Statement
Not applicable.
Data Availability Statement
No new data were created or analyzed in this study. Data sharing is not applicable to this article.
Acknowledgments
The authors would like to thank the referees and the Guest Editor I. Sason for their corrections and useful suggestions.
Conflicts of Interest
The authors declare no conflicts of interest.
References
- Daniels, P.T.; Bright, W. (Eds.) The World’s Writing Systems; Oxford University Press: Oxford, UK, 1996. [Google Scholar]
- Wikipedia: Writing Systems without Word Boundaries. Available online: https://en.wikipedia.org/wiki/Category:Writing_systems_without_word_boundaries (accessed on 25 March 2024).
- Cover, T.M.; Thomas, J.A. Elements of Information Theory, 2nd ed.; Wiley-Interscience: Hoboken, NJ, USA, 2006. [Google Scholar]
- Alon, N.; Orlitsky, A. A lower bound on the expected length of one-to-one codes. IEEE Trans. Inf. Theory 1994, 40, 1670–1672. [Google Scholar] [CrossRef] [Scilit]
- Blundo, C.; De Prisco, R. New bounds on the expected length of one-to-one codes. IEEE Trans. Inf. Theory 1996, 42, 246–250. [Google Scholar] [CrossRef] [Scilit]
- Jaynes, E. Note on unique decipherability. Ire Trans. Inf. Theory 1959, 5, 98–102. [Google Scholar] [CrossRef] [Scilit]
- Shannon, C.E. The Mathematical Theory of Communication; University of Illinois Press: Urbana, IL, USA, 1949. [Google Scholar]
- Courtade, T.; Verdú, S. Cumulant generating function of codeword lengths in optimal lossless compression. IEEE Int. Symp. Inf. Theory 2014, 2494–2498. [Google Scholar]
- Kontoyiannis, I.; Verdú, S. Optimal lossless data compression: Non-asymptotics and asymptotics. IEEE Trans. Inf. Theory 2014, 60, 777–795. [Google Scholar] [CrossRef] [Scilit]
- Kosut, O.; Sankar, L. Asymptotics and non-asymptotics for universal Fixed-to-Variable source coding. IEEE Trans. Inf. Theory 2017, 63, 3757–3772. [Google Scholar] [CrossRef] [Scilit]
- Szpankowski, W.; Verdu, S. Minimum expected length of Fixed-to-Variable lossless compression without prefix constraints. IEEE Trans. Inf. Theory 2011, 57, 4017–4025. [Google Scholar] [CrossRef] [Scilit]
- Knuth, D. The Art of Computer Programming, Volume 3, 2nd ed.; Sorting and Searching; Addison Wesley Longman Publishing Co., Inc.: Boston, MA, USA, 1998. [Google Scholar]
- Wyner, A. An upper bound on the entropy series. Inf. Control 1972, 20, 176–181. [Google Scholar] [CrossRef] [Scilit]
- Chan, S.-L.; Golin, M.J. A dynamic programming algorithm for constructing optimal 1-ended binary prefix-free codes. IEEE Trans. Inf. Theory 2000, 46, 1637–1644. [Google Scholar] [CrossRef] [Scilit]
Disclaimer/Publisher’s Note: The statements, opinions and data contained in all publications are solely those of the individual author(s) and contributor(s) and not of MDPI and/or the editor(s). MDPI and/or the editor(s) disclaim responsibility for any injury to people or property resulting from any ideas, methods, instructions or products referred to in the content. |
© 2024 by the authors. Licensee MDPI, Basel, Switzerland. This article is an open access article distributed under the terms and conditions of the Creative Commons Attribution (CC BY) license (https://creativecommons.org/licenses/by/4.0/).