lecture 15 recursion
play

Lecture 15: Recursion (Sections 5.8-5.10) CS 1110 Introduction to - PowerPoint PPT Presentation

http://www.cs.cornell.edu/courses/cs1110/2018sp Lecture 15: Recursion (Sections 5.8-5.10) CS 1110 Introduction to Computing Using Python [E. Andersen, A. Bracy, D. Gries, L. Lee, S. Marschner, C. Van Loan, W. White] Announcements Prelim 1


  1. http://www.cs.cornell.edu/courses/cs1110/2018sp Lecture 15: Recursion (Sections 5.8-5.10) CS 1110 Introduction to Computing Using Python [E. Andersen, A. Bracy, D. Gries, L. Lee, S. Marschner, C. Van Loan, W. White]

  2. Announcements Prelim 1 • Graded. 2 Emails went out (1 from gradescope, 1 from CMS) and some #s released • Regrade requests : we are processing them • Q3 : we are re-evaluating them Lab 8: alive! (There was no Lab 7.) 2

  3. Recursion • Recursive Function : A function that calls itself (directly or indirectly) 3

  4. Examples • Blast-off • Gift in gift • Family Trees • Towers of Hanoi • Deblanking 4

  5. Tower of Hanoi • Three towers: left , middle , and right • n disks of unique sizes on left • Goal : move all disks from left to right • Cannot put a larger disk on top of a smaller disk 1 2 3 4 middle right left 5

  6. 1 Disc: Easy! 1. Move from left to right 1 left middle right 6

  7. 2 Discs: Step 1 1. Move from left to middle 1 2 left middle right 7

  8. 2 Discs: Step 2 1. Move from left to middle 2. Move from left to right 2 1 left middle right 8

  9. 2 Discs: Step 3 (final) 1. Move from left to middle 2. Move from left to right 3. Move from middle to right 1 2 left middle right 9

  10. 3 Discs: Step 1 1. Move from left to right 1 2 3 left middle right 10

  11. 3 Discs: Step 2 1. Move from left to right 2. Move from left to middle 2 3 1 left middle right 11

  12. 3 Discs: Step 3 1. Move from left to right 2. Move from left to middle 3. Move from right to middle 3 2 1 left middle right 12

  13. 3 Discs: Step 4 1. Move from left to right 2. Move from left to middle 3. Move from right to middle 4. Move from left to right 1 3 2 left middle right 13

  14. 3 Discs: Step 5 1. Move from left to right 2. Move from left to middle 3. Move from right to middle 4. Move from left to right 5. Move from middle to left 1 2 3 left middle right 14

  15. 3 Discs: Step 6 1. Move from left to right 2. Move from left to middle 3. Move from right to middle 4. Move from left to right 5. Move from middle to left 6. Move from middle to right 1 2 3 left middle right 15

  16. 3 Discs: Step 7 (final) 1. Move from left to right 2. Move from left to middle 3. Move from right to middle 4. Move from left to right 5. Move from middle to left 2 6. Move from middle to right 1 3 left middle right 7. Move from left to right 16

  17. 4 Discs: Oh, boy... BUT, we already know how to solve Hanoi for 3 towers. Sooo…. 1 2 3 4 left middle right 17

  18. Divide and Conquer Goal : Solve really big problem P Idea : Split into smaller problems, solve, combine 3 Steps: 1. Decide what to do for simple cases 2. Decide how to break up the task 3. Decide how to combine your work 18

  19. Decide what to do for simple cases Move from left to right 1 left middle right 19

  20. Decide how to break up the task Simpler than the original task, ß 1 slowly becoming the “simple case” 2 3 ß Simple case 4 left 20

  21. Decide how to combine your work 1 1 = 2 1 2 3 3 4 4 Hanoi(3) Hanoi(4) (uncover the big one) 2 3 1 2 1 2 3 4 3 4 Hanoi(3) move the big one 21 (cover the big one)

  22. 4 Discs: High-level Idea 1. Move top three disks from left to middle 2. Move largest disk from left to right 1 1 3 3. Move top three disks from 2 3 middle to right 4 left middle right 2 22

  23. solve_hanoi(source, target, other, n_disks) “”” Prints instructions for how to move n_disks (sorted small to large, going down) from the source peg to the target peg, using the other peg if needed. “”” if (n== 1): print(“move from ”+source+“ to ”+ target) else: # need to move top n_disks-1 disks from source to # other… luckily, I have a function that does that! solve_hanoi(source, other, target, n_disks-1) # move the last disk to the target, that’s easy! print(“move from ”+source+“ to ”+ target) # now put everything back on the last disk at target solve_hanoi(other, target, source, n_disks-1) 23

  24. Recursion vs Iteration • Recursion is provably equivalent to iteration § Iteration includes for-loop and while-loop (later) § Anything can do in one, can do in the other • But some things are easier with recursion § And some things are easier with iteration • Will not teach you when to choose recursion • We just want you to understand the technique 24

  25. Examples • Blast-off • Gift in gift • Family Trees • Towers of Hanoi • Deblanking 25

  26. Divide and Conquer Goal : Solve problem P on a piece of data data 26

  27. Divide and Conquer Goal : Solve problem P on a piece of data data Idea : Split data into two parts and solve problem data 1 data 2 Solve Problem P Solve Problem P 27

  28. Divide and Conquer Goal : Solve problem P on a piece of data data Idea : Split data into two parts and solve problem data 1 data 2 Solve Problem P Solve Problem P Combine Answer! 28

  29. Exercise: Remove Blanks from a String def deblank(s): """Returns: s but with its blanks removed""" 1. Decide what to do on “small” data § If it is the empty string , nothing to do if s == '': return s § If it is a single character , delete it if a blank if s == ' ': # There is a space here return '' # Empty string else: return s 29

  30. Exercise: Remove Blanks from a String def deblank(s): """Returns: s but with its blanks removed""" 2. Decide how to break it up left = deblank(s[0]) # A string with no blanks right = deblank(s[1:]) # A string with no blanks 3. Decide how to combine the answer return left+right # String concatenation 30

  31. Putting it All Together def deblank(s): """Returns: s w/o blanks""" if s == '': return s elif len(s) == 1: Handle simple cases if s[0] == ‘ ‘: return '' else: return s left = deblank(s[0]) Break up the data right = deblank(s[1:]) return left+right Combine answers 31

  32. Putting it All Together def deblank(s): """Returns: s w/o blanks""" if s == '': return s elif len(s) == 1: Base Case if s[0] == ‘ ‘: return '' else: return s Recursive left = deblank(s[0]) Case right = deblank(s[1:]) return left+right 32

  33. Following the Recursion deblank a b c deblank deblank a b c stop (base case) deblank a b c deblank stop (base case) … 33

  34. Breaking it up (1) deblank a b c a b c deblank 34

  35. Breaking it up (2) deblank a b c a b c deblank a b c deblank 35

  36. Breaking it up (3) deblank a b c a b c deblank a b c deblank b c deblank 36

  37. Breaking it up (4) deblank a b c a b c deblank a b c deblank b c deblank b c deblank 37

  38. Breaking it up (5) deblank a b c a b c deblank a b c deblank b c deblank b c deblank deblank c 38

  39. Breaking it up (6) deblank a b c a b c deblank a b c deblank b c deblank b c deblank deblank c c 39

  40. Combining Left+Right (1) deblank a b c a b c deblank a b c deblank b c deblank b c deblank deblank c c c 40

  41. Combining Left+Right (2) deblank a b c a b c deblank a b c deblank b c deblank b c deblank û deblank c c c c 41

  42. Combining Left+Right (3) deblank a b c a b c deblank a b c deblank b c deblank b c b c deblank û deblank c c c c 42

  43. Combining Left+Right (4) deblank a b c a b c deblank a b c deblank û b c b c deblank b c b c deblank û deblank c c c c 43

  44. Combining Left+Right (5) deblank a b c a b c deblank a b c a b c deblank û b c b c deblank b c b c deblank û deblank c c c c 44

  45. Combining Left+Right (6) deblank a b c û a b c deblank a b c a b c a b c deblank û b c b c deblank b c b c deblank û deblank c c c c 45

  46. Combining Left+Right (7) deblank a b c a b c û a b c deblank a b c a b c a b c deblank û b c b c deblank b c b c deblank û deblank c c c c 46

Recommend


More recommend