Daniel Sutantyo, Department of Computing, Macquarie University
9.1 - Probabilistic Analysis
int max = 0;
for (int i = 0; i < n; i++){
if (arr[i] > max){
max = arr[i];
}
}
9.1 - Probabilistic Analysis
int max = 0;
for (int i = 0; i < n; i++){
if (arr[i] > max){
max = arr[i];
}
}
let M = element 1
for i = 2 to n:
compare element i with M
if M is less than element i:
assign i to M
9.1 - Probabilistic Analysis
let M = element 1
for i = 2 to n:
compare element i with M // cost is c_c
if M is less than element i:
assign i to M // cost is c_a
9.1 - Probabilistic Analysis
let M = element 1
for i = 2 to n:
compare element i with M // cost is c_c
if M is less than element i:
assign i to M // cost is c_a
9.1 - Probabilistic Analysis
let M = element 1
for i = 2 to n:
compare element i with M // cost is c_c
if M is less than element i:
assign i to M // cost is c_a
9.1 - Probabilistic Analysis
9.1 - Probabilistic Analysis
1, 2, 3, 4
1, 2, 4, 3
1, 3, 2, 4
1, 3, 4, 2
1, 4, 2, 3
1, 4, 3, 2
2, 1, 3, 4
2, 1, 4, 3
2, 3, 1, 4
2, 3, 4, 1
2, 4, 1, 3
2, 4, 3, 1
3, 1, 2, 4
3, 1, 4, 2
3, 2, 1, 4
3, 2, 4, 1
3, 4, 1, 2
3, 4, 2, 1
4, 1, 2, 3
4, 1, 3, 2
4, 2, 1, 3
4, 2, 3, 1
4, 3, 1, 2
4, 3, 2, 1
9.1 - Probabilistic Analysis
1, 2, 3, 4
1, 2, 4, 3
1, 3, 2, 4
1, 3, 4, 2
1, 4, 2, 3
1, 4, 3, 2
2, 1, 3, 4
2, 1, 4, 3
2, 3, 1, 4
2, 3, 4, 1
2, 4, 1, 3
2, 4, 3, 1
3, 1, 2, 4
3, 1, 4, 2
3, 2, 1, 4
3, 2, 4, 1
3, 4, 1, 2
3, 4, 2, 1
4, 1, 2, 3
4, 1, 3, 2
4, 2, 1, 3
4, 2, 3, 1
4, 3, 1, 2
4, 3, 2, 1
9.1 - Probabilistic Analysis
1, 2, 3, 4
1, 2, 4, 3
1, 3, 2, 4
1, 3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, 2, 1, 3
4, 2, 3, 1
4, 3, 1, 2
4, 3, 2, 1
9.1 - Probabilistic Analysis
1, 2, 3, 4
1, 2, 4, 3
1, 3, 2, 4
1, 3, 4, 2
1, 4, 2, 3
1, 4, 3, 2
2, 1, 3, 4
2, 1, 4, 3
2, 3, 1, 4
2, 3, 4, 1
2, 4, 1, 3
2, 4, 3, 1
3, 1, 2, 4
3, 1, 4, 2
3, 2, 1, 4
3, 2, 4, 1
3, 4, 1, 2
3, 4, 2, 1
4, 1, 2, 3
4, 1, 3, 2
4, 2, 1, 3
4, 2, 3, 1
4, 3, 1, 2
4, 3, 2, 1
1
1
1
1
1
1
2
2
2
2
2
2
3
2
3
3
2
2
4
3
3
3
2
2
9.1 - Probabilistic Analysis
n | permutation | assignments | average |
---|---|---|---|
4 | 24 | 50 | 2.083 |
5 | 120 | 274 | 2.283 |
6 | 720 | 1764 | 2.450 |
7 | 5040 | 13,068 | 2.593 |
8 | 40320 | 109,584 | 2.718 |
9 | 362,880 | 1,026,576 | 2.829 |
10 | 3,628,800 | 10,628,640 | 2.929 |
11 | 39,916,800 | 120,543,840 | 3.020 |
12 | 479,001,600 | 1,486,442,880 | 3.103 |
9.1 - Probabilistic Analysis
n | permutation | assignments | average |
---|---|---|---|
8 | 40320 | 109,584 | 2.718 |
9 | 362,880 | 1,026,576 | 2.829 |
10 | 3,628,800 | 10,628,640 | 2.929 |
11 | 39,916,800 | 120,543,840 | 3.020 |
12 | 479,001,600 | 1,486,442,880 | 3.103 |
9.1 - Probabilistic Analysis
9.1 - Probabilistic Analysis
\[x_1, \qquad x_2, \qquad x_3, \qquad x_4, \qquad \dots\]
Expected number of assignments: 1
have to do one assignment
9.1 - Probabilistic Analysis
have to do one assignment if \(x_2 > x_1\), 1 in 2 chance
\[x_1, \qquad x_2, \qquad x_3, \qquad x_4, \qquad \dots\]
Expected number of assignments: 1 + 0.5
9.1 - Probabilistic Analysis
have to do one assignment if \(x_3 > x_1\) and \(x_3 > x_2\), 1 in 3 chance
\[x_1, \qquad x_2, \qquad x_3, \qquad x_4, \qquad \dots\]
Expected number of assignments: 1 + 0.5 + 0.33
9.1 - Probabilistic Analysis
have to do one assignment if \(x_4 > x_1\), \(x_4 > x_2\), and \(x_4 > x_3\), 1 in 4 chance
\[x_1, \qquad x_2, \qquad x_3, \qquad x_4, \qquad \dots\]
Expected number of assignments: 1 + 0.5 + 0.33 + 0.25
9.1 - Probabilistic Analysis
Expected number of assignments: 1 + 0.5 + 0.33 + 0.25
\[ = \sum_{i=1}^n \frac{1}{i} \]
(this is called a harmonic series)
\[ \le \log n + 1\]
\[ 1 + \left(1 \times \frac{1}{2}\right) + \left(1 \times\frac{1}{3}\right) + \cdots + \left(1 \times \frac{1}{n}\right)\]
9.1 - Probabilistic Analysis
\[ = \sum_{i=1}^n \frac{1}{i} \]
(this is called a harmonic series)
\[ \le \log n + 1\]
\[ 1 + \left(1 \times \frac{1}{2}\right) + \left(1 \times\frac{1}{3}\right) + \cdots + \left(1 \times \frac{1}{n}\right)\]
9.1 - Probabilistic Analysis
\[ \frac{1}{n} + \frac{2}{n} + \frac{3}{n} + \cdots + \frac{n}{n} = \frac{1}{n}\sum_{i=1}^{n} i\]
\[= \frac{1}{n} \left(\frac{n(n+1)}{2}\right) = \frac{(n+1)}{2} = O(n)\]
9.1 - Probabilistic Analysis