You are on page 1of 21

25 January 2024

L-05
AGENDA:
❑Amortized Analysis

❑Recurrence Relation(RR): Introduction

❑Backward Substitution for Solving RR


Amortized Analysis
Amortized analysis is applied on data structures that support many
operations.
Asymptotic Analysis Amortized Analysis

Classical asymptotic analysis


gives worst case analysis of
Amortized analysis focuses on
each operation without taking
a sequence of operations
the effect of one operation on
the other

Amortized Analysis is applied to algorithms where an occasional


operation is very slow, but most of the other operations are
faster.
Aggregate Analysis for Amortized Analysis

1. Computing the total cost T(n) for a sequence of n operations

2. Divide T(n) by the number n of operations to obtain the amortized


cost or the average cost in the worst case

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 * 24 = 120

T(4) = 4T(3) = 4 * 6 =24

T(3) = 3T(2) =3*2=6

T(2) = 2T(1) =2*1=2


Recurrence Relation
Base
𝐓 𝐧 = 𝐧𝐓 𝐧 − 𝟏 , 𝐓 𝟏 = 𝟏 Case

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.

Folding 42 times will bring you to the Moon.


Designing Recurrence Relation
Solution:

Let T(n) = The thickness of paper after folding n times


T(1) = 2 = 21(Base Case)
T(2) = 4 = 2 *2 = 2T(1)
T(3) = 8 = 2*4 = 2T(2)
.
.
.

. T(n) = 2T(n-1) ; T(1) = 2


T(n) = 2T(n-1)
Designing Recurrence Relation
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.

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

Base Case: Recursive Case:


T(1) = 2 T(2) = 4 = T(1) + 2
T(3) = 7 = T(2) + 3
T(4) = 11 = T(3) + 4
……………………..
T(n) = T(n-1) + n

T(n) = T(n-1) + n ; T(1) = 2


Solving Recurrence Relation

❑ 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

T(n) = 2T(n-1) ; T(1) = 2


= 2[2T(n-2)] as T(n-1) = 2T(n-2)
=22T(n-2)
=22[2T(n-3)] as T(n-2) = 2T(n-3)
=23T(n-3)
.
.
. 1 = n – (n-1)
?
=2 T(1)
=2n-1T(n-(n-1))
=2n-1T(1) = 2n-12 = 2n = O(2n)
Backward Substitution Method

T(n) = The thickness of the paper after folding n times =2


n

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.

Solution: T(n) = T(n-1) + n ; T(1) = 2


= T(n-2) + (n-1) + n as T(n-1) = T(n-2) + (n-1)
= T(n-3) + (n-2) + (n-1) + n as T(n-2) = T(n-3) + (n-2)
= T(n-4) + (n-3) +(n-2) + (n-1) + n as T(n-3) = T(n-4) + (n-3)
.
.
.
=T(1) + 2 + 3 + 4 + ……………………… + n
= 2 + 2 + 3 + 4 + 5 + ……………………..+ n
= 1 + 1 + 2 + 3 + 4 + 5 + ……………………..+ n
= 1 + {1 + 2 + 3 + 4 + 5 + ……………………..+ n}
𝒏(𝒏+𝟏)
=1+ 𝟐
= O(n2)

𝒏(𝒏+𝟏)
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

Solution: 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)

You might also like