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

PPTX
Week4 practice
PPTX
Introduction to dynamic programming
PPTX
Introduction to Dynamic Programming, Principle of Optimality
PPT
Dynamic programming 2
DOCX
Mb0048 operations research
PPT
Lecture11
PDF
Combinatorial optimization CO-1
PPTX
Dynamic Programming
Week4 practice
Introduction to dynamic programming
Introduction to Dynamic Programming, Principle of Optimality
Dynamic programming 2
Mb0048 operations research
Lecture11
Combinatorial optimization CO-1
Dynamic Programming

What's hot (19)

PPTX
What Is Dynamic Programming? | Dynamic Programming Explained | Programming Fo...
PPTX
Module 5. bsm
PPT
14 recursion
PDF
Algorithm hierarchy
PPTX
Elements of dynamic programming
PDF
Dynamic Programming and Reinforcement Learning applied to Tetris Game
PDF
Sol1
PPT
Fundamental Programming Lect 5
PPT
Cs 1114 - lecture-8
PPT
Fundamental Programming Lect 3
PPT
Fundamental Programming Lect 2
PPT
Ch2 slides
PPT
Fundamental Programming Lect 4
PPTX
Information and network security 34 primality
PPT
Ch4 slides
PDF
Algorithm
PDF
Rational functions lecture
PPT
Divide and conquer algorithm
PPT
Pp10 input process-output
What Is Dynamic Programming? | Dynamic Programming Explained | Programming Fo...
Module 5. bsm
14 recursion
Algorithm hierarchy
Elements of dynamic programming
Dynamic Programming and Reinforcement Learning applied to Tetris Game
Sol1
Fundamental Programming Lect 5
Cs 1114 - lecture-8
Fundamental Programming Lect 3
Fundamental Programming Lect 2
Ch2 slides
Fundamental Programming Lect 4
Information and network security 34 primality
Ch4 slides
Algorithm
Rational functions lecture
Divide and conquer algorithm
Pp10 input process-output
Ad

Similar to Recursion (20)

PDF
Recursion.pdf
PDF
DS & Algo 2 - Recursion
PPTX
Introduction to Dynamic Programming.pptx
PDF
062636636366363773737373733+73737733+7.pdf
PPT
Lec-32 Recursion - Divide and Conquer in Queue
PPT
Lec-6 Recursion of Data Structures & Algorithms
PPT
3. Recursion and Recurrences.ppt detail about recursive learning
PPT
Recursion
PDF
C users_mpk7_app_data_local_temp_plugtmp_plugin-week3recursive
PPTX
Recursion Merge Sort Quick Sort.pptx
PPTX
CMSC 56 | Lecture 12: Recursive Definition & Algorithms, and Program Correctness
PPTX
Recursion
PPTX
06 Recursion in C.pptx
PDF
Recursion examples
PDF
C and Data Structures Lab Solutions
PDF
C lab excellent
PDF
C and Data Structures
PDF
Iterations and Recursions
PPTX
Recursion and Sorting Algorithms
PDF
815.07 machine learning using python.pdf
Recursion.pdf
DS & Algo 2 - Recursion
Introduction to Dynamic Programming.pptx
062636636366363773737373733+73737733+7.pdf
Lec-32 Recursion - Divide and Conquer in Queue
Lec-6 Recursion of Data Structures & Algorithms
3. Recursion and Recurrences.ppt detail about recursive learning
Recursion
C users_mpk7_app_data_local_temp_plugtmp_plugin-week3recursive
Recursion Merge Sort Quick Sort.pptx
CMSC 56 | Lecture 12: Recursive Definition & Algorithms, and Program Correctness
Recursion
06 Recursion in C.pptx
Recursion examples
C and Data Structures Lab Solutions
C lab excellent
C and Data Structures
Iterations and Recursions
Recursion and Sorting Algorithms
815.07 machine learning using python.pdf
Ad

More from Kasun Ranga Wijeweera (20)

PDF
Decorator Design Pattern in C#
PDF
Singleton Design Pattern in C#
PDF
Introduction to Design Patterns
PPTX
Algorithms for Convex Partitioning of a Polygon
PDF
Geometric Transformations II
PDF
Geometric Transformations I
PDF
Introduction to Polygons
PDF
Bresenham Line Drawing Algorithm
PDF
Digital Differential Analyzer Line Drawing Algorithm
PDF
Loops in Visual Basic: Exercises
PDF
Conditional Logic: Exercises
PDF
Getting Started with Visual Basic Programming
PDF
CheckBoxes and RadioButtons
PDF
Variables in Visual Basic Programming
PDF
Loops in Visual Basic Programming
PDF
Conditional Logic in Visual Basic Programming
PDF
Assignment for Variables
PDF
Assignment for Factory Method Design Pattern in C# [ANSWERS]
PDF
Assignment for Events
PDF
Mastering Arrays Assignment
Decorator Design Pattern in C#
Singleton Design Pattern in C#
Introduction to Design Patterns
Algorithms for Convex Partitioning of a Polygon
Geometric Transformations II
Geometric Transformations I
Introduction to Polygons
Bresenham Line Drawing Algorithm
Digital Differential Analyzer Line Drawing Algorithm
Loops in Visual Basic: Exercises
Conditional Logic: Exercises
Getting Started with Visual Basic Programming
CheckBoxes and RadioButtons
Variables in Visual Basic Programming
Loops in Visual Basic Programming
Conditional Logic in Visual Basic Programming
Assignment for Variables
Assignment for Factory Method Design Pattern in C# [ANSWERS]
Assignment for Events
Mastering Arrays Assignment

Recently uploaded (20)

PDF
Machine learning based COVID-19 study performance prediction
PDF
Assigned Numbers - 2025 - Bluetooth® Document
PDF
NewMind AI Weekly Chronicles - August'25-Week II
PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
PDF
cuic standard and advanced reporting.pdf
PDF
Unlocking AI with Model Context Protocol (MCP)
PPTX
Programs and apps: productivity, graphics, security and other tools
PPTX
Machine Learning_overview_presentation.pptx
PDF
gpt5_lecture_notes_comprehensive_20250812015547.pdf
PPTX
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PDF
Electronic commerce courselecture one. Pdf
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PPTX
A Presentation on Artificial Intelligence
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
MIND Revenue Release Quarter 2 2025 Press Release
Machine learning based COVID-19 study performance prediction
Assigned Numbers - 2025 - Bluetooth® Document
NewMind AI Weekly Chronicles - August'25-Week II
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
cuic standard and advanced reporting.pdf
Unlocking AI with Model Context Protocol (MCP)
Programs and apps: productivity, graphics, security and other tools
Machine Learning_overview_presentation.pptx
gpt5_lecture_notes_comprehensive_20250812015547.pdf
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
20250228 LYD VKU AI Blended-Learning.pptx
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
Advanced methodologies resolving dimensionality complications for autism neur...
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Electronic commerce courselecture one. Pdf
“AI and Expert System Decision Support & Business Intelligence Systems”
A Presentation on Artificial Intelligence
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Mobile App Security Testing_ A Comprehensive Guide.pdf
MIND Revenue Release Quarter 2 2025 Press Release

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); } }