This preview shows pages 1–3. Sign up to view the full content.
Mehran Sahami
Handout #4
CS103B
Analysis of Algorithms: The Recursive Case
We again thank Mehran Sahami for portions of this handout.
Key topics:
* Recurrence Relations
* Solving Recurrence Relations
* The Towers of Hanoi
* Analyzing Recursive Subprograms
Up until now, we have been analyzing nonrecursive algorithms, looking at how bigOh
notation may be used to characterize the growth rate of running times for various
algorithms. Such algorithm analysis becomes a bit more complicated when we turn our
attention to analyzing recursive algorithms. As a result, we augment the analytic tools in
our repertoire to help us perform such analyses. One such tool is an understanding of
recurrence relations, which we discuss presently.
Recurrence Relations
Recall that a recursive or inductive definition has the following parts:
1.
Base Case
: the initial condition or basis which defines the first (or first few)
elements of the sequence
2.
Inductive (Recursive) Case
: an inductive step in which later terms in the
sequence are defined in terms of earlier terms.
The inductive step in a recursive definition can be expressed as a
recurrence relation
,
showing how earlier terms in a sequence relate to later terms. We more formally define
this notion below.
A
recurrence relation
for a sequence a
1
, a
2
, a
3
, .
.. is a formula that relates each term a
k
to certain of its predecessors a
k1
, a
k2
, .
.., a
ki
, where
i
is a fixed integer and
k
is any
integer greater than or equal to
i
. The initial conditions for such a recurrence relation
specify the values of a
1
, a
2
, a
3
, .
.., a
i1
.
A recursive definition is one way of defining a sequence in mathematics (other ways to
define sequences include enumerating the sequence or coming up with a formula to
express the sequence). Suppose you have an enumerated sequence that satisfies a given
recursive definition (or recurrence relation). It is frequently very useful to have the
formula for the elements of this sequence in addition to the recurrence relation, especially
if you need to determine a very large member of the sequence. Such an explicit formula
is called a
solution
to the recurrence relation. If a member of the sequence can be
calculated using a fixed number of elementary operations, we say it is a
closed form
formula
.
Solving recurrence relations is the key to analyzing recursive subprograms.
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document– 2 –
Example 1
a) Make a list of all bit strings of length 0, 1, 2, & 3 that do not contain the bit pattern 11.
How many such strings are there?
b) Find the number of strings of length ten that do not contain the pattern 11.
a) One way to solve this problem is enumeration:
length 0: empty string
1
length 1: 0, 1
2
length 2: 00, 01, 10, 11
3
length 3: 000, 001, 010, 011, 100, 101, 110, 111
5
b) To do this for strings of length ten would be ridiculous because there are 1024
possible strings.
There must be a better way.
Suppose the number of bit strings of length less than some integer k that do not
This is the end of the preview. Sign up
to
access the rest of the document.
 Winter '08
 SAHAMI,M
 Algorithms

Click to edit the document details