SlideShare a Scribd company logo
3
Most read
22
Most read
24
Most read
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
)
(
n
c
n
T
n
c
n
T

















1
1
)
(
n
cn
b
n
aT
n
c
n
T
Examples of recurrence relations
 Example-1:
– Initial condition a0 = 1 (BASE CASE)
– Recursive formula: a n = 1 + 2a n-1 for n > 2
– First few terms are: 1, 3, 7, 15, 31, 63, …
 Example-2:
– Initial conditions a0 = 1, a1 = 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)
– f1 = 1, f2 = 2
 Recursive formula:
– f n+1 = f n-1 + f n for n > 3
 First few terms:
n 1 2 3 4 5 6 7 8 9 10 11
fn 1 2 3 5 8 13 21 34 55 89 144
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) + n2 -10
 Recursive terms come from when an algorithms calls
itself
 Non-recursive terms correspond to the non-recursive
cost of the algorithm: work the algorithm performs
within a function
 First, we need to know how to solve recurrences.
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: Expand the Recurrence.
Step-2: Express the 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: Evaluate the 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
(
0
0
)
(
n
n
s
c
n
n
s
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
(
0
0
)
(
n
n
s
c
n
n
s
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
(
0
0
)
(
n
n
s
n
n
n
s
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
(
0
0
)
(
n
n
s
n
n
n
s
)
(
1
k
n
s
i
n
k
n
i





23
 So far for n >= k we have
 What if k = n?
 Thus in general








0
)
1
(
0
0
)
(
n
n
s
n
n
n
s
)
(
1
k
n
s
i
n
k
n
i





2
1
0
)
0
(
1
1




 
 

n
n
i
s
i
n
i
n
i
2
1
)
(


n
n
n
s
Example-2
24
 Solve T(n) = 2T(n/2) + n.
Solution: Assume n = 2k (so k = log n).
T(n) = 2T(n/2) + n
= 2 ( 2T(n/22) + n/2 ) + n T(n/2) = 2T(n/22) + n/2
= 22 T(n/22) + 2n
= 22 ( 2T(n/23) + n/22 ) + 2n T(n/22) = 2T(n/23) + n/22
= 23T(n/23) + 3n
= …
= 2kT(n/2k) + k n
= n T(1) + n log n
= (n log n)

More Related Content

PPTX
Breadth First Search & Depth First Search
PDF
Recurrence relation solutions
PDF
Algorithm chapter 10
PPT
Data Structures- Part5 recursion
PPT
Divide and Conquer
PPT
Np cooks theorem
PPT
01 knapsack using backtracking
PDF
recursion tree method.pdf
Breadth First Search & Depth First Search
Recurrence relation solutions
Algorithm chapter 10
Data Structures- Part5 recursion
Divide and Conquer
Np cooks theorem
01 knapsack using backtracking
recursion tree method.pdf

What's hot (20)

PDF
Recursive algorithms
PPT
how to calclute time complexity of algortihm
PPTX
NP completeness
PDF
9. chapter 8 np hard and np complete problems
PPTX
0 1 knapsack using branch and bound
PDF
Iterations and Recursions
PPTX
8 queen problem
PPTX
Merge sort algorithm
PPTX
Topological Sorting
PPTX
Analysis of algorithm
PPTX
PPTX
Machine Learning-Linear regression
PPTX
Asymptotic notations
PPT
Dinive conquer algorithm
PPTX
Distributed Query Processing
PDF
Naive Bayes
PPTX
Learning in AI
PPTX
Tsp is NP-Complete
PPTX
Asymptotic Notations
PPT
Mergesort
Recursive algorithms
how to calclute time complexity of algortihm
NP completeness
9. chapter 8 np hard and np complete problems
0 1 knapsack using branch and bound
Iterations and Recursions
8 queen problem
Merge sort algorithm
Topological Sorting
Analysis of algorithm
Machine Learning-Linear regression
Asymptotic notations
Dinive conquer algorithm
Distributed Query Processing
Naive Bayes
Learning in AI
Tsp is NP-Complete
Asymptotic Notations
Mergesort
Ad

Similar to 8.-DAA-LECTURE-8-RECURRENCES-AND-ITERATION-METHOD.pdf (20)

PPT
Lec-4-2-Recurrence Relation-Iteration Method.ppt
PPT
lecture3.ppt
PPT
the ppt on chap6 of brassard and Bratley
PPT
04_Recurrences_1.ppt
PPT
recurrence relations in analysis of algorithm
PPTX
PCC_CS_404_OUCHITYAPRODHAN_17000124029[1].pptx
PDF
Lecture no 9(extended master method)
PPT
Analysis Of Algorithms Ii
PPTX
2.pptx
PPTX
solving_recurrence_relations_using_methods.pptx
PPT
Solving Recurrence slides with images.ppt
PPTX
Recurrence_Theory.pptx studdents nees free
PPTX
recurence solutions
PDF
CPSC 125 Ch 2 Sec 5
PDF
Recurrence relation
PPTX
Time Complexity of Recursive Functions.pptx
PPTX
solving_Recurrence_relations_using_methods1.pptx
PPTX
recurrence relations
PDF
3.pdf
PPTX
recurrence relation on ca1 topic by makaut.pptx
Lec-4-2-Recurrence Relation-Iteration Method.ppt
lecture3.ppt
the ppt on chap6 of brassard and Bratley
04_Recurrences_1.ppt
recurrence relations in analysis of algorithm
PCC_CS_404_OUCHITYAPRODHAN_17000124029[1].pptx
Lecture no 9(extended master method)
Analysis Of Algorithms Ii
2.pptx
solving_recurrence_relations_using_methods.pptx
Solving Recurrence slides with images.ppt
Recurrence_Theory.pptx studdents nees free
recurence solutions
CPSC 125 Ch 2 Sec 5
Recurrence relation
Time Complexity of Recursive Functions.pptx
solving_Recurrence_relations_using_methods1.pptx
recurrence relations
3.pdf
recurrence relation on ca1 topic by makaut.pptx
Ad

Recently uploaded (20)

PDF
How to run a consulting project- client discovery
PDF
Jean-Georges Perrin - Spark in Action, Second Edition (2020, Manning Publicat...
PDF
Data Engineering Interview Questions & Answers Cloud Data Stacks (AWS, Azure,...
PPTX
Managing Community Partner Relationships
PPTX
CYBER SECURITY the Next Warefare Tactics
PDF
[EN] Industrial Machine Downtime Prediction
PPTX
sac 451hinhgsgshssjsjsjheegdggeegegdggddgeg.pptx
PPTX
New ISO 27001_2022 standard and the changes
PDF
Introduction to the R Programming Language
PDF
Optimise Shopper Experiences with a Strong Data Estate.pdf
PDF
Global Data and Analytics Market Outlook Report
PDF
Transcultural that can help you someday.
PPTX
STERILIZATION AND DISINFECTION-1.ppthhhbx
PDF
Business Analytics and business intelligence.pdf
PPTX
SAP 2 completion done . PRESENTATION.pptx
PPTX
AI Strategy room jwfjksfksfjsjsjsjsjfsjfsj
PPTX
modul_python (1).pptx for professional and student
PPT
Predictive modeling basics in data cleaning process
PPTX
(Ali Hamza) Roll No: (F24-BSCS-1103).pptx
PDF
annual-report-2024-2025 original latest.
How to run a consulting project- client discovery
Jean-Georges Perrin - Spark in Action, Second Edition (2020, Manning Publicat...
Data Engineering Interview Questions & Answers Cloud Data Stacks (AWS, Azure,...
Managing Community Partner Relationships
CYBER SECURITY the Next Warefare Tactics
[EN] Industrial Machine Downtime Prediction
sac 451hinhgsgshssjsjsjheegdggeegegdggddgeg.pptx
New ISO 27001_2022 standard and the changes
Introduction to the R Programming Language
Optimise Shopper Experiences with a Strong Data Estate.pdf
Global Data and Analytics Market Outlook Report
Transcultural that can help you someday.
STERILIZATION AND DISINFECTION-1.ppthhhbx
Business Analytics and business intelligence.pdf
SAP 2 completion done . PRESENTATION.pptx
AI Strategy room jwfjksfksfjsjsjsjsjfsjfsj
modul_python (1).pptx for professional and student
Predictive modeling basics in data cleaning process
(Ali Hamza) Roll No: (F24-BSCS-1103).pptx
annual-report-2024-2025 original latest.

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

  • 1. Analysis and Design of Algorithms UNIT-1 Recurrence Relations
  • 2. Content  Recurrence Relation  Forming Recurrence Relation  Solving Recurrence Relations – Iterative Method – Substitution Method – Recursion Tree Method – Master’s Method 2
  • 3. 3
  • 5. Examples of recurrence relations  Example-1: – Initial condition a0 = 1 (BASE CASE) – Recursive formula: a n = 1 + 2a n-1 for n > 2 – First few terms are: 1, 3, 7, 15, 31, 63, …  Example-2: – Initial conditions a0 = 1, a1 = 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,…
  • 6. Example-3: Fibonacci sequence  Initial conditions: (BASE CASE) – f1 = 1, f2 = 2  Recursive formula: – f n+1 = f n-1 + f n for n > 3  First few terms: n 1 2 3 4 5 6 7 8 9 10 11 fn 1 2 3 5 8 13 21 34 55 89 144
  • 7. 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
  • 8. Recurrence Relations: Terms  Recurrence relations have two parts: – recursive terms and – non-recursive terms T(n) = 2T(n-2) + n2 -10  Recursive terms come from when an algorithms calls itself  Non-recursive terms correspond to the non-recursive cost of the algorithm: work the algorithm performs within a function  First, we need to know how to solve recurrences.
  • 9. 9
  • 10. 10
  • 11. 11 Solving Recurrence Relations  Iteration method  Substitution method  Recursion Tree  Master method
  • 13. 13
  • 14. 14
  • 15. 15 1. Iteration Method Step-1: Expand the Recurrence. Step-2: Express the 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: Evaluate the summation.  Also known as “Try back substituting until you know what is going on”.
  • 16. 16
  • 17. 17
  • 18. 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 ( 0 0 ) ( n n s c n n s
  • 19. 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. 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 ( 0 0 ) ( n n s c n n s Example-1
  • 21. 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 ( 0 0 ) ( n n s n n n s
  • 22. 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 ( 0 0 ) ( n n s n n n s ) ( 1 k n s i n k n i     
  • 23. 23  So far for n >= k we have  What if k = n?  Thus in general         0 ) 1 ( 0 0 ) ( n n s n n n s ) ( 1 k n s i n k n i      2 1 0 ) 0 ( 1 1          n n i s i n i n i 2 1 ) (   n n n s
  • 24. Example-2 24  Solve T(n) = 2T(n/2) + n. Solution: Assume n = 2k (so k = log n). T(n) = 2T(n/2) + n = 2 ( 2T(n/22) + n/2 ) + n T(n/2) = 2T(n/22) + n/2 = 22 T(n/22) + 2n = 22 ( 2T(n/23) + n/22 ) + 2n T(n/22) = 2T(n/23) + n/22 = 23T(n/23) + 3n = … = 2kT(n/2k) + k n = n T(1) + n log n = (n log n)