DR.
APJ ABDUL KALAM TECHNICAL UNIVERSITY
Branch - CSE
Design & Analysis of Algorithms
Lecture – 36
Randomized Algorithms
By
Mr. Prabhat Singh
Assistant Professor
Department of Computer Science & Engineering
ABES Engineering College, Ghaziabad
Divide and Conquer Approach in Quick Sort
Divide-and-Conquer paradigm in Quick Sort:
Divide: Partition (rearrange) the array A[p r] into two (possibly empty)
subarrays A[p q - 1] and A[q + 1 r] such that each element of A[p q - 1]
is less than or equal to A[q], which is, in turn, less than or equal to each
element of A[q + 1 r]. Compute the index q as part of this partitioning
procedure.
Conquer: Sort the two subarrays A[p q -1] and A[q +1 r] by recursive
calls to quicksort.
Combine: Since the subarrays are sorted in place, no work is needed to
combine them: the entire array A[p r] is now sorted.
Quick Sort Algorithm
Quick Sort Algorithm
Note: To sort an entire array A, the initial call is
QUICKSORT(A, 1, length[A])
Working of Partition Function: Quick Sort
Randomized Algorithms
A Randomized algorithm is an algorithm that employs a degree of randomness
as part of its logic.
The algorithm typically uses uniformly random bits as an auxiliary input to guide
its behavior, in the hope of achieving good performance in the "average case"
over all possible choices of random bits.
Formally, the algorithm's performance will be a random variable determined by
the random bits; thus either the running time, or the output (or both) are
random variables.
One has to distinguish between algorithms that use the random input to reduce
the expected running time or memory usage, but always terminate with a
correct result (Las Vegas algorithms) in a bounded amount of time.
Randomized version of Quick Sort:
Randomized version of Quick sort: Instead of always using A[r] as the pivot, we will
use a randomly chosen element from the sub-array A[p _ r].
We do so by exchanging element A[r] with an element chosen at random from
A[p _ r].
This modification, in which we randomly sample the range p,...,r, ensures that the
pivot element x = A[r] is equally likely to be any of the r - p + 1 elements in the sub-
array.
Because the pivot element is randomly chosen, we expect the split of the input
array to be reasonably well balanced on average.
The changes to PARTITION and QUICKSORT are small.
In the new partition procedure, we simply implement the swap before actually
partitioning:
Randomized version of Quick Sort:
RANDOMIZED-PARTITION(A, p, r):
1. i ← RANDOM(p, r)
2. exchange A[r] ↔ A[i]
3. return PARTITION(A, p, r)
RANDOMIZED-QUICKSORT(A, p, r):
1. If p < r
2. then q ← RANDOMIZED-PARTITION(A, p, r)
3. RANDOMIZED-QUICKSORT(A, p, q - 1)
4. RANDOMIZED-QUICKSORT(A, q + 1, r)
Randomized version of Quick Sort:
Randomized version of Quick Sort:
Analysis of Randomized Quicksort: We are discussing here worst and best case
complexity of randomized quicksort algorithm
Worst case complexity: Let T (n) be the worst-case time for the procedure
QUICKSORT on an input of size n. We have the recurrence
where the parameter q ranges from 0 to n - 1 because the procedure PARTITION
produces two sub-problems with total size n - 1. We guess that T (n) ≤ cn2 for some
constant c. Substituting this guess into recurrence. We obtain
Randomized version of Quick Sort:
Expected running time : If, in each level of recursion, the split induced by
RANDOMIZED-PARTITION puts any constant fraction of the elements on one side of
the partition, then the recursion tree has depth Θ(lg n), and O(n) work is performed
at each level.
Even if we add new levels with the most unbalanced split possible between these
levels, the total time remains O (n lg n).
Randomized version of Binary Search:
Randomized Binary Search: We are given a sorted array A[] of n elements. We need
to find if x is present in A or not. In binary search we always used middle element,
here we will randomly pick one element in given range.
In Binary Search we had middle = (start + end)/2.
In Randomized binary search we do following:
• Generate a random number t,
• Since range of number in which we want a random number is [start, end].
• Hence we do, t = t % (end-start+1)
• Then, t = start + t;
• Hence t is a random number between start and end.
Running Time complexity of Randomized Binary Search Algorithm is O(Logn)
THANK YOU