Guess T(n)=O(n3) (need to prove O and Ω separately)
Prove by induction that T(n)≤cn3 for large n (i.e. n≥n0)
Inductive hypothesis: T(k)≤ck3 for any k<n
Assuming ind. hyp. holds, prove T(n)≤cn3
CE100 Algorithms and Programming II
Substitution Method: Example (2)
Original recurrence: T(n)=4T(n/2)+n
From inductive hypothesis: T(n/2)≤c(n/2)3
Substitute this into the original recurrence:
T(n)≤4c(n/2)3+n
=(c/2)n3+n
=cn3–((c/2)n3–n)⟸ desired - residual
≤cn3
when ((c/2)n3–n)≥0
CE100 Algorithms and Programming II
Substitution Method: Example (3)
So far, we have shown:
T(n)≤cn3 when ((c/2)n3–n)≥0
We can choose c≥2 and n0≥1
But, the proof is not complete yet.
Reminder: Proof by induction: 1.Prove the base cases⟸ haven’t proved the base cases yet 2.Inductive hypothesis for smaller sizes 3.Prove the general case
CE100 Algorithms and Programming II
Substitution Method: Example (4)
We need to prove the base cases
Base: T(n)=Θ(1) for small n (e.g. for n=n0)
We should show that:
Θ(1)≤cn3 for n=n0 , This holds if we pick c big enough
So, the proof of T(n)=O(n3) is complete
But, is this a tight bound?
CE100 Algorithms and Programming II
Example: A tighter upper bound? (1)
Original recurrence: T(n)=4T(n/2)+n
Try to prove that T(n)=O(n2),
i.e. T(n)≤cn2 for all n≥n0
Ind. hyp: Assume that T(k)≤ck2 for k<n
Prove the general case:T(n)≤cn2
CE100 Algorithms and Programming II
Example: A tighter upper bound? (2)
Original recurrence: T(n)=4T(n/2)+n
Ind. hyp: Assume that T(k)≤ck2 for k<n
Prove the general case: T(n)≤cn2
T(n)=4T(n/2)+n≤4c(n/2)2+n=cn2+n=O(n2)⟸ Wrong! We must prove exactly
CE100 Algorithms and Programming II
Example: A tighter upper bound? (3)
Original recurrence:T(n)=4T(n/2)+n Ind. hyp: Assume that T(k)≤ck2 for k<n Prove the general case:T(n)≤cn2
So far, we have: T(n)≤cn2+n
No matter which positive c value we choose, this does not show that T(n)≤cn2
Proof failed?
CE100 Algorithms and Programming II
Example: A tighter upper bound? (4)
What was the problem?
The inductive hypothesis was not strong enough
Idea: Start with a stronger inductive hypothesis
Subtract a low-order term
Inductive hypothesis:T(k)≤c1k2–c2k for k<n
Prove the general case:T(n)≤c1n2−c2n
CE100 Algorithms and Programming II
Example: A tighter upper bound? (5)
Original recurrence:T(n)=4T(n/2)+n
Ind. hyp: Assume that T(k)≤c1k2–c2k for k<n
Prove the general case: T(n)≤c1n2–c2n
T(n)=4T(n/2)+n≤4(c1(n/2)2–c2(n/2))+n=c1n2–2c2n+n=c1n2–c2n–(c2n–n)≤c1n2–c2n for n(c2–1)≥0choose c2≥1
CE100 Algorithms and Programming II
Example: A tighter upper bound? (6)
We now need to prove
T(n)≤c1n2–c2n
for the base cases.
T(n)=Θ(1) for 1≤n≤n0 (implicit assumption)
Θ(1)≤c1n2–c2n for n small enough (e.g. n=n0)
We can choose c1 large enough to make this hold
We have proved that T(n)=O(n2)
CE100 Algorithms and Programming II
Substitution Method: Example 2 (1)
For the recurrence T(n)=4T(n/2)+n,
prove that T(n)=Ω(n2)
i.e. T(n)≥cn2 for any n≥n0
Ind. hyp:T(k)≥ck2 for any k<n
Prove general case:T(n)≥cn2
T(n)=4T(n/2)+n ≥4c(n/2)2+n =cn2+n ≥cn2 since n>0
Proof succeeded – no need to strengthen the ind. hyp as in the last example
CE100 Algorithms and Programming II
Substitution Method: Example 2 (2)
We now need to prove that T(n)≥cn2
for the base cases T(n)=Θ(1) for 1≤n≤n0 (implicit assumption) Θ(1)≥cn2 for n=n0
n0 is sufficiently small (i.e. constant)
We can choose c small enough for this to hold
We have proved that T(n)=Ω(n2)
CE100 Algorithms and Programming II
Substitution Method - Summary
Guess the asymptotic complexity
Prove your guess using induction
Assume inductive hypothesis holds for k<n
Try to prove the general case for n
Note: MUST prove the EXACT inequality CANNOT ignore lower order terms, If the proof fails, strengthen the ind. hyp. and try again
Prove the base cases (usually straightforward)
CE100 Algorithms and Programming II
Recursion Tree Method
A recursion tree models the runtime costs of a recursive execution of an algorithm.
The recursion tree method is good for generating guesses for the substitution method.
The recursion-tree method can be unreliable.
Not suitable for formal proofs
The recursion-tree method promotes intuition, however.
CE100 Algorithms and Programming II
Solve Recurrence (1) : T(n)=2T(n/2)+Θ(n)
CE100 Algorithms and Programming II
Solve Recurrence (2) : T(n)=2T(n/2)+Θ(n)
CE100 Algorithms and Programming II
Solve Recurrence (3) : T(n)=2T(n/2)+Θ(n)
CE100 Algorithms and Programming II
Example of Recursion Tree (1)
Solve T(n)=T(n/4)+T(n/2)+n2
CE100 Algorithms and Programming II
Example of Recursion Tree (2)
Solve T(n)=T(n/4)+T(n/2)+n2
CE100 Algorithms and Programming II
Example of Recursion Tree (3)
Solve T(n)=T(n/4)+T(n/2)+n2
CE100 Algorithms and Programming II
The Master Method
A powerful black-box method to solve recurrences.
The master method applies to recurrences of the form
T(n)=aT(n/b)+f(n)
where a≥1,b>1, and f is asymptotically positive.
CE100 Algorithms and Programming II
The Master Method: 3 Cases
(TODO : Add Notes )
Recurrence: T(n)=aT(n/b)+f(n)
Compare f(n) with nlogba
Intuitively:
Case 1:f(n) grows polynomially slower than nlogba
Case 2:f(n) grows at the same rate as nlogba
Case 3:f(n) grows polynomially faster than nlogba
CE100 Algorithms and Programming II
The Master Method: Case 1 (Bigger)
Recurrence: T(n)=aT(n/b)+f(n)
Case 1:f(n)nlogba=Ω(nε) for some constant ε>0
i.e., f(n) grows polynomialy slower than nlogba (by an nε factor)
Solution:T(n)=Θ(nlogba)
CE100 Algorithms and Programming II
The Master Method: Case 2 (Simple Version) (Equal)
Recurrence: T(n)=aT(n/b)+f(n)
Case 2:nlogbaf(n)=Θ(1)
i.e., f(n) and nlogba grow at similar rates
Solution:T(n)=Θ(nlogbalgn)
CE100 Algorithms and Programming II
The Master Method: Case 3 (Smaller)
Case 3:nlogbaf(n)=Ω(nε) for some constant ε>0
i.e., f(n) grows polynomialy faster than nlogba (by an nε factor)
and the following regularity condition holds:
af(n/b)≤cf(n) for some constant c<1
Solution: T(n)=Θ(f(n))
CE100 Algorithms and Programming II
The Master Method Example (case-1) : T(n)=4T(n/2)+n
a=4
b=2
f(n)=n
nlogba=nlog24=nlog222=n2log22=n2
f(n)=n grows polynomially slower than nlogba=n2
f(n)nlogba=nn2=n=Ω(nε)
CASE-1:
T(n)=Θ(nlogba)=Θ(nlog24)=Θ(n2)
CE100 Algorithms and Programming II
The Master Method Example (case-2) : T(n)=4T(n/2)+n2
a=4
b=2
f(n)=n2
nlogba=nlog24=nlog222=n2log22=n2
f(n)=n2 grows at similar rate as nlogba=n2
f(n)=Θ(nlogba)=n2
CASE-2:
T(n)=Θ(nlogbalgn)=Θ(nlog24lgn)=Θ(n2lgn)
CE100 Algorithms and Programming II
The Master Method Example (case-3) (1) : T(n)=4T(n/2)+n3
a=4
b=2
f(n)=n3
nlogba=nlog24=nlog222=n2log22=n2
f(n)=n3 grows polynomially faster than nlogba=n2
nlogbaf(n)=n2n3=n=Ω(nε)
CE100 Algorithms and Programming II
The Master Method Example (case-3) (2) : T(n)=4T(n/2)+n3 (con't)
Seems like CASE 3, but need to check the regularity condition
Regularity condition af(n/b)≤cf(n) for some constant c<1
4(n/2)3≤cn3 for c=1/2
CASE-3:
T(n)=Θ(f(n))⟹T(n)=Θ(n3)
CE100 Algorithms and Programming II
The Master Method Example (N/A case) : T(n)=4T(n/2)+n2lgn
a=4
b=2
f(n)=n2lgn
nlogba=nlog24=nlog222=n2log22=n2
f(n)=n2lgn grows slower than nlogba=n2
but is it polynomially slower?
=nlogbaf(n)lgnn2n2=lgn=Ω(nε) for any ε>0
is not CASE-1
Master Method does not apply!
CE100 Algorithms and Programming II
The Master Method : Case 2 (General Version)
Recurrence : T(n)=aT(n/b)+f(n)
Case 2: nlogbaf(n)=Θ(lgkn) for some constant k≥0
Solution : T(n)=Θ(nlogbalgk+1n)
CE100 Algorithms and Programming II
General Method (Akra-Bazzi)
T(n)=i=1∑kaiT(n/bi)+f(n)
Let p be the unique solution to
i=1∑k(ai/bip)=1
Then, the answers are the same as for the master method, but with np instead of nlogba (Akra and Bazzi also prove an even more general result.)
CE100 Algorithms and Programming II
Idea of Master Theorem (1)
Recursion Tree:
CE100 Algorithms and Programming II
Idea of Master Theorem (2)
CASE 1 : The weight increases geometrically from the root to the leaves. The leaves hold a constant fraction of the total weight.
nlogbaT(1)=Θ(nlogba)
CE100 Algorithms and Programming II
Idea of Master Theorem (3)
CASE 2 : (k=0) The weight is approximately the same on each of the logbn levels.
nlogbaT(1)=Θ(nlogbalgn)
CE100 Algorithms and Programming II
Idea of Master Theorem (4)
CASE 3 : The weight decreases geometrically from the root to the leaves. The root holds a constant fraction of the total weight.
nlogbaT(1)=Θ(f(n))
CE100 Algorithms and Programming II
Proof of Master Theorem: Case 1 and Case 2
Recall from the recursion tree (note h=lgbn=tree height)
SELECTION-SORT(A)
n = A.length;
for j=1 to n-1
smallest=j;
for i= j+1 to n
if A[i]<A[smallest]
smallest=i;
endfor
exchange A[j] with A[smallest]
endfor
CE100 Algorithms and Programming II
Selection Sort Algorithm
T(n)={Θ(1)T(n−1)+Θ(n)ifn=1n>1
Sequential Series
cost=n(n+1)/2=1/2n2+1/2n
Drop low-order terms
Ignore the constant coefficient in the leading term
T(n)=Θ(n2)
CE100 Algorithms and Programming II
Merge Sort Algorithm (initial setup)
Merge Sort is a recursive sorting algorithm, for initial case we need to call Merge-Sort(A,1,n) for sorting A[1..n]
initial case
A : Array
p : 1 (offset)
r : n (length)
Merge-Sort(A,1,n)
CE100 Algorithms and Programming II
Merge Sort Algorithm (internal iterations)
internal iterations
p=start−point
q=mid−point
r=end−point
A : Array
p : offset
r : length
Merge-Sort(A,p,r)
if p=r then (CHECK FOR BASE-CASE)
returnelse
q = floor((p+r)/2) (DIVIDE)
Merge-Sort(A,p,q) (CONQUER)
Merge-Sort(A,q+1,r) (CONQUER)
Merge(A,p,q,r) (COMBINE)
endif
CE100 Algorithms and Programming II
Merge Sort Combine Algorithm (1)
Merge(A,p,q,r)
n1 = q-p+1
n2 = r-q
//allocate left and right arrays
//increment will be from left to right
//left part will be bigger than right part
L[1...n1+1] //left array
R[1...n2+1] //right array
//copy left part of array
for i=1 to n1
L[i]=A[p+i-1]
//copy right part of array
for j=1 to n2
R[j]=A[q+j]
//put end items maximum values for termination
L[n1+1]=inf
R[n2+1]=inf
i=1,j=1for k=p to r
if L[i]<=R[j]
A[k]=L[i]
i=i+1else
A[k]=R[j]
j=j+1
CE100 Algorithms and Programming II
Example : Merge Sort
Divide: Trivial.
Conquer: Recursively sort 2 subarrays.
Combine: Linear- time merge.
T(n)=2T(n/2)+Θ(n)
Subproblems ⟹2
Subproblemsize ⟹n/2
Work dividing and combining ⟹Θ(n)
CE100 Algorithms and Programming II
Master Theorem: Reminder
T(n)=aT(n/b)+f(n)
Case 1: f(n)nlogba=Ω(nε)⟹T(n)=Θ(nlogba)
Case 2: nlogbaf(n)=Θ(lgkn)⟹T(n)=Θ(nlogbalgk+1n)
Case 3: f(n)nlogba=Ω(nε)⟹T(n)=Θ(f(n)) and af(n/b)≤cf(n) for c<1
CE100 Algorithms and Programming II
Merge Sort: Solving the Recurrence
T(n)=2T(n/2)+Θ(n) a=2,b=2,f(n)=Θ(n),nlogba=n
Case-2: nlogbaf(n)=Θ(lgkn)⟹T(n)=Θ(nlogbalgk+1n) holds for k=0
ITERATIVE-BINARY-SEARCH(A,V,low,high)
while low<=high
mid=floor((low+high)/2);
if v == A[mid]
return mid;
elseif v > A[mid]
low = mid + 1;
else
high = mid - 1;
endwhile
return NIL
CE100 Algorithms and Programming II
Binary Search (4): Recursive
RECURSIVE-BINARY-SEARCH(A,V,low,high)
if low>high
return NIL;
endif
mid = floor((low+high)/2);
if v == A[mid]
return mid;
elseif v > A[mid]
return RECURSIVE-BINARY-SEARCH(A,V,mid+1,high);
elsereturn RECURSIVE-BINARY-SEARCH(A,V,low,mid-1);
endif
CE100 Algorithms and Programming II
Binary Search (5): Recursive
T(n)=T(n/2)+Θ(1)⟹T(n)=Θ(lgn)
CE100 Algorithms and Programming II
Binary Search (6): Example (Find 9)
CE100 Algorithms and Programming II
Recurrence for Binary Search (7)
T(n)=1T(n/2)+Θ(1)
Subproblems ⟹1
Subproblemsize ⟹n/2
Work dividing and combining ⟹Θ(1)
CE100 Algorithms and Programming II
Binary Search: Solving the Recurrence (8)
T(n)=T(n/2)+Θ(1)
a=1,b=2,f(n)=Θ(1)⟹nlogba=n0=1
Case 2:nlogbaf(n)=Θ(lgkn)⟹T(n)=Θ(nlogbalgk+1n) holds for k=0
T(n)=Θ(lgn)
CE100 Algorithms and Programming II
Powering a Number: Divide & Conquer (1)
Problem: Compute an, where n is a natural number
NAIVE-POWER(a, n)
powerVal = 1;
for i = 1 to n
powerVal = powerVal * a;
endfor
return powerVal;
What is the complexity? ⟹T(n)=Θ(n)
CE100 Algorithms and Programming II
Powering a Number: Divide & Conquer (2)
Basic Idea:
an={an/2∗an/2a(n−1)/2∗a(n−1)/2∗aif n is evenif n is odd
CE100 Algorithms and Programming II
Powering a Number: Divide & Conquer (3)
POWER(a, n)
if n = 0 then
return1;
elseif n is even then
val = POWER(a, n/2);
return val * val;
elseif n is odd then
val = POWER(a,(n-1)/2)
return val * val * a;
endif
CE100 Algorithms and Programming II
Powering a Number: Solving the Recurrence (4)
T(n)=T(n/2)+Θ(1)
a=1,b=2,f(n)=Θ(1)⟹nlogba=n0=1
Case 2:nlogbaf(n)=Θ(lgkn)⟹T(n)=Θ(nlogbalgk+1n) holds for k=0
T(n)=Θ(lgn)
CE100 Algorithms and Programming II
Correctness Proofs for Divide and Conquer Algorithms
Proof by induction commonly used for Divide and Conquer Algorithms
Base case: Show that the algorithm is correct when the recursion bottoms out (i.e., for sufficiently small n)
Inductive hypothesis: Assume the alg. is correct for any recursive call on any smaller subproblem of size k, (k<n)
General case: Based on the inductive hypothesis, prove that the alg. is correct for any input of size n
CE100 Algorithms and Programming II
Example Correctness Proof: Powering a Number
Base Case:POWER(a,0) is correct, because it returns 1
Ind. Hyp: Assume POWER(a,k) is correct for any k<n