8.-DAA-LECTURE-8-RECURRENCES-AND-ITERATION-METHOD.pdf

431 views 24 slides Mar 07, 2023
Slide 1
Slide 1 of 24
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24

About This Presentation

Abc


Slide Content

Analysis and Design of
Algorithms
UNIT-1
Recurrence Relations

Content
Recurrence Relation
Forming Recurrence Relation
Solving Recurrence Relations
–Iterative Method
–Substitution Method
–Recursion Tree Method
–Master’s Method
2

3

Recurrence Examples














1
2
2
1
)(
nc
n
T
nc
nT 














1
1
)(
ncn
b
n
aT
nc
nT

Examples of recurrence relations
Example-1:
–Initial condition a
0= 1 (BASE CASE)
–Recursive formula: a
n= 1 + 2a
n-1for n >2
–First few terms are: 1, 3, 7, 15, 31, 63, …
Example-2:
–Initial conditions a
0= 1, a
1= 2 (BASE CASE)
–Recursive formula: a
n= 3(a
n-1+ a
n-2) for n >2
–First few terms are: 1, 2, 9, 33, 126, 477, 1809, 6858,
26001,…

Example-3: Fibonacci sequence
Initial conditions: (BASE CASE)
–f
1= 1, f
2= 2
Recursive formula:
–f
n+1= f
n-1+ f
n for n >3
First few terms:
n1234567891011
f
n123581321345589144

Example-4: Compound interest
Given
–P = initial amount (principal)
–n = number of years
–r = annual interest rate
–A = amount of money at the end of n years
At the end of:
1 year: A = P + rP = P(1+r)
2 years: A = P + rP(1+r) = P(1+r)
2
3 years: A = P + rP(1+r)
2
= P(1+r)
3

Obtain the formula A = P (1 + r)
n

Recurrence Relations: Terms
Recurrence relations have two parts:
–recursive terms and
–non-recursive terms
T(n) = 2T(n-2)+ n
2
-10
Recursive termscome from when an algorithms calls
itself
Non-recursiveterms correspond to the non-recursive
cost of the algorithm: work the algorithm performs
within a function
First, we need to know how to solverecurrences.

9

10

11
Solving Recurrence Relations
Iteration method
Substitution method
Recursion Tree
Master method

Iteration method
12

13

14

15
1. Iteration Method
Step-1: Expandthe Recurrence.
Step-2: Expressthe expansion as a summation,
by plugging the Recurrence back into itself,
until you see a Pattern.
( Use algebra to express as a summation)
Step-3: Evaluatethe summation.
Also known as “Try back substituting until you know
what is going on”.

16

17

18
Example-1
s(n) = c + s(n-1)
c + c + s(n-2)
2c + s(n-2)
2c + c + s(n-3)
3c + s(n-3)

kc + s(n-k) = ck + s(n-k)





0)1(
00
)(
nnsc
n
ns

Example-1
So far for n >= k we have
s(n) = ck + s(n-k)
What if k = n?
s(n) = cn + s(0) = cn
19

20
So far for n >= k we have
s(n) = ck + s(n-k)
What if k = n?
s(n) = cn + s(0) = cn
So
Thus in general
s(n) = cn





0)1(
00
)(
nnsc
n
ns
Example-1

21
s(n)
=n + s(n-1)
=n + n-1 + s(n-2)
=n + n-1 + n-2 + s(n-3)
=n + n-1 + n-2 + n-3 + s(n-4)
=…
= n + n-1 + n-2 + n-3 + … + n-(k-1) + s(n-k)





0)1(
00
)(
nnsn
n
ns

22
s(n)
=n + s(n-1)
=n + n-1 + s(n-2)
=n + n-1 + n-2 + s(n-3)
=n + n-1 + n-2 + n-3 + s(n-4)
=…
= n + n-1 + n-2 + n-3 + … + n-(k-1) + s(n-k)
=





0)1(
00
)(
nnsn
n
ns )(
1
knsi
n
kni



23
So far for n >= k we have
What if k = n?
Thus in general 





0)1(
00
)(
nnsn
n
ns )(
1
knsi
n
kni

 2
1
0)0(
11

 

n
nisi
n
i
n
i 2
1
)(


n
nns

Example-2
24
Solve T(n) = 2T(n/2) + n.
Solution: Assume n = 2
k
(so k = log n).
T(n) = 2T(n/2) + n
= 2 ( 2T(n/2
2
) + n/2 )+ n T(n/2) = 2T(n/2
2
) + n/2
= 2
2
T(n/2
2
)+ 2n
= 2
2
( 2T(n/2
3
) + n/2
2
)+ 2n T(n/2
2
) = 2T(n/2
3
) + n/2
2
= 2
3
T(n/2
3
) + 3n
= …
= 2
k
T(n/2
k
) + k n
= n T(1) + n log n
= (n log n)
Tags