Daniel Sutantyo
Department of Computing
Macquarie University
int max = 0;
for (int i = 0; i < n; i++){
if (arr[i] > max){
max = arr[i];
}
}int max = 0;
for (int i = 0; i < n; i++){
if (arr[i] > max){
max = arr[i];
}
}assign element 1 to M
for i = 2 to n:
compare element i with M
if M is less than element i:
assign i to Massign element 1 to M
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_athere is a total of \(n!\) possible combinations
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, 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
4
3
3
3
2
2
1
1
1
1
1
1
2
2
2
2
2
2
3
2
3
3
2
2
| 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 |
(a little distraction)
(a little distraction)
(a little distraction)
(a little distraction)
(a little distraction)
(a little distraction)
| n | permutation | assignments | average |
|---|---|---|---|
| 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 |
\[ E[X] = \sum_{i=1}^k x_ip_i = x_1p_1 + \cdots + x_kp_k\]
\[ = \sum_{i=1}^n \frac{1}{i} \]
(this is called 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)\]
assign element 1 to M
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_arandomise the list of elements
assign element 1 to M
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(a bit more distraction)
(a bit more distraction)
randomised algorithms vs probabilistic analysis
randomised algorithms vs probabilistic analysis
Deterministic algorithm
"Here is one input, what is the average running time?"
/\_/\
(='_' )
(, (") (")
Probabilistic algorithm
"Here is one input, what is the expected running time?"
(\____/)
( ͡ ͡° ͜ ʖ ͡ ͡°)
\╭☞ \╭☞
why?
Monte Carlo and Las Vegas algorithms
Monte Carlo and Las Vegas algorithms
Las Vegas Search Algorithm
Random rgen = new Random();
int x = 0;
while (x != target){
x = a[rgen.nextInt(a.length)];
}Monte Carlo Search Algorithm
Random rgen = new Random();
int x = 0;
while (x != target && count < max){
count++;
x = a[rgen.nextInt(a.length)];
}Monte Carlo and Las Vegas algorithms
(minor) examples
also known as permutation sort, stupid sort, slowsort,
&#%@sort, shotgun sort, and monkey sort
Quicksort
Quicksort
Quicksort
(we will cover this in the workshop)
Randomised Quicksort
Randomised Quicksort
Matrix Multiplication Verification
\[\begin{bmatrix} 5 & 5 & 3 \\ 7 & 1 & 3 \\ 4 & 1 & 5 \end{bmatrix}\]
\[\begin{bmatrix} 9 & 3 & 2 \\ 4 & 2 & 1 \\ 7 & 7 & 7 \end{bmatrix}\]
\[\begin{bmatrix} 86 & 46 & 36 \\ 88 & 44 & 36 \\ 75 & 49 & 44\end{bmatrix}\]
?=?
Matrix Multiplication Verification
\[\begin{bmatrix} 5 & 5 & 3 \\ 7 & 1 & 3 \\ 4 & 1 & 5 \end{bmatrix}\]
\[\begin{bmatrix} 1 \\ 1 \\ 1 \end{bmatrix}\]
\[\begin{bmatrix} 86 & 46 & 36 \\ 88 & 44 & 36 \\ 75 & 49 & 44\end{bmatrix}\]
\[\begin{bmatrix} 9 & 3 & 2 \\ 4 & 2 & 1 \\ 7 & 7 & 7 \end{bmatrix}\]
\[\begin{bmatrix} 1 \\ 1 \\ 1 \end{bmatrix}\]
\[\begin{bmatrix} 168 \\ 168 \\ 168 \end{bmatrix}\]
\[\begin{bmatrix} 168 \\ 168 \\ 168 \end{bmatrix}\]
=
=
Matrix Multiplication Verification
\[\begin{bmatrix} 5 & 5 & 3 \\ 7 & 1 & 3 \\ 4 & 1 & 5 \end{bmatrix}\]
\[\begin{bmatrix} 1 \\ 1 \\ 1 \end{bmatrix}\]
\[\begin{bmatrix} 86 & 46 & 36 \\ 88 & 44 & 36 \\ 75 & 49 & 44\end{bmatrix}\]
\[\begin{bmatrix} 9 & 3 & 2 \\ 4 & 2 & 1 \\ 7 & 7 & 7 \end{bmatrix}\]
\[\begin{bmatrix} 1 \\ 1 \\ 1 \end{bmatrix}\]
\[\begin{bmatrix} 168 \\ 168 \\ 168 \end{bmatrix}\]
\[\begin{bmatrix} 168 \\ 168 \\ 168 \end{bmatrix}\]
=
=
can be done in \(O(n^2)\)
sure!
\[\begin{bmatrix} 14 \\ 7 \\ 21 \end{bmatrix}\]
\[\begin{bmatrix} 5 & 5 & 3 \\ 7 & 1 & 3 \\ 4 & 1 & 5 \end{bmatrix}\]
\[\begin{bmatrix} 168 \\ 168 \\ 168 \end{bmatrix}\]
=
can this be done in \(O(n^2)\)?
Matrix Multiplication Verification
Matrix Multiplication Verification
Matrix Multiplication Verification
Summary
simple methods
| 10,000 | 100 |
| 20,000 | 141 |
| 30,000 | 173 |
| 40,000 | 200 |
| 50,000 | 223 |
\(n\)
\(\sqrt{n}\)
| 14 |
| 15 |
| 15 |
| 16 |
| 16 |
input size
simple methods
| 14 | 16383 | 127 |
| 15 | 32767 | 181 |
| 16 | 65535 | 255 |
| 17 | 131017 | 362 |
| 18 | 262143 | 511 |
input size
\(\sqrt{n}\)
\(n\)
simple methods
simple methods
Fermat's primality test
\[a^{p-1} \equiv 1 \pmod p\]
Fermat's primality test
\[a^{p-1} \equiv 1 \pmod p\]
\[ax \equiv bx \mod p\]
if and only if \(a = b\)
\(a, 2a, 3a, 4a, \dots, (p-1)a\) are all distinct
\(a\times 2a\times \dots \times(p-1)a = 1\times2\times3 \times \dots\times(p-1) \pmod p\)
\(a^{p-1} = 1\pmod p\)
Fermat's primality test
Fermat's primality test
\[a^{n-1} \equiv 1 \pmod n\]
Fermat's primality test
\[a^{n-1} \equiv 1 \pmod n\]
Miller-Rabin test
\[a^{n-1} \equiv 1 \pmod n\]
Miller-Rabin test
Miller-Rabin test
Miller-Rabin test
Miller-Rabin test
Miller-Rabin test