0% found this document useful (0 votes)
34 views28 pages

Data Structures and Algorithms: Recurrences

Uploaded by

Hello
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views28 pages

Data Structures and Algorithms: Recurrences

Uploaded by

Hello
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 28

Data Structures and Algorithms

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

• We will use this method in a number of future


lectures while doing complexity analysis of divide
and conquer algorithms (e.g. Merge Sort)

2
Recursion Tree Method

• Convert the recurrence into a tree


– Each node represents the cost of a single sub
problem

– Sum the costs within each level of the tree to


obtain a set of per-level costs

– Then sum up the costs of all levels to get total cost

• A difficult way of looking at iteration method ? 3


Example 1: Recursion Tree for
T(n) = 3T(n/4) + cn2
T(n)

T(n/4) T(n/4) T(n/4)


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 4
Example 1: Recursion Tree for
T(n) = 3T(n/4) + cn2
T(n)

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)

c(n/4)2 c(n/4)2 c(n/4)2

c(n/16)2 c(n/16)2 c(n/16)2 c(n/16)2 c(n/16)2 c(n/16)2 c(n/16)2 c(n/16)2 c(n/16)2

T(1) T(1) T(1) T(1) T(1) T(1) T(1) T(1) T(1)

Boundry Condition i.e. T(1) = O(1) or


Constant complexity of computing nodes at last level (leaf nodes) 7
Example 1: Recursion Tree for
T(n) = 3T(n/4) + cn2
• Complexity of tree is
sum of costs of all
levels
• To find the total cost
– Number of levels
(depth)
– Cost at each level
• Number of Nodes at each
level * Cost of one node
– Evaluate
8
Example 1: Recursion Tree for
T(n) = 3T(n/4) + cn2
Finding number of levels
Input size at level0 = n
Input size at level1 = n/4 = n/41
Input size at level2 = n/16 = n/42
………
Input size at leveli = n/4i
Input size at last level = 1
for last level,
n/4i = 1 or n=4i or i = log4n

Hence the tree has log 4n + 1 levels


(0, 1, 2, 3… log4n)
9
Example 1: Recursion Tree for
T(n) = 3T(n/4) + cn2
Finding Number of Nodes at each
Level
No. of Nodes at level0 = 1
No. of Nodes at level1 = 3 = 31
No. of Nodes at level2 = 9 = 32
………
No. of Nodes at leveli = 3i

No. of leaf Nodes ?


= 3log4n = nlog43 (Page 54)
Each contributing to the cost T(1)
and total cost will be nlog43 T(1) i.e. O (nlog43)
10
Example 1: Recursion Tree for
T(n) = 3T(n/4) + cn2
Finding Cost at Each level
n reduces by a factor of 4 at each
level while nodes increase by a factor
of 3.

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

Hence the tree has log 3/2n + 1 levels


(0, 1, 2, 3… log3/2n) 18
Example 3 of recursion tree
Solve T(n) = T(n/4) + T(n/2) + n2:

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

T(n/16) T(n/8) T(n/8) T(n/4)

22
Example 3 of recursion tree
Solve T(n) = T(n/4) + T(n/2) + n2:
n2
(n/4)2 (n/2)2

(n/16)2 (n/8)2 (n/8)2 (n/4)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

You might also like