Lec07_08Recurrence

Lec07_08Recurrence - Recurrences Execution of a recursive...

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

Recurrences

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

View Full Document
Execution of a recursive program Deriving & solving recurrence equations
Recursion What is the recursive definition of n ! ? Program int fact(int n) { if (n<=1) return 1; else return n*fact(n-1); } // Note '*' is done after returning from fact(n-1) = otherwise ) 1)! - ( ( 1 or 0 is if 1 ! n n n n

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

View Full Document
Recursive algorithms A recursive algorithm typically contains recursive calls to the same algorithm In order for the recursive algorithm to terminate, it must contain code for solving directly some “base case(s)” A direct solution does not include recursive calls. We use the following notation: DirectSolutionSize is the “size” of the base case DirectSolutionCount is the count of the number of operations done by the “direct solution”
fact(3) 3 2 1 fact(2) fact(1) A Call Tree for fact(3) The Run Time Environment The following 2 slides show the program stack after the third call from fact(3) When a function is called an activation records('ar') is created and pushed on the program stack. The activation record stores copies of local variables, pointers to other ‘ar’ in the stack and the return address. When a function returns the stack is popped . int fact(int n) { if (n<=1) return 1; else return n*fact(n-1); } returns 6 1 * * 2 6

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

View Full Document
EP Program stack Val fact: <ipF,ep0> N =3 function value [=6] return address static link = ep0 dynamic link = EP free memory AR(Driver) AR(fact 3) Snapshot of the environment at end of first call to fact ep0 EP Program stack Val fact: <ipF,ep0> free memory AR(Driver) Snapshot of the environment after fact l returns

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

View Full Document
Goal: Analyzing recursive algorithms Until now we have only analyzed (derived a count) for non-recursive algorithms. In order to analyze recursive algorithms we must learn to: Derive the recurrence equation from the code Solve recurrence equations.
Deriving a Recurrence Equation for a Recursive Algorithm Our goal is to compute the count (Time) T(n) as a function of n, where n is the size of the problem We will first write a recurrence equation for T(n) For example T(n)=T(n-1)+1 and T(1)=0 Then we will solve the recurrence equation When we solve the recurrence equation above we will find that T(n)=n, and any such recursive algorithm is linear in n.

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

View Full Document
a Recursive Algorithm 1.
This is the end of the preview. Sign up to access the rest of the document.

This document was uploaded on 01/04/2010.

Page1 / 77

Lec07_08Recurrence - Recurrences Execution of a recursive...

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

View Full Document
Ask a homework question - tutors are online