Daniel Sutantyo, Department of Computing, Macquarie University
6.1 - Recursion-Tree Method
6.1 - Recursion-Tree Method
\(\text{mergeSort}(0,n)\)
\(\text{mergeSort}(0,\lfloor\frac{n}{2}\rfloor)\)
\(\text{mergeSort}(\lceil\frac{n}{2}\rceil,n)\)
\(\text{mergeSort}(0,\lfloor\frac{n}{4}\rfloor)\)
\(\text{mergeSort}(\lceil\frac{n}{4}\rceil,\lfloor\frac{n}{2}\rfloor)\)
\(\text{mergeSort}(\lceil\frac{n}{2}\rceil,\lfloor\frac{3n}{4}\rfloor)\)
\(\text{mergeSort}(\lceil\frac{3n}{4}\rceil,n)\)
6.1 - Recursion-Tree Method
\(\text{mergeSort}(n)\)
\(\text{mergeSort}(\frac{n}{2})\)
\(\text{mergeSort}(\frac{n}{2})\)
\(\text{mergeSort}(\frac{n}{4})\)
\(\text{mergeSort}(\frac{n}{4})\)
\(\text{mergeSort}(\frac{n}{4})\)
\(\text{mergeSort}(\frac{n}{4})\)
\(T(n) = \begin{cases} \Theta(1) &\text{if $n = 1$}\\ 2T(n/2) + cn &\text{if $n > 1$} \end{cases}\)
6.1 - Recursion-Tree Method
\(\text{mergeSort}(n)\)
\(\text{mergeSort}(\frac{n}{2})\)
\(\text{mergeSort}(\frac{n}{2})\)
\(\text{mergeSort}(\frac{n}{4})\)
\(\text{mergeSort}(\frac{n}{4})\)
\(\text{mergeSort}(\frac{n}{4})\)
\(\text{mergeSort}(\frac{n}{4})\)
\(T(n) = \begin{cases} \Theta(1) &\text{if $n = 1$}\\ 2T(n/2) + cn &\text{if $n > 1$} \end{cases}\)
6.1 - Recursion-Tree Method
\(T(n)\)
\[T\left(\frac{n}{2}\right)\]
\[T\left(\frac{n}{4}\right)\]
1
\[T\left(\frac{n}{2}\right)\]
1
1
1
1
1
1
1
. . .
. . .
. . .
\(T(n) = \begin{cases} \Theta(1) &\text{if $n = 1$}\\ 2T(n/2) + cn &\text{if $n > 1$} \end{cases}\)
\[T\left(\frac{n}{4}\right)\]
\[T\left(\frac{n}{4}\right)\]
\[T\left(\frac{n}{4}\right)\]
6.1 - Recursion-Tree Method
\(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}{4}\]
1
\[\frac{cn}{4}\]
\[\frac{cn}{2}\]
\[\frac{cn}{4}\]
\[\frac{cn}{4}\]
1
1
1
1
1
1
1
. . .
. . .
. . .
\[cn\]
\[cn\]
\[cn\]
\[\Theta(n)\]
6.1 - Recursion-Tree Method
\(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}{4}\]
1
\[\frac{cn}{4}\]
\[\frac{cn}{2}\]
\[\frac{cn}{4}\]
\[\frac{cn}{4}\]
1
1
1
1
1
1
1
. . .
. . .
. . .
\[cn\]
\[cn\]
\[cn\]
\[\Theta(n)\]
6.1 - Recursion-Tree Method
\(T(n) = \begin{cases} \Theta(1) &\text{if $n = 1$}\\ 2T(n/2) + cn &\text{if $n > 1$} \end{cases}\)
6.1 - Recursion-Tree Method
\(T(n) = \begin{cases} \Theta(1) &\text{if $n = 1$}\\ 2T(n/2) + cn &\text{if $n > 1$} \end{cases}\)
\(T(n) = \Theta(n \log n)\)
6.1 - Recursion-Tree Method
\(T(n) = \begin{cases} \Theta(1) &\text{if $n = 1$}\\ 2T(n/2) + cn &\text{if $n > 1$} \end{cases}\)
\(T(n) = \Theta(n \log n)\)
\[\underbrace{cn + cn+ \cdots + cn}_{\text{$(\log_2 n)$ terms}} + \Theta (n) = \Theta(n \log n)\]
6.1 - Recursion-Tree Method
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));
}
\(T(n) = \begin{cases} \Theta(1) &\text{if $n \le 1$}\\ 2T(n/2) + \Theta(1)&\text{otherwise} \end{cases}\)
6.1 - Recursion-Tree Method
\(T(n) = \begin{cases} \Theta(1) &\text{if $n \le 1$}\\ 2T(n/2) + \Theta(1)&\text{otherwise} \end{cases}\)
\(T(n)\)
\[T\left(\frac{n}{2}\right)\]
1
1
1
1
1
1
1
1
. . .
. . .
. . .
\[T\left(\frac{n}{2}\right)\]
\[T\left(\frac{n}{4}\right)\]
\[T\left(\frac{n}{4}\right)\]
\[T\left(\frac{n}{4}\right)\]
\[T\left(\frac{n}{4}\right)\]
6.1 - Recursion-Tree Method
\(T(n) = \begin{cases} \Theta(1) &\text{if $n \le 1$}\\ 2T(n/2) + \Theta(1)&\text{otherwise} \end{cases}\)
\(cn\)
\[\frac{cn}{2}\]
\[\frac{cn}{4}\]
1
\[\frac{cn}{4}\]
\[\frac{cn}{2}\]
\[\frac{cn}{4}\]
\[\frac{cn}{4}\]
1
1
1
1
1
1
1
. . .
. . .
. . .
\[cn\]
\[cn\]
\[cn\]
\[\Theta(n)\]
6.1 - Recursion-Tree Method
\(T(n) = \begin{cases} \Theta(1) &\text{if $n \le 1$}\\ 2T(n/2) + \Theta(1)&\text{otherwise} \end{cases}\)
\(1\)
\[1\]
\[1\]
1
\[1\]
\[1\]
\[1\]
\[1\]
1
1
1
1
1
1
1
. . .
. . .
. . .
\[4\]
\[2\]
\[1\]
\[???\]
6.1 - Recursion-Tree Method
\(T(n) = \begin{cases} \Theta(1) &\text{if $n \le 1$}\\ 2T(n/2) + \Theta(1)&\text{otherwise} \end{cases}\)
6.1 - Recursion-Tree Method
\(T(n) = \begin{cases} \Theta(1) &\text{if $n \le 1$}\\ 2T(n/2) + \Theta(1)&\text{otherwise} \end{cases}\)
\[4\]
\[2\]
\[1\]
\[\Theta(2^{\log_2 n}) = \Theta(n)\]
\(1\)
\[1\]
\[1\]
1
\[1\]
\[1\]
\[1\]
\[1\]
1
1
1
1
1
1
1
. . .
. . .
. . .
6.1 - Recursion-Tree Method
\(T(n) = \begin{cases} \Theta(1) &\text{if $n \le 1$}\\ 2T(n/2) + \Theta(1)&\text{otherwise} \end{cases}\)
6.1 - Recursion-Tree Method
\(T(n) = \begin{cases} \Theta(1) &\text{if $n \le 1$}\\ 2T(n/2) + \Theta(1)&\text{otherwise} \end{cases}\)
\[\sum_{k=0}^m x^k = 1 + x + x^2 + \cdots + x^m = \frac{x^{m+1}-1}{x-1}\]
6.1 - Recursion-Tree Method
\(T(n) = \begin{cases} \Theta(1) &\text{if $n \le 1$}\\ 2T(n/2) + \Theta(1)&\text{otherwise} \end{cases}\)
\[\sum_{k=0}^{\log_2 n-1} 2^k = 1 + 2 + 2^2 + \cdots + 2^{\log_2 n-1}= \frac{2^{\log_2 n}-1}{2-1} = n - 1\]
\[\sum_{k=0}^m x^k = 1 + x + x^2 + \cdots + x^m = \frac{x^{m+1}-1}{x-1}\]
so linear search is \(\Theta(n)\)
6.1 - Recursion-Tree Method
\(T(n) = \begin{cases} \Theta(1) &\text{if $n \le 1$}\\ 3T(n/3) + \Theta(1)&\text{otherwise} \end{cases}\)
6.1 - Recursion-Tree Method
\(T(n) = \begin{cases} \Theta(1) &\text{if $n \le 1$}\\ 3T(n/3) + \Theta(1)&\text{otherwise} \end{cases}\)
\(1\)
1
1
1
1
1
1
. . .
. . .
\[9\]
\[3\]
\[1\]
\[\Theta(3^{\log_3 n}) = \Theta(n)\]
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
6.1 - Recursion-Tree Method
\(T(n) = \begin{cases} \Theta(1) &\text{if $n \le 1$}\\ 3T(n/3) + \Theta(1)&\text{otherwise} \end{cases}\)
\[\sum_{k=0}^m x^k = 1 + x + x^2 + \cdots + x^m = \frac{x^{m+1}-1}{x-1}\]
6.1 - Recursion-Tree Method
\(T(n) = \begin{cases} \Theta(1) &\text{if $n \le 1$}\\ 3T(n/3) + \Theta(1)&\text{otherwise} \end{cases}\)
\[\sum_{k=0}^m x^k = 1 + x + x^2 + \cdots + x^m = \frac{x^{m+1}-1}{x-1}\]
\[\sum_{k=0}^{\log_3 n-1} x^k = 1 + 3 + 3^2 + \cdots + 3^{\log_3 n -1} = \frac{3^{\log_3 n}-1}{3-1} = \frac{n-1}{2}\]
6.1 - Recursion-Tree Method
\(T(n) = \begin{cases} \Theta(1) &\text{if $n \le 1$}\\ 3T(n/3) + \Theta(1)&\text{otherwise} \end{cases}\)
\(2\)
2
2
2
2
2
2
. . .
. . .
\[9\]
\[3\]
\[1\]
\[\Theta(3^{\log_3 n}) = \Theta(n)\]
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
6.1 - Recursion-Tree Method
\(T(n) = \begin{cases} \Theta(1) &\text{if $n \le 1$}\\ 3T(n/3) + \Theta(1)&\text{otherwise} \end{cases}\)
\[\sum_{k=0}^{\log_3 n-1} x^k = 1 + 3 + 3^2 + \cdots + 3^{\log_3 n -1} = \frac{3^{\log_3 n}-1}{3-1} = \frac{n-1}{2}\]
\[\sum_{k=0}^{\log_3 n-1} x^k = 2(1) + 2(3) + 2(3^2) + \cdots + 2(3^{\log_3 n -1}) = \frac{2(3^{\log_3 n}-1)}{3-1} =n-1\]
6.1 - Recursion-Tree Method
\(T(n) = \begin{cases} \Theta(1) &\text{if $n \le 1$}\\ 3T(n/3) + \Theta(1)&\text{otherwise} \end{cases}\)
6.1 - Recursion-Tree Method
\(T(n) = \begin{cases} \Theta(1) &\text{if $n \le 1$}\\ 3T(n/4) + cn^2&\text{otherwise} \end{cases}\)
\(T(n)\)
\[T\left(\frac{n}{4}\right)\]
\[T\left(\frac{n}{4}\right)\]
\[T\left(\frac{n}{4}\right)\]
we assume \(n\) is a power of 4
6.1 - Recursion-Tree Method
\(T(n) = \begin{cases} \Theta(1) &\text{if $n \le 1$}\\ 3T(n/4) + cn^2&\text{otherwise} \end{cases}\)
\(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
6.1 - Recursion-Tree Method
\(T(n) = \begin{cases} \Theta(1) &\text{if $n \le 1$}\\ 3T(n/4) + cn^2&\text{otherwise} \end{cases}\)
\(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\]
???
6.1 - Recursion-Tree Method
\(T(n) = \begin{cases} \Theta(1) &\text{if $n \le 1$}\\ 3T(n/4) + cn^2&\text{otherwise} \end{cases}\)
6.1 - Recursion-Tree Method
\(T(n) = \begin{cases} \Theta(1) &\text{if $n \le 1$}\\ 3T(n/4) + cn^2&\text{otherwise} \end{cases}\)
\(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\]
\(\Theta(3^{\log_4 n})\)
6.1 - Recursion-Tree Method
\(T(n) = \begin{cases} \Theta(1) &\text{if $n \le 1$}\\ 3T(n/4) + cn^2&\text{otherwise} \end{cases}\)
\(cn^2\)
\[\frac{3}{16}cn^2\]
\[\frac{9}{256}cn^2\]
\(\Theta(3^{\log_4 n})\)
6.1 - Recursion-Tree Method
\(T(n) = \begin{cases} \Theta(1) &\text{if $n \le 1$}\\ 3T(n/4) + cn^2&\text{otherwise} \end{cases}\)
\(cn^2\)
\[\frac{3}{16}cn^2\]
\[\frac{9}{256}cn^2\]
\(\Theta(3^{\log_4 n})\)
\(+\)
\(+\)
\(+ \dots +\)
\[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+ \Theta(3^{\log_4 n})\]
\[cn^2\sum_{i=0}^{\log_4n-1}\left(\frac{3}{16}\right)^i+ \Theta(3^{\log_4 n})\]
\[= \frac{(3/16)^{\log_4 n}-1}{(3/16)-1}cn^2+ \Theta(3^{\log_4 n})\]
6.1 - Recursion-Tree Method
\(T(n) = \begin{cases} \Theta(1) &\text{if $n \le 1$}\\ 3T(n/4) + cn^2&\text{otherwise} \end{cases}\)
\[cn^2\sum_{i=0}^{\log_4n-1}\left(\frac{3}{16}\right)^i+ \Theta(3^{\log_4 n})\]
\[= \frac{(3/16)^{\log_4 n}-1}{(3/16)-1}cn^2+ \Theta(3^{\log_4 n})\]
\[\sum_{k=0}^\infty x^k = \frac{1}{1-x}\]
\[\sum_{k=0}^m x^k = 1 + x + x^2 + \cdots + x^m = \frac{x^{m+1}-1}{x-1}\]
6.1 - Recursion-Tree Method
\(T(n) = \begin{cases} \Theta(1) &\text{if $n \le 1$}\\ 3T(n/4) + cn^2&\text{otherwise} \end{cases}\)
\[\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+ \Theta(3^{\log_4 n})\]
\[< \sum_{i=0}^{\infty}\left(\frac{3}{16}\right)^i cn^2+ \Theta(3^{\log_4 n})\]
\[=\frac{1}{1-(3/16)} cn^2+ \Theta(3^{\log_4 n})\]
6.1 - Recursion-Tree Method
\(T(n) = \begin{cases} \Theta(1) &\text{if $n \le 1$}\\ 3T(n/4) + cn^2&\text{otherwise} \end{cases}\)
\[\frac{1}{1-(3/16)} cn^2+ \Theta(3^{\log_4 n})\]
\[=\frac{1}{1-(3/16)} cn^2+ \Theta(n^{\log_4 3})\]
\[\frac{1}{1-(3/16)} cn^2+ \Theta(n^{0.79}) = O(n^2)\]
6.1 - Recursion-Tree Method
\(T(n) = \begin{cases} \Theta(1) &\text{if $n \le 1$}\\ 3T(n/4) + cn^2&\text{otherwise} \end{cases}\)
\[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+ \Theta(3^{\log_4 n})\]
\[\left(\frac{3}{16}\right)^{\log_4 n} cn^2 = \frac{3^{\log_4 n}}{16^{\log_4 n}}cn^2\]
\[\left(\frac{3}{16}\right)^{\log_4 n} cn^2\]
\(16^{\log_4 n} = (4^2)^{\log_4 n} = (4^{\log_4 n})^2 = n^2\)
\[\left(\frac{3}{16}\right)^{\log_4 n} cn^2 = \frac{3^{\log_4 n}}{n^2}cn^2 = c\cdot 3^{\log_4 n}\]
6.1 - Recursion-Tree Method
\(T(n) = \begin{cases} \Theta(1) &\text{if $n \le 1$}\\ 3T(n/4) + cn^2&\text{otherwise} \end{cases}\)
\[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+ \Theta(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+ \left(\frac{3}{16}\right)^{\log_4 n}cn^2 \]
\[cn^2\sum_{i=0}^{\log_4n-1}\left(\frac{3}{16}\right)^i+ \Theta(3^{\log_4 n})\]
\[cn^2\sum_{i=0}^{\log_4n}\left(\frac{3}{16}\right)^i\]
or
6.1 - Recursion-Tree Method
6.1 - Recursion-Tree Method
I told you that recursion-tree will be in the exam
I just want a pass