Jump to content

Recursion (computer science)

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Liao (talk | contribs) at 04:04, 12 February 2006. The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
(diff) ← Previous revision | Latest revision (diff) | Newer revision → (diff)


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 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, and conversely.

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. For example, above function coded in the same language without recursion would be as follows:

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, 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.

Recursively defined 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:









see also

Recursion