Data Structures and Algorithms: Recurrences
Data Structures and Algorithms: Recurrences
Recurrences
1
1. Recursion Tree Method
• Extensively applied in Divide and Conquer type of
algorithms
– Divide the problem into a number of sub problems
– Conquer the sub-problems by solving them recursively
– Combine the solutions of the sub problems into the
solution for the original problem
2
Recursion Tree Method
The cn2 term at the root represents the cost at the top level
of recursion
The three sub trees represent the costs incurred by the sub
problem of size n/4 5
Example 1: Recursion Tree for
T(n) = 3T(n/4) + cn2
6
Example 1:
T(n) = 3T(n/4) + cn2
T(n)
At leveli ,
No. of Nodes = 3i
Input size = n/4i , Cost = c (n/4i)2
Cost at leveli = 3i x c (n/4i)2
= (3/16)i cn2
11
Copyright © The McGraw-Hill Companies, Inc. 12
Example 1: Recursion Tree for
T(n) = 3T(n/4) + cn 2
Overall Complexity
For i = 0, 1, 2, …, log4n – 1, log4n
T(n) = cn2 + 3/16 cn2 + (3/16)2 cn2 +…….. +
(3/16)log4n-1 cn2 + O (nlog43)
log 4 n -1 i
3 2
= ÷cn
16 + O (nlog 3)
4 Eq. 1
i =0
13
Example 1: Recursion Tree for
T(n) = 3T(n/4) + cn2
14
But
15
Recursion Tree Example 2
T(n) = 1
T(n) = T(n/3) + T(2n/3) + n
n=1
n>1
How many levels?
?
Cost at log3/2 n
T(?)
this level
n n But, not all branches
have same depth!
n/3 2n/3 n
Makes cost near the
n/9 2n/9 2n/9 4n/9 n leaves hard to
calculate.
…
…
…
…
Estimate!
16
Recursion Tree Example 2
T(n) = 1 n=1
T(n) = T(n/3) + T(2n/3) + n n>1
Cost at
T(?)
this level
n n
Overestimate.
n/3 2n/3 n Consider all branches to
be of max depth.
n/9 2n/9 2n/9 4n/9 n
…
T(n) n (log3/2 n) + n
…
…
…
1 1 n
… T(n) = O(n log n)
#levels = log3/2 n
17
Recursion Tree Example 2
Finding depth
Input size at level0 = n
Input size at level1 = 2n/3 = 21n/31
Input size at level2 = 4n/9 = 22n/32 =(2/3)2 n
………
Input size at leveli = (2/3)i n
Input size at last level = 1
for last level,
(2/3) I n= 1 or n=(3/2)i or i = log3/2n
19
Example 3 of recursion tree
Solve T(n) = T(n/4) + T(n/2) + n2:
T(n)
20
Example 3 of recursion tree
Solve T(n) = T(n/4) + T(n/2) + n2:
n2
T(n/4) T(n/2)
21
Example 3 of recursion tree
Solve T(n) = T(n/4) + T(n/2) + n2:
n2
(n/4)2 (n/2)2
22
Example 3 of recursion tree
Solve T(n) = T(n/4) + T(n/2) + n2:
n2
(n/4)2 (n/2)2
T(1)
23
Example 3 of recursion tree
Solve T(n) = T(n/4) + T(n/2) + n2:
n2 n2
5 n2
(n/4)2 (n/2)2
16
(n/16)2 (n/8)2 (n/8)2 (n/4)2
O(1)
24
Example 3 of recursion tree
Solve T(n) = T(n/4) + T(n/2) + n2:
n2 n2
5 n2
(n/4)2 (n/2)2
16
25 n 2
(n/16)2 (n/8)2 (n/8)2 (n/4)2
256
…
O(1)
25
Example 3 of recursion tree
Solve T(n) = T(n/4) + T(n/2) + n2:
n2 n2
5 n2
(n/4)2 (n/2)2
16
25 n 2
(n/16)2 (n/8)2 (n/8)2 (n/4)2
256
…
O(1) … … …
Leaf = nlogba < nlog22 < n
26
Example 3 of recursion tree
• # of nodes at each level i = (5/16)i
• Cost at each level i = (5/16)i n2
• Height of Tree = log2n (reason: Since we have
an uneven split at each level in term of n/4
and n/2. It will result in an unbalanced tree
with more depth for n/2 split.
• Cost at leaf level =nlog b a O(1) < nlog 2 2 O(1)
Total = n
2
5
1 16 16
5 2 5 3
16
Geometric series
= O(n2) 27
Formulas: Geometric series
n 1
1 - x
1 x x2 xn = for x 1
1- x
1
1 x x =
2
for |x| < 1
1- x
28