Created
July 15, 2024 06:32
-
-
Save qnighy/2a069108408c58476edce93c1d2cd5b0 to your computer and use it in GitHub Desktop.
Fast 2-pass majority finder
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Copyright 2024 Masaki Hara | |
// | |
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: | |
// | |
// The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. | |
// | |
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | |
/// Returns the majority element if it exists and None otherwise. | |
/// | |
/// The majority element is the element that appears more than n / 2 times in the input array. | |
/// | |
/// Assumption: | |
/// | |
/// - `get_iter`'s response is finite. | |
/// - `get_iter`'s first and second responses contain the same multi-set of elements. | |
/// - `T: Eq` is pure and is indeed an equivalence relation. | |
pub fn majority<T, F, I>(mut get_iter: F) -> Option<T> | |
where | |
T: Eq, | |
F: FnMut() -> I, | |
I: IntoIterator<Item = T>, | |
{ | |
let candidate = { | |
let mut iter = get_iter().into_iter(); | |
let Some(mut candidate) = iter.next() else { | |
return None; | |
}; | |
// Invariant: for all possible element value x, | |
// for x == candidate, 2 * subarray.count(x) - subarray.len() <= count | |
// for x != candidate, 2 * subarray.count(x) - subarray.len() <= -count | |
// Also, count is nonnegative. | |
let mut count = 1_usize; | |
for value in iter { | |
if value == candidate { | |
count += 1; | |
} else if count == 0 { | |
candidate = value; | |
count = 1; | |
} else { | |
count -= 1; | |
} | |
} | |
// The majority, if any, is set to this variable, | |
// because otherwise it would violate the postcondition | |
// derived from the invariant's second inequality. | |
candidate | |
}; | |
// Check if the candidate is indeed the majority. | |
// This is sometimes not the case, e.g. for the input [1, 2, 3] (where 3 is the candidate). | |
let mut total = 0_usize; | |
let mut count = 0_usize; | |
for value in get_iter() { | |
total += 1; | |
if value == candidate { | |
count += 1; | |
} | |
} | |
if count > total / 2 { | |
Some(candidate) | |
} else { | |
None | |
} | |
} | |
#[test] | |
fn test_majority() { | |
assert_eq!(majority(|| []), None::<i32>); | |
assert_eq!(majority(|| [1]), Some(1)); | |
assert_eq!(majority(|| [1, 1]), Some(1)); | |
assert_eq!(majority(|| [1, 2]), None); | |
assert_eq!(majority(|| [1, 1, 1]), Some(1)); | |
assert_eq!(majority(|| [1, 1, 2]), Some(1)); | |
assert_eq!(majority(|| [1, 2, 1]), Some(1)); | |
assert_eq!(majority(|| [2, 1, 1]), Some(1)); | |
assert_eq!(majority(|| [1, 2, 3]), None); | |
assert_eq!(majority(|| [1, 1, 1, 1]), Some(1)); | |
assert_eq!(majority(|| [1, 1, 1, 2]), Some(1)); | |
assert_eq!(majority(|| [1, 1, 2, 1]), Some(1)); | |
assert_eq!(majority(|| [1, 2, 1, 1]), Some(1)); | |
assert_eq!(majority(|| [2, 1, 1, 1]), Some(1)); | |
assert_eq!(majority(|| [1, 1, 2, 2]), None); | |
assert_eq!(majority(|| [1, 2, 1, 2]), None); | |
assert_eq!(majority(|| [2, 1, 1, 2]), None); | |
assert_eq!(majority(|| [1, 1, 2, 3]), None); | |
assert_eq!(majority(|| [1, 2, 1, 3]), None); | |
assert_eq!(majority(|| [2, 1, 1, 3]), None); | |
assert_eq!(majority(|| [1, 2, 3, 1]), None); | |
assert_eq!(majority(|| [2, 1, 3, 1]), None); | |
assert_eq!(majority(|| [2, 3, 1, 1]), None); | |
assert_eq!(majority(|| [1, 2, 3, 4]), None); | |
assert_eq!(majority(|| [1, 1, 1, 1, 1]), Some(1)); | |
assert_eq!(majority(|| [1, 1, 1, 1, 2]), Some(1)); | |
assert_eq!(majority(|| [1, 1, 1, 2, 1]), Some(1)); | |
assert_eq!(majority(|| [1, 1, 2, 1, 1]), Some(1)); | |
assert_eq!(majority(|| [1, 2, 1, 1, 1]), Some(1)); | |
assert_eq!(majority(|| [2, 1, 1, 1, 1]), Some(1)); | |
assert_eq!(majority(|| [1, 1, 1, 2, 2]), Some(1)); | |
assert_eq!(majority(|| [1, 1, 2, 1, 2]), Some(1)); | |
assert_eq!(majority(|| [1, 2, 1, 1, 2]), Some(1)); | |
assert_eq!(majority(|| [2, 1, 1, 1, 2]), Some(1)); | |
assert_eq!(majority(|| [1, 1, 2, 2, 1]), Some(1)); | |
assert_eq!(majority(|| [1, 2, 1, 2, 1]), Some(1)); | |
assert_eq!(majority(|| [2, 1, 1, 2, 1]), Some(1)); | |
assert_eq!(majority(|| [1, 2, 2, 1, 1]), Some(1)); | |
assert_eq!(majority(|| [2, 1, 2, 1, 1]), Some(1)); | |
assert_eq!(majority(|| [2, 2, 1, 1, 1]), Some(1)); | |
assert_eq!(majority(|| [1, 1, 1, 2, 3]), Some(1)); | |
assert_eq!(majority(|| [1, 1, 2, 1, 3]), Some(1)); | |
assert_eq!(majority(|| [1, 2, 1, 1, 3]), Some(1)); | |
assert_eq!(majority(|| [2, 1, 1, 1, 3]), Some(1)); | |
assert_eq!(majority(|| [1, 1, 2, 3, 1]), Some(1)); | |
assert_eq!(majority(|| [1, 2, 1, 3, 1]), Some(1)); | |
assert_eq!(majority(|| [2, 1, 1, 3, 1]), Some(1)); | |
assert_eq!(majority(|| [1, 2, 3, 1, 1]), Some(1)); | |
assert_eq!(majority(|| [2, 1, 3, 1, 1]), Some(1)); | |
assert_eq!(majority(|| [2, 3, 1, 1, 1]), Some(1)); | |
assert_eq!(majority(|| [1, 1, 2, 2, 3]), None); | |
assert_eq!(majority(|| [1, 2, 1, 2, 3]), None); | |
assert_eq!(majority(|| [2, 1, 1, 2, 3]), None); | |
assert_eq!(majority(|| [1, 1, 2, 3, 2]), None); | |
assert_eq!(majority(|| [1, 2, 1, 3, 2]), None); | |
assert_eq!(majority(|| [2, 1, 1, 3, 2]), None); | |
assert_eq!(majority(|| [1, 1, 3, 2, 2]), None); | |
assert_eq!(majority(|| [1, 2, 3, 1, 2]), None); | |
assert_eq!(majority(|| [2, 1, 3, 1, 2]), None); | |
assert_eq!(majority(|| [1, 3, 1, 2, 2]), None); | |
assert_eq!(majority(|| [1, 3, 2, 1, 2]), None); | |
assert_eq!(majority(|| [2, 3, 1, 1, 2]), None); | |
assert_eq!(majority(|| [3, 1, 1, 2, 2]), None); | |
assert_eq!(majority(|| [3, 1, 2, 1, 2]), None); | |
assert_eq!(majority(|| [3, 2, 1, 1, 2]), None); | |
assert_eq!(majority(|| [1, 1, 2, 3, 4]), None); | |
assert_eq!(majority(|| [1, 2, 1, 3, 4]), None); | |
assert_eq!(majority(|| [1, 2, 3, 1, 4]), None); | |
assert_eq!(majority(|| [1, 2, 3, 4, 1]), None); | |
assert_eq!(majority(|| [2, 1, 1, 3, 4]), None); | |
assert_eq!(majority(|| [2, 1, 3, 1, 4]), None); | |
assert_eq!(majority(|| [2, 1, 3, 4, 1]), None); | |
assert_eq!(majority(|| [2, 3, 1, 1, 4]), None); | |
assert_eq!(majority(|| [2, 3, 1, 4, 1]), None); | |
assert_eq!(majority(|| [2, 3, 4, 1, 1]), None); | |
assert_eq!(majority(|| [1, 2, 3, 4, 5]), None); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
What is here