SlideShare a Scribd company logo
Data Structure and Algorithms
Solving recurrences
meghav@kannuruniv.ac.in
Presented by
Megha V
Research scholar
Dept. of IT
Kannur University
Algorithm
Algorithms are of two types:
1. Recursive
2. Non recursive
meghav@kannuruniv.ac.in
Solving Recurrences
Iteration method
Substitution method
Recursion Tree method
Master’s Theorem
meghav@kannuruniv.ac.in
Recurrences and Running Time
• Recursive equation is an equation that defines a
sequence recursively. It is normally in the form:
T(n) = T(n-1) + n for all n≥0
T(0) = 0 initial condition
Recurrences arise when an algorithm contains recursive
calls to itself
•
•
•
What is the actual running time of the algorithm?
Need to solve the recurrence
– Find an explicit formula of the expression
– Bound the recurrence by an expression that involves n
meghav@kannuruniv.ac.in
Example Recurrences
• T(n) = T(n-1) + n Θ(n2)
– Recursive algorithm that loops through the input to
eliminate one item
• T(n) = T(n/2) + c Θ(lgn)
– Recursive algorithm that halves the input in one step
• T(n) = T(n/2) + n Θ(n)
– Recursive algorithm that halves the input but must
examine every item in the input
• T(n) = 2T(n/2) + 1 Θ(n)
– Recursive algorithm that splits the input into 2 halves
and does a constant amount of other work
meghav@kannuruniv.ac.in
Methods for Solving Recurrences
• Iteration method
• Substitution method
• Recursion tree method
• Master method
meghav@kannuruniv.ac.in
The Iteration Method
• Convert the recurrence into a summation and try
to bound it using known series
– Iterate the recurrence until the initial condition is
reached.
– Use back-substitution to express the recurrence in
terms of n and the initial (boundary) condition.
meghav@kannuruniv.ac.in
The Iteration Method
T(n) = c + T(n/2)
T(n) = c + T(n/2)
= c + c + T(n/4)
= c + c + c + T(n/8)
Assume n = 2k
T(n) = c + c + … + c + T(1)
k times
= clgn + T(1)
= Θ(lgn)
T(n/2) = c + T(n/4)
T(n/4) = c + T(n/8)
meghav@kannuruniv.ac.in
Iteration Method – Example
T(n) = n + 2T(n/2)
T(n) = n + 2T(n/2)
= n + 2(n/2 + 2T(n/4))
= n + n + 4T(n/4)
= n + n + 4(n/4 + 2T(n/8))
= n + n + n + 8T(n/8)
… = in + 2iT(n/2i)
= kn + 2kT(1)
= nlgn + nT(1) = Θ(nlgn)
Assume: n = 2k
T(n/2) = n/2 + 2T(n/4)
meghav@kannuruniv.ac.in
The substitution method
1. Guess a solution
2. Use induction to prove that the
solution works
meghav@kannuruniv.ac.in
Substitution method
• Guess a solution
–
–
T(n) = O(g(n))
Induction goal: apply the definition of the asymptotic notation
• T(n) ≤ d g(n), for some d > 0 and n ≥ n0
– Induction hypothesis: T(k) ≤ d g(k) for all k < n
• Prove the induction goal
– Use the induction hypothesis to find some values of the
constants d and n0 for which the induction goal holds
(strong induction)
meghav@kannuruniv.ac.in
Example: Binary Search
T(n) = c + T(n/2)
Guess: T(n) = O(lgn)
•
–
–
Induction goal: T(n) ≤ d lgn, for some d and n ≥ n0
Induction hypothesis: T(n/2) ≤ d lg(n/2)
• Proof of induction goal:
T(n) = T(n/2) + c ≤ d lg(n/2) + c
= d lgn – d + c ≤ d lgn
if: – d + c ≤ 0, d ≥ c
• Base case? meghav@kannuruniv.ac.in
Example 2
T(n) = T(n-1) + n
Guess: T(n) = O(n2)
– Induction goal: T(n) ≤ c n2, for some c and n ≥ n0
•
– Induction hypothesis: T(n-1) ≤ c(n-1)2 for all k < n
• Proof of induction goal:
T(n) = T(n-1) + n ≤ c (n-1)2 + n
= cn2 – (2cn – c - n) ≤ cn2
if: 2cn – c – n ≥ 0  c ≥ n/(2n-1)  c ≥ 1/(2 – 1/n)
– For n ≥ 1  2 – 1/n ≥ 1  any c ≥ 1 will work
meghav@kannuruniv.ac.in
Example 3
T(n) = 2T(n/2) + n
Guess: T(n) = O(nlgn)
•
– Induction goal: T(n) ≤ cn lgn, for some c and n ≥ n0
– Induction hypothesis: T(n/2) ≤ cn/2 lg(n/2)
• Proof of induction goal:
T(n) = 2T(n/2) + n ≤ 2c (n/2)lg(n/2) + n
= cn lgn – cn + n ≤ cn lgn
if: - cn + n ≤ 0  c ≥ 1
meghav@kannuruniv.ac.in
Example 4 - Substitution
T(n) = 3T(n/4) + cn2
• Guess: T(n) = O(n2)
– Induction goal: T(n) ≤ dn2, for some d and n ≥ n0
– Induction hypothesis: T(n/4) ≤ d (n/4)2
• Proof of induction goal:
T(n) = 3T(n/4) + cn2
≤ 3d (n/4)2 + cn2
= (3/16) d n2+ cn2
≤ d n2 if: d ≥ (16/13)c
• Therefore: T(n) = O(n2)
meghav@kannuruniv.ac.in
The recursion-tree method
Convert the recurrence into a tree:
– Each node represents the cost incurred at various
levels of recursion
– Sum up the costs of all levels
Used to “guess” a solution for the recurrence
meghav@kannuruniv.ac.in
Example 1
W(n) = 2W(n/2) + n2
•
•
•
•
Subproblem size at level i is: n/2i
Subproblem size hits 1 when 1 = n/2i i = lgn
Cost of the problem at level i = (n/2i)2 No. of nodes at level i = 2i
Total cost:
2
1
1 1
 1 
i
W (n)    2lgn
W(1)  n2
   n  n2
   O(n) n2
 O(n)  2n2

i0  2 
i0  2 
lgn1
n2 lgn1
 1 
i
i0 2i
 W(n) = O(n2) meghav@kannuruniv.ac.in
Example 2
E.g.: T(n) = 3T(n/4) + cn2
• Subproblem size at level i is: n/4i
• Subproblem size hits 1 when 1 = n/4i  i = log4n
• Cost of a node at level i = c(n/4i)2
• Number of nodes at level i = 3i  last level has 3log
4
n = nlog
4
3 nodes
• Total cost:
 T(n) = O(n2) 16
1
16
16
2
 O(n )
2
2
2 log4 3

log4 3
 log4 3

1
  cn  n
3
cn   n

 
 3 
i
cn  n 
 
 
T(n)  


i0 
log4 n1
 3 
i
i0
Master’s method
• solving recurrences of the form:
Idea: compare f(n) with 𝑛𝑙𝑜𝑔𝑏𝑎
• f(n) is asymptotically smaller or larger than 𝑛𝑙𝑜𝑔𝑏𝑎
by a polynomial factor n
• f(n) is asymptotically equal with 𝑛𝑙𝑜𝑔𝑏𝑎

b

 
where, a ≥ 1, b > 1, and f(n) > 0
T(n)  aT n   f (n)
meghav@kannuruniv.ac.in
Master’s method
Case 1: if f(n) = 𝑂(𝑛𝑙𝑜𝑔𝑏𝑎−)for some  > 0, then: T(n) = (𝑛𝑙𝑜𝑔𝑏𝑎 )
Case 2: if f(n) = (𝑛𝑙𝑜𝑔𝑏𝑎), then: T(n) = (𝑛𝑙𝑜𝑔𝑏𝑎 lgn)
Case 3: if f(n) = (𝑛𝑙𝑜𝑔𝑏𝑎+) for some  > 0, and if
af(n/b) ≤ cf(n) for some c < 1 and all sufficiently large n, then:
T(n) = (f(n))
meghav@kannuruniv.ac.in
Example 1
T(n) = 2T(n/2) + n
a = 2, b = 2, log22 = 1
Compare 𝑛𝑙𝑜𝑔22 with f(n) = n
 f(n) = (n)  Case 2
 T(n) = (nlgn)
meghav@kannuruniv.ac.in
Example 2
T(n) = 2T(n/2) + n2
a = 2, b = 2, log22 = 1
Compare n with f(n) = n2
 f(n) = (n1+) Case 3  verify regularity cond.
a f(n/b) ≤ c f(n)
 2 n2/4 ≤ c n2  c = ½ is a solution (c<1)
 T(n) = (n2)
meghav@kannuruniv.ac.in
Examples 3
T(n) = 2T(n/2) +
a = 2, b = 2, log22 = 1
Compare n with f(n) = n1/2
 f(n) = O(n1-) Case 1
 T(n) = (n)
n
meghav@kannuruniv.ac.in
Examples
T(n) = 3T(n/4) + nlgn
a = 3, b = 4, log43 = 0.793
Compare n0.793with f(n) = nlgn
f(n) = (nlog
4
3+ ) Case 3
Check regularity condition:
3(n/4)lg(n/4) ≤ (3/4)nlgn = c f(n), c=3/4
T(n) = (nlgn)
meghav@kannuruniv.ac.in
Assignment
Solve the following recurrence using Master’s method
1. T(n) = 9T(n/3)+n
2. T(n) = 16T(n/4)+n3
3. T(n) = 8T(n/2)+n2
4. T(n) = 2T(n/2)+n
meghav@kannuruniv.ac.in

More Related Content

What's hot

02 order of growth
02 order of growth02 order of growth
02 order of growth
Hira Gul
 
Algorithm analysis
Algorithm analysisAlgorithm analysis
Algorithm analysissumitbardhan
 
Divide and Conquer
Divide and ConquerDivide and Conquer
Divide and Conquer
Dr Shashikant Athawale
 
8 queens problem using back tracking
8 queens problem using back tracking8 queens problem using back tracking
8 queens problem using back trackingTech_MX
 
Insertion sort bubble sort selection sort
Insertion sort bubble sort  selection sortInsertion sort bubble sort  selection sort
Insertion sort bubble sort selection sort
Ummar Hayat
 
Mathematical Analysis of Recursive Algorithm.
Mathematical Analysis of Recursive Algorithm.Mathematical Analysis of Recursive Algorithm.
Mathematical Analysis of Recursive Algorithm.
mohanrathod18
 
Analysis of algorithm
Analysis of algorithmAnalysis of algorithm
Analysis of algorithm
Rajendra Dangwal
 
Time and space complexity
Time and space complexityTime and space complexity
Time and space complexityAnkit Katiyar
 
Sum of subset problem.pptx
Sum of subset problem.pptxSum of subset problem.pptx
Sum of subset problem.pptx
V.V.Vanniaperumal College for Women
 
Asymptotic notations
Asymptotic notationsAsymptotic notations
Asymptotic notationsNikhil Sharma
 
BackTracking Algorithm: Technique and Examples
BackTracking Algorithm: Technique and ExamplesBackTracking Algorithm: Technique and Examples
BackTracking Algorithm: Technique and Examples
Fahim Ferdous
 
Divide and conquer
Divide and conquerDivide and conquer
Divide and conquer
Dr Shashikant Athawale
 
Data Structures- Part5 recursion
Data Structures- Part5 recursionData Structures- Part5 recursion
Data Structures- Part5 recursion
Abdullah Al-hazmy
 
Design & Analysis of Algorithms Lecture Notes
Design & Analysis of Algorithms Lecture NotesDesign & Analysis of Algorithms Lecture Notes
Design & Analysis of Algorithms Lecture Notes
FellowBuddy.com
 
Finite Automata
Finite AutomataFinite Automata
Finite Automata
Mukesh Tekwani
 
Dinive conquer algorithm
Dinive conquer algorithmDinive conquer algorithm
Dinive conquer algorithmMohd Arif
 
Data structure tries
Data structure triesData structure tries
Data structure tries
Md. Naim khan
 
Analysis of algorithms
Analysis of algorithmsAnalysis of algorithms
Analysis of algorithms
Ganesh Solanke
 
N queen problem
N queen problemN queen problem
N queen problem
Ridhima Chowdhury
 

What's hot (20)

02 order of growth
02 order of growth02 order of growth
02 order of growth
 
Algorithm analysis
Algorithm analysisAlgorithm analysis
Algorithm analysis
 
Divide and Conquer
Divide and ConquerDivide and Conquer
Divide and Conquer
 
8 queens problem using back tracking
8 queens problem using back tracking8 queens problem using back tracking
8 queens problem using back tracking
 
Insertion sort bubble sort selection sort
Insertion sort bubble sort  selection sortInsertion sort bubble sort  selection sort
Insertion sort bubble sort selection sort
 
Mathematical Analysis of Recursive Algorithm.
Mathematical Analysis of Recursive Algorithm.Mathematical Analysis of Recursive Algorithm.
Mathematical Analysis of Recursive Algorithm.
 
Analysis of algorithm
Analysis of algorithmAnalysis of algorithm
Analysis of algorithm
 
Time and space complexity
Time and space complexityTime and space complexity
Time and space complexity
 
Sum of subset problem.pptx
Sum of subset problem.pptxSum of subset problem.pptx
Sum of subset problem.pptx
 
Asymptotic notations
Asymptotic notationsAsymptotic notations
Asymptotic notations
 
BackTracking Algorithm: Technique and Examples
BackTracking Algorithm: Technique and ExamplesBackTracking Algorithm: Technique and Examples
BackTracking Algorithm: Technique and Examples
 
Divide and conquer
Divide and conquerDivide and conquer
Divide and conquer
 
Data Structures- Part5 recursion
Data Structures- Part5 recursionData Structures- Part5 recursion
Data Structures- Part5 recursion
 
Backtracking
BacktrackingBacktracking
Backtracking
 
Design & Analysis of Algorithms Lecture Notes
Design & Analysis of Algorithms Lecture NotesDesign & Analysis of Algorithms Lecture Notes
Design & Analysis of Algorithms Lecture Notes
 
Finite Automata
Finite AutomataFinite Automata
Finite Automata
 
Dinive conquer algorithm
Dinive conquer algorithmDinive conquer algorithm
Dinive conquer algorithm
 
Data structure tries
Data structure triesData structure tries
Data structure tries
 
Analysis of algorithms
Analysis of algorithmsAnalysis of algorithms
Analysis of algorithms
 
N queen problem
N queen problemN queen problem
N queen problem
 

Similar to Solving recurrences

T2311 - Ch 4_Part1.pptx
T2311 - Ch 4_Part1.pptxT2311 - Ch 4_Part1.pptx
T2311 - Ch 4_Part1.pptx
GadaFarhan
 
3.pdf
3.pdf3.pdf
3.pdf
AlaaOdeh18
 
CS330-Lectures Statistics And Probability
CS330-Lectures Statistics And ProbabilityCS330-Lectures Statistics And Probability
CS330-Lectures Statistics And Probability
bryan111472
 
2.pptx
2.pptx2.pptx
2.pptx
MohAlyasin1
 
Recurrences
RecurrencesRecurrences
Recurrences
DEVTYPE
 
Time complexity
Time complexityTime complexity
Time complexity
Kartik Chandra Mandal
 
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
RishikeshJha33
 
04_Recurrences_1.ppt
04_Recurrences_1.ppt04_Recurrences_1.ppt
04_Recurrences_1.ppt
MouDhara1
 
Divide and conquer
Divide and conquerDivide and conquer
Divide and conquer
Vikas Sharma
 
Algorithm Design and Complexity - Course 3
Algorithm Design and Complexity - Course 3Algorithm Design and Complexity - Course 3
Algorithm Design and Complexity - Course 3Traian Rebedea
 
Recurrence relation
Recurrence relationRecurrence relation
Recurrence relation
Ajay Chimmani
 
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
 
Computer science-formulas
Computer science-formulasComputer science-formulas
Computer science-formulas
RAJIV GANDHI INSTITUTE OF TECHNOLOGY
 
Quicksort analysis
Quicksort analysisQuicksort analysis
Quicksort analysisPremjeet Roy
 
Admission in india 2015
Admission in india 2015Admission in india 2015
Admission in india 2015
Edhole.com
 
RECURRENCE EQUATIONS & ANALYZING THEM
RECURRENCE EQUATIONS & ANALYZING THEMRECURRENCE EQUATIONS & ANALYZING THEM
RECURRENCE EQUATIONS & ANALYZING THEM
Alpana Ingale
 

Similar to Solving recurrences (20)

T2311 - Ch 4_Part1.pptx
T2311 - Ch 4_Part1.pptxT2311 - Ch 4_Part1.pptx
T2311 - Ch 4_Part1.pptx
 
Recurrences
RecurrencesRecurrences
Recurrences
 
3.pdf
3.pdf3.pdf
3.pdf
 
CS330-Lectures Statistics And Probability
CS330-Lectures Statistics And ProbabilityCS330-Lectures Statistics And Probability
CS330-Lectures Statistics And Probability
 
2.pptx
2.pptx2.pptx
2.pptx
 
Recurrences
RecurrencesRecurrences
Recurrences
 
Time complexity
Time complexityTime complexity
Time complexity
 
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
 
04_Recurrences_1.ppt
04_Recurrences_1.ppt04_Recurrences_1.ppt
04_Recurrences_1.ppt
 
Divide and conquer
Divide and conquerDivide and conquer
Divide 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
 
Recurrence relation
Recurrence relationRecurrence relation
Recurrence relation
 
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
 
L2
L2L2
L2
 
Computer science-formulas
Computer science-formulasComputer science-formulas
Computer science-formulas
 
Slide2
Slide2Slide2
Slide2
 
Quicksort analysis
Quicksort analysisQuicksort analysis
Quicksort analysis
 
Admission in india 2015
Admission in india 2015Admission in india 2015
Admission in india 2015
 
RECURRENCE EQUATIONS & ANALYZING THEM
RECURRENCE EQUATIONS & ANALYZING THEMRECURRENCE EQUATIONS & ANALYZING THEM
RECURRENCE EQUATIONS & ANALYZING THEM
 

More from Megha V

Soft Computing Techniques_Part 1.pptx
Soft Computing Techniques_Part 1.pptxSoft Computing Techniques_Part 1.pptx
Soft Computing Techniques_Part 1.pptx
Megha V
 
JavaScript- Functions and arrays.pptx
JavaScript- Functions and arrays.pptxJavaScript- Functions and arrays.pptx
JavaScript- Functions and arrays.pptx
Megha V
 
Introduction to JavaScript
Introduction to JavaScriptIntroduction to JavaScript
Introduction to JavaScript
Megha V
 
Python Exception Handling
Python Exception HandlingPython Exception Handling
Python Exception Handling
Megha V
 
Python- Regular expression
Python- Regular expressionPython- Regular expression
Python- Regular expression
Megha V
 
File handling in Python
File handling in PythonFile handling in Python
File handling in Python
Megha V
 
Python programming -Tuple and Set Data type
Python programming -Tuple and Set Data typePython programming -Tuple and Set Data type
Python programming -Tuple and Set Data type
Megha V
 
Python programming –part 7
Python programming –part 7Python programming –part 7
Python programming –part 7
Megha V
 
Python programming Part -6
Python programming Part -6Python programming Part -6
Python programming Part -6
Megha V
 
Python programming: Anonymous functions, String operations
Python programming: Anonymous functions, String operationsPython programming: Anonymous functions, String operations
Python programming: Anonymous functions, String operations
Megha V
 
Python programming- Part IV(Functions)
Python programming- Part IV(Functions)Python programming- Part IV(Functions)
Python programming- Part IV(Functions)
Megha V
 
Python programming –part 3
Python programming –part 3Python programming –part 3
Python programming –part 3
Megha V
 
Parts of python programming language
Parts of python programming languageParts of python programming language
Parts of python programming language
Megha V
 
Python programming
Python programmingPython programming
Python programming
Megha V
 
Strassen's matrix multiplication
Strassen's matrix multiplicationStrassen's matrix multiplication
Strassen's matrix multiplication
Megha V
 
Algorithm Analysis
Algorithm AnalysisAlgorithm Analysis
Algorithm Analysis
Megha V
 
Algorithm analysis and design
Algorithm analysis and designAlgorithm analysis and design
Algorithm analysis and design
Megha V
 
Genetic algorithm
Genetic algorithmGenetic algorithm
Genetic algorithm
Megha V
 
UGC NET Paper 1 ICT Memory and data
UGC NET Paper 1 ICT Memory and data  UGC NET Paper 1 ICT Memory and data
UGC NET Paper 1 ICT Memory and data
Megha V
 
Seminar presentation on OpenGL
Seminar presentation on OpenGLSeminar presentation on OpenGL
Seminar presentation on OpenGL
Megha V
 

More from Megha V (20)

Soft Computing Techniques_Part 1.pptx
Soft Computing Techniques_Part 1.pptxSoft Computing Techniques_Part 1.pptx
Soft Computing Techniques_Part 1.pptx
 
JavaScript- Functions and arrays.pptx
JavaScript- Functions and arrays.pptxJavaScript- Functions and arrays.pptx
JavaScript- Functions and arrays.pptx
 
Introduction to JavaScript
Introduction to JavaScriptIntroduction to JavaScript
Introduction to JavaScript
 
Python Exception Handling
Python Exception HandlingPython Exception Handling
Python Exception Handling
 
Python- Regular expression
Python- Regular expressionPython- Regular expression
Python- Regular expression
 
File handling in Python
File handling in PythonFile handling in Python
File handling in Python
 
Python programming -Tuple and Set Data type
Python programming -Tuple and Set Data typePython programming -Tuple and Set Data type
Python programming -Tuple and Set Data type
 
Python programming –part 7
Python programming –part 7Python programming –part 7
Python programming –part 7
 
Python programming Part -6
Python programming Part -6Python programming Part -6
Python programming Part -6
 
Python programming: Anonymous functions, String operations
Python programming: Anonymous functions, String operationsPython programming: Anonymous functions, String operations
Python programming: Anonymous functions, String operations
 
Python programming- Part IV(Functions)
Python programming- Part IV(Functions)Python programming- Part IV(Functions)
Python programming- Part IV(Functions)
 
Python programming –part 3
Python programming –part 3Python programming –part 3
Python programming –part 3
 
Parts of python programming language
Parts of python programming languageParts of python programming language
Parts of python programming language
 
Python programming
Python programmingPython programming
Python programming
 
Strassen's matrix multiplication
Strassen's matrix multiplicationStrassen's matrix multiplication
Strassen's matrix multiplication
 
Algorithm Analysis
Algorithm AnalysisAlgorithm Analysis
Algorithm Analysis
 
Algorithm analysis and design
Algorithm analysis and designAlgorithm analysis and design
Algorithm analysis and design
 
Genetic algorithm
Genetic algorithmGenetic algorithm
Genetic algorithm
 
UGC NET Paper 1 ICT Memory and data
UGC NET Paper 1 ICT Memory and data  UGC NET Paper 1 ICT Memory and data
UGC NET Paper 1 ICT Memory and data
 
Seminar presentation on OpenGL
Seminar presentation on OpenGLSeminar presentation on OpenGL
Seminar presentation on OpenGL
 

Recently uploaded

Orion Context Broker introduction 20240604
Orion Context Broker introduction 20240604Orion Context Broker introduction 20240604
Orion Context Broker introduction 20240604
Fermin Galan
 
top nidhi software solution freedownload
top nidhi software solution freedownloadtop nidhi software solution freedownload
top nidhi software solution freedownload
vrstrong314
 
How to Position Your Globus Data Portal for Success Ten Good Practices
How to Position Your Globus Data Portal for Success Ten Good PracticesHow to Position Your Globus Data Portal for Success Ten Good Practices
How to Position Your Globus Data Portal for Success Ten Good Practices
Globus
 
Lecture 1 Introduction to games development
Lecture 1 Introduction to games developmentLecture 1 Introduction to games development
Lecture 1 Introduction to games development
abdulrafaychaudhry
 
Quarkus Hidden and Forbidden Extensions
Quarkus Hidden and Forbidden ExtensionsQuarkus Hidden and Forbidden Extensions
Quarkus Hidden and Forbidden Extensions
Max Andersen
 
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
Juraj Vysvader
 
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdfDominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
AMB-Review
 
Using IESVE for Room Loads Analysis - Australia & New Zealand
Using IESVE for Room Loads Analysis - Australia & New ZealandUsing IESVE for Room Loads Analysis - Australia & New Zealand
Using IESVE for Room Loads Analysis - Australia & New Zealand
IES VE
 
Vitthal Shirke Microservices Resume Montevideo
Vitthal Shirke Microservices Resume MontevideoVitthal Shirke Microservices Resume Montevideo
Vitthal Shirke Microservices Resume Montevideo
Vitthal Shirke
 
Navigating the Metaverse: A Journey into Virtual Evolution"
Navigating the Metaverse: A Journey into Virtual Evolution"Navigating the Metaverse: A Journey into Virtual Evolution"
Navigating the Metaverse: A Journey into Virtual Evolution"
Donna Lenk
 
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, BetterWebinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
XfilesPro
 
BoxLang: Review our Visionary Licenses of 2024
BoxLang: Review our Visionary Licenses of 2024BoxLang: Review our Visionary Licenses of 2024
BoxLang: Review our Visionary Licenses of 2024
Ortus Solutions, Corp
 
Top Features to Include in Your Winzo Clone App for Business Growth (4).pptx
Top Features to Include in Your Winzo Clone App for Business Growth (4).pptxTop Features to Include in Your Winzo Clone App for Business Growth (4).pptx
Top Features to Include in Your Winzo Clone App for Business Growth (4).pptx
rickgrimesss22
 
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoamOpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
takuyayamamoto1800
 
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.ILBeyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
Natan Silnitsky
 
Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
Globus
 
Globus Compute Introduction - GlobusWorld 2024
Globus Compute Introduction - GlobusWorld 2024Globus Compute Introduction - GlobusWorld 2024
Globus Compute Introduction - GlobusWorld 2024
Globus
 
Enterprise Resource Planning System in Telangana
Enterprise Resource Planning System in TelanganaEnterprise Resource Planning System in Telangana
Enterprise Resource Planning System in Telangana
NYGGS Automation Suite
 
A Sighting of filterA in Typelevel Rite of Passage
A Sighting of filterA in Typelevel Rite of PassageA Sighting of filterA in Typelevel Rite of Passage
A Sighting of filterA in Typelevel Rite of Passage
Philip Schwarz
 
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
Globus
 

Recently uploaded (20)

Orion Context Broker introduction 20240604
Orion Context Broker introduction 20240604Orion Context Broker introduction 20240604
Orion Context Broker introduction 20240604
 
top nidhi software solution freedownload
top nidhi software solution freedownloadtop nidhi software solution freedownload
top nidhi software solution freedownload
 
How to Position Your Globus Data Portal for Success Ten Good Practices
How to Position Your Globus Data Portal for Success Ten Good PracticesHow to Position Your Globus Data Portal for Success Ten Good Practices
How to Position Your Globus Data Portal for Success Ten Good Practices
 
Lecture 1 Introduction to games development
Lecture 1 Introduction to games developmentLecture 1 Introduction to games development
Lecture 1 Introduction to games development
 
Quarkus Hidden and Forbidden Extensions
Quarkus Hidden and Forbidden ExtensionsQuarkus Hidden and Forbidden Extensions
Quarkus Hidden and Forbidden Extensions
 
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
 
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdfDominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
 
Using IESVE for Room Loads Analysis - Australia & New Zealand
Using IESVE for Room Loads Analysis - Australia & New ZealandUsing IESVE for Room Loads Analysis - Australia & New Zealand
Using IESVE for Room Loads Analysis - Australia & New Zealand
 
Vitthal Shirke Microservices Resume Montevideo
Vitthal Shirke Microservices Resume MontevideoVitthal Shirke Microservices Resume Montevideo
Vitthal Shirke Microservices Resume Montevideo
 
Navigating the Metaverse: A Journey into Virtual Evolution"
Navigating the Metaverse: A Journey into Virtual Evolution"Navigating the Metaverse: A Journey into Virtual Evolution"
Navigating the Metaverse: A Journey into Virtual Evolution"
 
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, BetterWebinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
 
BoxLang: Review our Visionary Licenses of 2024
BoxLang: Review our Visionary Licenses of 2024BoxLang: Review our Visionary Licenses of 2024
BoxLang: Review our Visionary Licenses of 2024
 
Top Features to Include in Your Winzo Clone App for Business Growth (4).pptx
Top Features to Include in Your Winzo Clone App for Business Growth (4).pptxTop Features to Include in Your Winzo Clone App for Business Growth (4).pptx
Top Features to Include in Your Winzo Clone App for Business Growth (4).pptx
 
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoamOpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
 
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.ILBeyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
 
Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
 
Globus Compute Introduction - GlobusWorld 2024
Globus Compute Introduction - GlobusWorld 2024Globus Compute Introduction - GlobusWorld 2024
Globus Compute Introduction - GlobusWorld 2024
 
Enterprise Resource Planning System in Telangana
Enterprise Resource Planning System in TelanganaEnterprise Resource Planning System in Telangana
Enterprise Resource Planning System in Telangana
 
A Sighting of filterA in Typelevel Rite of Passage
A Sighting of filterA in Typelevel Rite of PassageA Sighting of filterA in Typelevel Rite of Passage
A Sighting of filterA in Typelevel Rite of Passage
 
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
 

Solving recurrences

  • 1. Data Structure and Algorithms Solving recurrences meghav@kannuruniv.ac.in Presented by Megha V Research scholar Dept. of IT Kannur University
  • 2. Algorithm Algorithms are of two types: 1. Recursive 2. Non recursive meghav@kannuruniv.ac.in
  • 3. Solving Recurrences Iteration method Substitution method Recursion Tree method Master’s Theorem meghav@kannuruniv.ac.in
  • 4. Recurrences and Running Time • Recursive equation is an equation that defines a sequence recursively. It is normally in the form: T(n) = T(n-1) + n for all n≥0 T(0) = 0 initial condition Recurrences arise when an algorithm contains recursive calls to itself • • • What is the actual running time of the algorithm? Need to solve the recurrence – Find an explicit formula of the expression – Bound the recurrence by an expression that involves n meghav@kannuruniv.ac.in
  • 5. Example Recurrences • T(n) = T(n-1) + n Θ(n2) – Recursive algorithm that loops through the input to eliminate one item • T(n) = T(n/2) + c Θ(lgn) – Recursive algorithm that halves the input in one step • T(n) = T(n/2) + n Θ(n) – Recursive algorithm that halves the input but must examine every item in the input • T(n) = 2T(n/2) + 1 Θ(n) – Recursive algorithm that splits the input into 2 halves and does a constant amount of other work meghav@kannuruniv.ac.in
  • 6. Methods for Solving Recurrences • Iteration method • Substitution method • Recursion tree method • Master method meghav@kannuruniv.ac.in
  • 7. The Iteration Method • Convert the recurrence into a summation and try to bound it using known series – Iterate the recurrence until the initial condition is reached. – Use back-substitution to express the recurrence in terms of n and the initial (boundary) condition. meghav@kannuruniv.ac.in
  • 8. The Iteration Method T(n) = c + T(n/2) T(n) = c + T(n/2) = c + c + T(n/4) = c + c + c + T(n/8) Assume n = 2k T(n) = c + c + … + c + T(1) k times = clgn + T(1) = Θ(lgn) T(n/2) = c + T(n/4) T(n/4) = c + T(n/8) meghav@kannuruniv.ac.in
  • 9. Iteration Method – Example T(n) = n + 2T(n/2) T(n) = n + 2T(n/2) = n + 2(n/2 + 2T(n/4)) = n + n + 4T(n/4) = n + n + 4(n/4 + 2T(n/8)) = n + n + n + 8T(n/8) … = in + 2iT(n/2i) = kn + 2kT(1) = nlgn + nT(1) = Θ(nlgn) Assume: n = 2k T(n/2) = n/2 + 2T(n/4) meghav@kannuruniv.ac.in
  • 10. The substitution method 1. Guess a solution 2. Use induction to prove that the solution works meghav@kannuruniv.ac.in
  • 11. Substitution method • Guess a solution – – T(n) = O(g(n)) Induction goal: apply the definition of the asymptotic notation • T(n) ≤ d g(n), for some d > 0 and n ≥ n0 – Induction hypothesis: T(k) ≤ d g(k) for all k < n • Prove the induction goal – Use the induction hypothesis to find some values of the constants d and n0 for which the induction goal holds (strong induction) meghav@kannuruniv.ac.in
  • 12. Example: Binary Search T(n) = c + T(n/2) Guess: T(n) = O(lgn) • – – Induction goal: T(n) ≤ d lgn, for some d and n ≥ n0 Induction hypothesis: T(n/2) ≤ d lg(n/2) • Proof of induction goal: T(n) = T(n/2) + c ≤ d lg(n/2) + c = d lgn – d + c ≤ d lgn if: – d + c ≤ 0, d ≥ c • Base case? meghav@kannuruniv.ac.in
  • 13. Example 2 T(n) = T(n-1) + n Guess: T(n) = O(n2) – Induction goal: T(n) ≤ c n2, for some c and n ≥ n0 • – Induction hypothesis: T(n-1) ≤ c(n-1)2 for all k < n • Proof of induction goal: T(n) = T(n-1) + n ≤ c (n-1)2 + n = cn2 – (2cn – c - n) ≤ cn2 if: 2cn – c – n ≥ 0  c ≥ n/(2n-1)  c ≥ 1/(2 – 1/n) – For n ≥ 1  2 – 1/n ≥ 1  any c ≥ 1 will work meghav@kannuruniv.ac.in
  • 14. Example 3 T(n) = 2T(n/2) + n Guess: T(n) = O(nlgn) • – Induction goal: T(n) ≤ cn lgn, for some c and n ≥ n0 – Induction hypothesis: T(n/2) ≤ cn/2 lg(n/2) • Proof of induction goal: T(n) = 2T(n/2) + n ≤ 2c (n/2)lg(n/2) + n = cn lgn – cn + n ≤ cn lgn if: - cn + n ≤ 0  c ≥ 1 meghav@kannuruniv.ac.in
  • 15. Example 4 - Substitution T(n) = 3T(n/4) + cn2 • Guess: T(n) = O(n2) – Induction goal: T(n) ≤ dn2, for some d and n ≥ n0 – Induction hypothesis: T(n/4) ≤ d (n/4)2 • Proof of induction goal: T(n) = 3T(n/4) + cn2 ≤ 3d (n/4)2 + cn2 = (3/16) d n2+ cn2 ≤ d n2 if: d ≥ (16/13)c • Therefore: T(n) = O(n2) meghav@kannuruniv.ac.in
  • 16. The recursion-tree method Convert the recurrence into a tree: – Each node represents the cost incurred at various levels of recursion – Sum up the costs of all levels Used to “guess” a solution for the recurrence meghav@kannuruniv.ac.in
  • 17. Example 1 W(n) = 2W(n/2) + n2 • • • • Subproblem size at level i is: n/2i Subproblem size hits 1 when 1 = n/2i i = lgn Cost of the problem at level i = (n/2i)2 No. of nodes at level i = 2i Total cost: 2 1 1 1  1  i W (n)    2lgn W(1)  n2    n  n2    O(n) n2  O(n)  2n2  i0  2  i0  2  lgn1 n2 lgn1  1  i i0 2i  W(n) = O(n2) meghav@kannuruniv.ac.in
  • 18. Example 2 E.g.: T(n) = 3T(n/4) + cn2 • Subproblem size at level i is: n/4i • Subproblem size hits 1 when 1 = n/4i  i = log4n • Cost of a node at level i = c(n/4i)2 • Number of nodes at level i = 3i  last level has 3log 4 n = nlog 4 3 nodes • Total cost:  T(n) = O(n2) 16 1 16 16 2  O(n ) 2 2 2 log4 3  log4 3  log4 3  1   cn  n 3 cn   n     3  i cn  n      T(n)     i0  log4 n1  3  i i0
  • 19. Master’s method • solving recurrences of the form: Idea: compare f(n) with 𝑛𝑙𝑜𝑔𝑏𝑎 • f(n) is asymptotically smaller or larger than 𝑛𝑙𝑜𝑔𝑏𝑎 by a polynomial factor n • f(n) is asymptotically equal with 𝑛𝑙𝑜𝑔𝑏𝑎  b    where, a ≥ 1, b > 1, and f(n) > 0 T(n)  aT n   f (n) meghav@kannuruniv.ac.in
  • 20. Master’s method Case 1: if f(n) = 𝑂(𝑛𝑙𝑜𝑔𝑏𝑎−)for some  > 0, then: T(n) = (𝑛𝑙𝑜𝑔𝑏𝑎 ) Case 2: if f(n) = (𝑛𝑙𝑜𝑔𝑏𝑎), then: T(n) = (𝑛𝑙𝑜𝑔𝑏𝑎 lgn) Case 3: if f(n) = (𝑛𝑙𝑜𝑔𝑏𝑎+) for some  > 0, and if af(n/b) ≤ cf(n) for some c < 1 and all sufficiently large n, then: T(n) = (f(n)) meghav@kannuruniv.ac.in
  • 21. Example 1 T(n) = 2T(n/2) + n a = 2, b = 2, log22 = 1 Compare 𝑛𝑙𝑜𝑔22 with f(n) = n  f(n) = (n)  Case 2  T(n) = (nlgn) meghav@kannuruniv.ac.in
  • 22. Example 2 T(n) = 2T(n/2) + n2 a = 2, b = 2, log22 = 1 Compare n with f(n) = n2  f(n) = (n1+) Case 3  verify regularity cond. a f(n/b) ≤ c f(n)  2 n2/4 ≤ c n2  c = ½ is a solution (c<1)  T(n) = (n2) meghav@kannuruniv.ac.in
  • 23. Examples 3 T(n) = 2T(n/2) + a = 2, b = 2, log22 = 1 Compare n with f(n) = n1/2  f(n) = O(n1-) Case 1  T(n) = (n) n meghav@kannuruniv.ac.in
  • 24. Examples T(n) = 3T(n/4) + nlgn a = 3, b = 4, log43 = 0.793 Compare n0.793with f(n) = nlgn f(n) = (nlog 4 3+ ) Case 3 Check regularity condition: 3(n/4)lg(n/4) ≤ (3/4)nlgn = c f(n), c=3/4 T(n) = (nlgn) meghav@kannuruniv.ac.in
  • 25. Assignment Solve the following recurrence using Master’s method 1. T(n) = 9T(n/3)+n 2. T(n) = 16T(n/4)+n3 3. T(n) = 8T(n/2)+n2 4. T(n) = 2T(n/2)+n meghav@kannuruniv.ac.in