Skip to content

Algorithm Analysis

Basic Notations

We must have some notation to better describe our algorithm.

Definitions

(i) We call T(N)=O(f(N)), if there exists positive constants c, n0, such that

T(N)cf(N),Nn0

(ii) We call T(N)=Ω(g(N)), if there exists positive constants c, n0, such that

T(N)cg(N),Nn0

(iii) we call T(N)=Θ(h(N)), if and only if T(N)=O(h(N)) and T(N)=Ω(h(N)).

(iv) we call T(N)=o(p(N)), if forall constant c>0, there exists n0 such that

T(N)<cp(N),N>n0

Or less formally, T(N)=o(p(N)) if T(N)=O(p(N)) and T(N)Θ(p(N)).

The following rules are usually really useful for algorithm analysis.

Rules

(i) If T1(N)=O(f(N)) and T2(N)=O(g(N)), then

T1(N)+T2(N)=O(f(N)+g(N))(steps summation)T1(N)×T2(N)=O(f(N)×g(N))(steps multiplication).

(ii) If T(N) is a polynomial of degree k, then

T(N)=Θ(Nk).

(Proved by find c1, c2 to be its upper and lower bound)

(iii) logkN=O(N), k.

Recursions

We take Merge Sort as an example.

Merge
Merge(A, p, q, r)
    n1 = q - p + 1
    n2 = r - q
    L[1...(n1 + 1)]
    R[1...(n2 + 1)]
    for i = 1 to n1
        L[i] = A[p + i - 1]
    for j = 1 to n2
        R[j] = A[q + j]
    L(n1 + 1) = R(n2 + 1) = +inf
    for k = p to r
        if L[i] <= R[j]
            A[k] = L[i]
            i = i + 1
        else
            A[k] = R[j]
            j = j + 1

whose time cost is O(N), where N=r+p1.

MergeSort
MergeSort(A, p, r)
    if p < r
        q = (p + r) / 2
        MergeSort(A, p, q)
        MergeSort(A, q + 1, r)
        Merge(A, p, q, r)

Assume the time cost of MergeSort is T(N), where N=r+p1, then we have

T(N)={O(1),N=12T(N/2)+O(N),N>1

It is apparent to draw a graph of tree for this recursion, get its time cost and then prove it using Induction.

Generalize the above thoughts and we have the following theorem.

Master Theorem

Assume T(N) is the time cost for a recursive algorithm, and satisfies

T(N)={O(1),N=1aT(N/b)+f(N),N>1

where a1, b>1 and f(N)>0 for sufficient large N.

(i) If there exists ε>0 such that f(N)=O(Nlogbaε), then T(N)=Θ(Nlogba).

(ii) If f(N)=Θ(Nlogba), then T(N)=Θ(NlogbalogN).

(iii) If there exists ε>0, such that f(N)=Ω(Nlogba+ε), and for sufficient large N, af(N/b)cf(N), c<1, then T(N)=Θ(f(N)).

The above ε in (i) and (iii) denotes the relationship on polynomial. And the latter condition in (iii) of the above theorem is called regularity condition.

The focus is to compare the relative size between Nlogba and f(N). If the former is larger, then time cost is determined by T(N)=aT(N/b), which is O(Nlogba), meaning the bifurcation of the tree is large enough such that the time cost of each layer can be ignored. However, on the other hand, if the latter is larger, then time cost is determined by f(N), which is O(f(N)).

Q1. Solve for time cost T(n) in the following situation

(i) T(n)=9T(n/3)+n

(ii) T(n)=T(2n/3)+1

(iii) T(n)=3T(n/4)+nlgn

nlogba=nlog39=n2>n

so

f(n)=n=O(nlogba)

which is (i) of Master Theorem. So T(n)=Θ(n2).

nlogba=nlog3/21=n0=1

so

f(n)=1=Θ(1)=Θ(nlogba)

which is (ii) of Master Theorem. So T(n)=Θ(logn).

nlogba=nlog43<n1<nlgn

which does not satisfy (i), (ii) of Master Theorem, so we need to confirm if it satisfies the regularity condition of (iii) in Master Theorem. Notice that

af(n/b)=3n4lg(n4)

So if we choose c=3/4, then

af(n/b)cf(n),n1

So it satisfies (iii) of Master Theorem. Thus time cost T(n)=Θ(nlogn)

Notice that there exists gap between (i) and (ii) in Master Theorem, cause some function may not satisfy ε. There also exsits a gap between (i) and (ii) in Master Theorem, cause the regularity condition.

Q2. Solve for time cost T(n) in the following situation

(i) T(n)=2T(n/2)+nlgn

(ii) T(n)=T(n/2)+n(2cosnπ)

The above two could not be solved by Master Theorem. We have to use recursive tree.

Divide & Conquer

  • 二分搜索

C++
BinarySearch(A, x)
    left, right = 0, len(A)-1

    while left<=right:
        mid = (left+right)/2
        if(x==A[mid]):
            return mid;
        else if (A[mid]<x):
            left = mid+1;
        else if (A[mid]>x):
            right = mid-1;

    return -1;
time cost O(logn), space cost O(1).

C++
BinarySearch(A, x, left, right)
    if left> right:
        return -1;

    mid = (left+right)/2;

    if(A[mid]==x):
        return mid;
    else if (A[mid]<x):
        return BinarySearch(A,x, mid+1,right);
    else if (A[mid]>x):
        return BinarySearch(A,x, left, mid-1);

time cost O(logn), space cost O(logn).