princeton university cos 423 theory of algorithms spring 2001 kevin wayne average case analysis
TRANSCRIPT
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.
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.
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
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
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
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
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
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
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
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