SlideShare a Scribd company logo
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

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

3.pdf
3.pdf3.pdf
3.pdf
AlaaOdeh18
 
lecture 3
lecture 3lecture 3
lecture 3
sajinsc
 
Computer science-formulas
Computer science-formulasComputer science-formulas
Computer science-formulas
RAJIV GANDHI INSTITUTE OF TECHNOLOGY
 
Recurrence_Theory.pptx studdents nees free
Recurrence_Theory.pptx studdents nees freeRecurrence_Theory.pptx studdents nees free
Recurrence_Theory.pptx studdents nees free
White44420
 
Digital Signal Processing
Digital Signal ProcessingDigital Signal Processing
Digital Signal Processing
aj ahmed
 
Recurrences
RecurrencesRecurrences
Recurrences
DEVTYPE
 
Solving linear homogeneous recurrence relations
Solving linear homogeneous recurrence relationsSolving linear homogeneous recurrence relations
Solving linear homogeneous recurrence relations
Dr. Maamoun Ahmed
 
Maths
MathsMaths
Divide and conquer
Divide and conquerDivide and conquer
Divide and conquer
Vikas Sharma
 
Recurrence equations
Recurrence equationsRecurrence equations
Recurrence equations
Tarun Gehlot
 
03 dc
03 dc03 dc
03 dc
Hira Gul
 
5.2 divide and conquer
5.2 divide and conquer5.2 divide and conquer
5.2 divide and conquer
Krish_ver2
 
RECURRENCE EQUATIONS & ANALYZING THEM
RECURRENCE EQUATIONS & ANALYZING THEMRECURRENCE EQUATIONS & ANALYZING THEM
RECURRENCE EQUATIONS & ANALYZING THEM
Alpana Ingale
 
recurrence relations
 recurrence relations recurrence relations
recurrence relations
Anurag Cheela
 
recurence solutions
recurence solutionsrecurence solutions
recurence solutions
soumya8396
 
Admission in india 2015
Admission in india 2015Admission in india 2015
Admission in india 2015
Edhole.com
 
Impact of Linear Homogeneous Recurrent Relation Analysis
Impact of Linear Homogeneous Recurrent Relation AnalysisImpact of Linear Homogeneous Recurrent Relation Analysis
Impact of Linear Homogeneous Recurrent Relation Analysis
ijtsrd
 
Recurrence relationclass 5
Recurrence relationclass 5Recurrence relationclass 5
Recurrence relationclass 5
Kumar
 
Recurrences
RecurrencesRecurrences
Recurrences
Ala' Mohammad
 
Series in Discrete Structure || Computer Science
Series in Discrete Structure || Computer ScienceSeries in Discrete Structure || Computer Science
Series in Discrete Structure || Computer Science
MubasharGhazi
 

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

3.pdf
3.pdf3.pdf
3.pdf
 
lecture 3
lecture 3lecture 3
lecture 3
 
Computer science-formulas
Computer science-formulasComputer science-formulas
Computer science-formulas
 
Recurrence_Theory.pptx studdents nees free
Recurrence_Theory.pptx studdents nees freeRecurrence_Theory.pptx studdents nees free
Recurrence_Theory.pptx studdents nees free
 
Digital Signal Processing
Digital Signal ProcessingDigital Signal Processing
Digital Signal Processing
 
Recurrences
RecurrencesRecurrences
Recurrences
 
Solving linear homogeneous recurrence relations
Solving linear homogeneous recurrence relationsSolving linear homogeneous recurrence relations
Solving linear homogeneous recurrence relations
 
Maths
MathsMaths
Maths
 
Divide and conquer
Divide and conquerDivide and conquer
Divide and conquer
 
Recurrence equations
Recurrence equationsRecurrence equations
Recurrence equations
 
03 dc
03 dc03 dc
03 dc
 
5.2 divide and conquer
5.2 divide and conquer5.2 divide and conquer
5.2 divide and conquer
 
RECURRENCE EQUATIONS & ANALYZING THEM
RECURRENCE EQUATIONS & ANALYZING THEMRECURRENCE EQUATIONS & ANALYZING THEM
RECURRENCE EQUATIONS & ANALYZING THEM
 
recurrence relations
 recurrence relations recurrence relations
recurrence relations
 
recurence solutions
recurence solutionsrecurence solutions
recurence solutions
 
Admission in india 2015
Admission in india 2015Admission in india 2015
Admission in india 2015
 
Impact of Linear Homogeneous Recurrent Relation Analysis
Impact of Linear Homogeneous Recurrent Relation AnalysisImpact of Linear Homogeneous Recurrent Relation Analysis
Impact of Linear Homogeneous Recurrent Relation Analysis
 
Recurrence relationclass 5
Recurrence relationclass 5Recurrence relationclass 5
Recurrence relationclass 5
 
Recurrences
RecurrencesRecurrences
Recurrences
 
Series in Discrete Structure || Computer Science
Series in Discrete Structure || Computer ScienceSeries in Discrete Structure || Computer Science
Series in Discrete Structure || Computer Science
 

Recently uploaded

A gentle exploration of Retrieval Augmented Generation
A gentle exploration of Retrieval Augmented GenerationA gentle exploration of Retrieval Augmented Generation
A gentle exploration of Retrieval Augmented Generation
dataschool1
 
一比一原版(曼大毕业证书)曼尼托巴大学毕业证如何办理
一比一原版(曼大毕业证书)曼尼托巴大学毕业证如何办理一比一原版(曼大毕业证书)曼尼托巴大学毕业证如何办理
一比一原版(曼大毕业证书)曼尼托巴大学毕业证如何办理
ytypuem
 
Namma-Kalvi-11th-Physics-Study-Material-Unit-1-EM-221086.pdf
Namma-Kalvi-11th-Physics-Study-Material-Unit-1-EM-221086.pdfNamma-Kalvi-11th-Physics-Study-Material-Unit-1-EM-221086.pdf
Namma-Kalvi-11th-Physics-Study-Material-Unit-1-EM-221086.pdf
22ad0301
 
一比一原版多伦多大学毕业证(UofT毕业证书)学历如何办理
一比一原版多伦多大学毕业证(UofT毕业证书)学历如何办理一比一原版多伦多大学毕业证(UofT毕业证书)学历如何办理
一比一原版多伦多大学毕业证(UofT毕业证书)学历如何办理
eoxhsaa
 
How To Control IO Usage using Resource Manager
How To Control IO Usage using Resource ManagerHow To Control IO Usage using Resource Manager
How To Control IO Usage using Resource Manager
Alireza Kamrani
 
一比一原版卡尔加里大学毕业证(uc毕业证)如何办理
一比一原版卡尔加里大学毕业证(uc毕业证)如何办理一比一原版卡尔加里大学毕业证(uc毕业证)如何办理
一比一原版卡尔加里大学毕业证(uc毕业证)如何办理
oaxefes
 
社内勉強会資料_Hallucination of LLMs               .
社内勉強会資料_Hallucination of LLMs               .社内勉強会資料_Hallucination of LLMs               .
社内勉強会資料_Hallucination of LLMs               .
NABLAS株式会社
 
一比一原版(UofT毕业证)多伦多大学毕业证如何办理
一比一原版(UofT毕业证)多伦多大学毕业证如何办理一比一原版(UofT毕业证)多伦多大学毕业证如何办理
一比一原版(UofT毕业证)多伦多大学毕业证如何办理
exukyp
 
一比一原版马来西亚博特拉大学毕业证(upm毕业证)如何办理
一比一原版马来西亚博特拉大学毕业证(upm毕业证)如何办理一比一原版马来西亚博特拉大学毕业证(upm毕业证)如何办理
一比一原版马来西亚博特拉大学毕业证(upm毕业证)如何办理
eudsoh
 
一比一原版加拿大渥太华大学毕业证(uottawa毕业证书)如何办理
一比一原版加拿大渥太华大学毕业证(uottawa毕业证书)如何办理一比一原版加拿大渥太华大学毕业证(uottawa毕业证书)如何办理
一比一原版加拿大渥太华大学毕业证(uottawa毕业证书)如何办理
uevausa
 
一比一原版南昆士兰大学毕业证如何办理
一比一原版南昆士兰大学毕业证如何办理一比一原版南昆士兰大学毕业证如何办理
一比一原版南昆士兰大学毕业证如何办理
ugydym
 
一比一原版(UO毕业证)渥太华大学毕业证如何办理
一比一原版(UO毕业证)渥太华大学毕业证如何办理一比一原版(UO毕业证)渥太华大学毕业证如何办理
一比一原版(UO毕业证)渥太华大学毕业证如何办理
bmucuha
 
一比一原版英属哥伦比亚大学毕业证(UBC毕业证书)学历如何办理
一比一原版英属哥伦比亚大学毕业证(UBC毕业证书)学历如何办理一比一原版英属哥伦比亚大学毕业证(UBC毕业证书)学历如何办理
一比一原版英属哥伦比亚大学毕业证(UBC毕业证书)学历如何办理
z6osjkqvd
 
Template xxxxxxxx ssssssssssss Sertifikat.pptx
Template xxxxxxxx ssssssssssss Sertifikat.pptxTemplate xxxxxxxx ssssssssssss Sertifikat.pptx
Template xxxxxxxx ssssssssssss Sertifikat.pptx
TeukuEriSyahputra
 
Module 1 ppt BIG DATA ANALYTICS NOTES FOR MCA
Module 1 ppt BIG DATA ANALYTICS NOTES FOR MCAModule 1 ppt BIG DATA ANALYTICS NOTES FOR MCA
Module 1 ppt BIG DATA ANALYTICS NOTES FOR MCA
yuvarajkumar334
 
一比一原版加拿大麦吉尔大学毕业证(mcgill毕业证书)如何办理
一比一原版加拿大麦吉尔大学毕业证(mcgill毕业证书)如何办理一比一原版加拿大麦吉尔大学毕业证(mcgill毕业证书)如何办理
一比一原版加拿大麦吉尔大学毕业证(mcgill毕业证书)如何办理
agdhot
 
一比一原版(uob毕业证书)伯明翰大学毕业证如何办理
一比一原版(uob毕业证书)伯明翰大学毕业证如何办理一比一原版(uob毕业证书)伯明翰大学毕业证如何办理
一比一原版(uob毕业证书)伯明翰大学毕业证如何办理
9gr6pty
 
一比一原版莱斯大学毕业证(rice毕业证)如何办理
一比一原版莱斯大学毕业证(rice毕业证)如何办理一比一原版莱斯大学毕业证(rice毕业证)如何办理
一比一原版莱斯大学毕业证(rice毕业证)如何办理
zsafxbf
 
一比一原版兰加拉学院毕业证(Langara毕业证书)学历如何办理
一比一原版兰加拉学院毕业证(Langara毕业证书)学历如何办理一比一原版兰加拉学院毕业证(Langara毕业证书)学历如何办理
一比一原版兰加拉学院毕业证(Langara毕业证书)学历如何办理
hyfjgavov
 
Overview IFM June 2024 Consumer Confidence INDEX Report.pdf
Overview IFM June 2024 Consumer Confidence INDEX Report.pdfOverview IFM June 2024 Consumer Confidence INDEX Report.pdf
Overview IFM June 2024 Consumer Confidence INDEX Report.pdf
nhutnguyen355078
 

Recently uploaded (20)

A gentle exploration of Retrieval Augmented Generation
A gentle exploration of Retrieval Augmented GenerationA gentle exploration of Retrieval Augmented Generation
A gentle exploration of Retrieval Augmented Generation
 
一比一原版(曼大毕业证书)曼尼托巴大学毕业证如何办理
一比一原版(曼大毕业证书)曼尼托巴大学毕业证如何办理一比一原版(曼大毕业证书)曼尼托巴大学毕业证如何办理
一比一原版(曼大毕业证书)曼尼托巴大学毕业证如何办理
 
Namma-Kalvi-11th-Physics-Study-Material-Unit-1-EM-221086.pdf
Namma-Kalvi-11th-Physics-Study-Material-Unit-1-EM-221086.pdfNamma-Kalvi-11th-Physics-Study-Material-Unit-1-EM-221086.pdf
Namma-Kalvi-11th-Physics-Study-Material-Unit-1-EM-221086.pdf
 
一比一原版多伦多大学毕业证(UofT毕业证书)学历如何办理
一比一原版多伦多大学毕业证(UofT毕业证书)学历如何办理一比一原版多伦多大学毕业证(UofT毕业证书)学历如何办理
一比一原版多伦多大学毕业证(UofT毕业证书)学历如何办理
 
How To Control IO Usage using Resource Manager
How To Control IO Usage using Resource ManagerHow To Control IO Usage using Resource Manager
How To Control IO Usage using Resource Manager
 
一比一原版卡尔加里大学毕业证(uc毕业证)如何办理
一比一原版卡尔加里大学毕业证(uc毕业证)如何办理一比一原版卡尔加里大学毕业证(uc毕业证)如何办理
一比一原版卡尔加里大学毕业证(uc毕业证)如何办理
 
社内勉強会資料_Hallucination of LLMs               .
社内勉強会資料_Hallucination of LLMs               .社内勉強会資料_Hallucination of LLMs               .
社内勉強会資料_Hallucination of LLMs               .
 
一比一原版(UofT毕业证)多伦多大学毕业证如何办理
一比一原版(UofT毕业证)多伦多大学毕业证如何办理一比一原版(UofT毕业证)多伦多大学毕业证如何办理
一比一原版(UofT毕业证)多伦多大学毕业证如何办理
 
一比一原版马来西亚博特拉大学毕业证(upm毕业证)如何办理
一比一原版马来西亚博特拉大学毕业证(upm毕业证)如何办理一比一原版马来西亚博特拉大学毕业证(upm毕业证)如何办理
一比一原版马来西亚博特拉大学毕业证(upm毕业证)如何办理
 
一比一原版加拿大渥太华大学毕业证(uottawa毕业证书)如何办理
一比一原版加拿大渥太华大学毕业证(uottawa毕业证书)如何办理一比一原版加拿大渥太华大学毕业证(uottawa毕业证书)如何办理
一比一原版加拿大渥太华大学毕业证(uottawa毕业证书)如何办理
 
一比一原版南昆士兰大学毕业证如何办理
一比一原版南昆士兰大学毕业证如何办理一比一原版南昆士兰大学毕业证如何办理
一比一原版南昆士兰大学毕业证如何办理
 
一比一原版(UO毕业证)渥太华大学毕业证如何办理
一比一原版(UO毕业证)渥太华大学毕业证如何办理一比一原版(UO毕业证)渥太华大学毕业证如何办理
一比一原版(UO毕业证)渥太华大学毕业证如何办理
 
一比一原版英属哥伦比亚大学毕业证(UBC毕业证书)学历如何办理
一比一原版英属哥伦比亚大学毕业证(UBC毕业证书)学历如何办理一比一原版英属哥伦比亚大学毕业证(UBC毕业证书)学历如何办理
一比一原版英属哥伦比亚大学毕业证(UBC毕业证书)学历如何办理
 
Template xxxxxxxx ssssssssssss Sertifikat.pptx
Template xxxxxxxx ssssssssssss Sertifikat.pptxTemplate xxxxxxxx ssssssssssss Sertifikat.pptx
Template xxxxxxxx ssssssssssss Sertifikat.pptx
 
Module 1 ppt BIG DATA ANALYTICS NOTES FOR MCA
Module 1 ppt BIG DATA ANALYTICS NOTES FOR MCAModule 1 ppt BIG DATA ANALYTICS NOTES FOR MCA
Module 1 ppt BIG DATA ANALYTICS NOTES FOR MCA
 
一比一原版加拿大麦吉尔大学毕业证(mcgill毕业证书)如何办理
一比一原版加拿大麦吉尔大学毕业证(mcgill毕业证书)如何办理一比一原版加拿大麦吉尔大学毕业证(mcgill毕业证书)如何办理
一比一原版加拿大麦吉尔大学毕业证(mcgill毕业证书)如何办理
 
一比一原版(uob毕业证书)伯明翰大学毕业证如何办理
一比一原版(uob毕业证书)伯明翰大学毕业证如何办理一比一原版(uob毕业证书)伯明翰大学毕业证如何办理
一比一原版(uob毕业证书)伯明翰大学毕业证如何办理
 
一比一原版莱斯大学毕业证(rice毕业证)如何办理
一比一原版莱斯大学毕业证(rice毕业证)如何办理一比一原版莱斯大学毕业证(rice毕业证)如何办理
一比一原版莱斯大学毕业证(rice毕业证)如何办理
 
一比一原版兰加拉学院毕业证(Langara毕业证书)学历如何办理
一比一原版兰加拉学院毕业证(Langara毕业证书)学历如何办理一比一原版兰加拉学院毕业证(Langara毕业证书)学历如何办理
一比一原版兰加拉学院毕业证(Langara毕业证书)学历如何办理
 
Overview IFM June 2024 Consumer Confidence INDEX Report.pdf
Overview IFM June 2024 Consumer Confidence INDEX Report.pdfOverview IFM June 2024 Consumer Confidence INDEX Report.pdf
Overview IFM June 2024 Consumer Confidence INDEX Report.pdf
 

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)