11. Recursion_outside

# 2010 goodrich tamassia using recursion 17 a better

• Notes
• 21

This preview shows pages 16–21. Sign up to view the full content.

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

Using Recursion 17 A Better Fibonacci Algorithm  Use linear recursion instead Algorithm  LinearFibonacci (k):       Input:  A nonnegative integer k       Output:  Pair of Fibonacci numbers (F , F k - 1 )      if  k =  then return  (k 0)      else (i,  j)    =    LinearFibonacci ( -   1) return  (i +j, i)   LinearFibonacci  makes k - 1 recursive calls ©  2010 Goodrich, Tamassia
Using Recursion 18 Multiple Recursion Motivating example:  summation puzzles pot  pan  bib dog  cat  pig boy  girl  baby Multiple recursion:  makes potentially many recursive calls not just one or two ©  2010 Goodrich, Tamassia

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

Using Recursion 19 Algorithm for Multiple Recursion Algorithm PuzzleSolve (k,S,U): Input: Integer k, sequence S, and set U (universe of elements to test) Output: Enumeration of all k-length extensions to S using elements in U without repetitions for all e in U do Remove e from U {e is now being used} Add e to the end of S if k = 1 then Test whether S is a configuration that solves the puzzle if S solves the puzzle then return “Solution found: ” S else PuzzleSolve (k - 1, S,U) Add e back to U {e is now unused} Remove e from the end of S ©  2010 Goodrich, Tamassia
Example ©  2010 Stallmann 20 Using Recursion cbb + ba = abc a,b,c stand for 7,8,9; not  necessarily in that order [] {a,b,c} [a] {b,c} a=7 [b] {a,c} b=7 [c] {a,b} c=7 [ab] {c} a=7,b=8 c=9 [ac] {b} a=7,c=8 b=9 [ba] {c} b=7,a=8 c=9 [bc] {a} b=7,c=8 a=9 [ca] {b} c=7,a=8 b=9 [cb] {a} c=7,b=8 a=9 might be able to stop sooner Slide by Matt Stallmann  included with permission. Slide by Matt Stallmann  included with permission. 799 + 98 = 997

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

Using Recursion 21 Visualizing PuzzleSolve PuzzleSolve (3,(),{a,b,c}) Initial call PuzzleSolve (2,c,{a,b}) PuzzleSolve (2,b,{a,c}) PuzzleSolve (2,a,{b,c}) PuzzleSolve (1,ab,{c}) PuzzleSolve (1,ac,{b}) PuzzleSolve (1,cb,{a}) PuzzleSolve (1,ca,{b}) PuzzleSolve (1,bc,{a}) PuzzleSolve (1,ba,{c}) abc acb bac bca cab cba ©  2010 Goodrich, Tamassia
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