wu :: forums
« wu :: forums - majority element »

Welcome, Guest. Please Login or Register.
May 18th, 2024, 2:15am

RIDDLES SITE WRITE MATH! Home Home Help Help Search Search Members Members Login Login Register Register
   wu :: forums
   riddles
   cs
(Moderators: william wu, SMQ, Eigenray, ThudnBlunder, Icarus, towr, Grimbal)
   majority element
« Previous topic | Next topic »
Pages: 1  Reply Reply Notify of replies Notify of replies Send Topic Send Topic Print Print
   Author  Topic: majority element  (Read 1305 times)
oriole
Newbie
*





   


Posts: 32
majority element  
« on: Jun 30th, 2008, 6:55am »
Quote Quote Modify Modify

Well this problem has been posted earlier  
 
http://www.ocf.berkeley.edu/~wwu/cgi-bin/yabb/YaBB.cgi?board=riddles_cs; action=display;num=1120478027
 
but I have a doubt in the solution provided.
 
Even for input 3 , 3 , 4 , 2 , 4 , 4, 2
it will display result as 4.
 
Has anyone verified this putting this input ?
 
a[i]     element    counter
3    3    1
3    3    2
4    3    1
2    3    0
4    4    1
4    4    2
2    4    1
 
4 is not the majority element yet it will come out to be one since counter > 0
IP Logged
towr
wu::riddles Moderator
Uberpuzzler
*****



Some people are average, some are just mean.

   


Gender: male
Posts: 13730
Re: majority element  
« Reply #1 on: Jun 30th, 2008, 7:02am »
Quote Quote Modify Modify

If you read the thread:
on Jul 4th, 2005, 6:17am, towr wrote:
This returns the majority element assuming there is one. You'll still have to check whether there actually  is one (by checking whether this element occurs more than N/2 times).

The result the algorithm gives is the element that is the majority element, given that there is one. If you give invalid input (because the premise is that there is a majority element, and your example doesn't have one), then you have to check for that.
« Last Edit: Jun 30th, 2008, 7:05am by towr » IP Logged

Wikipedia, Google, Mathworld, Integer sequence DB
oriole
Newbie
*





   


Posts: 32
Re: majority element  
« Reply #2 on: Jun 30th, 2008, 7:06am »
Quote Quote Modify Modify

on Jun 30th, 2008, 7:02am, towr wrote:
If you read the thread:
 
The result the algorithm gives is the element that is the majority element, given that there is one. If you give invalid input (because the premise is that there is a majority element, and your example doesn't have one), then you have to check for that.

 
 
In case we have both the conditions that majority may/may not be present, in that case we have to take care of other condition also and the algo proposed wont be applicable.
As the question put on demanded considering both the situation I was under the impression it will take care of both situations.
IP Logged
towr
wu::riddles Moderator
Uberpuzzler
*****



Some people are average, some are just mean.

   


Gender: male
Posts: 13730
Re: majority element  
« Reply #3 on: Jun 30th, 2008, 7:19am »
Quote Quote Modify Modify

on Jun 30th, 2008, 7:06am, oriole wrote:
As the question put on demanded considering both the situation I was under the impression it will take care of both situations.
It just requires an additional check, as mentioned in the post.
 
Besides, the question asks "write an O(N) algorithm to find the majority element if it exists"; I wasn't asked to do anything in particular if it didn't exist Wink
 
And it's not like it's the only detail missing from the pseudo code in the orange block, no libraries were included, no output written, nor input read, no function wrapper. It just gives the bare bones of the algorithm. This has two advantages, 1) I can indulge my laziness, 2) it doesn't complicate the understanding of the code by obfuscating the main points in a lot of needless details.
IP Logged

Wikipedia, Google, Mathworld, Integer sequence DB
towr
wu::riddles Moderator
Uberpuzzler
*****



Some people are average, some are just mean.

   


Gender: male
Posts: 13730
Re: majority element  
« Reply #4 on: Jun 30th, 2008, 7:31am »
Quote Quote Modify Modify

Here, I'll humour you.
 
Code:

extern find_majority_element_assuming_it_exists(Array arr);
 
function  find_majority_element(Array arr)
{
  arr.element_type element = find_majority_element_assuming_it_exists(arr);
  unsigned counter=0;
  for(unsigned i=0; i < arr.size(); i++)
   if(arr[i] == element) counter++;
 
  if (counter > n/2)
    return element;
  else
    throw HONESTLY_NOONE_TOLD_ME_WHAT_TO_DO_IF_THERE_WASNT_A_MAJORITY_ELEMENT_EXCE PTION;
}

 
Where the function find_majority_element_assuming_it_exists implements the code in the other thread, which finds the majority element if it exists.
« Last Edit: Jun 30th, 2008, 7:36am by towr » IP Logged

Wikipedia, Google, Mathworld, Integer sequence DB
oriole
Newbie
*





   


Posts: 32
Re: majority element  
« Reply #5 on: Jun 30th, 2008, 10:39am »
Quote Quote Modify Modify

on Jun 30th, 2008, 7:31am, towr wrote:
Here, I'll humour you.
 
Code:

extern find_majority_element_assuming_it_exists(Array arr);
 
function  find_majority_element(Array arr)
{
  arr.element_type element = find_majority_element_assuming_it_exists(arr);
  unsigned counter=0;
  for(unsigned i=0; i < arr.size(); i++)
   if(arr[i] == element) counter++;
 
  if (counter > n/2)
    return element;
  else
    throw HONESTLY_NOONE_TOLD_ME_WHAT_TO_DO_IF_THERE_WASNT_A_MAJORITY_ELEMENT_EXCE PTION;
}

 
Where the function find_majority_element_assuming_it_exists implements the code in the other thread, which finds the majority element if it exists.

 
 
well instead of putting this whole new check just check the count variable (in the previous thread). It should be > 2  
 Smiley
« Last Edit: Jun 30th, 2008, 10:40am by oriole » IP Logged
towr
wu::riddles Moderator
Uberpuzzler
*****



Some people are average, some are just mean.

   


Gender: male
Posts: 13730
Re: majority element  
« Reply #6 on: Jun 30th, 2008, 11:55am »
Quote Quote Modify Modify

on Jun 30th, 2008, 10:39am, oriole wrote:
well instead of putting this whole new check just check the count variable (in the previous thread). It should be > 2
Not necessarily. It may be no more than 1 (e.g. if you alternate the majority element and other elements; [4,1,4,2,4,3,4] -> count goes 1,0,1,0,1,0,1).  
And it may be > 2 for elements that aren't the majority element (e.g. if you trail any sequence with a new element that occurs less than half the time (and more than twice); [1,2,3,4,5,5,5] -> count goes 1,0,1,0,1,2,3).
 
You need to run through the array again to make sure. Or you need another algorithm entirely. And considering this one is fairly cheap (only two checks for every element), I doubt it would do better over all.
« Last Edit: Jun 30th, 2008, 11:57am by towr » IP Logged

Wikipedia, Google, Mathworld, Integer sequence DB
Pages: 1  Reply Reply Notify of replies Notify of replies Send Topic Send Topic Print Print

« Previous topic | Next topic »

Powered by YaBB 1 Gold - SP 1.4!
Forum software copyright © 2000-2004 Yet another Bulletin Board