Data Structures and Algorithms: (CS210/ESO207/ESO211)
Data Structures and Algorithms: (CS210/ESO207/ESO211)
(CS210/ESO207/ESO211)
Lecture 15
Methods for solving the recurrences which occur
frequently in the analysis of algorithms.
1
Commonly occurring recurrences
T(n) = T(n/2) + cn
T(n) = T(7n/10) + T(n/5) + cn
T(n) = 4T(n/2) + cn
3
T(n) = 3T(n/2) + cn
1.5
T(n) = T( n) + cn
2
Methods for solving
Common Recurrences in algorithm analysis
3
Methods for solving common Recurrences
Unfolding the recurrence.
Guessing the solution and then proving by induction.
A General solution for a large class of recurrences (Master
theorem)
4
Solving a recurrence by unfolding
Let T(1) = 1,
T(n) = cn + 4 T(n/2) for n>1, where c is some positive constant
Solving the recurrence for T(n) by unfolding (expanding)
T(n) = cn + 4 T(n/2)
= cn + 2cn + 4
2
T(n/2
2
)
= cn + 2cn + 4cn + 4
3
T(n/2
3
)
= cn + 2cn + 4cn + 8cn + + 4
log
2
= cn + 2cn + 4cn + 8cn + +
2
= O(
2
)
5
A geometric increasing series with log n terms and common ratio 2
Solving a recurrence by
guessing and then proving by induction
T(1) =
1
T() = 2T(/2) +
2
Guess: T() log + for some constants and .
Proof by induction:
Base case: holds true if
1
Induction hypothesis: T() log + for all <
To prove: T() log +
Proof: T() = 2T(/2) +
2
2(
2
log
2
+ ) +
2
// by induction hypothesis
= log - + 2 +
2
= log + + ( +
2
- )
log + if +
2
6
Hence T() (
1
+
2
) log +
1
for all value of .
So T() = ( log )
These inequalities can
be satisfied
simultaneously by
selecting
=
1
and =
1
+
2
It looks similar/identical to
the recurrence of merge sort.
So we guess
T(n) = O(n log n)
Solving a recurrence by
guessing and then proving by induction
Key points:
You have to make a right guess (past experience may help)
What if your guess is too loose ?
Be careful in the proof by induction
To realize the last point, find the error in the following reasoning.
For the recurrence T(1) =
1
, and T() = 2T(/2) +
2
,
one guesses T() = O() and provides the following (wrong)proof by
induction.
Induction hypothesis: T() + for all <
T() = 2T(/2) +
2
2(
2
+ ) +
2
// by induction hypothesis
= +
2
+ 2
= O()
7
A General Method for solving a large class of
Recurrences
8
Solving a large class of recurrences
T(1) = 1,
T(n) = f(n) + a T(n/b)
Where
a and b are constants and b >1
f(n) is a multiplicative function:
f(xy) = f(x)f(y)
Example of a multiplicative function : f(n) =
AIM : To solve T(n) for n =
9
Solving a slightly general class of recurrences
T(n) = f(n) + T(n/)
= f(n) + f(n/) +
2
T(n/
2
)
= f(n) + f(n/) +
2
f(n/
2
) +
3
T(n/
3
)
=
= f(n) + f(n/) + +
f(n/
) + +
1
f(n/
1
) +
T(1)
= (
f(n/
)
1
=0
) +
after rearranging
=
f(n/
)
1
=0
continued to the next page
10
T(n) =
f(n/
)
1
=0
(since f is multiplicative)
=
f(n)/f(
)
1
=0
=
f(n)/(f())
1
=0
=
+ f(n)
/(f())
1
=0
=
+ f(n) (/f())
1
=0
=
+ (f())
(/f())
1
=0
Case 1: = f(), T(n) = ??
Case 2: < f(), T(n) = ??
11
A geometric series
( + 1)
+(f())
O(1)
= O(
log
log
)
= O(f(n))
For < f(), the sum of this
series is bounded by
f()
= O(1)
= O((f())
)
Three cases
T(n) =
+ (f())
(/f())
1
=0
Case 1: = f(), T(n) =
log
log
Case 2: < f(), T(n) = O(f(n))
Case 3: > f(), T(n) = ??
12
+ O(
) = O(
log
)
For > f(), the sum of this
series is equal to
f()
f()
Master theorem
T(1) = 1,
T(n) = f(n) + a T(n/b) where f is multiplicative.
There are the following solutions
Case 1: = f(), T(n) =
log
log
Case 2: < f(), T(n) = O(f(n))
Case 3: > f(), T(n) = O(
log
)
13
Examples
14
Master theorem
If T(1) = 1, and T(n) = f(n) + a T(n/b) where f is multiplicative,
then there are the following solutions
Case 1: = f(), T(n) =
log
log
Case 2: < f(), T(n) = O(f(n))
Case 3: > f(), T(n) = O(
log
)
Example 1: T()= + 4 T(/2)
Solution: T()= ??
15
O(
2
)
This is case 3
Master theorem
If T(1) = 1, and T(n) = f(n) + a T(n/b) where f is multiplicative,
then there are the following solutions
Case 1: = f(), T(n) =
log
log
Case 2: < f(), T(n) = O(f(n))
Case 3: > f(), T(n) = O(
log
)
Example 2: T()=
2
+ 4 T(/2)
Solution: T()= ??
16
O(
2
log
4
)
This is case 1
Master theorem
If T(1) = 1, and T(n) = f(n) + a T(n/b) where f is multiplicative,
then there are the following solutions
Case 1: = f(), T(n) =
log
log
Case 2: < f(), T(n) = O(f(n))
Case 3: > f(), T(n) = O(
log
)
Example 3: T()=
3
+ 4 T(/2)
Solution: T()= ??
17
O(
3
)
This is case 2
Master theorem
If T(1) = 1, and T(n) = f(n) + a T(n/b) where f is multiplicative,
then there are the following solutions
Case 1: = f(), T(n) =
log
log
Case 2: < f(), T(n) = O(f(n))
Case 3: > f(), T(n) = O(
log
)
Example 4: T()= 2
1.5
+ 3 T(/2)
Solution: T()= ??
18
We can not apply master theorem directly since f(n) = 2
1.5
is not
multiplicative. But if we define G(n)= T(n)/2, then we get
G(n) =
1.5
+ 3 T(/2). Now we can apply master theorem (case 3)
to get G(n) = O(
log
2
3
) = O(
1.58
). Hence T(n) = O(
1.58
).
Master theorem
If T(1) = 1, and T(n) = f(n) + a T(n/b) where f is multiplicative,
then there are the following solutions
Case 1: = f(), T(n) =
log
log
Case 2: < f(), T(n) = O(f(n))
Case 3: > f(), T(n) = O(
log
)
Example 6: T() = T( ) + c
Solution: T()= ??
19
We can not apply master theorem directly since T( ) <>
T(/) for any constant .
Solving T() = T( ) + c using the method of
unfolding
T() = c + T( )
= c + c + T(
4
)
= c + c + c
4
+ T(
8
)
= c + c + c
+ + T(1)
= O()
20
A series which is decreasing at a rate
faster than any geometric series
Can you guess the
number of terms in
this series ?
log log
Master theorem
If T(1) = 1, and T(n) = f(n) + a T(n/b) where f is multiplicative,
then there are the following solutions
Case 1: = f(), T(n) =
log
log
Case 2: < f(), T(n) = O(f(n))
Case 3: > f(), T(n) = O(
log
)
Example 5: T()= (log )
2
+ 2 T(/2)
Solution: T()= ??
21
We can not apply master theorem since
f(n) = (log )
2
is not multiplicative.
Using the method of unfolding, it can
be shown that T(n) = O( (log )
3
).
Homework
Solve the following recurrences systematically (if possible by various
methods). Assume that T(1) =1 for all these recurrences.
T() = 1 + 2 T(/2)
T() =
3
+ 2 T(/2)
T() =
2
+ 7 T(/3)
T() = / log + 2T(/2)
T() = 1 + T(/5)
T() = + 2 T(/4)
T() = 1 + T( )
T() = + T(9 /10)
T() = log + T(/4)
22