Recursion

837 views

Published on

Published in: Education, Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
837
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
42
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Recursion

  1. 1. Disclaimer: This presentation is prepared by trainees ofbaabtra as a part of mentoring program. This is not officialdocument of baabtra –Mentoring PartnerBaabtra-Mentoring Partner is the mentoring division of baabte System Technologies Pvt .Ltd
  2. 2. RecursionSubhin P.V111subru@gmail.comwww.facebook.com/subhinvelayudhantwitter.com/111subruin.linkedin.com/in/Subhin P V8129076036
  3. 3. WHAT IS RECURSION• In simple language Recursion is the repeatedoccurrence of a particular event• In programming Recursion is the repeatedoccurrence of a procedure as a result of theprocedure calling itself.• Recursive procedure is the name given to theprocedures calling themselves.• The parameters passed each time into thefunction must be different.
  4. 4. Content of a Recursive Method• Base case(s).– Values of the input variables for which we perform norecursive calls are called base cases (there should be atleast one base case).– Every possible chain of recursive calls must eventuallyreach a base case.• Recursive calls.– Calls to the current method.– Each recursive call should be defined so that it makesprogress towards a base case.
  5. 5. Types of Recursion• Single and Multiple Recursion– Recursion that only contains a single self-reference is known as single recursion. Eg:Factorial function– Recursion that contains multiple self-references isknown as multiple recursion. Eg: Fibonaccifunction
  6. 6. Types of Recursion• Direct And Indirect Recursion– IF a Function F calls itself. (i.e) F calls F. Then thisrecursion is Direct Recursion.static int Direct (int n){if (n<=0) return 0;else return n + Direct (n-1);}
  7. 7. Types of Recursion• Indirect Recursion– IF a Function F calls G. and Function G calls F. Thenthis recursion is Indirect Recursion.static int InDirect (int n){if (n<=0) return 0;else return n + Buddy (n-1);}int Buddy (int n){return InDirect (n);}
  8. 8. Recursively Defined Sequences•Example:•The sequence {an} of powers of 2 is given nonrecursively byan = 2n for n = 0, 1, 2, … .•The same sequence can also be definedRecursively:a0 = 1an+1 = 2an for n = 0, 1, 2, …
  9. 9. RECURSIVELY DEFINED FUNCTIONS• Here is the non-recursive definition of the factorialfunction:– n! = 1· 2· 3· ··· · (n-1)· n• Here is the recursive definition of a factorial:(here f(n) = n!)• The Java code for the Factorial function:// recursive procedure for computing factorialpublic static int Factorial(int n) {if (n == 0) return 1; // base caseelse return n * Factorial(n- 1); // recursive case}elsenfnnnf)1(0if1)(
  10. 10. RECURSIVE ALGORITHMS• Example: Recursive Fibonacci Algorithmprocedure fib(n: nonnegative integer)if n = 0 then fib(0) := 0else if n = 1 then fib(1) := 1else fib(n) := fib(n – 1) + fib(n – 2)
  11. 11. RECURSIVE ALGORITHMS(contd..)Recursive Fibonacci Evaluation:
  12. 12. If this presentation helped you, please visit ourpage facebook.com/baabtra and like it.Thanks in advance.www.baabtra.com | www.massbaab.com |www.baabte.com
  13. 13. Contact Us

×