arXiv:2610.09157cs.DSOct 2026

Manuscript Offprint

arXiv:2610.09157

Massively Parallel Algorithms for Huffman Coding

Masoud Seddighin, Saeed Seddighin

Open access. PaperVantage converted this article to text, so layout and figures may differ from the original.

Abstract

Huffman coding is one of the oldest and most fundamental problems in computer science. Given a string of length nn over a general alphabet, the goal is to assign a binary codeword to each character so that no codeword is a prefix of another and the total encoded length of the string is minimized. Huffman coding is widely used in practical compression systems, including file compression. As modern datasets continue to grow, it is natural to study whether a Huffman code can be constructed efficiently in the massively parallel computation (MPC) model. The celebrated Huffman coding algorithm admits two straightforward MPC implementations: for any constant ϵ∈(0,1)\epsilon \in (0,1), one uses O(nϵ)O(n^\epsilon) memory per machine but requires Θ(log⁡n)\Theta(\log n) rounds, while the other runs in O(1)O(1) rounds but requires Θ(n)\Theta(\sqrt{n}) memory per machine. We give the first nontrivial MPC algorithm for Huffman coding that bypasses both limitations. For every constant ϵ>0\epsilon > 0, our algorithm uses Oϵ(log⁡log⁡n)O_\epsilon(\log \log n) rounds and O(nϵ)O(n^\epsilon) memory per machine, while its total memory and total computation are O~(n)\widetilde{O}(n).

This provides a rare example in which an exact solution to a problem whose classical algorithm is sequential in nature can be obtained in a sublogarithmic number of MPC rounds.

1 Introduction

Huffman coding is one of the oldest and most fundamental problems in computer science [2, 5, 7, 11]. Given a string of length n over a general alphabet, the goal is to assign a binary codeword to each character so that no codeword is a prefix of another and the total encoded length of the string is minimized. Huffman coding is widely used in practical compression systems, including file compression.

The celebrated Huffman coding algorithm solves this problem optimally in linear time via a remarkably simple greedy idea. It first represents the text as a sequence of character frequencies f1≤f2≤⋯≤fmf_1 \leq f_2 \leq \cdots \leq f_m, where mm is the size of the alphabet. It then iteratively merges the two smallest frequencies into a single new node with frequency equal to their sum, inserts this merged node back into the (sorted) collection, and repeats this process until a single tree remains. The resulting tree defines the codewords: each leaf corresponds to a character, and its codeword is given by the sequence of left/right branches on the root-to-leaf path.

The study of Huffman coding in the Massively Parallel Computation (MPC) model is motivated by both practical and theoretical considerations. Practically, MPC has become the standard paradigm for processing data at scale, underlying frameworks such as MapReduce, Hadoop, and Spark, precisely the setting in which Huffman coding is needed. Theoretically, Huffman coding belongs to a broader class of combinatorial problems with simple, efficient sequential algorithms that become surprisingly challenging to implement in the MPC model. This class includes the Longest Increasing Subsequence, Edit Distance, the Longest Common Subsequence problems which have all been subject to MPC studies [6] (STOC'17), [1] (JACM), [4] (SODA'19).

∗^{*}Tehran Institute of Advanced Studies

The massively parallel computation (MPC) model consists of a number of machines, each with memory sublinear in the input size. Computation proceeds in rounds: within a round, each machine performs local computation on the data it holds, and between rounds, machines exchange data with one another, again subject to their memory limit. The goal is to minimize the round complexity while keeping the memory per machine, the total memory, and the total work as small as possible.

In this work, we give the first non-trivial MPC algorithm for Huffman coding. Before stating our result, it is worth first pointing out the trivial ways in which Huffman's algorithm can already be turned into an MPC algorithm, so as to make clear where the real difficulty lies. We assume in this paper that the input of the Huffman coding problem is a non-binary text of size n and the output is a single integer value which is the shortest length of a binary string that can represent the input via prefix-free codewords.

Trivial Approach 1. The first approach achieves O(1)O(1) rounds using O(n)O(\sqrt{n}) local memory. The idea is to represent all frequencies in a compressed format using a threshold τ=n\tau = \sqrt{n}. For every j≤τj \leq \tau, we store only the number of frequencies equal to j, while frequencies larger than τ\tau are stored explicitly. Since the frequencies always sum to n, there are at most n\sqrt{n} such large frequencies. Hence, the entire representation has size O(n)O(\sqrt{n}) and can be gathered on one machine. That machine then simulates the classical Huffman algorithm using this compressed representation, maintaining the counters and the explicit list as new frequencies are created. This gives an O(1)O(1)-round MPC algorithm with O(n)O(n) total memory and O(n)O(n) total work.

Trivial Approach 2. A second approach uses O(n1−ϵ)O(n^{1-\epsilon}) machines with O(nϵ)O(n^{\epsilon}) memory each, for any fixed ϵ∈(0,1)\epsilon \in (0,1), and runs in O(log⁡n)O(\log n) rounds. The frequencies are kept sorted and processed in phases r=0,1,2,…r = 0,1,2,\ldots. Phase rr handles the frequencies in dyadic range Ir=[2r,2r+1)I_r = [2^r, 2^{r+1}). At the beginning of phase rr, at most one frequency is smaller than 2r2^r; we call it the carry. If the carry and another frequency exist, the carry is first merged with the smallest other frequency. The frequencies in IrI_r are then paired consecutively and all pairs are merged in parallel. If their number is odd, the last frequency becomes the carry for the next phase.

Every parent created from two frequencies in IrI_r has frequency at least 2r+12^{r+1}, so these parallel merges are consistent with a sequential execution of Huffman's algorithm. Each phase takes O(1)O(1) rounds, and there are O(log⁡n)O(\log n) phases since every frequency is at most nn. Thus, the algorithm uses O(log⁡n)O(\log n) rounds, O(n)O(n) total memory, and O~(n)\widetilde{O}(n) total computation.

Our Approach. The Ω(n)\Omega(\sqrt{n}) memory requirement of the first approach and the logarithmic round complexity of the second approach are fundamental barriers of these methods that cannot be overcome with modifications. Our algorithm bypasses both limitations by utilizing an entirely different framework. That is, our algorithm requires Oϵ(log⁡log⁡n)O_{\epsilon}(\log \log n) rounds of computation and requires O~(nϵ)\widetilde{O}(n^{\epsilon}) memory per machine. The total work and total memory of our algorithm are O~(n)\widetilde{O}(n) as desired.

A natural question is how these approaches compare when the input is not the raw text but the frequencies f1,f2,…,fmf_1, f_2, \ldots, f_m themselves that are polynomially bounded in mm (in this case the input length is mm instead of nn). Approach 2 then requires O(log⁡m)O(\log m) rounds, O~(mϵ)\widetilde{O}(m^\epsilon) memory per machine, and O~(m)\widetilde{O}(m) total memory and total work. Approach 1, by contrast, breaks down: its memory per machine grows to O(∑ifi)O(\sqrt{\sum_i f_i}), which becomes impractical as soon as ∑ifi\sum_i f_i grows to quadratic. Our approach is almost unaffected by this change of input model: its round complexity remains Oϵ(log⁡log⁡m)O_\epsilon(\log \log m), its memory per machine remains Oϵ(mϵ)O_\epsilon(m^\epsilon), and its total memory and total work are bounded by Oϵ(m1+ϵ)O_\epsilon(m^{1+\epsilon}).