ws 2006-07 algorithmentheorie 03 – randomized algorithms (overview and randomised quicksort) prof....

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

Upload: jasmine-hopkins

Post on 18-Dec-2015

212 views

Category:

Documents


0 download

TRANSCRIPT

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

WS 2006-07

Algorithmentheorie

03 – Randomized Algorithms(Overview and randomised Quicksort)

Prof. Dr. Th. Ottmann

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

2 WS 2006-07

Randomized algorithms

• Classes of randomized algorithms• Randomized Quicksort• Randomized algorithm for Closest Pair• Randomized primality test• Cryptography

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

3 WS 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

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

4 WS 2006-07

Quicksort

A[l … r-1] p

pA[l...m – 1] A[m + 1...r]

Quicksort Quicksort

Unsorted range A[l, r] in array A

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

5 WS 2006-07

Quicksort

Algorithm: Quicksort

Input: unsorted range [l, r] in array A

Output: sorted range [l, r] in array A

1 if 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)

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

6 WS 2006-07

The divide step

l r

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

7 WS 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)

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

8 WS 2006-07

Worst-case input

n elements:

Running time: (n-1) + (n-2) + … + 2 + 1 = n·(n-1)/2

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

9 WS 2006-07

Randomized Quicksort

Algorithm: Quicksort

Input: unsorted range [l, r] in array A

Output: sorted range [l, r] in array A

1 if 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)

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

10 WS 2006-07

Analysis 1

n elements; let Si be the i-th smallest element

S1 is chosen as pivot with probability 1/n: Sub-problems of sizes 0 and n-1

Sk is chosen as pivot with probability 1/n:Sub-problems of sizes k-1 and n-k

Sn is chosen as pivot with probability 1/n:Sub-problems of sizes n-1 and 0

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

11 WS 2006-07

Analysis 1

Expected running time:

nnO

nkTn

nknTkTn

nT

n

k

n

k

log

12

11

1

1

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

12 WS 2006-07

Analysis 2: Representation of Quicksort as a tree

= S6 S2 S8 S1 S4 S7 S9 S3 S5

S6

S5

S1

S3

S4

S2

S7

S8

S9

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

13 WS 2006-07

Analysis 2

Expected number of comparisons:

ijij

n

iijij

n

i

XEXE ][11

ijijijij pppXE )1(01][

pij = probability that Si is compared with Sj

otherwise0

with compared is if1 jiij

SSX

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

14 WS 2006-07

Calculation of pij

Si is compared with Sj iff Si or Sj is chosen as pivot element in before any other Sl , i<l<j.

{Si … Sl … Sj}

Each of the elements Si , … , Sj is chosen first as the pivot with the same probability.

Sl

{… Si … Sl … Si …}

pij = 2/(j-i+1)

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

15 WS 2006-07

Analysis 2

n

k

n

k

n

i

in

k

n

i

ij

n

iijij

n

i

kn

k

k

ijp

1

11

1

21

11

12

12

2

1

2

nkHn

kn ln/1

1

Expected number of comparisons: