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/2019sp 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] Recursion Recursive


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/2019sp

slide-2
SLIDE 2

Recursion

Recursive Function: A function that calls itself (see also Recursive Function) Two parts to every recursive function:

  • 1. A simple case: can be solved easily
  • 2. A complex case: can be made simpler (and simpler,

and simpler… until it looks like the simple case)

2

slide-3
SLIDE 3

3

Russian Dolls!

slide-4
SLIDE 4

4

What is the simple case that can be solved easily?

A: The case where the doll has a seam and another doll inside of it. B: The case where the doll has no seam and no doll inside of it. C: A & B are both simple D: I do not know

slide-5
SLIDE 5

5

Russian Dolls!

import russian d1 = russian.Doll("Dmitry", None) d2 = russian.Doll("Catherine", d1)

id1

name hasSeam "Dmitry" False None Doll innerDoll

id1 d1

Heap Space Global Space

id2

name hasSeam "Catherine" True id1 Doll innerDoll

id2 d2

"Dmitry" "Catherine"

slide-6
SLIDE 6

def open_doll(d): """Input: a Russian Doll Opens the Russian Doll d """ print("My name is "+ d.name) if d.hasSeam: inner = d.innerDoll

  • pen_doll(inner)

else: print("That's it!") idx

name hasSeam Doll innerDoll

slide-7
SLIDE 7

Examples

  • Russian Dolls
  • Blast Off!
  • Towers of Hanoi

7

slide-8
SLIDE 8

blast_off(5) # must be a positive int 5 4 3 2 1 BLAST OFF! blast_off(0) BLAST OFF!

8

Blast Off!

slide-9
SLIDE 9

blast_off(5) # must be a positive int 5 4 3 2 1 BLAST OFF! blast_off(0) BLAST OFF!

9

Blast Off!

What is the simple case that can be solved easily?

A: negative n B: positive n C: n == 0 D: n == 1 E: I do not know.

slide-10
SLIDE 10

def blast_off(n): """Input: a positive int Counts down from n to Blast-Off! """ if (n == 0): print("BLAST OFF!") else: print(n) blast_off(n-1)

10

Blast Off!

slide-11
SLIDE 11

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

11

4

left middle right

3 2 1

slide-12
SLIDE 12

1 Disc: Easy!

  • 1. Move from left to right

12

1

left middle right

Solving for 1 tower is easy! That's the simple case!

slide-13
SLIDE 13

2 Discs: Step 1

  • 1. Move from left to middle

13

left middle right

2 1

Thought: If I could get Disk 1 off of Disk 2, I could move Disk 2 to where it's supposed to go…. Moving 1 disk is easy!

slide-14
SLIDE 14

2 Discs: Step 2

14

left middle right

2 1

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

Thought: Now that Disk 1 is gone, I can move Disk 2 to where it's supposed to go.

slide-15
SLIDE 15

2 Discs: Step 3 (final)

15

left middle right

2 1

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

Thought: Now that Disk 2, is where it's supposed to be, all I have to do is move Disk 1. Moving 1 disk is easy!

slide-16
SLIDE 16

3 Discs!

16

left middle right

3 2 1

Thought: If I could get Disks 1& 2 off of Disk 3, I could move Disk 3 to where it's supposed to go…. And I know how to move 2 Disks from the previous slide!

slide-17
SLIDE 17

3 Discs: Moving Disks 1&2 off of Disk 3 (1)

  • 1. Move from left to right

17

left middle right

3 2 1

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

18

left middle right

3 2 1

3 Discs: Moving Disks 1&2 off of Disk 3 (2)

slide-19
SLIDE 19

3 Discs: Moving Disks 1&2 off of Disk 3 (3)

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

19

left middle right

3 2 1

slide-20
SLIDE 20

3 Discs: Move Disk 3 to the Goal

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

20

left middle right

3 2 1

slide-21
SLIDE 21

3 Discs: Moving Disks 1&2 to the Goal (1)

  • 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

21

left middle right

3 2 1

slide-22
SLIDE 22

3 Discs: Moving Disks 1&2 to the Goal (2)

  • 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

22

left middle right

3 2 1

slide-23
SLIDE 23

3 Discs: Moving Disks 1&2 to the Goal (3)

  • 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

23

left middle right

3 2 1

slide-24
SLIDE 24

4

4 Discs: Oh, boy...

24

left middle right

3 2 1

Thought: If I could get Disks 1&2&3 off of Disk 4, I could move Disk 4 to where it's supposed to go…. And I know how to move 3 Disks from the previous slide!

slide-25
SLIDE 25

Rely on the solution for the simpler case

25

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

1 2 3

Hanoi(3,LàM)

(uncover the big one)

move the big one Hanoi(3,MàR)

(cover the big one)

Hanoi(4,LàR)

=

slide-26
SLIDE 26

26

solve_hanoi(n, start, goal, temp)

if n == 1: print("move from "+start+" to "+goal) else: # need to move top n-1 disks from start to temp so that I can move # the bottom disk to goal… luckily, I have a function that does that! solve_hanoi(n-1, start, temp, goal) # move the bottom disk from start to goal print(“move from ”+ start +“ to ”+ goal) # now put everything back on the last disk at goal solve_hanoi(n-1, temp, goal, start)

"""Prints instructions for how to move n disks (sorted small to large, going down) from the start peg to the goal peg, using the temp peg if needed. """

1 2 3

slide-27
SLIDE 27

Divide and Conquer

Goal: Solve really big problem P Idea: Split into simpler 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

27

slide-28
SLIDE 28

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

28