In this post, a Monte Carlo algorithm is discussed.

Problem Statement : Given an unsorted array A[] of n numbers and ε > 0, compute an element whose rank (position in sorted A[]) is in the range [(1 – ε)n/2, (1 + ε)n/2].
For ½ Approximate Median Algorithm &epsilom; is 1/2 => rank should be in the range [n/4, 3n/4]

We can find k’th smallest element in O(n) expected time and O(n) worst case time.

What if we want in less than O(n) time with low probable error allowed?
Following steps represent an algorithm that is O((Log n) x (Log Log n)) time and produces incorrect result with probability less than or equal to 2/n2.

  1. Randomly choose k elements from the array where k=c log n (c is some constant)
  2. Insert then into a set.
  3. Sort elements of the set.
  4. Return median of the set i.e. (k/2)th element from the set
[ad type=”banner”]

c Programming

[pastacode lang=”cpp” manual=”%2F*%20C%2B%2B%20program%20to%20find%20Approximate%20Median%20using%0A%20%20%201%2F2%20Approximate%20Algorithm%20*%2F%0A%23include%3Cbits%2Fstdc%2B%2B.h%3E%0Ausing%20namespace%20std%3B%0A%20%0A%2F%2F%20This%20function%20returns%20the%20Approximate%20Median%0Aint%20randApproxMedian(int%20arr%5B%5D%2Cint%20n)%0A%7B%0A%20%20%20%20%2F%2F%20Declaration%20for%20the%20random%20number%20generator%0A%20%20%20%20random_device%20rand_dev%3B%0A%20%20%20%20mt19937%20generator(rand_dev())%3B%0A%20%0A%20%20%20%20%2F%2F%20Random%20number%20generated%20will%20be%20in%20the%20range%20%5B0%2Cn-1%5D%0A%20%20%20%20uniform_int_distribution%3Cint%3E%20distribution(0%2C%20n-1)%3B%0A%20%0A%20%20%20%20if%20(n%3D%3D0)%0A%20%20%20%20%20%20%20%20return%200%3B%0A%20%0A%20%20%20%20int%20k%20%3D%2010*log2(n)%3B%20%2F%2F%20Taking%20c%20as%2010%0A%20%0A%20%20%20%20%2F%2F%20A%20set%20stores%20unique%20elements%20in%20sorted%20order%0A%20%20%20%20set%3Cint%3E%20s%3B%0A%20%20%20%20for%20(int%20i%3D0%3B%20i%3Ck%3B%20i%2B%2B)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%2F%2F%20Generating%20a%20random%20index%0A%20%20%20%20%20%20%20%20int%20index%20%3D%20distribution(generator)%3B%0A%20%0A%20%20%20%20%20%20%20%20%2F%2FInserting%20into%20the%20set%0A%20%20%20%20%20%20%20%20s.insert(arr%5Bindex%5D)%3B%0A%20%20%20%20%7D%0A%20%0A%20%20%20%20set%3Cint%3E%20%3A%3Aiterator%20itr%20%3D%20s.begin()%3B%0A%20%0A%20%20%20%20%2F%2F%20Report%20the%20median%20of%20the%20set%20at%20k%2F2%20position%0A%20%20%20%20%2F%2F%20Move%20the%20itr%20to%20k%2F2th%20position%0A%20%20%20%20advance(itr%2C%20(s.size()%2F2)%20-%201)%3B%0A%20%0A%20%20%20%20%2F%2F%20Return%20the%20median%0A%20%20%20%20return%20*itr%3B%0A%7D%0A%20%0A%2F%2F%20Driver%20method%20to%20test%20above%20method%0Aint%20main()%0A%7B%0A%20%20%20%20int%20arr%5B%5D%20%3D%20%7B1%2C%203%2C%202%2C%204%2C%205%2C%206%2C%208%2C%207%7D%3B%0A%20%20%20%20int%20n%20%3D%20sizeof(arr)%2Fsizeof(int)%3B%0A%20%20%20%20printf(%22Approximate%20Median%20is%20%25d%5Cn%22%2CrandApproxMedian(arr%2Cn))%3B%0A%20%20%20%20return%200%0A%7D%0A%0A” message=”” highlight=”” provider=”manual”/]

 

Time Complexity:
We use a set provided by the STL in C++. In STL Set, insertion for each element takes O(log k). So for k insertions, time taken is O (k log k).
Now replacing k with c log n
=>O(c log n (log (clog n))) =>O (log n (log log n))

How is probability of error less than 2/n2?
Algorithm makes an error if the set S has at least k/2 elements are from the Left Quarter or Right Quarter.

Randomized Algorithms | Set 3 (1/2 Approximate Median)

It is quite easy to visualize this statement since the median which we report will be (k/2)th element and if we take k/2 elements from the left quarter(or right quarter) the median will be from the left quarter (or the right quarter).

[ad type=”banner”]

An array can be divided into 4 quarters each of size n/4. So P(selecting left quarter) is 1/4. So what is the probability that at least k/2 elements are from the Left Quarter or Right Quarter? This probability problem is same as below :

Given a coin which gives HEADS with probability 1/4 and TAILS with 3/4. The coin is tossed k times. What is the probability that we get at least k/2 HEADS is less than or equal to?

Explanation:

Randomized Algorithms | Set 3 (1/2 Approximate Median)

If we put k = c log n for c = 10, we get 
P <= (1/2)2log n
P <= (1/2)log n2
P <= n-2

Probability of selecting at least k/2 elements from the left quarter) <= 1/n2
Probability of selecting at least k/2 elements from the left or right quarter) <= 2/n2

Therefore algorithm produces incorrect result with probability less that or equal to 2/n2.

[ad type=”banner”]

Categorized in: