Recursion Announcements for Today Prelim 1 Other Announcements - - PowerPoint PPT Presentation

recursion announcements for today
SMART_READER_LITE
LIVE PREVIEW

Recursion Announcements for Today Prelim 1 Other Announcements - - PowerPoint PPT Presentation

Lecture 14 Recursion Announcements for Today Prelim 1 Other Announcements Reading: 5.8 5.10 Tonight at 5:15 OR 7:30 Assignment 3 now graded AD (5:15, Uris G01) Mean 93.4, Median 98 E-K (5:15, Statler) Time : 7


slide-1
SLIDE 1

Recursion

Lecture 14

slide-2
SLIDE 2

Announcements for Today

Prelim 1

  • Tonight at 5:15 OR 7:30

§ A–D (5:15, Uris G01) § E-K (5:15, Statler) § L–P (7:30, Uris G01) § Q-Z (7:30, Statler)

  • Graded by noon on Sun

§ Scores will be in CMS § In time for drop date

Other Announcements

  • Reading: 5.8 – 5.10
  • Assignment 3 now graded

§ Mean 93.4, Median 98 § Time: 7 hrs, StdDev: 3.5 hrs § But only 535 responses

  • Assignment 4 posted Friday

§ Parts 1-3: Can do already § Part 4: material from today § Due two weeks from today

10/11/18 Recursion 2

slide-3
SLIDE 3

Recursion

  • Recursive Definition:

A definition that is defined in terms of itself

  • Recursive Function:

A function that calls itself (directly or indirectly)

PIP stands for “PIP Installs Packages”

10/11/18 Recursion 3

slide-4
SLIDE 4

A Mathematical Example: Factorial

  • Non-recursive definition:

n! = n n-1 … 2 1 = n (n-1 … 2 1)

  • Recursive definition:

n! = n (n-1)! 0! = 1

10/11/18 Recursion 4

for n ≥ 0 Recursive case Base case What happens if there is no base case?

slide-5
SLIDE 5

Factorial as a Recursive Function

def factorial(n): """Returns: factorial of n. Pre: n ≥ 0 an int""" if n == 0: return 1 return n*factorial(n-1)

  • n! = n (n-1)!
  • 0! = 1

10/11/18 Recursion 5

What happens if there is no base case? Recursive case Base case(s)

slide-6
SLIDE 6

Example: Fibonnaci Sequence

  • Sequence of numbers: 1, 1, 2, 3, 5, 8, 13, ...

a0 a1 a2 a3 a4 a5 a6

§ Get the next number by adding previous two § What is a8?

10/11/18 Recursion 6

A: a8 = 21 B: a8 = 29 C: a8 = 34 D: None of these.

slide-7
SLIDE 7

Example: Fibonnaci Sequence

  • Sequence of numbers: 1, 1, 2, 3, 5, 8, 13, ...

a0 a1 a2 a3 a4 a5 a6

§ Get the next number by adding previous two § What is a8?

10/11/18 Recursion 7

A: a8 = 21 B: a8 = 29 C: a8 = 34 D: None of these. correct

slide-8
SLIDE 8

Example: Fibonnaci Sequence

  • Sequence of numbers: 1, 1, 2, 3, 5, 8, 13, ...

a0 a1 a2 a3 a4 a5 a6

§ Get the next number by adding previous two § What is a8?

  • Recursive definition:

§ an = an-1 + an-2 Recursive Case § a0 = 1 Base Case § a1 = 1 (another) Base Case

10/11/18 Recursion 8

Why did we need two base cases this time?

slide-9
SLIDE 9

Fibonacci as a Recursive Function

def fibonacci(n):

"""Returns: Fibonacci no. an Precondition: n ≥ 0 an int""" if n <= 1: return 1 return (fibonacci(n-1)+ fibonacci(n-2))

10/11/18 Recursion 9

Recursive case Base case(s) Note difference with base case conditional.

slide-10
SLIDE 10

Fibonacci as a Recursive Function

def fibonacci(n):

"""Returns: Fibonacci no. an Precondition: n ≥ 0 an int""" if n <= 1: return 1 return (fibonacci(n-1)+ fibonacci(n-2))

  • Function that calls itself

§ Each call is new frame § Frames require memory § ∞ calls = ∞ memory

10/11/18 Recursion 10

n fibonacci 3 5 n fibonacci 1 4 n fibonacci 1 3

slide-11
SLIDE 11

Fibonacci: # of Frames vs. # of Calls

  • Fibonacci is very inefficient.

§ fib(n) has a stack that is always ≤ n § But fib(n) makes a lot of redundant calls

fib(5) fib(4) fib(3) fib(2) fib(2) fib(1) fib(0) fib(0) fib(1) fib(1)

11 Recursion

fib(3) fib(2) fib(1) fib(0) fib(1)

10/11/18

slide-12
SLIDE 12

Fibonacci: # of Frames vs. # of Calls

  • Fibonacci is very inefficient.

§ fib(n) has a stack that is always ≤ n § But fib(n) makes a lot of redundant calls

fib(5) fib(4) fib(3) fib(2) fib(2) fib(1) fib(0) fib(0) fib(1) fib(1)

12 Recursion

fib(3) fib(2) fib(1) fib(0) fib(1) Path to end = the call stack

10/11/18

slide-13
SLIDE 13

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

§ This is a topic for more advanced classes

  • We just want you to understand the technique

10/11/18 Recursion 13

slide-14
SLIDE 14

Recursion is best for Divide and Conquer

Goal: Solve problem P on a piece of data

10/11/18 Recursion 14

data

slide-15
SLIDE 15

Recursion is best for Divide and Conquer

Goal: Solve problem P on a piece of data

10/11/18 Recursion 15

data

Idea: Split data into two parts and solve problem

data 1 data 2

Solve Problem P Solve Problem P

slide-16
SLIDE 16

Recursion is best for Divide and Conquer

Goal: Solve problem P on a piece of data

10/11/18 Recursion 16

data

Idea: Split data into two parts and solve problem

data 1 data 2

Solve Problem P Solve Problem P Combine Answer!

slide-17
SLIDE 17

Divide and Conquer Example

Count the number of 'e's in a string:

10/11/18 Recursion 17

p e n n e

Two 'e's

p e n n e

One 'e' One 'e'

slide-18
SLIDE 18

Divide and Conquer Example

Count the number of 'e's in a string:

10/11/18 Recursion 18

p e n n e

Two 'e's

p e n n e

Zero 'e's Two 'e's

slide-19
SLIDE 19

Divide and Conquer Example

Count the number of 'e's in a string:

10/11/18 Recursion 19

p e n n e

Two 'e's

p e n n e

Zero 'e's Two 'e's

Will talk about how to break-up later

slide-20
SLIDE 20

Three Steps for Divide and Conquer

  • 1. Decide what to do on “small” data

§ Some data cannot be broken up § Have to compute this answer directly

  • 2. Decide how to break up your data

§ Both “halves” should be smaller than whole § Often no wrong way to do this (next lecture)

  • 3. Decide how to combine your answers

§ Assume the smaller answers are correct § Combining them should give bigger answer

10/11/18 Recursion 20

slide-21
SLIDE 21

Divide and Conquer Example

def num_es(s): """Returns: # of 'e's in s""" # 1. Handle small data if s == '': return 0 elif len(s) == 1: return 1 if s[0] == 'e' else 0 # 2. Break into two parts left = num_es(s[0]) right = num_es(s[1:]) # 3. Combine the result return left+right

“Short-cut” for

if s[0] == 'e’: return 1 else: return 0

10/11/18 Recursion 21

p e n n e

2 +

s[0] s[1:]

slide-22
SLIDE 22

Divide and Conquer Example

def num_es(s): """Returns: # of 'e's in s""" # 1. Handle small data if s == '': return 0 elif len(s) == 1: return 1 if s[0] == 'e' else 0 # 2. Break into two parts left = num_es(s[0]) right = num_es(s[1:]) # 3. Combine the result return left+right

“Short-cut” for

if s[0] == 'e’: return 1 else: return 0

10/11/18 Recursion 22

p e n n e

2 +

s[0] s[1:]

slide-23
SLIDE 23

Divide and Conquer Example

def num_es(s): """Returns: # of 'e's in s""" # 1. Handle small data if s == '': return 0 elif len(s) == 1: return 1 if s[0] == 'e' else 0 # 2. Break into two parts left = num_es(s[0]) right = num_es(s[1:]) # 3. Combine the result return left+right

“Short-cut” for

if s[0] == 'e’: return 1 else: return 0

10/11/18 Recursion 23

p e n n e

2 +

s[0] s[1:]

slide-24
SLIDE 24

Divide and Conquer Example

def num_es(s): """Returns: # of 'e's in s""" # 1. Handle small data if s == '': return 0 elif len(s) == 1: return 1 if s[0] == 'e' else 0 # 2. Break into two parts left = num_es(s[0]) right = num_es(s[1:]) # 3. Combine the result return left+right

“Short-cut” for

if s[0] == 'e’: return 1 else: return 0

10/11/18 Recursion 24

p e n n e

2 +

s[0] s[1:]

slide-25
SLIDE 25

Divide and Conquer Example

def num_es(s): """Returns: # of 'e's in s""" # 1. Handle small data if s == '': return 0 elif len(s) == 1: return 1 if s[0] == 'e' else 0 # 2. Break into two parts left = num_es(s[0]) right = num_es(s[1:]) # 3. Combine the result return left+right

10/11/18 Recursion 25

Base Case Recursive Case

slide-26
SLIDE 26

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

10/11/18 Recursion 26

slide-27
SLIDE 27

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

10/11/18 Recursion 27

slide-28
SLIDE 28

Putting it All Together

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

10/11/18 Recursion 28

Handle small data Break up the data Combine answers

slide-29
SLIDE 29

Putting it All Together

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

10/11/18 Recursion 29

Base Case Recursive Case

slide-30
SLIDE 30

Minor Optimization

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

10/11/18 Recursion 30

Needed second base case to handle s[0]

slide-31
SLIDE 31

Minor Optimization

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

10/11/18 Recursion 31

Eliminate the second base by combining

Less recursive calls

slide-32
SLIDE 32

Following the Recursion

a b c deblank

10/11/18 Recursion 32

slide-33
SLIDE 33

Following the Recursion

a b c deblank a b c deblank

10/11/18 Recursion 33

slide-34
SLIDE 34

Following the Recursion

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

10/11/18 Recursion 34

slide-35
SLIDE 35

Following the Recursion

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

10/11/18 Recursion 35

slide-36
SLIDE 36

Following the Recursion

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

10/11/18 Recursion 36

slide-37
SLIDE 37

Following the Recursion

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

10/11/18 Recursion 37

slide-38
SLIDE 38

Following the Recursion

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

10/11/18 Recursion 38

slide-39
SLIDE 39

Following the Recursion

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

10/11/18 Recursion 39

slide-40
SLIDE 40

Following the Recursion

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

10/11/18 Recursion 40

slide-41
SLIDE 41

Following the Recursion

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

10/11/18 Recursion 41

slide-42
SLIDE 42

Following the Recursion

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

10/11/18 Recursion 42

slide-43
SLIDE 43

Following the Recursion

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

10/11/18 Recursion 43

slide-44
SLIDE 44

Following the Recursion

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

10/11/18 Recursion 44

slide-45
SLIDE 45

Following the Recursion

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

10/11/18 Recursion 45

slide-46
SLIDE 46

Final Modification

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

10/11/18 Recursion 46

Real work done here

slide-47
SLIDE 47

Final Modification

def deblank(s): """Returns: s w/o blanks""" if s == '': return s left = s if s[0] in string.whitespace left = '' right = deblank(s[1:]) return left+right

10/11/18 Recursion 47

Module string has special constants to simplify detection of whitespace and other characters. Real work done here

slide-48
SLIDE 48

Next Time: Breaking Up Recursion

10/11/18 Recursion 48