Topic 1 Recurrences Short
Topic 1 Recurrences Short
Algorithms:
COMP3121/3821/9101/9801
Aleks Ignjatovic
TOPIC 1: RECURRENCES
COMP3121/3821/9101/9801 1 / 20
Asymptotic notation
f (n) = O(g(n)) means that f (n) does not grow substantially faster
than g(n) because a multiple of g(n) eventually dominates f (n).
COMP3121/3821/9101/9801 2 / 20
Asymptotic notation
Omega notation: f (n) = (g(n)) is an abbreviation for:
COMP3121/3821/9101/9801 5 / 20
n
T (n) = a T + f (n)
b
size of instance = n
a many size of instances = n/b
instances
size of instances = n/b2
. depth of
. recursion:
. log !
. .
. . . .
. .
. . .
. .
. .
size of instances = 1
COMP3121/3821/9101/9801 6 / 20
Some recurrences can be solved explicitly, but this tends to be
tricky.
COMP3121/3821/9101/9801 7 / 20
Master Theorem:
Let:
a 1 and b > 1 be integers;
f (n) > 0 be a monotonically increasing function;
T (n) be the solution of the recurrence T (n) = a T (n/b) + f (n);
Then:
1 If f (n) = O(nlogb a ) for some > 0, then T (n) = (nlogb a );
2 If f (n) = (nlogb a ), then T (n) = (nlogb a log2 n);
3 If f (n) = (nlogb a+ ) for some > 0, and for some c < 1,
a f (n/b) c f (n)
COMP3121/3821/9101/9801 8 / 20
Master Theorem - a remark
Note that for any b > 1,
logb n = logb 2 log2 n;
Since b > 1 is constant (does not depend on n), we have for
c = logb 2 > 0
logb n = c log2 n;
1
log2 n = logb n;
c
Thus,
logb n = (log2 n)
and also
log2 n = (logb n).
So whenever we have f = (g(n) log n) we do not have to specify
what base the log is - all bases produce equivalent asymptotic
estimates.
COMP3121/3821/9101/9801 9 / 20
Master Theorem - Examples
Let T (n) = 4 T (n/2) + n;
and so on . . ., we get
(1)
z }|
{
n n n
T (n) = a T +f (n) = a a T 2
+f + f (n)
b b b
| {z } | {z }
(2) (2)
2 n n
2 n n n
=a T 2
+a f b + f (n) = a aT +f + af b + f (n)
b b3 b2
| {z } | {z }
(3) (3)
3 n
+a2 f bn2 + a f n
=a T 3 b + f (n) = . . .
b
| {z }
COMP3121/3821/9101/9801 12 / 20
Master Theorem Proof:
Continuing in this way logb n 1 many times we get ...
n n n
T (n) = a3 T 3 +a2 f 2 + a f + f (n) =
| {zb } b b
= ...
n n
= ablogb nc T blog nc + ablogb nc1 f blog nc1 + . . .
b b n b n
b
3 n 2
+ a f 3 + a f 2 + af + f (n)
b b b
blogb nc1
logb n
n X n
a T + ai f
blogb n i=0
bi
COMP3121/3821/9101/9801 14 / 20
Master Theorem Proof:
Case 1 - continued:
blogb nc1
!
blogb nc
X n
logb a (b ) 1
ai f i = O n
i=0
b b 1
bblogb nc 1
logb a
= O n
b 1
n 1
= O nlogb a
b 1
log a
n b nlogb a
=O
b 1
= O nlogb a
blogb nc1 n
X
Since we had: T (n) nlogb a T (1) + ai f we get:
i=0
bi
T (n) nlogb a T (1) + O nlogb a
= nlogb a
COMP3121/3821/9101/9801 15 / 20
Master Theorem Proof:
Case 2: f (m) = (mlogb a )
blogb nc1 n blogb nc1 n logb a
X X
ai f = ai
i=0
bi i=0
bi
blogb nc1 n logb a
X i
= a
i=0
bi
blogb nc1 i
X a
= nlogb a
i=0
(bi )logb a
blogb nc1
X a i
= nlogb a
i=0
blogb a
blogb nc1
X
= nlogb a 1
i=0
= nlogb a blogb nc
COMP3121/3821/9101/9801 16 / 20
Master Theorem Proof:
Case 2 (continued):
Thus,
we get:
T (n) nlogb a T (1) + nlogb a log2 n
= nlogb a log2 n
COMP3121/3821/9101/9801 17 / 20
Master Theorem Proof:
Case 3: f (m) = (mlogb a+ ) and a f (n/b) c f (n) for some 0 < c < 1.
c
We get by substitution: f (n/b) f (n)
a
c
f (n/b2 ) f (n/b)
a
c
f (n/b3 ) f (n/b2 )
a
...
c
f (n/bi ) f (n/bi1 )
a
By chaining these inequalities we get
c c c c2
f (n/b2 ) f (n/b) f (n) = 2 f (n)
a | {z } a |a {z } a
c c c2 c3
f (n/b3 ) f (n/b2 ) 2 f (n) = 3 f (n)
a | {z } a |a {z } a
...
c c ci1 ci
f (n/bi ) f (n/bi1 ) i1 f (n) = i f (n)
a | {z } a |a {z } a
COMP3121/3821/9101/9801 18 / 20
Master Theorem Proof:
Case 3 (continued):
ci
We got f (n/bi ) f (n)
ai
Thus,
blogb nc1 blogb nc1
X n X ci X f (n)
ai f ai f (n) < f (n) ci =
i=0
bi i=0
ai i=0
1 c
f (n) = (nlogb a+ )
Note: we have seen that the Master Theorem does NOT apply, but the technique
used in its proof still works! Just unwind the recurrence and sum up the logarithmic
overheads.
COMP3121/3821/9101/9801 20 / 20