Jump to content

Boyer–Moore majority vote algorithm

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Baiyubin (talk | contribs) at 14:23, 26 December 2014 (Created page with 'Boyer-Moore Vote Algorithm solves the majority vote problem in linear time (O(n)) and constant memory (O(1)). The majority vote problem is to determine in any gi...'). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
(diff) ← Previous revision | Latest revision (diff) | Newer revision → (diff)

Boyer-Moore Vote Algorithm solves the majority vote problem in linear time (O(n)) and constant memory (O(1)). The majority vote problem is to determine in any given sequence of choices whether there is a choice with more occurrences than all the others, and if so, to determine this choice. Mathematically, given a finite sequence (length n) of numbers, find the majority number defined as the number that appears more than ⌊ n/2 ⌋ times.

Description

The algorithm is carried out in two steps.

1. Eliminate all elements except one.

   As we iterate through the array of numbers, we maintain a current candidate and a counter initialized to 0. 
   With the current element x in iteration, we update the counter and (possibly) the candidate:
   If the counter is 0, we set the current candidate to x and the counter to 1.
   If the counter is not 0, we increment or decrement the counter based on whether x is the current candidate.

2. Determine if the remaining element is a valid majority element.

   With the candidate acquired in step 1. Iterate through the array of numbers and count it's occurrences. Determine if the result is more than half of the sequence's length. If so, the candidate is the majority, otherwise the sequence doesn't contain a majority. 

[1]