Lecture 12 13
Lecture 12 13
Recurrence Equation
(Solving Recurrence using
Recursion Tree Methods)
Lecture – 12 and 13
Overview
• A recurrence is a function is defined in terms of
– one or more base cases, and
– itself, with smaller arguments.
Examples:
𝑛
Solve the recurrence 𝑇 𝑛 = 3𝑇 + Θ 𝑛2 by
4
using recursion tree method.
Recursion Tree Method (Example 1)
Answer:
We start by focusing on finding an upper bound for the
solution by using good guess. As we know that floors
and ceilings usually do not matter when solving the
recurrences, we drop the floor and write the recurrence
equation as follows:
𝑛
𝑇 𝑛 = 3𝑇 + 𝑐𝑛2 , 𝑐 > 0
4
The term 𝑐𝑛2 , at the root represent the costs incurred by
𝑛
the subproblems of size .
4
Recursion Tree Method (Example 1)
𝑛
𝑇 𝑛 = 3𝑇 + 𝑐𝑛2 , 𝑐 > 0 𝑖𝑠 𝑎 𝑐𝑜𝑛𝑠𝑡𝑎𝑛𝑡
4
𝑇(𝑛)
Fig –(a)
Figure (a) shows T(n), which progressively expands in (b) and (d) to form recursion
tree.
𝑐 𝑛 2
𝑛 𝑛 𝑛
𝑇 𝑇 𝑇
4 4 4
Fig –(b)
L1.10
Recursion Tree Method (Example 1)
𝑛
𝑇 𝑛 = 3𝑇 + 𝑐𝑛2 , 𝑐 > 0 𝑖𝑠 𝑎 𝑐𝑜𝑛𝑠𝑡𝑎𝑛𝑡
4
𝑐 𝑛 2
𝑛 𝑛 𝑛 𝑛 𝑛 𝑛 𝑛 𝑛 𝑛
𝑇 𝑇 𝑇 𝑇 𝑇 𝑇 𝑇 𝑇 𝑇
16 16 16 16 16 16 16 16 16
Fig –(c)
L1.11
Recursion Tree Method (Example 1)
𝑛
𝑇 𝑛 = 3𝑇 + 𝑐𝑛2 , 𝑐 > 0 𝑖𝑠 𝑎 𝑐𝑜𝑛𝑠𝑡𝑎𝑛𝑡
4
2 2
𝑐 𝑛 𝑐 𝑛
3 2
𝑐(𝑛/4)2 𝑐(𝑛/4)2 𝑐(𝑛/4)2 𝑐 𝑛
16
log 4 𝑛
2
𝑛 2
𝑛 2 𝑛 2 𝑛 2 𝑛 2 𝑛 2
𝑛 2 𝑛 2 𝑛 2 3 2
𝑐
16
𝑐
16
𝑐
16
𝑐
16
𝑐
16
𝑐
16
𝑐
16
𝑐
16
𝑐
16 𝑐 𝑛
16
𝑖
3 2
𝑐 𝑛
16
𝑖
𝑇 1 𝑇 1 𝑇 1 ………………………..𝑇 1 3
Fig –(d)
L1.12
Recursion Tree Method (Example 1)
Analysis
First, we find the height of the recursion tree
𝑛
Observe that a node at depth ′𝑖′ reflects a subproblem of size .
4 𝑖
𝑛
i.e. the subproblem size hits 𝑛 = 1, when =1
4 𝑖
𝑛
So, if 4 𝑖
=1
⟹𝑛= 4 𝑖 𝐴𝑝𝑝𝑙𝑦 𝐿𝑜𝑔 𝑏𝑜𝑡ℎ 𝑠𝑖𝑑𝑒
⟹ log 𝑛 = log 4 𝑖
⟹ 𝑖 = log 4 𝑛
So the height of the tree is log 4 𝑛.
Recursion Tree Method (Example 1)
Second, we determine the cost of each level of the tree.
The number of nodes at depth ′𝑖′ is 3 𝑖 . It was observed that
2 3 𝑖
3 3 3 3
𝑇 𝑛 = 𝑐𝑛2 + 𝑐𝑛2 + 𝑐𝑛2 + 𝑐𝑛2 + ⋯ + 𝑐𝑛2 + (3)𝑖
16 16 16 16
𝑛 2
So, each node at depth ′𝑖 ′ 𝑖. 𝑒. 𝑖 = 0,1,2,3,4, … , log 4 𝑛 − 1 has cost 𝑐 .
4𝑖
𝑛 2
Hence the total cost at level ′𝑖′ is 3𝑖 𝑐 4𝑖
𝑖
𝑖
𝑛 2
𝑖
𝑛2 3𝑖 2
3
⟹ 3 . 𝑐. 𝑖 ⟹ 3 . 𝑐. 𝑖 ⟹ 𝑖 . 𝑐. 𝑛 ⟹ . 𝑐. 𝑛2
4 16 16 16
Recursion Tree Method (Example 1)
However, the bottom level is special. Each of the bottom node has
contribute cost = 𝑇(1)
Hence the cost of the bottom level is = 3𝑖
⟹ 3log4 𝑛 𝑎𝑠 𝑖 = log 4 𝑛 𝑡ℎ𝑒 ℎ𝑒𝑖𝑔ℎ𝑡 𝑜𝑓 𝑡ℎ𝑒 𝑡𝑟𝑒𝑒
⟹ 𝑛log4 3
So, the total cost of entire tree is
𝑇 𝑛
2 3 𝑖
3 3 3 3
= 𝑐𝑛2 + 𝑐𝑛2 + 𝑐𝑛2 + 𝑐𝑛2 + ⋯ + ⋯ + 𝑐𝑛2 + Θ(𝑛log4 3 )
16 16 16 16
log4 𝑛 𝑖
3
𝑇 𝑛 = 𝑐𝑛2 + Θ(𝑛log4 3 )
16
𝑖=0
Recursion Tree Method (Example 1)
The left term is just a sum of Geometric series. So the value of 𝑇 𝑛 is as follows.
3ൗ log4 𝑛 − 1
𝑇 𝑛 = 16 𝑐𝑛2 + Θ(𝑛log4 3 )
3
−1
16
The above equation looks very complicated. So, we use an infinite geometric series as an upper
bound. Hence the new form of the equation is given below:
log4 𝑛−1 𝑖
3
𝑇 𝑛 = 𝑐𝑛2 + Θ(𝑛log4 3 )
𝑖=0
16 𝑆𝑢𝑚 𝑜𝑓 𝑓𝑖𝑛𝑖𝑡𝑒 𝐺𝑒𝑜𝑚𝑒𝑡𝑟𝑖𝑐 𝑃𝑟𝑜𝑔𝑟𝑒𝑠𝑠𝑖𝑜𝑛
∞ 𝑛
3
𝑖 2
𝑆𝑛 = 𝑎 + 𝑎𝑟 + 𝑎𝑟 + . . . + 𝑎𝑟
𝑇 𝑛 ≤ 𝑐𝑛2 + Θ(𝑛log4 3 )
16
𝑖=0 𝑛
1 𝑟 𝑛+1 −1
2 log4 3
𝑇 𝑛 ≤ 𝑐𝑛 + Θ(𝑛 )
1−
3 𝑆𝑛 = 𝑎𝑟 𝑖 = 𝑎
16 𝑟−1
𝑇 𝑛 ≤
16 2
𝑐𝑛 + Θ(𝑛log4 3 )
𝑖=0
13
𝑇 𝑛 = Ο(𝑛2 )
Recursion Tree Method (Example 2)
Example 2
𝑛
Solve the recurrence 𝑇 𝑛 = 4𝑇 + 𝑛 by using recursion tree
2
method.
𝑛
𝑇 𝑛 = 4𝑇 + 𝑐𝑛 , 𝑐 > 0
2
The term 𝑐𝑛, at the root represent the costs incurred by the
𝑛
subproblems of size .
2
𝑇(𝑛)
Fig –(a)
𝑛 4
𝑛 𝑐 𝑐𝑛
𝑛 𝑐 2
𝑐
𝑛
2 2
𝑐
2 2
log 2 𝑛
2
4
𝑛 𝑛 𝑇
𝑛
𝑛 𝑛 𝑇
𝑛
𝑐𝑛
𝑇
4 𝑇
𝑛
𝑇
𝑛
4
𝑇
4 𝑇
𝑛
4 𝑇
𝑛
𝑇
𝑛 𝑇
𝑛
4 𝑇
𝑛
𝑛 𝑛 𝑇
𝑛
4 𝑇
4
𝑇
4
4
2
4 4 4 𝑇 4
4 𝑇
4 4 3
4
𝑐𝑛
2
𝑖
4
𝑐𝑛
2
𝑇 1 𝑇 1 𝑇 1 ………………………..𝑇 1 4 𝑖
Fig –(b)
Recursion Tree Method (Example 2)
Analysis
First, we find the height of the recursion tree
𝑛
Observe that a node at depth ′𝑖′ reflects a subproblem of size .
2 𝑖
𝑛
i.e. the subproblem size hits 𝑛 = 1, when =1
2 𝑖
𝑛
So, if =1
2 𝑖
⟹𝑛= 2 𝑖 𝐴𝑝𝑝𝑙𝑦 𝐿𝑜𝑔 𝑏𝑜𝑡ℎ 𝑠𝑖𝑑𝑒
⟹ log 𝑛 = log 2 𝑖
⟹ 𝑖 = log 2 𝑛
So the height of the tree is log 2 𝑛.
Recursion Tree Method (Example 2)
Second, we determine the cost of each level of the tree.
The number of nodes at depth ′𝑖′ is 4 𝑖 .
So, each node at depth ′𝑖 ′ 𝑖. 𝑒. 𝑖 = 0,1,2,3,4, … … , log 2 𝑛 − 1 has cost
𝑛
𝑐 2𝑖 .
𝑛
Hence the total cost at level ′𝑖′ is 4𝑖 𝑐 2𝑖
𝑖
𝑛
⟹ 4 . 𝑐. 𝑖
2
𝑖
𝑛
⟹ 4 . 𝑐. 𝑖
2
𝑖
4
⟹ 𝑖 . 𝑐𝑛
2
𝑖
4
⟹ . 𝑐𝑛
2
Recursion Tree Method (Example 2)
However, the bottom level is special. Each of the bottom node has contribute
cost = 𝑇(1)
Hence the cost of the bottom level is = 4𝑖
⟹ 4log2 𝑛 𝑎𝑠 𝑖 = log 2 𝑛 𝑡ℎ𝑒 ℎ𝑒𝑖𝑔ℎ𝑡 𝑜𝑓 𝑡ℎ𝑒 𝑡𝑟𝑒𝑒
⟹ 𝑛log2 4
𝑐𝑛 𝑛
𝑛 𝑛 𝑛
𝑐 𝑐 2 =𝑛
2 2 2
log 2 𝑛
𝑛 𝑛 𝑛 𝑛 𝑛
𝑇 𝑇 𝑇 𝑇 22 =𝑛
4 4 4 4 22
𝑛
2𝑖 =𝑛
2𝑖
𝑇 1 𝑇 1 𝑇 1 ………………………..𝑇 1
2𝑖
Fig –(b)
Recursion Tree Method (Example 3)
Analysis
First, we find the height of the recursion tree
𝑛
Observe that a node at depth ′𝑖′ reflects a subproblem of size .
2 𝑖
𝑛
i.e. the subproblem size hits 𝑛 = 1, when =1
2 𝑖
𝑛
So, if =1
2 𝑖
⟹𝑛= 2 𝑖 𝐴𝑝𝑝𝑙𝑦 𝐿𝑜𝑔 𝑏𝑜𝑡ℎ 𝑠𝑖𝑑𝑒
⟹ log 𝑛 = log 2 𝑖
⟹ 𝑖 = log 2 𝑛
So the height of the tree is log 2 𝑛.
Recursion Tree Method (Example 3)
Second, we determine the cost of each level of the tree.
The number of nodes at depth ′𝑖′ is 4 𝑖 .
𝑛
So, each node at depth ′𝑖 ′ 𝑖. 𝑒. 𝑖 = 0,1,2,3,4, … … , log 2 𝑛 − 1 has cost 𝑐 .
2𝑖
𝑛
Hence the total cost at level ′𝑖′ is 2𝑖 𝑐 2𝑖 = 𝑐𝑛
However, the bottom level is special. Each of the bottom node has contribute
cost = 𝑇(1)
Hence the cost of the bottom level is = 2𝑖
⟹ 2log2 𝑛 𝑎𝑠 𝑖 = log 2 𝑛 𝑡ℎ𝑒 ℎ𝑒𝑖𝑔ℎ𝑡 𝑜𝑓 𝑡ℎ𝑒 𝑡𝑟𝑒𝑒
⟹ 𝑛log2 2
⟹𝑛
Recursion Tree Method (Example 3)
𝑇 𝑛 = 𝑐𝑛 + 𝑐𝑛 + 𝑐𝑛 + 𝑐𝑛 + ⋯ + ⋯ + 𝑐𝑛
log 𝑛
𝑇 𝑛 = 𝑐𝑛 σ𝑖=02 1𝑖 .
𝑇 𝑛 = 𝑐𝑛 (log 2 𝑛 + 1)
𝑇 𝑛 = 𝑐𝑛 log 2 𝑛 + 𝑐𝑛
𝑛 𝑛
𝑛 𝑐 7
𝑐 𝑐 8
2 4 𝑐𝑛
8
log 2 𝑛
2
𝑇
𝑛
𝑇
𝑛
𝑇
𝑛
𝑇
𝑛
8
𝑇
𝑛
16
𝑇
𝑛
𝑇
𝑛
𝑇
𝑛
𝑇
𝑛
7
2 4 8 32 16 32 64
𝑐𝑛
8
𝑖
7
𝑐𝑛
8
𝑇 1 𝑇 1 𝑇 1 ………………………..𝑇 1 𝑐𝑛
Fig –(b)
Recursion Tree Method (Example 4)
Analysis
First, we find the height of the recursion tree
𝑛 𝑛 𝑛
Hear the problem divide into three subproblem of size 𝑖 , 𝑖 , 𝑎𝑛𝑑 𝑖 .
2 4 8
For calculating the height of the tree, we consider the longest path of the tree. It has
been observed that the node on the left-hand side is the longest path of the tree.
𝑛
Hence the node at depth ′𝑖′ reflects a subproblem of size .
2𝑖
𝑛
i.e. the subproblem size hits 𝑛 = 1, when =1
2𝑖
𝑛
So, if =1
2𝑖
⟹ 𝑛 = 2𝑖 𝐴𝑝𝑝𝑙𝑦 𝐿𝑜𝑔 𝑏𝑜𝑡ℎ 𝑠𝑖𝑑𝑒
⟹ log 𝑛 = log 2 𝑖
⟹ 𝑖 = log 2 𝑛
So the height of the tree is log 2 𝑛.
Recursion Tree Method (Example 4)
𝑛 𝑛 𝑛
Second, we determine the cost of the tree in level ′𝑖′ = + +
2𝑖 4𝑖 8𝑖
So, the total cost of the tree is:
2 3
7 7 7
𝑇 𝑛 = 𝑐𝑛 + 𝑐𝑛 + 𝑐𝑛 + 𝑐𝑛 + ⋯ + ⋯
8 8 8
For simplicity we take ∞ Geometric Series
2 3
7 7 7
𝑇 𝑛 = 𝑐𝑛 + 𝑐𝑛 + 𝑐𝑛 + 𝑐𝑛 + ⋯ + ∞
8 8 8
∞ 𝑖
7
𝑇 𝑛 ≤ 𝑐𝑛
8
𝑖=0
1
𝑇 𝑛 ≤ 𝑐𝑛
7
1−
8
8
𝑇 𝑛 ≤ 𝑐𝑛
1
𝑇 𝑛 ≤ 8𝑐𝑛
𝐻𝑒𝑛𝑐𝑒, 𝑇 𝑛 = Ο(𝑛)