Presentation Description

132 Views

Download Presentation
## CHAPTER 9

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -

**CHAPTER 9**RECURSION Chapter 9: Recursion**Recursion**• Concept of recursion • A recursive: Benefit and Cost • Comparison : Iterative and recursive functions • Example : Simple and complex • Converting recursive to non-recursive equivalent functions Chapter 9: Recursion**Simple Recursion**• Concept: defining a solution in terms of a simpler version of the solution • Recursion is often used instead of iteration • C permits a function to call itself. • We say that a function is calling itself when we can find a function call inside the body of the function with the same name as it has. • This function call is invoking itself. Chapter 9: Recursion**What is a recursive solution?**• A solution of a problem is a recursive solution if it is expressible in a smaller version of itself and if ultimately a simple non recursive solution can be found. • Hence, a function that call itself directly or indirectly to solve a smaller version of its task until a final call does not require a self call is a recursive function • A recursive technique is an alternative to iteration technique (using loop), a commonly used technique by student due to its simplicity. Chapter 9: Recursion**A Recursive Solution**• In order to implement a recursion to solve a problem, you need to consider some of these questions that are significantly useful such as • How can your problem being defined in term of smaller version of itself ? • How does each recursive call diminish the size of the problem? • What instance of the problem can serve as the case base? • As the problem size diminishes, will you reach the case base? Chapter 9: Recursion**A Recursive: Benefit & Cost**• The Benefits: • often your code will be shorter • it is usually easier to define the solution recursively • and writing the code is just a matter of implementing the definition • more elegant solution • you may not need local variables to implement your solution • some solutions are only expressible recursively (or at least are only easily expressible recursively) • this is true of some tree and graph operations, and search problems that require backtracking • The Cost/disadvantages: • recursive solutions are often harder to debug • recursion requires numerous function calling • this can lead to much poorer run-time performance • takes up more memory space (on the run-time stack) • in a few cases, solution may be much less efficient than an iterative solution**A Recursive: Examples of usage**• Factorial: • fact(n): if(n < 2) 1 else n * fact(n – 1) • Fibonacci: • fib(n): if (n < 3) 1 else fib(n – 1) + fib(n – 2) • Finding largest item in an array: • largest(a, n): if (n = = 0) return a[0] else return max(a[n], largest(a, n-1)) • We can also find the largest using a binary search like strategy: • largest(a, low, high): • if(low = = high) return a[low] • else return max(largest(a, low, (low+high)/2)), largest(a, (low+high)/2+1, high)) • Writing elements backwards • output_backward(S, n): if(n>=0) • output_backwards(S, n-1) • output character in S at position (n)**Comparison: Iterative Functions**• As mentioned earlier, we can use a recursion technique as an alternative to the iteration technique. • Assume we want to calculate the summation value of 1 + 2 + 3 + 4 + 5. • We can use iteration technique to accomplish this task int cal (int n) {int j=1; while ( j < n) { sum = sum + j; j++; } } Chapter 9: Recursion**Comparison: Recursive Functions**• Alternatively, we can use recursion technique to solve the problem. int cal( int n) { if (n == 1 ) return 1; else return ( n + cal(n-1)); } Chapter 9: Recursion**Comparison: Recursive Functions (con’t)**N=4 => 4 + 3 + 2 + 1 = 10 “return ( n + cal(n-1))” Final value = 10 is returned 4 4 4 + 6 = 10 is returned 4 + cal (3) 4 + cal (3) 3 + 3 = 6 is returned 3 + cal (2) 3 + cal (2) 2 + 1 = 3 is returned 2 + cal (1) 2 + cal (1) 1 returned 1 1 a) Sequence of recursive calls b) Value returned from each recursive call (backtracking) Chapter 9: Recursion**A Recursive: Simple example1**#include <stdio.h> void print_integers(int); int main( ) { int number; printf(“Enter an integer: “); scanf(“%d”, number); print_integers(number); } void print_integers(int n) { if (n>=1) { printf(“%d\n”, n); print_integers(n-1); } } Chapter 9: Recursion**A Recursive: Simple example1 (con’t)**• The function print_integers also has a function call in its body to itself. • The parameter in the statement print_integers(n-1); is 1 less than n, the value in the previous call. • In other words, the problem that the function is expected to solve is smaller or simpler version of the previous problem. • Thus we can say that the problem size is gradually diminishing. Chapter 9: Recursion**A Recursive: Simple example2**• It is important to understand how a recursive function works. Assuming you have a factorial function as below int Fact ( int n) { if ( n == 1) return 1; i else return (n * Fact ( n-1)); ii } Chapter 9: Recursion**A Recursive: Simple example2 (con’t)**• You can find the value of factorial 4 by calling Fact(4), but how does this function works and produce the output of Fact(4). Fact(4) = ( 4 * Fact(3)) = ( 4 * (3 * Fact(2))) = (4 * ( 3 * (2 * Fact(1)))) = (4 * (3 * ( 2 * 1))) = (4 * (3 * (2))) = (4 * (6)) = 24 Chapter 9: Recursion**Fact (4)**Fact (4) i ii 4 * Fact (3) Fact 3 i ii. 3 * Fact (2) Fact 2 i ii. 2 * Fact (1) Fact 4 i ii 4 * 6 Fact 1 i return 1 ii Fact 3 i ii. 3 * 2 Fact 2 i ii. 2 * 1 Tracing a Recursion Function Using graphical representation, this process can be illustrated as below Chapter 9: Recursion**A Recursive: Simple example3**• Assuming we want to develop a recursive solution to calculate the value of Xn . Firstly, we may need to study the formal notation solution of this problem. We can write the solution of Xn as below X0 = 1…………………(i) base case statement Xn = X + Xn-1…………(ii) recursive statement • Statement (i) will serve as our base case statement and the statement (ii) will serve as the recursive statement. Chapter 9: Recursion**A Recursive: Simple example3 (con’t)**• So by having the definition, we can develop the recursive function as below. int pow ( int X, int n) { if ( n == 0) return 1; else return (X * pow ( X,n-1)); } Chapter 9: Recursion**A Recursive: Simple example3 (con’t)**• Exercise: • Trace example3 using a graphical representation as discussed in slide no. 10. • The initial value of variable X=2 and n=3 • What is final output returned. Chapter 9: Recursion**Example 4: Fibonacci numbers – Iteration technique**Fibonacci - a series of numbers created by adding the last two numbers in the series to produce the next number in the series, i.e., 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, etc. int fibonacci(int seq_num) { int first = 0, second = 1, count = 3, fibo; if(seq_num == 0) fibo = first; else if(seq_num == 1) fibo = second; else while(count <= seq_num) { fibo = first + second; first = second; second = fibo; count++; } return fibo; } Chapter 9: Recursion**Example 4: Fibonacci numbers - Recursive technique**int fibonacci(int seq_num) { if(seq_num == 0) return 0; else if(seq_num == 1) return 1; else return fibonacci(seq_num -1) + fibonacci(seq_num -2); } • This has simplified the iteration technique in the previous slide Chapter 9: Recursion**Tree of sub problems: Fibonacci**(fib 5) (fib 3) (fib 4) (fib 1) (fib 2) (fib 2) (fib 3) (fib 0) (fib 1) (fib0)(fib 1) (fib 1) (fib 2) (fib 0)(fib 1) Chapter 9: Recursion**SUMMARY**• This chapter has introduced you; • How to construct simple recursion • How recursion can simplify the iteration technique in certain cases • A few examples on recursion • Fibonacci • Factorial • Power of Chapter 9: Recursion