thinking recursively xkcd #878
DESCRIPTION
Thinking Recursively xkcd #878. Recursion. Recursive call is divider Instructions before happen as stack is built Instructions after happen as stack torn down. Fibonacci Sequence. Famous recursively defined sequence. Naïve Implementation. Direct code translation. Fibonacci. - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: Thinking Recursively xkcd #878](https://reader033.vdocuments.site/reader033/viewer/2022061502/56813679550346895d9e0719/html5/thumbnails/1.jpg)
Thinking Recursively
xkcd #878
![Page 2: Thinking Recursively xkcd #878](https://reader033.vdocuments.site/reader033/viewer/2022061502/56813679550346895d9e0719/html5/thumbnails/2.jpg)
Recursion
• Recursive call is divider– Instructions before happen as stack is built– Instructions after happen as stack torn down
![Page 3: Thinking Recursively xkcd #878](https://reader033.vdocuments.site/reader033/viewer/2022061502/56813679550346895d9e0719/html5/thumbnails/3.jpg)
Fibonacci Sequence
• Famous recursivelydefined sequence
![Page 4: Thinking Recursively xkcd #878](https://reader033.vdocuments.site/reader033/viewer/2022061502/56813679550346895d9e0719/html5/thumbnails/4.jpg)
Naïve Implementation
• Direct code translation
![Page 5: Thinking Recursively xkcd #878](https://reader033.vdocuments.site/reader033/viewer/2022061502/56813679550346895d9e0719/html5/thumbnails/5.jpg)
Fibonacci
• Multiple recursive calls = combinatorial explosion:
return fib(3) + fib(2)
return fib(2) + fib(1)
return fib(1) + fib(0)
return 1
return fib(1) + fib(0)
return 0
return 1
return 1 return 0
1: call fib(3)
2: call fib(2)
3: call fib(1)
4: return fib(1)
7: return fib(2)
5: call fib(0)
6: return fib(0)
8: call fib(1)
9: return fib(1)
10: return fib(3) 11: call fib(2)
16: return fib(2)
12: call fib(1) 13: return fib(1) 14: return fib(0)
15: return fib(0)
fib(4) 0: call fib(4) 17: return fib(4)
![Page 6: Thinking Recursively xkcd #878](https://reader033.vdocuments.site/reader033/viewer/2022061502/56813679550346895d9e0719/html5/thumbnails/6.jpg)
Better Fibonacci
• Redefined Fibonacci function:– Parameters keep track of previous values
– Call must include first two terms:
![Page 7: Thinking Recursively xkcd #878](https://reader033.vdocuments.site/reader033/viewer/2022061502/56813679550346895d9e0719/html5/thumbnails/7.jpg)
Reduced Load
• Parameters store old work…
![Page 8: Thinking Recursively xkcd #878](https://reader033.vdocuments.site/reader033/viewer/2022061502/56813679550346895d9e0719/html5/thumbnails/8.jpg)
Recursive Helpers
• This is ugly:
• Can make it a recursive helper:
• Call from main:
![Page 9: Thinking Recursively xkcd #878](https://reader033.vdocuments.site/reader033/viewer/2022061502/56813679550346895d9e0719/html5/thumbnails/9.jpg)
Moral
• Parameters are your main tool– Use them to "store" information– Use them to change where work happens
![Page 10: Thinking Recursively xkcd #878](https://reader033.vdocuments.site/reader033/viewer/2022061502/56813679550346895d9e0719/html5/thumbnails/10.jpg)
Recursive Design
• Recursive function design– What is base case?– What is one step?– What parameters do I need?• Do I want/need extra ones to simplify problem?
![Page 11: Thinking Recursively xkcd #878](https://reader033.vdocuments.site/reader033/viewer/2022061502/56813679550346895d9e0719/html5/thumbnails/11.jpg)
Number of Digits
• How many digits does an integer have?– What is base case?– What is one step?– What parameters do I need?
![Page 12: Thinking Recursively xkcd #878](https://reader033.vdocuments.site/reader033/viewer/2022061502/56813679550346895d9e0719/html5/thumbnails/12.jpg)
Number of Digits
• How many digits does integer have?– What is base case?
Anything < 10 is 1 digit– What is one step?– What parameters do I need?
![Page 13: Thinking Recursively xkcd #878](https://reader033.vdocuments.site/reader033/viewer/2022061502/56813679550346895d9e0719/html5/thumbnails/13.jpg)
Number of Digits
• How many digits does integer have?– What is base case?
Anything < 10 is 1 digit– What is one step?
Digits(n) = 1 + Digits(n/10)– What parameters do I need?
![Page 14: Thinking Recursively xkcd #878](https://reader033.vdocuments.site/reader033/viewer/2022061502/56813679550346895d9e0719/html5/thumbnails/14.jpg)
Number of Digits
• How many digits does integer have?– What is base case?
Anything < 10 is 1 digit– What is one step?
Digits(n) = 1 + Digits(n/10)– What parameters do I need?
n
![Page 15: Thinking Recursively xkcd #878](https://reader033.vdocuments.site/reader033/viewer/2022061502/56813679550346895d9e0719/html5/thumbnails/15.jpg)
Number Of Digits
• Code:– Assumes number >= 0
![Page 16: Thinking Recursively xkcd #878](https://reader033.vdocuments.site/reader033/viewer/2022061502/56813679550346895d9e0719/html5/thumbnails/16.jpg)
Recursion With Array
• Want to total an array using recursion:– What is base case?
Size 0 will equal 0– What is one step?
Total(size n) = nth element + Total(size n-1)– What parameters do I need?
Array, size
![Page 17: Thinking Recursively xkcd #878](https://reader033.vdocuments.site/reader033/viewer/2022061502/56813679550346895d9e0719/html5/thumbnails/17.jpg)
Recursion With Array
• Want to total an array using recursion:
• Work backwards through array, pretending it gets smaller
![Page 18: Thinking Recursively xkcd #878](https://reader033.vdocuments.site/reader033/viewer/2022061502/56813679550346895d9e0719/html5/thumbnails/18.jpg)
Palindrome
• Is a string a Palindrome? (e.g. "radar")– What is base case?– What is one step?– What parameters do I need?
![Page 19: Thinking Recursively xkcd #878](https://reader033.vdocuments.site/reader033/viewer/2022061502/56813679550346895d9e0719/html5/thumbnails/19.jpg)
Palindrome
• Is a string a Palindrome? (e.g. "radar")– What is base case?• 0 or 1 letters is a palindrome : "d"• If first letter != last, is NOT a palindrome : "ben"
– What is one step?– What parameters do I need?
![Page 20: Thinking Recursively xkcd #878](https://reader033.vdocuments.site/reader033/viewer/2022061502/56813679550346895d9e0719/html5/thumbnails/20.jpg)
Palindrome
• Is a string a Palindrome? (e.g. "radar")– What is base case?• 0 or 1 letters is a palindrome : "d"• If first letter != last, is NOT a palindrome : "ben"
– What is one step?"madamImadam"Test all but first and last
– What parameters do I need?
![Page 21: Thinking Recursively xkcd #878](https://reader033.vdocuments.site/reader033/viewer/2022061502/56813679550346895d9e0719/html5/thumbnails/21.jpg)
Palindrome
• Is a string a Palindrome? (e.g. "radar")– What is base case?• 0 or 1 letters is a palindrome : "d"• If first letter != last, is NOT a palindrome : "ben"
– What is one step?"madamImadam"Test all but first and last
– What parameters do I need? Current string
![Page 22: Thinking Recursively xkcd #878](https://reader033.vdocuments.site/reader033/viewer/2022061502/56813679550346895d9e0719/html5/thumbnails/22.jpg)
Palindrome
• Is a string a Palindrome? (e.g. "radar")
![Page 23: Thinking Recursively xkcd #878](https://reader033.vdocuments.site/reader033/viewer/2022061502/56813679550346895d9e0719/html5/thumbnails/23.jpg)
Palindrome
• Is a string a Palindrome? (e.g. "radar")– What is base case?• 0 or 1 letters is a palindrome : "d"• If first letter != last, is NOT a palindrome : "ben"
– What is one step?Test all but first and last
– What parameters do I need? Current string• Do I want/need extra ones to simplify problem?
Indexes to point to "start" and "end"
![Page 24: Thinking Recursively xkcd #878](https://reader033.vdocuments.site/reader033/viewer/2022061502/56813679550346895d9e0719/html5/thumbnails/24.jpg)
Palindrome
• Extra parameters avoid making new strings:
• Use as helper function:
![Page 25: Thinking Recursively xkcd #878](https://reader033.vdocuments.site/reader033/viewer/2022061502/56813679550346895d9e0719/html5/thumbnails/25.jpg)
Binary Search
• Binary Search– Pick middle of remaining search space– Too high? Eliminate middle and above– Too low? Eliminate middle and below
![Page 26: Thinking Recursively xkcd #878](https://reader033.vdocuments.site/reader033/viewer/2022061502/56813679550346895d9e0719/html5/thumbnails/26.jpg)
Binary Search
• Binary search– What is base case?– What is one step?– What parameters do I need?
![Page 27: Thinking Recursively xkcd #878](https://reader033.vdocuments.site/reader033/viewer/2022061502/56813679550346895d9e0719/html5/thumbnails/27.jpg)
Binary Search
• Binary search– What is base case?– What is one step?– What parameters do I need?
List, value looking for, lowest possible location, highest possible location
![Page 28: Thinking Recursively xkcd #878](https://reader033.vdocuments.site/reader033/viewer/2022061502/56813679550346895d9e0719/html5/thumbnails/28.jpg)
Binary Search
• Binary search– What is base case?
If lowest possible location > highest possible location, it can't be there
– What is one step?– What parameters do I need?
List, value looking for, lowest possible location, highest possible location
![Page 29: Thinking Recursively xkcd #878](https://reader033.vdocuments.site/reader033/viewer/2022061502/56813679550346895d9e0719/html5/thumbnails/29.jpg)
Binary Search
• Binary search– What is base case?
If lowest possible location > highest possible location, it can't be there
– What is one step?Find middle• If key == middle, return middle location• If key < middle, highest is now middle -1• If key > middle, lowest is now middle + 1
– What parameters do I need?…
![Page 30: Thinking Recursively xkcd #878](https://reader033.vdocuments.site/reader033/viewer/2022061502/56813679550346895d9e0719/html5/thumbnails/30.jpg)
Binary Search
• Binary search – recursive helper
• Non-recursive starter: