1 / 25

Recursion

Recursion. by Ender Ozcan. Recursion in computing. Recursion in computer programming defines a function in terms of itself. Advantage : Infinite set of possible sentences, designs or other data can be defined, parsed or produced by a finite computer program. Example: the natural numbers.

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 by Ender Ozcan

  2. Recursion in computing • Recursion in computer programming defines a function in terms of itself. • Advantage: Infinite set of possible sentences, designs or other data can be defined, parsed or produced by a finite computer program.

  3. Example: the natural numbers • The canonical example of a recursively defined set is given by the natural numbers: • 0 is in N • if n is in N, then n + 1 is in N • The set of natural numbers is the smallest set satisfying the previous two properties. • Here's an alternative recursive definition of N: • 0, 1 are in N; • if n and n + 1 are in N, then n + 2 is in N; • N is the smallest set satisfying the previous two properties.

  4. Example: Factorial integer Fact ( integer X ) { if X < 0 then return -1; // invalid arg if X = 1 then return 1; return Fact(X-1) * X; } • Computing 4!, requires a call to Fact(4) • Fact(4)  returns Fact(3)*4 6*4=24 • Fact(3)  returns Fact(2)*3 2*3=6 • Fact(2) returns Fact(1)*2 1*2=2 • Fact(1)  returns 1

  5. Basic steps of recursive programs • Initialize the algorithm. • Termination Criteria – check to see whether the current value(s) being processed match the base case. If so, process and return the value. • Redefine the answer in terms of a smaller or simpler sub-problem or sub-problems. • Run the algorithm on the sub-problem. • Combine the results in the formulation of the answer. • Return the results.

  6. Example – Sum of n numbers • Suppose we have a list of n numbers, and we want to sum them. • Initialize the algorithm. This algorithm's seed value is the first number to process and is passed as a parameter to the function. • Check for the base case. The program needs to check and see if the list is empty. If so, we return zero because the sum of all members of an empty list is zero. • Redefine the answer in terms of a simpler sub-problem. We can define the answer as the sum of the rest of the list plus the contents of the current number. To determine the sum of the rest of the list, we call this function again with the next number. • Combine the results. After the recursive call completes, we add the value of the current node to the results of the recursive call.

  7. data data data head next next next tail 3 159 78 Linked List Linked list is a basic data structure, used to hold a sequence of items in memory <3, 159, 78>

  8. C implementation using linked lists int sum_list(struct list_node *l) { if(l == NULL) return 0; return l.data + sum_list(l.next); }

  9. Comparing loops with recursive functions

  10. To Loop or Not To Loop Rule of thumb for programmers • If you can solve a problem using iteration, avoid recursion

  11. Fibonacci Sequence • <0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, …> • The original problem that Fibonacci investigated (in the year 1202) was about how fast rabbits could breed in ideal circumstances (assuming they don’t die).

  12. nth Fibonacci Number • fib(n) = fib(n – 1) + fib (n – 2) for n>1, and fib(0)=0, fib(1)=1. • C program computing nth fibonacci number: int fib(int n) { if (n == 0) return 0; if (n == 1) return 1; return fib(n-1) + fib(n-2); }

  13. nth Fibonacci Number-Revisited fib(5) fib(4) fib(3) fib(3) fib(2) fib(1) fib(2) fib(1) fib(2) fib(1) fib(0) fib(1) fib(0) Try to avoid solving overlapping sub-problem instances fib(1) fib(0)

  14. Recursive vs. Iterative Solution int fib(int n) { if (n == 0) return 0; if (n == 1) return 1; return fib(n-1) + fib(n-2); } int fib(int n) { int i, *fib=new int[n]; fib[0]=0; fib[1]=1; for (i=2; i<n; i++) fib[i]=fib[i-1]+fib[i-2]; return fib[n]; } Running time is ~ a(n+n) Running time is ~ cn

  15. Tower of Hanoi Puzzle • Puzzle was invented by the French mathematician Edouard Lucas in 1883. • We are given a tower of 8 disks, initially stacked in increasing size on one of three pegs. The objective is to transfer the entire tower to one of the other pegs, moving only one disk at a time and never a larger one onto a smaller.

  16. Src Aux Dst Recursive Solution • Call the three pegs Src (Source), Aux (Auxiliary) and Dst (Destination). n disks

  17. Src Aux Dst Recursive Solution – Step 1 • Move the top n-1 disks from Src to Aux (using Dst as an intermediary peg) n-1 disks

  18. Src Aux Dst Recursive Solution – Step 2 • Move the bottom disks from Src to Dst

  19. Src Aux Dst Recursive Solution – Step 3 • Move n-1 disks from Aux to Dst (using Src as an intermediary peg)

  20. Pseudocode • Solve(n, Src, Aux, Dst) • if n is 0 return • Solve(n-1, Src, Dst, Aux) • Move from Src to Dst • Solve(n-1, Aux, Src, Dst)

  21. Src Aux Dst Analysis of the Algorithm I • Assume than T(n) is the number of steps required to move n disks from Src to Dst n disks

  22. Src Aux Dst Analysis of the Algorithm II • Moving the top n-1 disks from Src to Aux will take T(n-1) steps n-1 disks

  23. Src Aux Dst Analysis of the Algorithm III • Moving the bottom disk from Src to Dst will take a single step

  24. Src Aux Dst Analysis of the Algorithm IV • Moving n-1 disks from Aux to Dst will take T(n-1) steps

  25. Overall Complexity • T(n) = 2T(n-1) + 1 {T(n-1) = 2T(n-2) + 1 } • T(n) = 22T(n-2) + 2 + 1 substitute T(n-1) • T(n) = 23T(n-3)+ 22 + 2 + 1 • … • T(n) = 2n-1 T( n-(n-1)) +…+ 23 +22+2+1 T(1)=1 • T(n) = 2n-1 +…+ 23 +22+2+1 (geometric series) • T(n) = 2n- 1

More Related