thinking recursively xkcd #878

30
Thinkin g Recursi vely xkcd #878

Upload: naida-giles

Post on 03-Jan-2016

99 views

Category:

Documents


0 download

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 Presentation

TRANSCRIPT

Page 1: Thinking Recursively xkcd #878

Thinking Recursively

xkcd #878

Page 2: Thinking Recursively xkcd #878

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

Fibonacci Sequence

• Famous recursivelydefined sequence

Page 4: Thinking Recursively xkcd #878

Naïve Implementation

• Direct code translation

Page 5: Thinking Recursively xkcd #878

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

Better Fibonacci

• Redefined Fibonacci function:– Parameters keep track of previous values

– Call must include first two terms:

Page 7: Thinking Recursively xkcd #878

Reduced Load

• Parameters store old work…

Page 8: Thinking Recursively xkcd #878

Recursive Helpers

• This is ugly:

• Can make it a recursive helper:

• Call from main:

Page 9: Thinking Recursively xkcd #878

Moral

• Parameters are your main tool– Use them to "store" information– Use them to change where work happens

Page 10: Thinking Recursively xkcd #878

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

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

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

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

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

Number Of Digits

• Code:– Assumes number >= 0

Page 16: Thinking Recursively xkcd #878

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

Recursion With Array

• Want to total an array using recursion:

• Work backwards through array, pretending it gets smaller

Page 18: Thinking Recursively xkcd #878

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

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

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

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

Palindrome

• Is a string a Palindrome? (e.g. "radar")

Page 23: Thinking Recursively xkcd #878

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

Palindrome

• Extra parameters avoid making new strings:

• Use as helper function:

Page 25: Thinking Recursively xkcd #878

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

Binary Search

• Binary search– What is base case?– What is one step?– What parameters do I need?

Page 27: Thinking Recursively xkcd #878

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

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

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

Binary Search

• Binary search – recursive helper

• Non-recursive starter: