Skip to content
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

Added Majority Voting Algorithm #9866

Merged
merged 27 commits into from
Oct 6, 2023
Merged

Conversation

sarvjeetdev
Copy link
Contributor

Describe your change:

  • Add an algorithm?
  • Fix a bug or typo in an existing algorithm?
  • Documentation change?

Checklist:

  • I have read CONTRIBUTING.md.
  • This pull request is all my own work -- I have not plagiarized.
  • I know that pull requests will not be merged if they fail the automated tests.
  • This PR only changes one algorithm file. To ease review, please open separate PRs for separate algorithms.
  • All new Python files are placed inside an existing directory.
  • All filenames are in all lowercase characters with no spaces or dashes.
  • All functions and variable names follow Python naming conventions.
  • All function parameters and return values are annotated with Python type hints.
  • All functions have doctests that pass the automated testing.
  • All new algorithms include at least one URL that points to Wikipedia or another similar explanation.
  • If this pull request resolves one or more open issues then the description above includes the issue number(s) with a closing keyword: "Fixes #ISSUE-NUMBER".

@algorithms-keeper algorithms-keeper bot added require tests Tests [doctest/unittest/pytest] are required tests are failing Do not merge until tests pass labels Oct 5, 2023
Copy link

@algorithms-keeper algorithms-keeper bot left a comment

Choose a reason for hiding this comment

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

Click here to look at the relevant links ⬇️

🔗 Relevant Links

Repository:

Python:

Automated review generated by algorithms-keeper. If there's any problem regarding this review, please open an issue about it.

algorithms-keeper commands and options

algorithms-keeper actions can be triggered by commenting on this PR:

  • @algorithms-keeper review to trigger the checks for only added pull request files
  • @algorithms-keeper review-all to trigger the checks for all the pull request files, including the modified files. As we cannot post review comments on lines not part of the diff, this command will post all the messages in one comment.

NOTE: Commands are in beta and so this feature is restricted only to a member or owner of the organization.

@algorithms-keeper algorithms-keeper bot added the awaiting reviews This PR is ready to be reviewed label Oct 5, 2023
@algorithms-keeper algorithms-keeper bot added require descriptive names This PR needs descriptive function and/or variable names require type hints https://docs.python.org/3/library/typing.html and removed require tests Tests [doctest/unittest/pytest] are required labels Oct 5, 2023
Copy link

@algorithms-keeper algorithms-keeper bot left a comment

Choose a reason for hiding this comment

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

Click here to look at the relevant links ⬇️

🔗 Relevant Links

Repository:

Python:

Automated review generated by algorithms-keeper. If there's any problem regarding this review, please open an issue about it.

algorithms-keeper commands and options

algorithms-keeper actions can be triggered by commenting on this PR:

  • @algorithms-keeper review to trigger the checks for only added pull request files
  • @algorithms-keeper review-all to trigger the checks for all the pull request files, including the modified files. As we cannot post review comments on lines not part of the diff, this command will post all the messages in one comment.

NOTE: Commands are in beta and so this feature is restricted only to a member or owner of the organization.

@algorithms-keeper algorithms-keeper bot removed the require type hints https://docs.python.org/3/library/typing.html label Oct 5, 2023
Copy link

@algorithms-keeper algorithms-keeper bot left a comment

Choose a reason for hiding this comment

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

Click here to look at the relevant links ⬇️

🔗 Relevant Links

Repository:

Python:

Automated review generated by algorithms-keeper. If there's any problem regarding this review, please open an issue about it.

algorithms-keeper commands and options

algorithms-keeper actions can be triggered by commenting on this PR:

  • @algorithms-keeper review to trigger the checks for only added pull request files
  • @algorithms-keeper review-all to trigger the checks for all the pull request files, including the modified files. As we cannot post review comments on lines not part of the diff, this command will post all the messages in one comment.

NOTE: Commands are in beta and so this feature is restricted only to a member or owner of the organization.

@algorithms-keeper algorithms-keeper bot removed the require descriptive names This PR needs descriptive function and/or variable names label Oct 5, 2023
Co-authored-by: Christian Clauss <cclauss@me.com>
@algorithms-keeper algorithms-keeper bot removed the tests are failing Do not merge until tests pass label Oct 6, 2023
@algorithms-keeper algorithms-keeper bot added the tests are failing Do not merge until tests pass label Oct 6, 2023
@algorithms-keeper algorithms-keeper bot removed the tests are failing Do not merge until tests pass label Oct 6, 2023
@algorithms-keeper algorithms-keeper bot added the tests are failing Do not merge until tests pass label Oct 6, 2023
@algorithms-keeper algorithms-keeper bot removed the tests are failing Do not merge until tests pass label Oct 6, 2023
"""


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.

sarvjeetdev and others added 2 commits October 6, 2023 17:37
Co-authored-by: Christian Clauss <cclauss@me.com>
Co-authored-by: Christian Clauss <cclauss@me.com>
@algorithms-keeper algorithms-keeper bot added the tests are failing Do not merge until tests pass label Oct 6, 2023
@algorithms-keeper algorithms-keeper bot removed the tests are failing Do not merge until tests pass label Oct 6, 2023
@cclauss
Copy link
Member

cclauss commented Oct 6, 2023

OK... Following the pseudocode and the graph in the wiki article...

We need two functions.
The first one takes in only votes and it only has three local variables (fixed memory size): candidate (fka m), i, and vote (fka x) and returns only one value: candidate. The phase 1 function should not use a Counter.

The candidates are:
B Blue circle
R Red square
Y Yellow star

>>> votes = "BBRBRYYRRRBRRYRR"
>>> def majority_vote_pass_one(votes):
...     candidate = None
...     i = 0
...     for vote in votes:
...         if i == 0:
...             candidate = vote
...             i = 1
...         elif candidate == vote:
...             i += 1
...         else:
...             i -= 1
...         print(f"{vote = }, {candidate = }, {i = }")
...     return candidate
...
>>> majority_vote_pass_one(votes)
vote = 'B', candidate = 'B', i = 1
vote = 'B', candidate = 'B', i = 2
vote = 'R', candidate = 'B', i = 1
vote = 'B', candidate = 'B', i = 2
vote = 'R', candidate = 'B', i = 1
vote = 'Y', candidate = 'B', i = 0
vote = 'Y', candidate = 'Y', i = 1
vote = 'R', candidate = 'Y', i = 0
vote = 'R', candidate = 'R', i = 1
vote = 'R', candidate = 'R', i = 2
vote = 'B', candidate = 'R', i = 1
vote = 'R', candidate = 'R', i = 2
vote = 'R', candidate = 'R', i = 3
vote = 'Y', candidate = 'R', i = 2
vote = 'R', candidate = 'R', i = 3
vote = 'R', candidate = 'R', i = 4
'R'

Copy link
Member

@cclauss cclauss left a comment

Choose a reason for hiding this comment

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

Nice! Thanks for your patience!

@cclauss cclauss merged commit 795e97e into TheAlgorithms:master Oct 6, 2023
@algorithms-keeper algorithms-keeper bot removed the awaiting reviews This PR is ready to be reviewed label Oct 6, 2023
@sarvjeetdev
Copy link
Contributor Author

Thank you so much, this was my first time, finally did it. Thanks a lot for the support and sorry for the mistakes. Keep up the good work. God bless you.

sedatguzelsemme pushed a commit to sedatguzelsemme/Python that referenced this pull request Sep 15, 2024
* Create MajorityVoteAlgorithm.py

* Update and rename MajorityVoteAlgorithm.py to majorityvotealgorithm.py

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Update and rename majorityvotealgorithm.py to majority_vote_algorithm.py

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Update majority_vote_algorithm.py

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Update majority_vote_algorithm.py

* Update majority_vote_algorithm.py

* Update other/majority_vote_algorithm.py

Co-authored-by: Christian Clauss <cclauss@me.com>

* renaming variables majority_vote_algorithm.py

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Update majority_vote_algorithm.py

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Update majority_vote_algorithm.py

* Update majority_vote_algorithm.py

* Update majority_vote_algorithm.py

* Update majority_vote_algorithm.py

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Update other/majority_vote_algorithm.py

Co-authored-by: Christian Clauss <cclauss@me.com>

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Update other/majority_vote_algorithm.py

Co-authored-by: Christian Clauss <cclauss@me.com>

* adding more testcases majority_vote_algorithm.py

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Update majority_vote_algorithm.py

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Update majority_vote_algorithm.py

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Christian Clauss <cclauss@me.com>
@isidroas isidroas mentioned this pull request Jan 25, 2025
14 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants