This page is no longer actively maintained.
You might want to visit
the most recent Math 156 page.
Recursion and Recurrances
Many (perhaps most) recursive algorithms fall into one of two categories:
- tail-recursion
- divide-and-conquer
We would like to develop some tools that allow us to fairly easily
determine the efficiency of these types of algorithms. The process
will be to first express the running time as a recurrence and then
develop tools for solving the kinds of recurrences that arrise.
Tail Recursion
Tail recursion is the kind of recursion that pulls off 1 "unit" from
the "end" (tail) of the data and processes the remainder recursively:
foo (data, n) {
tailData = preProcess(data, n);
foo (tailData, n-1);
postProcess(data, tailData, n);
}
If p and q are the running times of preProcess()
and postProcess(), and f is the running time of foo(), then
we have
f(n) = p(n) + f(n-1) + q(n)
This simplifies to
f(n) = f(n-1) + g(n)
If we let g(n) = p(n) + q(n). So we will be interested
in solving recurrences of this type.
Actually, we will look at recurrences of a somewhat more general
type, namely
f(n) = a f(n-1) + b f(n-2) + g(n),
but only for certain kinds of functions g(n).
This will allow us to handle things like the Fibonacci recurrance
(which arose in the analysis of Euclid's Algorithm) and a few
other things beyond the basic tail recursion.
Divide-and-Conquer
foo (data, n) {
// split data into k chunks
dataChunks = preProcess(data, n); // dataChunks is an array
// process each chunk
for (i=0; i < k; i++) {
foo(dataChunk[i], n/k);
}
postProcess(data, dataChunks, n);
}
If p and q are the running times of preProcess()
and postProcess(), and f is the running time of foo(), then
we have
f(n) = p(n) + k f(n/k) + q(n)
This simplifies to
f(n) = k f(n/k) + g(n)
If we let g(n) = p(n) + q(n). So we will be interested
in solving recurrences of this type.
The example above assumes that the data gets chopped into k
pieces of the same size and that each piece gets processed. In
some algoirithms, only some of the pieces get processed recursively
(as in binary search) so we will look at a slightly more general
recurence, namely:
f(n) = a f(n/b) + g(n).
Once again, we will only deal with certain kinds of functions g(n).
To current Math 156 Home Page
This page maintained by:
Randall Pruim
Department of Mathematics and Statistics
Calvin College
rpruim@calvin.edu
Last Modified:
Thursday, 04-Oct-2001 12:20:55 EDT