SlideShare a Scribd company logo
Recurrence Relations and Big
O
How do we compute the costs of running our code
Recursive Functions (Math Ones)
• Similar to coding, a recursive function in math is something that calls
itself.
• We have a base case(s), and the recursive state.
• Example: T(1)=1 ,T(2)=1, Tn= T(n-1)+T(n-2) for n>=3
• A pretty comprehensive topic. However we want to focus on Software
Engineering, so we will look at them wrt to understanding asymptotic
complexities.
Building it (and use)
• We can take the code. Any base case in the base-case directly
translates as a base case in our math formulation.
• Our recursive case translates directly as well.
• We substitute values till we hit a base case.
• Add the number of operations.
• OR we draw a tree.
• And We add. They are the same process, so it depends on what you
prefer.
Example 1: T(1)=1 ,T(2)=1, Tn= T(n-1)+T(n-2)
for n>=3. Calculating complexity
• T(n)= T(n-1)+T(n-2)
• = T(n-2)+T(n-3)+T(n-3)+T(n-4)
• = …
• We have constantly substituted the recurrence for one lower.
Eventually we want to get to base case.
• We can see a pattern form. Each higher level function call splits into 2
lower level ones (till we hit the base case). T(n)-> T(n-1) +T(n-2);
T(n-1)->T(n-2)+T(n-3) … T(n-(n-3))->T(2) +T(1) where we hit basecase
Example 1: Sketching our tree
Example 1: Code
• function F1(n){
• if(n==1 || n==2){
• return 1
• }else{
• return F1(n-1)+F1(n-2)
• }
• }
Calculating the Time Complexity (COST)
• We can see that the function calls itself Twice per recursive call (our
sketch of this pattern has 2 branches from every level).
• So the number of calls for T(n) is 2*T(n-1). NOTE: We are talking
about number of calls not cost.
• Each function call has 1 operation (the addition). Base cases have the
return.
• So how many calls do we have??
• What is the cost? Can you think of a generalized formula
Big O analysis
• Definition: f(n) = O(g(n)) if there exists a positive integer n0 and a
positive constant c, such that f(n)≤c*g(n) ∀ n≥n0
• It states the following. Imagine we have 2 functions f and g. f is in the
Big O of g if we can find an n0 and c such that f(n)≤c*g(n) ∀ n≥n0.
• Essentially, we can say that f is in the Big O of n if we can show that g
will be greater than f for all values more than n.
Simple way to calculate Costs
• Number of Function Calls * Operations per call (general outline for
calculations).
• This is where understanding Recurrence Relationships come into play
• RRs can help you strip the code down into essentials. Helpful for
figuring out function calls.
• Think of the problems we’ve already done. What do you think of their
costs? How would you split their number of function calls and
operations per call?
Your Interesting Question
• https://www.hackerrank.com/challenges/jumping-on-the-
clouds/problem
• Try to break down this problem into cases and solve.
• Lmk if you need help with something

More Related Content

PPT
03 mathematical anaylsis
PPTX
Mathematical Analysis of Non-Recursive Algorithm.
PPTX
Asymptotic analysis
PPTX
Algorithms required for data structures(basics like Arrays, Stacks ,Linked Li...
PPTX
Recursion
PPTX
Algorithm analysis
PPTX
Divide and conquer strategy
PPTX
Asymptotic notation ada
03 mathematical anaylsis
Mathematical Analysis of Non-Recursive Algorithm.
Asymptotic analysis
Algorithms required for data structures(basics like Arrays, Stacks ,Linked Li...
Recursion
Algorithm analysis
Divide and conquer strategy
Asymptotic notation ada

What's hot (20)

PPT
Recursion - Algorithms and Data Structures
PPT
Algorithms with-java-advanced-1.0
PPTX
Introduction of calculus in programming
PPTX
Numerical on dichotomous search
PPTX
Lecture 3 time complexity
PPTX
Lecture 3 complexity
PPTX
Numerical on bisection method
PPTX
Pointer arithmetic in c
PPTX
Beginning direct3d gameprogrammingmath02_logarithm_20160324_jintaeks
PPTX
segment tree algorithm.pptx
PPTX
Daa unit 5
PPT
Lecture11
DOCX
Code Optimization in MIPS
PPT
Divide and conquer
PPTX
Divide and conquer
PDF
PDF
Convolution
PDF
CBSE Python Dictionary Assignment
Recursion - Algorithms and Data Structures
Algorithms with-java-advanced-1.0
Introduction of calculus in programming
Numerical on dichotomous search
Lecture 3 time complexity
Lecture 3 complexity
Numerical on bisection method
Pointer arithmetic in c
Beginning direct3d gameprogrammingmath02_logarithm_20160324_jintaeks
segment tree algorithm.pptx
Daa unit 5
Lecture11
Code Optimization in MIPS
Divide and conquer
Divide and conquer
Convolution
CBSE Python Dictionary Assignment
Ad

Similar to Recurrence relationships (20)

PPTX
Complexity Analysis of Recursive Function
PDF
Lecture 5 6_7 - divide and conquer and method of solving recurrences
PDF
3.pdf
PPT
PPT
algo_vc_lecture8.ppt
PPTX
3. D&C and Recurrence Relation.ppYtxVVVV
PPT
CS8451 - Design and Analysis of Algorithms
PPT
Complexity analysis
PDF
Copy of y16 02-2119divide-and-conquer
PDF
Chapter One.pdf
PPTX
Weekends with Competitive Programming
PPTX
2.pptx
PPTX
Lec 5 asymptotic notations and recurrences
PPTX
Presentation_23953_Content_Document_20240906040454PM.pptx
PPTX
DS Unit-1.pptx very easy to understand..
PPT
recurrence relation is explained in this
PPT
Analysis of algo
PPTX
Design and analysis of algorithms.pptx
PPTX
Analysis of Algorithms (1).pptx, asymptotic
PDF
lec 03wweweweweweweeweweweewewewewee.pdf
Complexity Analysis of Recursive Function
Lecture 5 6_7 - divide and conquer and method of solving recurrences
3.pdf
algo_vc_lecture8.ppt
3. D&C and Recurrence Relation.ppYtxVVVV
CS8451 - Design and Analysis of Algorithms
Complexity analysis
Copy of y16 02-2119divide-and-conquer
Chapter One.pdf
Weekends with Competitive Programming
2.pptx
Lec 5 asymptotic notations and recurrences
Presentation_23953_Content_Document_20240906040454PM.pptx
DS Unit-1.pptx very easy to understand..
recurrence relation is explained in this
Analysis of algo
Design and analysis of algorithms.pptx
Analysis of Algorithms (1).pptx, asymptotic
lec 03wweweweweweweeweweweewewewewee.pdf
Ad

More from Devansh16 (14)

PDF
Spine net learning scale permuted backbone for recognition and localization
PPTX
Sigmoid function machine learning made simple
PDF
Accounting for variance in machine learning benchmarks
PDF
When deep learners change their mind learning dynamics for active learning
PPTX
Semi supervised learning machine learning made simple
PDF
Paper Annotated: SinGAN-Seg: Synthetic Training Data Generation for Medical I...
PDF
A simple framework for contrastive learning of visual representations
PDF
Paper Explained: Deep learning framework for measuring the digital strategy o...
PDF
Paper Explained: One Pixel Attack for Fooling Deep Neural Networks
PDF
Paper Explained: Understanding the wiring evolution in differentiable neural ...
PPTX
Machine Learning Made Simple: Differential evolution
PDF
Paper Explained: RandAugment: Practical automated data augmentation with a re...
PDF
Noisy student images
PDF
Deep learning ensembles loss landscape
Spine net learning scale permuted backbone for recognition and localization
Sigmoid function machine learning made simple
Accounting for variance in machine learning benchmarks
When deep learners change their mind learning dynamics for active learning
Semi supervised learning machine learning made simple
Paper Annotated: SinGAN-Seg: Synthetic Training Data Generation for Medical I...
A simple framework for contrastive learning of visual representations
Paper Explained: Deep learning framework for measuring the digital strategy o...
Paper Explained: One Pixel Attack for Fooling Deep Neural Networks
Paper Explained: Understanding the wiring evolution in differentiable neural ...
Machine Learning Made Simple: Differential evolution
Paper Explained: RandAugment: Practical automated data augmentation with a re...
Noisy student images
Deep learning ensembles loss landscape

Recently uploaded (20)

PDF
SMART SIGNAL TIMING FOR URBAN INTERSECTIONS USING REAL-TIME VEHICLE DETECTI...
PPTX
introduction to high performance computing
PPTX
Fundamentals of Mechanical Engineering.pptx
PDF
R24 SURVEYING LAB MANUAL for civil enggi
PDF
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
PDF
EXPLORING LEARNING ENGAGEMENT FACTORS INFLUENCING BEHAVIORAL, COGNITIVE, AND ...
PDF
Level 2 – IBM Data and AI Fundamentals (1)_v1.1.PDF
PPTX
Current and future trends in Computer Vision.pptx
PPTX
UNIT - 3 Total quality Management .pptx
PDF
PREDICTION OF DIABETES FROM ELECTRONIC HEALTH RECORDS
PDF
Automation-in-Manufacturing-Chapter-Introduction.pdf
PDF
PPT on Performance Review to get promotions
PPTX
Safety Seminar civil to be ensured for safe working.
PPT
Total quality management ppt for engineering students
PPT
Introduction, IoT Design Methodology, Case Study on IoT System for Weather Mo...
PDF
III.4.1.2_The_Space_Environment.p pdffdf
PPT
A5_DistSysCh1.ppt_INTRODUCTION TO DISTRIBUTED SYSTEMS
PDF
Soil Improvement Techniques Note - Rabbi
PPT
Occupational Health and Safety Management System
PDF
737-MAX_SRG.pdf student reference guides
SMART SIGNAL TIMING FOR URBAN INTERSECTIONS USING REAL-TIME VEHICLE DETECTI...
introduction to high performance computing
Fundamentals of Mechanical Engineering.pptx
R24 SURVEYING LAB MANUAL for civil enggi
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
EXPLORING LEARNING ENGAGEMENT FACTORS INFLUENCING BEHAVIORAL, COGNITIVE, AND ...
Level 2 – IBM Data and AI Fundamentals (1)_v1.1.PDF
Current and future trends in Computer Vision.pptx
UNIT - 3 Total quality Management .pptx
PREDICTION OF DIABETES FROM ELECTRONIC HEALTH RECORDS
Automation-in-Manufacturing-Chapter-Introduction.pdf
PPT on Performance Review to get promotions
Safety Seminar civil to be ensured for safe working.
Total quality management ppt for engineering students
Introduction, IoT Design Methodology, Case Study on IoT System for Weather Mo...
III.4.1.2_The_Space_Environment.p pdffdf
A5_DistSysCh1.ppt_INTRODUCTION TO DISTRIBUTED SYSTEMS
Soil Improvement Techniques Note - Rabbi
Occupational Health and Safety Management System
737-MAX_SRG.pdf student reference guides

Recurrence relationships

  • 1. Recurrence Relations and Big O How do we compute the costs of running our code
  • 2. Recursive Functions (Math Ones) • Similar to coding, a recursive function in math is something that calls itself. • We have a base case(s), and the recursive state. • Example: T(1)=1 ,T(2)=1, Tn= T(n-1)+T(n-2) for n>=3 • A pretty comprehensive topic. However we want to focus on Software Engineering, so we will look at them wrt to understanding asymptotic complexities.
  • 3. Building it (and use) • We can take the code. Any base case in the base-case directly translates as a base case in our math formulation. • Our recursive case translates directly as well. • We substitute values till we hit a base case. • Add the number of operations. • OR we draw a tree. • And We add. They are the same process, so it depends on what you prefer.
  • 4. Example 1: T(1)=1 ,T(2)=1, Tn= T(n-1)+T(n-2) for n>=3. Calculating complexity • T(n)= T(n-1)+T(n-2) • = T(n-2)+T(n-3)+T(n-3)+T(n-4) • = … • We have constantly substituted the recurrence for one lower. Eventually we want to get to base case. • We can see a pattern form. Each higher level function call splits into 2 lower level ones (till we hit the base case). T(n)-> T(n-1) +T(n-2); T(n-1)->T(n-2)+T(n-3) … T(n-(n-3))->T(2) +T(1) where we hit basecase
  • 6. Example 1: Code • function F1(n){ • if(n==1 || n==2){ • return 1 • }else{ • return F1(n-1)+F1(n-2) • } • }
  • 7. Calculating the Time Complexity (COST) • We can see that the function calls itself Twice per recursive call (our sketch of this pattern has 2 branches from every level). • So the number of calls for T(n) is 2*T(n-1). NOTE: We are talking about number of calls not cost. • Each function call has 1 operation (the addition). Base cases have the return. • So how many calls do we have?? • What is the cost? Can you think of a generalized formula
  • 8. Big O analysis • Definition: f(n) = O(g(n)) if there exists a positive integer n0 and a positive constant c, such that f(n)≤c*g(n) ∀ n≥n0 • It states the following. Imagine we have 2 functions f and g. f is in the Big O of g if we can find an n0 and c such that f(n)≤c*g(n) ∀ n≥n0. • Essentially, we can say that f is in the Big O of n if we can show that g will be greater than f for all values more than n.
  • 9. Simple way to calculate Costs • Number of Function Calls * Operations per call (general outline for calculations). • This is where understanding Recurrence Relationships come into play • RRs can help you strip the code down into essentials. Helpful for figuring out function calls. • Think of the problems we’ve already done. What do you think of their costs? How would you split their number of function calls and operations per call?
  • 10. Your Interesting Question • https://www.hackerrank.com/challenges/jumping-on-the- clouds/problem • Try to break down this problem into cases and solve. • Lmk if you need help with something