The frequency moments of a sequence containing m~ele-ments of type i, for 1~z~n, are the numbers F& =~= 1 m?. We consider the space complexity of randomized algorithms that approximate the numbers Fk, when the elements of the sequence are given one by one and cannot be stored. Surprisingly, it turns out that the numbers FO, F1 and F2 can be approximated in logarithmic space, whereas the approximation of F& for k~6 requires nQ(l) space. Applications to data bases are mentioned as well. and Fz is the repeat rate or Gini's index of homogeneity needed in order to compute the surprise imdez of the sequence (see, e.g., [11] Permission to mske dlgitellhard copies of all or part of thk material for personal or classroom use is granted without fee provided that the copiee are not~de or dktributed for profit or commercial advantage, the copyright notwe, the title of tbe publication and its date appesr, and notice is. given that copyright is by permission of the ACM, Inc. memory bits, a number Y so that the probability that Y deviates from Fk by more than~Fk as at most c. Proof. Without trying to optimize our absolute constants, define .SI = 'kn~~"k and 52 = 2 lg(l/c). (To simplify the presentation we omit, from now on, all floor and ceiling signs whenever these are not essential). We first assume the length of the sequence m is known in advance, and then comment on the required modifications if this is not the case. Note that in order to compute X we only need to maintain the lg n bits representing a~= 1 and the lg m bits representing the number of occurrences of 1. The expected value E(X) of X is, by definition, To estimate the variance Var(X) = E(X2) -(E(X))2 of X we bound E(X2); where the first ineq. is obtained from the following inequality which holds for any numbers a > b >0: We need the following simple inequality: (Note that the sequence ml = n~lk, mz = . = m~= 1 shows that this is tight, up to a constant factor. )
Theoretical work REF proved that it is possible to estimate items frequency to a constant factor in logarithmic space.
The space complexity of approximating the frequency moments
STOC '96
Mathematics Computer Science