Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 11 Recursion. A recursive function is a function that calls itself either directly, or indirectly through another function; it is an alternative.

Similar presentations


Presentation on theme: "Lecture 11 Recursion. A recursive function is a function that calls itself either directly, or indirectly through another function; it is an alternative."— Presentation transcript:

1 Lecture 11 Recursion

2 A recursive function is a function that calls itself either directly, or indirectly through another function; it is an alternative to iteration A recursive solution is generally less efficient in terms of system overhead, due to the overhead of extra function calls; however recursive functions – allow us to think of solutions to problems that may be recursive in nature – allow us to work with data structures that are best accessed recursively A recursive problem Imagine a computer environment that does not support the multiplication operator (*), but it does support addition (+)

3 Multiplication using the + operator public int multiply (int m, int n) // IN: m and n, values to be multiplied // PRE: m and n are defined and n > 0 // POST: returns m * n // RETURNS: Product of m * n if n is positive; otherwise, returns m { if (n <= 1) return m; else return m + multiply (m, n - 1); }

4 Tracing the code Trace: In order to display multiply(6(m), 4(n)), we need to know and therefore call – 6 + multiply (6, 3) – in order to calculate this result we need to know and therefore call 6 + multiply (6, 2) in order to calculate this result we need to know – 6 + multiply (6, 1) we know the result of multiply (6, 1) because n = 1, 6 is returned to the calling function – 6 + 6 (returned) = 12 and is returned to the calling function 6 + 12 (returned) = 18 and is returned to the calling function – 6 + 18 (returned) = 24 and is returned to the calling function in main() 24 is displayed notice the multiply() function was called 5 times

5 Algorithm of Recursive Functions The recursive functions we will work with will generally consist of an if statement with the form shown below if the stopping case is reached// if (n <= 1) solve the problem// return m else reduce the problem using recursion // return m + multiply(m, n - 1 ) Problems that lend themselves to recursive solution have the following characteristics – one or more simple cases of the problem (stopping cases) have a straightforward, non-recursive solution – For the other cases, there is a process (using recursion) for substituting one or more reduced cases of the problem closer to a stopping case – Eventually the problem can be reduced to stopping cases only

6 Factorial = 6! A factorial is a number n such that n = n * (n-1) * (n - 2) * (n - 3) … (n - (n - 2)) * ( n - (n -1)) * 1 or n = n * factorial(n - 1) 6 factorial 6! = 6 * 5 * 4 * 3 * 2 * 1 or recursively 6! = 6 * 5! -> 5 * 4! -> 4 * 3! -> 3 * 2! -> 2 * 1 1! = 1 by definition (the stopping case)

7 int Factorial(int n) // IN: n, value to find the factorial of // PRE: n is defined and n > 0 // POST: returns n! // RETURNS: Factorial of n if n is positive; otherwise, returns 1 { if (n == 0) return 1; else return n * Factorial (n - 1); }

8 Head Recursion If the recursive call happens at the beginning of the code, then we have a case of head recursion Eg: public void foo(int n){ if (n>0) foo(n-1); System.out.println(n); }

9 Tail Recursion If the recursive call happens at the end of the code, then we have a case of tail recursion Eg: public void foo(int n){ if (n>0) System.out.println(n); foo(n-1); } Equivalent to running a loop Exercise: convert the code to an iterative loop

10 Tower of Hanoi Problem Suppose we have 3 pegs named A, B and C We have 3 disks named (1-largest, 2-next, 3- smallest) in peg A. How do we move 3 pegs from A to C using B? – We can only move one disk at a time – We cannot place a larger disk on the top of a smaller one Now how do we generalize this algorithm? A BC

11 A Recursive Algorithm If you have one disk, then the problem is solved – Move disk from source to destination If you have 2 disks how would you solve the problem? If you have n disks (n > 1), then divide and conquer – Move (n-1) disks from source to intermediate – Move one disk from source to destination – Move (n-1) disks from intermediate to destination This gives us a very elegant solution

12 Tower of Hanoi Code // TOWER OF HANOI ALGORITHM public void hanoi(int n, char source, char dest, char inter) { if (n==1) System.out.println("move disk ” + n + " from “ +source+" to “+dest) ; else { hanoi(n-1,source,inter,dest); System.out.println (“move disk ”+ n + " from “+source+" to “+dest) ; hanoi(n-1,inter,dest,source); } A=source, B=destination, C=intermediate A BC

13 Understanding Hanoi Recursion (n=3) left call - source to inter using dest – right call - inter to dest using source Hanoi(3,A,B,C) Hanoi(2,A,C,B)Hanoi(2,C,B,A) Hanoi(1,A,B,C)Hanoi(1,B,C,A)Hanoi(1,C,A,B)Hanoi(1,A,B,C) A to B A to CC to B A to B B to CC to AA to B


Download ppt "Lecture 11 Recursion. A recursive function is a function that calls itself either directly, or indirectly through another function; it is an alternative."

Similar presentations


Ads by Google