{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

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.

{[ 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