Question

The mode of a set of numbers is defined to be the number that occurs most...

The mode of a set of numbers is defined to be the number that occurs most frequently in the set. The set {4,6,2,4,3,1} has a mode of 4.

a) Suppose we know that there is an (unknown) element that occurs (n/2)+1 times in the set. Design an O(n) time algorithm to find the mode. Justify the time complexity of your algorithm.

b) Design an algorithm to compute the mode of a set of n numbers. You should try to solve the problem in smallest running time. Justify the time complexity of your algorithm.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

a)

The algorithm that works in O(n) is known as Moore’s Voting Algorithm. Basic idea of the algorithm is that if we cancel out each occurrence of an element e with all the other elements that are different from e then e will exist till end if it is a majority element.

findCandidate(a[], size)
1.  Initialize index and count of majority element
     maj_index = 0, count = 1
2.  Loop for i = 1 to size – 1
    (a) If a[maj_index] == a[i]
          count++
    (b) Else
        count--;
    (c) If count == 0
          maj_index = i;
          count = 1
3.  Return a[maj_index]

Above algorithm loops through each element and maintains a count of a[maj_index]. If the next element is same then increment the count, if the next element is not same then decrement the count, and if the count reaches 0 then changes the maj_index to the current element and set the count again to 1. So, the first phase of the algorithm gives us a candidate element.

In the second phase we need to check if the candidate is really a majority element. Second phase is simple and can be easily done in O(n). We just need to check if count of the candidate element is greater than n/2.

Example :
Let the array be A[] = 2, 2, 3, 5, 2, 2, 6

  • Initialize maj_index = 0, count = 1
  • Next element is 2, which is same as a[maj_index] => count = 2
  • Next element is 3, which is different from a[maj_index] => count = 1
  • Next element is 5, which is different from a[maj_index] => count = 0
    Since count = 0, change candidate for majority element to 5 => maj_index = 3, count = 1
  • Next element is 2, which is different from a[maj_index] => count = 0
    Since count = 0, change candidate for majority element to 2 => maj_index = 4
  • Next element is 2, which is same as a[maj_index] => count = 2
  • Next element is 6, which is different from a[maj_index] => count = 1
  • Finally candidate for majority element is 2.

First step uses Moore’s Voting Algorithm to get a candidate for majority element.

2. Check if the element obtained in step 1 is majority element or not :

printMajority (a[], size)
1.  Find the candidate for majority
2.  If candidate is majority. i.e., appears more than n/2 times.
       Print the candidate
3.  Else
       Print "No Majority Element"

b) The best approach is to create a count array of size k and initialize all elements of count[] as 0. Iterate through all elements of input array, and for every element arr[i], increment count[arr[i]]. Finally, iterate through count[] and return the index with maximum value. This approach takes O(n) time, but requires O(k) space.

Following is the O(n) time and O(1) extra space approach. Let us understand the approach with a simple example where arr[] = {2, 3, 3, 5, 3, 4, 1, 7}, k = 8, n = 8 (number of elements in arr[]).

1) Iterate though input array arr[], for every element arr[i], increment arr[arr[i]%k] by k (arr[] becomes {2, 11, 11, 29, 11, 12, 1, 15 })

2) Find the maximum value in the modified array (maximum value is 29). Index of the maximum value is the maximum repeating element (index of 29 is 3).

3) If we want to get the original array back, we can iterate through the array one more time and do arr[i] = arr[i] % k where i varies from 0 to n-1.

How does the above algorithm work? Since we use arr[i]%k as index and add value k at the index arr[i]%k, the index which is equal to maximum repeating element will have the maximum value in the end. Note that k is added maximum number of times at the index equal to maximum repeating element and all array elements are smaller than k.

Add a comment
Know the answer?
Add Answer to:
The mode of a set of numbers is defined to be the number that occurs most...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Problem #1: (15 points) You have two sorted lists of integers, L, and L2. You know...

    Problem #1: (15 points) You have two sorted lists of integers, L, and L2. You know the lengths of each list, L1 has length N, and L2 has length N2 (a) Design an efficient algorithm (only pseudocode) to output a sorted list L L2 (the intersection of L and L2). (b) If you know that N2> Ni. What is the running time complexity of your algorithm? Justify Important Note For this problem, you don't need to submit any implementation in...

  • Problem 1 (5+15 points) Consider the set P of n points and suppose we are given...

    Problem 1 (5+15 points) Consider the set P of n points and suppose we are given the points of P one point at a time. After receiving each point, we compute the convex hull of the points seen so far. (a) As a naive approach, we could run Graham’s scan once for each point, with a total running time of O(n2 log n). Write down the pesuedocode for this algorithm. (b) Develop an O(n2) algorithm to solve the problem. Write...

  • Note: when a problem has an array of real numbers, you cannot use counting sort or...

    Note: when a problem has an array of real numbers, you cannot use counting sort or radix sort. ​​​​​​​ For this problem, you are given a number t and an array A with n real numbers that are not sorted. Describe an algorithm that finds the t numbers in A that are closest to the median of A. That is, if A = {x1, . . . , xa) and xrn is the median, we want to find the t...

  • Devise a heap-sorting-based algorithm for finding the k smallest EVEN elements of an unsorted set of...

    Devise a heap-sorting-based algorithm for finding the k smallest EVEN elements of an unsorted set of n-element array. The corresponding average analytical time-complexity should also be provided. (Show your work; the time complexity for heap-building must be included; it is assumed that 50% of elements are even )

  • Let A be an array containing n numbers (positive and negative). Develop a divide and conquer algo...

    need help in this algorithm question Let A be an array containing n numbers (positive and negative). Develop a divide and conquer algorithm that finds the two indices 1 sisjsn such that A[k] (the sum of the elements from i to j) is maximized. For example, in the array A [10,-5,-6,5, 7,-2,4, -11], the sub-array A[4:6] has the sum 5+ 7-2+4-14 and no other sub-array contains elements that sum to a value greater than 14, so for this input the...

  • Full and complete answer in order to get credit. Thank you. Suppose you are given an...

    Full and complete answer in order to get credit. Thank you. Suppose you are given an unsorted list of n distinct numbers. However, the list is close to being sorted in the sense that each number is at most k positions away from its original position in the sorted list. For example, suppose the sorted list is in ascending order, then the smallest element in the original list will lie between position 1 and position k + 1, as position...

  • You are given a set of integer numbers A = {a1, a2, ..., an}, where 1...

    You are given a set of integer numbers A = {a1, a2, ..., an}, where 1 ≤ ai ≤ m for all 1 ≤ i ≤ n and for a given positive integer m. Give an algorithm which determines whether you can represent a given positive integer k ≤ nm as a sum of some numbers from A, if each number from A can be used at most once. Your algorithm must have O(nk) time complexity.

  • Lower bound arguments. In class, we proved that any comparison-based algorithm that has to sort n...

    Lower bound arguments. In class, we proved that any comparison-based algorithm that has to sort n numbers runs in Ω (nlogn) time. Let’s change the problem a bit. Suppose S[1. . . n] is a sorted array. We want to know if S contains some element x. a. How would you solve this problem and what is the running time of your algorithm? (Note: you can just say what algorithm you will use.) b. Show that any comparison-based algorithm(i.e., one...

  • Example 1.9: 1.23 "The median of an ordered set is an element such that the number...

    Example 1.9: 1.23 "The median of an ordered set is an element such that the number of elements less than the median is within one of the number that are greater, assuming no ties. a. Write an algorithm to find the median of three distinct integers a, b, and c. b. Describe D. the set of inputs for your algorithm. in light of the discussion in Sec- tion 1.4.3 following Example 1.9. c. How many comparisons does your algorithm do...

  • Reconstructing a tree You are asked to reconstruct the reporting hierarchy of a huge company Disorganized,...

    Reconstructing a tree You are asked to reconstruct the reporting hierarchy of a huge company Disorganized, Inc., based on information that is complete but poorly organized. The information is available in an n-element array, in which each element of the array is a pair (emp, boss) where emp is the name of an employee and boss is the supervisor of the employee, and n is the number of employees in Disorganized. You may assume that the names of all employees...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT