Recursive Function Analysis
Recursive Function Analysis
CS245-2016S-03
Recursive Function Analysis
David Galles
2
n
! n2 (n2 + 1)
i =
i=1
2
n4 + n2
=
2
(n4 )
03-3: Recursive Functions
long power(long x, long n) {
if (n == 0)
return 1;
else
return x * power(x, n-1);
}
Ans: theta(n)
03-4: Recurrence Relations
T (n) = Time required to solve a problem of size n
Recurrence relations are used to determine the
running time of recursive programs recurrence
relations themselves are recursive
T (0) = time to solve problem of size 0
Base Case
T (n) = time to solve problem of size n
Recursive Case
03-5: Recurrence Relations
long power(long x, long n) {
if (n == 0)
return 1;
else
return x * power(x, n-1);
}
T (0) = c1 for some constant c1
T (n) = c2 + T (n 1) for some constant c2
03-6: Solving Recurrence Relations
T (0) = c1
T (n) = T (n 1) + c2
If we knew T (n 1), we could solve T (n).
T (n) = T (n 1) + c2
03-7: Solving Recurrence Relations
T (0) = c1
T (n) = T (n 1) + c2
If we knew T (n 1), we could solve T (n).
T (n) = T (n 1) + c2 T (n 1) = T (n 2) + c2
= T (n 2) + c2 + c2
= T (n 2) + 2c2
03-8: Solving Recurrence Relations
T (0) = c1
T (n) = T (n 1) + c2
If we knew T (n 1), we could solve T (n).
T (n) = T (n 1) + c2 T (n 1) = T (n 2) + c2
= T (n 2) + c2 + c2
= T (n 2) + 2c2 T (n 2) = T (n 3) + c2
= T (n 3) + c2 + 2c2
= T (n 3) + 3c2
03-9: Solving Recurrence Relations
T (0) = c1
T (n) = T (n 1) + c2
If we knew T (n 1), we could solve T (n).
T (n) = T (n 1) + c2 T (n 1) = T (n 2) + c2
= T (n 2) + c2 + c2
= T (n 2) + 2c2 T (n 2) = T (n 3) + c2
= T (n 3) + c2 + 2c2
= T (n 3) + 3c2 T (n 3) = T (n 4) + c2
= T (n 4) + 4c2
03-10: Solving Recurrence Relations
T (0) = c1
T (n) = T (n 1) + c2
If we knew T (n 1), we could solve T (n).
T (n) = T (n 1) + c2 T (n 1) = T (n 2) + c2
= T (n 2) + c2 + c2
= T (n 2) + 2c2 T (n 2) = T (n 3) + c2
= T (n 3) + c2 + 2c2
= T (n 3) + 3c2 T (n 3) = T (n 4) + c2
= T (n 4) + 4c2
= ...
= T (n k) + kc2
03-11: Solving Recurrence Relations
T (0) = c1
T (n) = T (n k) + k c2 for all k
If we set k = n, we have:
T (n) = T (n n) + nc2
= T (0) + nc2
= c1 + nc2
(n)
03-12: Building a Better Power
long power(long x, long n) {
if (n==0) return 1;
if (n==1) return x;
if ((n % 2) == 0)
return power(x*x, n/2);
else
return power(x*x, n/2) * x;
}
theta(log(n))
03-13: Building a Better Power
long power(long x, long n) {
if (n==0) return 1;
if (n==1) return x;
if ((n % 2) == 0)
return power(x*x, n/2);
else
return power(x*x, n/2) * x;
}
T (0) = c1
T (1) = c2
T (n) = T (n/2) + c3
(Assume n is a power of 2)
03-14: Solving Recurrence Relations
T (n) = T (n/2) + c3
03-15: Solving Recurrence Relations
T (0) = c1
T (1) = c2
T (n) = T (n/2) + c3
T (n) = T (n/2k ) + kc3
We want to get rid of T (n/2k ). Since we know T (1) ...
n/2k = 1
n = 2k
lg n = k
03-21: Solving Recurrence Relations
T (1) = c2
T (n) = T (n/2k ) + kc3
Set k = lg n:
lg n
T (n) = T (n/2 ) + (lg n)c3
= T (n/n) + c3 lg n
= T (1) + c3 lg n
= c2 + c3 lg n
(lg n)
03-22: Power Modifications
long power(long x, long n) {
if (n==0) return 1;
if (n==1) return x;
if ((n % 2) == 0)
return power(x*x, n/2);
else
return power(x*x, n/2) * x;
}
03-23: Power Modifications
long power(long x, long n) {
if (n==0) return 1; < stuck at this loop, always hit (n%2) == 0,
if (n==1) return x; wont reach the base case
the inner power(x,2) part
if ((n % 2) == 0)
return power(power(x,2), n/2);
else
return power(power(x,2), n/2) * x;
}
This version of power will not work. Why?
03-24: Power Modifications
long power(long x, long n) {
if (n==0) return 1; < because it stuck at this loop too, wont
if (n==1) return x; hit the base case, keep calling the
recursive function,
if ((n % 2) == 0) the inner power(x,n/2), 2) < this part
T (0) = c1
T (1) = c2
T (n) = 2k T (n/2k ) + (2k 1)c3
Pick a value for k such that n/2k = 1:
n/2k = 1
n = 2k
lg n = k
T (n) = 2lg n T (n/2lg n ) + (2lg n 1)c3
= nT (n/n) + (n 1)c3
= nT (1) + (n 1)c3
= nc2 + (n 1)c3
(n)
03-29: Recursion Trees
We can also do this substitution visually, leads to
Recursion Trees
Consider:
T (n) = 2T (n/2) + Cn
T (1) = C2
T (0) = C2
03-30: Recursion Trees
Start with the recursive definition
T(n) = Cn + 2T(n/2)
03-31: Recursion Trees
Move the equation around a bit to get:
T(n) = Cn
+ +
T(n/2) T(n/2)
C(n/2) C(n/2)
+ + + +
C(n/2) C(n/2)
+ + + +
C(n/2) C(n/2) Cn
lg n
...
...
...
...
C2 C2 C2 C2 C2 C2 C2 C2 C2 C2 C2 C2 C2 ... C2 C2 C2 C2 C2 C2 C2 C2 n
Cn( lg n-1) + C 2n
lg n
2 = n leaves (n lg n )
03-35: Recursion Trees
T (1) = C1
T (n) = T (n 1) + C2
03-36: Recursion Trees
T (0) = C1
T (1) = C1
T (n) = T (n/2) + C2
03-37: Substitution Method
We can prove that a bound is correct using
induction, this is the substituion method
T (1) = C1
T (n) = T (n 1) + C2
Show: T (n) O( ? )
03-38: Substitution Method
We can prove that a bound is correct using
induction, this is the substituion method
T (1) = C1
T (n) = T (n 1) + C2
Recursive case:
Recursive case:
Recursive case:
T (1) = C1
T (n) = T (n 1) + C2
Recursive case:
Recursive case:
Recursive case:
T (0) = C2
T (1) = C2
T (n) = 2T (n/2) + C1 n
Base cases:
T (0) = C1 C 0 lg 0 for some constant C
T (1) = C1 C 1 lg 1 for some constant C
Hmmm....
03-50: Substitution Method
T (0) = C2
T (1) = C2
T (n) = 2T (n/2) + C1 n
Show: T (n) O(n lg n), that is, T (n) C n lg n
T (1) = 1
"n#
T (n) = 2T +1
2
(Work on board)
03-56: Substitution Method
Try T (n) cn:
"n#
T (n) = 2T 1
" n2#
2c +1
2
cn + 1
<= cn ? (so we know it is invalid proof to assume T(n) <= cn
"n#
T (n) = 2T +1
" "2n # #
2 c b +1
2
cn 2b + 1
cn b
As long as b 1
03-58: Master Method
Recursion Tree for: T (n) = 2T (n/4) + C2
03-59: Master Method
Totals for each
level in the tree:
T(n) = C C
2 2
log4 n levels in the tree
C C 2C
2 2 2
C C C C 4C
2 2 2 2 2
C C C C C C C C 8C
2 2 2 2 2 2 2 2 2
...
k
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
2 C
2
...
... + 2
log 4 n
C
C C C C C C C C C C C C 2
1 1 1 1 1 1 1 1 1 1 1 1
( n 1/2 )
log 4 n log 4 2 1/2
2 = n = n leaves in the tree
03-60: Master Method
Recursion Tree for: T (n) = 2T (n/2) + n2
2 2 n2
log2 n levels in the tree
(n/2) (n/2)
2
+ + + +
n2
2 2 2 2
(n/4) (n/4) (n/4) (n/4) 4
+ + + + + +
+ +
2 2 2 2 2 2 2 2 n2
(n/8) (n/8) (n/8) (n/8) (n/8) (n/8) (n/8) (n/8)
8
...
n2
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
k
2
...
C1 C1 C1 C1 ... C1 C1 C1 C1
+ 2
log 4 n
C
1
T(n) = n2
n2
(n/4) 2 (n/4) 2 (n/4) 2 (n/4) 2 (n/4) 2 (n/4) 2 (n/4) 2 (n/4) 2 (n/4) 2 (n/4) 2 (n/4) 2 (n/4) 2 (n/4) 2 (n/4) 2 (n/4) 2 (n/4) 2 n2
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
n2
...
1 1 1 1 1 1 1 1 + n2
log 2 n log 2 4
n 2 lg n
2
4 = n = n leaves in the tree
03-64: Master Method
Recursion Tree for: T (n) = aT (n/b) + f (n)
03-65: Master Method
Totals for each
level in the tree:
logb n levels in the tree
f(n)
# of children = a f(n)
f (n/b)
... ... f (n/b) ... ... f (n/b) # of chldren = a a f (n/b)
2
f (n/b 2 ) f (n/b 2 ) f (n/b ) f (n/b 2 ) f (n/b 2 ) f (n/b 2 ) f (n/b 2 ) f (n/b 2 ) f (n/b 2 ) 2 2
# of chldren = a a f (n/b )
...
k k
...
...
...
...
...
...
...
...
...
a f (n/b )
...
C C C C ... C C C C
C n
log b a
log b n log b a
a = n leaves in the tree
03-66: Master Method
T (n) = aT (n/b) + f (n)
a = 9, b = 3, f (n) = n
nlogb a = nlog3 9 = n2
n O(n2 )
T (n) = (n2 )
03-69: Master Method
T (n) = T (2n/3) + 1
03-70: Master Method
T (n) = T (2n/3) + 1 < f(n) = 1
Note, b = 3/2
a = 1, b = 3/2, f (n) = 1
n^log(3/2) 1 =
T (n) = (1 lg n) = (lg n)
03-71: Master Method
T (n) = 3T (n/4) + n lg n
03-72: Master Method
T (n) = 3T (n/4) + n lg n
a = 3, b = 4, f (n) = n lg n
nlogb a = nlog4 3 = n0.792
n lg n (n0.792+ )
3(n/4) lg(n/4) c n lg n
T (n) (n lg n)
03-73: Master Method
T (n) = 2T (n/2) + n lg n
03-74: Master Method
T (n) = 2T (n/2) + n lg n
a = 2, b = 2, f (n) = n lg n
nlogb a = nlog2 2 = n1
Master method does not apply!
n1+ grows faster than n lg n for any > 0
Logs grow incredibly slowly! lg n o(n ) for any > 0