The Boyer–Moore majority vote algorithm is an algorithm for finding the majority of a sequence of elements using linear time and a constant number of words of memory. It is named after Robert S. Boyer and J Strother Moore, who published it in 1981,[1] and is a prototypical example of a streaming algorithm.
In its simplest form, the algorithm finds a majority element, if there is one: that is, an element that occurs repeatedly for more than half of the elements of the input. A version of the algorithm that makes a second pass through the data can be used to verify that the element found in the first pass really is a majority.[1]
If a second pass is not performed and there is no majority the algorithm will not detect that no majority exists. In the case that no strict majority exists, the returned element can be arbitrary; it is not guaranteed to be the element that occurs most often (the mode of the sequence). It is not possible for a streaming algorithm to find the most frequent element in less than linear space, for sequences whose number of repetitions can be small.[2]
Description
editThe algorithm maintains in its local variables a sequence element and a counter, with the counter initially zero. It then processes the elements of the sequence, one at a time. When processing an element x, if the counter is zero, the algorithm stores x as its remembered sequence element and sets the counter to one. Otherwise, it compares x to the stored element and either increments the counter (if they are equal) or decrements the counter (otherwise). At the end of this process, if the sequence has a majority, it will be the element stored by the algorithm. This can be expressed in pseudocode as the following steps:
- Initialize an element m and a counter c with c = 0
- For each element x of the input sequence:
- If c = 0, then assign m = x and c = 1
- else if m = x, then assign c = c + 1
- else assign c = c − 1
- Return m
Even when the input sequence has no majority, the algorithm will report one of the sequence elements as its result. However, it is possible to perform a second pass over the same input sequence in order to count the number of times the reported element occurs and determine whether it is actually a majority. This second pass is needed, as it is not possible for a sublinear-space algorithm to determine whether there exists a majority element in a single pass through the input.[3]
Analysis
editThe amount of memory that the algorithm needs is the space for one element and one counter. In the random access model of computing usually used for the analysis of algorithms, each of these values can be stored in a machine word and the total space needed is O(1). If an array index is needed to keep track of the algorithm's position in the input sequence, it doesn't change the overall constant space bound. The algorithm's bit complexity (the space it would need, for instance, on a Turing machine) is higher, the sum of the binary logarithms of the input length and the size of the universe from which the elements are drawn.[2] Both the random access model and bit complexity analyses only count the working storage of the algorithm, and not the storage for the input sequence itself.
Similarly, on a random access machine the algorithm takes time O(n) (linear time) on an input sequence of n items, because it performs only a constant number of operations per input item. The algorithm can also be implemented on a Turing machine in time linear in the input length (n times the number of bits per input item).[4]
Correctness
editAfter processing n input elements, the input sequence can be partitioned into (n−c) / 2 pairs of unequal elements, and c copies of m left over. This is a proof by induction; it is trivially true when n = c = 0, and is maintained every time an element x is added:
- If x = m, add it to the set of c copies of m (and increment c).
- If x ≠ m and c > 0, then remove one of the c one copies of m from the left-over set and pair it with the final value (and decrement c).
- If c = 0, then set m ← x and add x to the (previously empty) set of copies of m (and set c to 1).
In all cases, the loop invariant is maintained.[1]
After the entire sequence has been processed, it follows that no element x ≠ m can have a majority, because x can equal at most one element of each unequal pair and none of the remaining c copies of m. Thus, if there is a majority element, it can only be m.[1]
See also
edit- Element distinctness problem, the problem of testing whether a collection of elements has any repeated elements
- Majority function, the majority of a collection of Boolean values
- Majority problem (cellular automaton), the problem of finding a majority element in the cellular automaton computational model
- Misra–Gries heavy hitters algorithm and Misra–Gries summary, a natural generalization of the Boyer–Moore majority vote algorithm that stores more than one item and more than one count
References
edit- ^ a b c d Boyer, R. S.; Moore, J S. (1991), "MJRTY - A Fast Majority Vote Algorithm" (PS), in Boyer, R. S. (ed.), Automated Reasoning: Essays in Honor of Woody Bledsoe, Automated Reasoning Series, Dordrecht, The Netherlands: Kluwer Academic Publishers, pp. 105–117, doi:10.1007/978-94-011-3488-0_5, DTIC ADA131702. Originally published as a technical report in 1981.
- ^ a b Trevisan, Luca; Williams, Ryan (January 26, 2012), "Notes on streaming algorithms" (PDF), CS154: Automata and Complexity, Stanford University.
- ^ Cormode, Graham; Hadjieleftheriou, Marios (October 2009), "Finding the frequent items in streams of data" (PDF), Communications of the ACM, 52 (10): 97–105, doi:10.1145/1562764.1562789, S2CID 823439,
no algorithm can correctly distinguish the cases when an item is just above or just below the threshold in a single pass without using a large amount of space
. - ^ Eppstein, David (October 1, 2016), "Voting on a Turing machine using constant-amortized-time counters", 11011110, retrieved 2023-12-31