Divide and Conquer⚓︎
General idea:
Sample problems:
- Binary search
- Finding maximum and minimum
- Merge sort
- Quick sort
- Strassen's matrix multiplication
Recurrence Relations⚓︎
- \(T(n) = T(n-1) + 1\)
Test(n)
will make \(n+1\) calls, and prinf()
will execute \(n\) times. Time complexity \(T(n)=n+1\propto O(n)\)
Recurrence relation is:
We can get: \(T\left( n \right) =T\left( n-k \right) +k\). Assume \(n-k=0\), then \(T(n)=1+n\)
- \(T(n)=T(n-1)+n\)
We get \(T(n)=T(n-1)+2n+2\). For simplicity, let \(T(n)=T(n-1)+n\). Recurrence relation is:
We get \(T(n)=T(n-k)+(n-(k-1))+(n-(k-2))+\cdots +(n-1)+n\).The total time: \(T\left( n \right) =\frac{n\left( n+1 \right)}{2}\propto O\left( n^2 \right)\)
- \(T(n)=T(n-1)+\log n\)
The recurrence relation:
Because \(T\left( n \right) =T\left( n-k \right) +\log 1+\log 2+\cdots \log \left( n-1 \right) +\log n\), we get: \(T\left( n \right) =\log n!\propto O\left( n\log n \right)\)
Based on the examples above, we can summarize:
- \(T(n)=2T(n-1)+1\)
Recurrence relation:
\(T\left( n \right) =2^k\cdot T\left( n-k \right) +2^{k-1}+2^{k-2}+\cdots +2^2+2+1\). Total time: \(1+2+2^2+\cdots +2^n=2^{n+1}-1\propto O(2^n)\)
Similarly:
- Dividing Function \(T\left( n \right) =T\left( \frac{n}{2} \right) +1\)
Recurrence relation:
Because \(T\left( n \right) =T\left( \frac{n}{2^k} \right) +k\), we get \(T\left( n \right) \propto O\left( \log n \right)\)
Similarly, if \(T\left( n \right) =T\left( \frac{n}{2} \right) +n\), then \(T\left( n \right) \propto O\left( n \right)\);
If \(T\left( n \right) =2T\left( \frac{n}{2} \right) +n\), then \(T\left( n \right) \propto O\left( n\log n \right)\) because \(T\left( n \right) =2^k\cdot T\left( \frac{n}{2^k} \right) +k\cdot n\)
- Root function
Recurrence relation:
We get \(T\left( n \right) =T\left( n^{\frac{1}{2^k}} \right) +k\). Assume \(n=2^m\), then \(T\left( n \right) \propto \varTheta \left( \log\log _2n \right)\).
Masters Theorem⚓︎
Masters Theorem for Decreasing Functions⚓︎
If \(T\left( n \right) =aT\left( n-b \right) +f\left( n \right)\), where \(a>0, b>0, f\left( n \right) =O\left( n^k \right)\) for \(k\geqslant 0\):
If \(a=1\), we get \(T\left( n \right) \propto O\left( n\cdot f\left( n \right) \right)\);
If \(a>1\), we get \(T\left( n \right) \propto O\left( a^{\frac{n}{b}}\cdot f\left( n \right) \right)\);
If \(a<1\), we get \(T\left( n \right) \propto O\left( f\left( n \right) \right)\)
Masters Theorem for Dividing Functions⚓︎
If \(T\left( n \right) =aT\left( \frac{n}{b} \right) +f\left( n \right)\), where \(a\geqslant 1, b>1, f\left( n \right) =\varTheta \left( n^k\left( \log n \right) ^p \right)\):
Case 1: If \(\log _ba>k\), then \(T\left( n \right) \propto \varTheta \left( n^{\log _ba} \right)\)
Case 2: If \(\log _ba=k\):
- If \(p>-1\), then \(T\left( n \right) \propto \varTheta \left( n^k\left( \log n \right) ^{p+1} \right)\);
- If \(p=-1\), then \(T\left( n \right) \propto \varTheta \left( n^k\log\log n \right)\);
- If \(p<-1\), then \(T\left( n \right) \propto \varTheta \left( n^k \right)\)
Case 3: If \(\log _ba<k\):
- If \(p\geqslant 0\), then \(T\left( n \right) \propto \varTheta \left( n^k\left( \log n \right) ^p \right)\);
- If \(p<0\), then \(T\left( n \right) \propto \varTheta \left( n^k \right)\)