Optimal Prefix Codes with Fewer Distinct
Codeword Lengths are Faster to Construct∗
arXiv:cs/0509015v4 [] 29 Sep 2016

Ahmed Belal and Amr Elmasry

†

Abstract
A new method for constructing minimum-redundancy binary prefix codes
is described. Our method does not explicitly build a Huffman tree; instead
it uses a property of optimal prefix codes to compute the codeword lengths
corresponding to the input weights. Let n be the number of weights and k
be the number of distinct codeword lengths as produced by the algorithm for
the optimum codes. The running time of our algorithm is O(k · n). Following
our previous work in [1], no algorithm can possibly construct optimal prefix
codes in o(k · n) time. When the given weights are presorted our algorithm
performs O(9k · log2k n) comparisons.

1

Introduction

Minimum-redundancy coding plays an important role in data compression applications [12] as it gives the best possible compression of a finite text when using one
static codeword per alphabet symbol. This encoding method is extensively used
in various areas of Computer Science like picture compression, data transmission,
etc. In accordance, the algorithms used for calculating minimum-redundancy prefix
codes that correspond to sets of input symbol weights are of great interest [3, 7, 8, 9].
The minimum-redundancy prefix code problem is to determine, for a given list
W = [w1 , . . . , wn ] of n positive symbol weights,
a list L = [l1 , P
. . . , ln ] of n correspondP
ing integer codeword lengths such that ni=1 2−li ≤ 1, and ni=1 wP
i li is minimized.
n
−li
(Throughout the paper, when we state that the Kraft
≤ 1 is
i=1 2
Pinequality
n
−li
satisfied, it is implicitly satisfied with an equality, i.e. i=1 2 = 1.) Once we have
the codeword lengths for a given list of symbol weights, constructing a corresponding
prefix code can be easily done in linear time using standard techniques.
Finding a minimum-redundancy code for W = [w1 , . . . , wP
n ] is equivalent to finding a binary tree with minimum-weight external path length ni=1 w(xi )l(xi ) among
∗

A preliminary version of the paper appeared in the 23rd Annual Symposium on Theoretical
Aspects of Computer Science (STACS) 2006 [2].
†
Department of Computer and Systems Engineering, Alexandria University, Egypt

1

all binary trees with leaves x1 , . . . , xn , where w(xi ) = wi , and l(xi ) = li is the depth
of xi in the corresponding tree. Hence, if we consider a leaf as a weighted node,
the minimum-redundancy prefix code problem can be defined as the problem of
constructing such an optimal binary tree for a given set of weighted leaves.
Based on a greedy approach, Huffman’s algorithm [6] constructs specific optimal
trees, which are referred to as Huffman trees. While every Huffman code is an
optimal prefix code, the converse is not true. The Huffman algorithm starts with a
forest H of n single-node trees whose values correspond to the given n weights. In
the general step, the algorithm selects two roots with the smallest values to become
children of a new root, which is added to H. This new node is assigned a value
equals to the sum of the values of its two children. The general step is repeated
until there is only one tree in H, that is called the Huffman tree. The internal nodes
of a Huffman tree are thereby assigned values throughout the algorithm; the value
of an internal node is the sum of the weights of the leaves of its subtree. With an
efficient implementation, this algorithm requires O(n log n) time and linear space.
The algorithm can be implemented in linear time if the input list was presorted [10].
A distribution-sensitive algorithm is an algorithm whose performance relies on
how the distribution of the input affects the output. For example, a related such
algorithm is that of Moffat and Turpin [9], where they show how to construct an
optimal prefix code on an alphabet of n symbols initially sorted by weight, and
including r distinct symbol weights, in O(r + r log(n/r)) time. An output-sensitive
algorithm is an algorithm whose performance relies on properties of its output. The
algorithms proposed in [7] are in a sense output sensitive, since their additional
space complexities depend on the maximum codeword length l of the output code;
the B-LazyHuff algorithm [7] runs in O(n) time and requires O(l) extra storage to
construct an optimal prefix code on an n-symbol alphabet initially sorted by weight.
In this paper we give an output-sensitive recursive algorithm for constructing
minimum-redundancy prefix codes; our algorithm’s performance depends on k, the
number of different codeword lengths (that is the number of levels that have leaves
in the corresponding optimal binary tree). As for Huffman’s algorithm, we use the
RAM model to analyse our algorithm, where we allow comparisons and additions
as unit cost operations. We distinguish two cases: the so called sorted case, if the
sequence of input weights is presorted, and the unsorted case, otherwise. For the
unsorted case, the running time of our algorithm is O(k · n), which asymptotically
surpasses Huffman’s bound when k = o(log n). For the sorted case, our algorithm
performs O(9k · log2k n) comparisons, which is sub-linear for sufficiently small k.
Throughout the paper, we interchangeably use the terms leaves and weights.
Unless otherwise stated, we assume that the input weights are unsorted. Unless
explicitly distinguished, a node of a tree can be either a leaf or an internal node.
The levels of the tree are numbered bottom up starting from 0, i.e. the root has the
highest number l, its children are at level l − 1, and the farthest leaves from the root
are at level 0; the length of the codeword assigned to a weight at level j is l − j.
2

The paper is organized as follows. In Section 2 we recall a property of optimal
prefix-code trees on which our construction algorithm relies. In Section 3 we give
the basic algorithm and prove its correctness. We show in Section 4 how to implement the basic algorithm of Section 3 to ensure the output-sensitive behavior. An
enhancement to the algorithm to achieve the O(k · n) time bound is illustrated in
Section 5. We conclude the paper in Section 6.

2

The exclusion property

Consider a binary tree T whose leaves correspond to a list of weights, such that
the value of an internal node of T equals the sum of the weights of the leaves of
its subtree. Assume that the nodes of T are numbered, starting from 1, in a nondecreasing order by their values. We may assume that the values are distinct, as
otherwise we may use a lexicographic order to distinguish between equal values. The
sibling property, introduced by Gallager [5, 11], states that a tree that corresponds
to a prefix code is a Huffman tree if and only if the nodes numbered 2i − 1 and 2i
are siblings for all i ≥ 1. The sibling property can be alternatively formulated as:
1. The nodes at each level of T can be numbered in non-decreasing order of their
values as y1 , y2 , y3 . . ., such that y2i−1 and y2i are siblings for all i ≥ 1.
2. The values of the nodes at a level are not smaller than the values of the nodes
at the lower levels.
In [1], we called this second property the exclusion property. In general, building
a Huffman tree T that has the sibling property (both the first and second properties)
for a list of n weights by evaluating all its internal nodes requires Ω(n log n). This
follows from the fact that knowing the values of the internal nodes of T implies
knowing the sorted order of the input weights; a problem that requires Ω(n log n) in
the comparison-based decision-tree model. Since it is enough to know which weights
will go to which level without necessarily knowing the order of these weights, our
main idea is that we do not have to explicitly construct T in order to find the
optimal codeword lengths. Instead, we only need to find the values of some of—and
not all—the internal nodes while maintaining the exclusion property.

3

The basic construction method

Given a list of weights, we build a corresponding optimal tree level by level in a
bottom-up manner. Starting with the lowest level (level 0), a weight is momentarily
assigned to a level as long as its value is less than the sum of two nodes with the
currently smallest value(s) at that level; this ensures the exclusion property. Kraft’s
inequality is enforced by making sure that, at the end of the algorithm, the number
3

of nodes at every level is even, and that the number of nodes at the highest level
containing leaves is a power of two. As a result, some weights may be moved upwards
from their initially-assigned levels to the higher levels. The details follow.

3.1

An illustrative example

To introduce the main ideas, we start with an example. Consider a list with thirty
weights: ten weights of value 2, ten of value 3, five of value 5, and five of value 9.
To construct the optimal codes, we start by finding the smallest two weights
in the list; these have the values 2, 2. We now identify all the weights in the list
with value less than 4, the sum of these two smallest weights. There are twenty such
weights: ten weights of value 2 and ten of value 3. All these weights are momentarily
assigned to the bottom level, that is level 0 with respect to our level numbering.
The number of nodes at level 0 is now even; so, we go to the next upper level (level
1). We identify the smallest two nodes at level 1, amongst the two smallest internal
nodes resulting from combining nodes already at level 0 (these have values 4, 4)
and the two smallest weights among those remaining in the list (these have values
5, 5). It follows that the smallest two nodes at level 1 will be the two internal nodes
4, 4 whose sum is 8. All the remaining weights with value less than 8 are to be
momentarily assigned to level 1. Accordingly, level 1 now contains an odd number
of nodes: ten internal nodes and five weights of value 5. See Figure 1(a).
To make the number of nodes at level 1 even, we move the node with the largest
value at level 1 to the, still empty, next upper level (level 2). The node to be moved,
in this case, is an internal node with value 6. Moving an internal node one level up
implies moving the weights in its subtree one level up. So, the subtree consisting
of the two weights of value 3 is moved one level up. At the end of this stage, level
0 contains ten weights of value 2 and eight weights of value 3; level 1 contains two
weights of value 3 and five weights of value 5. See Figure 1(b).
The currently smallest two internal nodes at level 2 have values 6, 8 and the
smallest weight in the list has value 9. This means that all the five remaining
weights in the list will be momentarily assigned to level 2. Now, level 2 contains
eight internal nodes and five weights, for a total of thirteen nodes. See Figure 1(c).
Since we are done with all the weights, we only need to enforce the condition
that the number of nodes at level 3 is a power of two. All we need to do is to move
the three nodes with the largest value(s), from level 2, one level up. The largest
three nodes at level 2 are the three internal nodes of values 12, 12 and 10. So, we
move eight weights of value 3 and two weights of value 5 one level up. As a result,
the number of nodes at level 3 will be 8; that is a power of two. The root will then
be at level 6. The final distribution of weights will be: ten weights of value 2 at level
0; ten weights of value 3 and three weights of value 5 at level 1; and the remaining
weights, two of value 5 and five of value 9, at level 2. The corresponding codeword
lengths are 6, 5 and 4 respectively. See Figure 1(d).
4

4

4

Level 1

2

2

Level 0

2

2

2

2

2

2

2

3

2

3

3

3

3

3

3 3

3

5

5

5

5

5

5

5

5

5

5

3

(a) Initial assignment for levels 0 and 1

Level 2

6

3

Level 1

3

4

4

2

Level 0

2

2

2

2

2

2

2

2

3

3

2

3

3

3

3

3

3

(b) Moving the node with the largest rank at level 1 to level 2

Level 2

6

3

Level 1

8

3

4

4

2

2

Level 0

2

5

2

2

2

2

2

2

3

2

3

3

3

3

3

3

5

5

5

9

9

9

9

9

9

9

9

9

9

5

3

(c) Initial assignment for level 2

Level 3

Level 2

Level 1

Level 0

10

5

12

12

5

6

3

3

3

3

3

3

3

3

3

8

3

2

5

4

4

2

2

2

2

2

2

2

2

5

5

2

(d) The final weight assignments

Figure 1: An example for the basic construction method. Squares represent leaves
and circles represent internal nodes. The values are written inside the nodes.

5

Note that we have not included all the internal nodes within Figure 1. We have
consciously only drawn the internal nodes that are required to be evaluated by our
algorithm; this will be elaborated throughout the next subsections.

3.2

The algorithm

The idea of the algorithm should be clear. We construct an optimal tree by maintaining the exclusion property for all the levels. Once the weights are placed in such
a way that the exclusion property is satisfied, the property will as well be satisfied
among the internal nodes. Adjusting the number of nodes at each level to satisfy the
Kraft inequality will not affect the exclusion property, since we are always moving
the largest nodes from one level to the next higher level. A formal description follows. (Note that the main ideas of our basic algorithm described in this subsection
are pretty similar to those of the Lazy-Traversal algorithm described in [7].)
1. Consider a list of input symbol weights W (not necessarily sorted). Two
weights with the smallest value(s) are found, removed from W , and placed at
the lowest level 0; their sum S is computed. The list W is scanned and all
weights less than S are removed from W and placed at level 0. Set η ← 0.
2. Repeat the following steps until W is empty:
(a) If the number of nodes at level η is odd, move the subtree rooted at a
node with the largest value from level η to level η + 1.
(b) Determine the new weights that will go to level η + 1 as follows. Find two
internal nodes with the smallest value(s) at level η + 1, and the smallest
two weights among those remaining in W . Find the two smallest values
amongst these four, and let their sum be S. Scan W for all weights less
than S, and assign them to level η + 1.
(c) η ←− η + 1.
3. Set η̂ ← η, i.e. η̂ is the highest level that is currently assigned weights. Let
m be the current number of nodes at level η̂. If m is not a power of 2, move
2dlg me − m subtrees rooted at the nodes with the largest value(s) from level η̂
to level η̂ + 1.

3.3

Proof of correctness

To guarantee its optimality, we need to show that both the Kraft inequality and the
exclusion property hold for the constructed tree.

6

Maintaining the Kraft inequality. First, we show by induction that the number
of nodes at every level, other than the root, is even. Assume that this is true up to
level η − 1. Since any subtree of our tree is a full binary tree (every node has zero or
two children), the number of nodes per level within any subtree is even except for
its root. At step 2(a) of the algorithm, if the number of nodes at level η is odd, we
move a subtree one level up. We are thus moving even numbers of nodes between
levels among the lower η − 1 levels. Hence, the number of nodes per level remains
even among those levels. On the other hand, the number of nodes at level η either
decreases by 1 (if the promoted subtree root has no children) or increases by 1 (if it
has two children). Either way, the number of nodes at level η becomes even.
Second, we show that the number of nodes at the last level that is assigned
weights is a power of two. At step 3 of the algorithm, if m is a power of two, no
subtrees are moved up and the Kraft inequality holds. Otherwise, we move 2dlg me −m
nodes from level η̂ to level η̂ + 1, leaving 2m − 2dlg me nodes at level η̂ other than the
children of the roots that have just been moved to level η̂ + 1. Now, the number of
nodes at level η̂ + 1 is m − 2dlg me−1 internal nodes resulting from combining pairs of
nodes from level η̂, plus the 2dlg me − m nodes that we have just moved. This sums
up to 2dlg me−1 nodes; that is a power of two.
Maintaining the exclusion property. We prove by induction that the—even
stronger—sibling property holds, as long as we are evaluating the prescribed internal nodes following Huffman’s rules. Obviously, the property is not broken when
assigning the weights at level 0. Assume that the sibling property holds up to level
η − 1. Throughout the algorithm, we maintain the exclusion property by making
sure that the sum of two nodes with the smallest value(s) at a level is larger than the
values of the nodes at this level. When we move a subtree from level η − 1 one level
up, its root has the largest value at its level. The validity of the sibling property at
the lowest η − 1 levels implies that the children of a node with the largest value at a
level have the largest value(s) among the nodes at their level. Hence, all the nodes
moved up must have had the largest value(s) among the nodes of their level. After
being moved one level up, the values of these nodes are thus larger than those of
the nodes at the lower levels, and the exclusion property is still maintained.

3.4

Discussion

We are still far from being done yet. Though we have introduced the main idea
behind the algorithm, some crucial details are still missing. Namely, we did not
show how to evaluate the essential internal nodes. Should we have to evaluate
all the internal nodes, breaking the n log n bound would have been impossible [1].
Fortunately, we only need to evaluate few internal nodes per level. More precisely,
except for the last level that is assigned weights, we may need to evaluate three
internal nodes per level: two with the smallest value(s) and one with the largest
7

value. The challenge is how to do that efficiently. Another pitfall of our basic
method, the way we have just described, is that we are to evaluate internal nodes
for every level of the tree up to the last level that is assigned weights. The work
would thus be proportional to the difference between the length of the maximum
and the minimum codeword lengths. We still need to do better, and the way out
is to skip doing work at the levels that will not be assigned any weights. Again,
the challenge is how to do that efficiently. Subsequently, we explain how to resolve
these issues in the next section.

4

The detailed construction method

Up to this point, we have not shown how to evaluate the internal nodes needed by
our basic algorithm, and how to search within the list W to decide which weights
are to be assigned to which levels. The main intuition behind the novelty of our
approach is that it does not require evaluating all the internal nodes of the tree
corresponding to the prefix code, and would thus surpass the n log n bound for
several cases. In this section, we show how to implement the basic algorithm in an
output-sensitive behavior, filling in the missing details.

4.1

An illustrative example

The main idea is clarified through an example with 3n/2 + 2 weights, where n
is a power of two. Assume that the resulting optimal tree will turn out to have
k = 3: n leaves at level 0, n/2 at level 1, and two at level lg n. Note that the 3n/2
leaves at levels 0 and 1 combine to produce two internal nodes at level lg n. It is
straightforward to come up with a set of weights that fulfill this outcome. However,
to illustrate how the algorithm works for any such set of weights, it is better to
handle the situation without explicitly deciding the weights.
For such case, we show how to apply our algorithm so that the optimal codeword lengths are produced in linear time, even if the weights were not presorted.
Determining the weights to be assigned to level 0 can be easily done by finding the
smallest two weights and scanning through the list of weights. To determine the
weights to be assigned to level 1, we need to find the values of the smallest two
internal nodes at level 1; these are respectively the sum of the smallest two pairs
of weights. After this point, let us assume that the algorithm uses an oracle that
recommends checking level lg n next.
A more involved task is to evaluate the two internal nodes y1 and y2 at level lg n,
which amounts to identifying the smallest as well as the largest n/2 nodes amongst
the n nodes at level 1. The crucial advantage is that we do not need to sort the
values of the nodes at level 1. In addition, we do not need to explicitly evaluate all
the n/2 internal nodes at level 1 resulting from the pairwise combinations of the n
weights at level 0. What we really need is the sum of the smaller half as well as the
8

y2

y1
Level 4

?

?

Level 3

Level 2

Level 1

Level 0

Figure 2: An optimal code for 3n/2 weights, where n = 16 and k = 3.
sum of the larger half among the nodes at level 1. See Figure 2. We show next that
evaluating y1 and y2 can be done in linear time by a simple pruning procedure.
The nodes at level 1 consist of two sets; one set has n/2 leaves whose weights
are known and thus their median M can be found in linear time [4], and another
set containing n/2 internal nodes whose values are not known, but whose median
M 0 can still be computed in linear time by simply finding the two middle weights
of the n leaves at level 0 and adding them. Assuming without loss of generality
that M > M 0 , then the larger half of the n/2 weights at level 1 contribute to y2 ,
and the smaller half of the n weights at level 0 contribute to y1 . The above step
of finding new medians for the leftovers of the two sets is repeated recursively on a
problem half the size. This results in a procedure with a running time that satisfies
the recurrence T (n) = T (n/2) + O(n), whose solution results in T (n) = O(n).
If the list of weights was presorted, no comparisons are required to find M or M 0 ;
we only need to compare them. The total number of comparisons needed satisfies
the recurrence Cs (n) = Cs (n/2) + O(1), and hence Cs (n) = O(log n).

4.2

Overview

Following the basic construction method, the optimal tree will be built bottom up.
However, this will not be done level by level; we shall only work in the vicinity of the
levels that will end up having leaves. Once we finish assigning weights to a level, we
should be able to jump to the next higher level to consider (Section 4.3.3). As stated
earlier, throughout the algorithm we have to efficiently evaluate some internal nodes.
In general, for a specified level and a fixed t, we need to be able to evaluate the node
with the t-th smallest or largest value among the internal nodes, or even among all
the nodes, at that level (Section 4.3.2). To be able to do that efficiently, we shall
start by illustrating a method to evaluate one specific internal node that serves as
the median of the nodes on the specified level. More precisely, if one counts the
9

number of weights contributing to each node at the specified level, and supposedly
sorts the list of nodes on that level by value, the sought node will be the node that
splits this sorted list in two sublists the number of weights contributing to each is
as close as possible to the other. We shall show how such a splitting procedure is
accomplished in a recursive manner (Section 4.3.1). Once we are able to evaluate
such a node, the other tasks—like finding the next level to be assigned weights and
finding the t-th smallest or largest nodes—can be done by repeatedly calling the
splitting procedure. The details are to come up next.

4.3

The algorithm

Let η1 = 0 < η2 < . . . ηj be the levels that have already been assigned weights after
some iterations of our algorithm (other levels only have internal
nodes). Let ni be
Pj
the number of leaves so far assigned to level ηi , and Nj = i=1 ni .
At the current iteration, we are looking forward to compute ηj+1 , the level that
will be next assigned weights by our algorithm. We use the fact that the weights that
have already been assigned up to level ηj are the only weights that may contribute
to the values of the internal nodes below and up to level ηj+1 .
Consider the internal node χ0j at level ηj , where the sum of the number of leaves
in the subtrees of level-ηj internal nodes whose values are smaller than that of χ0j is
at most but closest to Nj−1 /2. We call χ0j the splitting node of the internal nodes
at level ηj . In other words, if we define the multiplicity of a node to be the number
of leaves in its subtree, then χ0j is the weighted-by-multiplicity median within the
sorted-by-value sequence of the internal nodes at level ηj .
Analogously, consider the node χj (not necessarily an internal node) at level ηj ,
where the sum of the number of leaves in the subtrees of level-ηj internal nodes
whose values are smaller than that of χj plus the number of level-ηj leaves whose
values are smaller than that of χj is at most but closest to Nj /2. We call χj the
splitting node of all nodes at level ηj . Informally, χ0j splits the weights below level
ηj in two groups having almost equal counts, and χj splits the weights below and
up to level ηj in two groups having almost equal counts.
We extend the notion of splitting nodes to subsets of nodes. Let A be a list of
weights constituting the leaves of a subset of the internal nodes at level ηj having
consecutive ranks when sorted by value. The splitting node χ0j (A) is defined as
the weighted-by-multiplicity median within the sorted-by-value sequence of those
internal nodes. Let B be a subset of leaves at level ηj having consecutive ranks
when sorted by value. The splitting node χj (A, B) is defined as the weighted-bymultiplicity median within the sorted-by-value sequence of B in addition to the
internal nodes at level ηj whose subtrees have the set of leaves A.
Figure 3 illustrates those definitions: The internal node with value 8 is the
splitting node χ0j of the internal nodes; the sum of the multiplicity of the internal
nodes with values smaller than 8 is 16, and the sum of the multiplicity of those with
10

l
3

7

6

3

4

2

2

3

2

2

r
3

2

4

5

8

12

20

21

26

32

43

60

χ0j

3

9

10

17

22

72

χj

χ0j [l..r]

Figure 3: Nodes χ0j , χ0j [l..r] and χj at level ηj . Numbers inside nodes represent their
values, and those outside internal nodes represent their multiplicity. For illustration
purposes, the internal nodes are drawn sorted by value and so are the leaves.
values larger than 8 is 18. The leaf with value 10 is the splitting node χj of all
nodes; the sum of the multiplicity of the nodes with values smaller than 10 is 21 (19
contribute to internal nodes plus 2 leaves), and the sum of the multiplicity of those
with values larger than 10 is 21 (18 contribute to internal nodes plus 3 leaves).
4.3.1

Finding the splitting node

To find the splitting node χj of all nodes at level ηj , we repeatedly identify splitting
nodes of subsets of the internal nodes. The main idea is to apply a pruning procedure
in a manner similar to binary search. Starting with all the leaves and internal
nodes at level ηj , we repeatedly perform the following steps: compare the median
of the leaves and the splitting node of the internal nodes, discard sublists of nodes
from further consideration, and compute the median of the remaining leaves or the
splitting node of the remaining internal nodes. The details follow.
The method cut (j, L) divides the list of weights L assigned to the j lower levels
into two lists, the list of weights L that is assigned to level ηj and the list L0 of the
other weights. We use the method find-median(L) to identify the leaf M with the
median weight among the list L of the nj weights assigned to level ηj , and partition
L into three sublists {M }, L1 and L2 around M , where L1 is the list with the smaller
values. We recursively find the splitting node χ0j of the internal nodes at level ηj
using the method f ind-splitting-internal(j, L0 ) given the list L0 of the Nj−1 weights
at the levels below ηj , and partition L0 into three sublists L0(χ0 ) , L01 and L02 around
j
χ0j , where L01 is the list of weights contributing to the internal nodes with the smaller
values. We use the method add-weights(L0(χ0 ) ) to compute the value of χ0j by adding
j
the weights L0(χ0 ) constituting the leaves of the subtree of χ0j .
j
Comparing the values of M and χ0j , assume without loss of generality that M >
χ0j . We can then conclude that either the weights in L2 in addition to M must have
larger values than χj , or the internal nodes corresponding to the weights in L01 in
addition to χ0j must have smaller values than χj . In the former case, we set L to
L1 , and find the new median M and the new lists L1 and L2 . In the latter case,
we set L0 to L02 , and find the new splitting node χ0j and the new lists L01 and L02 .
The pruning process continues until only the weights contributing to χj remain. As
a byproduct, we compute, O1 and O2 , the two lists of weights contributing to the
11

Algorithm 1 f ind-splitting-all(j, L)
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:

(L, L0 ) ← cut (j, L)
(M, L1 , L2 ) ← f ind-median(L)
if (j = 1) then
return |L1 | + 1, {M }, L1 , L2
end if
(p, L0(χ0 ) , L01 , L02 ) ← f ind-splitting-internal(j, L0 )
j
O1 ← O2 ← nil, pos ← 1, s1 ← bNj /2c, s2 ← Nj − s1 − 1
while (|L| =
6 0 and |L0 | =
6 0) do
if (M > add-weights(L0(χ0 ) )) then
j
if (|L1 | + |L0 | − |L02 | > s1 ) then
O2 ← catenate({M }, L2 , O2 ), s2 ← s2 − 1 − |L2 |, L ← L1
(M, L1 , L2 ) ← f ind-median(L)
else
O1 ← catenate(O1 , L01 , L0(χ0 ) ), s1 ← s1 − |L01 | − |L0(χ0 ) |, pos ← pos + p, L0 ← L02
j
j
(p, L0(χ0 ) , L01 , L02 ) ← f ind-splitting-internal(j, L0 )
j
end if
else
if (|L2 | + |L0 | − |L01 | > s2 ) then
O1 ← catenate(O1 , L1 , {M }), s1 ← s1 − |L1 | − 1, pos ← pos + |L1 | + 1, L ← L2
(M, L1 , L2 ) ← f ind-median(L)
else
O2 ← catenate(L0(χ0 ) , L02 , O2 ), s2 ← s2 − |L0(χ0 ) | − |L02 |, L0 ← L01
j
j
(p, L0(χ0 ) , L01 , L02 ) ← f ind-splitting-internal(j, L0 )
j
end if
end if
end while
if (|L| = 0) then
while (|L01 | > s1 or |L02 | > s2 ) do
if (|L02 | > s2 ) then
O1 ← catenate(O1 , L01 , L0(χ0 ) ), s1 ← s1 − |L01 | − |L0(χ0 ) |, pos ← pos + p, L0 ← L02
j
j
else
O2 ← catenate(L0(χ0 ) , L02 , O2 ), s2 ← s2 − |L0(χ0 ) | − |L02 |, L0 ← L01
j
j
end if
(p, L0(χ0 ) , L01 , L02 ) ← f ind-splitting-internal(j, L0 )
j
end while
O1 ← catenate(O1 , L01 ), O2 ← catenate(L02 , O2 ), pos ← pos + |L01 |
return pos, L0(χ0 ) , O1 , O2
j
else
while (|L1 | > s1 or |L2 | > s2 ) do
if (|L2 | > s2 ) then
O1 ← catenate(O1 , L1 , {M }), s1 ← s1 − |L1 | − 1, pos ← pos + |L1 | + 1, L ← L2
else
O2 ← catenate({M }, L2 , O2 ), s2 ← s2 − 1 − |L2 |, L ← L1
end if
(M, L1 , L2 ) ← f ind-median(L)
end while
O1 ← catenate(O1 , L1 ), O2 ← catenate(L2 , O2 ), pos ← pos + |L1 |
return pos, {M }, O1 , O2
end if

12

nodes at level ηj whose values are smaller and respectively larger than χj . We also
compute, pos, the rank of χj among the nodes at level ηj when sorted by value. In
the sequel, We use the method catenate() to concatenate the lists in the same order
as they appear in its arguments into one list. See the pseudo-code of Algorithm 1.
Next, consider the problem of finding the splitting node χ0j of the internal nodes
at level ηj . Observe that χj−1 is a descendant of χ0j ; so, we start by recursively finding
the node χj−1 . Let α be the rank of χj−1 among the nodes at level ηj−1 when sorted
by value; the numbering starts from 1. Knowing that exactly λ = 2ηj −ηj−1 nodes
from level ηj−1 contribute to every internal node at level ηj , we conclude that the
largest β = (α − 1) − λ · b(α − 1)/λc among the α − 1 nodes whose ranks are smaller
than χj−1 and the smallest λ − β − 1 nodes among those whose ranks are larger
than χj−1 are the nodes contributing to χ0j . We proceed by finding such nodes; a
procedure that requires recursively finding more splitting nodes at level ηj−1 in a
way that will be illustrated in the next subsection. To summarize, the splitting
node χ0j of level ηj is evaluated as follows. The aforementioned pruning procedure
is applied to split the weights already assigned to the lower j − 1 levels to three
groups: those contributing to χj−1 , those contributing to the nodes smaller than
χj−1 at level ηj−1 , and those contributing to the nodes larger than χj−1 at level
ηj−1 . The weights contributing to χ0j are: the weights of the first group, the weights
among the second group contributing to the largest β nodes smaller than χj−1 , and
the weights among the third group contributing to the smallest λ − β − 1 nodes
larger than χj−1 . We also compute, pos, the rank of χ0j among the internal nodes
at level ηj . See the pseudo-code of Algorithm 2.
Algorithm 2 f ind-splitting-internal(j, L)
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:

(α, Lχj−1 , O1 , O2 ) ← f ind-splitting-all(j − 1, L)
L1 ← L2 ← nil, λ ← 2ηj −ηj−1
β ← α − 1 − λ · b(α − 1)/λc
if (β 6= 0) then
(O1 , L1 ) ← f ind-t-largest(β, j − 1, O1 )
end if
if (λ − β − 1 6= 0) then
(L2 , O2 ) ← f ind-t-smallest(λ − β − 1, j − 1, O2 )
end if
pos ← dα/λe, L0(χ0 ) ← catenate(L1 , Lχj−1 , L2 )
j
return pos, L0(χ0 ) , O1 , O2

4.3.2

j

Finding the t-th smallest/largest node

Consider the node = that has the t-th smallest/largest rank among the nodes at
level ηj . We use the method find-t-smallest()/find-t-largest() to evaluate =.
As for the case of finding the splitting node, we find the leaf with the median
weight M among the list of the nj weights already assigned to level ηj , and eval13

uate the splitting node χ0j of the internal nodes at level ηj (applying Algorithm 2
recursively) using the list of the Nj−1 leaves of the lower levels. Comparing M to
χ0j , we discard either M or χ0j plus one of the four sublists—the two sublists of nj
leaves and the two sublists of Nj−1 leaves—as not contributing to =. Repeating this
pruning procedure, we identify the weights that contribute to = and hence evaluate
=. Accordingly, we also identify the list of weights that contribute to the nodes at
level ηj whose values are smaller and those whose values are larger than =.
The main ideas of this procedure are pretty similar to those of finding the splitting node, and hence we omit the details and leave them for the reader.
4.3.3

Computing ηj+1 (the next level that will be assigned weights)

We start by finding the minimum weight w among the weights still remaining in
W , by applying the method minimum-weight(W ) that performs a linear scan if the
weights are unsorted. We use the value of w to search within the nodes at level ηj in
a manner similar to binary search. The main idea is to find the maximum number
of nodes with the smallest value(s) at level ηj such that the sum of their values is
less than w. We find the splitting node χj at level ηj , and evaluate the sum of χj
plus the weights contributing to the nodes at level ηj whose values are less than that
of χj . Comparing this sum with w, we decide with which sublist of the Nj leaves to
proceed to find its splitting node. At the end of this searching procedure, we would
have identified the weights contributing to the γ smallest nodes at level ηj such that
the sum of their values is less than w and γ is maximum. (Note that γ is at least 2,
as there are at least two such weights.) We conclude that the level to be considered
next for assigning weights is level ηj + blg γc. See the pseudo-code of Algorithm 3.
Algorithm 3 compute-next-level(j, L, W )
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:

w ← minimum-weight(W )
L ← L, sum ← 0, γ ← 0
while (|L| =
6 0) do
(α, Lχj , O1 , O2 ) ← f ind-splitting-all(j, L)
s ← add-weights(O1 ) + add-weights(Lχj )
if (sum + s < w) then
sum ← sum + s, γ ← γ + α, L ← O2
else
L ← O1
end if
end while
ηj+1 ← ηj + blg γc
return ηj+1

To prove the correctness of this procedure, consider any level η where ηj < η <
ηj + blg γc. The subtrees of two internal nodes with the smallest value(s) at level η
have at most 2η−ηj +1 ≤ 2blg γc ≤ γ nodes at level ηj . Hence, the sum of the values of
14

such two nodes is less than w. For the exclusion property to hold, no weights are to
be assigned to any of these levels. On the contrary, the subtrees of the two internal
nodes with the smallest values at level ηj + blg γc have more than γ nodes at level
ηj , and hence the sum of their values is at least w. For the exclusion property to
hold, at least the weight w is to be momentarily assigned to level ηj + blg γc.
4.3.4

Maintaining the Kraft inequality

After computing the value of ηj+1 , we need to maintain the Kraft inequality. This is
accomplished by moving the subtrees of some of the nodes with the largest value(s)
from level ηj one level up. Let nj be the number of nodes currently at level ηj as
counted by the method count-nodes(j), and let λ = 2ηj+1 −ηj . We shall show that the
number of subtrees to be moved up is ν = λ · dnj /λe − nj . See the pseudo-code of
Algorithm 4. Note that when ηj+1 − ηj = 1 (as in the case of our basic algorithm),
then ν = 1 if nj is odd and ν = 0 otherwise.
Algorithm 4 maintain-Kraf t-inequality(j, L, W )
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:

nj ← count-nodes(j)
if (|W | =
6 0) then
λ ← 2ηj+1 −ηj
ν ← λ · dnj /λe − nj
else
ν ← 2dlg nj e − nj
end if
(L, L) ← f ind-t-largest(ν, j, L)
for every weight w ∈ L do
level(w) ← level(w) + 1
end for

To establish the correctness of this procedure, we need to show that both the
Kraft inequality and the exclusion property hold. For a realizable construction, the
number of nodes at level ηj has to be even, and if ηj+1 − ηj 6= 1, the number of nodes
at level ηj +1 has to divide λ/2. If nj divides λ, no subtrees are moved to level ηj +1
and the Kraft inequality holds. If nj does not divide λ, then λ · dnj /λe − nj nodes
are moved to level ηj + 1, leaving 2nj − λ · dnj /λe nodes at level ηj other than those
of the subtrees that have just been moved one level up. Now, the number of nodes
at level ηj + 1 is nj − λ · dnj /λe/2 internal nodes with children remaining (not moved
up) at level ηj , plus the λ · dnj /λe − nj nodes that we have just moved. This sums
up to λ · dnj /λe/2 nodes, which divides λ/2, and the Kraft inequality holds. The
exclusion property holds following the same argument given in Section 3.3. Kraft’s
inequality for the highest level that is assigned leaves, i.e. when |W | = 0, is correctly
maintained also following the argument given in Section 3.3.

15

4.3.5

Summary of the algorithm

1. The smallest two weights are found, moved from W to the lowest level η1 = 0,
and their sum S is computed. The rest of W is searched for weights less than
S, which are assigned to level 0 as well. Set j ← 1.
2. Repeat the following steps until W is empty:
(a) Compute ηj+1 (the next level that will be assigned weights).
(b) Maintain the Kraft inequality at level ηj (by moving the ν = λ·dnj /λe−nj
subtrees with the largest values from this level one level up, where λ =
2ηj+1 −ηj and nj is the current number of nodes at level ηj ).
(c) Find the values of the smallest two internal nodes at level ηj+1 , and the
smallest two weights from those remaining in W . Find two nodes with
the smallest value(s) among these four, and let their sum be S.
(d) Search the rest of W , and assign the weights less than S to level ηj+1 .
(e) j ←− j + 1.
3. If nj is not a power of 2, move the 2dlg nj e − nj subtrees rooted at the nodes
with the largest values from level ηj to level ηj + 1.

4.4

Complexity analysis

Let T (j, |L|) be an upper bound on the time required by our algorithm to find the
splitting node (and also for the t-th smallest node) of a set of nodes at level ηj which
are roots of subtrees having the list of leaves L. It follows that T (j, Nj ) bounds the
time to find χj . Let T 0 (j, |L|) be an upper bound on the time required to find the
splitting node of a set of internal nodes at level ηj which are roots of subtrees having
the list of leaves L. It follows that T 0 (j, Nj−1 ) bounds the time to find χ0j .
First, consider Algorithm 1. The total amount of work required to find the
medians among the nj weights assigned to level ηj in all the recursive calls is O(nj ).
During the pruning procedure to locate χj , the time for the i-th recursive call to find
a splitting node of the internal nodes at level ηj is at most T 0 (j, bNj−1 /2i−1 c). The
P
pruning procedure, therefore, requires at most îi=1 T 0 (j, bNj−1 /2i−1 c)+O(nj ) time,
P
where î = blg Nj−1 c. Mathematically, T (j, Nj ) ≤ îi=1 T 0 (j, bNj−1 /2i−1 c) + O(nj ).
Second, consider Algorithm 2. To find the splitting node of the internal nodes
at level ηj we find the splitting node of all the nodes at level ηj−1 for the same list
of weights. We also find the t-th smallest and largest nodes among each half of
this list of weights; the time for each of these two calls is at most T (j − 1, b|L|/2c).
Mathematically, T 0 (j, |L|) ≤ T (j − 1, |L|) + 2 T (j − 1, b|L|/2c) + O(1).

16

Summing up the bounds, the next relations follow:
T (1, N1 ) = O(n1 ),
T (j, 1) = O(1),
T (j, Nj ) ≤

î
X

i−1

T (j − 1, bNj−1 /2

c) + 2

i=1

î−1
X

T (j − 1, bNj−1 /2i c) + O(nj ).

i=1
a

Substitute with T (a, b) ≤ c · 4 · b, for 1 ≤ a < j, 1 ≤ b < Nj , and some big
constant c. Then, we induce for j ≥ 2 that
∞
∞
X

X
T (j, Nj ) ≤ c · 4j−1 · Nj−1
1/2i−1 + 2
1/2i + O(nj )
i=1

i=1

j

≤ c · 4 · Nj−1 + c · nj .
Using the fact that Nj = Nj−1 + nj , then
T (j, Nj ) = O(4j · Nj ).
Consider the case when the list of weights W is already sorted. Let Cs (j, Nj )
be the number of comparisons required to find the splitting node at level ηj . The
number of comparisons, in all recursive calls, performed against the medians among
the nj weights assigned to level ηj , is at most 3 lg (nj + 1) (at most lg (nj + 1)
comparisons to find χj , another lg (nj + 1) to find the β-th largest node among the
nodes smaller than χj , and a third lg (nj + 1) to find the (λ − β − 1)-th smallest
node among those larger than χj ). The next relations follow:
Cs (1, N1 ) = 0,
Cs (j, 1) = 0,
Cs (j, Nj ) ≤

î
X

i−1

Cs (j − 1, bNj−1 /2

c) + 2

i=1

î−1
X

Cs (j − 1, bNj−1 /2i c) + 3 lg (nj + 1).

i=1

Since the number of terms forming the summands in the previous relation is at
most 3î − 1 < 3 lg Nj−1 , it follows that
Cs (j, Nj ) < 3 lg Nj−1 · Cs (j − 1, Nj−1 ) + 3 lg (nj + 1)
< 3 lg Nj · Cs (j − 1, Nj−1 ) + 3 lg Nj .
P
i
Substitute with Cs (a, b) ≤ 3a−1 · a−1
i=1 lg b, for 1 ≤ a < j, 1 ≤ b < Nj . We thus
obtain for j ≥ 2 that
j−2

Cs (j, Nj ) < 3 lg Nj · 3

·

j−2
X
i=1

j−1

≤ 3j−1 ·

X

lgi Nj

i=1

= O(3 · logj−1 Nj ).
j

17

lgi Nj + 3 lg Nj

Third, consider Algorithm 3. The time required by this procedure is dominated
by the O(n) time to find the minimum weight w among the weights remaining in
W plus the time for the calls to find the splitting nodes. Let T 00 (j, Nj ) be the time
required by this procedure, and let î = blg Nj c. Then,
00

T (j, Nj ) ≤

î
X

T (j, bNj /2i−1 c) + O(n) = O(4j · Nj + n).

i=1

Let Cs00 (j, Nj ) be the number of comparisons required by Algorithm 3 when the
list of weights W is presorted. Then,
Cs00 (j, Nj )

≤

î
X

Cs (j, bNj /2i−1 c) + O(1) = O(3j · logj Nj ).

i=1

Finally, consider Algorithm 4. The required time is dominated by the time to find
the weights contributing to the ν nodes with the largest values at level ηj , which is
O(4j ·Nj ). If W is presorted, the number of comparisons involved is O(3j ·logj−1 Nj ).
Using the bounds deduced for the described steps of the detailed algorithm, we
conclude that the time required by the general iteration is O(4j · Nj + n).
If W is presorted, For achieving the claimed bounds, the only point left to be
mentioned is how to find the weights of W smaller than the sum of the values
of the smallest two nodes at level ηj . Once this sum is evaluated, we apply an
exponential search that is followed by a binary search on the weights of W ; this
requires O(log nj ) comparisons. It follows that the required number of comparisons
for the general iteration is still O(3j · logj Nj ).
To complete the analysis we need to show the effect of maintaining the Kraft
inequality on the complexity of the algorithm. Consider the scenario when, as a
result of moving subtrees one level up, all the weights at a level move up to the
next level that already had other weights. As a result, the number of levels that
contain leaves decreases. It is possible that within a single iteration the number of
the levels that contain leaves decreases to half its value. If this happens for several
iterations, the amount of work done by the algorithm would have been significantly
larger compared to k, the actual number of the final distinct codeword lengths.
Fortunately, this scenario will not happen quite often. In the next lemma, we
bound the number of iterations performed by the algorithm by 2k. We even show—
the stronger result—that at any step of the algorithm the number of levels that
are assigned weights at this point is at most twice the number of the final distinct
codeword lengths for the weights that have been assigned so far.
Lemma 1 Consider the set of weights that will have the τ -th largest codeword length
among the optimal prefix codes at the end of the algorithm. During the course of
execution of the algorithm these weights will be assigned to at most two consecutive
18

(among the levels that are assigned weights) levels, with level numbers at most 2τ −1
and 2τ . Hence, the number of iterations performed by the algorithm is at most 2k,
where k is the number of distinct codeword lengths for the optimal prefix codes.
Proof. Consider a set of weights that will turn out to have the same final codeword
length. Assume (for the sake of contradiction) that, during the course of execution
of the algorithm, these weights are initially assigned to three levels ηj < ηj+1 < ηj+2 .
The only way for such weights to later catch each other at the same level would be
as a result of moving subtrees up to maintain the Kraft inequality.
We show next that for this to happen, just after assigning the weights at level
ηj , there would exist an internal node y at level ηj + 1 whose value is strictly smaller
than a weight w among those weights that will be assigned to level ηj+2 . From the
exclusion property, the value of y is at most w; so, we need to show that the value of
y is not equal to w. Let s1 and s2 be the two smallest nodes at level ηj+1 just after
the weights have been assigned to level ηj+1 . If the value of y is smaller than that
of s1 and s2 , it is then smaller than w by the exclusion property. If the value of y is
equal to that of either s1 or s2 , then the value of y is not equal to w; for otherwise,
w would have been assigned first to level ηj+1 . We are left with the case where the
value of y is larger than those of s1 and s2 (this only happens when ηj+1 = ηj + 1).
In accordance, s1 and s2 are both leaves; for otherwise, we could have named one
of them y and the claim would have been fulfilled. If the value of y is equal to w,
then it is equal to the sum of s1 and s2 ; for otherwise, w would have been assigned
first to level ηj+1 . In such a case, s1 and s2 are equal in value and both are equal
to the value of both children of y. But, following the way our algorithm works, this
can not happen as the algorithm should have then assigned s1 and s2 first to level
ηj and not ηj+1 . We conclude that the value of y is indeed strictly smaller than w.
Suppose next that, at some point during the algorithm, the weights that were
assigned first to level ηj are moved up to catch the weights at level ηj+2 . It follows
that y will accordingly move to level ηj+2 + 1. Since the value of y is smaller than
w, the exclusion property will not hold; a fact that contradicts the behavior of our
algorithm. It follows that these weights were initially assigned to at most two levels.
We prove the second part of the lemma by induction on τ . The base case follows
easily for τ = 1. Assume that the argument is true for τ − 1. By induction, the
levels of the weights that will have the (τ − 1)-th largest optimal codeword length
will be assigned to the at most 2τ −3 and 2τ −2 levels. From the exclusion property,
it follows that the weights that have the τ -th largest optimal codeword length must
be at the next upper levels. Using the first part of the lemma, the number of such
levels is at most two. It follows that these weights are assigned to the, at most,
2τ − 1 and 2τ levels among the levels that are assigned weights.
It follows that the weights with the τ -th largest optimal codeword length will be
assigned within 2τ iterations. Since the number of distinct codeword lengths is k,
the number of iterations performed by the algorithm is at most 2k.

19

Using Lemma 1, the time required by our algorithm to assign the set of weights
whose optimal codeword length is the j-th largest, among all distinct lengths, is
j
O(42j · n) = O(16
for all such lengths, the total time required by our
Pk · n). Summing
algorithm is j=1 O(16j · n) = O(16k · n).
Consider the case when the list of weights W is presorted. Using Lemma 1, the
number of comparisons performed to assign the weights whose codeword length is the
j-th largest among all distinct lengths is O(9j ·log2j n). Summing
Pk for all jsuch lengths,
the number of comparisons performed by our algorithm is j=1 O(9 · log2j n) =
O(9k · log2k n). The next theorem follows.
Theorem 1 Constructing a minimum-redundancy prefix code for a set of n weights
presorted by value can be done using O(9k · log2k n) comparisons, where k is the
number of distinct codeword lengths of the output code.
Corollary 1 If the list of weights was presorted, for k < c · lg n/ lg lg n3 and any
constant c < 1/2, the above algorithm requires o(n) comparisons.

5

The improved algorithm

The drawback of the algorithm we described in the previous section is that it uses
many recursive median-finding calls. We perform the following enhancement for the
unsorted case. The main idea we use here is to incrementally reorder the already
assigned weights throughout the algorithm while assigning more weights. This is
done by partitioning the assigned weights into unsorted blocks, such that the weights
of one block are smaller or equal to the weights of the succeeding block. The time
bound required by the recursive calls improves when dealing with shorter blocks.
The invariant we maintain is that during the execution of the general iteration
of the algorithm, after assigning weights to j levels, the weights that have already
been assigned to a level ηj 0 , j 0 ≤ j, are partitioned into blocks each of size at
0
most dnj 0 /8j−j e, such that the weights of each block are smaller or equal to the
weights of the next block. To accomplish this invariant, once we assign weights
to a level, the weights of each block among those already assigned to all the lower
levels are partitioned into eight almost equal blocks, by finding the weights at the
seven quartiles and partitioning around these weights. Each partitioning process
takes linear time [4]. Using Lemma 1, the number of iterations performed by the
algorithm is at most 2k. The amount of work required for this partitioning is O(n)
per iterations, for a total of an extra O(k · n) time for the partitioning procedures.
For j − j 0 ≥ log8 Nj 0 , all the weights assigned to level ηj 0 and the lower levels are
already sorted as a result of the partitioning procedure. We maintain the invariant
that the internal nodes of all these levels are evaluated and their values are explicitly
stored, by performing the following incremental evaluation procedure once the above
condition is satisfied. The internal nodes at level ηj 0 −1 have been evaluated in a
20

previous iteration, since the above invariant must have been fulfilled earlier for level
ηj 0 −1 . What we need to do at this iteration is to merge the sorted sequence of the
weights assigned to level ηj 0 −1 with the sorted sequence of the internal nodes of level
ηj 0 −1 , and evaluate the corresponding internal nodes at level ηj 0 . This extra work
can be done in a total of O(n) time per iteration. As a consequence, finding the
value of a node—the splitting node or the t-th smallest or largest node—within any
of these lower levels is done in constant time; see the recursive relations below.
The dominant step for all our procedures is to find the median weight among the
weights already assigned to a level ηj 0 . This step can now be done faster. To find
such median weight, we can identify the block that has such median (the middle
0
block) in constant time, then we find the required weight in O(dnj 0 /8j−j e), which
is the size of the block at this level. To perform any of our procedures—like finding
the splitting node at a level—the median finding process can be repeated a number
0
of times equals to the logarithm of the number of blocks; that is O(log 8j−j + 1) =
O(j − j 0 + 1). The total work done at that level besides the recursive calls will
0
then be O(dnj 0 · (j − j 0 + 1)/8j−j e). Let G(j 0 , j, Nj 0 ) be the time performed by
the improved algorithm at and below level j 0 while assigning the weights at level j,
where j 0 ≤ j and î = blg Nj 0 −1 c. The next recursive relations follow:
If j − j 0 > log8 nj 0
G(j 0 , j, Nj 0 ) = 0,
otherwise
G(j 0 , j, 1) = O(1),
G(1, j, N1 ) = O(dn1 · j/8j−1 e),
0

G(j , j, Nj 0 ) ≤

î+1
X

0

i−1

G(j − 1, j, bNj 0 −1 /2

c) + 2

i=1

î
X

G(j 0 − 1, j, bNj 0 −1 /2i c)

i=1
j−j 0

0

+ O(dn · (j − j + 1)/8
j0

.

Substitute with G(a, j, b) ≤ c · b · (j − a + 1)/8j−a , for 1 ≤ a < j 0 , 1 ≤ b < Nj 0 ,
1 ≤ j − a ≤ log8 b, and some big constant c. Then,
0

j−j 0 +1

0

G(j , j, Nj 0 ) < c · Nj 0 −1 · (j − j + 2)/8

·

∞
X

i−1

1/2

i=1

i=1

0

+dc · nj 0 · (j − j 0 + 1)/8j−j e
0
≤ c · (Nj 0 −1 + nj 0 ) · (j − j 0 + 1)/8j−j .
Since Nj 0 = Nj 0 −1 + nj 0 , it follows that
0

G(j 0 , j, Nj 0 ) = O(Nj 0 · (j − j 0 + 1)/8j−j ).
21

+2

∞
X

i

1/2



The work done to assign the weights at level j is therefore
G(j, j, Nj ) = O(Nj ) = O(n).
Since the number of iterations performed by the algorithm is at most 2k, by
Lemma 1. Summing up for these iterations, the running time for performing the
recursive calls is O(k · n). The next main theorem follows.
Theorem 2 Constructing a minimum-redundancy prefix code for a set of n unsorted weights can be done in O(k · n) time, where k is the number of distinct
codeword lengths of the output code.

6

Comments

We gave an output-sensitive algorithm for constructing minimum-redundancy prefix
codes, whose running time is O(k·n). For sufficiently small values of k, this algorithm
asymptotically improves over other known algorithms that require O(n log n) time.
It is quite interesting to know that the construction of optimal prefix codes can
be done in linear time when k turns out to be a constant. For sufficiently small
values of k, if the sequence of weights was presorted, the number of comparisons
performed by our algorithm is asymptotically better than other known algorithms
that require O(n) comparisons. It is quite interesting to know that the number of
comparisons required to construct an optimal prefix code for a sorted sequence of
weights is poly-logarithmic when k turns out to be a constant.
We have shown in [1] that the verification of a given prefix code for optimality
requires Ω(n log n) in the algebraic decision-tree model. That lower bound was
illustrated through an example of a prefix code with k = Θ(log n) distinct codeword
lengths. Since the construction is harder than the verification, constructing the
codes for such example thus requires Ω(n log n) time. This implies that there is no
algorithm for constructing optimal prefix codes that runs in o(k ·n), for otherwise we
could have been able to construct optimal codes for the example in [1] in o(n log n).
One remaining question is if it is possible or not to make our algorithm faster in
practice by avoiding so many recursive calls to a median-finding procedure.

References
[1] A. Belal and A. Elmasry. Verification of minimum-redundancy prefix codes. IEEE
Transactions on Information Theory, 52(4) (2006), 1399-1404.
[2] A. Belal and A. Elmasry. Distribution-sensitive construction of minimumredundancy prefix codes. 23rd Annual Symposium on Theoretical Aspects of
Computer Science (2006), 92-103.
22

[3] M. Buro. On the maximum length of Huffman codes. Information Processing
Letters 45 (1993), 219-223.
[4] T. Cormen, C. Leiserson, R. Rivest and C. Stein. Introduction to algorithms, 3rd
Edition. The MIT press (2009).
[5] R. Gallager. Variations on a theme by Huffman. IEEE Transactions on Information Theory 24(6) (1978), 668-674.
[6] D. Huffman. A method for the construction of minimum-redundancy codes. Proc.
IRE 40 (1952), 1098-1101.
[7] R. Milidiú, A. Pessoa, and E. Laber. Three space-economical algorithms for calculating minimum-redundancy prefix codes. IEEE Transactions on Information
Theory 47(6) (2001), 2185-2198.
[8] A. Moffat and J. Katajainen. In-Place calculation of minimum-redundancy codes.
4th Workshop on Algorithms and Data Structures (1995), 393-402.
[9] A. Moffat and A. Turpin. Efficient construction of minimum-redundancy codes
for large alphabets. IEEE Transactions on Information Theory 44(4) (1998),
1650-1657.
[10] J. Van Leeuwen. On the construction of Huffman trees. 3rd International Colloquium for Automata, Languages and Programming (1976), 382-410.
[11] J. S. Vitter. Design and analysis of dynamic Huffman codes. Journal of the
ACM 34(4) (1987), 825-845.
[12] J. Zobel and A. Moffat. Adding compression to a full-text retrieval system.
Software: Practice and Experience 25(8) (1995), 891-903.

23

