SlideShare a Scribd company logo
1 of 25
Table Of Contents
Solving Recurrences
The Master Theorem
Recurrence Relations
Recurrences
• The expression:
is a recurrence.
– Recurrence: an equation that describes a function in
terms of its value on smaller functions






>+





=
=
1
2
2
1
)(
ncn
n
T
nc
nT
Recurrence Examples



>
=
−+
=
0
0
)1(
0
)(
n
n
nsc
ns



>−+
=
=
0)1(
00
)(
nnsn
n
ns






>+





=
=
1
2
2
1
)(
nc
n
T
nc
nT







>+





=
=
1
1
)(
ncn
b
n
aT
nc
nT
Solving Recurrences
• Substitution method
• Iteration method
• Master method
Solving Recurrences
• The substitution method (CLR 4.1)
– “Making a good guess” method
– Guess the form of the answer, then use
induction to find the constants and show that
solution works
– Examples:
• T(n) = 2T(n/2) + Θ(n) T(n) = Θ(n lg n)
• T(n) = 2T(n/2) + n ???
Solving Recurrences
• The substitution method (CLR 4.1)
– “Making a good guess” method
– Guess the form of the answer, then use
induction to find the constants and show that
solution works
– Examples:
• T(n) = 2T(n/2) + Θ(n)  T(n) = Θ(n lg n)
• T(n) = 2T(n/2) + n  T(n) = Θ(n lg n)
• T(n) = 2T(n/2 )+ 17) + n  ???
Substitution method
• Guess the form of the solution .
• Use mathematical induction to find the constants and
show that the solution works .
The substitution method can be used to establish either upper
or lower bounds on a recurrence.
An example (Substitution method )
• T(n) = 2T(floor(n/2) ) +n
We guess that the solution is T(n)=0(n lg n).
i.e. to show that T(n) ≤ cn lg n , for some constant c> 0 and n ≥ m.
Assume that this bound holds for [n/2]. So , we get
T(n) ≤ 2(c floor (n/2) lg(floor(n/2))) + n
≤ cn lg(n/2) + n
= cn lg n – cn lg 2 + n
= cn lg n – cn + n
≤ cn lg n
where , the last step holds as long as c≥ 1.
• Boundary conditions :
Suppose , T(1)=1 is the sole boundary condition of the
recurrence .
then , for n=1 , the bound T(n)≤ c n lg n yields
T(1)≤ c lg1=0 , which is at odds with T(1)=1.
Thus ,the base case of our inductive proof fails to hold.
To overcome this difficulty , we can take advantage of the
asymptotic notation which only requires us to prove
T(n)≤c n lg n for n≥ m.
The idea is to remove the difficult boundary condition T(1)= 1
from consideration.
Thus , we can replace T(1) by T(2) as the base cases in the
inductive proof , letting m=2.
Contd....
From the recurrence , with T(1) = 1, we get
T(2)=4
We require T(2)≤ c 2 lg 2
It is clear that , any choice of c≥2 suffices for
the base cases
Are we done?
• Have we proved the case for n =3.
• Have we proved that T(3) ≤ c 3 lg 3.
• No. Since floor(3/2) = 1 and for n =1, it does not hold. So
induction does not apply on n= 3.
• From the recurrence, with T(1) = 1, we get T(3) = 5.
The inductive proof that T(n) ≤ c n lg n for some constant c≥1
can now be completed by choosing c large enough that
T(3)≤c 3 lg 3 also holds.
It is clear that , any choice of c ≥ 2 is sufficient for this to hold.
Thus we can conclude that T(n) ≤ c n lg n for any c ≥ 2 and n ≥ 2.
Solving Recurrences
• Another option is “iteration method”
– Expand the recurrence
– Work some algebra to express as a
summation
– Evaluate the summation
• We will show some examples
• Solve the following recurrence:
T(n) = T(αn) + T(βn) + n,
where 0 < α ≤ β < 1
Assume suitable initial conditions.
Assignment 4
Recursive Complexity Patterns
• When you have simple recurrence
relations of the form
• T(n) = T(n-1) + f(n), then you have
complexity of O(n*f(n))
• T(n) = T(n/k) + f(n), then you have
complexity of O(logk n * f(n))
• Consider the Fibonacci sequence recursive calculation:
• F(1) = 1
F(2) = 1
F(n) = F(n-1)+F(n-2)
• Its recurrence relation time is
• T(1) = 1
T(2) = 1
T(n) = T(n-1) + T(n-2) + 1
• Solving is a little beyond this course, but T(n) = O(2n
) or
"exponential“
Consider the recurrence relation T(n) = T(n-1) + T(n/2) + n
Which of the following is a good tight upper bound on T(n)
(a) Θ n2
(b) Θ(n2logn)
(c) Θ(2logn2)
(d) Θ(nlogn2)
• Lets Solve it by substitution.
T(n) = T(n-1) + T(n/2) + n
= T(n-2) + T(n/4) + (n-1) + n
= T(n-3) + T(n/8) + (n-2) + (n-1) + n
= T(n-4) + T(n/16) + (n-3) + (n-2) + (n-1) + n
= ------------
= ------------
= -------------
= T(n-k) + T(n/(2^k)) + (n-(k-1)) + (n-(k-2)) + ------------------------------ + (n-2) + (n-1) + n
Now consider n = 2^k, then k = log n
= T((2^k) - k) + T((2^k)/(2^k)) + (n - k + 1) + (n - k + 2) + ---------------------- + (n - 1) + n
Now Put value of K
= (2^(log n) - log n) + 1 + (n - log n + 1) + (n - log n + 2) + ------------------------ + n
Here series {(n - log n + 1) + (n - log n + 2) + ---------------------- + (n - 1) + n} are in AP with (log n) element then
Hence Sn = ((log n)/2) *(n -log n + 1 + n) = O(n.log n)
Hence,
T(n) = 2^(log n)
From all the given answer, Option C is asymptotically close to T(n), Hence C will be the answer.
The Master Theorem
• Given: a divide and conquer algorithm
– An algorithm that divides the problem of size
n into a subproblems, each of size n/b
– Let the cost of each stage (i.e., the work to
divide the problem + combine solved
subproblems) be described by the function
f(n)
• Then, the Master Theorem gives us a
cookbook for the algorithm’s running time:
The Master Theorem
• if T(n) = aT(n/b) + f(n) then
( )
( )
( )
( )
( )
( )












<
>











<
Ω=
Θ=
=
Θ
Θ
Θ
=
+
−
1
0
largefor)()/(
AND)(
)(
)(
)(
log)(
log
log
log
log
log
c
nncfbnaf
nnf
nnf
nOnf
nf
nn
n
nT
a
a
a
a
a
b
b
b
b
b
ε
ε
ε
Cont.
Using The Master Method
• T(n) = 9T(n/3) + n
– a=9, b=3, f(n) = n
– nlogb a
= nlog3 9
= Θ(n2
)
– Since f(n) = O(nlog3 9 - ε
), where ε=1, case 1
applies:
– Thus the solution is T(n) = Θ(n2
)
( ) ( )ε−
=Θ= aa bb
nOnfnnT loglog
)(when)(
More Examples of Master’s
Theorem
• T(n) = 3T(n/5) + n o/p nlog35
• T(n) = 2T(n/2) + n o/p : nlogn
• T(n) = 2T(n/2) + 1
• T(n) = T(n/2) + n
• T(n) = T(n/2) + 1
When Master’s Theorem cannot
be applied
• T(n) = 2T(n/2) + n logn
• T(n) = 2T(n/2) + n/ logn
Reference
• https://en.wikipedia.org/wiki/Master_theorem
• http://stackoverflow.com/questions/8185079/how-to-calculate-
binary-search-complexity
• http://www.csd.uwo.ca/~moreno/CS433-
CS9624/Resources/master.pdf
• https://en.wikipedia.org/wiki/Time_complexity
• http://stackoverflow.com/questions/20512642/big-o-confusion-
log2n-vs-log3n

More Related Content

What's hot

Asymptotic Notation and Data Structures
Asymptotic Notation and Data StructuresAsymptotic Notation and Data Structures
Asymptotic Notation and Data StructuresAmrinder Arora
 
Master method
Master method Master method
Master method Rajendran
 
Complexity analysis - The Big O Notation
Complexity analysis - The Big O NotationComplexity analysis - The Big O Notation
Complexity analysis - The Big O NotationJawad Khan
 
Algorithms Lecture 2: Analysis of Algorithms I
Algorithms Lecture 2: Analysis of Algorithms IAlgorithms Lecture 2: Analysis of Algorithms I
Algorithms Lecture 2: Analysis of Algorithms IMohamed Loey
 
Stressen's matrix multiplication
Stressen's matrix multiplicationStressen's matrix multiplication
Stressen's matrix multiplicationKumar
 
recurrence relations
 recurrence relations recurrence relations
recurrence relationsAnurag Cheela
 
Sparse matrix and its representation data structure
Sparse matrix and its representation data structureSparse matrix and its representation data structure
Sparse matrix and its representation data structureVardhil Patel
 
Binary Search Tree in Data Structure
Binary Search Tree in Data StructureBinary Search Tree in Data Structure
Binary Search Tree in Data StructureDharita Chokshi
 
Asymptotic notations
Asymptotic notationsAsymptotic notations
Asymptotic notationsEhtisham Ali
 
Asymptotic analysis
Asymptotic analysisAsymptotic analysis
Asymptotic analysisSoujanya V
 
Data Structures and Algorithm Analysis
Data Structures  and  Algorithm AnalysisData Structures  and  Algorithm Analysis
Data Structures and Algorithm AnalysisMary Margarat
 
Lec 5 asymptotic notations and recurrences
Lec 5 asymptotic notations and recurrencesLec 5 asymptotic notations and recurrences
Lec 5 asymptotic notations and recurrencesAnkita Karia
 

What's hot (20)

String matching algorithms
String matching algorithmsString matching algorithms
String matching algorithms
 
Divide and conquer
Divide and conquerDivide and conquer
Divide and conquer
 
Asymptotic Notation and Data Structures
Asymptotic Notation and Data StructuresAsymptotic Notation and Data Structures
Asymptotic Notation and Data Structures
 
Insertion Sorting
Insertion SortingInsertion Sorting
Insertion Sorting
 
Master method
Master method Master method
Master method
 
Greedy algorithm
Greedy algorithmGreedy algorithm
Greedy algorithm
 
Merge sort algorithm power point presentation
Merge sort algorithm power point presentationMerge sort algorithm power point presentation
Merge sort algorithm power point presentation
 
Complexity analysis - The Big O Notation
Complexity analysis - The Big O NotationComplexity analysis - The Big O Notation
Complexity analysis - The Big O Notation
 
Recurrence relation
Recurrence relationRecurrence relation
Recurrence relation
 
Algorithms Lecture 2: Analysis of Algorithms I
Algorithms Lecture 2: Analysis of Algorithms IAlgorithms Lecture 2: Analysis of Algorithms I
Algorithms Lecture 2: Analysis of Algorithms I
 
Stressen's matrix multiplication
Stressen's matrix multiplicationStressen's matrix multiplication
Stressen's matrix multiplication
 
recurrence relations
 recurrence relations recurrence relations
recurrence relations
 
Tree and graph
Tree and graphTree and graph
Tree and graph
 
Sparse matrix and its representation data structure
Sparse matrix and its representation data structureSparse matrix and its representation data structure
Sparse matrix and its representation data structure
 
Binary Search Tree in Data Structure
Binary Search Tree in Data StructureBinary Search Tree in Data Structure
Binary Search Tree in Data Structure
 
Asymptotic notations
Asymptotic notationsAsymptotic notations
Asymptotic notations
 
Asymptotic analysis
Asymptotic analysisAsymptotic analysis
Asymptotic analysis
 
Data Structures and Algorithm Analysis
Data Structures  and  Algorithm AnalysisData Structures  and  Algorithm Analysis
Data Structures and Algorithm Analysis
 
Lec 5 asymptotic notations and recurrences
Lec 5 asymptotic notations and recurrencesLec 5 asymptotic notations and recurrences
Lec 5 asymptotic notations and recurrences
 
Complexity analysis in Algorithms
Complexity analysis in AlgorithmsComplexity analysis in Algorithms
Complexity analysis in Algorithms
 

Viewers also liked

Establishing the recurrence relation
Establishing the recurrence relationEstablishing the recurrence relation
Establishing the recurrence relationShaun Wilson
 
lecture 4
lecture 4lecture 4
lecture 4sajinsc
 
Floyd Warshall algorithm easy way to compute - Malinga
Floyd Warshall algorithm easy way to compute - MalingaFloyd Warshall algorithm easy way to compute - Malinga
Floyd Warshall algorithm easy way to compute - MalingaMalinga Perera
 
Recurrence relationclass 5
Recurrence relationclass 5Recurrence relationclass 5
Recurrence relationclass 5Kumar
 
Applied And Persuasive Applications For Museums
Applied And Persuasive Applications For MuseumsApplied And Persuasive Applications For Museums
Applied And Persuasive Applications For MuseumsPietro Polsinelli
 
Getting It Right: “Bad Paper” Legislation That Works
Getting It Right: “Bad Paper” Legislation That WorksGetting It Right: “Bad Paper” Legislation That Works
Getting It Right: “Bad Paper” Legislation That WorksSwords to Plowshares
 
Estrés. Cómo se produce un ataque de estrés y estrategias para manejarlo.
Estrés. Cómo se produce un ataque de estrés y estrategias para manejarlo.Estrés. Cómo se produce un ataque de estrés y estrategias para manejarlo.
Estrés. Cómo se produce un ataque de estrés y estrategias para manejarlo.Nayma Consultores
 
Sintesis informativa 29 de marzo 2017
Sintesis informativa 29 de marzo 2017Sintesis informativa 29 de marzo 2017
Sintesis informativa 29 de marzo 2017megaradioexpress
 
Bloco Esquerda
Bloco EsquerdaBloco Esquerda
Bloco Esquerdaamattos76
 
Tues. March 7th Pine River Announcements
Tues. March 7th Pine River Announcements  Tues. March 7th Pine River Announcements
Tues. March 7th Pine River Announcements Pine River
 
Federalists vs. Republicans
Federalists vs. RepublicansFederalists vs. Republicans
Federalists vs. Republicansmwhittakerms
 
Fläckvis lappning och vägreglernas förbud mot split friction, granlund wsp
Fläckvis lappning och vägreglernas förbud mot split friction, granlund wspFläckvis lappning och vägreglernas förbud mot split friction, granlund wsp
Fläckvis lappning och vägreglernas förbud mot split friction, granlund wspJohan Granlund
 
Social Research on Violence
Social Research on ViolenceSocial Research on Violence
Social Research on ViolenceMd.Ashfak sayed
 

Viewers also liked (18)

Establishing the recurrence relation
Establishing the recurrence relationEstablishing the recurrence relation
Establishing the recurrence relation
 
lecture 4
lecture 4lecture 4
lecture 4
 
Floyd Warshall algorithm easy way to compute - Malinga
Floyd Warshall algorithm easy way to compute - MalingaFloyd Warshall algorithm easy way to compute - Malinga
Floyd Warshall algorithm easy way to compute - Malinga
 
Recurrence relationclass 5
Recurrence relationclass 5Recurrence relationclass 5
Recurrence relationclass 5
 
Master method
Master methodMaster method
Master method
 
The Floyd–Warshall algorithm
The Floyd–Warshall algorithmThe Floyd–Warshall algorithm
The Floyd–Warshall algorithm
 
Applied And Persuasive Applications For Museums
Applied And Persuasive Applications For MuseumsApplied And Persuasive Applications For Museums
Applied And Persuasive Applications For Museums
 
Getting It Right: “Bad Paper” Legislation That Works
Getting It Right: “Bad Paper” Legislation That WorksGetting It Right: “Bad Paper” Legislation That Works
Getting It Right: “Bad Paper” Legislation That Works
 
Estrés. Cómo se produce un ataque de estrés y estrategias para manejarlo.
Estrés. Cómo se produce un ataque de estrés y estrategias para manejarlo.Estrés. Cómo se produce un ataque de estrés y estrategias para manejarlo.
Estrés. Cómo se produce un ataque de estrés y estrategias para manejarlo.
 
Sintesis informativa 29 de marzo 2017
Sintesis informativa 29 de marzo 2017Sintesis informativa 29 de marzo 2017
Sintesis informativa 29 de marzo 2017
 
Bloco Esquerda
Bloco EsquerdaBloco Esquerda
Bloco Esquerda
 
Tues. March 7th Pine River Announcements
Tues. March 7th Pine River Announcements  Tues. March 7th Pine River Announcements
Tues. March 7th Pine River Announcements
 
Bn1029 demo sap sd
Bn1029 demo  sap sdBn1029 demo  sap sd
Bn1029 demo sap sd
 
Speed costs
Speed costsSpeed costs
Speed costs
 
The Impact of Volatility on Wealth
The Impact of Volatility on WealthThe Impact of Volatility on Wealth
The Impact of Volatility on Wealth
 
Federalists vs. Republicans
Federalists vs. RepublicansFederalists vs. Republicans
Federalists vs. Republicans
 
Fläckvis lappning och vägreglernas förbud mot split friction, granlund wsp
Fläckvis lappning och vägreglernas förbud mot split friction, granlund wspFläckvis lappning och vägreglernas förbud mot split friction, granlund wsp
Fläckvis lappning och vägreglernas förbud mot split friction, granlund wsp
 
Social Research on Violence
Social Research on ViolenceSocial Research on Violence
Social Research on Violence
 

Similar to Time complexity

Recurrences
RecurrencesRecurrences
RecurrencesDEVTYPE
 
04_Recurrences_1.ppt
04_Recurrences_1.ppt04_Recurrences_1.ppt
04_Recurrences_1.pptMouDhara1
 
T2311 - Ch 4_Part1.pptx
T2311 - Ch 4_Part1.pptxT2311 - Ch 4_Part1.pptx
T2311 - Ch 4_Part1.pptxGadaFarhan
 
Solving recurrences
Solving recurrencesSolving recurrences
Solving recurrencesMegha V
 
Copy of y16 02-2119divide-and-conquer
Copy of y16 02-2119divide-and-conquerCopy of y16 02-2119divide-and-conquer
Copy of y16 02-2119divide-and-conquerJoepang2015
 
Algorithm Design and Complexity - Course 3
Algorithm Design and Complexity - Course 3Algorithm Design and Complexity - Course 3
Algorithm Design and Complexity - Course 3Traian Rebedea
 
Lecture 5 6_7 - divide and conquer and method of solving recurrences
Lecture 5 6_7 - divide and conquer and method of solving recurrencesLecture 5 6_7 - divide and conquer and method of solving recurrences
Lecture 5 6_7 - divide and conquer and method of solving recurrencesjayavignesh86
 
8.-DAA-LECTURE-8-RECURRENCES-AND-ITERATION-METHOD.pdf
8.-DAA-LECTURE-8-RECURRENCES-AND-ITERATION-METHOD.pdf8.-DAA-LECTURE-8-RECURRENCES-AND-ITERATION-METHOD.pdf
8.-DAA-LECTURE-8-RECURRENCES-AND-ITERATION-METHOD.pdfRishikeshJha33
 
lecture 3
lecture 3lecture 3
lecture 3sajinsc
 
Analysis Of Algorithms Ii
Analysis Of Algorithms IiAnalysis Of Algorithms Ii
Analysis Of Algorithms IiSri Prasanna
 
Divide and conquer
Divide and conquerDivide and conquer
Divide and conquerVikas Sharma
 
RECURRENCE EQUATIONS & ANALYZING THEM
RECURRENCE EQUATIONS & ANALYZING THEMRECURRENCE EQUATIONS & ANALYZING THEM
RECURRENCE EQUATIONS & ANALYZING THEMAlpana Ingale
 
Improvised Master's Theorem
Improvised Master's TheoremImprovised Master's Theorem
Improvised Master's TheoremLaiba Younas
 
Recurrence equations
Recurrence equationsRecurrence equations
Recurrence equationsTarun Gehlot
 
Quicksort analysis
Quicksort analysisQuicksort analysis
Quicksort analysisPremjeet Roy
 
270-102-divide-and-conquer_handout.pdfCS 270Algorithm.docx
270-102-divide-and-conquer_handout.pdfCS 270Algorithm.docx270-102-divide-and-conquer_handout.pdfCS 270Algorithm.docx
270-102-divide-and-conquer_handout.pdfCS 270Algorithm.docxeugeniadean34240
 
recurence solutions
recurence solutionsrecurence solutions
recurence solutionssoumya8396
 

Similar to Time complexity (20)

Recurrences
RecurrencesRecurrences
Recurrences
 
04_Recurrences_1.ppt
04_Recurrences_1.ppt04_Recurrences_1.ppt
04_Recurrences_1.ppt
 
3.pdf
3.pdf3.pdf
3.pdf
 
T2311 - Ch 4_Part1.pptx
T2311 - Ch 4_Part1.pptxT2311 - Ch 4_Part1.pptx
T2311 - Ch 4_Part1.pptx
 
Solving recurrences
Solving recurrencesSolving recurrences
Solving recurrences
 
Copy of y16 02-2119divide-and-conquer
Copy of y16 02-2119divide-and-conquerCopy of y16 02-2119divide-and-conquer
Copy of y16 02-2119divide-and-conquer
 
Algorithm Design and Complexity - Course 3
Algorithm Design and Complexity - Course 3Algorithm Design and Complexity - Course 3
Algorithm Design and Complexity - Course 3
 
Lecture 5 6_7 - divide and conquer and method of solving recurrences
Lecture 5 6_7 - divide and conquer and method of solving recurrencesLecture 5 6_7 - divide and conquer and method of solving recurrences
Lecture 5 6_7 - divide and conquer and method of solving recurrences
 
8.-DAA-LECTURE-8-RECURRENCES-AND-ITERATION-METHOD.pdf
8.-DAA-LECTURE-8-RECURRENCES-AND-ITERATION-METHOD.pdf8.-DAA-LECTURE-8-RECURRENCES-AND-ITERATION-METHOD.pdf
8.-DAA-LECTURE-8-RECURRENCES-AND-ITERATION-METHOD.pdf
 
lecture 3
lecture 3lecture 3
lecture 3
 
Analysis Of Algorithms Ii
Analysis Of Algorithms IiAnalysis Of Algorithms Ii
Analysis Of Algorithms Ii
 
Divide and conquer
Divide and conquerDivide and conquer
Divide and conquer
 
Computer science-formulas
Computer science-formulasComputer science-formulas
Computer science-formulas
 
RECURRENCE EQUATIONS & ANALYZING THEM
RECURRENCE EQUATIONS & ANALYZING THEMRECURRENCE EQUATIONS & ANALYZING THEM
RECURRENCE EQUATIONS & ANALYZING THEM
 
Improvised Master's Theorem
Improvised Master's TheoremImprovised Master's Theorem
Improvised Master's Theorem
 
Recurrence equations
Recurrence equationsRecurrence equations
Recurrence equations
 
Recurrences
RecurrencesRecurrences
Recurrences
 
Quicksort analysis
Quicksort analysisQuicksort analysis
Quicksort analysis
 
270-102-divide-and-conquer_handout.pdfCS 270Algorithm.docx
270-102-divide-and-conquer_handout.pdfCS 270Algorithm.docx270-102-divide-and-conquer_handout.pdfCS 270Algorithm.docx
270-102-divide-and-conquer_handout.pdfCS 270Algorithm.docx
 
recurence solutions
recurence solutionsrecurence solutions
recurence solutions
 

Recently uploaded

Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104misteraugie
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.pptRamjanShidvankar
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...christianmathematics
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDThiyagu K
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docxPoojaSen20
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfJayanti Pande
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Disha Kariya
 
An Overview of Mutual Funds Bcom Project.pdf
An Overview of Mutual Funds Bcom Project.pdfAn Overview of Mutual Funds Bcom Project.pdf
An Overview of Mutual Funds Bcom Project.pdfSanaAli374401
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsTechSoup
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 
Gardella_PRCampaignConclusion Pitch Letter
Gardella_PRCampaignConclusion Pitch LetterGardella_PRCampaignConclusion Pitch Letter
Gardella_PRCampaignConclusion Pitch LetterMateoGardella
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxDenish Jangid
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfagholdier
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin ClassesCeline George
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxVishalSingh1417
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactPECB
 

Recently uploaded (20)

Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docx
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..
 
An Overview of Mutual Funds Bcom Project.pdf
An Overview of Mutual Funds Bcom Project.pdfAn Overview of Mutual Funds Bcom Project.pdf
An Overview of Mutual Funds Bcom Project.pdf
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
Gardella_PRCampaignConclusion Pitch Letter
Gardella_PRCampaignConclusion Pitch LetterGardella_PRCampaignConclusion Pitch Letter
Gardella_PRCampaignConclusion Pitch Letter
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptx
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 

Time complexity

  • 1. Table Of Contents Solving Recurrences The Master Theorem
  • 3. Recurrences • The expression: is a recurrence. – Recurrence: an equation that describes a function in terms of its value on smaller functions       >+      = = 1 2 2 1 )( ncn n T nc nT
  • 5. Solving Recurrences • Substitution method • Iteration method • Master method
  • 6. Solving Recurrences • The substitution method (CLR 4.1) – “Making a good guess” method – Guess the form of the answer, then use induction to find the constants and show that solution works – Examples: • T(n) = 2T(n/2) + Θ(n) T(n) = Θ(n lg n) • T(n) = 2T(n/2) + n ???
  • 7. Solving Recurrences • The substitution method (CLR 4.1) – “Making a good guess” method – Guess the form of the answer, then use induction to find the constants and show that solution works – Examples: • T(n) = 2T(n/2) + Θ(n)  T(n) = Θ(n lg n) • T(n) = 2T(n/2) + n  T(n) = Θ(n lg n) • T(n) = 2T(n/2 )+ 17) + n  ???
  • 8. Substitution method • Guess the form of the solution . • Use mathematical induction to find the constants and show that the solution works . The substitution method can be used to establish either upper or lower bounds on a recurrence.
  • 9. An example (Substitution method ) • T(n) = 2T(floor(n/2) ) +n We guess that the solution is T(n)=0(n lg n). i.e. to show that T(n) ≤ cn lg n , for some constant c> 0 and n ≥ m. Assume that this bound holds for [n/2]. So , we get T(n) ≤ 2(c floor (n/2) lg(floor(n/2))) + n ≤ cn lg(n/2) + n = cn lg n – cn lg 2 + n = cn lg n – cn + n ≤ cn lg n where , the last step holds as long as c≥ 1.
  • 10. • Boundary conditions : Suppose , T(1)=1 is the sole boundary condition of the recurrence . then , for n=1 , the bound T(n)≤ c n lg n yields T(1)≤ c lg1=0 , which is at odds with T(1)=1. Thus ,the base case of our inductive proof fails to hold. To overcome this difficulty , we can take advantage of the asymptotic notation which only requires us to prove T(n)≤c n lg n for n≥ m. The idea is to remove the difficult boundary condition T(1)= 1 from consideration. Thus , we can replace T(1) by T(2) as the base cases in the inductive proof , letting m=2.
  • 11. Contd.... From the recurrence , with T(1) = 1, we get T(2)=4 We require T(2)≤ c 2 lg 2 It is clear that , any choice of c≥2 suffices for the base cases
  • 12. Are we done? • Have we proved the case for n =3. • Have we proved that T(3) ≤ c 3 lg 3. • No. Since floor(3/2) = 1 and for n =1, it does not hold. So induction does not apply on n= 3. • From the recurrence, with T(1) = 1, we get T(3) = 5. The inductive proof that T(n) ≤ c n lg n for some constant c≥1 can now be completed by choosing c large enough that T(3)≤c 3 lg 3 also holds. It is clear that , any choice of c ≥ 2 is sufficient for this to hold. Thus we can conclude that T(n) ≤ c n lg n for any c ≥ 2 and n ≥ 2.
  • 13. Solving Recurrences • Another option is “iteration method” – Expand the recurrence – Work some algebra to express as a summation – Evaluate the summation • We will show some examples
  • 14. • Solve the following recurrence: T(n) = T(αn) + T(βn) + n, where 0 < α ≤ β < 1 Assume suitable initial conditions. Assignment 4
  • 15. Recursive Complexity Patterns • When you have simple recurrence relations of the form • T(n) = T(n-1) + f(n), then you have complexity of O(n*f(n)) • T(n) = T(n/k) + f(n), then you have complexity of O(logk n * f(n))
  • 16. • Consider the Fibonacci sequence recursive calculation: • F(1) = 1 F(2) = 1 F(n) = F(n-1)+F(n-2) • Its recurrence relation time is • T(1) = 1 T(2) = 1 T(n) = T(n-1) + T(n-2) + 1 • Solving is a little beyond this course, but T(n) = O(2n ) or "exponential“
  • 17. Consider the recurrence relation T(n) = T(n-1) + T(n/2) + n Which of the following is a good tight upper bound on T(n) (a) Θ n2 (b) Θ(n2logn) (c) Θ(2logn2) (d) Θ(nlogn2)
  • 18. • Lets Solve it by substitution. T(n) = T(n-1) + T(n/2) + n = T(n-2) + T(n/4) + (n-1) + n = T(n-3) + T(n/8) + (n-2) + (n-1) + n = T(n-4) + T(n/16) + (n-3) + (n-2) + (n-1) + n = ------------ = ------------ = ------------- = T(n-k) + T(n/(2^k)) + (n-(k-1)) + (n-(k-2)) + ------------------------------ + (n-2) + (n-1) + n Now consider n = 2^k, then k = log n = T((2^k) - k) + T((2^k)/(2^k)) + (n - k + 1) + (n - k + 2) + ---------------------- + (n - 1) + n Now Put value of K = (2^(log n) - log n) + 1 + (n - log n + 1) + (n - log n + 2) + ------------------------ + n Here series {(n - log n + 1) + (n - log n + 2) + ---------------------- + (n - 1) + n} are in AP with (log n) element then Hence Sn = ((log n)/2) *(n -log n + 1 + n) = O(n.log n) Hence, T(n) = 2^(log n) From all the given answer, Option C is asymptotically close to T(n), Hence C will be the answer.
  • 19. The Master Theorem • Given: a divide and conquer algorithm – An algorithm that divides the problem of size n into a subproblems, each of size n/b – Let the cost of each stage (i.e., the work to divide the problem + combine solved subproblems) be described by the function f(n) • Then, the Master Theorem gives us a cookbook for the algorithm’s running time:
  • 20. The Master Theorem • if T(n) = aT(n/b) + f(n) then ( ) ( ) ( ) ( ) ( ) ( )             < >            < Ω= Θ= = Θ Θ Θ = + − 1 0 largefor)()/( AND)( )( )( )( log)( log log log log log c nncfbnaf nnf nnf nOnf nf nn n nT a a a a a b b b b b ε ε ε
  • 21. Cont.
  • 22. Using The Master Method • T(n) = 9T(n/3) + n – a=9, b=3, f(n) = n – nlogb a = nlog3 9 = Θ(n2 ) – Since f(n) = O(nlog3 9 - ε ), where ε=1, case 1 applies: – Thus the solution is T(n) = Θ(n2 ) ( ) ( )ε− =Θ= aa bb nOnfnnT loglog )(when)(
  • 23. More Examples of Master’s Theorem • T(n) = 3T(n/5) + n o/p nlog35 • T(n) = 2T(n/2) + n o/p : nlogn • T(n) = 2T(n/2) + 1 • T(n) = T(n/2) + n • T(n) = T(n/2) + 1
  • 24. When Master’s Theorem cannot be applied • T(n) = 2T(n/2) + n logn • T(n) = 2T(n/2) + n/ logn
  • 25. Reference • https://en.wikipedia.org/wiki/Master_theorem • http://stackoverflow.com/questions/8185079/how-to-calculate- binary-search-complexity • http://www.csd.uwo.ca/~moreno/CS433- CS9624/Resources/master.pdf • https://en.wikipedia.org/wiki/Time_complexity • http://stackoverflow.com/questions/20512642/big-o-confusion- log2n-vs-log3n