Additional reading: p.420 of Rissanen's paper on a universal prior for integers.
Lemma:
is a necessary and sufficient condition for
to be the lengths of codewords of a prefix code of size n.
It is easiest to prove this by using the prefix-tree model of coding. We first prove the only if or necessary part and then prove the if or sufficient part.
Proof of necessity. Let
.
Then the li can all be represented as the leaves of a binary tree of depth m from the root. Consider such a tree that is full, i.e. every node at depth less than m has two sub-trees and every node at depth m has two leaves. Then, the node at depth i is the ancestor of 2m-i leaves.
So the total number of leaves fathered by all of the nodes at
depths
is
Since no leaf is on the path to any other leaf, the above quantity cannot be greater than the number of leaves in the full tree. So
This proves necessity, i.e. a code with i codewords of lengths li is a prefix code only if the inequality (11) holds.
We now have to prove sufficiency, i.e. If inequality (11) holds then it is possible to construct a prefix code with just the li as the lengths of its codewords.
Proof of sufficiency.
Again, start with a full tree of depth m. Now arrange the lengths
li in non-decreasing order such that
.
Pick any unassigned node at depth l1 from the root and delete all 2ln-l1 of its descendant leaves as well as all the nodes in the paths to them. This turns the node into a leaf which is assigned a codeword of length l1.
Repeat this process for
.
We claim that
if (11) holds then it will be possible to find a unique
assignment thus for each li.
Proof of this is by contradiction. Assume not. Then for some j < n, no unassigned and undeleted node at depth lj could be found, i.e. all nodes at depth lj have been either assigned or deleted. This number is 2lj.
But recall that each previous assignment of li has excluded
(assigned or deleted) exactly
2lj-li nodes at depth j. So
the total number of nodes at level j excluded upto this point is
.
Then
which contradicts the Kraft inequality. So it will indeed be possible to find assignments for every length li if the inequality holds.