Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

Solving a Recurrence Relation using Repeated Substitution

𝒏𝒏
Solution of recurrence relation: 𝑻𝑻(𝒏𝒏) = 𝟐𝟐𝑻𝑻 �𝟐𝟐� + 𝒏𝒏 − 𝟏𝟏, given T(1) = 1
[Note that this is the recurrence relation that we get for merge sort]

𝑛𝑛
𝑇𝑇(𝑛𝑛) = 2𝑇𝑇 � � + 𝑛𝑛 − 1 ----- (1)
2
𝑛𝑛 𝑛𝑛
We can write 𝑇𝑇 � � as follows by replacing 𝑛𝑛 by in Equation (1) above
2 2

𝑛𝑛 𝑛𝑛 𝑛𝑛
𝑇𝑇 � � = 2𝑇𝑇 � 2 � + − 1
2 2 2
𝑛𝑛
Putting the value of 𝑇𝑇 � � in (1) we get
2
𝑛𝑛 𝑛𝑛
𝑇𝑇(𝑛𝑛) = 2 �2𝑇𝑇 � 2 � + − 1� + 𝑛𝑛 − 1
2 2
2 𝑛𝑛
= �2 𝑇𝑇 � 2 � + 𝑛𝑛 − 2� + 𝑛𝑛 − 1
2
2 𝑛𝑛
= 2 𝑇𝑇 � 2 � + (𝑛𝑛 + 𝑛𝑛) − (1 + 2) ----- (2)
2
𝑛𝑛 𝑛𝑛
Now, again using (1) we can write 𝑇𝑇 � 2 � as follows by replacing 𝑛𝑛 by
2 22
𝑛𝑛 𝑛𝑛 𝑛𝑛
𝑇𝑇 � 2 � = 2𝑇𝑇 � 3 � + 2 − 1
2 2 2
𝑛𝑛
Putting the value of 𝑇𝑇 � 2 � in (2) we get following
2

𝑛𝑛 𝑛𝑛
𝑇𝑇(𝑛𝑛) = 22 �2𝑇𝑇 � 3 � + − 1� + 𝑛𝑛 + 𝑛𝑛 − (1 + 2)
2 22
𝑛𝑛
= 23 𝑇𝑇 � 3 � + 𝑛𝑛 − 22 + 𝑛𝑛 + 𝑛𝑛 − (1 + 2)
2
𝑛𝑛
= 23 𝑇𝑇 � 3 � +
(𝑛𝑛 + 𝑛𝑛 + 𝑛𝑛) − (1 + 2 + 22 )
2
…. …..
………
Continue like this for k steps after which T(n) can be expressed as follows
𝑛𝑛
𝑇𝑇(𝑛𝑛) = 2𝑘𝑘 𝑇𝑇 � 𝑘𝑘 � + �����������
(𝑛𝑛 + 𝑛𝑛 + ⋯ . +𝑛𝑛) − (1 + 2 + 22 +. . +2𝑘𝑘 )
2
𝑘𝑘 times
𝑛𝑛
Let 2𝑘𝑘 = 𝑛𝑛, then =1
2𝑘𝑘
𝑛𝑛
So 𝑇𝑇(𝑛𝑛) = 2𝑘𝑘 𝑇𝑇 � 𝑘𝑘 � + �����������
(𝑛𝑛 + 𝑛𝑛 + ⋯ . +𝑛𝑛) − (1 + 2 + 22 +. . +2𝑘𝑘 )
2
𝑘𝑘 times
= 𝑛𝑛𝑛𝑛(1) + (𝑛𝑛 + 𝑛𝑛 + ⋯ . +𝑛𝑛) − (1 + 2 + 22 +. . +2𝑘𝑘 )
�����������
𝑘𝑘 times
1×�2𝑘𝑘+1 −1� 𝑎𝑎�𝑟𝑟 𝑛𝑛 +1 −1�
= 𝑛𝑛 × 1 + 𝑛𝑛𝑛𝑛 − 2−1
[Since sum of geometric progression: 𝑎𝑎 + 𝑎𝑎𝑎𝑎 + 𝑎𝑎𝑟𝑟 2 +. . +𝑎𝑎𝑟𝑟 𝑛𝑛 = 𝑟𝑟−1
]
𝑘𝑘+1
= 𝑛𝑛 + 𝑛𝑛𝑛𝑛 − �2 − 1�
= 𝑛𝑛 + 𝑛𝑛𝑛𝑛 − �2 × 2𝑘𝑘 − 1�
= 𝑛𝑛 + 𝑛𝑛𝑛𝑛 − 2𝑛𝑛 + 1
= 𝑛𝑛𝑛𝑛 − 𝑛𝑛 + 1 … (3)

Since 2𝑘𝑘 = 𝑛𝑛, we get k = log n


So from Equation (3) 𝑇𝑇(𝑛𝑛) = 𝑛𝑛 log 𝑛𝑛 − 𝑛𝑛 + 1
= 𝑂𝑂(𝑛𝑛 log 𝑛𝑛)
Hence solution of given recurrence relation is 𝑶𝑶(𝒏𝒏 𝐥𝐥𝐥𝐥𝐥𝐥 𝒏𝒏)
Exercise problems:

Solve following recurrence relations using repeated substitution. [Assume n = 2k]

1. T(n) = T(n-1) + c , given T(1) = 1 and c is a constant [Ans: O(n)]

2. T(n) = T(n/2) + c , given T(1) = 1 [Ans: O(log n)]

3. T(n)=T(n−1)+n, given T(1)=0 [Ans: O(n2)]

4. T(n) = 2 T(n-1) + c , given T(1) = 1 and c is a constant [Ans: O(2n)]

You might also like