Daniel Sutantyo
Department of Computing
Macquarie University
2
3
...
15
16
...
[2]
[3]
...
[3,5]
...
input
output
Complexity analysis of algorithms
Correctness analysis of algorithms
Learn how to write efficient algorithms
Learn how to write correct algorithms
Learn how to design good algorithms
no intermediate cities
1 intermediate city
\((n-2)\) choices
\(\vdots\)
A
A
B
B
\(\vdots\)
A
B
2 intermediate cities
\((n-2)(n-3)\) choices
1 choice
$$ 1 + (n-2) + (n-3)(n-2) + (n-4)(n-3)(n-2) + \cdots + \binom{n-2}{k}k! $$
(via 1 city)
(via 2 cities)
(via 3 cities)
(via \(k\) cities)
50! = 30,414,093,201,713,378,043,612,608,166,064,768,844,377,641,568,960,512,000,000,000,000
13
63
44
22
93
21
72
67
13
63
44
22
93
21
72
67
44
13
22
72
21
63
93
67
4 comparisons
3 comparisons
3 comparisons
Cartoon by Stefan Szeider, available at https://www.ac.tuwien.ac.at/people/szeider/cartoon/
Cartoon by Stefan Szeider, available at https://www.ac.tuwien.ac.at/people/szeider/cartoon/
Cartoon by Stefan Szeider, available at https://www.ac.tuwien.ac.at/people/szeider/cartoon/
2
3
1
4
5
6
7
sorted
unsorted
1
2
4
6
3
5
7
sorted
unsorted
Best case:
Worst case:
7
6
4
2
5
3
1
sorted
unsorted
Skiena, Figure 2.2, page 35
\(n\) \(n^2\) \(n^{20}\) \(2^n\) \(2^{n+3}\)
\(100\)
\(200\)
\(1 \times 10^4\)
\(1 \times 10^{40}\)
\(1.26 \times 10^{30}\)
\(4 \times 10^4\)
\(1.05 \times 10^{46}\)
\(1.61 \times 10^{60}\)
\(300\)
\(9 \times 10^4\)
\(3.49 \times 10^{49}\)
\(2.04 \times 10^{90}\)
\(400\)
\(1.6 \times 10^5\)
\(1.10 \times 10^{52}\)
\(2.58 \times 10^{120}\)
\(1.01 \times 10^{31}\)
\(1.28 \times 10^{61}\)
\(1.63 \times 10^{91}\)
\(2.07 \times 10^{121}\)
Skiena Figure 2.3, page 36
\(f(n)\)
\(c\cdot g(n)\)
\(3n^2 + 100n + 25 \le 100n^2 + 100n^2 + 100n^2 = 300n^2\)
i.e. use \(c = 300\) and \(n_0 = 1\)
for all \(n \ge 1\)
(that wasn't hard to prove, was it?)
for all \(n \ge 1\)
Skiena Figure 2.3, page 36
\(c\cdot g(n)\)
\( f(n)\)
Skiena Figure 2.3, page 36
\(f(n)\)
\(c_2\cdot g(n)\)
\(c_1\cdot g(n)\)
\(T(n) = \begin{cases} \Theta(1) &\text{if $n \le c$}\\ aT(n/b) + D(n) + C(n) &\text{otherwise} \end{cases}\)
where
(from CLRS, Figure 2.4, page 35)
\(T(n) = \begin{cases} \Theta(1) &\text{if $n = 1$}\\ 2T(n/2) + \Theta(n) &\text{if $n > 1$} \end{cases}\)
\(T(n) = \begin{cases} \Theta(1) &\text{if $n = 1$}\\ 2T(n/2) + \Theta(n) &\text{if $n > 1$} \end{cases}\)
\(T(n) = \begin{cases} c &\text{if $n = 1$}\\ 2T(n/2) + cn &\text{if $n > 1$} \end{cases}\)
\(T(n) = \begin{cases} \Theta(1) &\text{if $n = 1$}\\ T(n/2) + \Theta(1) &\text{if $n > 1$} \end{cases}\)
1
1
1
1
\(\log n\)
so binary search is \(\Theta(\log n)\)
LHS :
\[ \sum_{i=1}^1 i = 1\]
\[\frac{1(1+1)}{2} = 1\]
RHS :
\[\sum_{i=1}^n i = \frac{n(n+1)}{2}\]
\[ \sum_{i=1}^k i = \frac{k(k+1)}{2} \text{for some $k \ge 1, k \in \mathbb N$}\]
Remember, the statement we need to prove is
\[\sum_{i=1}^n i = \frac{n(n+1)}{2}\]
\[\sum_{i=1}^{k+1} i = (k+1) + \sum_{i=1}^{k} i \]
\[= (k+1) + \frac{k(k+1)}{2} \]
\[= \frac{2k+2 + k^2 + k}{2} = \frac{k^2 +3k + 2}{2}\]
\[= \frac{(k+1)(k+2)}{2}\]
(as required)
(from the induction hypothesis)
(from CLRS, Appendix A, page 1150)
\[\sum_{i=1}^n i = O(n) \]
\[\sum_{i=1}^{k+1} i = (k+1) + \sum_{i=1}^k i = O(k) + k+1 = O(k+1)\]
\[\sum_{i=1}^k i = O(k)\]
\[\sum_{i=1}^{k+1} i = O(k+1)\]
WRONG!
\[\sum_{i=1}^n (i \times i!) = (n+1)! - 1 \]
\[\sum_{i=1}^k (i \times i!) = (k+1)! - 1 \]
\[\sum_{i=1}^{k+1} (i \times i!) = (k+2)! - 1 \]
\[\sum_{i=1}^{k+1} (i \times i!) = (k+1) \times (k+1)! + \sum_{i=1}^k (i \times i!) \]
\[= (k+1) \times (k+1)! + (k+1)! - 1\]
\[= (k+1)! ( k+1+1) - 1\]
\[= (k+2)(k+1)! - 1\]
\[= (k+2)! - 1\]
as required
\(2^n > n^2\) for \(n \ge 5\)
\(2^{k+1} = 2\cdot 2^k > 2k^2 = k^2 + k^2 \)
since \(k \ge 5\), we have \(k^2 \ge 5k\), hence
\(2^{k+1} > k^2 + 5k > k^2 + 2k + 1 = (k+1)^2\)
as required