Lec07_08Recurrence

Lec07_08Recurrence - Recurrences Execution of a recursive...

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

View Full Document Right Arrow Icon
Recurrences
Background image of page 1

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

View Full DocumentRight Arrow Icon
Execution of a recursive program Deriving & solving recurrence equations
Background image of page 2
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
Background image of page 3

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

View Full DocumentRight Arrow Icon
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”
Background image of page 4
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
Background image of page 5

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

View Full DocumentRight Arrow Icon
ep2 Program stack Val fact: <ipF,ep0> N =3 function value [=?] return address static link = ep0 dynamic link =ep1 free memory AR(Driver) AR(fact 3) Snapshot of the environment at end of third call to fact ep0 N =2 function value [=?] return address static link = ep0 dynamic link =ep2 AR(fact 2) ep1 EP N =1 function value [=1] return address static link = ep0 dynamic link = EP AR(fact 1) AR=Activation Record ep= environmental pointer EP Program stack Val fact: <ipF,ep0> N =3 function value [=?] return address static link = ep0 dynamic link = ep1 free memory AR(Driver) AR(fact 3) Snapshot of the environment at end of second call to fact ep0 N =2 function value [=2] return address static link = ep0 dynamic link = EP AR(fact 2) ep1
Background image of page 6
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
Background image of page 7

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

View Full DocumentRight Arrow Icon
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.
Background image of page 8
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.
Background image of page 9

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

View Full DocumentRight Arrow Icon
a Recursive Algorithm 1.
Background image of page 10
Image of page 11
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 Right Arrow Icon
Ask a homework question - tutors are online