Design of Algorithms

Analysis of Algorithms

Analysis of Algorithms

Table of Contents

What analysis measures

Running time

Orders of Growth

Some functions log2n<n<nlog2n<n2<n3<2n<n!\log_{2}n < n < n\log_{2}n < n^2 < n^3 < 2^n < n!

Efficiencies

Algorithm run-time can be dependent on particulars of input e.g. sequential search

Efficiency can be:

Asymptotic Notations

Notations for comparing orders of growth:

e.g.
nO(n2)n \in O(n^2)
n2(n1)O(n2)\frac{n}{2}(n-1)\in O(n^2)
n3∉O(n2)n^3\not\in O(n^2)

Definition: A function t(n)O(g(n))t(n) \in O(g(n)) if cR+,n0Z+\exists c \in \R^+, n_0 \in \Z^+ s.t. nn0\forall n\ge n_0: t(n)cg(n)t(n) \le cg(n)

big_o

Big O

Definition: A function t(n)Ω(g(n))t(n) \in \Omega(g(n)) if cR+,n0Z+\exists c \in \R^+, n_0 \in \Z^+ s.t. nn0\forall n\ge n_0: t(n)cg(n)t(n) \ge cg(n)

Definition: A function t(n)Θ(g(n))t(n) \in \Theta(g(n)) if c1,c2R+,n0Z+\exists c_1,c_2 \in \R^+, n_0 \in \Z^+ s.t. nn0\forall n\ge n_0: c1g(n)t(n)c2g(n)c_1 g(n) \le t(n) \le c_2 g(n)

big_theta

Big Θ\Theta

Theorem: If t1(n)O(g1(n))t_{1}(n) \in O(g_{1}(n)) and t2(n)O(g2(n))t_{2}(n) \in O(g_{2}(n)): t1(n)+t2(n)O(max{g1(n),g2(n)})t_{1}(n)+t_{2}(n) \in O(\max\{g_{1}(n), g_{2}(n)\})

Analogous assertions also hold for Ω\Omega, Θ\Theta

Comparing Orders of Growth

L’Hopital’s rule

limnt(n)g(n)=limnt(n)g(n)\lim_{n\rightarrow\infty}\frac{t(n)}{g(n)} = \lim_{n\rightarrow\infty}\frac{t'(n)}{g'(n)}

Stirling’s Formula

For large nn n!2πnnenn! \approx \sqrt{2\pi n}\frac{n}{e}^n

Efficiency Classes

Class Name Comments
1 constant very few algorithms fall in this class
logn\log n logarithmic results from cutting problem’s size by constant factor
nn linear scan a list of size nn e.g. sequential search
nlognn\log n linearithmic divide-and-conquer e.g. mergesort; quicksort
n2n^2 quadratic two embedded loops e.g. basic sorting; n×nn\times n matrix operations
n3n^3 cubic three embedded loops; e.g. often used in linear algebra
2n2^n exponential generate all subsets of nn-element set
n!n! factorial generate all permutations of nn-element set

Process: Analysing time efficiency of non-recursive algorithms

  1. define parameter indicating input’s size
  2. identify algorithm’s basic operation (typically on innermost loop)
  3. check if number of times basic operation is executed is only a function of input size
    • if not: worst case, average case to be considered separately
  4. set up sum expressing number of times the basic operation is executed
  5. use formulas/sum manipulation to find a closed form solution for the count or determine order of growth

Basic rules

Scalar multiplication

i=lucai=ci=luai\sum_{i=l}^{u}{ca_i} = c\sum_{i=l}^{u}{a_i}

Addition

i=luai+bi=i=luai+i=lubi\sum_{i=l}^{u}{a_i+b_i} = \sum_{i=l}^{u}{a_i}+\sum_{i=l}^{u}{b_i}

i=lu1=ul+1\sum_{i=l}^{u}1 = u-l+1 In particular i=1n1=n\sum_{i=1}^{n}{1} = n

Triangle numbers

i=lni=n(n+1)2\sum_{i=l}^{n}{i} = \frac{n(n+1)}{2}

Geometric series

i=1nxk=1xk+11x\sum_{i=1}^{n}{x^k} = \frac{1-x^{k+1}}{1-x}

Process: analysing time efficiency of recursive algorithms

  1. define parameter indicating input size
  2. identify basic operation
  3. check if number of times basic operation is executed is only a function of input size
    • if not: worst case, average case to be considered separately
  4. set up recurrence relation and initial condition corresponding to number of times basic operation is executed
  5. solve recurrence or ascertain order of growth of its solution

Divide and Conquer

DEFINITION: eventually non-decreasing

DEFINITION: smooth

f(n)f(n) is smooth if:

THEOREM:

Let f(n)f(n) be smooth. For any fixed integer b2b\ge 2: f(bn)Θ(f(n))f(bn)\in\Theta(f(n)) i.e. cb,dbR+,n0Z+\exists c_b, d_b \in \R^+, n_0 \in \Z^+ s.t. dbf(n)f(bn)cbf(n) for nn0d_b f(n) \le f(bn) \le c_b f(n) \text{ for } n\ge n_0

THEOREM: Smoothness rule

Let T(n)T(n) be an eventually non-decreasing function Let f(n)f(n) be a smooth function. If T(n)Θ(f(n)) T(n) \in \Theta(f(n)) for values of nn that are powers of bb where b2b\ge 2, then: T(n)Θ(f(n))T(n) \in \Theta(f(n))

THEOREM: Master Theorem

Let T(n)T(n) be an eventually non-decreasing function that satisfies the recurrence T(n)=aT(n/b)+f(n) for n=bk,k=1,2,...T(n)=aT(n/b)+f(n) \text{ for } n= b^k, k = 1, 2, ... T(1)=cT(1) = c where a1,b2,c>0a\ge1,b\ge2,c>0. If f(n)Θ(nd)f(n)\in\Theta(n^d) where d0d\ge0, then T(n){Θ(nd) if a<bd Θ(ndlogn) if a=bd Θ(nlogab) if a>bdT(n)\in\begin{cases}\Theta(n^d)\text{ if }a<b^d\\\ \Theta(n^d\log{n})\text{ if }a=b^d\\\ \Theta(n^{\log{a}_b)}\text{ if }a>b^d\end{cases}


Edit this page.