COP 3503 – Computer Science II
–
CLASS NOTES

DAY #7
Problem Solving Techniques
We now shift our focus from runtime complexity to introducing several general
techniques which can be applied to problem solving.
Different problems require
different approaches in defining their solutions.
To this end the algorithm designer
needs to have a good understanding of many different techniques that can be
applied toward solving the problem at hand.
This set of notes introduces the problem solving techniques of
recursion
,
divide
and conquer
,
dynamic programming
, and
backtracking
.
Recursion
•
A method or function either directly or indirectly calls itself.
•
Can be a powerful problem solving tool.
•
Many algorithms and mathematical properties are naturally expressed in a
recursive manner.
•
Some programming languages are inherently recursive (Lisp, Scheme).
If properly done, a recursive function F calling itself, does not lead to an infinite
cycle of the function calling itself.
Instead, each recursive call is made on a
different, generally simpler, instance of the problem.
Day 7  1
Example:
Consider the factorial function, n!.
0! = 1
n! = n * (n1) * … * 3 * 2 *1
To determine 6! using this definition requires that we calculate:
6! = 6 * 5 * 4 * 3 * 2 * 1 = 720
Defining the factorial function recursively, we have:
1 if n = 0
fact (n) =
1 if n = 1
n * fact(n1) if n >= 2
Now 6! = 6 * 5!
= 6 * (5 * 4!)
= 6 * (5 * (4 * 3!))
= 6 * (5 * (4 * (3 * 2!)))
= 6 * (5 * (4 * (3 * (2 * 1!))))
= 6 * (5 * (4 * (3 * (2 * (1 * 0!)))))
= 6 * 5 * 4 * 3 * 2 * 1 * 1
This preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentA recursive function is a function which is defined in terms of itself.
For example,
it is common to express the factorial function,
f(n) = n! where n is an integer
,
as
the recursive function:

≤
=
1
n
)
1
n
(
f
n
1
n
1
)
n
(
f
This definition states that
f(n)
equals 1 whenever n is less than or equal to 1.
For
example, f(3) = f(0) = f(1) = 1. However, when
n
is more than 1,
f(n)
is defined
recursively, since the definition of
f
now contains an occurrence of
f
on the right
side.
Note that the use of
f
on the right side does not constitute a circular definition
since the parameter of
f
on the right side is smaller than the one on the left side.
For example, the from the definition we have: f(2) = 2f(1) and the definition also
gives us f(1) = 1, substitution then provides the answer that f(2) = 2(1) = 2.
Similarly, for f(3) the definition gives us: f(3) = 3f(2), from above we know that
f(2) = 2f(1) and f(1) = 1, thus f(3) = 3f(2) = 3(2(f(1))) = 3(2(1))) = 6.
For a recursive definition of
f(n)
(assuming direct recursion) to be a complete
specification of the function
f
, it must meet the following requirements:
The definition must include a
base
component
in which
f(n)
is defined
directly (i.e., without recursion) for one or more values of
n
.
For simplicity,
we assume that the domain of
f
is the nonnegative integers and that the base
covers the case 0
≤
n
≤
k for some constant
k
.
(Note: it is possible to have
recursive definitions in which the base covers the case
≥
k
instead, but
these definitions occur infrequently.)
In the
recursive component
all occurrences of
f
on the right side must have a
parameter smaller than
n
so that repeated application of the recursive
This is the end of the preview.
Sign up
to
access the rest of the document.
 Summer '09
 Computer Science, Recursion, MCSs

Click to edit the document details