Recursion in Computer Science



Recursion is a method that defining a thing using the thing being defined in its definition. For example, we define an expression as a number, or a concatenation, in order, of an expression, an operand, and an another expression. The definition of an expression is recursive since the definition applies expressions.

In Computer Science, "divide and conquer" is a very general programming strategy to solve problems. The idea of this strategy is based on simplifying a problem into small scaled same problems. For example, assuming we want to sort a list of books in a book store, we can partition the books to be sorted into multiple small groups of books. For each group, we can partition the books again, etc. When each small groups of books are sorted, we can combine the sorted groups of books together and eventually the whole book lot will be sorted. Recursion can be applied to write sorting algorithms based on the divide and conquer strategy. Indeed, the Quick Sort algorithm is using recursion in its definition, so is Merge Sort algorithm, etc.

The major advantage using recursion is its simplicity. For some data structures, such as trees, it is natural to use recursion to define them. For example, we can define a tree as a node, or a node with edges connecting with other trees. It is natural to apply recursion in algorithms that operate on such data organizations. However, applying recursion does come with cost of memory. During the time when running an algorithm, the implementation of recursion is typically using a stack, which is part of primary memory, to do the bookkeeping about how deep the recursion proceeds and what remains uncompleted. If the depth of a recursion is too large during running time, it may cause a process running the algorithm to run up memory and crash.

Fortunately, sometimes we can replace recursion in algorithms with iterations. For example, if a recursive call is immediately followed by a return exist statement in a subroutine, which is called a tail recursive call, the implicit stack cost can be dispensed by rewriting the recursive call without costing stack memory.

In the following, I will illustrate how to rewrite a tail-recursive call using the inorder tree traversal algorithm. The first procedure applies two recursion calls to traverse a tree pointed by P.

Procedure Traverse(pointer P)
if (P is not empty) then
Traverse(LC(P)) //Traverse the left child of the tree
Invisit(P) //This is a inorder tree traversal
Traverse(RC(P)) //Traverse the right child of the tree
return;
end if
return

The second Traverse call in the procedure is tail-recursive since it follows the return of the procedure immediately. We can rewrite the tail recursive call, but keep the first recursive call in the algorithm. The new procedure is as follows.

Procedure Traverse(Pointer P)
while P is not empty do
Traverse(LC(P))
Invisit(P)
P = RC(P)
end while
return

To simply algorithms in code, it is important to apply recursion. However, attention is required to understand the implementation memory cost of recursion.

Comments

Peter Jones said…
Great approach - I've blogged on programming as a nurse -

http://hodges-model.blogspot.com/2009/09/nursing-variables-and-constant-values.html

http://hodges-model.blogspot.com/2009/04/cash-transactions-care-continuity.html

http://hodges-model.blogspot.com/2009/04/nursing-as-reverse-engineering.html

http://hodges-model.blogspot.com/2008/10/reflection-programming-and-caring-ii.html

http://hodges-model.blogspot.com/2008/09/composability-programming-and-caring.html

Noting your focus the following
conceptual model introduced through a website and blog -

http://hodges-model.blogspot.com/

- may be of interest?

Originally created in the UK by Brian E Hodges (Ret.) at Manchester Metropolitan University -

Hodges' Health Career - Care Domains - Model [h2cm]

http://www.p-jones.demon.co.uk/

- can help map health, social care and OTHER issues, problems and solutions. The model takes a situated and multi-contextual view across four knowledge domains:

* Interpersonal;
* Sociological;
* Empirical;
* Political.

Our links pages cover each care (knowledge) domain e.g. SOCIOLOGY:

http://www.p-jones.demon.co.uk/links3.htm

SCIENCES:

http://www.p-jones.demon.co.uk/linksTwo.htm

Best regards,

Peter Jones
RMN, RGN, CPN(Cert), PGCE, PG(Dip) COPE, BA (Hons.).
Community Mental Health Nurse for Older Adults,
Independent Scholar and Informatics Specialist
Lancashire
UK
--
website: Hodges' Health Career - Care Domains - Model
http://www.p-jones.demon.co.uk/
blog: Welcome to the QUAD
http://hodges-model.blogspot.com/
h2cm: help 2C more - help 2 listen - help 2 care
http://twitter.com/h2cm

Popular posts from this blog

25 Google Interview Questions

Convert LaTeX to HTML

Art of Software Development