1 / 21

RECURSION

What is recursion? <br>

myung1
Download Presentation

RECURSION

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. RECURSION DCIT 25 BSINFOTECH 202-C

  2. OUR TEAM MARIE ANDREA PANIZARES MA. JOANNE VILLACERAN KEN GILI MARIEL VILLETE JOHN MARK MANASALA JENZ ASISTORES TIME KEEPER PRESENTOR SUMMARIZER RECORDER LEADER JOHN PATRICK BENTIHABA DATA COLLECTOR ARTIST

  3. Recursion is based on Mathematical notion of induction, where an inductive proof can be built by proving the theorem for n=1, then by showing in more general, that if an assumption that a theorem holds for n = r implies it holds for n = r+1, then theorem holds for all n. The induction is a powerful mathematical tool for proving many interesting theorems. For example, we can prove that, for any n >= 1, 1 + 2 + …. + n = (n/2)(n+1) We prove this by assuming if the case for (n-1) holds true then that implies that the case for n holds as well.

  4. Recursion is a widely used phenomenon in computer science used to solve complex problems by breaking them down into simpler ones. Recursion is a process by which a function calls itself directly or indirectly. The corresponding function is called as recursive function. Using recursive algorithms, certain complex problems can be solved quite easily.

  5. WHAT IS DIRECT RECURSION AND INDIRECT RECURSION

  6. DIRECT RECURSION VS. INDIRECT RECURSION

  7. STRUCTURES OF DIRECT RECURSION & INDIRECT RECURSION DIRECT INDIRECT

  8. Recursion: The Pros and Cons • PROS • Recursion can reduce time complexity • Recursion adds clarity and reduces the time needed to write and debug code • Recursion is better at tree traversal • CONS • Recursion uses more memory • Recursion can be slow • More overhead due to function calls.

  9. RECURSIVE PROGRAM/ ALGORITHM • A recursion algorithm uses "smaller (or simpler)" input values to achieve the result for the current input. A recursion method may solve a problem if it can be solved by solving smaller versions of the same issue, and the smaller versions decrease to easily solved problems. simplify the computation for one generation's element to the previous generation's element. • If a set or a function is defined recursively, then a recursive algorithm to compute its members or values mirrors the definition. Initial steps of the recursive algorithm correspond to the basis clause of the recursive definition and they identify the basis elements. They are then followed by steps corresponding to the inductive clause, which reduce the computation for an element of one generation to that of elements of the immediately preceding generation..

  10. KEY COMPONENTS OF A RECURSIVE ALGORITHM DESIGN • 1. What is a smaller identical problem(s)? • Decomposition • 2. How are the answers to smaller problems combined to form the answer to the larger problem? • Composition • 3. Which is the smallest problem that can be solved easily (without further decomposition)? • Base/ stopping case

  11. RECURSIVE EXAMPLE OF FACTORIAL n! = n · (n-1) · (n-2) ··· 3· 2· 1 n! = 4· 3· 2· 1 = 24 2! = 2· 1 = 2 1! = 1 0! = 1 Fact (n)

  12. RECURSIVE EXAMPLE OF FACTORIAL n! = n · (n-1) · (n-2) ··· 3· 2· 1 n! = n · (n-1)! (n-1)! = (n-1) · (n-2) ··· 3· 2· 1 2! = 2· 1! = 2 1! = 1· 0! = 1 0! = 0· (-1)! if n≥ 1 otherwise (if n =0) fact (n)

  13. RECURSIVE EXAMPLE OF FACTORIAL if n≥ otherwise (if n =0) int fact (int n){ //assumming that n is a positive integer or 0 if (n >= 1) { return n* (n-1) + fact(n- 1 ); } else { return 1; } } f (0) f (1) f(4) • f (0) -> 1 • f(1) -> 1 * f (0) • f(2) -> 2 * f (2) • f(3) -> 3 * f(2) • f(4) -> 4 * f(3)

  14. RECURSIVE EXAMPLE ATM/LINE ANALOGY

  15. SOLVING FACTORIAL USING RECURSION

  16. KEY TO SUCCESSFUL RECURSION • Recursion will not work correctly unless you follow some specific guidelines: • The heart of the method definition can be an if-else statement or some other branching statement. • One or more of the branches should include a recursive invocation of the method. • Recursive invocations should use "smaller" arguments or solve "smaller" versions of the task. • One or more branches should include no recursive invocations. These are the stopping cases or base cases.

  17. RECURSION VS. ITERATION Recursion is when a function calls itself within its code, thus repeatedly executing the instructions present inside it. Iterationis when a loop repeatedly executes the set of instructions like "for" loops and "while" loops.

  18. DIFFERENCE BETWEEN RECURSION & ITERATION Recursion and iteration are both different ways to execute a set of instructions repeatedly. The main difference between these two is that in recursion, we use function calls to execute the statements repeatedly inside the function body, while in iteration, we use loops like “for” and “while” to do the same.

  19. WHY RECURSION INSTEAD OF ITERATION? • Recursion is usually slower than iteration due to the overhead of maintaining the stack. • Recursion uses more memory than iteration. • Recursion makes the code smaller.

  20. WHEN TO USE RECURSION AND ITERATION? • When should you use iteration, and when use recursion? There are (at least) these three factors to consider: • Iterative functions are typically faster than their recursive counterparts. So, if speed is an issue, you would normally use iteration. • If the stack limit is too constraining then you will prefer iteration over recursion. • Some procedures are very naturally programmed recursively, and all but unmanageable iteratively. Here, then, the choice is clear.

  21. SUMMARY .......

More Related