View
214
Download
1
Category
Preview:
Citation preview
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 recursivelydefined sequence
Naïve Implementation
• Direct code translation
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)
Better Fibonacci
• Redefined Fibonacci function:– Parameters keep track of previous values
– Call must include first two terms:
Reduced Load
• Parameters store old work…
Recursive Helpers
• This is ugly:
• Can make it a recursive helper:
• Call from main:
Moral
• Parameters are your main tool– Use them to "store" information– Use them to change where work happens
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?
Number of Digits
• How many digits does an integer have?– What is base case?– What is one step?– What parameters do I need?
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?
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?
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
Number Of Digits
• Code:– Assumes number >= 0
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
Recursion With Array
• Want to total an array using recursion:
• Work backwards through array, pretending it gets smaller
Palindrome
• Is a string a Palindrome? (e.g. "radar")– What is base case?– What is one step?– What parameters do I need?
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?
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?
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
Palindrome
• Is a string a Palindrome? (e.g. "radar")
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"
Palindrome
• Extra parameters avoid making new strings:
• Use as helper function:
Binary Search
• Binary Search– Pick middle of remaining search space– Too high? Eliminate middle and above– Too low? Eliminate middle and below
Binary Search
• Binary search– What is base case?– What is one step?– What parameters do I need?
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
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
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?…
Binary Search
• Binary search – recursive helper
• Non-recursive starter:
Recommended