Written byKrung Sinapiromsaran
July 2557
Recall \begin{array}{lcl} f(n) = o(g(n)) & \mbox{iff} & \lim_{n \rightarrow \infty} \frac{f(n)}{g(n)} = 0 \\ f(n) = \omega(g(n)) & \mbox{iff} & \lim_{n \rightarrow \infty} \frac{f(n)}{g(n)} = \infty \\ f(n) = \theta(g(n)) & \mbox{iff} & \lim_{n \rightarrow \infty} \frac{f(n)}{g(n)} = r > 0 \end{array}
Including some more obsecure classes of complexity \begin{array}{l} n^n >> n! >> c^n >> n^3 >> n^2 >> n^{1+\varepsilon} >> \\ n \log n >> n >> \sqrt{n} >> \log^2 n >> \log n >> \\ \frac{\log n}{\log\log n} >> \log\log n >> \alpha(n) >> 1\end{array}
Name | T = f(n) |
---|---|
Constant | 1 |
Logarithmic | \log n |
Linear | n |
Log-linear (or linearithmic) | n \log n |
Quadratic | n^2 |
Cubic | n^3 |
Polynomial | n^k |
Exponential | b^n |
On a computer executing 1 instruction per nanosecond, running time for varying input size:
IBM Roadrunner 1.026 quadrillion calculations per second (1.026 \times 10^15) Running an Ο(n^2) algorithm
2.3 million times slower, stacked on top of each other would be 27km high!
But we’re going to beat Roadrunner with one iPhone using a better algorithm,
Apple iPhone 450 million calculations per second Running an Ο(n \log n) algorithm
No. items | iPhone | Roadrunner |
---|---|---|
1 million | 0.03 sec. | 0.0009 sec. |
100 million | 4.1 sec. | 9.75 sec. |
1000 million | 46 sec. | 974 sec. |
statement 1
statement 2
if condition is true then
statement 1
else
statement 2
endif
while condition is true do
statement 1
endwhile
for i := 1 to n do
s := s + 1
endfor
for i := 1 to n do
for j := 1 to n do
s := s + 1
endfor
endfor
for i := 1 to n do
for j := 1 to i do
s := s + 1
endfor
endfor
Move n discs from A to B using C as a spare observing following rules:
Function ToH(n:N; source;dest;spare:Pole)
1. If n = 1 then
2. Move one disc from source to dest
3. else
4. ToH(n-1, source,spare,dest)
5. ToH(1,source,dest,spare)
6. ToH(n-1,spare,dest,source)
7. endif
T(n)
Name | T = f(n) |
---|---|
Constant | 1 |
Logarithmic | \log n |
Linear | n |
Log-linear (or linearithmic) | n \log n |
Quadratic | n^2 |
Cubic | n^3 |
Polynomial | n^k |
Exponential | b^n |
Q: Where do we draw the line? What is an “efficient” algorithm?
Is there a limit to what we can consider feasible?
Solve “P = NP?” and EARN \$1,000,000