Daniel Sutantyo
Department of Computing
Macquarie University
\(T(n) = \begin{cases} \Theta(1) &\text{if $n \le c$}\\ aT(n/b) + D(n) + C(n) &\text{otherwise} \end{cases}\)
where
divide and conquer method
public static int linearSearch(int[] a, int i, int j, int target){
if (i > j)
return -1;
if (a[i] == target)
return i;
return linearSearch(a,i+1,j,target);
}
public static int linearSearch(int[] a, int i, int j, int target){
if (i > j)
return -1;
if (a[i] == target)
return i;
return linearSearch(a,i+1,j,target);
}
public static int linearSearch(int[] a, int i, int j, int target) {
if (i > j)
return -1;
int m = (i+j)/2;
if (a[m] == target)
return m;
return Math.max(linearSearch(a,i,m-1,target), linearSearch(a,m+1,j,target));
}
divide and conquer method
recursive relation between a problem and its subproblems
linearSearch(a[], 0, 8, target)
linearSearch(a[], 0, 3, target)
linearSearch(a[], 5, 8, target)
linearSearch(a[], i, j, target) = max(
linearSearch(a[],i,(i+j)/2-1,target),
linearSearch(a[],(i+j)/2+1,j,target)
)
recursive relation between a problem and its subproblems
linearSearch(a[], 0, 8, target)
linearSearch(a[], 0, 3, target)
linearSearch(a[], 5, 8, target)
\(n\)
\[\frac{n}{2}\]
\[\frac{n}{2}\]
recurrence
\(T(n) = \begin{cases} \Theta(1) &\text{if $n \le c$}\\ aT(n/b) + D(n) + C(n) &\text{otherwise} \end{cases}\)
\(T(n) = \begin{cases} \Theta(1) &\text{if $n \le 1$}\\ 2T(n/2) + \Theta(1)&\text{otherwise} \end{cases}\)
recurrence
\(T(n) = \begin{cases} \Theta(1) &\text{if $n \le 1$}\\ 2T(n/2) + \Theta(1)&\text{otherwise} \end{cases}\)
\(T(n) = \begin{cases} \Theta(1) &\text{if $n \le 1$}\\ 3T(n/3) + \Theta(1)&\text{otherwise} \end{cases}\)
recurrence
A[ 0 .. 8 ]
A [ 0 .. 3 ]
A [ 5 .. 8 ]
\(n\)
\(\lfloor (n-1)/2 \rfloor \)
\(\lfloor n/2 \rfloor \)
recurrence
\(n\)
\[\frac{n}{2}\]
\[\frac{n}{2}\]
\(n\)
\[\frac{n}{3}\]
\[\frac{n}{3}\]
\[\frac{n}{3}\]
solving recurrence to obtain bound for time complexity
solving recurrence to obtain bound for time complexity
master theorem
master theorem
Given a recurrence of the form
\(T(n) = aT(n/b) + f(n) \)
\(T(n) = aT(n/b) + f(n) \)
\(f(n) = O(n^{\log_b a-\epsilon})\)
\(f(n) = \Theta(n^{\log_b a})\)
\(f(n) = \Omega(n^{\log_b a + \epsilon})\)
\(T(n) = aT(n/b) + cn^k \)
So we can write \(f(n) = cn^k\)
master theorem (simplification)
master theorem (simplification)
\(T(n) = aT(n/b) + cn^k \)
\( k = \log_b a - \epsilon\)
\( b^k = a - b^\epsilon\)
(take both sides to the power of \(b\))
\(b^k < a\)
in other words:
master theorem (simplification)
linear search
\(T(n) = \begin{cases} \Theta(1) &\text{if $n \le 1$}\\ 2T(n/2) + \Theta(1)&\text{otherwise} \end{cases}\)
\(T(n) = \begin{cases} \Theta(1) &\text{if $n \le 1$}\\ 3T(n/3) + \Theta(1)&\text{otherwise} \end{cases}\)
linear search
linear search
master theorem (simplification)
\(T(n) = aT(n/b) + cn^k \)
master theorem (simplification)
\[T(n)=\begin{cases}\Theta(n^{\log_b a}),&\text{if $f(n) = O(n^{\log_b a - \epsilon})$ for $\epsilon > 0$}\\\Theta(n^{\log_ba}\log n),&\text{if $f(n)=\Theta(n^{\log_b a})$}\\\Theta(f(n)),&\text{if $f(n) = \Omega(n^{\log_ba+\epsilon})$ for $\epsilon > 0$}\end{cases}\]
\[T(n) = aT(n/b) + f(n) \]
\[T(n)=\begin{cases}\Theta(n^{\log_ba}),&\text{if $a > b^k$ }\\\Theta(n^{\log_ba}\log n),&\text{if $a = b^k$}\\\Theta(n^k),&\text{if $a < b^k$}\end{cases}\]
\[T(n) = aT(n/b) + cn^k \]
examples
\[T(n) = aT(n/b) + cn^k\]
\[T(n)=\begin{cases}\Theta(n^{\log_ba}),&\text{if $a > b^k$ }\\\Theta(n^{\log_ba}\log n),&\text{if $a = b^k$}\\\Theta(n^k),&\text{if $a < b^k$}\end{cases}\]
\(T(n) = \begin{cases} \Theta(1) &\text{if $n \le 1$}\\ 2T(n/2) + \Theta(n)&\text{otherwise} \end{cases}\)
\(a = 2, b = 2, k = 1 \quad\rightarrow\quad T(n) = \Theta(n\log n)\)
examples
\(T(n) = \begin{cases} \Theta(1) &\text{if $n \le 1$}\\ T(n/2) + \Theta(1)&\text{otherwise} \end{cases}\)
\(a = 1, b = 2, k = 0 \quad\rightarrow\quad T(n) = \Theta(\log n)\)
\[T(n) = aT(n/b) + cn^k\]
\[T(n)=\begin{cases}\Theta(n^{\log_ba}),&\text{if $a > b^k$ }\\\Theta(n^{\log_ba}\log n),&\text{if $a = b^k$}\\\Theta(n^k),&\text{if $a < b^k$}\end{cases}\]
examples
\(T(n) = \begin{cases} \Theta(1) &\text{if $n \le 1$}\\ 4T(n/2) + \Theta(n)&\text{otherwise} \end{cases}\)
\(a = 4, b = 2, k = 1 \quad\rightarrow\quad T(n) = \Theta(n^{2})\)
\[T(n) = aT(n/b) + cn^k\]
\[T(n)=\begin{cases}\Theta(n^{\log_ba}),&\text{if $a > b^k$ }\\\Theta(n^{\log_ba}\log n),&\text{if $a = b^k$}\\\Theta(n^k),&\text{if $a < b^k$}\end{cases}\]
examples
\(T(n) = \begin{cases} \Theta(1) &\text{if $n \le 1$}\\ 3T(n/2) + \Theta(n)&\text{otherwise} \end{cases}\)
\(a = 3, b = 2, k = 1 \quad\rightarrow\quad T(n) = \Theta(n^{\log_2 3})\)
\[T(n) = aT(n/b) + cn^k\]
\[T(n)=\begin{cases}\Theta(n^{\log_ba}),&\text{if $a > b^k$ }\\\Theta(n^{\log_ba}\log n),&\text{if $a = b^k$}\\\Theta(n^k),&\text{if $a < b^k$}\end{cases}\]
examples
\(T(n) = \begin{cases} \Theta(1) &\text{if $n \le 1$}\\ T(n/2) + \Theta(n)&\text{otherwise} \end{cases}\)
\(a = 1, b = 2, k = 1 \quad\rightarrow\quad T(n) = \Theta(n)\)
\[T(n) = aT(n/b) + cn^k\]
\[T(n)=\begin{cases}\Theta(n^{\log_ba}),&\text{if $a > b^k$ }\\\Theta(n^{\log_ba}\log n),&\text{if $a = b^k$}\\\Theta(n^k),&\text{if $a < b^k$}\end{cases}\]
example: merge sort
\[T(n) = 2T(n/2) + \Theta(n)\]
\(T(n) = \begin{cases} \Theta(1) &\text{if $n = 1$}\\ 2T(n/2) + cn &\text{if $n > 1$} \end{cases}\)
step 1: draw the tree
\(T(n) = \begin{cases} \Theta(1) &\text{if $n = 1$}\\ 2T(n/2) + cn &\text{if $n > 1$} \end{cases}\)
\(cn\)
\[\frac{cn}{2}\]
\[\frac{cn}{2}\]
step 1: draw the tree
\(cn\)
\[\frac{cn}{2}\]
\[\frac{cn}{4}\]
1
1
\[\frac{cn}{4}\]
\[\frac{cn}{2}\]
\[\frac{cn}{4}\]
\[\frac{cn}{4}\]
1
1
1
1
1
1
1
1
1
1
1
1
1
1
. . .
. . .
step 2: count the number of operations on each level
\(cn\)
\[\frac{cn}{2}\]
\[\frac{cn}{4}\]
1
1
\[\frac{cn}{4}\]
\[\frac{cn}{2}\]
\[\frac{cn}{4}\]
\[\frac{cn}{4}\]
1
1
1
1
. . .
1
1
1
1
. . .
\[n\]
\[n\]
\[n\]
\[n\]
step 3: determine the height of the tree
\(cn\)
\[\frac{cn}{2}\]
\[\frac{cn}{4}\]
1
1
\[\frac{cn}{4}\]
\[\frac{cn}{2}\]
\[\frac{cn}{4}\]
\[\frac{cn}{4}\]
1
1
1
1
. . .
1
1
1
1
. . .
\[n\]
\[n\]
\[n\]
\[n\]
step 3: determine the height of the tree
step 4: determine the total number of operations
\(T(n) = O(n \log n)\)
example: linear search
\(T(n) = \begin{cases} \Theta(1) &\text{if $n \le 1$}\\ 2T(n/2) + \Theta(1)&\text{otherwise} \end{cases}\)
example: linear search
\(cn\)
\[\frac{cn}{2}\]
\[\frac{cn}{4}\]
1
1
\[\frac{cn}{4}\]
\[\frac{cn}{2}\]
\[\frac{cn}{4}\]
\[\frac{cn}{4}\]
1
1
1
1
. . .
1
1
1
1
. . .
\[n\]
\[n\]
\[n\]
\[n\]
example: linear search
\(1\)
1
1
1
1
1
1
1
1
1
1
1
1
. . .
1
1
1
1
. . .
\[4\]
\[2\]
\[1\]
\[???\]
example: linear search
example: linear search
1
1
1
1
1
1
1
1
1
1
1
1
1
. . .
1
1
1
1
. . .
\[4\]
\[2\]
\[1\]
\[2^{\log_2 n} = n\]
example: linear search
\[ 1 + 2 + 4 + \cdots + n \]
\[ = 2^0 + 2^1 + 2^2 + \cdots + 2^{\log_2 n} \]
\[ = \sum_{k=0}^{\log_2 n} 2^k \]
\[\sum_{k=0}^m x^k = 1 + x + x^2 + \cdots + x^m = \frac{x^{m+1}-1}{x-1}\]
example: linear search
\[\sum_{k=0}^{\log_2 n} x^k = 1 + 2 + 2^2 + \cdots + 2^{\log_2 n}= \frac{2^{\log_2 n+1}-1}{2-1} = 2n - 1\]
\[\sum_{k=0}^m x^k = 1 + x + x^2 + \cdots + x^m = \frac{x^{m+1}-1}{x-1}\]
from here we can see that linear search is \(O(n)\)
\(T(n) = \begin{cases} \Theta(1) &\text{if $n \le 1$}\\ 3T(n/3) + \Theta(1)&\text{otherwise} \end{cases}\)
example: linear search (3 partitions)
example: linear search (3 partitions)
1
1
1
1
1
1
1
. . .
. . .
\[9\]
\[3\]
\[1\]
\[3^{\log_3 n} = n\]
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
\[ 1 + 3 + 9 + \cdots + n \]
\[ = 3^0 + 3^1 + 3^2 + \cdots + 3^{\log_3 n} \]
\[ = \sum_{k=0}^{\log_3 n} 3^k \]
\[\sum_{k=0}^m x^k = 1 + x + x^2 + \cdots + x^k = \frac{x^{m+1}-1}{x-1}\]
example: linear search (3 partitions)
\[\sum_{k=0}^{\log_2 n} x^k = 1 + 3 + 3^2 + \cdots + 3^n = \frac{3^{\log_3 n+1}-1}{3-1} = \frac{3n-1}{2}\]
\[\sum_{k=0}^m x^k = 1 + x + x^2 + \cdots + x^k = \frac{x^{m+1}-1}{x-1}\]
and this version of linear search is also \(O(n)\), as expected
example: linear search (3 partitions)
another example
\[T(n) = 3T(n/4) + \Theta(n^2)\]
\(T(n) = \begin{cases} \Theta(1) &\text{if $n = 1$}\\ 3T(n/4) + cn^2 &\text{if $n > 1$} \end{cases}\)
\(T(n) = \begin{cases} \Theta(1) &\text{if $n = 1$}\\ 3T(n/4) + cn^2 &\text{if $n > 1$} \end{cases}\)
\(cn^2\)
\[\frac{cn^2}{16}\]
\[\frac{cn^2}{16}\]
\[\frac{cn^2}{16}\]
step 1: draw the tree
step 1: draw the tree
\(cn^2\)
\[\frac{cn^2}{16}\]
\[\frac{cn^2}{16}\]
\[\frac{cn^2}{16}\]
\[\frac{cn^2}{256}\]
\[\frac{cn^2}{256}\]
\[\frac{cn^2}{256}\]
\[\frac{cn^2}{256}\]
\[\frac{cn^2}{256}\]
\[\frac{cn^2}{256}\]
\[\frac{cn^2}{256}\]
\[\frac{cn^2}{256}\]
\[\frac{cn^2}{256}\]
1
1
1
1
. . . . . . . . . . . . . . . . . . . . .
1
1
1
1
1
1
1
1
1
1
1
1
1
1
\(cn^2\)
\[\frac{cn^2}{16}\]
\[\frac{cn^2}{16}\]
\[\frac{cn^2}{16}\]
\[\frac{cn^2}{256}\]
\[\frac{cn^2}{256}\]
\[\frac{cn^2}{256}\]
1
1
1
. . . . . . . . . . . . . . . . . . . . .
1
1
\[\frac{cn^2}{256}\]
\[\frac{cn^2}{256}\]
\[\frac{cn^2}{256}\]
\[\frac{cn^2}{256}\]
\[\frac{cn^2}{256}\]
\[\frac{cn^2}{256}\]
1
1
1
1
1
1
\(cn^2\)
\[\frac{3}{16}cn^2\]
\[\frac{9}{256}cn^2\]
???
step 2: count number of operations on each level
step 3: determine the height of the tree
\(cn^2\)
\[\frac{cn^2}{16}\]
\[\frac{cn^2}{16}\]
\[\frac{cn^2}{16}\]
\[\frac{cn^2}{256}\]
\[\frac{cn^2}{256}\]
\[\frac{cn^2}{256}\]
1
1
1
. . . . . . . . . . . . . . . . . . . . .
1
1
\[\frac{cn^2}{256}\]
\[\frac{cn^2}{256}\]
\[\frac{cn^2}{256}\]
\[\frac{cn^2}{256}\]
\[\frac{cn^2}{256}\]
\[\frac{cn^2}{256}\]
1
1
1
1
1
1
\(cn^2\)
\[\frac{3}{16}cn^2\]
\[\frac{9}{256}cn^2\]
\(3^{\log_4 n}\)
step 3: determine the height of the tree
step 4: determine the total number of operations
\[cn^2 + \frac{3}{16}cn^2 + \frac{9}{256}cn^2+\cdots+ 3^{\log_4 n}\]
\[cn^2 + \frac{3}{16}cn^2 + \left(\frac{3}{16}\right)^2cn^2+\cdots+ \left(\frac{3}{16}\right) ^{\log_4 n - 1}cn^2+ 3^{\log_4 n}\]
\[\left(\frac{3}{16}\right)^i\]
each term has
\(i\) goes from 0 to \(\log_4 n - 1\)
\[\sum_{k=0}^n x^k = 1 + x + x^2 + \cdots + x^n = \frac{x^{n+1}-1}{x-1}\]
\[cn^2 + \frac{3}{16}cn^2 + \left(\frac{3}{16}\right)^2cn^2+\cdots+ \left(\frac{3}{16}\right) ^{\log_4 n - 1}cn^2+ 3^{\log_4 n}\]
\[cn^2\sum_{i=0}^{\log_4n-1}\left(\frac{3}{16}\right)^i+ 3^{\log_4 n}\]
\[= \frac{(3/16)^{\log_4 n}-1}{(3/16)-1}cn^2+ 3^{\log_4 n}\]
step 4: determine the total number of operations
\[\sum_{k=0}^\infty x^k = \frac{1}{1-x}\]
\[cn^2\sum_{i=0}^{\log_4n-1}\left(\frac{3}{16}\right)^i + 3^{\log_4 n}\]
\[= \frac{(3/16)^{\log_4 n}-1}{(3/16)-1}cn^2+ 3^{\log_4 n}\]
step 4: determine the total number of operations
\[\sum_{k=0}^n x^k <\sum_{k=0}^\infty x^k = \frac{1}{1-x}\]
\[\sum_{i=0}^{\log_4n-1}\left(\frac{3}{16}\right)^i cn^2+ 3^{\log_4 n}\]
step 4: determine the total number of operations
\[< \sum_{i=0}^{\infty}\left(\frac{3}{16}\right)^i cn^2+ 3^{\log_4 n}\]
\[=\frac{1}{1-(3/16)} cn^2+ 3^{\log_4 n}\]
step 4: determine the total number of operations
\[\frac{1}{1-(3/16)} cn^2+ 3^{\log_4 n} = \frac{16}{13}cn^2 + 3^{\log_4 n}\]
\[ 3^{\log_4 n} = n^{\log_4 3}\]
\[T(n) \le \frac{16}{13}cn^2 + n^{\log_43}\]
\[T(n) = O(n^2)\]
using master method
\(T(n) = \begin{cases} \Theta(1) &\text{if $n \le 1$}\\3 T(n/4) + \Theta(n^2)&\text{otherwise} \end{cases}\)
\(a = 3, b = 4, k = 2 \quad\rightarrow\quad T(n) = \Theta(n^2)\)
\[T(n) = aT(n/b) + cn^k\]
\[T(n)=\begin{cases}\Theta(n^{\log_ba}),&\text{if $a > b^k$ }\\\Theta(n^{\log_ba}\log n),&\text{if $a = b^k$}\\\Theta(n^k),&\text{if $a < b^k$}\end{cases}\]
example
\(T(n) = \begin{cases} 1 &\text{if $n \le 1$}\\ 2T(\lfloor n/2\rfloor) + n&\text{otherwise} \end{cases}\)
example
\(T(n) = \begin{cases} 1 &\text{if $n \le 1$}\\ 2T(\lfloor n/2\rfloor) + n&\text{otherwise} \end{cases}\)
example
\(T(n) = \begin{cases} 1 &\text{if $n \le 1$}\\ 2T(\lfloor n/2\rfloor) + n&\text{otherwise} \end{cases}\)
example
example
example
example
example
\(T(2k) = 2*T(k) + 2k\)
\(\le 2ck\log k + 2k\)
(from the recurrence)
(from the induction hypothesis)
\(\le 2ck\log 2k + 2k\)
example
\(T(2k)\le 2ck\log 2k + 2k\)
\(T(2k) \le 2ck\log 2k\)
example
\(T(k) = 2T(\lfloor k/2 \rfloor) + k \)
\(\le c(\lfloor k/2 \rfloor )\log(\lfloor k/2\rfloor) + k \)
\(\le ck\log(k/2 ) + k\)
\(= ck\log k - ck\log 2 + k\)
\(\le ck\log k\)
example
\(T(k) = 2T(k/2) + k \)
\(\le c(\lfloor k/2 \rfloor )\log(\lfloor k/2\rfloor) + k \)
\(\le ck\log(k/2 ) + k\)
\(= ck\log k - ck\log 2 + k\)
\(\le ck\log k\)
final words
\(x = x_1B^m + x_0\)
\(y = y_1B^m + y_0\)
\(x_0 < B^m\)
\(y_0 < B^m\)
\(xy = (x_1B^m + x_0)(y_1B^m+y_0)\)
\(= (x_1y_1)B^{2m} + (x_1y_0+x_0y_1)B^m + x_0y_0\)
\(x = x_1B^m + x_0\)
\(y = y_1B^m + y_0\)
\(xy = (x_1y_1)B^{2m} + (x_1y_0+x_0y_1)B^m + x_0y_0\)
\(12345678 = 1234*10^4 + 5678\)
\(87654321 = 8765*10^4 + 4321\)
\(\begin{aligned}12345678 * 87654321 =\ &(1234*8765)10^{8}\\ &+ (1234*4321+5678*8765)10^4 \\&+ (5678*4321)\end{aligned}\)
\(\text{mul}(1234,8765)\)
\(\text{mul}(12345678,87654321)\)
\(\begin{aligned}12345678 * 87654321 =\ &(1234*8765)10^{8}\\ &+ (1234*4321+5678*8765)10^4 \\&+ (5678*4321)\end{aligned}\)
\(\text{mul}(5678*8765)\)
\(\text{mul}(1234*4321)\)
\(\text{mul}(5678*4321)\)
\(\text{mul}(1234,8765)\)
\(\text{mul}(12345678,87654321)\)
\(\text{mul}(5678,8765)\)
\(\text{mul}(1234,4321)\)
\(\text{mul}(5678,4321)\)
\(\text{mul}(12,87)\)
\(\text{mul}(12,65)\)
\(\text{mul}(34,87)\)
\(\text{mul}(34,65)\)
\(\text{mul}(1,8)\)
\(\text{mul}(1,7)\)
\(\text{mul}(2,8)\)
\(\text{mul}(2,7)\)
recurrence equation
\(x = x_1B^m + x_0\)
\(y = y_1B^m + y_0\)
\(xy = (x_1y_1)B^{2m} + (x_1y_0+x_0y_1)B^m + x_0y_0\)
\(T(n) = \begin{cases} O(1) &\text{if $n \le 1$}\\ 4T(n/2) + O(n) &\text{otherwise} \end{cases}\)
master method
\(T(n) = \begin{cases} 1&\text{if $n \le 1$}\\ 4T(n/2) +cn &\text{otherwise} \end{cases}\)
\[T(n)=\begin{cases}\Theta(n^{\log_ba}),&\text{if $a > b^k$ }\\\Theta(n^{\log_ba}\log n),&\text{if $a = b^k$}\\\Theta(n^k),&\text{if $a < b^k$}\end{cases}\]
\(a = 4, b = 2, k = 1 \quad \rightarrow \quad T(n) = \Theta(n^2)\)
recursion tree method
\[cn\]
\[\frac{cn}{2}\]
\[\frac{cn}{2}\]
\[\frac{cn}{4}\]
\[\frac{cn}{4}\]
\[\frac{cn}{4}\]
\[\frac{cn}{4}\]
\[\frac{cn}{2}\]
\[\frac{cn}{2}\]
\[\frac{4cn}{2} = 2cn\]
\[cn\]
\[\frac{16cn}{4} = 4cn\]
\[\frac{4^icn}{2^i} = 2^icn\]
\[\frac{cn}{4}\]
\[\frac{cn}{4}\]
\[\frac{cn}{4}\]
\[\frac{cn}{4}\]
\[\frac{cn}{4}\]
\[\frac{cn}{4}\]
\[\frac{cn}{4}\]
\[\frac{cn}{4}\]
\[\frac{cn}{4}\]
\[\frac{cn}{4}\]
\[\frac{cn}{4}\]
\[\frac{cn}{4}\]
substitution method
\(T(k) = 4T(k/2) + ck\)
\(\le 4ck^2/4 + ck = ck^2 + ck\)
\(T(k) = O(k^2)\)
substitution method
substitution method
\(T(k) = 4T(k/2) + ck\)
\(\le 4(ck^2/4-k/2) + ck\)
\(\le ck^2 -2k + ck\)
\(\le ck^2\)
(from recurrence)
(from induction hypothesis)
(if \(c \le 2\))