04. Analysis Algo- Recur II

04. Analysis Algo- Recur II - Mehran Sahami CS103B...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
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 non-recursive algorithms, looking at how big-Oh 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 k-1 , a k-2 , . .., a k-i , 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 i-1 . 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.
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
– 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
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 10/01/2011 for the course CS 103B taught by Professor Sahami,m during the Winter '08 term at Stanford.

Page1 / 18

04. Analysis Algo- Recur II - Mehran Sahami CS103B...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online