Daniel Sutantyo, Department of Computing, Macquarie University
9.2 - Randomised Algorithms
9.2 - Randomised Algorithms
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.2 - Randomised Algorithms
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.2 - Randomised Algorithms
9.2 - Randomised Algorithms
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.2 - Randomised Algorithms
9.2 - Randomised Algorithms
9.2 - Randomised Algorithms
Deterministic algorithm
"Here is one input, what is the average running time?"
/\_/\ ???
(='_' )
(, (") (")
Probabilistic algorithm
"Here is one input, what is the expected running time?"
(\____/) i gots you
( ͡ ͡° ͜ ʖ ͡ ͡°)
\╭☞ \╭☞
9.2 - Randomised Algorithms
9.2 - Randomised Algorithms
9.2 - Randomised Algorithms
3
6
4
1
1
7
2
9.2 - Randomised Algorithms
3
6
4
1
1
7
2
9.2 - Randomised Algorithms
3
6
4
1
1
7
2
9.2 - Randomised Algorithms
\(n\)
\(\frac{n}{2}\)
\(\frac{n}{4}\)
\(\frac{n}{4}\)
\(\frac{n}{2}\)
\(\frac{n}{4}\)
\(\frac{n}{4}\)
9.2 - Randomised Algorithms
3
1
2
1
4
6
7
3
1
2
4
6
7
3
2
4
6
7
3
4
6
7
4
6
7
9.2 - Randomised Algorithms
\(n\)
\(n-1\)
\(n-2\)
\(n-3\)
9.2 - Randomised Algorithms
9.2 - Randomised Algorithms
9.2 - Randomised Algorithms
9.2 - Randomised Algorithms
\[\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}\]
?=?
9.2 - Randomised Algorithms
\[\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}\]
=
=
9.2 - Randomised Algorithms
\[\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)\)
can this be done in \(O(n^2)\)
9.2 - Randomised Algorithms
\[\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)\)
can this be done in \(O(n^2)\)
yes!
\[\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}\]
=
9.2 - Randomised Algorithms
9.2 - Randomised Algorithms
9.2 - Randomised Algorithms
\(a^{p-1} \equiv 1 \pmod p\) for primes \(p\)
9.2 - Randomised Algorithms
9.2 - Randomised 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)];
}
9.2 - Randomised Algorithms
9.2 - Randomised Algorithms
9.2 - Randomised Algorithms
9.2 - Randomised Algorithms