Linear Time Voting Algorithm. I don't get it

前端 未结 5 660
南方客
南方客 2020-12-07 20:31

As I was reading this (Find the most common entry in an array), the Boyer and Moore\'s Linear Time Voting Algorithm was suggested.

If you follow the link to the si

相关标签:
5条回答
  • 2020-12-07 20:58

    When the test case is "AAACCBB", the set has no majority. Because no element occurs more than 3 times since the length of "AAACCBB" is 7.

    Here's the code for "the Boyer and Moore's Linear Time Voting Algorithm":

    int Voting(vector<int> &num) {
            int count = 0;
            int candidate;
    
            for(int i = 0; i < num.size(); ++i) {
                if(count == 0) {
                    candidate = num[i];
                    count = 1;
                }
                else
                    count = (candidate == num[i]) ? ++count : --count;
            }
            return candidate;
        }
    
    0 讨论(0)
  • 2020-12-07 21:09

    Small but an important addition to the other explanations. Moore's Voting algorithm has 2 parts -

    1. first part of running Moore's Voting algorithm only gives you a candidate for the majority element. Notice the word "candidate" here.

    2. In the second part, we need to iterate over the array once again to determine if this candidate occurs maximum number of times (i.e. greater than size/2 times).

    First iteration is to find the candidate & second iteration is to check if this element occurs majority of times in the given array.

    So time complexity is: O(n) + O(n) ≈ O(n)

    0 讨论(0)
  • 2020-12-07 21:12

    I wrote a C++ code for this algorithm

    char find_more_than_half_shown_number(char* arr, int len){
    int i=0;
    std::vector<int> vec;
    while(i<len){
        if(vec.empty()){     
            vec.push_back(arr[i]);
            vec.push_back(1);
        }else if(vec[0]==arr[i]){ 
            vec[1]++;
        }else if(vec[0]!=arr[i]&&vec[1]!=0){
            vec[1]--;
        }else{                   
            vec[0]=arr[i];
        }
        i++;
    }
    int tmp_count=0;
    for(int i=0;i<len;i++){
        if(arr[i]==vec[0])
            tmp_count++;
    }
    if(tmp_count>=(len+1)/2)
        return vec[0];
    else
        return -1;
    }
    

    and the main function is as below:

    int main(int argc, const char * argv[])
    {
        char arr[]={'A','A','A','C','C','B','B','C','C','C','B','C','C'};
        int len=sizeof(arr)/sizeof(char);
        char rest_num=find_more_than_half_shown_number(arr,len);
        std::cout << "rest_num="<<rest_num<<std::endl;
        return 0;
    }
    
    0 讨论(0)
  • 2020-12-07 21:16

    The algorithm only works when the set has a majority -- more than half of the elements being the same. AAACCBB in your example has no such majority. The most frequent letter occurs 3 times, the string length is 7.

    0 讨论(0)
  • 2020-12-07 21:21

    From the first linked SO question:

    with the property that more than half of the entries in the array are equal to N

    From the Boyer and Moore page:

    which element of a sequence is in the majority, provided there is such an element

    Both of these algorithms explicitly assume that one element occurs at least N/2 times. (Note in particular that "majority" is not the same as "most common.")

    0 讨论(0)
提交回复
热议问题