# [Lecture 2] Asymptotic Notation, Algorithm Analysis

## 1. Asymptotic Notation

### 1.1 Definitions

Here are some definitions to demonstrate the value trend of a function when the input N grows up .

The big-O notation $O$ defines a class of functions that have the same attribution :

$T(N)=O(f(N))$ if there are positive constants $c$ and ${n}_{0}$ such that $T(N)\le cf(N)$ when $N\ge {n}_{0}$.

Note that 「$=$」 here is not a tradition symbol 「*equal*」, which describes $T(N)$ belongs to a set of functions satisfied with the requirement above.

$O(f(N))$ guarantees the function $T(N)$ has upper bound in the long run. We will not mind what the constant items $c$ and ${n}_{0}$ would be when $N$ becomes a huge number, that’s a fundamental idea about the term 「asymptotic」, $T(N)$ will be under some restriction of $f(N)$ in the end, when we say some function is of $O(f(N))$, we guarantee the growth rate of this function will not be greater than $f(N)$.

Almost the same thing happens with the big-omega notation $\mathrm{\Omega}$ :

$T(N)=\mathrm{\Omega}(f(N))$ if there are positive constants $c$ and ${n}_{0}$ such that $T(N)\ge cf(N)$ when $N\ge {n}_{0}$

To make it more exactly, there is big-theta notation $\mathrm{\Theta}$ to control the upper bound and lower bound :

$T(N)=\mathrm{\Theta}(f(N))$ if and only if $T(N)=O(f(N))$ and $T(N)=\mathrm{\Omega}(f(N))$

And there is little-o notation describing the strict relation case, in which $T(N)$ is not in the set of $\mathrm{\Theta}(f(N))$ :

$T(N)=o(f(N))$ if $T(N)=O(f(N))$ and $T(N)\ne \mathrm{\Theta}(N)$

As we will see those notations in our future, they are efficient tools to compare the relative growth rate of functions. Thus we will introduce some important rules firstly.

### 1.2 Rules of Asymptotic Notations

**Rule 1**

Supposing that

we have :

**Rule 2**

If $T(N)$ is a polynomial of degree $k$, then $T(N)=\mathrm{\Theta}({N}^{k})$

**Rule 3**

${\mathrm{log}}^{k}N=O(N)$, for any constant $k$, that means the logarithmic functions grow more slowly than any polynomial functions

And we can make a list of functions in the order of growth rate :

Function | Name |
---|---|

$c$ | Constant |

$\mathrm{log}N$ | Logarithmic |

${\mathrm{log}}^{2}N$ | Log-squared |

$N$ | Linear |

$N\mathrm{log}N$ | |

${N}^{2}$ | Quadratic |

${N}^{3}$ | Cubic |

${2}^{N}$ | Exponential |

This list can be helpful to get intuitive impression. Besides we can compare the growth rate of two functions in a more general way by computing the limit :

The value of the limit shows the relationship of two functions :

- The limit is $0$ : $f(N)=o(g(N))$
- The limit is $c\ne 0$ : $f(N)=\mathrm{\Theta}(g(N))$
- The limit is $\mathrm{\infty}$ : $g(N)=o(f(N))$
- No limit : No relationship of two functions

Here’s a brief example, supposing we have $f(N)=N\mathrm{log}N$ and $g(N)={N}^{1.5}$ :

Using L’Hôpital’s rule, we have :

According to Rule 1 :

Using L’Hôpital’s rule again :

In fact this is another example to show $\mathrm{log}N$ is slower than any power function ${N}^{a}$ when $a$ is positive.

## 2. Algorithm Analysis

### 2.1 Model and Running Time

Take a piece of code, and we would like to know how well it would performance, the typical idea is to measure the **running time**. However there are lots of factors that will have effects on time, such as compiler options, the clock frequency of this machine you use, etc. We want our program analysis to be independent with those non-program factors.

In order to realise independence, we use a model assuming the basic operations, such as addition, multiplication, comparison, and assignment run for a time unit. Though we know exactly loading data from memory is much slower than executing addition in CPU. In addition we also assume our memory is infinity.

Since the input scale can be changeable, we will use ${T}_{worst}$ in worst-case to guarantee the upper bound of running time, and ${T}_{avg}$ in average-case to represent typical behaviour.

( Note that ${T}_{avg}$ sometimes would be really hard to analysis, in other case, the definition about **average** will have affect on ${T}_{avg}$. )

Now we will introduce the methods for algorithm analysis thought a simple problem.