2. Graded Problem (Page limit: 1 sheet; 2 sides) An element of an array A[1 nl i
ID: 3593745 • Letter: 2
Question
2. Graded Problem (Page limit: 1 sheet; 2 sides) An element of an array A[1 nl is called a majority element if more than half of A are equal to x. Your task is to find whether a given array has a majority element and if so, what it is. We do not assume the elements of A are integers, or otherwise ordered; the only query you can access the elements of A is of the form whether A Aj] in constant time, for any i and j. Solve this problem with a Divide-and-Conquer algorithm that runs in time O(nlogn). Prove your a- gorithm is correct, and prove the time bound. (Hint: By dividing A into two halves Al and A2, does the knowledge of whether Al and/or A2 have a majority element help? You must be rigorous in your reasoning.) Here is another Divide-and-Conquer approach: We will pair up elements of A arbitrarily. If the pair of elements are different, then discard them both. If the pair of elements are the same, then keep exactly one of them. This produces an array of at most n/2 elements. Prove that if A has a majority element, then no matter how the pair is done, the reduced array also has a majority element and it must be the same. Does the converse hold? Now devise a Divide-and-Conquer algorithm following this strategy that runs in time O(n)Explanation / Answer
Solution:
1)
We split the array A into a pair of subarrays A1 and A2 of [*fr1] the scale. we decide the bulk component of A1
and A2. at that time we have a tendency to do a linear time equality operation to choose whether or not it'sattainable to seek out a majority
element.
procedure GetMajorityElement(a[1...n])
Input: Array a of objects
Output: Majority component of a
if n = 1: come back a[1]
k = n/2
elemlsub = GetMajorityElement(a[1...k])
elemrsub = GetMajorityElement(a[k+1...n]
if elemlsub = elemrsub:
return elemlsub
lcount = GetFrequency(a[1...n],elemlsub)
rcount = GetFrequency(a[1...n],elemrsub)
if lcount > k+1:
return elemlsub
else if rcount > k+1:
return elemrsub
else come back NO-MAJORITY-ELEMENT
T(n) = 2T(n/2) + O(n)
GetFrequency computes the amount of times a component (elemlsub or elemrsub) seems within the given array
a[1...n]. 2 calls to GetFrequency is O(n). at that time comparisons area unit done to validate the existence of
majority component. GetFrequency is that the linear time equality operation.
2)
Using the projected divide-and-conquer operation, so it's attainable to grant a linear time algorithmic rule.
Idea is to combine up the weather at random to urge n/2 pairs. In every combine if the 2 parts area unit completely different we have a tendency to
discard each of them. If they're same just one of them is unbroken.
Let m be a majority component of A. Frequency of m is bigger than n/2 wherever n is that the variety of
elements of A. Since we have a tendency to area unit forming pairs of two, there must be a minimum of one combine wherever each the weather of
the combine area unit m since otherwise frequency of m during a can't be bigger than n/2. at the most all pairs willhave
both the weather of the combine as m. thus when the procedure a minimum of one component m are left or at the most n/2
will be left wherever every of them is m. so out of at the most n/2
elements, one among the component should be m.
procedure GetMajorityElementLinear(a[1...n])
Input: Array a of objects
Output: Majority component of a
if n = 2:
if a[1] = a[2]
return a[1]
else
return NO-MAJORITY-ELEMENT
Create temporary workerorary|a short lived|a brief} array temp
for i = one to n:
if a[i] = a[i+1]:
Insert a[i] into temporary worker
i = i+1
return GetMajorityElementLinear(temp)
procedure CheckSanity(a[1...n])
Input: Array a of objects
Output: Majority component of a
m = GetMajorityElementLinear(a[1...n])
freq = GetFrequency(a[1...n],m)
if freq > [n/2]+ 1:
return m
else
return NO-MAJORITY-ELEMENT
T(n) = T(n/2) + O(n)
Please, please upvote and raise your doubts within the comments.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.