Analyzing Algorithms part 2

Andy Novocin

Daily Problem:

Find \( c \geq 0 \) such that \( f(n) \leq c \cdot g(n) \) for all \( n \geq 2 \):

Families of functions

Grasping the infinite!

Largest Attackable Problem

Suppose you can reliably perform \(10^9\) operations per second
e.g. My laptop: 1.6GHz Dual Core so 3.2 Giga FLOPS

If you've designed an algorithm which uses \( 2^n \) operations


Largest Attackable Problem

If you've designed an algorithm which uses \( n^2 \) operations:


SoU 1 Second 1 Minute 1 Hour 1 Day 1 Year My Lifetime All of time
\( n^n \) 9 10 11 12 14 15 20
\( n! \) 12 13 15 16 18 19 26
\( 2^n \) 29 35 41 46 54 59 88
\( n^{14} \) 4 5 7 9 15 19 80
\( n^{10} \) 7 11 18 24 44 62 461
\( n^7 \) 19 34 62 97 227 371 6400
\( n^3 \) 512 \(2.05 \cdot 10^{3} \) \(8.19 \cdot 10^{3} \) \(3.28 \cdot 10^{4} \) \(2.62 \cdot 10^{5} \) \(5.24 \cdot 10^{5} \) \(5.37 \cdot 10^{8} \)
\( n^2 \) \(1.64 \cdot 10^{4} \) \(1.31 \cdot 10^{5} \) \(1.05 \cdot 10^{6} \) \(8.39 \cdot 10^{6} \) \(1.34 \cdot 10^{8} \) \(5.37 \cdot 10^{8} \) \(1.76 \cdot 10^{13} \)
\( n\sqrt{n} \) \(5.24 \cdot 10^{5} \) \(8.39 \cdot 10^{6} \) \(1.34 \cdot 10^{8} \) \(1.07 \cdot 10^{9} \) \(6.87 \cdot 10^{10} \) \(5.50 \cdot 10^{11} \) \(5.76 \cdot 10^{17} \)
\( n\log(n) \) \(6.71 \cdot 10^{7} \) \(4.29 \cdot 10^{9} \) \(2.75 \cdot 10^{11} \) \(4.40 \cdot 10^{12} \) \(1.13 \cdot 10^{15} \) \(3.60 \cdot 10^{16} \) \(9.67 \cdot 10^{24} \)
\( n \) \(1.00 \cdot 10^{9} \) \(6.00 \cdot 10^{10} \) \(3.60 \cdot 10^{12} \) \(8.64 \cdot 10^{13} \) \(3.15 \cdot 10^{16} \) \(9.78 \cdot 10^{17} \) \(4.42 \cdot 10^{26} \)
\( \sqrt{n} \) \(1.00 \cdot 10^{18} \) \(3.60 \cdot 10^{21} \) \(1.30 \cdot 10^{25} \) \(7.46 \cdot 10^{27} \) \(9.95 \cdot 10^{32} \) \(9.56 \cdot 10^{35} \) \(1.95 \cdot 10^{53} \)
\( \log{n} \) \(\geq 10^{10^{9}}\) \(\geq 10^{10^{10}}\) \(\geq 10^{10^{12}}\) \(\geq 10^{10^{13}}\) \(\geq 10^{10^{16}}\) \(\geq 10^{10^{17}}\) \(\geq 10^{10^{26}}\)

Traveling Salesman Problem


You have a route of \( n \) cities


Find shortest cycle

which visits all of them

and gets you home

Brute Force Solution?

Takes \( \mathcal{O}(n!) \)

Selection Sort

for (i = 0; i < n; i++){
  min = i;
  for (j=i+1; j < n; j++)
      if (s[j] < s[min]) min=j;
  swap(s[i], s[min]);
}

Count the worst-case swaps.

The Selection Sort Dance

Stretch, find partner, solve

2-21 True of False:

Challenge 2


Grab a book, open to page 730 (last page)


Count the number of pages you see as you:


Tell me what the Miller-Rabin algorithm does?

Dividing, Doubling, and Logs

Binary Search


Finding a name in the phone book:


Takes \( \mathcal{O}(\log{n}) \) page turns.

Why \( \log{n} \)?


Recall that \( b^p = x \iff \log_{b}{x} = p \)


When there is repeated doubling, after \(i\) iterations the total is \( n = 2^i\).


So \( \log{ n } = i \), in other words: \( \log{} \) counts the iterations.

\(L_i\) has \( 2^i \) nodes and \( \log{n} \) is the height

Properties of Logs

\( \log{xy} = \log{x} + \log{y} \)


\( \log_{b}{x} = \frac{\log_{k}{x}}{\log_{k}{b}} \)


\( \log_b{x^c} = c \log_n{x} \)


For most of our purposes the base will not matter

Divide and Conquer


Can you split your problem into halves?


If so then there will be \( \log{n} \) splits.


Repeated squares


Merge Sort (more later)

Some useful sums


\( \sum_{i=0}^n b^i = \frac{b^{n+1} - 1}{b - 1} = \mathcal{O}(b^n) \)


\( \sum_{i=1}^{n} \frac{1}{i} \approx \ln{n} \)


\( \sum_{i=1}^n \log {i} = \log{n!} = \Theta (n \log{n}) \)


Andy's secret \( (1 + \Delta)^n \) method (if there is time)