11. Recursion_outside

Recursive algorithm first attempt algorithm binaryfib

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

View Full Document Right Arrow Icon
Recursive algorithm (first attempt): Algorithm BinaryFib ( k ) : Input: Nonnegative integer k Output: The k th Fibonacci number F k if k = 1 then return k else return BinaryFib ( k - 1 ) + BinaryFib ( k - 2 ) © 2010 Goodrich, Tamassia
Background image of page 15

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

View Full Document Right Arrow Icon
Using Recursion 16 Analysis Let n k  be the number of recursive calls by  BinaryFib (k) n 0  = 1 n 1  = 1 n 2  =  n 1  +  n 0  + 1 = 1 + 1 + 1 = 3 n 3  =  n 2  +  n 1  + 1 = 3 + 1 + 1 = 5 n 4  =  n 3  +  n 2  + 1 = 5 + 3 + 1 = 9 n 5  =  n 4  +  n 3  + 1 = 9 + 5 + 1 = 15 n 6  =  n 5  +  n 4  + 1 = 15 + 9 + 1 = 25 n 7  =  n 6  +  n 5  + 1 = 25 + 15 + 1 = 41 n 8  =  n 7  +  n 6  + 1 = 41 + 25 + 1 = 67 . Note that n k  at least doubles every other time That is, n k  > 2 k/2 . It is exponential! © 2010 Goodrich, Tamassia
Background image of page 16
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

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

View Full Document Right Arrow Icon
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
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

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

View Full Document Right Arrow Icon
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
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 ]}

Page15 / 21

Recursive algorithm first attempt Algorithm BinaryFib k...

This preview shows document pages 15 - 21. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online