Skip to content

Majority Vote Problem #202

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 1 commit into from
Jul 3, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
3 changes: 2 additions & 1 deletion algorithm/category.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@
},
"greedy": {
"list": {
"job_scheduling": "Job Scheduling Problem"
"job_scheduling": "Job Scheduling Problem",
"majority_element": "Majority Element(Boyer–Moore majority vote algorithm)"
},
"name": "Greedy"
},
Expand Down
63 changes: 63 additions & 0 deletions algorithm/greedy/majority_element/basic/code.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
function isMajorityElement ( element ) {
var count = 0;
logger._print ('Verify majority element ' + element );
for (var i = N - 1; i >= 0; i--) {
tracer._notify (i,A[i])._wait ();
if (A[i] == element) {
count++;
} else {
tracer._denotify (i);
}
}
logger._print ('Count of our assumed majority element ' + count);
if(count>Math.floor (N/2)) {
logger._print ('Our assumption was correct!');
return true;
}
logger._print ('Our assumption was incorrect!');
return false;
}

function findProbableElement () {
var index = 0, count = 1;
tracer._select (index)._wait();
logger._print ('Beginning with assumed majority element : ' + A[index] + ' count : ' +count);
logger._print ('--------------------------------------------------------');
for( var i = 1; i < N; i++ ) {
tracer._notify (i,A[i])._wait ();
if(A[index]==A[i]) {
count++;
logger._print ('Same as assumed majority element! Count : ' + count);
} else {
count--;
logger._print ('Not same as assumed majority element! Count : ' + count);
}

if(count===0) {
logger._print ('Wrong assumption in majority element');
tracer._deselect (index);
tracer._denotify (i);
index = i;
count = 1;
tracer._select (i)._wait ();
logger._print ('New assumed majority element!'+ A[i] +' Count : '+count);
logger._print ('--------------------------------------------------------');
} else {
tracer._denotify (i);
}
}
logger._print ('Finally assumed majority element ' + A[index]);
logger._print ('--------------------------------------------------------');
return A[index];
}

function findMajorityElement () {
var element = findProbableElement ();
if(isMajorityElement (element) === true) {
logger._print ('Majority element is ' + element);
} else {
logger._print ('No majority element');
}
}

findMajorityElement ();
5 changes: 5 additions & 0 deletions algorithm/greedy/majority_element/basic/data.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
var A = [ 1, 3, 3, 2, 1, 1, 1 ];
var N = A.length;

var tracer = new Array1DTracer('List of element')._setData (A);
var logger = new LogTracer ('Console');
13 changes: 13 additions & 0 deletions algorithm/greedy/majority_element/desc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"Majority Element(Boyer–Moore majority vote algorithm)": "The majority vote problem is to determine in any given sequence of choices whether there is a choice with more occurrences than half of the total number of choices in the sequence and if so, to determine this choice.",
"Complexity": {
"time": " O(N)",
"space": "O(logN)"
},
"References": [
"<a href='https://en.wikipedia.org/wiki/Boyer%E2%80%93Moore_majority_vote_algorithm'>Wikipedia</a>"
],
"files": {
"basic": "Find majority element in array using Boyer–Moore majority vote algorithm"
}
}
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy