Question

In: Computer Science

You were given a kettle of n birds, which look all the same to you. To...

You were given a kettle of n birds, which look all the same to you. To decide if two birds are of the same species, you perform the following experiment – you put the two of them in a cage together. If they are friendly to each other, then they are of the same species. Otherwise, you separate them quickly before survival of the fittest kicks in.

1. Suppose that there are exactly p species present in your kettle of n birds. and one species has a plurality: more birds belong to that species than to any other species. Present a procedure to pick out the birds from the plurality species as efficiently as possible (i.e., minimize the number of experiments you have to do as a function of n and p). Do not assume that p = O(1).

Solutions

Expert Solution

This involves the concept of Moore’s Voting algorithm. It is similar to the part in which we have to find the number of species which were occurring more than half of the times. So if you had understood that part just move to the end there i am explaining this part.

This is just for the basic part if we want the species having majority.

the idea behind solving the above-given problem is through Moore’s Voting algorithm.

like, let's say that we have an array of a[] indicating the birds let's assume that the A, C, D belongs to the dominant species.

a[]= A B C D E

So here we keep two possible values and keep their count. these values indicate that they are the ones that are present in the majority in the group. Let's say these are first and second and c1=0,c2=0 are their respective counts.

so initially first='*' and second ='*' and c1=0,c2=0;

now we loop over all the species from i=0 to i=4. (n-1=4)

i=0 : first!='A'. && second !='A' but c1==0 so we make first='A' and c1=1;

i=1: first!='B'. && second !='B' && c1!=0 but c2==0 so we make second='B' and c2=1;

i=2 first!='C'. && second !='B' && c1!=0 but c2!=0 so we make c1--,c2-- both becomes 0. Here assuming that C don't belong to the group of both 'A' & 'B'.

i=3 first=='D' so make c1++; c1=1;

i=4 first=='E' so make c1++; c1=2;

Here first=='X' this means that it is checking if these two belong to the same group or not. See the code given below for more details

so the basic code behind this is given below

bool check( char x, char y){

return (fight(x,y)); // this will call some random function which will tell if thspecies x,y belong to same type or not

}

for (int i = 0; i < n; i++) {

        if (check(first,a[i]))

            count1++;

        else if (check(second,a[i]))

            count2++;

        else if (count1 == 0) {

            count1++;

            first = a[i];

        }

        else if (count2 == 0) {

            count2++;

            second = a[i];

        }

        else {

            count1--;

            count2--;

        }

}

Now we just need to loop one more time over all N species and check the number of counts that the species belong to the first and second. Fo any of these two whichever will have more than half of the species will be that answer;

int cnt1=0,cnt2=0;

for(int i=0;i<n;i++)

{

if(check(a[i],first)  

cnt1++;

else if(check(a[i],second)

cnt2++;

}

if(cnt1>n/2)

cout<<first<<"\n;

else if(cnt2>n/2)

cout<<second <<"\n";

else

cout<<"answer not possible";

So the time complexity for the above code is O(n) where n is size of the array and space complexity is O(1) i.e. constant.

The second part starts from here just like in the previous part where we know that the answer must be possible from either the first or the second value this time instead of just maintaining first and second we maintain p value and their respective counts. So basically above part can be considered as the solution for p=2. for p=1the answer will be the size of array. The idea behind is similar to the previous one . Here is the basic code given below.

struct bird

{

    char ch;

    int c;

};

struct bird temp[p-1];

    for (int i=0; i<p-1; i++)

        temp[i].c = 0;

    for (int i = 0; i < n; i++)

    {

        int j;

        for (j=0; j<p-1; j++)

        {

            if (check(temp[j].ch,arr[i]))//check function is mentioned in the first part

            {

                 temp[j].c += 1;

                 break;

            }

        }

        if (j == p-1)

        {

            int l;

            for (l=0; l<p-1; l++)

            {

                if (temp[l].c == 0)

                {

                    temp[l].ch = arr[i];

                    temp[l].c = 1;

                    break;

                }

            }

            if (l == p-1)

                for (l=0; l<p; l++)

                    temp[l].c -= 1;

        }

    }

int mx=0,pos=0;

    for (int i=0; i<p-1; i++)

    {

        int ac = 0;

        for (int j=0; j<n; j++)

            if (arr[j] == temp[i].e)

                ac++;

        if (ac > mx)

{

mx=ac;

pos=i;

}

    }

cout << "Number:" << temp[pos].ch << " Count:" << mx << endl;

The time complexity for this part will be O(n*p) and space complexity will be O(p) n is the length of the array and p is the plurality.


Related Solutions

You are attempting to determine whether two birds are members of the same species or not....
You are attempting to determine whether two birds are members of the same species or not. A. Using the morphospecies concept, what is a line of evidence that would support that the two birds are members of the same species? B.Using the biological species concept, what is a line of evidence that would support that the two birds are members of the same species?
You are again given the same 5 observations that you were provided with in the Correlation...
You are again given the same 5 observations that you were provided with in the Correlation problem.However,you are now asked to derive the regression line,y’= a+bX for the data below.After you have calculated the regression equation,explain to me how you could use it.                                X(Independent Variable) Y(Dependent variable) Month Advertising Expenditure (in Millions of $) Sales Revenue (in Millions of $)        July 1 3 August 2 7 September 3 5 October 4 11 November 5 14 12-Calculating the Standard Error of...
You are again given the same 5 observations that you were provided with in the Correlation...
You are again given the same 5 observations that you were provided with in the Correlation problem.However,you are now asked to derive the regression line,y’= a+bX for the data below.After you have calculated the regression equation,explain to me how you could use it.                                X(Independent Variable) Y(Dependent variable) Month Advertising Expenditure (in Millions of $) Sales Revenue (in Millions of $)        July 1 3 August 2 7 September 3 5 October 4 11 November 5 14 Calculating the Standard Error of...
Look at situation where there are 2 molecules of the same protein, which means same primary...
Look at situation where there are 2 molecules of the same protein, which means same primary sequence. In addition they fold at the same time in the same funnel. Each protein, which is represented by a marble, is predicted to take a special path as it moves down the funnel during the folding process. While it folds, it is possible that the two proteins collide on the way down the funnel, and as a result of this collision, one or...
When you look across the industry and note all the "as a Service" businesses, which "as...
When you look across the industry and note all the "as a Service" businesses, which "as a Service" type stands out as the most profilic? Why do you think this is so?
If you were a senior executive at Alibaba, would you have made the same decision? Which...
If you were a senior executive at Alibaba, would you have made the same decision? Which of the following would you recommend for taking the company's stock public? a. List on the New York Stock Exchange to avoid China's governance structure rules. b. Keep the company private and seek additional private investors to raise capital. c. List on the Hong Kong Stock Exchange to show confidence in the Chinese economy and allow the Chinese consumers who have built up the...
For this project, you will look at exam scores for two different sections of the same...
For this project, you will look at exam scores for two different sections of the same class: an 8am section and a 12pm section. Both sections were given the same exams. In each section there were two versions distributed: Version A and Version B. So in each section some students got version A and some students got Version B. You will use CrunchIt to create graphs and compute values in this assignment. The data file can be found in Moodle...
Students were asked to look at their hands and decide which finger on their hands is...
Students were asked to look at their hands and decide which finger on their hands is longer: the index finger or the ring finger. The results of the survey are in the following table. males females Index finger longer 27 48 Ring finger longer 69 99 96 147 Interpret, in terms a non-statistics person would understand, your 90% confidence interval, explaining what it tells us about the proportions of males and females whose ring finger is longer than their index...
Do you think that if you were a CFO of a company that you could look...
Do you think that if you were a CFO of a company that you could look at your cash flow statement and make changes in your business? What questions would you ask about a cash flow statement that would lead to changes in a business? 
1. Given an array of integers a dimension n. If the array contains the same number...
1. Given an array of integers a dimension n. If the array contains the same number of even and odd elements get (a1 + an) (a2 + an-1) ... 2. Given an array of integers dimension n. All array elements with even numbers preceding the first element to the maximum, multiplied by the maximum. 3. Given an array of dimension n. Insert after each zero element of the element in the middle (or the amount of secondary elements for even...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT