Presentation is loading. Please wait.

Presentation is loading. Please wait.

WS 2006-07 Algorithmentheorie 03 – Randomized Algorithms (Overview and randomised Quicksort) Prof. Dr. Th. Ottmann.

Similar presentations


Presentation on theme: "WS 2006-07 Algorithmentheorie 03 – Randomized Algorithms (Overview and randomised Quicksort) Prof. Dr. Th. Ottmann."— Presentation transcript:

1 WS 2006-07 Algorithmentheorie 03 – Randomized Algorithms (Overview and randomised Quicksort) Prof. Dr. Th. Ottmann

2 2WS 2006-07 Randomized algorithms Classes of randomized algorithms Randomized Quicksort Randomized algorithm for Closest Pair Randomized primality test Cryptography

3 3WS 2006-07 Classes of randomized algorithms Las Vegas algorithms always correct; expected running time (“probably fast”) Examples: randomized Quicksort, randomized algorithm for closest pair Monte Carlo algorithms (mostly correct): probably correct; guaranteed running time Example: randomized primality test

4 4WS 2006-07 Quicksort A[l … r-1]p pA[l...m – 1]A[m + 1...r] Quicksort Unsorted range A[l, r] in array A

5 5WS 2006-07 Quicksort Algorithm: Quicksort Input: unsorted range [l, r] in array A Output: sorted range [l, r] in array A 1if r > l 2 then choose pivot element p = A[r] 3 m = divide(A, l, r) /* Divide A according to p: A[l],....,A[m – 1]  p  A[m + 1],...,A[r] */ 4 Quicksort(A, l, m - 1) 5 Quicksort (A, m + 1, r)

6 6WS 2006-07 The divide step l r

7 7WS 2006-07 The divide step divide(A, l, r): returns the index of the pivot element in A can be done in time O(r – l)

8 8WS 2006-07 Worst-case input n elements: Running time: (n-1) + (n-2) + … + 2 + 1 = n·(n-1)/2

9 9WS 2006-07 Randomized Quicksort Algorithm: Quicksort Input: unsorted range [l, r] in array A Output: sorted range [l, r] in array A 1if r > l 2 then randomly choose a pivot element p = A[i] in range [l, r] 3 swap A[i] and A[r] 4 m = divide(A, l, r) /* Divide A according to p: A[l],....,A[m – 1]  p  A[m + 1],...,A[r] */ 5 Quicksort(A, l, m - 1) 6 Quicksort(A, m + 1, r)

10 10WS 2006-07 Analysis 1 n elements; let S i be the i-th smallest element S 1 is chosen as pivot with probability 1/n: Sub-problems of sizes 0 and n-1 S k is chosen as pivot with probability 1/n: Sub-problems of sizes k-1 and n-k S n is chosen as pivot with probability 1/n: Sub-problems of sizes n-1 and 0

11 11WS 2006-07 Analysis 1 Expected running time:

12 12WS 2006-07 Analysis 2: Representation of Quicksort as a tree  = S 6 S 2 S 8 S 1 S 4 S 7 S 9 S 3 S 5 S6S6 S5S5 S1S1 S3S3 S4S4 S2S2 S7S7 S8S8 S9S9

13 13WS 2006-07 Analysis 2 Expected number of comparisons: p ij = probability that S i is compared with S j

14 14WS 2006-07 Calculation of p ij  S i is compared with S j iff S i or S j is chosen as pivot element in  before any other S l, i<l<j. {S i … S l … S j }  Each of the elements S i, …, S j is chosen first as the pivot with the same probability. SlSl {… S i … S l … S i …} p ij = 2/(j-i+1)

15 15WS 2006-07 Analysis 2 Expected number of comparisons:


Download ppt "WS 2006-07 Algorithmentheorie 03 – Randomized Algorithms (Overview and randomised Quicksort) Prof. Dr. Th. Ottmann."

Similar presentations


Ads by Google