Presentation is loading. Please wait.

Presentation is loading. Please wait.

Recursion In general there are two approaches to writing repetitive algorithms. One uses loops(while, do while and for): the other uses recursion. Recursion.

Similar presentations


Presentation on theme: "Recursion In general there are two approaches to writing repetitive algorithms. One uses loops(while, do while and for): the other uses recursion. Recursion."— Presentation transcript:

1 Recursion In general there are two approaches to writing repetitive algorithms. One uses loops(while, do while and for): the other uses recursion. Recursion is a repetitive process in which a function calls itself. The ability to invoke itself enables a recursive function to be repeated with different parameter values. Generally, a recursive solution is less efficient than an interative solution in terms of computer time due to the overhead for the extra function calls; however, in many instances, the use of recursion enables us to specify a very natural, simple solution to a problem that would otherwise be very difficult to solve. A repetitive function is defined recursively whenever the function appears within the definition itself. For example, the factorial function can be defined recursively. The factorial of a number is the product of the integral values from 0 to the number : Factorial ( 0 ) = 1 Factorial ( n ) = n * (n - 1) * (n - 2) * (n - 3) * … (n - (n - 1)) or Factorial ( n ) = n * ( Factorial( n - 1) ) if n > 0 Example: Factorial ( 4 ) = 4 * (4 - 1) * (4 - 2) * (4 - 3) Factorial ( 4 ) = 4 * 3 * 2 * 1 Factorial ( 4 ) = 24

2 Recursion The decomposition of Factorial ( 3 ) using the formula above is shown below: Factorial ( 3 ) = 3 * Factorial ( 2 ) Factorial ( 2 ) = 2 * Factorial ( 1 ) Factorial ( 1 ) = 1 * Factorial ( 0 ) Factorial ( 0 ) = 1 Factorial ( 3 ) = 3 * 2 = 6 Factorial ( 2 ) = 2 * 1 = 2 Factorial ( 1 ) = 1 * 1 = 1

3 Recursion The following is a function, written in C, to solve the factorial problem interatively: /* * Computes n! * Pre: n is greater than or equal to zero */ int factorial(int n) { int i, /* local variables */ product = 1; /* Compute the product n x (n-1) x (n-2) x... x 2 x 1 */ for (i = n; i > 1; --i) { product = product * i; } /* Return function result */ return (product); }

4 To solve the Factorial problem using recursion ( or any other recursive solution) the function must include the following: 1) statements to solve the simple case. and 2) a statement that reduces the size of the problem by a recursive call (general case) The recursive algorithm will generally consist of an if statement with the following form: if this is a simple case solve it else redefine (reduce) the problem using recursion Therefore, every recursive call must either solve part of the problem or reduce the size of the problem. The simple case in our Factorial problem is Factorial ( 0 ) which can be solved with the answer of 1. The general case contains the logic needed to reduce the size of the problem which would be n * Factorial ( n - 1 ). Recursion

5 The following is a function, written in C, to solve the factorial problem interatively: /* * Compute n! using a recursive definition * Pre: n >= 0 */ int factorial(int n) { int ans; if (n == 0) ans = 1; else ans = n * factorial(n - 1); return (ans); } simple case general case

6 Recursion The following traces a call to the recursive function Factorial using the integer constant 3 as the argument: int factorial(int n) { int ans; if (n == 0) ans = 1; else ans = n * factorial(n - 1); return (ans); } int factorial(int n) { int ans; if (n == 0) ans = 1; else ans = n * factorial(n - 1); return (ans); } int factorial(int n) { int ans; if (n == 0) ans = 1; else ans = n * factorial(n - 1); return (ans); } int factorial(int n) { int ans; if (n == 0) ans = 1; else ans = n * factorial(n - 1); return (ans); } n is now 2 n is now 1 n is now 0 2 1 1 ans is 6 ans = factorial ( 3); n is 3

7 Recursion The following recursive function counts the number of occurrences of a character ch in string str: int count(char ch, const char *str) { int ans; if (str[0] == '\0') /* simple case */ ans = 0; else /* redefine problem using recursion */ if (ch == str[0]) /* first character must be counted */ ans = 1 + count(ch, &str[1]); else /* first character is not counted */ ans = count(ch, &str[1]); return (ans); }


Download ppt "Recursion In general there are two approaches to writing repetitive algorithms. One uses loops(while, do while and for): the other uses recursion. Recursion."

Similar presentations


Ads by Google