Recursion (computer science)

This is an old revision of this page, as edited by CBM (talk | contribs) at 14:46, 16 July 2006 (Recursive functions: comment out link to recursive function, which is not a main article). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

Recursion in computer programming defines a function in terms of itself. One example application of recursion is in parsers for programming languages. The great advantage of recursion is that an infinite set of possible sentences, designs or other data can be defined, parsed or produced by a finite computer program.

Recursive algorithms

A common method of simplification is to divide a problem into subproblems of the same type. As a computer programming technique, this is called divide and conquer and is key to the design of many important algorithms, as well as being a fundamental part of dynamic programming.

Virtually all programming languages in use today allow the direct specification of recursive functions and procedures. When such a function is called, the computer (for most languages on most stack-based architectures) or the language implementation keeps track of the various instances of the function (on many architectures, by using a call stack, although other methods may be used). Conversely, every recursive function can be transformed into an iterative function by using a stack.

Any function that can be evaluated by a computer can be expressed in terms of recursive functions, without use of iteration through use of continuations in continuation-passing style, and conversely any recursive function can be expressed in terms of iteration.

To make a very literal example out of this: If an unknown word is seen in a book, the reader can make a note of the current page number and put the note on a stack (which is empty so far). The reader can then look the new word up and, while reading on the subject, may find yet another unknown word. The page number of this word is also written down and put on top of the stack. At some point an article is read that does not require any explanation. The reader then returns to the previous page number and continues reading from there. This is repeated, sequentially removing the topmost note from the stack. Finally, the reader returns to the original book. This is a recursive approach.

Some languages designed for logic programming and functional programming provide recursion as the only means of repetition directly available to the programmer. Such languages generally make tail recursion as efficient as iteration, letting programmers express other repetition structures (such as Scheme's map and for) in terms of recursion.

Recursion is deeply embedded in the theory of computation, with the theoretical equivalence of recursive functions and Turing machines at the foundation of ideas about the universality of the modern computer.

John McCarthy's 91 function is another example of a recursively defined function.

The Quicksort and Mergesort algorithms are also commonly done using recursion, which allows them to run in an average of O(n log n) time.

Many operations involving tree data structures also use recursion, as it is often quite difficult to iteratively traverse a tree of unknown length.

In addition, some numerical methods for finding approximate solutions to mathematical equations use recursion. In Newton's method, for example, an approximate root of a function is provided as initial input to the method. The calculated result (output) is then used as input to the method, with the process repeated until a sufficiently accurate value is obtained.

Recursive programming

One basic form of recursive computer program is to define one or a few base cases, and then define rules to break down other cases into the base case. This is analytic, and is the most common design for parsers for computer languages.

Another, similar form is generative recursion. This is synthetic. In this scheme, the computer uses rules to assemble cases, and starts by selecting a base case. This scheme is often used when a computer must design something automatically, such as code, a machine part or some other data.

The commonly used example (using the Euphoria programming language, in this case) is the function used to calculate the factorial of an integer:

function Factorial ( integer X )
   if X < 0 then return "Invalid argument" end if
   if X = 0 then return 1 end if
   return Factorial(X-1) * X
end function

Although the recursive factorial function is calculating the same value as the iterative function below, depending on language implementation, recursive function may consume additional memory for each call. I.e. factorial(20) may use ten times more memory than factorial(10). The Scheme language is especially efficient at recursion, but naive recursive implementations in earlier versions of C were very notoriously wasteful. Nowadays, only the most performance-hungry software, such as video games, missile guidance systems and graphics card drivers, should worry about whether recursion will be slower than iterative for or while loops.

Recursive functions are often regarded as more elegant than alternative implementations and they are usually shorter and simpler. To highlight this, here is the above function coded in the same language but without recursion:

function Factorial ( integer X )
   integer temp_result
   if X < 0 then return "Invalid argument" end if
   temp_result = 1
   for i = 1 to X do
       temp_result *= i
   end for
   return temp_result
end function

In this particular example the iterative implementation is slightly faster in practice than the recursive one. (Note that an even faster implementation for the Factorial function is that of using a lookup table.) There are other types of problems that seem to have an inherently recursive solution, i.e. they need to keep track of prior state. One example is tree traversal, which is possible to implement iteratively with the help of a stack data structure, but the need for the stack arguably defeats the purpose of the iterative solution. Another possible reason for choosing an iterative rather than a recursive algorithm is that in today's programming languages, thread stacks are often much smaller than memory available in the heap, and recursive algorithms tend to require more stack space than iterative algorithms.

Recursion vs. Iteration

While recursion can simplify the solution of a problem, often resulting in shorter, more easily understood source code, it is often less efficient, in terms of both time and space, than iterative solutions. There are some methods to convert recursive algorithm into an iterative algorithm using a loop and branching structure.

Recursive functions

Functions whose domains can be recursively defined can be given recursive definitions patterned after the recursive definition of their ___domain.

The canonical example of a recursively defined function is the following definition of the factorial function  :

 
   for any natural number   

Given this definition, also called a recurrence relation, we work out   as follows:

 
 
 
 
 
 
 
 

Types of recursive functions

Most of simple recursive functions can be classified in one of following types of recursive functions.

Tail recursive function

A function executes a number of tests and then if a certain test is true returns a certain value. If all tests evaluated as false, the function calls it self recursively on reduced input value. In tail recursive functions, the return value of last function call is the return value of the whole recursion. The template in Common Lisp would be:

(DEFUN func (X)
   (COND (end-test-1 end-value-1)
      (end-test-2 end-value-2)
;       ...
      (end-test-n end-value-n)
      (T (func reduced-x))))

Double-Test Tail Recursion

The most common variat of Tail recursive function is double-test tail recursion. This type of recursion first tests if the recursion reached the end (without finding the result), then tests if it found result and if neither if true, calls it self recursively. The template in Common Lisp would be:

(DEFUN func (X)
   (COND (end-test-1 end-value-1)
      (end-test-2 end-value-2)
      (T (func reduced-x))))

Single-Test Tail Recursion

If we are sure that the function will find what it's looking for eventualy, we can omit the first test and only test if the result has been found. The template in Common Lisp would be:

(DEFUN func (X)
   (COND (end-test end-value)
      (T (func reduced-x))))

Augmenting Recursion

In augmenting recursion, the return value of one function call is augmented before returning it to the calling function. Therefore, the return value of a recursion is not the same as a return value of each function call. The template in Common Lisp would be:

(DEFUN func (X)
   (COND (end-test end-value)
      (T (aug-fun aug-val
         (func reduced-x)))))

Terminology

Base Case: a simple case (of a recursive problem) that can be solved directly without further recursion.

See also

References