princeton university cos 423 theory of algorithms spring 2001 kevin wayne average case analysis

11
Princeton University COS 423 Theory of Algorithms Spring 2001 Kevin Wayne Average Case Analysis

Upload: solomon-robbins

Post on 13-Dec-2015

220 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Princeton University COS 423 Theory of Algorithms Spring 2001 Kevin Wayne Average Case Analysis

Princeton University • COS 423 • Theory of Algorithms • Spring 2001 • Kevin Wayne

Average Case Analysis

Page 2: Princeton University COS 423 Theory of Algorithms Spring 2001 Kevin Wayne Average Case Analysis

2

Beyond Worst Case Analysis

Worst-case analysis. Analyze running time as function of worst input of a given size.

Average case analysis. Analyze average running time over some distribution of inputs. Ex: quicksort.

Amortized analysis. Worst-case bound on sequence of operations. Ex: splay trees, union-find.

Competitive analysis. Make quantitative statements about online algorithms. Ex: paging, load balancing.

Page 3: Princeton University COS 423 Theory of Algorithms Spring 2001 Kevin Wayne Average Case Analysis

3

Average Case Analysis

Average case analysis. Analyze average running time over some distribution of inputs. Ex: quicksort.

– O(N log N) if input is assumed to be in random order.– leads to randomized algorithm with O(N log N) expected running

time, independent of input Major disadvantage: hard to quantify what input distributions will

look like in practice.

Page 4: Princeton University COS 423 Theory of Algorithms Spring 2001 Kevin Wayne Average Case Analysis

4

Quicksort

Quicksort. Assume all elements are unique. Assume input is a random permutation of inputs. Denote ith largest element by i.

void quicksort(Item a[], int left, int right) { int m; if (right > left) { m = partition(a, left, right); quicksort(a, left, m - 1); quicksort(a, m + 1, right); }}

quicksort.c

Page 5: Princeton University COS 423 Theory of Algorithms Spring 2001 Kevin Wayne Average Case Analysis

5

Quicksort: BST Representation of Pivots

10 pivot elements

7 6 12 3 8 7 1 15 5 16 14 91711 13 104

135

7 6 4 3 8 2 1 5 15 16 14 11179 1310 12

16113 9

1 2 4 3 8 6 7 9 11 1614 1512 1310 175

2 4 7 12 15 17

1 2 3 4 8 6 7 9 12 1714 1511 1310 165

1 6 8 14

1 2 3 4 76 8 9 12 1714 1511 1310 165

Page 6: Princeton University COS 423 Theory of Algorithms Spring 2001 Kevin Wayne Average Case Analysis

6

Probability that i = 2 and j = 7 get compared. Let x be pivot element that separates i and j. Case 1: x {3, 4, 5, 6} i and j not compared.

Quicksort: Average Case Analysis

10

135

16113 9

2 4 7 12 15 17

1 6 8 14

Page 7: Princeton University COS 423 Theory of Algorithms Spring 2001 Kevin Wayne Average Case Analysis

7

Probability that i = 2 and j = 7 get compared. Let x be pivot element that separates i and j. Case 1: x {3, 4, 5, 6} i and j not compared. Case 2: x {2, 7} i and j are compared.

Quicksort: Average Case Analysis

10

137

16113 9

2 5 8 12 15 17

1 144 6

Page 8: Princeton University COS 423 Theory of Algorithms Spring 2001 Kevin Wayne Average Case Analysis

8

Probability that i = 2 and j = 7 get compared. Let x be pivot element that separates i and j. Case 1: x {3, 4, 5, 6} i and j not compared. Case 2: x {2, 7} i and j are compared.

Quicksort: Average Case Analysis

12

] compared and Pr[

ij

)ji(ji

Page 9: Princeton University COS 423 Theory of Algorithms Spring 2001 Kevin Wayne Average Case Analysis

9

Quicksort: average case analysis.

Expected # of comparisons =

Remark: same analysis if we choose partitionelement uniformly at random.

Running time independent of input.

NNj

N

jN

jijN

j

N

i

i

jNji

ln2

12

12

12

1

2

1

1 21

12

] compared and Pr[

ij

)ji(ji

Quicksort: Average Case Analysis

Page 10: Princeton University COS 423 Theory of Algorithms Spring 2001 Kevin Wayne Average Case Analysis

10

Comparison Based Sorting Lower Bound

a1 < a2

a1 < a3

a2 < a3 a1 < a3

a2 < a32, 1, 3

2, 3, 1 3, 2, 11, 3, 2 3, 1, 2

1, 2, 3

YES

YES

YES

YES

YES

NO

NO

NO

NO

NO

Decision Tree of Program

Page 11: Princeton University COS 423 Theory of Algorithms Spring 2001 Kevin Wayne Average Case Analysis

11

Comparison Based Sorting Lower Bound

Lower bound = (N log2N): applies to comparison-based algorithms.

Tree height h determines worst case running time. N! different input orderings. One (or more) leaves corresponds to each ordering. Binary tree with N! leaves has height:

)log(

loglog

)/(log

)!(log

2

22

2

2

NN

eNNN

eN

NhN

Stirling's formula