1 / 29

Recursion - Fibonacci

Recursion - Fibonacci. Let’s examine how this would work for another classic recursive problem. The Fibonacci sequence: Fib(0) = 1 Fib(1) = 1 Fib(n) = Fib(n-2) + Fib(n-1) How can we code this? What parts are the base case? What parts are the recursive step?. Recursion - Fibonacci.

Download Presentation

Recursion - Fibonacci

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Recursion - Fibonacci • Let’s examine how this would work for another classic recursive problem. • The Fibonacci sequence: Fib(0) = 1 Fib(1) = 1 Fib(n) = Fib(n-2) + Fib(n-1) • How can we code this? • What parts are the base case? • What parts are the recursive step?

  2. Recursion - Fibonacci int fibonacci(int n) { if(n == 0 || n == 1) return 1; else return fibonacci(n-2) + fibonacci(n-1); }

  3. res: n: pos: part: Recursion - Fibonacci int fibonacci(int n) { if(n == 0 || n == 1) return 1; else A: return fibonacci(n-2) + B: fibonacci(n-1); } We’ll use the below graphics to aid our analysis of this method.

  4. n: pos: part: 3 A --- res: n: pos: part: 1 5 A --- Recursion - Fibonacci if(n == 0 || n == 1) return 1; else A: return fibonacci(n-2) + B: fibonacci(n-1);

  5. n: pos: part: 2 A --- n: pos: part: 3 B 1 res: n: pos: part: 1 5 A --- Recursion - Fibonacci if(n == 0 || n == 1) return 1; else A: return fibonacci(n-2) + B: fibonacci(n-1);

  6. n: pos: part: 2 B 1 n: pos: part: 3 B 1 res: n: pos: part: 1 5 A --- Recursion - Fibonacci if(n == 0 || n == 1) return 1; else A: return fibonacci(n-2) + B: fibonacci(n-1);

  7. n: pos: part: 3 B 1 res: n: pos: part: 2 5 A --- Recursion - Fibonacci if(n == 0 || n == 1) return 1; else A: return fibonacci(n-2) + B: fibonacci(n-1);

  8. res: n: pos: part: 3 5 A --- Recursion - Fibonacci if(n == 0 || n == 1) return 1; else A: return fibonacci(n-2) + B: fibonacci(n-1);

  9. n: pos: part: 2 A --- n: pos: part: 4 A --- res: n: pos: part: … 5 B 3 Recursion - Fibonacci if(n == 0 || n == 1) return 1; else A: return fibonacci(n-2) + B: fibonacci(n-1);

  10. n: pos: part: 2 A --- n: pos: part: 4 A --- res: n: pos: part: … 5 B 3 Recursion - Fibonacci Didn’t we already get an answer for n = 2? Yep. So I’ll save us some time. if(n == 0 || n == 1) return 1; else A: return fibonacci(n-2) + B: fibonacci(n-1);

  11. n: pos: part: 4 A --- res: n: pos: part: 2 5 B 3 Recursion - Fibonacci if(n == 0 || n == 1) return 1; else A: return fibonacci(n-2) + B: fibonacci(n-1);

  12. n: pos: part: 3 A --- n: pos: part: 4 B 2 n: pos: part: 5 B 3 Recursion - Fibonacci Didn’t we already get an answer for n = 3? Yep. So I’ll save us some time. if(n == 0 || n == 1) return 1; else A: return fibonacci(n-2) + B: fibonacci(n-1);

  13. n: pos: part: 4 B 2 res: n: pos: part: 3 5 B 3 Recursion - Fibonacci Didn’t we already get an answer for n = 3? Yep. So I’ll save us some time. if(n == 0 || n == 1) return 1; else A: return fibonacci(n-2) + B: fibonacci(n-1);

  14. res: n: pos: part: 5 5 B 3 Recursion - Fibonacci if(n == 0 || n == 1) return 1; else A: return fibonacci(n-2) + B: fibonacci(n-1);

  15. res: 8 Recursion - Fibonacci if(n == 0 || n == 1) return 1; else A: return fibonacci(n-2) + B: fibonacci(n-1);

  16. Recursion - Fibonacci • Can this be done more efficiently? • You betcha! First off, note that we had had to recalculate some of the intermediate answers. • What if we could have saved those answers? • It’s possible, and the corresponding technique is called dynamic programming. • We’ll not worry about that for now.

  17. Recursion, pt. 2: Thinking it Through

  18. Sorting • One classic application for recursion is for use in sorting. • What might be some strategies we could use for recursively sorting? • During the course introduction, a rough overview of quicksort was mentioned. • There are other divide-and-conquer type strategies.

  19. Merge Sort • One technique, called merge sort, operates on the idea of merging two pre-sorted arrays. • How could such a technique help us?

  20. Merge Sort • One technique, called merge sort, operates on the idea of merging two pre-sorted arrays. • If we have two presorted arrays, then the smallest overall element must be in the first slot of one of the two arrays. • It dramatically reduces the effort needed to find each element in its proper order.

  21. Merge Sort • Problem: when presented with a fresh array, with items in random order within it… how can we use this idea of “merging” to sort the array? • Hint: think with recursion! • Base case: n = 1 – a single item is automatically a “sorted” list.

  22. Merge Sort • We’ve found our base case, but what would be our recursive step? • Given the following two sorted arrays, how would we merge them into a single sorted array? [13] => [13 42] [42]

  23. Merge Sort • Given the following two sorted arrays, how would we merge them into a single sorted array? [-2, 47] => [-2, 47, 57, 101] [57, 101]

  24. Merge Sort • Given the following two sorted arrays, how would we merge them into a single sorted array? [13, 42] => [7, 101] 7, 13 42 [ , , , ] 101

  25. Merge Sort • Can we find a pattern that we can use to make a complete recursive step?

  26. Merge Sort • Given the following two sorted arrays, how would we merge them into a single sorted array? [13, 42] => [7, 101] 7, 13 42 [ , , , ] 101

  27. Merge Sort • In words: • If both lists still have remaining elements, pick the smaller of the first elements and consider that element removed. • If only one list has remaining elements, copy the remaining elements into place. • This is the recursive step of merge sort.

  28. Merge Sort 13 32 77 55 43 1 42 88 [13 32 77 55],[43 1 42 88] [13 32],[77 55],[43 1],[42 88] ||| [13 32],[55 77],[ 1 43],[42 88] [13 32 55 77],[ 1 42 43 88] [1 13 32 42 43 55 77 88]

  29. Merge Sort • Note that for each element insertion into the new array, only one element needs to be examined from each of the two old arrays. • It’s possible because the two arrays are presorted. • The “merge” operation thus takes O(N) time.

More Related