Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Introduction to Randomized Algorithms Md. Aashikur Rahman Azim.

Similar presentations


Presentation on theme: "1 Introduction to Randomized Algorithms Md. Aashikur Rahman Azim."— Presentation transcript:

1 1 Introduction to Randomized Algorithms Md. Aashikur Rahman Azim

2 2 Deterministic Algorithms Goal: Prove for all input instances the algorithm solves the problem correctly and the number of steps is bounded by a polynomial in the size of the input. ALGORITHM INPUT OUTPUT

3 3 Randomized Algorithms In addition to input, algorithm takes a source of random numbers and makes random choices during execution; Behavior can vary even on a fixed input; ALGORITHM INPUT OUTPUT RANDOM NUMBERS

4 4 Quick Sort Select: pick an arbitrary element x in S to be the pivot. Partition: rearrange elements so that elements with value less than x go to List L to the left of x and elements with value greater than x go to the List R to the right of x. Recursion: recursively sort the lists L and R.

5 5 Worst Case Partitioning of Quick Sort T(n) = T(n-1) + T(0) + Theta (n) = T(n-1) + Theta(n) Proof it mathematically.

6 6 Best Case Partitioning of Quick Sort T(n) <= 2T(n/2) + Theta(n) Proof it mathematically.

7 7 Average Case of Quick Sort T(n) <= T(9n/10) + T(n/10) + cn

8 8

9 9 Randomized 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)

10 10 Randomized Quick Sort Exchange A[r] with an element chosen at random from A[p…r] in Partition. The pivot element is equally likely to be any of input elements. For any given input, the behavior of Randomized Quick Sort is determined not only by the input but also by the random choices of the pivot. We add randomization to Quick Sort to obtain for any input the expected performance of the algorithm to be good.

11 11 Analysis of Randomized Quick Sort

12 12 Linearity of Expectation If X 1, X 2, …, X n are random variables, then

13 13 Notation Rename the elements of A as z 1, z 2,..., z n, with z i being the i th smallest element (Rank “i”). Define the set Z ij = {z i, z i+1,..., z j } be the set of elements between z i and z j, inclusive. 10 6145389 7 2 z1z1 z2z2 z9z9 z8z8 z5z5 z3z3 z4z4 z6z6 z 10 z7z7

14 14 Expected Number of Total Comparisons in PARTITION Let X ij = I {z i is compared to z j } Let X be the total number of comparisons performed by the algorithm. Then by linearity of expectation indicator random variable The expected number of comparisons performed by the algorithm is

15 15 Comparisons in PARTITION Observation 1: Each pair of elements is compared at most once during the entire execution of the algorithm –Elements are compared only to the pivot point! –Pivot point is excluded from future calls to PARTITION Observation 2: Only the pivot is compared with elements in both partitions z1z1 z2z2 z9z9 z8z8 z5z5 z3z3 z4z4 z6z6 z 10 z7z7 10 6145389 7 2 Z 1,6 = {1, 2, 3, 4, 5, 6} Z 8,9 = {8, 9, 10}{7} pivot Elements between different partitions are never compared

16 16 Comparisons in PARTITION Case 1: pivot chosen such as: z i < x < z j –z i and z j will never be compared Case 2 : z i or z j is the pivot –z i and z j will be compared –only if one of them is chosen as pivot before any other element in range z i to z j 10 6145389 7 2 z1z1 z2z2 z9z9 z8z8 z5z5 z3z3 z4z4 z6z6 z 10 z7z7 Z 1,6 = {1, 2, 3, 4, 5, 6} Z 8,9 = {8, 9, 10}{7}

17 17 Expected Number of Comparisons in PARTITION Pr {Z i is compared with Z j } = Pr{Z i or Z j is chosen as pivot before other elements in Z i,j } = 2 / (j-i+1) = O(nlgn)


Download ppt "1 Introduction to Randomized Algorithms Md. Aashikur Rahman Azim."

Similar presentations


Ads by Google