{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Lec07_08Recurrence

Lec07_08Recurrence - Recurrences Execution of a recursive...

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

View Full Document Right Arrow Icon
Recurrences
Image of page 1

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

View Full Document Right Arrow Icon
Execution of a recursive program Deriving & solving recurrence equations
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
Image of page 3

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

View Full Document Right 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”
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
Image of page 5

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

View Full Document Right 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
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
Image of page 7

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

View Full Document Right 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.
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.
Image of page 9

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

View Full Document Right Arrow Icon
a Recursive Algorithm 1.
Image of page 10
Image of page 11
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    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.

    Student Picture

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

  • Left Quote Icon

    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.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    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.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern