{"id":26442,"date":"2017-12-20T21:10:24","date_gmt":"2017-12-20T15:40:24","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=26442"},"modified":"2017-12-20T21:10:24","modified_gmt":"2017-12-20T15:40:24","slug":"randomized-algorithms-set-3-12-approximate-median","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/randomized-algorithms-set-3-12-approximate-median\/","title":{"rendered":"Randomized Algorithms | Set 3 (1\/2 Approximate Median)"},"content":{"rendered":"<p>In this post, a Monte Carlo algorithm is discussed.<\/p>\n<p><strong>Problem Statement : <\/strong>Given an unsorted array A[] of n numbers and \u03b5 &gt; 0, compute an element whose rank (position in sorted A[]) is in the range [(1 \u2013 \u03b5)n\/2, (1 + \u03b5)n\/2].<br \/>\nFor \u00bd Approximate Median Algorithm &amp;epsilom; is 1\/2 =&gt; rank should be in the range [n\/4, 3n\/4]\n<p>We can find k\u2019th smallest element in <a href=\"http:\/\/www.geeksforgeeks.org\/kth-smallestlargest-element-unsorted-array-set-2-expected-linear-time\/\" target=\"_blank\" rel=\"noopener\">O(n) expected time<\/a> and <a href=\"http:\/\/www.geeksforgeeks.org\/kth-smallestlargest-element-unsorted-array-set-3-worst-case-linear-time\/\" target=\"_blank\" rel=\"noopener\">O(n) worst case<\/a> time.<\/p>\n<p><strong>What if we want in less than O(n) time with low probable error allowed?<\/strong><br \/>\nFollowing 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\/n<sup>2<\/sup>.<\/p>\n<ol>\n<li>Randomly choose k elements from the array where k=c log n (c is some constant)<\/li>\n<li>Insert then into a set.<\/li>\n<li>Sort elements of the set.<\/li>\n<li>Return median of the set i.e. (k\/2)th element from the set<\/li>\n<\/ol>\n[ad type=&#8221;banner&#8221;]\n<p>c Programming<\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <\/div> <pre class=\"language-cpp code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-cpp code-embed-code\">\/* C++ program to find Approximate Median using<br\/>   1\/2 Approximate Algorithm *\/<br\/>#include&lt;bits\/stdc++.h&gt;<br\/>using namespace std;<br\/> <br\/>\/\/ This function returns the Approximate Median<br\/>int randApproxMedian(int arr[],int n)<br\/>{<br\/>    \/\/ Declaration for the random number generator<br\/>    random_device rand_dev;<br\/>    mt19937 generator(rand_dev());<br\/> <br\/>    \/\/ Random number generated will be in the range [0,n-1]<br\/>    uniform_int_distribution&lt;int&gt; distribution(0, n-1);<br\/> <br\/>    if (n==0)<br\/>        return 0;<br\/> <br\/>    int k = 10*log2(n); \/\/ Taking c as 10<br\/> <br\/>    \/\/ A set stores unique elements in sorted order<br\/>    set&lt;int&gt; s;<br\/>    for (int i=0; i&lt;k; i++)<br\/>    {<br\/>        \/\/ Generating a random index<br\/>        int index = distribution(generator);<br\/> <br\/>        \/\/Inserting into the set<br\/>        s.insert(arr[index]);<br\/>    }<br\/> <br\/>    set&lt;int&gt; ::iterator itr = s.begin();<br\/> <br\/>    \/\/ Report the median of the set at k\/2 position<br\/>    \/\/ Move the itr to k\/2th position<br\/>    advance(itr, (s.size()\/2) - 1);<br\/> <br\/>    \/\/ Return the median<br\/>    return *itr;<br\/>}<br\/> <br\/>\/\/ Driver method to test above method<br\/>int main()<br\/>{<br\/>    int arr[] = {1, 3, 2, 4, 5, 6, 8, 7};<br\/>    int n = sizeof(arr)\/sizeof(int);<br\/>    printf(&quot;Approximate Median is %d\\n&quot;,randApproxMedian(arr,n));<br\/>    return 0<br\/>}<\/code><\/pre> <\/div>\n<p>&nbsp;<\/p>\n<p><strong>Time Complexity:<\/strong><br \/>\nWe 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).<br \/>\nNow replacing k with c log n<br \/>\n=&gt;O(c log n (log (clog n))) =&gt;O (log n (log log n))<\/p>\n<p><strong>How is probability of error less than 2\/n<sup>2<\/sup>?<\/strong><br \/>\nAlgorithm makes an error if the set S has at least k\/2 elements are from the Left Quarter or Right Quarter.<\/p>\n<p><img fetchpriority=\"high\" decoding=\"async\" class=\"aligncenter size-full wp-image-26456\" src=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/05\/median.png\" alt=\"Randomized Algorithms | Set 3 (1\/2 Approximate Median)\" width=\"867\" height=\"268\" srcset=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/05\/median.png 867w, https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/05\/median-300x93.png 300w, https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/05\/median-768x237.png 768w\" sizes=\"(max-width: 867px) 100vw, 867px\" \/><\/p>\n<p>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).<\/p>\n[ad type=&#8221;banner&#8221;]\n<p>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 :<\/p>\n<p>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?<\/p>\n<p><b>Explanation:<\/b><\/p>\n<p><img decoding=\"async\" class=\"aligncenter size-full wp-image-26460\" src=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/05\/probability.png\" alt=\"Randomized Algorithms | Set 3 (1\/2 Approximate Median)\" width=\"692\" height=\"389\" srcset=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/05\/probability.png 692w, https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/05\/probability-300x168.png 300w, https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/05\/probability-470x264.png 470w, https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/05\/probability-640x360.png 640w, https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/05\/probability-215x120.png 215w, https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/05\/probability-414x232.png 414w\" sizes=\"(max-width: 692px) 100vw, 692px\" \/><\/p>\n<pre>If we put k = c log n for c = 10, we get \r\nP &lt;= (1\/2)<sup>2log n<\/sup>\r\nP &lt;= (1\/2)<sup>log n2<\/sup>\r\nP &lt;= n<sup>-2<\/sup>\r\n<\/pre>\n<p>Probability of selecting at least k\/2 elements from the left quarter) &lt;= 1\/n<sup>2<\/sup><br \/>\nProbability of selecting at least k\/2 elements from the left or right quarter) &lt;= 2\/n<sup>2<\/sup><\/p>\n<p>Therefore algorithm produces incorrect result with probability less that or equal to 2\/n<sup>2<\/sup>.<\/p>\n[ad type=&#8221;banner&#8221;]\n","protected":false},"excerpt":{"rendered":"<p>Randomized Algorithms | Set 3 (1\/2 Approximate Median)-Randomized Algorithms<br \/>\nRandomly choose k elements from the array where k=c log n (c is some constant)<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[78696,78806,78685,78681,78679,78807,78680,78683],"class_list":["post-26442","post","type-post","status-publish","format-standard","hentry","category-coding","tag-application-of-randomized-algorithm","tag-approximate-median-formula","tag-las-vegas-randomized-algorithm","tag-randomized-algorithm-example","tag-randomized-algorithm-in-daa","tag-randomized-algorithms-tutorial","tag-randomized-quicksort-algorithm","tag-types-of-randomized-algorithms"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/26442","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/comments?post=26442"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/26442\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=26442"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=26442"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=26442"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}