1 / 30

Thinking Recursively xkcd #878

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.

naida-giles
Download Presentation

Thinking Recursively xkcd #878

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. Thinking Recursivelyxkcd #878

  2. Recursion • Recursive call is divider • Instructions before happen as stack is built • Instructions after happen as stack torn down

  3. Fibonacci Sequence • Famous recursivelydefined sequence

  4. Naïve Implementation • Direct code translation

  5. Fibonacci • Multiple recursive calls = combinatorial explosion:

  6. Better Fibonacci • Redefined Fibonacci function: • Parameters keep track of previous values • Call must include first two terms:

  7. Reduced Load • Parameters store old work…

  8. Recursive Helpers • This is ugly: • Can make it a recursive helper: • Call from main:

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

  10. 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?

  11. Number of Digits • How many digits does an integer have? • What is base case? • What is one step? • What parameters do I need?

  12. 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?

  13. 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?

  14. 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

  15. Number Of Digits • Code: • Assumes number >= 0

  16. 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

  17. Recursion With Array • Want to total an array using recursion: • Work backwards through array, pretending it gets smaller

  18. Palindrome • Is a string a Palindrome? (e.g. "radar") • What is base case? • What is one step? • What parameters do I need?

  19. 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?

  20. 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?

  21. 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

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

  23. 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"

  24. Palindrome • Extra parameters avoid making new strings: • Use as helper function:

  25. Binary Search • Binary Search • Pick middle of remaining search space • Too high? Eliminate middle and above • Too low? Eliminate middle and below

  26. Binary Search • Binary search • What is base case? • What is one step? • What parameters do I need?

  27. 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

  28. 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

  29. 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?…

  30. Binary Search • Binary search – recursive helper • Non-recursive starter:

More Related