Uploaded by aymen150595

test quiz

advertisement
1. World Peace. You have decided to organize a grassroots campaign for world peace. Your plan is to assign ordinary
citizens into groups of k penpals (Qalmi dost) such that each group contains citizens from k different countries.
People in each group will exchange letters in an effort to increase their understanding of each other’s cultures.
Given k and the populations of the participating countries in array A, you must determine the maximum number
of groups that can be formed.
Note that no individual may be assigned to more than one group, and that some individuals may be left without a
group.
In the first algorithm we form a group from the k countries that have the highest number of citizens. We apply
this principle at every single step and then sort the sequence again to see which are the next k countries having
the highest number of citizens. This idea is illustrated in the following pseudo code:
Groups = 0
Repeat
Sort (A)
// sorts the array in decreasing order
Min = A[K]
If Min > 0 Groups = Groups + 1
For I = 1 to K
A[I] = A[I] - 1
End For
Until Min = 0
Return Groups
Unfortunately, a country can have up to a billion citizens, so we cannot afford to make only one group at a time.
Theoretically, for a given set of k countries, we can make groups until all the citizens in one of these countries
have been grouped. And this can be done in a single step:
Groups = 0
Repeat
Sort (A)
// sorts the array in decreasing order
Min = A[K]
Groups = Groups + Min
For I = 1 to K
A[I] = A[I] - Min
End For
Until Min = 0
Return Groups
(a) (6 points) Taha tells you that both greedy algorithms are accurate (optimal), do you agree? if not, identify
and back your statement with an exchange argument (preferably a counterexample)?
(b) (4 points) Which one will you prefer to use? Argue taking the performance and accuracy into consideration.
Download