Reccurence - Algorithm
Reccurence - Algorithm
Reccurence - Algorithm
Recurrences
&
Master Method
Mashiour Rahman
Assistant Professor
[email protected]
American International University Bangladesh
Recursion
• An object is recursive if
it contains itself as part of it, or
it is defined in terms of itself.
• Factorial: n!
How do you compute 10!?
n! = 1 * 2 * 3 *...* n
n! = n * (n-1)!
fac(2)
2 * fac(1)
fac(1)
1
Mashiour Rahman AIUB::CSC2105::Algorithm Recurrences 4
Bookkeeping
• The computer maintains an activation stack for active
procedure calls ( compiler construction). Example
for fac(5).
fac(1) 1 =1
fac(2) 2*fac(1) 1 =2
fac(3) 3*fac(2) 2 =6
fac(4) 4*fac(3) 6 = 24
fac3
fac3 • fac3 is incorrect: Infinite
INPUT
INPUT::nn––aanatural
naturalnumber.
number.
OUTPUT
recursion. The termination
OUTPUT::n! n!(factorial
(factorialof
ofn)
n)
condition is never reached.
fac
fac33(n)
(n)
return
return nn ** fac3(n-1)
fac3(n-1)
if nn
if 22 then
then return
return 11
B A
• Definition of even:
0 is even (let)
N is odd if N-1 is even
N is even if N-1 is odd
= 2(2T(n/4) + n/2)
n/2 + n expand So upper bound for k is
= 22T(n/4) + 2n substitute n = 2k
= 22(2T(n/8) + n/4)
n/4 + 2n expand K = log n = lg n
2
= 23T(n/8) + 3n observe pattern
= 23T(n/23) + 3n observe pattern
T( n ) = 2kT(n/2k) + kn
= n T(n/n) + n lg n T(n) = O(nlgn)
= n + n lg n
Mashiour Rahman AIUB::CSC2105::Algorithm Recurrences 16
Repeated Substitution Method
• The procedure is straightforward:
Substitute, Expand, Substitute, Expand, …
Observe a pattern and determine the expression after
the k-th substitution.
Find out what the highest value of k (number of iterations,
e.g., lg n) should be to get to the base case of the
recurrence (e.g., T(1)).
Insert the value of T(1) and the expression of k into your
expression.
Mashiour Rahman AIUB::CSC2105::Algorithm Recurrences 17
2 if n 1
T ( n)
2T (n / 2) 2n 3 if n 1
T(n) = 4T(n/2) + n
4c(n/2)2 + n
= cn2 + n
cn2
Mashiour Rahman AIUB::CSC2105::Algorithm Recurrences 23
...Substitution Method
• What is the problem? Rewriting
T(n) = O(n2) = cn2 + (something positive)
T(n) = 4T(n/2) + n
4(c(n/2)2 – d(n/2)) + n
= cn2 – 2dn + n
= (cn2 – dn) – (dn – n)
cn2 – dn if d
Mashiour Rahman AIUB::CSC2105::Algorithm Recurrences 25
Recursion Tree
• A recursion tree is a convenient way to visualize what
happens when a recurrence is iterated.
Good for ”guessing” asymtotic solutions to recurrences
T (n) T (n / 4) T (n / 2) n 2
alogb n
alogb n
Split problem into a parts. There are alogb n
logbn levels. There are a logb n nlogb a leaves.
Mashiour Rahman AIUB::CSC2105::Algorithm Recurrences 29
…Master Method
• Iterating the recurrence (expanding the tree) yields
T(n) = f(n) + aT(n/b)
= f(n) + af(n/b) + a2T(n/b2)
= f(n) + af(n/b) + a2f(n/b2) + …
alogbn-1f(n/blogbn-1) + alogbnT(1)
logb n 1
T(n) =
j j logb a
a f(n / b ) (n )
j 0
Inverse of Case 1
logb a
f(n) grows polynomially faster than n
Also need a “regularity” condition
c 1 and n0 0 such that af (n / b) cf (n) n n0
• The work at the root dominates
division/recombination cost
BinarySearch(A,
BinarySearch(A, l,l, r,
r, q):
q):
mm :=
:= (l+r)/2
(l+r)/2
if
if A[m]=q
A[m]=q then
then return
return mm
else
else ifif A[m]>q
A[m]>q then
then
BinarySearch(A,
BinarySearch(A, l, l, m-1,
m-1, q)
q)
else
else BinarySearch(A,
BinarySearch(A, m+1,
m+1, r,
r, q)
q)
T(n) = T(n/2) + 1
1. a=1, b=2, f(n) = 1
2. nlog21 = 1
3. 1 = (1)
Case 2: T(n) = (log n)
T(n) = 9T(n/3) + n
1. a=9, b=3, f(n) = n
2. nlog39 = n2
3. n = (nlog39 - ) with = 1
Case 1: T(n) = (n2)
2. nlog43 = n0.792
Case 3: