Randomized Algorithms
Last updated
Last updated
Randomized algorithms make decisions randomly as they process the input. In a worst- case world, an algorithm that does its own internal randomization may be able to offset certain worst-case phenomena. Problems that may not have been solvable by efficient deterministic algorithms may still be amenable to randomized algorithms.
Let be a set of numbers . The median of is the largest element in . if is even. if is odd.
The median could be computed in by sorting the numbers. However, the time complexity could be reduced to with randomized divide-and-conquer.
Given a set of numbers and a number between 1 and , consider the function Select(S, k)
that returns the kth largest element in . Select(S, n/2)
and Select(S, (n + 1/2)
could be used to find the median.
The basic structure of the algorithm is selecting an element as the pivot,and form the sets and , and then determining which of or contains the k-th largest element, and iterate only on this one.
The algorithm is called recursively on strictly smaller set, thus it must terminate. Regardless of how the pivot is choosen, the algorithm returns the k-th largest element oof .
The good choice of pivot should produce and that are approximately equal in size.
If the pivot is always the median, the recurrence is . The solution of the recurrence is .
If the pivot is reasonably well-centered and could reduce the sets in the recursive call by a factor of , the recurrence is . The solution of the recurrence is .
If the pivot is always the minimum, the recurrence is . The solution of the recurrence is .
Since a fairly large fraction of the elements are reasonably well-centered, the pivot could be choosed at random.
Let an element of set be central if at least a quarter of the elements are smaller than it and at least a quarter of the elements are larger than it.
Quicksort behaves slightly different from quickselect: rather than looking for the median on just one side of the splitter, the algorithm sorts both sides recursively and glues the two sorted pieces together to produce the overall output.
If a central element is chosen as a pivot, the set will shrink by a factor of . Since half of elements in the set are central, the probability that our random choice of pivot produces a central element is . Therefore, the expected number of iterations before a central element is found is 2.
Since the sum is a geometric series that converges, the expected running time of Select(n, k)
is .
If the pivot is always the median, the recurrence is . The solution of the recurrence is .
If the pivot is always the minimum, the recurrence is . The solution of the recurrence is .
The expected running time for the algorithm on a set S, excluding the time spent on recursive calls, is . The expected running time of Quicksort is .