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\), \(n_0\), such that
(ii) We call \(T(N)=\Omega(g(N))\), if there exists positive constants \(c\), \(n_0\), such that
(iii) we call \(T(N)=\Theta(h(N))\), if and only if \(T(N)=O(h(N))\) and \(T(N)=\Omega(h(N))\).
(iv) we call \(T(N)=o(p(N))\), if forall constant \(c>0\), there exists \(n_0\) such that
Or less formally, \(T(N)=o(p(N))\) if \(T(N)=O(p(N))\) and \(T(N)\neq \Theta(p(N))\).
The following rules are usually really useful for algorithm analysis.
Rules
(i) If \(T_1(N)=O(f(N))\) and \(T_2(N)=O(g(N))\), then
(ii) If \(T(N)\) is a polynomial of degree \(k\), then
(Proved by find \(c_1\), \(c_2\) to be its upper and lower bound)
(iii) \(\log^kN=O(N)\), \(\forall k\).
Recursions¶
We take Merge Sort as an example.
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+p-1\).
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+p-1\), then we have
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
where \(a\geq 1\), \(b>1\) and \(f(N)>0\) for sufficient large \(N\).
(i) If there exists \(\varepsilon>0\) such that \(f(N)=O(N^{\log_b{a-\varepsilon}})\), then \(T(N)=\Theta(N^{\log_ba})\).
(ii) If \(f(N)=\Theta(N^{\log_ba})\), then \(T(N)=\Theta(N^{\log_b a}\log N)\).
(iii) If there exists \(\varepsilon>0\), such that \(f(N)=\Omega(N^{\log_b a+\varepsilon})\), and for sufficient large \(N\), \(af(N/b)\leq cf(N)\), \(c<1\), then \(T(N)=\Theta(f(N))\).
The above \(\varepsilon\) 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 \(N^{\log_b a}\) and \(f(N)\). If the former is larger, then time cost is determined by \(T(N)=aT(N/b)\), which is \(O(N^{\log_b a})\), 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)+n\lg n\)
so
which is (i) of Master Theorem. So \(T(n)=\Theta(n^2)\).
so
which is (ii) of Master Theorem. So \(T(n)=\Theta(\log n)\).
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
So if we choose \(c=3/4\), then
So it satisfies (iii) of Master Theorem. Thus time cost \(T(n)=\Theta(n\log n)\)
Notice that there exists gap between (i) and (ii) in Master Theorem, cause some function may not satisfy \(\varepsilon\). There also exsits a gap between (i) and (ii) in Master Theorem, cause the regularity condition.
(i) \(T(n)=2T(n/2)+n\lg n\)
(ii) \(T(n)=T(n/2)+n(2-\cos n\pi)\)
The above two could not be solved by Master Theorem. We have to use recursive tree.
Divide & Conquer¶
- 二分搜索
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;
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(log n)\), space cost \(O(log n)\).