Presentation is loading. Please wait.

Presentation is loading. Please wait.

Recursion. What is recursion? smaller version Sometimes, the best way to solve a problem is by solving a smaller version of the exact same problem first.

Similar presentations


Presentation on theme: "Recursion. What is recursion? smaller version Sometimes, the best way to solve a problem is by solving a smaller version of the exact same problem first."— Presentation transcript:

1 Recursion

2 What is recursion? smaller version Sometimes, the best way to solve a problem is by solving a smaller version of the exact same problem first smaller problem Recursion is a technique that solves a problem by solving a smaller problem of the same type

3 Recursion More than programming technique: a way of describing, defining, or specifying things. a way of designing solutions to problems (divide and conquer).

4 Basic Recursion 1. Base cases: Always have at least one case that can be solved without using recursion. 2. Make progress: Any recursive call must make progress toward a base case.

5 Mathematical Examples Fibonacci Sequence: 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, … Fibonacci Function: Fib(0) = 1 // base case Fib(1) = 1 // base case Fib(n) = Fib(n-1) + Fib(n-2) // recursive call, n>1 Unlike most recursive algorithms:  two base cases, not just one  two recursive calls, not just one

6 Euclid's Algorithm In about 300 BC, Euclid wrote an algorithm to calculate the greatest common divisor (GCD) of two numbers x and y where (x < y). This can be stated as: 1. Divide y by x with remainder r. 2. Replace y by x, and x with r. 3. Repeat step 1 until r is zero. When this algorithm terminates, y is the highest common factor.

7 GCD(34017, 16966) Euclid's algorithm works as follows: 34,017/16,966 produces a remainder 85 16,966/85 produces a remainder 51 85/51 produces a remainder 34 51/34 produces a remainder 17 34/17 produces a remainder 0 The highest common divisor of 34,017 and 16,966 is 17.

8 Key Elements of Euclid's Algorithm : Simple arithmetic operations (calculating the remainder after division) Comparison of a number against 0 (test) Ability to repeatedly execute the same set of instructions (loop) Any computer programming language has these basic elements.

9 Factorial Sequence Factorial Function factorial(0) = 1 factorial(n) = n * factorial(n-1) [for n>0] Compute factorial(3).

10 Factorial Sequence Factorial Function factorial(0) = 1 factorial(n) = n * factorial(n-1) [for n>0] Compute factorial(3). factorial(3) = 3 * factorial(2) = 3 * ( 2 * factorial(1) ) = 3 * ( 2 * ( 1 * factorial(0) ) = 3 * ( 2 * ( 1 * 1 ) )) = 6

11 Coding the factorial function Recursive Implementation int factorial(int n) { if (n==0) // base case return 1; else return n * factorial(n-1); }

12 Recursive Call Stack

13 Recursion vs. Iteration Recursion is based upon calling the same function over and over. Iteration simply `jumps back' to the beginning of the loop. A function call is often more expensive than a jump.

14 Recursion vs. Iteration Iteration can be used in place of recursion An iterative algorithm uses a looping construct A recursive algorithm uses a branching structure Recursive solutions are often less efficient, in terms of both time and space, than iterative solutions Recursion can simplify the solution of a problem, often resulting in shorter, more easily understood source code

15 Recursion to Iteration Conversion Most recursive algorithms can be translated by a fairly mechanical procedure into iterative algorithms. Sometimes this is very straightforward most compilers detect a special form of recursion, called tail recursion, and automatically translate into iteration automatically. Sometimes, the translation is more involved May require introducing an explicit stack with which to `fake' the effect of recursive calls.

16 Coding Factorial Function Iterative implementation int factorial(int n) { int fact = 1; for(int count = 2; count <= n; count++) fact = fact * count; return fact; }

17 Combinations: n choose k () Given n things, how many different sets of size k can be chosen? n n-1 n-1 = +, 1 < k < n(recursive solution) k k k-1 n n! =, 1 < k < n(closed-form solution) k k!(n-k)! with base cases: n = n (k = 1), = 1 (k = n) 1 n Pascal’s Triangle

18 int combinations(int n, int k) { if(k == 1) // base case 1 return n; else if (n == k) // base case 2 return 1; else return(combinations(n-1, k) + combinations(n-1, k-1)); } Combinations: n choose k

19 Combinations:

20 Writing a Recursive Function Determine the base case(s) (the one for which you know the answer) Determine the general case(s) (the one where the problem is expressed as a smaller version of itself) Verify the algorithm (use the "Three-Question-Method")

21 Three-Question Method The Base-Case Question: Is there a non-recursive way out of the function, and does the routine work correctly for this "base" case? The Smaller-Caller Question: Does each recursive call to the function involve a smaller case of the original problem, leading inescapably to the base case? The General-Case Question: Assuming that the recursive call(s) work correctly, does the whole function work correctly?

22 Euclid's Algorithm Recall: Find greatest common divisor (GCD) of two integers x and y where (x < y): 1. y / x, save remainder r. 2. Replace y by x, and x with r. 3. Repeat until r is zero. When this algorithm terminates, y is the highest common factor.

23 What is the base case(s)? (1) If first > last, return false (2) If item==info[midPoint], return true What is the general case? if item < info[midPoint] search the first half if item > info[midPoint], search the second half Recursive Binary Search

24 boolean binarySearch(Item info[], Item item, int first, int last) { int midPoint; if(first > last) // base case 1 return false; else { midPoint = (first + last)/2; if(item < info[midPoint]) return BinarySearch(info, item, first, midPoint-1); else if (item == info[midPoint]) { // base case 2 item = info[midPoint]; return true; } else return binarySearch(info, item, midPoint+1, last); } Recursive Binary Search

25 Implementing Recursion What happens when a function gets called? int b(int x) { int z,y; ……………… // other statements z = a(x) + y; return z; } int a(int w) { return w+w; }

26 When a Function is Called An activation record is stored into a stack (run-time or call stack) The computer has to stop executing function b and starts executing function a Since it needs to come back to function b later, it needs to store everything about function b that is going to need (x, y, z, and the place to start executing upon return) Then, x from a is bounded to w from b Control is transferred to function a

27 After function a is executed, the activation record is popped out of the run-time stack All the old values of the parameters and variables in function b are restored and the return value of function a replaces a(x) in the assignment statement When a Function is Called

28 When Recursive Function Called Except the fact that the calling and called functions have the same name, there is really no difference between recursive and non-recursive calls int f(int x) { int y; if(x==0) return 1; else { y = 2 * f(x-1); return y+1; }

29 =f(3) =f(2) =f(1) 2*f(2) 2*f(1) =f(0)

30 When to Use Recursion When the depth of recursive calls is relatively "shallow" The recursive version does about the same amount of work as the non- recursive version The recursive version is shorter and simpler than the non-recursive solution

31 Benefits of Recursion Recursive functions are clearer, simpler, shorter, and easier to understand than their non-recursive counterparts. The program directly reflects the abstract solution strategy (algorithm). From a practical software engineering point of view, greatly enhances the cost of maintaining the software.

32 Disadvantages of Recursion Makes it easier to write simple and elegant programs, but it also makes it easier to write inefficient ones. Use recursion to ensure correctness, not efficiency. My simple, elegant recursive algorithms are inherently inefficient.

33

34 Recursive InsertItem (Sorted List) location

35 What is the base case(s)? If the list is empty, insert item into the empty list If item < location.info, insert item as the first node in the current list What is the general case? Insert(location.next, item) Recursive InsertItem (Sorted List)

36 void insert(Node node, Item item) { if(node == NULL) || (item < node.getItem()) { // base cases NodeType temp = node; node = new NodeType ; location.setElement(item); location.setNext(temp); } else insert(node.getNext(), item); // general case } void insertItem(Item newItem) { insert(head, newItem); } Recursive InsertItem (Sorted List)

37 - No "predLoc" pointer is needed for insertion location

38  Space: Every invocation of a function call requires:  space for parameters and local variables  space for return address Thus, a recursive algorithm needs space proportional to the number of nested calls to the same function. Recursion Overhead

39  Time: The operations involved in calling a function  allocating, and later releasing, local memory  copying values into the local memory for the parameters  branching to/returning from the function All contribute to the time overhead.

40 Cumulative Affects  If a function has very large local memory requirements, it would be very costly to program it recursively.  Even if there is very little overhead in a single function call, recursive functions often call themselves many times, which can magnify a small individual overhead into a very large cumulative overhead.

41 Cumulative Affects - Factorial int factorial(int n) { if (n == 0) {return 1;} else {return n * factorial(n-1);} }  Little overhead  only one word of local memory for the parameter n.  However, when we try to compute factorial(20)  21 words of memory are allocated.

42 Cumulative Affects - Factorial (cont’d) factorial(20) -- allocate 1 word of memory, call factorial(19) -- allocate 1 word of memory, call factorial(18) -- allocate 1 word of memory,. call factorial(2) -- allocate 1 word of memory, call factorial(1) -- allocate 1 word of memory, call factorial(0) -- allocate 1 word of memory, At this point, 21 words of memory and 21 activation records have been allocated.

43 Recursive Functions int f(int x) { int y; if(x==0) return 1; else { y = 2 * f(x-1); return y+1; }

44 Iteration as a special case of recursion Iteration is a special case of recursion: void do_loop () { do {... } while (e); } is equivalent to: void do_loop () {... ; if (e) do_loop(); } A compiler can recognize instances of this form of recursion and turn them into loops or simple jumps.

45 Recursive DeleteItem (Sorted List) location

46 What is the size factor? The number of elements in the list What is the base case(s)? If item == location->info, delete node pointed by location What is the general case? Delete(location->next, item) Recursive DeleteItem (sorted list)

47 template void Delete(NodeType * &location, ItemType item) { if(item == location->info)) { NodeType * tempPtr = location; location = location->next; delete tempPtr; } else Delete(location->next, item); } template void SortedType ::DeleteItem(ItemType item) { Delete(listData, item); } Recursive DeleteItem (sorted list) (cont.)

48 Recursion can be very inefficient is some cases 15

49 We have seen one form of circularity already in our classes, with a For loop. Int x; For (x=0; x<=10; x++) { cout<<x; }

50 Problems solving used loops In a for loop, we have a set loop structure which controls the length of the repetition. Many problems solved using loops may be solved using a recursion. In recursion, problems are defined in terms of smaller versions of themselves.

51 Power function There are recursive definitions for many mathematical problems: The function Power (used to raise the number y to the xth power). Assume x is a non-negative integer: Y^x = 1 if x is 0; otherwise, Y*Y^(x-1)

52 Power Function 2^3 = 2*2^2 = 2 * 4 = 8 2^2 = 2*2^1 = 2 * 2 = 4 2^1 = 2*2^0= 2 * 1 = 2 2^0 = 1

53 Factorial Function The factorial function has a natural recursive definition: n!= 1, if n = 0 or if n = 1; otherwise, n! = n * (n - 1)!

54 For example: 5! = 5*4! = 5*24 4! = 4*3! = 4*6 3! = 3*2! = 3*2 2! = 2*1! = 2*1 1! = 1

55 Excessive Recursion When a program runs too deep: When a simple loop runs more efficiently: Fibonacci sequence:

56 Ackermann’s Function “…one of the fastest growing non- primitive recursive functions. Faster growing than any primitive recursive function.” It grows from 0 to 2^65546 in a few breaths.

57 Basis for Ackermann’s If A(0, n)=n+1, by definition If A(m, 0)=A(m-1, 1) else, A(m, n)=A(m-1, A(m, n-1))… …until A(0, A(1, A(…m-2, n-1)))…back to definition

58 Example A(2, 2)=A(1, A(2, 1))=A(0, A(1, A(2, 1))) …=A(1, A(2, 1))+1=A(0, A(1, A(2, 0)))+1 …=A(1, A(1, 1))+2=A(0, A(1, A(1, 0))) …=A(1, A(0, 1))+3=A(0, A(0, 0))+5=7!!!

59 Shortcuts: If A(0, n)=n+1 If A(1, n)=n+2 If A(2, n)=2n+3 If A(3, n)=2^n+3 If A(4, n)=2^(n+3)*2 If A(5, n)=TOO MUCH!!!!!

60 A(4, 1)=13 A(4, 2)=65533 A(4, 3)=2^65536-3 A(4, 4)=2^(2^(65536)-3)

61 “Ackermann’s function is a form of meta-multiplication.”Dr. Forb. a+b=adding the operand a*b=adding the operand “a” to itself b times a^b=multiplying the operand “a” by itself b times a@b=a^b, b times…a@@b=a@b, b times

62 Recursion Recursion can be seen as building objects from objects that have set definitions. Recursion can also be seen in the opposite direction as objects that are defined from smaller and smaller parts. “Recursion is a different concept of circularity.”(Dr. Britt, Computing Concepts Magazine, March 97, pg.78)

63 Suppose that we have a series of functions for finding the power of a number x. pow0(x) = 1 pow1(x) = x = x * pow0(x) pow2(x) = x * x = x * pow1(x) pow3(x) = x * x * x = x * pow2(x) We can turn this into something more usable by creating a variable for the power and making the pattern explicit: pow(0,x) = 1 pow(n,x) = x * pow(n-1,x) Finding the powers of numbers

64 For instance: 2**3 = 2 * 2**2 = 2 * 4 = 8 2**2 = 2 * 2**1 = 2 * 2 = 4 2**1 = 2 * 2**0 = 2 * 1 = 2 2**0 = 1

65 Almost all programming languages allow recursive functions calls. That is they allow a function to call itself. And some languages allow recursive definitions of data structures. This means we can directly implement the recursive definitions and recursive algorithms that we have just been discussing. For example, the definition of the factorial function factorial(0) = 1 factorial(n) = n * factorial(n-1) [ for n > 0 ]. can be written in C without the slightest change: int factorial(int n) { if (n == 0) return 1 ; else return n * factorial(n-1) ; }

66 Basic Recursion What we see is that if we have a base case, and if our recursive calls make progress toward reaching the base case, then eventually we terminate. We thus have our first two fundamental rules of recursion:

67 Euclid's Algorithm Euclid's Algorithm determines the greatest common divisor of two natural numbers a, b. That is, the largest natural number d such that d | a and d | b. GCD(33,21)=3 33 = 1*21 + 12 21 = 1*12 + 9 12 = 1*9 + 3 9 = 3*3

68 The main thing to note here is that the variables that will hold the intermediate results, S1 and S2, have been declared as global variables. This is a mistake. Although the function looks just fine, its correctness crucially depends on having local variables for storing all the intermediate results. As shown, it will not correctly compute the fibonacci function for n=4 or larger. However, if we move the declaration of s1 and s2 inside the function, it works perfectly. This sort of bug is very hard to find, and bugs like this are almost certain to arise whenever you use global variables to storeintermediate results of a recursive function.

69 {Non-recursive version of Power function} FUNCTION PowerNR (Base : real; Exponent : integer) : real; {Preconditions: Exponent >= 0} {Accepts Base and exponent values} {Returns Base to the Exponent power} VAR Count : integer; {Counts number of times BAse is multiplied} Product : real; {Holds the answer as it is being calculated} BEGIN Product := 1; FOR Count := 1 TO Exponent DO Product := Product * Base; PowerNR := Product END; {PowerNR}

70 Consider the following program for computing the Fibonacci Sequence int fibonacci (int n) { if (n == 0) {return 1}; else if (n == 1) {return 1}; else {return fibonacci(n-1) + fibonacci(n- 2)}; }


Download ppt "Recursion. What is recursion? smaller version Sometimes, the best way to solve a problem is by solving a smaller version of the exact same problem first."

Similar presentations


Ads by Google