{'fraction_non_alphanumeric': 0.0367451744289003, 'fraction_numerical': 0.010868602797945812, 'mean_word_length': 4.415607767921362, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 3, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
Hierarchical Heavy Hitters with the Space Saving Algorithm

The Hierarchical Heavy Hitters problem extends the notion of frequent items to data arranged in a hierarchy. This problem has applications to network traffic monitoring, anomaly detection, and DDoS detection. We present a new streaming approximation algorithm for computing Hierarchical Heavy Hitters that has several advantages over previous algorithms. It improves on the worst-case time and space bounds of earlier algorithms, is conceptually simple and substantially easier to implement, offers improved accuracy guarantees, is easily adopted to a distributed or parallel setting, and can be efficiently implemented in commodity hardware such as ternary content addressable memory (TCAMs). We present experimental results showing that for parameters of primary practical interest, our two-dimensional algorithm is superior to existing algorithms in terms of speed and accuracy, and competitive in terms of space, while our one-dimensional algorithm is also superior in terms of speed and accuracy for a more limited range of parameters.


Introduction
Finding heavy hitters, or frequent items, is a fundamental problem in the data streaming paradigm. As a practical motivation, network managers often wish to determine which IP addresses are sending or receiving the most traffic, in order to detect anomalous activity or optimize performance. Often, the large volume of network traffic makes it infeasible to store the relevant data in memory. Instead, we can use a streaming algorithm to compute (approximate) statistics in real time given sequential access to the data and using space sublinear in both the universe size and stream length.
We present and analyze a streaming approximation algorithm for a generalization of the Heavy Hitters problem, known as Hierarchical Heavy Hitters (HHHs). The definition of HHHs is motivated by the observation that some data are naturally hierarchical, and ignoring this when tracking frequent items may mean the loss of useful information. Returning to our example of IP addresses, suppose that a single entity controls all IP addresses of the subnet 021.132.145.*, where * is a wildcard byte. It is possible for the controlling entity to spread out traffic uniformly among this set of IP addresses, so that no single IP address within the set of addresses 021.132.145.* is a heavy hitter. Nonetheless, a network manager may want to know if the sum of the traffic of all IP addresses in the subnet exceeds a specified threshold.
One can expand the concept further to consider multidimensional hierarchical data. For example, one might track traffic between source-destination pairs of IP addresses at the router level. In that case, the network manager may want to know if there is a Heavy Hitter for network traffic at the level of two IP addresses, between a source IP address and a destination subnet, between a  source subnet and a destination IP address, or between two subnets. This motivates the study of the two-dimensional HHH problem. There is some subtlety in the appropriate definitions, as it makes sense to require that an element is not marked as an HHH simply because it has a significant descendant, but because the aggregation of its children makes it significant; otherwise, the algorithm returns redundant, less helpful information. We present the definitions shortly, following previous work that has explored HHHs for both one-dimensional and multi-dimensional hierarchies [6,12,7,8,11,15,23].
HHHs have many applications, and have been central to proposals for real-time anomaly detection [25] and DDos detection [22]. While IP addresses serve as our motivating example throughout the paper, our algorithm applies to arbitrary hierarchical data such as geographic or temporal data. We demonstrate that our algorithm has several advantages, combining improved worst-case time and space bounds with more practical advantages such as simplicity, parallelizability, and superior performance on real-world data.
Our algorithm utilizes the Space Saving algorithm, proposed by Metwally et al. [18], as a subroutine. Space Saving is a counter-based algorithm for estimating item frequencies, meaning the algorithm tracks a subset of items from the universe, maintaining an approximate count for each item in the subset. Specifically, the algorithm input is a stream of pairs (i, c) where i is an item and c > 0 is a frequency increment for that item. At each time step the algorithm tracks a set T of items, each with a counter. If the next item i in the stream is in T , its counter is updated appropriately. Otherwise, the item with the smallest counter in T is removed and replaced by i, and the counter for i is set to the counter value of the item replaced, plus c. This approach for replacing items in the set may seem counterintuitive, as the item i may have an exaggerated count after placement, but the result is that if T is large enough, all Heavy Hitters will appear in the final set. Indeed, Space Saving has recently been identified as the most accurate and efficient algorithm in practice for computing Heavy Hitters [5], and, as we later discuss, it also possesses strong theoretical error guarantees [2].

Related Work
We require some notation to introduce prior related work and our contributions; this notation is more formally defined in Section 2. In what follows, N is the sum of all frequencies of items in the stream, is an accuracy parameter so that all outputs are within N of their true count, and H represents the size of the hierarchy (specifically, the size of the underlying lattice) the data belongs to. Unitary updates refer to systems where the count for an item increases by only 1 on each step, or equivalently, where we just count item appearances.
The one-dimensional HHH problem was first defined in [6], which also gave the first streaming algorithms for it. Several possible definitions and corresponding algorithms for the multi-dimensional problem were introduced in [7,8]. The definition we use here is the most natural, and was considered in several subsequent works [12,23]. In terms of practical applications, multi-dimensional HHHs were used in [10,11] to find patterns of traffic termed "compressed traffic clusters", in [25] for real-time anomaly detection, and in [22] for DDoS detection.
The Space Saving algorithm was used in [15] in algorithms for the one-dimensional HHH problem. Their algorithms require O(H 2 / ) space, while our algorithm requires O(H/ ) space. Very recently, [23] presented an algorithm for the two-dimensional HHH problem, requiring O(H 3/2 / ) space.
Other recent work studies the HHH problem with a focus on developing algorithms well-suited to commodity hardware such as ternary content-addressable memories (TCAMs) [13]. Our algorithms are also well-suited to commodity hardware, as we describe in Section 5. The primary difference between the present work and [13] is that the algorithms of [13] reduce overhead by only updating rules periodically, rather than on a per-packet basis. This leads to lightweight algorithms with no provable accuracy guarantees. However, simulation results in [13] suggest these algorithms perform well in practice. In contrast, our algorithms possess very strong accuracy guarantees, but likely result in more overhead than the approach of [13]. Which approach is preferable may depend on the setting and on the constraints of the data owner.

Our Contributions
In solving the Approximate HHH problem, there are three metrics that we seek to optimize: the time and space required to process each update and to output the list of approximate HHHs and their estimated frequencies and the quality of the output, in terms of the number of prefixes in the final output and the accuracy of the estimates. Our approach has several advantages over previous work. 3. We obtain a refined analysis of error propagation to achieve better accuracy guarantees and provide non-trivial bounds on the number of HHHs output by our algorithm in one and two dimensions. These bounds were not provided for the algorithms in [6,7,8].
4. The space usage of our algorithm can be fixed a priori, independent of the sum of frequencies N , as it only depends on the number of counters maintained by each instance of Space Saving, which we set at 1 in the absence of assumptions about the data distribution. In contrast, the space usage of the algorithms of [7] and [8] depends on the input stream, and these algorithms dynamically add and prune counters over the course of execution, which can be infeasible in realistic settings.

5.
Our algorithm is conceptually simpler and substantially easier to implement than previous algorithms. We firmly believe programmer time should be viewed as a resource similar to running time and space. We were able to use an off-the-shelf implementation of Space Saving, but this fact notwithstanding, we still spent roughly an order of magnitude less time implementing our algorithms, compared to those from [7,8].
6. Our algorithms extend easily to more restricted settings. For example, we describe in Section 5 how to efficiently implement our algorithms using TCAMs, how to parallelize them, how to apply them to distributed data streams, and how to handle sliding windows or streams with deletions.
We present experimental results showing that for parameters of primary practical interest, our two-dimensional algorithm is superior to existing algorithms in terms of speed and accuracy, and competitive in terms of space, while our one-dimensional algorithm is also superior in terms of speed and accuracy for a more limited range of parameters. In short, we believe our algorithm offers a significantly better combination of simplicity and efficiency than any existing algorithm.

Notation and Definitions
As mentioned above, the theoretical framework developed in this section was described in [8], and considered in several subsequent works [12,23]. In examples throughout this paper, we consider the IP address hierarchy at bytewise granularity: for example, the generalization of 021.132.145.146 by one byte is 021.132.145.*, by two bytes is 021.132.*.*, by three bytes is 021.*.*.*, and by four bytes is *.*.*.*. In two dimensions, we consider pairs of IP addresses, corresponding to source and destination IPs. Each IP prefix that is not fully general in either dimensions has two parents. Define p q by (p ≺ q) ∨ (p = q). The generalization relation defines a lattice structure in the obvious manner. We overload our notation to define the sublattice of a set of elements P as (e P ) ⇐⇒ ∃p ∈ P such that e P . Let H denote the total number of nodes in the lattice: . We call an element fully specified, if it is not the generalization of any other element, e.g. 021.132.145.163 is fully specified. We call an element fully general in dimension i if par(e, i) does not exist. We refer to the unique element that is fully general in all dimensions as the root. For ease of reference, we label each element in the lattice with a vector of length d, whose i'th entry is at most h i , to indicate which lattice node the element belongs to, with the vector corresponding to each fully specified element having i'th entry equal to h i , and the vector corresponding to the root having all entries equal to 0. For example, the element (021.132.145. * , 123.122.121.120) is assigned vector (3,4), and (021. * . * . * , 123.122.121. * ) is assigned vector (1,3). We define Level(i) of the lattice to be the set of labels for which the sum of the entries in the label equals i. We overload terminology and refer to an element p as a member of Level(i) if the label assigned to p is in Level(i). Let L = d i=1 h i denote the deepest level in the hierarchy, that of the fully specified elements.
Definition 2.1. (Heavy Hitters) Given a multiset S of size N and a threshold φ, a Heavy Hitter (HH) is an element whose frequency in S is no smaller than φN . Let f (e) denote the frequency of each element e in S. The set of heavy hitters is HH = {e : f (e) ≥ φN }.
From here on, we assume we are given a multiset S of (fully-specified) elements from a (possibly multidimensional) hierarchical domain of depth L, and a threshold φ. 1. HHH L , the hierarchical heavy hitters at level L, are the heavy hitters of S, that is the fully specified elements whose frequencies exceed φN .
2. Given a prefix p from Level(l), 0 ≤ l < L, define HHH p l+1 to be the set {h ∈ HHH l+1 ∧h ≺ p} i.e. HHH p l+1 is the set of descendants of p that have been identified as HHHs. Define the conditioned count of p to be F p = (e∈S)∧(e p)∧(e HHH p l+1 ) f (e). The set HHH l is defined as 3. The set of exact Hierarchical Heavy Hitters HHH is defined as the set HHH 0 . Finding the set of hierarchical heavy hitters and estimating their frequencies requires linear space to solve exactly, which is prohibitive. Indeed, even finding the set of heavy hitters requires linear space [20], and the hierarchical problem is even more general. For this reason, we study the approximate HHH problem. Definition 2.4. (Approximate HHHs) Given parameter , the Approximate Hierarchical Heavy Hitters problem with threshold φ is to output a set of items P from the lattice, and lower and upper bounds f min (p) and f max (p), such that they satisfy two properties, as follows.
2. Coverage. For all prefixes p, define P p to be the set {q ∈ P : q ≺ p}. Define the conditioned count of p with respect to P to be F p = (e∈S)∧(e p)∧(e Pp) f (e). We require for all prefixes p / ∈ P , F p < φN .
Here a, b, c, d, w, x, y, and z represent some distinct integers between 10 and 255.
Intuitively, the Approximate HHH problem requires outputting a set P such that no prefix with large conditioned count (with respect to P ) is omitted, along with accurate estimates for the unconditioned counts of prefixes in P . One might consider it natural to require accurate estimates of the conditioned counts of each p ∈ P as well, but as shown in [12], Ω(1/φ d+1 ) space would be necessary if we required equally accurate estimates for the conditioned counts, and this can be excessively large in practice.

Our Algorithm, Sketched
Our algorithm utilizes the Space Saving algorithm, proposed by Metwally et al. [18] as a subroutine, so we briefly describe it and some of its relevant properties. As mentioned, Space Saving takes as input a stream of pairs (i, c), where i is an item and c > 0 is a frequency increment for that item. It tracks a small subset T of items from the stream with a counter for each i ∈ T . If the next item i in the stream is in T , its counter is updated appropriately. Otherwise, the item with the smallest counter in T is removed and replaced by i, and the counter for i is set to the counter value of the item replaced, plus c. We now describe guarantees of Space Saving from [2].
Let N be the sum of all frequencies of items in the stream, let m be the number of counters maintained by Space Saving, and for any j < m, let N res(j) denote the sum of all but the top j frequencies. Berinde et al. [2] showed that for any j < m, wheref (i) and f (i) are the estimated and true frequencies of item i, respectively. By setting j = 0, this implies that |f i −f i | ≤ N m , so only 1 counters are needed to ensure error at most N in any estimated frequency. For frequency distributions whose "tails" fall off sufficiently quickly, Space Saving provably requires o( 1 ) space to ensure error at most N (see [2] for more details).
Using a suitable min-heap based implementation of Space Saving, insertions take O(log m) time, and lookups require O(1) time under arbitrary positive counter updates. When all updates are unitary (of the form c = 1), both insertions and lookups can be processed in O(1) time using the Stream Summary data structure [18].
Our algorithm for HHH problems is conceptually simple: it keeps one instance of a Heavy Hitter algorithm at each node in the lattice, and for every update e we compute all generalizations of e and insert each one separately into a different Heavy Hitter data structure. When determining which prefixes to output as approximate HHHs, we start at the bottom level of the lattice and work towards the top, using the inclusion-exclusion principle to obtain estimates for the conditioned counts of each prefix. We output any prefix whose estimated conditioned count exceeds the threshold φN .
We mention that the ideas underlying our algorithm have been implicit in earlier work on HHHs, but have apparently been considered impractical or otherwise inferior to more complicated approaches. Notably, [8] briefly proposes an algorithm similar to ours based on sketches. Their algorithm can handle deletions as well as insertions, but it requires more space and has significantly less efficient output and insertion procedures. Significantly, this algorithm is only mentioned in [8] as an extension, and is not studied experimentally. An algorithm similar to ours is also briefly described in [12] to show the asymptotic tightness of a lower bound argument. Interestingly, they clearly state their algorithm is not meant to be practical. Finally, [6] describes a procedure similar to our one-dimensional algorithm, but concludes that it is both slower and less space efficient than other algorithms. We therefore consider one of our primary contributions to be the identification of our approach as not only practical, but in fact superior in many respects to previous more complicated approaches.
We chose the Space Saving algorithm [18] as our Heavy Hitter algorithm. In contrast, the algorithms of [7,8] are conceptually based on the Lossy Counting Heavy Hitter algorithm [17]. A number of the advantages enjoyed by our algorithm can be traced directly to our choice of Space Saving over Lossy Counting, but not all. For example, the one-dimensional HHH algorithm of [15] is also based on Space Saving, yet our algorithm has better space guarantees.

One-Dimensional Hierarchies
We now provide pseudocode for our algorithm in the one-dimensional case, which is much simpler than the case of arbitrary dimension. As discussed, we use the Space Saving algorithm at each node of the hierarchy, updating all appropriate nodes for each stream element, and then conservatively estimate conditioned counts to determine an appropriate output set.
InitializeHHH() 1 Initialize an instance SS(n) of Space Saving with −1 counters at each node n of the hierarchy.
InsertHHH(element e, count c) 1 /*Line 4 tells the n'th instance of Space Saving to process c insertions of prefix p*/ 2 for all p such that e p 3 Let n be the lattice node that p belongs to 4 UpdateSS(SS(n), p, c) OutputHHH1D(threshold φ) 1 /* par(e) is parent of e*/ 2 Let s e = 0 for all e 3 /*s e conservatively estimates the difference between unconditioned and conditioned counts of e*/ 4 for each e in postorder else s par(e) + = s e Figure 3 illustrates an execution of our one-dimensional algorithm on a stream of IP addresses at byte-wise granularity.  The following lemma is useful in proving that our one-dimensional algorithm satisfies various nice properties.
Proof. By Definition 2.4, Since the hierarchy is one-dimensional, for each e ∈ S such that e P p , there is exactly To prove coverage, we first show by induction that s p = h∈Hp f min (h). This is true at level L because in this case s p = 0 and H p is empty. Suppose the claim is true for all prefixes at level k. Then for p at level k − 1, where the first equality holds by inspection of Lines 5-9 of the output procedure, and the second equality holds by the inductive hypothesis. This completes the induction. By Lemma 3.1, where the inequality holds by the Accuracy guarantees. Coverage follows, since our algorithm is conservative. That is, if item p is not output, then from Line 6 of the output procedure we have We remark that under realistic assumptions on the data distribution, our algorithm satisfies the Accuracy and Coverage requirements using space o( H ). Specifically, [2,Theorem 8] shows that, if the tail of the frequency distribution (i.e. the quantity N res(k) for a certain value of k) is bounded by that of the Zipfian distribution with parameter α, then Space Saving requires space O( − 1 α ) to estimate all frequencies within error N . Notice that if the frequency distribution of the stream itself satisfies this "bounded-tail" condition, then the frequency distributions at higher levels of the hierarchy do as well. Hence our algorithm requires only space O(H − 1 α ) if the tail of the stream is bounded by that of a Zipfian distribution with parameter α.  [18].
To obtain the time bound on output operations, notice that although the pseudocode for procedure OutputHHH1D indicates that we iterate through every possible prefix e, we actually need only iterate over those e tracked by the instance of Space Saving corresponding to e's label. We may restrict our search to these e because, for any prefix e not tracked by the corresponding Space For all prefixes p in the lattice, define the estimated conditioned count of p to be F p := f max (p)− s p . By performing a refined analysis of error propagation, we can bound the number of HHHs output by our one-dimensional algorithm, and use this result to provide Accuracy guarantees on the estimated conditioned counts.
The total number of approximate HHHs output by our one-dimensional algorithm is at most 1 φ−2 . Moreover, the maximum error in the approximate conditioned counts, F p − F p , is at most 1 φ−2 N . Proof. We first sketch why not too many approximate HHHs are output. A prefix p is output if and only if F p > φN , and F p ≥ F p . The key observation is that for each approximate HHH h ∈ P output by our algorithm, h "contributes" error at most N to the estimated conditioned count F p of at most one ancestor p ∈ P of h. Therefore, the total error in the approximate conditioned counts of the output set P is small. Consequently, the sum of the true conditioned counts F p of all p ∈ P is very close to φN |P |, implying that |P | cannot be much larger than N φ since the stream has length N .
We make this argument precise. We showed in proving Theorem 3.2 that for all p, s p = Combining Lemma 3.1 and Equation 2, we see that To show that the sum of the true conditioned counts F p of all p ∈ P is very close to φN |P |, we use By the Accuracy guarantees, p∈P f max (p)−f (p) is at most |P | N . To bound p∈P h∈Hp f (h)− f min (h) , we observe that for any item h ∈ P , h ∈ H p for at most one ancestor p ∈ P (because in one dimension, if h ≺ p and h ≺ p for distinct p, p ∈ P , then either p ≺ p or p ≺ p, contradicting the fact that h ∈ H p and h ∈ H p ). Combining this fact with the Accuracy guarantees, we again obtain an upper bound of |P | N . In summary, we have shown that Since the total length of the stream is N , and in one dimension each fully specified item contributes its count to F p for at most one p, it follows that p∈P F p ≤ N and hence |P | ≤ 1 φ−2 as claimed. Lastly, we bound the maximum error F p − F p in any estimated conditioned count. We showed that which, by the Accuracy guarantees, is at most The upper bound on output size provided in Theorem 3.4 is very nearly tight, as there may be

Two-Dimensional Hierarchies
In moving from one to multiple dimensions, only the output procedure must change. In one dimension, discounting items that were already output as HHHs was simple. There was no doublecounting involved, since no two children of an item p had common descendants. To deal with the double-counting, we use the principle of inclusion-exclusion in a manner similar to [8] and [7]. At a high level, our two-dimensional output procedure works as follows. As before, we start at the bottom of the lattice, and compute HHHs one level at a time. For any node p, we have to estimate the conditioned count for p by discounting the counts of items that are already output as HHHs. However, Lemma 3.1 no longer holds: it is not necessarily true that F p = f max (p) − q∈Hp f (q) in two or more dimensions, because for fully specified items that have two or more ancestors H p , we have subtracted their count multiple times. Our algorithm compensates by adding these counts back into the sum.
Before formally presenting our two-dimensional algorithm, we need the following theorem. Let glb(h, h ) denote the greatest lower bound of h and h , that is, the unique common descendant q of h and h satisfying ∀p : (q p) ∧ (p h) ∧ (p h ) =⇒ p = q. In the case where h and h have no common descendants, the we treat glb(h, h ) as the "trivial item" which has count 0.
Theorem 4.1. In two dimensions, let T p be the set of all q expressible as the greatest lower bound of two distinct elements of H p , but not of 3 or more distinct elements in H p . Then The proof appears in Appendix A. Below, we give pseudocode for our two-dimensional output procedure. We compute estimated conditioned counts F p = f max (p) − h 1 ∈Hp f min (h 1 ) + q∈Tp f max (q). As in the one-dimensional case, the Accuracy guarantees of the algorithm follow immediately from those of Space Saving. Coverage requirements are satisfied by combining Theorem 4.1 with the Accuracy guarantees.
Our two-dimensional algorithm performs each insert operation in O(H log 1 ) time under arbitrary updates, and O(H) time under unitary updates, just as in the one-dimensional case. Although the output operation is considerably more expensive in the multi-dimensional case, experimental results indicate that this operation is not prohibitive in practice (see Section 6).

OutputHHH2D(threshold φ)
1 P = ∅ 2 for level l=L downto 0 3 for each item p at level l 4 Let n be the lattice node that p belongs to 5 (f min (p), f max (p))=GetEstimateSS(SS(n), p) 6 F print(p, f min (p), f max (p)) Using Theorem 4.1, we obtain a non-trivial upper bound on the number of HHHs output by our two-dimensional algorithm. The proof is in Appendix A.
Theorem 4.2. Let A = 1 + min(h 1 , h 2 ), where h i is the depth of dimension i of the lattice. For small enough , the number of approximate HHHs output by our two-dimensional algorithm is at most The error guarantee obtained from Theorem 4.2 appears messy, but yields useful bounds in many realistic settings. For example, for IP addresses at byte-wise granularity, A = 5. Plugging in φ = .1, = 10 −4 yields |P | ≤ 53, which is very close to the maximum number of exact HHHs: A/φ = 50. As further examples, setting φ = .05 and = 10 −5 yields a bound of |P | ≤ 102, and setting φ = .01 and = 10 −6 yields a bound of |P | ≤ 536, both of which are reasonably close to A φ . Of course, the bound of Theorem 4.2 should not be viewed as tight in practice, but rather as a worst-case guarantee on output size.
Higher Dimensions. In higher dimensions, we can again keep one instance of Space Saving at each node of the hierarchy to compute estimates f min (p) and f max (p) of the unconditioned count of each prefix p. We need only modify the Output procedure to conservatively estimate the conditioned count of each prefix.
We can show that the natural generalization of Theorem 4.1 does not hold in three dimensions. However, we can compute estimated conditioned sublattice counts F p as Inclusion-exclusion implies that, in any dimension, F p ≤ F p , and hence by outputting p if F p ≥ φN we can satisfy Coverage.

Extensions
Our algorithms are easily adopted to distributed or parallel settings, and can be efficiently implemented in commodity hardware such as ternary content addressable memories.
Distributed Implementation. In many practical scenarios a data stream is distributed across several locations rather than localized at a central node (see, e.g., [16,21]). For example, multiple sensors may be distributed across a network. We extend our algorithms to this setting.
Multiple independent instances of Space Saving can be merged to obtain a single summary of the concatenation of the distributed data streams with only a constant factor loss in accuracy, as shown in [2]. We use this form of their result: Theorem 11], simplified statement): Given summaries of k distributed data streams produced by k instances of Space Saving each with 1 counters, a summary of the concatenated stream can be obtained such that the error in any estimated frequency is at most 3 N , where N is the length of the concatenated stream.
To handle k distributed data streams, we may simply run one instance of our algorithm independently on each stream (with 3 counters each), and afterward, for each node in the lattice, merge all k corresponding instances of Space Saving into a single instance. After the merge, we have a single instance of Space Saving for each node in the lattice that has essentially the same error guarantees (up to a small constant factor) as a centralized implementation. Our output procedure is exactly as in the centralized implementation.
Parallel Implementation. In all of our algorithms, the update operation involves updating a number of independent Space Saving instances. It is therefore trivial to parallelize this algorithm. We have parallelized this algorithm using OpenMP. Our limited experiments show essentially linear speedup, up to the point where we reach the limitation of the shared memory constraint.
TCAM Implementation. Recently, there has been an effort to develop network algorithms that utilize Ternary Content Addressable Memories, or TCAMs, to process streaming queries faster. TCAMs are specialized, widely deployed hardware that support constant-time queries for a bit vector within a database of ternary vectors, where every bit position represents 0, 1 or * . The * is a wild card bit matching either a 0 or a 1. In any query, if there is one or more match, the address of the highest-priority match is returned. Previous work describes a TCAM-based implementation of Space Saving for unitary updates, and shows experimentally that it is several times faster than software solutions [1].
Since our algorithms require the maintenance of H independent instances of Space Saving, it is easy to see that our algorithms can be implemented given access to H separate TCAMs, each requiring just a few KBs of memory. With more effort, we can devise implementations of our algorithms that use just a single commodity TCAM. Commodity TCAMs can store hundreds of thousands or millions of data entries [1], and therefore a single TCAM can store tens of instances of Space Saving even when = .0001.
Our simplest TCAM-based implementation takes advantage of the fact that TCAMs have extra bits. A typical TCAM has a width of 144 symbols allotted for each entry in the database, and this typically leaves several dozen unused symbols for each entry. The implementation of Space Saving of [1] uses extra bits to store frequencies, but we can use additional unused bits to identify the instance of Space Saving associated with each item in the database.
For illustration, consider the one-dimensional byte-wise IP hierarchy. We associate two extra bits with each entry in the database: 00 will correspond to the top-most level of the hierarchy, 01 to the second level, 10 to the third, and 11 to the fourth. Then we treat each IP address a.b.c.d as four separate searches: a.b.c.d.00, a.b.c. * .01, a.b. * . * .10, and a. * . * . * .11, thereby updating each ancestor of a.b.c.d in turn. The TCAM needs to store the smallest counter for each of the four Space Saving instances, and otherwise the TCAM-based implementation from [1] is easily modified to handle multiple instances of Space Saving on a single TCAM.
Alternatively, we could compute approximate unconditioned counts by keeping a single instance of Space Saving with (item, mask) pairs as keys, rather than H separate instances of Space Saving. It is clear that this approach still satisfies the Accuracy guarantees for each prefix, and has the advantage of only having to store the smallest counter for a single instance of Space Saving.
Sliding Windows and Streams with Deletions. Our algorithms as described only work for insert-only streams, due to our choice of Space Saving as our heavy hitter algorithm. However, the accuracy and coverage guarantees of our HHH algorithms still hold even if we replace Space Saving with other heavy hitter algorithms. This is because our proofs of accuracy and coverage applied the inclusion-exclusion principle to express conditioned counts in terms of unconditioned counts, and then used the fact that our heavy hitter algorithm provides accurate estimates on the unconditioned counts; this analysis is independent of the heavy hitter algorithm used. Hence we can extend our results to additional scenarios by using other algorithms.
For example, it may be desirable to compute HHHs over only a sliding window of the last n items seen in the stream. [14] presents a deterministic algorithm for computing -approximate heavy hitters over sliding windows using O(1/ ) space. Thus, by replacing Space Saving with this algorithm, we obtain an algorithm that computes approximate HHHs over sliding windows using space O(H/ ), which asymptotically matches the space usage of our algorithm. However, it appears this algorithm is markedly slower and less space-efficient in practice.
Similarly, many sketch-based heavy hitter algorithms such as that of [9] can compute -approximate heavy hitters, even in the presence of deletions, using space O( 1 log N ). By replacing Space Saving with such a sketch-based algorithm, we obtain a HHH algorithm using space O( H log N ) that can handle streams with deletions. (As noted previously, this variation was mentioned in [8].)

Experimental Results
We have implemented two versions of our algorithm in C and tested it using GCC version 4.1.2 on a host with four single-core 64-bit AMD Opteron 850 processors each running at 2.4GHz with a 1MB cache and 8GB of shared memory. The first version -termed hhh below -uses a heap-based implementation of Space Saving that can handle arbitrary updates, while the second versiontermed unitary below -uses the Stream Summary data structure and can only handle unitary updates. Both versions use an off-the-shelf implementation from [4] for Space Saving; further optimizations, as well as different tradeoffs between time and space, may be possible by modifying the off-the-shelf implementation. We have used a real packet trace from www.caida.org [3] in all experiments below. (We have tried other traces to confirm that these results are demonstrative. Note that all of our graphs are in color and may not display well in grayscale.) Throughout our experiments, all algorithms define the frequency of an IP address or an IP address pair to be the number of packets associated with that item, as opposed to the number of bytes of raw data. This ensures that all algorithms (including unitary) process exactly the same updates. Consequently, the stream length N in all of our experiments refers to the number of packets in the stream (i.e. the prefix of the packet trace [3] that we used).
We tested our algorithms at both byte-wise and bit-wise granularities in one and two dimensions. Bit-wise hierarchies are more expensive to handle, as H, the number of nodes in the lattice structure implied by the hierarchy, becomes much larger. However, it may be useful to track approximate HHHs at bit-wise granularity in many realistic situations. For example, a single entity might control a subnet of IP addresses spanning just a few bits rather than an entire byte. However, we observed similar (relative) behavior between all algorithms at both bit-wise and byte-wise granularity, and thus we display results only for byte-wise hierarchies for succinctness.
For comparison we also implemented the full and partial ancestry algorithms from [8], labeled full and partial respectively. We compare the algorithms' performance in several respects: time and memory usage, the size of the output set, and the accuracy of the unconditioned count estimates. Our algorithm performs at least as well as the other two in terms of output size and accuracy. Except for extremely small values of (less than about .0001), which correspond to extremely high accuracy guarantees, our two-dimensional algorithm is also significantly faster (more than three times faster for some parameter settings of high practical interest). Our one-dimensional algorithm is also faster than its competitors for values of greater than about .01, and competitive across all values of . Our algorithm uses slightly more memory than its competitors. Below, we discuss each aspect separately.
In summary, our one-dimensional algorithm is competitive in practice with existing solutions, and possesses other desirable properties that existing solutions lack, such as improved simplicity and ease of implementation, improved worst-case guarantees, and the ability to preallocate space even without knowledge of the stream length. Our two-dimensional algorithm possesses all of the same desirable properties, and is also significantly faster than existing solutions for parameter values of primary practical interest. The primary disadvantage of our algorithms is slightly increased space usage.
All of our implementations are available online at [19].
Memory. Both versions of our algorithm use more memory than full and partial. The difference between hhh, partial, and full is a small constant factor; unitary uses about twice as much space as hhh. The largest difference in space usage appears in one dimension, as shown in Figure 4a. The difference is much smaller in two dimensions, as shown in Figure 4b. In both cases, the better space usage of partial comes at the cost of significantly decreased accuracy and increased output size, as discussed below. We conclude that in situations where the decreased accuracy of partial cannot be tolerated, the memory usage of our algorithms is not a major disadvantage, as hhh and full have similar memory requirements, especially in two dimensions. Ideally, we would be able to present our results with the independent variable a programmerlevel object such as memory usage, rather than the error-parameter . In practice, a programmer may be allowed at most 1 MB of space to deploy an HHH algorithm on a network sensor, and have to optimize speed and accuracy subject to this constraint. But while the mapping between and memory usage is straightforward for our algorithm (the Space Saving implementation uses 36 bytes per counter, and we use a fixed H counters), this mapping is less clear for partial and full, as their space usage is data dependent, with counters added and pruned over the course of execution. Figures 4c and 4d show the empirical mapping between space usage and for a fixed stream length           of N = 30 million with one-and two-dimensional bytewise hierarchies. This setting highlights the importance of our improved worst-case space bounds, even though our algorithm uses slightly more space in practice. It can be imperative to guarantee assigned memory will not be exceeded, and our algorithm allows a more aggressive choice of error parameter while maintaining a worst-case guarantee.
We emphasize that we did not attempt to optimize memory usage for our algorithms using characteristics of the data, as suggested in Theorem 3.2. It is therefore likely that our algorithms can function with less memory than partial and full in many practical settings.
Note that the running time and memory usage are independent of φ, as φ only affects the output stage, which we have not included in our measurements as the resources consumed by this stage were negligible.
Time. We observe that in both one and two dimensions, both unitary and hhh are faster than partial and full except for extremely small values of . The speed of each algorithm for each setting of is illustrated for a fixed stream length of N = 30 million in Figures 4k and 4l; our algorithms are fastest in one dimension for greater than about .01 and in two dimensions for greater than about .0001.
We show how runtime grows with stream length for fixed values of in Figures 4e-4j. For concreteness, on a stream with N = 250 million, unitary processes about 2.2 million updates per second in one dimension at a byte-wise granularity when = .1, while hhh processes 1.85 million, partial 1.3 million, and full processes 1.4 million. Here, N corresponds to the number of packets (not weighted by size), and the updates per second statistic specifies the number of packets processed per second by our implementation. In two dimensions for = .1, unitary processes over 370, 000 updates per second and hhh processes 300, 000, while partial processes 71, 000, and full processes about 100, 000. Thus, our algorithms ran more than three times faster than partial and full for this particular setting of parameters. Output Size. The output size and accuracy are measures of the quality of output and depend on the value of φ. All three algorithms produce a near-optimal output size, with partial consistently outputting the largest sets. The largest difference observed is shown in Figure 5a.
Accuracy. We define the relative error of the output to be max p∈output fmax(p)−f min (p) N . Clearly, the relative error is between 0 and 1, because of the accuracy guarantees of our algorithms. We find that the relative error can vary significantly for all algorithms, but our algorithm uniformly performs best. The relative error of the partial ancestry algorithm is often close to the theoretical upper bound of 1, making it by far the least accurate of the algorithms tested.
TCAM Simulations. We simulated our TCAM-conscious implementation of our algorithm on the same packet traces as above, in order to estimate the number of TCAM operations our implementation requires per packet processed. [1] experimentally demonstrates that TCAM READ, WRITE, and SEARCH operations all take roughly the same amount of time. Thus, we counted the total number of READ, WRITE, and SEARCH operations our TCAM implementation required, without distinguishing between the three. We found that for one-dimensional IP addresses at byte-wise granularity, each packet required about 14 TCAM operations on average, or 2.8 TCAM operations per instance of Space Saving maintained by our algorithm. This is slightly better than the worstcase behavior of the implementation of [1], which requires up to 4 TCAM operations per update. Our two-dimensional algorithm at byte-wise granularity requires about 65 TCAM operations per packet; since the two-dimensional algorithm maintains 25 instances of Space Saving, this translates to only 2.6 TCAM operations per instance of Space Saving. We attribute this improvement in TCAM operations per instance of Space Saving to the fact that the frequency distribution at high nodes in the two-dimensional lattice is highly non-uniform.

Conclusion
The trend in the literature on the approximate HHH problem has been towards increasingly complicated algorithms. In this work, we present what is perhaps the simplest algorithm for HHHs in arbitrary dimension, and demonstrate that it is superior to the existing standard in many respects, and competitive in all others. We believe our algorithm offers the best tradeoff between simplicity and performance.
it is now clear that f (u) will appear once in the sum over pairs, m − 2 times in the sum over triples, and, in general, m−2 k times in the sum over groups of size k + 2. When combined with the sign structure in the sum this gives a resulting contribution from u of Thus in the two dimensional case as claimed.
Proof of Theorem 4.2. The proof will closely parallel that of Theorem 3.4. We bound the total error in the estimated conditioned counts, aggregated over all p ∈ P , and this will imply that the sum of the true conditioned counts of all p ∈ P is large. Hence there cannot be too many approximate HHHs output. We showed in Theorem 4.1 that Therefore, Our goal is to show that the sum of the true conditioned counts of all p ∈ P is large by bounding the total error in the estimated conditioned counts, aggregated over all p ∈ P . To this end, consider the sum We refer to the second term on the right hand side of the last expression, p∈P f max (p)−f (p) , as "Term-Two error", the third term, p∈P h 1 ∈Hp f (h 1 )−f min (h 1 ) , as "Term-Three" error, and the fourth term, p∈P q∈Tp f max (q) − f (q) as "Term-Four error". By the Accuracy guarantees, it is immediate that the Term-Two error is bounded above by |P | N .
In order to bound Term-Three error, we must briefly introduce the notion of comparable items in a lattice. Two elements x and y are comparable under the relation if the label of y is less than or equal to that of x on every attribute. Let A be the size of the largest antichain in the lattice, that is, the maximum size of any subset of prefixes such that any two items in the subset are incomparable. It was shown in [8] that A = 1 + min(h 1 , h 2 ). We show that p∈P |H p | ≤ A|P |; it then follows by the Accuracy guarantees that the Term-Three error is bounded above by |P |A N .