{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Handout 10

# Handout 10 - Mehran Sahami CS103B Handout#10 Analysis of...

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

Mehran Sahami Handout #10 CS103B January 23, 2009 Analysis of Algorithms: The Recursive Case We again thank Maggie Johnson for portions of this handout. 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 , noting how earlier terms in a sequence relate to late 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. Example 1 A single pair of rabbits (one male and one female, of course) is born at the end of January 2009. Assume the following interesting conditions: 1. Rabbit pairs give birth to one new male/female pair at the end of every month after the month in which they are born. 2. No deaths ever occur (not even from exhaustion...)

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

View Full Document
2 Problem : Find a recurrence relation that can be used to tell us how many rabbits we will have at the end of one year (i.e., the end of January 2010). Solution : We will keep track of time by using the variable x to denote the number of months that have passed since January 2009, and the function T x to denote how many rabbits are currently in the population as a function of x. Thus, the end of January 2009 is denoted by x = 0. Since we know we have two rabbits at the end of January 2009, we can write: T 0 = 2. Note that this function determines the base case in our recursive definition.
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern