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

lecture 15 recursion
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 1

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]

http://www.cs.cornell.edu/courses/cs1110/2018sp

slide-2
SLIDE 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

slide-3
SLIDE 3

Recursion

  • Recursive Function:

A function that calls itself (directly or indirectly)

3

slide-4
SLIDE 4

Examples

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

4

slide-5
SLIDE 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

5

4

left middle right

3 2 1

slide-6
SLIDE 6

1 Disc: Easy!

  • 1. Move from left to right

6

1

left middle right

slide-7
SLIDE 7

2 Discs: Step 1

  • 1. Move from left to middle

7

left middle right

2 1

slide-8
SLIDE 8

2 Discs: Step 2

8

left middle right

2 1

  • 1. Move from left to middle
  • 2. Move from left to right
slide-9
SLIDE 9

2 Discs: Step 3 (final)

9

left middle right

2 1

  • 1. Move from left to middle
  • 2. Move from left to right
  • 3. Move from middle to right
slide-10
SLIDE 10

3 Discs: Step 1

  • 1. Move from left to right

10

left middle right

3 2 1

slide-11
SLIDE 11

3 Discs: Step 2

  • 1. Move from left to right
  • 2. Move from left to middle

11

left middle right

3 2 1

slide-12
SLIDE 12

3 Discs: Step 3

  • 1. Move from left to right
  • 2. Move from left to middle
  • 3. Move from right to middle

12

left middle right

3 2 1

slide-13
SLIDE 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

13

left middle right

3 2 1

slide-14
SLIDE 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

14

left middle right

3 2 1

slide-15
SLIDE 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

15

left middle right

3 2 1

slide-16
SLIDE 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
  • 6. Move from middle to right
  • 7. Move from left to right

16

left middle right

3 2 1

slide-17
SLIDE 17

4

4 Discs: Oh, boy...

17

left middle right

3 2 1

BUT, we already know how to solve Hanoi for 3 towers. Sooo….

slide-18
SLIDE 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

slide-19
SLIDE 19

Decide what to do for simple cases

19

1

left middle right

Move from left to right

slide-20
SLIDE 20

Decide how to break up the task

20

4

left

3 2 1

ß Simple case Simpler than the original task, slowly becoming the “simple case” ß

slide-21
SLIDE 21

Decide how to combine your work

21

4 3 2 1 4 3 2 1 4 3 2 1 4 3 2 1

1 2 3

Hanoi(3)

(uncover the big one)

move the big one Hanoi(3)

(cover the big one)

Hanoi(4)

=

slide-22
SLIDE 22

4

4 Discs: High-level Idea

22

left middle right

3 2 1

  • 1. Move top three disks from

left to middle

  • 2. Move largest disk from left

to right

  • 3. Move top three disks from

middle to right

1 2 3

slide-23
SLIDE 23

23

solve_hanoi(source, target, other, n_disks)

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)

“””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. “””

slide-24
SLIDE 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

slide-25
SLIDE 25

Examples

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

25

slide-26
SLIDE 26

Divide and Conquer

Goal: Solve problem P on a piece of data

26

data

slide-27
SLIDE 27

Divide and Conquer

Goal: Solve problem P on a piece of data

27

data

Idea: Split data into two parts and solve problem

data 1 data 2

Solve Problem P Solve Problem P

slide-28
SLIDE 28

Divide and Conquer

Goal: Solve problem P on a piece of data

28

data

Idea: Split data into two parts and solve problem

data 1 data 2

Solve Problem P Solve Problem P Combine Answer!

slide-29
SLIDE 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

slide-30
SLIDE 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

slide-31
SLIDE 31

Putting it All Together

def deblank(s): """Returns: s w/o blanks""" if s == '': return s elif len(s) == 1: if s[0] == ‘ ‘: return '' else: return s left = deblank(s[0]) right = deblank(s[1:]) return left+right

31

Handle simple cases Break up the data Combine answers

slide-32
SLIDE 32

Putting it All Together

def deblank(s): """Returns: s w/o blanks""" if s == '': return s elif len(s) == 1: if s[0] == ‘ ‘: return '' else: return s left = deblank(s[0]) right = deblank(s[1:]) return left+right

32

Base Case Recursive Case

slide-33
SLIDE 33

Following the Recursion

a b c deblank

33

deblank a b c deblank stop (base case) a deblank stop (base case) b c deblank …

slide-34
SLIDE 34

Breaking it up (1)

a b c deblank a b c deblank

34

slide-35
SLIDE 35

Breaking it up (2)

a b c a deblank a b c deblank b c deblank

35

slide-36
SLIDE 36

Breaking it up (3)

a b c a deblank a b c deblank b c deblank b c deblank

36

slide-37
SLIDE 37

Breaking it up (4)

a b c a b deblank a b c deblank b c deblank b c deblank c deblank

37

slide-38
SLIDE 38

Breaking it up (5)

a b c a b deblank a b c deblank b c deblank b c deblank c deblank c deblank

38

slide-39
SLIDE 39

Breaking it up (6)

a b c a b deblank a b c deblank b c deblank b c deblank c deblank c deblank c

39

slide-40
SLIDE 40

Combining Left+Right (1)

a b c a b deblank a b c deblank b c deblank b c deblank c deblank c deblank c c

40

slide-41
SLIDE 41

Combining Left+Right (2)

a b c a b deblank a b c deblank b c deblank b c deblank c deblank c deblank c c c

û

41

slide-42
SLIDE 42

Combining Left+Right (3)

a b c a b deblank a b c deblank b c deblank b c deblank c deblank c deblank c c c c b

û

42

slide-43
SLIDE 43

Combining Left+Right (4)

a b c a b deblank a b c deblank b c deblank b c deblank c deblank c deblank c c c c b c b

û û

43

slide-44
SLIDE 44

Combining Left+Right (5)

a b c a b deblank a b c deblank b c deblank b c deblank c deblank c deblank c c c c b c b c b a

û û

44

slide-45
SLIDE 45

Combining Left+Right (6)

a b c a b deblank a b c deblank b c deblank b c deblank c deblank c deblank c c c c b c b c b a c b a

û û û

45

slide-46
SLIDE 46

Combining Left+Right (7)

a b c a b c c c c b c b c b a c b a c b a deblank a b c deblank b c deblank b c deblank c deblank c deblank

û û û

46