Recurrences
Recurrences
Recurrences
Chandrabose Aravindan
<AravindanC@ssn.edu.in>
def fact (n ) :
i f (n < 2):
return 1
else :
r e t u r n n ∗ f a c t ( n−1)
def fact (n ) :
i f (n < 2):
return 1
else :
r e t u r n n ∗ f a c t ( n−1)
def fact (n ) :
i f (n < 2):
return 1
else :
r e t u r n n ∗ f a c t ( n−1)
def fact (n ) :
i f (n < 2):
return 1
else :
r e t u r n n ∗ f a c t ( n−1)
T (n) = T (n − 1) + c
T (n) = T (n − 1) + c
T (n − 1) = T (n − 2) + c
T (n) = T (n − 1) + c
T (n − 1) = T (n − 2) + c
..
.
T (2) = T (1) + c
T (n) = T (n − 1) + c
T (n − 1) = T (n − 2) + c
..
.
T (2) = T (1) + c
T (n) = T (n − 1) + c
T (n − 1) = T (n − 2) + c
..
.
T (2) = T (1) + c
T (n) + T (n − 1) + T (n − 2) + · · · + T (2) =
T (n − 1) + T (n − 2) + · · · + T (2) + T (1) + (n − 1)c
T (n) = T (n − 1) + c
T (n − 1) = T (n − 2) + c
..
.
T (2) = T (1) + c
T (n) + T (n − 1) + T (n − 2) + · · · + T (2) =
T (n − 1) + T (n − 2) + · · · + T (2) + T (1) + (n − 1)c
T (n) +
T (n 1) +
− T (n −2) + · · · +
T (2)
=
T (n
− 1) +
T (n
− 2) + · · · + T (2)
+ T (1) + (n − 1)c
T (n) = T (n − 1) + c
T (n − 1) = T (n − 2) + c
..
.
T (2) = T (1) + c
T (n) + T (n − 1) + T (n − 2) + · · · + T (2) =
T (n − 1) + T (n − 2) + · · · + T (2) + T (1) + (n − 1)c
T (n) +
T (n 1) +
− T (n −2) + · · · +
T (2)
=
T (n
− 1) +
T (n
− 2) + · · · + T (2)
+ T (1) + (n − 1)c
T (n) = T (n − 1) + c
T (n − 1) = T (n − 2) + c
..
.
T (2) = T (1) + c
T (n) + T (n − 1) + T (n − 2) + · · · + T (2) =
T (n − 1) + T (n − 2) + · · · + T (2) + T (1) + (n − 1)c
T (n) +
T (n 1) +
− T (n −2) + · · · +
T (2)
=
T (n
− 1) +
T (n
− 2) + · · · + T (2)
+ T (1) + (n − 1)c
T (n) = T (n − 1) + c
T (n − 1) = T (n − 2) + c
..
.
T (2) = T (1) + c
T (n) + T (n − 1) + T (n − 2) + · · · + T (2) =
T (n − 1) + T (n − 2) + · · · + T (2) + T (1) + (n − 1)c
T (n) +
T (n 1) +
− T (n −2) + · · · +
T (2)
=
T (n
− 1) +
T (n
− 2) + · · · + T (2)
+ T (1) + (n − 1)c
T (n) = T (n − 1) + cn
T (n) = T (n − 1) + cn
T (n − 1) = T (n − 2) + c(n − 1)
T (n) = T (n − 1) + cn
T (n − 1) = T (n − 2) + c(n − 1)
..
.
T (2) = T (1) + 2c
T (n) = T (n − 1) + cn
T (n − 1) = T (n − 2) + c(n − 1)
..
.
T (2) = T (1) + 2c
T (n) = T (n − 1) + cn
T (n − 1) = T (n − 2) + c(n − 1) T (n) = T (1) + c(2 + 3 + · · · + n)
..
.
T (2) = T (1) + 2c
T (n) = T (n − 1) + cn
T (n − 1) = T (n − 2) + c(n − 1) T (n) = T (1) + c(2 + 3 + · · · + n)
..
. = T (1) + c( n(n+1) − 1) 2
T (2) = T (1) + 2c
T (n) = T (n − 1) + cn
T (n − 1) = T (n − 2) + c(n − 1) T (n) = T (1) + c(2 + 3 + · · · + n)
..
. = T (1) + c( n(n+1) − 1) 2
T (2) = T (1) + 2c
This arises, for example, in the divide and conquer algorithms such as
mergesort
The input is divided into two halves and both the halves are solved
independently
Then both the solutions are merged in linear time to get the overall
solution
When i = log n
T (n) = nT (1) + n log n
When i = log n
T (n) = nT (1) + n log n
T (n) = 2T (n/2) + n
T (n) = 2T (n/2) + n
T (n) 2T (n/2)
n = n +1
T (n) = 2T (n/2) + n
T (n) 2T (n/2)
n = n +1
T (n) T (n/2)
n = n/2 +1
T (n) = 2T (n/2) + n
T (n) 2T (n/2)
n = n +1
T (n) T (n/2)
n = n/2 +1
T (n/2) T (n/4)
n/2 = n/4 +1
T (n/4) T (n/8)
n/4 = n/8 +1
..
.
T (2) T (1)
2 = 1 +1
T (n) = 2T (n/2) + n
T (n) 2T (n/2)
n = n +1
T (n) T (n/2)
n = n/2 +1
T (n/2) T (n/4)
n/2 = n/4 +1
T (n/4) T (n/8)
n/4 = n/8 +1
..
.
T (2) T (1)
2 = 1 +1
T (n) = 2T (n/2) + n
T (n) 2T (n/2)
n = n +1
T (n) T (n/2)
n = n/2 +1
T (n/2) T (n/4)
n/2 = n/4 +1
T (n/4) T (n/8)
n/4 = n/8 +1
..
.
T (2)
2 = T 1(1) + 1
T (n)
n = T (1) + log n
T (n) = cn + n log n
T (n) = 2T (n/2) + n
T (n) 2T (n/2)
n = n +1
T (n) T (n/2)
n = n/2 +1
T (n/2) T (n/4)
n/2 = n/4 +1
T (n/4) T (n/8)
n/4 = n/8 +1
..
.
T (2)
2 = T 1(1) + 1
T (n)
n = T (1) + log n
T (n) = cn + n log n
O(n log n)
C. Aravindan (SSN Institutions) Data Structures March 12, 2024 8 / 14
Slightly Complicated Example
n−1
nT (n) = 2 T (j) + cn2
X
j=0
n−1
nT (n) = 2 T (j) + cn2
X
j=0
n−2
(n − 1)T (n − 1) = 2 T (j) + c(n − 1)2
X
j=0
n−1
nT (n) = 2 T (j) + cn2
X
j=0
n−2
(n − 1)T (n − 1) = 2 T (j) + c(n − 1)2
X
j=0
— — — — — — — — — — — — — —Subtract
n−1
nT (n) = 2 T (j) + cn2
X
j=0
n−2
(n − 1)T (n − 1) = 2 T (j) + c(n − 1)2
X
j=0
— — — — — — — — — — — — — —Subtract
n−1
nT (n) = 2 T (j) + cn2
X
j=0
n−2
(n − 1)T (n − 1) = 2 T (j) + c(n − 1)2
X
j=0
— — — — — — — — — — — — — —Subtract
T (n) T (n − 1) 2c
= +
n+1 n n+1