<< Chapter < Page | Chapter >> Page > |
The function f is the function that satisfies the following two clauses:
Basis Clause: f(0) = 0! = 1
Inductive Clause: For all natural number n, f(n+1) = (n+1) f(n).
Note that here Extremal Clause is not necessary, because the set of natural numbers can be defined recursively and that has the extremal clause in it. So there is no chance of other elements to come into the function being defined.
Using this definition, 3! can be found as follows:
Since 0 ! = 1, 1 ! = 1 * 0 ! = 1 * 1 = 1 ,
Hence 2 ! = 2 * 1 ! = 2 * 1 = 2 .
Hence 3 ! = 3 * 2 ! = 3 * 2 * 1 = 6 .
Example 6: The function f(n) = 2n + 1 for natural numbers n can be defined recursively as follows:
The function f is the function that satisfies the following two clauses:
Basis Clause: f(0) = 1
Inductive Clause: For all natural number n, f(n+1) = f(n) + 2 .
See above for the extremal clause.
Example 7: The function f(n) = 2n for natural numbers n can be defined recursively as follows:
The function f is the function that satisfies the following two clauses:
Basis Clause: f(0) = 1
Inductive Clause: For all natural number n, f(n+1) = 2 f(n) .
See Example 5 for the extremal clause.
Example 8: The function L from the set S of strings over {a, b} to the set of natural numbers that gives the length of a string can be defined recursively as follows:
The function L is the function that satisfies the following two clauses:
Basis Clause: For symbols a and b of the alphabet, L(a) = 1 and L(b) = 1.
Inductive Clause: For any string x and y of S, L(xy) = L(x) + L(y) , where xy is the concatenation of strings x and y.
See Example 5 for the extremal clause.
This function L gives the number of a's and b's.
A recursive algorithm is an algorithm which calls itself with "smaller (or simpler)" input values, and which obtains the result for the current input by applying simple operations to the returned value for the smaller (or simpler) input. More generally if a problem can be solved utilizing solutions to smaller versions of the same problem, and the smaller versions reduce to easily solvable cases, then one can use a recursive algorithm to solve that problem. For example, the elements of a recursively defined set, or the value of a recursively defined function can be obtained by a recursive algorithm.
If a set or a function is defined recursively, then a recursive algorithm to compute its members or values mirrors the definition. Initial steps of the recursive algorithm correspond to the basis clause of the recursive definition and they identify the basis elements. They are then followed by steps corresponding to the inductive clause, which reduce the computation for an element of one generation to that of elements of the immediately preceding generation.
In general, recursive computer programs require more memory and computation compared with iterative algorithms, but they are simpler and for many cases a natural way of thinking about the problem.
Example 1: Algorithm for finding the k-th even natural number Note here that this can be solved very easily by simply outputting 2*(k - 1) for a given k . The purpose here, however, is to illustrate the basic idea of recursion rather than solving the problem.
Notification Switch
Would you like to follow the 'Discrete structures' conversation and receive update notifications?