Amortized Analysis Recurrence Relation (RR) : Backward Substitution For Solving RR
Amortized Analysis Recurrence Relation (RR) : Backward Substitution For Solving RR
Amortized Analysis Recurrence Relation (RR) : Backward Substitution For Solving RR
L-05
AGENDA:
❑Amortized Analysis
3. For all operations the same amortized cost T(n)/n is assigned, even if
they are of different types
Aggregate Analysis for Amortized Analysis
Example:
VERY SLOW
Cost of operation-1 = O(n)
Cost of operation-2 = O(1)
Cost of operation-3 = O(1) FA S T
…………………………………
Cost of operation-n = O(1)
=======================
T(n) = n + 1 + 1 + 1 +……………………….+ 1 = n + n-1 = 2n-1
𝑻(𝒏) 𝟐𝒏−𝟏 𝟐𝒏
Amortized cost = = ≈ = 𝟐 = 𝑶 𝟐 = 𝑶(𝟏)
𝒏 𝒏 𝒏
Recurrence Relation
An expression which is defined in terms of itself using smaller inputs
1 2
𝐓 𝐧 = 𝐓 𝐧 − 𝟏 + 𝟏 ,𝐓 𝟏 = 𝟏
Base
Case
𝐓 𝐧 = 𝐧𝐓 𝐧 − 𝟏 ; 𝐓 𝟏 =𝟏
𝐧
𝐓 𝐧 = 𝟐𝐓 +𝐧 ; 𝐓 𝟐 =𝟏
𝟐
Recurrence Relation
Base
𝐓 𝐧 = 𝐓 𝐧 − 𝟏 + 𝟏 ,𝐓 𝟏 = 𝟏 Case
If n =5 then
T(5) = T(4) + 1 = 4 + 1 = 5
T(4) = T(3) + 1 = 3 + 1 = 4
T(3) = T(2) + 1 = 2 + 1 = 3
T(2) = T(1) + 1 = 1 + 1 = 2
Recurrence Relation
Base
𝐓 𝐧 = 𝐓 𝐧 − 𝟏 + 𝟏 ,𝐓 𝟏 = 𝟏 Case
T(5) = T(4) + 1
= T(3) + 1 + 1
= T(2) + 1 + 1 +1
= T(1) + 1 + 1+ 1 +1
= 1 + 1 + 1+ 1 +1
=5
Recurrence Relation
Base
𝐓 𝐧 = 𝐧𝐓 𝐧 − 𝟏 , 𝐓 𝟏 = 𝟏 Case
If n =5 then
T(5) = 5T(4)
= 5*4T(3)
= 5*4*3T(2)
= 5*4*3*2T(1)
=5*4*3*2*1
=5! = 120
Recurrence Relation
Base
𝐓 𝐧 = 𝟐𝐓 𝐧/𝟐 + 𝐧 , 𝐓 𝟏 = 𝟏 Case
T(16) = 2T(8) + 16
If n =16 then
= 2[2T(4) + 8] +16
T(16) = 2T(8) + 16 = 22T(4) + 16 + 16
= 22[2T(2) + 4] + 16 + 16
T(8) = 2T(4) + 8
= 23T(2) + 16 + 16 + 16
T(4) = 2T(2) + 4 = 23[2T(1) + 2] + 16 + 16 + 16
= 24T(1) + 16 + 16 + 16 + 16
T(2) = 2T(1) + 2
= 16 + 16 + 16 + 16 + 16
= 80
Designing Recurrence Relation
Problem : Let us consider a piece of paper with 1 unit of thickness. If we fold it once
the thickness is 2 units. If we fold it twice its thickness is 4 units and so on . Design a
recurrence relation to compute the thickness of paper after folding it n times.
2 2
2
1 3
1
3
7
1
6
4
4
5
Designing Recurrence Relation
2
1
10
11 3
7 9
6 5
Designing Recurrence Relation
T(n) = Max number of regions obtained if we draw n chords in a circle
❑ Backward Substitution
❑ Recursion tree method
❑ Master Method
Backward Substitution Method
Problem : Let us consider a piece of paper with 1 unit of thickness. If we fold it once the thickness is 2 units.
If we fold it twice its thickness is 4 units and so on. Design a recurrence relation to compute the thickness of
paper after folding it n times.
Solution: Let T(n) = The thickness of the paper after folding n times
384,400 km
T(42) = The thickness of the paper after folding 42 times = 242 = 4 398 046.51 km
Backward Substitution Method
Problem : If we draw a chord in the circle maximum number of region we obtain is 2. Drawing 2 chord
divides the circle into maximum 4 regions and so on. Design a recurrence relation to compute maximum
number of regions in a circle after drawing n chords.
𝒏(𝒏+𝟏)
T(n) = Max number of regions obtained after drawing n chords = 1 + 𝟐
Backward Substitution Method
Solve the following Recurrence Relation: T(n) = 2T(n/2) + 2 ; T(2) =1
𝒏
= 2[2T(𝟐𝟐) + 2] + 2 𝒏 𝒏
As 𝑻 = 𝟐𝑻 + 𝟐
𝟐 𝟐𝟐
𝒏
= 𝟐𝟐 𝑻 + 𝟐𝟐 + 𝟐
𝟐𝟐
𝒏 𝒏 𝒏
= 𝟐𝟐 𝟐𝑻 + 𝟐 + 𝟐𝟐 + 𝟐 As 𝑻 =𝟐𝑻 + 𝟐
𝟐𝟑 𝟐𝟐 𝟐𝟑
𝒏
= 𝟐𝟑 𝑻 + 𝟐𝟑 + 𝟐𝟐 + 𝟐
𝟐𝟑
.
.
.
𝒏
= 𝟐𝒊 𝑻 + 𝟐𝒊 + 𝟐𝒊−𝟏 + ⋯ … … … … + 𝟐𝟑 + 𝟐𝟐 + 𝟐
𝟐𝒊
𝒏
= 𝟐𝒊 𝑻 + 𝟐𝒊 + 𝟐𝒊−𝟏 + ⋯ … … … … + 𝟐𝟑 + 𝟐𝟐 + 𝟐
𝟐𝒊
𝒏
Remember the base case T(2) = 1 = 𝟐 → 𝒏 = 𝟐. 𝟐𝒊 = 𝟐𝒊+𝟏
𝟐𝒊
= 𝟐𝒊 𝑻 𝟐 + 𝟐𝒊 + 𝟐𝒊−𝟏 + ⋯ … … … … + 𝟐𝟑 + 𝟐𝟐 + 𝟐
= 𝟐𝒊 ∗ 𝟏 + 𝟐𝒊 + 𝟐𝒊−𝟏 + ⋯ … … … … + 𝟐𝟑 + 𝟐𝟐 + 𝟐
= 𝟐𝒊 + 𝟐𝒊 + 𝟐𝒊−𝟏 + ⋯ … … … … + 𝟐𝟑 + 𝟐𝟐 + 𝟐
= 𝟐 + 𝟐𝟐 + 𝟐𝟑 + … … … … … . + 𝟐𝒊−𝟏 + 𝟐𝒊 + 𝟐𝒊
𝒂(𝒓𝒏 − 𝟏)
= 𝟐{ 𝟏 + 𝟐 + 𝟐𝟐 + 𝟐𝟑 + … … … … … . + 𝟐𝒊−𝟏 } + 𝟐𝒊 𝑺𝒏 = 𝒂 + 𝒂𝒓 + 𝒂𝒓𝟐 + 𝒂𝒓𝟑 + … . +𝒂𝒓𝒏−𝟏 =
𝒓−𝟏
𝟏(𝟐𝒊 −𝟏)
=2[ ] + 𝟐𝒊
𝟐−𝟏
𝒏
(𝟐𝒊+𝟏 −𝟐) = 𝟐𝒊+𝟏 −𝟐 + 𝟐𝒊 = 𝟐 → 𝒏 = 𝟐. 𝟐𝒊 = 𝟐𝒊+𝟏
= + 𝟐𝒊 𝟐𝒊
𝟏
=n-2+𝟐
𝒏
=
𝟑𝒏
𝟐
−𝟐 = O(n)