Skip to content

Added Majority Voting Algorithm #9866

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 27 commits into from
Oct 6, 2023
Merged
Changes from 17 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
5b3a3e9
Create MajorityVoteAlgorithm.py
sarvjeetdev Oct 5, 2023
6495340
Update and rename MajorityVoteAlgorithm.py to majorityvotealgorithm.py
sarvjeetdev Oct 5, 2023
b881325
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 5, 2023
e3580d4
Update and rename majorityvotealgorithm.py to majority_vote_algorithm.py
sarvjeetdev Oct 5, 2023
2398221
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 5, 2023
ba13c2a
Update majority_vote_algorithm.py
sarvjeetdev Oct 5, 2023
83413d1
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 5, 2023
2bcd208
Update majority_vote_algorithm.py
sarvjeetdev Oct 5, 2023
88efe5b
Update majority_vote_algorithm.py
sarvjeetdev Oct 6, 2023
b0608a0
Update other/majority_vote_algorithm.py
sarvjeetdev Oct 6, 2023
6ff610a
renaming variables majority_vote_algorithm.py
sarvjeetdev Oct 6, 2023
288608e
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 6, 2023
cf8a2e3
Update majority_vote_algorithm.py
sarvjeetdev Oct 6, 2023
7cf0e5d
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 6, 2023
fbd6082
Update majority_vote_algorithm.py
sarvjeetdev Oct 6, 2023
21f5635
Update majority_vote_algorithm.py
sarvjeetdev Oct 6, 2023
57e64ce
Update majority_vote_algorithm.py
sarvjeetdev Oct 6, 2023
23ea6fe
Update majority_vote_algorithm.py
sarvjeetdev Oct 6, 2023
5131a19
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 6, 2023
5e398b2
Update other/majority_vote_algorithm.py
sarvjeetdev Oct 6, 2023
9d2e3dc
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 6, 2023
00c65aa
Update other/majority_vote_algorithm.py
sarvjeetdev Oct 6, 2023
a263f7a
adding more testcases majority_vote_algorithm.py
sarvjeetdev Oct 6, 2023
63d60e9
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 6, 2023
6756885
Update majority_vote_algorithm.py
sarvjeetdev Oct 6, 2023
a215f1b
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 6, 2023
c0a3f88
Update majority_vote_algorithm.py
cclauss Oct 6, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions other/majority_vote_algorithm.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import collections

"""
This is Booyer-Moore Majority Vote Algorithm. The problem statement goes like:
Given an integer array of size n, find all elements that appear more than ⌊ n/k ⌋ times.
We have to solve in O(n) time and O(1) Space.
URL : https://en.wikipedia.org/wiki/Boyer%E2%80%93Moore_majority_vote_algorithm
"""


def majority_element(total_votes: list[int], min_votes_required: int) -> list[int]:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If partition is min_votes_required then how does this algorithm use a constant number of words of memory as discussed in the Wikipedia article? If there are 30 candidates then there will ~10 times as much memory used than 3 candidates.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Problem : We need to find the elements (votes or candidates) that appear at least floor( total votes / k) times ,( here k is partitioning the array i.e. total_votes, that's why I named it partition before ). We need to solve in a linear time. This is not representing real world voting.

This was a problem came in an interview that I attended. Assuming that we have all the votes and the factor is fixed like 2 or 3. What I have contributed is a general case of the algorithm.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know this is not very special algo but It was something new in the interview and I wanted to contribute for the Hacktoberfest and that's the reason I submitted this.

"""
>>> majority_element([1, 2, 2, 3, 1, 3, 2],3)
[2]
"""
majority_candidate_counter: counter[int, int] = collections.Counter()
for vote in total_votes:
majority_candidate_counter[vote] += 1
if len(majority_candidate_counter) == min_votes_required:
majority_candidate_counter -= collections.Counter(
set(majority_candidate_counter)
)
majority_candidate_counter = collections.Counter(
vote for vote in total_votes if vote in majority_candidate_counter
)
return [
vote
for vote in majority_candidate_counter
if majority_candidate_counter[vote] > len(total_votes) / min_votes_required
]


if __name__ == "__main__":
import doctest

doctest.testmod()