{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

2010 goodrich tamassia using recursion 17 a better

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

View Full Document Right Arrow Icon
©  2010 Goodrich, Tamassia
Background image of page 16

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

View Full Document Right Arrow Icon
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
Background image of page 17
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
Background image of page 18

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

View Full Document Right Arrow Icon
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
Background image of page 19
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
Background image of page 20

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

View Full Document Right Arrow Icon
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
Background image of page 21
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}