The coin collector's problem
Suppose a coin collector has a number of coins of various denominations, each of which has aDescription of the package-merge algorithm
Assume that the largest denomination is 1 dollar, and that N is an integer. (The algorithm works even if these assumptions do not hold, by trivial modifications.) The coin collector first separates his coins into lists, one for each denomination, sorted by numismatic value. He then packages the smallest denomination coins in pairs, starting from the pair of smallest total numismatic value. If there is one coin left over, it will be the coin of highest numismatic value of that denomination, and it is set aside and ignored henceforth. These packages are then merged into the list of coins of the next smallest denomination, again in order of numismatic value. The items in that list are then packaged in pairs, and merged into the next smallest list, and so forth. Finally, there is a list of items, each of which is a 1 dollar coin or a package consisting of two or more smaller coins whose denominations total 1 dollar. They are also sorted in order of numismatic value. The coin collector then selects the least value N of them. Note that the time of the algorithm is linear in the number of coins.Reduction of length-limited Huffman coding to the coin collector's problem
Let ''L'' be the maximum length any code word is permitted to have. Let ''p''1, …, ''pn'' be the frequencies of the symbols of the alphabet to be encoded. We first sort the symbols so that ''p''''i'' ≤ ''p''''i''+1. Create ''L'' coins for each symbol, of denominations 2−1, …, 2−''L'', each of numismatic value ''pi''. Use the package-merge algorithm to select the set of coins of minimum numismatic value whose denominations total ''n'' − 1. Let ''hi'' be the number of coins of numismatic value ''pi'' selected. The optimal length-limited Huffman code will encode symbol ''i'' with a bit string of length ''hi''. ThePerformance improvements and generalizations
With this reduction, the algorithm is ''O(nL)''-time and ''O(nL)''-space. However, the original paper, "''A fast algorithm for optimal length-limited Huffman codes''", shows how this can be improved to ''O(nL)''-time and ''O(n)''-space. The idea is to run the algorithm a first time, only keeping enough data to be able to determine two equivalent subproblems that sum to half the size of the original problem. This is done recursively, resulting in an algorithm that takes about twice as long but requires only linear space. Many other improvements have been made to the package-merge algorithm to reduce the multiplicative constant and to make it faster in special cases, such as those problems having repeated ''pi''s. The package-merge approach has also been adapted to related problems such as alphabetic coding. Methods involvingReferences
External links
* * {{cite conference , first1=Alistair , last1=Moffat , first2=Andrew , author-link1=Alistair Moffat (computer scientist) , last2=Turpin , first3=Jyrki , last3=Katajainen , title=Space-Efficient Construction of Optimal Prefix Codes , conference=IEEE Data Compression Conference , date=March 1995 , location=Snowbird, Utah, USA , doi=10.1109/DCC.1995.515509 * An implementation of the package-merge algorithm