Daniel Sutantyo, Department of Computing, Macquarie University
6.0 - Divide and Conquer
Reference for this week: CLRS Chapter 4
Divide and Conquer
Optimal Substructure
6.0 - Divide and Conquer
Divide and Conquer
Optimal Substructure
Reference for this week: CLRS Chapter 4
6.0 - Divide and Conquer
6.0 - Divide and Conquer
13
4
19
11
32
22
7
32
22
7
32
22
7
13
4
19
11
13
4
19
11
6.0 - Divide and Conquer
13
4
19
11
32
7
22
13
19
32
22
13
19
13
6.0 - Divide and Conquer
6.0 - Divide and Conquer
\(T(n) = 2T(n/2) + n\)
the number of operations:
\(T(1) = 1\)
6.0 - Divide and Conquer
\(T(n) = T(n/2) + 1\)
the number of operations:
\(T(1) = 1\)
6.0 - Divide and Conquer
\(T(n) = \begin{cases} \Theta(1) &\text{if $n \le c$}\\ aT(n/b) + D(n) + C(n) &\text{otherwise} \end{cases}\)
6.0 - Divide and Conquer
13
4
19
11
32
22
7
\(T(n) = 2T(n/2) + n\)
6.0 - Divide and Conquer
13
4
19
11
32
22
7
32
22
7
13
4
19
11
\(T(n) = 2T(n/2) + n\)
6.0 - Divide and Conquer
13
4
19
11
32
22
7
32
22
7
32
22
7
13
4
19
11
13
4
19
11
\(T(n) = 2T(n/2) + n\)
6.0 - Divide and Conquer
19
32
7
22
11
13
4
13
4
19
11
32
7
22
22
7
32
11
19
4
13
\(T(n) = 2T(n/2) + n\)
6.0 - Divide and Conquer
19
32
7
22
11
13
4
13
4
19
11
32
7
22
22
7
32
11
19
4
13
\(T(n) = 2T(n/2) + n\)
6.0 - Divide and Conquer
19
32
7
22
11
13
4
13
4
19
11
32
7
22
22
7
32
11
19
4
13
\(T(n) = 2T(n/2) + n\)
6.0 - Divide and Conquer
\(T(n) = \begin{cases} \Theta(1) &\text{if $n \le c$}\\ aT(n/b) + D(n) + C(n) &\text{otherwise} \end{cases}\)
6.0 - Divide and Conquer
13
4
19
11
32
7
22
13
19
32
22
13
19
13
\(T(n) = T(n/2) + 1\)
... or is it?
\(T(n) = \begin{cases} \Theta(1) &\text{if $n \le c$}\\ aT(n/b) + D(n) + C(n) &\text{otherwise} \end{cases}\)
6.0 - Divide and Conquer
\(T(n) \le aT(n/b) + f(n)\)
\(T(n) \ge aT(n/b) + f(n)\)
or
\(T(n) = \begin{cases} \Theta(1) &\text{if $n \le c$}\\ aT(n/b) + D(n) + C(n) &\text{otherwise} \end{cases}\)
6.0 - Divide and Conquer
\(T(n) = \begin{cases} \Theta(1) &\text{if $n \le c$}\\ aT(n/b) + D(n) + C(n) &\text{otherwise} \end{cases}\)
6.0 - Divide and Conquer
\(n\)
\[\frac{n}{2}\]
\[\frac{n}{2}\]
\(n\)
\[\frac{n}{3}\]
\[\frac{n}{3}\]
\[\frac{n}{3}\]
\(T(n) = \begin{cases} \Theta(1) &\text{if $n \le c$}\\ aT(n/b) + D(n) + C(n) &\text{otherwise} \end{cases}\)
6.0 - Divide and Conquer
\(T(n) = \begin{cases} \Theta(1) &\text{if $n \le c$}\\ aT(n/b) + D(n) + C(n) &\text{otherwise} \end{cases}\)
6.0 - Divide and Conquer
\(T(n) = \begin{cases} \Theta(1) &\text{if $n \le c$}\\ aT(n/b) + D(n) + C(n) &\text{otherwise} \end{cases}\)
6.0 - Divide and Conquer
6.0 - Divide and Conquer
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));
}
6.0 - Divide and Conquer
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));
}
linearSearch(a[], 0, 8, target)
linearSearch(a[], 0, 3, target)
linearSearch(a[], 5, 8, target)
\(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$}\\ 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}\)
6.0 - Divide and Conquer
6.0 - Divide and Conquer
22
7
32
11
19
4
13
6.0 - Divide and Conquer
22
7
32
11
19
4
13
6.0 - Divide and Conquer
22
7
32
11
19
4
13
22
7
32
11
19
4
6.0 - Divide and Conquer
22
7
32
11
19
4
13
22
7
32
11
19
4
6.0 - Divide and Conquer
22
7
32
11
19
4
13
22
7
32
11
19
4
6.0 - Divide and Conquer
22
7
32
11
19
4
13
22
7
32
11
19
4
22
7
19
4
6.0 - Divide and Conquer
22
7
32
11
19
4
13
22
7
32
11
19
4
22
7
19
4
6.0 - Divide and Conquer
22
7
32
11
19
4
13
22
7
32
11
19
4
22
7
19
4
6.0 - Divide and Conquer
13
32
11
22
7
19
4
6.0 - Divide and Conquer
13
32
11
22
7
19
4
13
32
11
22
7
19
13
32
11
22
7
19
6.0 - Divide and Conquer
13
32
11
22
7
19
4
13
32
11
22
7
19
13
32
11
22
7
19
13
32
11
22
19
13
32
11
22
19
\(T(n) = \begin{cases} \Theta(1) &\text{if $n \le 1$}\\ 2T(n/2) + \Theta(n)&\text{otherwise} \end{cases}\)
\(T(n) = \begin{cases} \Theta(1) &\text{if $n \le 1$}\\ T(n-1) + \Theta(n)&\text{otherwise} \end{cases}\)
6.0 - Divide and Conquer
6.0 - Divide and Conquer