Boyer-Moore majority vote algorithm(摩尔投票算法)

简介

Boyer-Moore majority vote algorithm(摩尔投票算法)是一种在线性时间O(n)和空间复杂度的情况下,在一个元素序列中查找包含最多的元素。它是以Robert S.Boyer和J Strother Moore命名的,1981年发明的,是一种典型的流算法(streaming algorithm)。

在它最简单的形式就是,查找最多的元素,也就是在输入中重复出现超过一半以上(n/2)的元素。如果序列中没有最多的元素,算法不能检测到正确结果,将输出其中的一个元素之一。

当元素重复的次数比较小的时候,对于流算法不能在小于线性空间的情况下查找频率最高的元素。

算法描述

算法在局部变量中定义一个序列元素(m)和一个计数器(i),初始化的情况下计数器为0. 算法依次扫描序列中的元素,当处理元素x的时候,如果计数器为0,那么将x赋值给m,然后将计数器(i)设置为1,如果计数器不为0,那么将序列元素m和x比较,如果相等,那么计数器加1,如果不等,那么计数器减1。处理之后,最后存储的序列元素(m),就是这个序列中最多的元素。

如果不确定是否存储的元素m是最多的元素,还可以进行第二遍扫描判断是否为最多的元素。

perudocode

  • Initialize an element m and a counter i with i = 0
  • For each element x of the input sequence:
    • if i = 0, then assign m = x and i = 1
    • else if m = x, then assign i = i + 1
    • else assign i = i − 1
  • Return m

算法举例

Given an array of size n, find the majority element. The majority element is the element that appears more than ⌊ n/2 ⌋ times.

You may assume that the array is non-empty and the majority element always exist in the array.

实现代码

  1. class Solution {
  2. public:
  3. // moore majority vote algorithm
  4. int majorityElement(vector<int>& nums) {
  5. int m;
  6. int count = 0;
  7. for (int i = 0; i < nums.size(); i++) {
  8. if (count == 0) {
  9. m = nums[i];
  10. count++;
  11. } else if (nums[i] == m) {
  12. count++;
  13. } else
  14. count--;
  15. }
  16. return m;
  17. }
  18. };

还有一个类似的算法题,就是判断一个序列中,某个元素的个数是否超过n/2,其中一种解法就是利用分治算法。还可以用上面找到的摩尔投票算法,第一遍扫描输出一个存储的元素,然后还需要进行第二遍扫描来判断元素在序列中是否确实超过n/2了。 因为一个元素超过一半,最后肯定会留下,但是最后留下的不一定超过一半,所以要扫描第二遍。

代码实现

  1. #include <iostream>
  2. #include <vector>
  3. #include <cmath>
  4. using namespace std;
  5. class Solution {
  6. public:
  7. // divide and conquer
  8. int majorityElement(vector<int>& nums, int majority) {
  9. if (nums.size() <= 2) {
  10. int num = 0;
  11. for (int i = 0; i < nums.size(); i++) {
  12. if (nums[i] == majority)
  13. num++;
  14. }
  15. return num;
  16. }
  17. int middle = floor(nums.size() / 2);
  18. vector<int> left(nums.begin(), nums.begin() + middle);
  19. vector<int> right(nums.begin() + middle, nums.end());
  20. int left_num = majorityElement(left, majority);
  21. int right_num = majorityElement(right, majority);
  22. return left_num + right_num;
  23. }
  24. // moore majority vote algorithm
  25. // 判断majority 是否大于一般以上,不含等于。
  26. int moore_majority_vote_algorithm(vector<int>& nums, int majority) {
  27. int m;
  28. int counter = 0;
  29. // 第一轮扫描
  30. for (int i = 0; i < nums.size(); i++) {
  31. if (counter == 0) {
  32. counter = 1;
  33. m = nums[i];
  34. } else if (m != nums[i]) {
  35. counter--;
  36. } else
  37. counter++;
  38. }
  39. if (m != majority)
  40. return -1;
  41. int new_counter = 0;
  42. // 第二轮扫描
  43. for (int i = 0; i < nums.size(); i++) {
  44. if (nums[i] == m)
  45. new_counter++;
  46. }
  47. return new_counter;
  48. }
  49. };
  50. int main() {
  51. int num[] = {2, 2, 1, 1, 1, 2};
  52. vector<int> vec(num, num + 6);
  53. Solution* solution = new Solution();
  54. int counter;
  55. cout << (counter = solution->majorityElement(vec, 2)) << endl;
  56. //cout << (counter = solution->moore_majority_vote_algorithm(vec, 2)) << endl;
  57. if (counter > floor(vec.size() / 2)) {
  58. cout << "Yes" << endl;
  59. } else
  60. cout << "No" << endl;
  61. return 0;
  62. }

Moore majority vote algorithm(摩尔投票算法)的更多相关文章

  1. Boyer and Moore Fast majority vote algorithm(快速选举算法)

    问题来来自于leetcode上的一道题目,https://leetcode.com/problems/majority-element/,大意是是找出一个数组中,出现次数超过一个半的数字,要求是O(n ...

  2. 洛谷 P2397:yyy loves Maths VI (mode)(摩尔投票算法)

    题目背景 自动上次redbag用加法好好的刁难过了yyy同学以后,yyy十分愤怒.他还击给了redbag一题,但是这题他惊讶的发现自己居然也不会,所以只好找你 题目描述 [h1]udp2:第一题因为语 ...

  3. leetcode 229. Majority Element II(多数投票算法)

    就是简单的应用多数投票算法(Boyer–Moore majority vote algorithm),参见这道题的题解. class Solution { public: vector<int& ...

  4. LeetCode 169. Majority Element - majority vote algorithm (Java)

    1. 题目描述Description Link: https://leetcode.com/problems/majority-element/description/ Given an array ...

  5. 摩尔投票算法( Boyer-Moore Voting Algorithm)

    一.Majority Element题目介绍:给定一个长度为n的数组的时候,找出其中的主元素,即该元素在数组中出现的次数大于n/2的取整.题目中已经假定所给的数组一定含有元素,且主元素一定存在.一下是 ...

  6. Boyer-Moore Majority Vote Algorithm

    介绍算法之前, 我们来看一个场景, 假设您有一个未排序的列表.您想知道列表中是否存在一个数量占列表的总数一半以上的元素, 我们称这样一个列表元素为 Majority 元素.如果有这样一个元素, 求出它 ...

  7. A Linear Time Majority Vote Algorithm

    介绍一种算法,它可以在线性时间和常数空间内,在一个数组内找出出现次数超过一半的某个数字. 要解决这个问题并不难,可以使用排序或哈希,但是这两种算法都不能同时满足时间或空间的要求. 然而,该算法(A L ...

  8. leetcode 169. Majority Element 多数投票算法(Boyer-Moore Majority Vote algorithm)

    题目: Given an array of size n, find the majority element. The majority element is the element that ap ...

  9. LeetCode题解-----Majority Element II 摩尔投票法

    题目描述: Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. The a ...

随机推荐

  1. Mac下安装apk(命令形式)

    1 连接上设备 2 卸载原有程序 3 进入adb程序目录/Applications/adt-bundle-mac-x86_64-20131030/sdk/platform-tools 4 键入命令 . ...

  2. 延迟任务和循环任务ScheduledExecutorService

    public class ScheduledThreadPool { public static ScheduledExecutorService scheduledThreadPool = Exec ...

  3. 关于增强for循环和普通for循环是否需要判断为null的情况

    1.增强for循环: public static void main(String[] args) { List<Object> list = null; for(Object s : l ...

  4. Java 集合框架之 JDK 1.5 新特性

    forEach 循环 多用于元素迭代. 适用范围: - 数组 - 实现 Iterable 接口的集合类 格式: for(类型 变量 : Collection 集合 | 数组) { } 传统 for 和 ...

  5. 贪玩ML系列之一个BP玩一天

    手写串行BP算法,可调batch_size 既要:1.输入层f(x)=x  隐藏层sigmoid 输出层f(x)=x 2.run函数实现单条数据的一次前馈 3.train函数读入所有数据for循环处理 ...

  6. export,import ,export default的区别

    首先要知道export,import ,export default是什么 ES6模块主要有两个功能:export和importexport用于对外输出本模块(一个文件可以理解为一个模块)变量的接口i ...

  7. pip安装lxml报错 Fatal error in launcher: Unable to create process using '"c:\users\administrator\appdata\local\programs\python\python36\python.exe" "C:\Users\Administrator\AppData\L

    pip install lxml 安装报错 E:\apollo\spider_code>Fatal error in launcher: Unable to create process usi ...

  8. Drawable.Callback

     一.介绍 public abstract void invalidateDrawable (Drawable who) Called when the drawable needs to be re ...

  9. Handler 与 Toast

    Toast或者Dialog中都有一个Handler的成员变量,所以如果不是在主线程中使用Toast或Dialog,则需要在使用Toast或者Dialog的线程中初始化Looper. Looper.pr ...

  10. Codeforces Round#251(Div 2)D Devu and his Brother

    --你以为你以为的.就是你以为的? --有时候还真是 题目链接:http://codeforces.com/contest/439/problem/D 题意大概就是要求第一个数组的最小值要不小于第二个 ...