4 Substitution Method and Master Theorem
4 Substitution Method and Master Theorem
Substitution Method
Master Theorem
2
How to determine complexity of code structures
If Statement: Take the complexity of the most expensive case :
char key;
int[][] A = new int[5][5];
int[][] B = new int[5][5];
int[][] C = new int[5][5];
........
if(key == '+') {
for(int i = 0; i < n; i++)
for(int j = 0; j < n; j++) O(n2)
C[i][j] = A[i][j] + B[i][j];
} // End of if block
Overall
complexity
else if(key == 'x')
O(n3)
C = matrixMult(A, B);
O(n3)
else
System.out.println("Error! Enter '+' or 'x'!");
O(1)
1 n=0
T(n)
Solving the Recurrence Relation = T(n) = T(n-1) + 1 n>0
T(n-1) = T(n-2) + 1
T(n) = {T(n-2) + 1} + 1
Put n=n-1 both Sides
T(n) = T(n-2) + 2
T(n-1-1) = T(n-1-2) + 1
T(n) = [T(n-3) + 1 ] + 2
T(n-2) = T(n-3) + 1
T(n) = T(n-3) + 3
1 n=0
T(n) =
Solving the Recurrence Relation T(n) = T(n-1) + 1 n>0
Substitution Method
T(n) = T(n-2) + 2
T(n) = T(n-3) + 3
T(n) = T(n-4) + 4
T(n) = T(n-5) + 5
T(n) = T(n-k) + k
Solving the Recurrence Relation
A time will come k=n
Substitution Method
So we can put k=n
T(n) = T(n-1) + 1 n>0
T(n) = T(n-n) + n
T(n) = T(n-2) + 2
T(n) = T(0) + n
T(n) = T(n-3) + 3
T(n) = T(n-4) + 4
T(n) = T(n-5) + 5
T(n) = 1 + n
T(n) = T(n-k) + k
T(n) = theta(n)
Example
Recurrence Relation
Solution – Recursion TreeT(n)
1 n=0 n T (n-1)
T(n) =
T(n) = T(n-1) + n n>0
n-1 T (n-2)
n-2 T (n-3)
n-k T (n-k-1)
1 T (0)
Recurrence Relation Solution – Recursion
Tree 1 n=0
T(n) n T(n) =
T(n) = T(n-1) + n n>0
n T (n-1) n-1
1 T (0) 0
Recurrence Relation Solution – Recursion
Tree 1 n=0
T(n) n T(n) =
T(n) = T(n-1) + n n>0
n T (n-1) n-1
1 T (0) 0
The M a s te r Theorem
O (n d ) if log b a <
T(n) = O (n d log d if log b a
n)
O (n
log b a
) = d if log b
a > d
The M a s te r Theorem
12
Solving Existing Recurrences
● Consider the mergesort recurrence
T(0) = Θ (1)
T(1) = Θ (1)
T(n) = T(⌈n / 2⌉) + T(⌊n / 2⌋) +
Θ (n)
Solving Existing Recurrences
● Consider the mergesort recurrence
T(0) = Θ (1)
T(1) = Θ (1)
T(n) ≤ 2T(⌈n / 2⌉) + Θ (n)
T(0) = Θ (1)
T(1) = Θ (1)
T(n) ≤ 2T(⌈n / 2⌉) + Θ (n)
T(0) = Θ (1)
T(1) = Θ (1)
T(n) ≤ 2T(⌈n / 2⌉) + Θ (n)
T(0) = Θ (1)
T(1) = Θ (1)
T(n) ≤ 2T(⌈n / 2⌉) + Θ (n)
T(0) = Θ (1)
T(1) = Θ (1)
T(n) ≤ 2T(⌈n / 2⌉) + Θ (n)
T(0) = Θ (1)
T(1) = Θ (1)
T(n) ≤ 2T(⌈n / 2⌉) + Θ (n)
cn d cn d
c (n / c (n / ... c (n / ac (n /
b)d b)d b)d b)d
c (n / c (n / c (n / a2 c (n /
b 2 )d b 2 )d b 2 )d b 2 )d
c (n / c (n / c (n / a3 c (n /
b 3 )d b 3 )d b 3 )d b 3 )d
...
log b n
c c ... c ca