SlideShare a Scribd company logo
1 of 13
Recursion


         Kasun Ranga Wijeweera
     (Email: krw19870829@gmail.com)
Department of Statistics and Computer Science
             Faculty of Science
         University of Peradeniya
What is Recursion?
• A fundamental concept in computer science and mathematics
• A recursive program is one that calls itself
• There must be a termination condition
Recurrences
• Recursive definitions of functions are quite common in
  mathematics
• The simplest type, involving integer arguments are called
  recurrence relations
• Most familiar such a function is the factorial function, defined
  by the formula

              N ! = N * (N - 1) !, for N >=1 with 0 ! = 1
Recurrences
• The corresponding simple recursive program is as follows

  int factorial (int N)
  {
        if (N == 0) return 1;
        return N * factorial (N - 1);
  }

• Problem:
  factorial (-1) leads to an infinite loop
Recurrences
• A second well-known recurrence relation is the one that
  defines the Fibonacci numbers

  F (N) = F (N - 1) + F (N - 2), for N >= 2 with F (0) = F (1) =1

• This defines the sequence

   1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610,…
Recurrences
• The corresponding recursive program is as follows

  int fibonacci (int N)
  {
        if (N <= 1) return 1;
        return fibonacci (N - 1) + fibonacci (N - 2);
  }

• The running time of this program is exponential
Recurrences
• To compute F (N) in linear time

  #define max 25
  int fibonacci (int N)
  {
        int i, F[max];
        F[0] = 1; F[1] = 1;
        for(i = 2; i <= max; i++)
                 F[i] = F[i - 1] + F[i - 2];
        return F[N];
  }
Divide and Conquer
• Most of the recursive programs use two recursive calls, each
  operating on about half the input
• This is so called “divide and conquer” paradigm
• Used to achieve significant economics
• They normally do not reduce to trivial loops like the factorial
  program
• They normally do not lead to excessive re-computing as
  Fibonacci program, because the input is divided without
  overlap
Divide and Conquer: Example
• Let us consider the task of drawing the markings for each inch
  on a ruler
• There is a mark at the 1/2 point
• Slightly shorter marks at 1/4 intervals
• Still shorter marks at 1/8 intervals, etc
Divide and Conquer: Example
• Suppose the desired resolution is 1/(2^n)
• Put a mark at every point between 0 and (2^n), end points not
  included
• A procedure mark (x, h) is used to mark h units high at x
  position
• The middle mark should be n units high
• The marks in the left and right halves should be n-1 units high
Divide and Conquer: Example
• The relevant “divide and conquer” recursive program is as
  follows

  rule (int a, int r, int h)
  {
       int m = (a + r) / 2;
                if (h > 0)
                {
                          mark (m, h);
                          rule (a, m, h-1);
                          rule (m, r, h-1);
                }
  }
Any Questions?
Thank You!

More Related Content

What's hot

What Is Dynamic Programming? | Dynamic Programming Explained | Programming Fo...
What Is Dynamic Programming? | Dynamic Programming Explained | Programming Fo...What Is Dynamic Programming? | Dynamic Programming Explained | Programming Fo...
What Is Dynamic Programming? | Dynamic Programming Explained | Programming Fo...Simplilearn
 
Algorithm hierarchy
Algorithm hierarchyAlgorithm hierarchy
Algorithm hierarchyChangyu Yang
 
Elements of dynamic programming
Elements of dynamic programmingElements of dynamic programming
Elements of dynamic programmingTafhim Islam
 
Dynamic Programming and Reinforcement Learning applied to Tetris Game
Dynamic Programming and Reinforcement Learning applied to Tetris GameDynamic Programming and Reinforcement Learning applied to Tetris Game
Dynamic Programming and Reinforcement Learning applied to Tetris GameSuelen Carvalho
 
Fundamental Programming Lect 5
Fundamental Programming Lect 5Fundamental Programming Lect 5
Fundamental Programming Lect 5Namrah Erum
 
Fundamental Programming Lect 3
Fundamental Programming Lect 3Fundamental Programming Lect 3
Fundamental Programming Lect 3Namrah Erum
 
Fundamental Programming Lect 2
Fundamental Programming Lect 2Fundamental Programming Lect 2
Fundamental Programming Lect 2Namrah Erum
 
Fundamental Programming Lect 4
Fundamental Programming Lect 4Fundamental Programming Lect 4
Fundamental Programming Lect 4Namrah Erum
 
Information and network security 34 primality
Information and network security 34 primalityInformation and network security 34 primality
Information and network security 34 primalityVaibhav Khanna
 
Rational functions lecture
Rational functions lectureRational functions lecture
Rational functions lectureAdnanBukhari13
 
Divide and conquer algorithm
Divide and conquer algorithmDivide and conquer algorithm
Divide and conquer algorithmCHANDAN KUMAR
 
Pp10 input process-output
Pp10 input process-outputPp10 input process-output
Pp10 input process-outputmenisantixs
 

What's hot (19)

What Is Dynamic Programming? | Dynamic Programming Explained | Programming Fo...
What Is Dynamic Programming? | Dynamic Programming Explained | Programming Fo...What Is Dynamic Programming? | Dynamic Programming Explained | Programming Fo...
What Is Dynamic Programming? | Dynamic Programming Explained | Programming Fo...
 
Module 5. bsm
Module 5. bsmModule 5. bsm
Module 5. bsm
 
14 recursion
14 recursion14 recursion
14 recursion
 
Algorithm hierarchy
Algorithm hierarchyAlgorithm hierarchy
Algorithm hierarchy
 
Elements of dynamic programming
Elements of dynamic programmingElements of dynamic programming
Elements of dynamic programming
 
Dynamic Programming and Reinforcement Learning applied to Tetris Game
Dynamic Programming and Reinforcement Learning applied to Tetris GameDynamic Programming and Reinforcement Learning applied to Tetris Game
Dynamic Programming and Reinforcement Learning applied to Tetris Game
 
Sol1
Sol1Sol1
Sol1
 
Fundamental Programming Lect 5
Fundamental Programming Lect 5Fundamental Programming Lect 5
Fundamental Programming Lect 5
 
Cs 1114 - lecture-8
Cs 1114 - lecture-8Cs 1114 - lecture-8
Cs 1114 - lecture-8
 
Fundamental Programming Lect 3
Fundamental Programming Lect 3Fundamental Programming Lect 3
Fundamental Programming Lect 3
 
Fundamental Programming Lect 2
Fundamental Programming Lect 2Fundamental Programming Lect 2
Fundamental Programming Lect 2
 
Ch2 slides
Ch2 slidesCh2 slides
Ch2 slides
 
Fundamental Programming Lect 4
Fundamental Programming Lect 4Fundamental Programming Lect 4
Fundamental Programming Lect 4
 
Information and network security 34 primality
Information and network security 34 primalityInformation and network security 34 primality
Information and network security 34 primality
 
Ch4 slides
Ch4 slidesCh4 slides
Ch4 slides
 
Algorithm
AlgorithmAlgorithm
Algorithm
 
Rational functions lecture
Rational functions lectureRational functions lecture
Rational functions lecture
 
Divide and conquer algorithm
Divide and conquer algorithmDivide and conquer algorithm
Divide and conquer algorithm
 
Pp10 input process-output
Pp10 input process-outputPp10 input process-output
Pp10 input process-output
 

Similar to Recursion

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
 
chapter1.pdf ......................................
chapter1.pdf ......................................chapter1.pdf ......................................
chapter1.pdf ......................................nourhandardeer3
 
Lagrange Interpolation
Lagrange InterpolationLagrange Interpolation
Lagrange InterpolationSaloni Singhal
 
Newton Backward Interpolation
Newton Backward InterpolationNewton Backward Interpolation
Newton Backward InterpolationSaloni Singhal
 
Recurrence relationships
Recurrence relationshipsRecurrence relationships
Recurrence relationshipsDevansh16
 
1 chapter1 introduction
1 chapter1 introduction1 chapter1 introduction
1 chapter1 introductionSSE_AndyLi
 
Analysis of algorithms
Analysis of algorithmsAnalysis of algorithms
Analysis of algorithmsGanesh Solanke
 
Arithmetic sequence in elementary and HS
Arithmetic sequence in elementary and HSArithmetic sequence in elementary and HS
Arithmetic sequence in elementary and HSRoseEdenAbitong2
 
Effective Algorithm for n Fibonacci Number By: Professor Lili Saghafi
Effective Algorithm for n Fibonacci Number By: Professor Lili SaghafiEffective Algorithm for n Fibonacci Number By: Professor Lili Saghafi
Effective Algorithm for n Fibonacci Number By: Professor Lili SaghafiProfessor Lili Saghafi
 
01 - DAA - PPT.pptx
01 - DAA - PPT.pptx01 - DAA - PPT.pptx
01 - DAA - PPT.pptxKokilaK25
 
Introducción al Análisis y diseño de algoritmos
Introducción al Análisis y diseño de algoritmosIntroducción al Análisis y diseño de algoritmos
Introducción al Análisis y diseño de algoritmosluzenith_g
 

Similar to Recursion (20)

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
 
algo_vc_lecture8.ppt
algo_vc_lecture8.pptalgo_vc_lecture8.ppt
algo_vc_lecture8.ppt
 
Recursion
RecursionRecursion
Recursion
 
chapter1.pdf ......................................
chapter1.pdf ......................................chapter1.pdf ......................................
chapter1.pdf ......................................
 
Lagrange Interpolation
Lagrange InterpolationLagrange Interpolation
Lagrange Interpolation
 
Newton Backward Interpolation
Newton Backward InterpolationNewton Backward Interpolation
Newton Backward Interpolation
 
Chap12 slides
Chap12 slidesChap12 slides
Chap12 slides
 
Recurrence relationships
Recurrence relationshipsRecurrence relationships
Recurrence relationships
 
1 chapter1 introduction
1 chapter1 introduction1 chapter1 introduction
1 chapter1 introduction
 
Analysis of algorithms
Analysis of algorithmsAnalysis of algorithms
Analysis of algorithms
 
Arithmetic sequence in elementary and HS
Arithmetic sequence in elementary and HSArithmetic sequence in elementary and HS
Arithmetic sequence in elementary and HS
 
algorithm Unit 2
algorithm Unit 2 algorithm Unit 2
algorithm Unit 2
 
Unit 2 in daa
Unit 2 in daaUnit 2 in daa
Unit 2 in daa
 
RecursionWeek8.ppt
RecursionWeek8.pptRecursionWeek8.ppt
RecursionWeek8.ppt
 
Effective Algorithm for n Fibonacci Number By: Professor Lili Saghafi
Effective Algorithm for n Fibonacci Number By: Professor Lili SaghafiEffective Algorithm for n Fibonacci Number By: Professor Lili Saghafi
Effective Algorithm for n Fibonacci Number By: Professor Lili Saghafi
 
Alg1
Alg1Alg1
Alg1
 
6-Python-Recursion PPT.pptx
6-Python-Recursion PPT.pptx6-Python-Recursion PPT.pptx
6-Python-Recursion PPT.pptx
 
Algorithms
Algorithms Algorithms
Algorithms
 
01 - DAA - PPT.pptx
01 - DAA - PPT.pptx01 - DAA - PPT.pptx
01 - DAA - PPT.pptx
 
Introducción al Análisis y diseño de algoritmos
Introducción al Análisis y diseño de algoritmosIntroducción al Análisis y diseño de algoritmos
Introducción al Análisis y diseño de algoritmos
 

More from Kasun Ranga Wijeweera

Algorithms for Convex Partitioning of a Polygon
Algorithms for Convex Partitioning of a PolygonAlgorithms for Convex Partitioning of a Polygon
Algorithms for Convex Partitioning of a PolygonKasun Ranga Wijeweera
 
Digital Differential Analyzer Line Drawing Algorithm
Digital Differential Analyzer Line Drawing AlgorithmDigital Differential Analyzer Line Drawing Algorithm
Digital Differential Analyzer Line Drawing AlgorithmKasun Ranga Wijeweera
 
Getting Started with Visual Basic Programming
Getting Started with Visual Basic ProgrammingGetting Started with Visual Basic Programming
Getting Started with Visual Basic ProgrammingKasun Ranga Wijeweera
 
Variables in Visual Basic Programming
Variables in Visual Basic ProgrammingVariables in Visual Basic Programming
Variables in Visual Basic ProgrammingKasun Ranga Wijeweera
 
Conditional Logic in Visual Basic Programming
Conditional Logic in Visual Basic ProgrammingConditional Logic in Visual Basic Programming
Conditional Logic in Visual Basic ProgrammingKasun Ranga Wijeweera
 
Assignment for Factory Method Design Pattern in C# [ANSWERS]
Assignment for Factory Method Design Pattern in C# [ANSWERS]Assignment for Factory Method Design Pattern in C# [ANSWERS]
Assignment for Factory Method Design Pattern in C# [ANSWERS]Kasun Ranga Wijeweera
 

More from Kasun Ranga Wijeweera (20)

Decorator Design Pattern in C#
Decorator Design Pattern in C#Decorator Design Pattern in C#
Decorator Design Pattern in C#
 
Singleton Design Pattern in C#
Singleton Design Pattern in C#Singleton Design Pattern in C#
Singleton Design Pattern in C#
 
Introduction to Design Patterns
Introduction to Design PatternsIntroduction to Design Patterns
Introduction to Design Patterns
 
Algorithms for Convex Partitioning of a Polygon
Algorithms for Convex Partitioning of a PolygonAlgorithms for Convex Partitioning of a Polygon
Algorithms for Convex Partitioning of a Polygon
 
Geometric Transformations II
Geometric Transformations IIGeometric Transformations II
Geometric Transformations II
 
Geometric Transformations I
Geometric Transformations IGeometric Transformations I
Geometric Transformations I
 
Introduction to Polygons
Introduction to PolygonsIntroduction to Polygons
Introduction to Polygons
 
Bresenham Line Drawing Algorithm
Bresenham Line Drawing AlgorithmBresenham Line Drawing Algorithm
Bresenham Line Drawing Algorithm
 
Digital Differential Analyzer Line Drawing Algorithm
Digital Differential Analyzer Line Drawing AlgorithmDigital Differential Analyzer Line Drawing Algorithm
Digital Differential Analyzer Line Drawing Algorithm
 
Loops in Visual Basic: Exercises
Loops in Visual Basic: ExercisesLoops in Visual Basic: Exercises
Loops in Visual Basic: Exercises
 
Conditional Logic: Exercises
Conditional Logic: ExercisesConditional Logic: Exercises
Conditional Logic: Exercises
 
Getting Started with Visual Basic Programming
Getting Started with Visual Basic ProgrammingGetting Started with Visual Basic Programming
Getting Started with Visual Basic Programming
 
CheckBoxes and RadioButtons
CheckBoxes and RadioButtonsCheckBoxes and RadioButtons
CheckBoxes and RadioButtons
 
Variables in Visual Basic Programming
Variables in Visual Basic ProgrammingVariables in Visual Basic Programming
Variables in Visual Basic Programming
 
Loops in Visual Basic Programming
Loops in Visual Basic ProgrammingLoops in Visual Basic Programming
Loops in Visual Basic Programming
 
Conditional Logic in Visual Basic Programming
Conditional Logic in Visual Basic ProgrammingConditional Logic in Visual Basic Programming
Conditional Logic in Visual Basic Programming
 
Assignment for Variables
Assignment for VariablesAssignment for Variables
Assignment for Variables
 
Assignment for Factory Method Design Pattern in C# [ANSWERS]
Assignment for Factory Method Design Pattern in C# [ANSWERS]Assignment for Factory Method Design Pattern in C# [ANSWERS]
Assignment for Factory Method Design Pattern in C# [ANSWERS]
 
Assignment for Events
Assignment for EventsAssignment for Events
Assignment for Events
 
Mastering Arrays Assignment
Mastering Arrays AssignmentMastering Arrays Assignment
Mastering Arrays Assignment
 

Recently uploaded

presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century educationjfdjdjcjdnsjd
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024The Digital Insurer
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodJuan lago vázquez
 
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot ModelNavi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot ModelDeepika Singh
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CVKhem
 
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...apidays
 
Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024The Digital Insurer
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdflior mazor
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...apidays
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...apidays
 
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu SubbuApidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbuapidays
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobeapidays
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyKhushali Kathiriya
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfsudhanshuwaghmare1
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businesspanagenda
 
Ransomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfRansomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfOverkill Security
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MIND CTI
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProduct Anonymous
 

Recently uploaded (20)

presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot ModelNavi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
 
Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu SubbuApidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
Ransomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfRansomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdf
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 

Recursion

  • 1. Recursion Kasun Ranga Wijeweera (Email: krw19870829@gmail.com) Department of Statistics and Computer Science Faculty of Science University of Peradeniya
  • 2. What is Recursion? • A fundamental concept in computer science and mathematics • A recursive program is one that calls itself • There must be a termination condition
  • 3. Recurrences • Recursive definitions of functions are quite common in mathematics • The simplest type, involving integer arguments are called recurrence relations • Most familiar such a function is the factorial function, defined by the formula N ! = N * (N - 1) !, for N >=1 with 0 ! = 1
  • 4. Recurrences • The corresponding simple recursive program is as follows int factorial (int N) { if (N == 0) return 1; return N * factorial (N - 1); } • Problem: factorial (-1) leads to an infinite loop
  • 5. Recurrences • A second well-known recurrence relation is the one that defines the Fibonacci numbers F (N) = F (N - 1) + F (N - 2), for N >= 2 with F (0) = F (1) =1 • This defines the sequence 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610,…
  • 6. Recurrences • The corresponding recursive program is as follows int fibonacci (int N) { if (N <= 1) return 1; return fibonacci (N - 1) + fibonacci (N - 2); } • The running time of this program is exponential
  • 7. Recurrences • To compute F (N) in linear time #define max 25 int fibonacci (int N) { int i, F[max]; F[0] = 1; F[1] = 1; for(i = 2; i <= max; i++) F[i] = F[i - 1] + F[i - 2]; return F[N]; }
  • 8. Divide and Conquer • Most of the recursive programs use two recursive calls, each operating on about half the input • This is so called “divide and conquer” paradigm • Used to achieve significant economics • They normally do not reduce to trivial loops like the factorial program • They normally do not lead to excessive re-computing as Fibonacci program, because the input is divided without overlap
  • 9. Divide and Conquer: Example • Let us consider the task of drawing the markings for each inch on a ruler • There is a mark at the 1/2 point • Slightly shorter marks at 1/4 intervals • Still shorter marks at 1/8 intervals, etc
  • 10. Divide and Conquer: Example • Suppose the desired resolution is 1/(2^n) • Put a mark at every point between 0 and (2^n), end points not included • A procedure mark (x, h) is used to mark h units high at x position • The middle mark should be n units high • The marks in the left and right halves should be n-1 units high
  • 11. Divide and Conquer: Example • The relevant “divide and conquer” recursive program is as follows rule (int a, int r, int h) { int m = (a + r) / 2; if (h > 0) { mark (m, h); rule (a, m, h-1); rule (m, r, h-1); } }