Docsity

Average Case Analysis - Algorithm and Complexity Analysis - Lecture Slides, Slides of Computer Science

Toggle sidebar
bg1
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
Docsity.com
pf3

Partial preview of the text

Download Average Case Analysis - Algorithm and Complexity Analysis - Lecture Slides and more Slides Computer Science in PDF only on Docsity!

Average Case Analysis

Beyond Worst Case Analysis

Worst-case analysis.^ n

Analyze running time as function of worst input of a given size. Average case analysis.^ n

Analyze average running time over some distribution of inputs. n^

Ex: quicksort. Amortized analysis.^ n

Worst-case bound on sequence of operations. n^

Ex: splay trees, union-find. Competitive analysis.^ n

Make quantitative statements about online algorithms. n^

Ex: paging, load balancing.

3

Average Case Analysis

Average case analysis.^ n

Analyze average running time over some distribution of inputs. n^

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 runningtime, independent of input n^

Major disadvantage: hard to quantify what input distributions willlook like in practice.

Quicksort

Quicksort.^ n

Assume all elements are unique. n^

Assume input is a random permutation of inputs. n^

Denote i

th^

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

Docsity.com

5

Quicksort: BST Representation of Pivots

pivot elements

Probability that i = 2 and j = 7 get compared.^ n

Let x be pivot element that separates i and j. n^

Case 1: x

i and j not compared.

Quicksort: Average Case Analysis

7

Probability that i = 2 and j = 7 get compared.^ n

Let x be pivot element that separates i and j. n^

Case 1: x

i and j not compared.

n^

Case 2: x

i and j are compared.

Quicksort: Average Case Analysis

Probability that i = 2 and j = 7 get compared.^ n

Let x be pivot element that separates i and j. n^

Case 1: x

i and j not compared.

n^

Case 2: x

i and j are compared.

Quicksort: Average Case Analysis

]

compared

and Pr[

i j

)j i(

j

i

Docsity.com