CS 115 Lecture 20 Recursion Neil Moore Department of Computer - - PowerPoint PPT Presentation

cs 115 lecture 20
SMART_READER_LITE
LIVE PREVIEW

CS 115 Lecture 20 Recursion Neil Moore Department of Computer - - PowerPoint PPT Presentation

CS 115 Lecture 20 Recursion Neil Moore Department of Computer Science University of Kentucky Lexington, Kentucky 40506 neil@cs.uky.edu 1 December 2015 Recursion Problemscomputational, mathematical, and otherwisecan be defined and


slide-1
SLIDE 1

CS 115 Lecture 20

Recursion Neil Moore

Department of Computer Science University of Kentucky Lexington, Kentucky 40506 neil@cs.uky.edu

1 December 2015

slide-2
SLIDE 2

Recursion

Problems—computational, mathematical, and otherwise—can be defined and solved recursively. That is, in terms of themselves.

Neil Moore (UK CS) CS 115 Lecture 20 Fall 2015 2 / 12

slide-3
SLIDE 3

Recursion

Problems—computational, mathematical, and otherwise—can be defined and solved recursively. That is, in terms of themselves. A compound sentence is two sentences with “and” between them.

Neil Moore (UK CS) CS 115 Lecture 20 Fall 2015 2 / 12

slide-4
SLIDE 4

Recursion

Problems—computational, mathematical, and otherwise—can be defined and solved recursively. That is, in terms of themselves. A compound sentence is two sentences with “and” between them. A Python expression may contain two expressions with an operator between them: (3 + 2) * (4 - 9).

Neil Moore (UK CS) CS 115 Lecture 20 Fall 2015 2 / 12

slide-5
SLIDE 5

Recursion

Problems—computational, mathematical, and otherwise—can be defined and solved recursively. That is, in terms of themselves. A compound sentence is two sentences with “and” between them. A Python expression may contain two expressions with an operator between them: (3 + 2) * (4 - 9). Point a video camera at its own display—hall of mirrors.

Neil Moore (UK CS) CS 115 Lecture 20 Fall 2015 2 / 12

slide-6
SLIDE 6

Recursion

Problems—computational, mathematical, and otherwise—can be defined and solved recursively. That is, in terms of themselves. A compound sentence is two sentences with “and” between them. A Python expression may contain two expressions with an operator between them: (3 + 2) * (4 - 9). Point a video camera at its own display—hall of mirrors. Many mathematical structures are defined recursively.

◮ Fibonacci numbers, factorials, fractals, . . . Neil Moore (UK CS) CS 115 Lecture 20 Fall 2015 2 / 12

slide-7
SLIDE 7

Recursion

Problems—computational, mathematical, and otherwise—can be defined and solved recursively. That is, in terms of themselves. A compound sentence is two sentences with “and” between them. A Python expression may contain two expressions with an operator between them: (3 + 2) * (4 - 9). Point a video camera at its own display—hall of mirrors. Many mathematical structures are defined recursively.

◮ Fibonacci numbers, factorials, fractals, . . . ◮ Mathematicians call this induction (same thing as recursion). ◮ It’s also a common method of mathematical proof. Neil Moore (UK CS) CS 115 Lecture 20 Fall 2015 2 / 12

slide-8
SLIDE 8

Recursion

Problems—computational, mathematical, and otherwise—can be defined and solved recursively. That is, in terms of themselves. A compound sentence is two sentences with “and” between them. A Python expression may contain two expressions with an operator between them: (3 + 2) * (4 - 9). Point a video camera at its own display—hall of mirrors. Many mathematical structures are defined recursively.

◮ Fibonacci numbers, factorials, fractals, . . . ◮ Mathematicians call this induction (same thing as recursion). ◮ It’s also a common method of mathematical proof.

Search for recursion on Google.

◮ Note the search suggestion. Neil Moore (UK CS) CS 115 Lecture 20 Fall 2015 2 / 12

slide-9
SLIDE 9

Recursion

Problems—computational, mathematical, and otherwise—can be defined and solved recursively. That is, in terms of themselves. A compound sentence is two sentences with “and” between them. A Python expression may contain two expressions with an operator between them: (3 + 2) * (4 - 9). Point a video camera at its own display—hall of mirrors. Many mathematical structures are defined recursively.

◮ Fibonacci numbers, factorials, fractals, . . . ◮ Mathematicians call this induction (same thing as recursion). ◮ It’s also a common method of mathematical proof.

Search for recursion on Google.

◮ Note the search suggestion. Neil Moore (UK CS) CS 115 Lecture 20 Fall 2015 2 / 12

slide-10
SLIDE 10

Recursion in programming

The idea behind recursion in programming:

◮ Break down a complex problem into a simpler version

  • f the same problem.

Neil Moore (UK CS) CS 115 Lecture 20 Fall 2015 3 / 12

slide-11
SLIDE 11

Recursion in programming

The idea behind recursion in programming:

◮ Break down a complex problem into a simpler version

  • f the same problem.

◮ Implemented by functions that call themselves. Neil Moore (UK CS) CS 115 Lecture 20 Fall 2015 3 / 12

slide-12
SLIDE 12

Recursion in programming

The idea behind recursion in programming:

◮ Break down a complex problem into a simpler version

  • f the same problem.

◮ Implemented by functions that call themselves. ⋆ Recursive functions. Neil Moore (UK CS) CS 115 Lecture 20 Fall 2015 3 / 12

slide-13
SLIDE 13

Recursion in programming

The idea behind recursion in programming:

◮ Break down a complex problem into a simpler version

  • f the same problem.

◮ Implemented by functions that call themselves. ⋆ Recursive functions. ◮ The same computation recurs (occurs repeatedly). ⋆ This is not the same as iteration (looping)! Neil Moore (UK CS) CS 115 Lecture 20 Fall 2015 3 / 12

slide-14
SLIDE 14

Recursion in programming

The idea behind recursion in programming:

◮ Break down a complex problem into a simpler version

  • f the same problem.

◮ Implemented by functions that call themselves. ⋆ Recursive functions. ◮ The same computation recurs (occurs repeatedly). ⋆ This is not the same as iteration (looping)! ⋆ But it is possible to convert iteration to recursion, and vice versa. Neil Moore (UK CS) CS 115 Lecture 20 Fall 2015 3 / 12

slide-15
SLIDE 15

Recursion in programming

The idea behind recursion in programming:

◮ Break down a complex problem into a simpler version

  • f the same problem.

◮ Implemented by functions that call themselves. ⋆ Recursive functions. ◮ The same computation recurs (occurs repeatedly). ⋆ This is not the same as iteration (looping)! ⋆ But it is possible to convert iteration to recursion, and vice versa.

Recursion is often the most natural way of thinking about a problem.

Neil Moore (UK CS) CS 115 Lecture 20 Fall 2015 3 / 12

slide-16
SLIDE 16

Recursion in programming

The idea behind recursion in programming:

◮ Break down a complex problem into a simpler version

  • f the same problem.

◮ Implemented by functions that call themselves. ⋆ Recursive functions. ◮ The same computation recurs (occurs repeatedly). ⋆ This is not the same as iteration (looping)! ⋆ But it is possible to convert iteration to recursion, and vice versa.

Recursion is often the most natural way of thinking about a problem.

◮ Some computations are very difficult to perform without recursion. Neil Moore (UK CS) CS 115 Lecture 20 Fall 2015 3 / 12

slide-17
SLIDE 17

Recursion in programming

The idea behind recursion in programming:

◮ Break down a complex problem into a simpler version

  • f the same problem.

◮ Implemented by functions that call themselves. ⋆ Recursive functions. ◮ The same computation recurs (occurs repeatedly). ⋆ This is not the same as iteration (looping)! ⋆ But it is possible to convert iteration to recursion, and vice versa.

Recursion is often the most natural way of thinking about a problem.

◮ Some computations are very difficult to perform without recursion. Neil Moore (UK CS) CS 115 Lecture 20 Fall 2015 3 / 12

slide-18
SLIDE 18

Thinking recursively

Suppose we want to write a function that prints a triangle of stars. print triangle(4) →* * * * * * * * * * We could use nested loops, but let’s try using recursion instead.

Neil Moore (UK CS) CS 115 Lecture 20 Fall 2015 4 / 12

slide-19
SLIDE 19

Thinking recursively

Suppose we want to write a function that prints a triangle of stars. print triangle(4) →* * * * * * * * * * We could use nested loops, but let’s try using recursion instead.

◮ Pretend someone else has already written a function to print a triangle

  • f size 3.

Neil Moore (UK CS) CS 115 Lecture 20 Fall 2015 4 / 12

slide-20
SLIDE 20

Thinking recursively

Suppose we want to write a function that prints a triangle of stars. print triangle(4) →* * * * * * * * * * We could use nested loops, but let’s try using recursion instead.

◮ Pretend someone else has already written a function to print a triangle

  • f size 3. How would you print a triangle of size 4?

Neil Moore (UK CS) CS 115 Lecture 20 Fall 2015 4 / 12

slide-21
SLIDE 21

Thinking recursively

Suppose we want to write a function that prints a triangle of stars. print triangle(4) →* * * * * * * * * * We could use nested loops, but let’s try using recursion instead.

◮ Pretend someone else has already written a function to print a triangle

  • f size 3. How would you print a triangle of size 4?

⋆ First call that function. ⋆ Then print a row of four stars. Neil Moore (UK CS) CS 115 Lecture 20 Fall 2015 4 / 12

slide-22
SLIDE 22

Thinking recursively

Suppose we want to write a function that prints a triangle of stars. print triangle(4) →* * * * * * * * * * We could use nested loops, but let’s try using recursion instead.

◮ Pretend someone else has already written a function to print a triangle

  • f size 3. How would you print a triangle of size 4?

⋆ First call that function. ⋆ Then print a row of four stars. ◮ What about size 5? ⋆ Print a triangle of size 4. ⋆ Then print a row of five stars. Neil Moore (UK CS) CS 115 Lecture 20 Fall 2015 4 / 12

slide-23
SLIDE 23

Thinking recursively

Suppose we want to write a function that prints a triangle of stars. print triangle(4) →* * * * * * * * * * We could use nested loops, but let’s try using recursion instead.

◮ Pretend someone else has already written a function to print a triangle

  • f size 3. How would you print a triangle of size 4?

⋆ First call that function. ⋆ Then print a row of four stars. ◮ What about size 5? ⋆ Print a triangle of size 4. ⋆ Then print a row of five stars. ◮ Recursion: Use the solution to a simpler version of the same problem! Neil Moore (UK CS) CS 115 Lecture 20 Fall 2015 4 / 12

slide-24
SLIDE 24

Thinking recursively

Suppose we want to write a function that prints a triangle of stars. print triangle(4) →* * * * * * * * * * We could use nested loops, but let’s try using recursion instead.

◮ Pretend someone else has already written a function to print a triangle

  • f size 3. How would you print a triangle of size 4?

⋆ First call that function. ⋆ Then print a row of four stars. ◮ What about size 5? ⋆ Print a triangle of size 4. ⋆ Then print a row of five stars. ◮ Recursion: Use the solution to a simpler version of the same problem! Neil Moore (UK CS) CS 115 Lecture 20 Fall 2015 4 / 12

slide-25
SLIDE 25

A (broken) recursive function

def print triangle(side len): # First solve a simpler version of the problem. print triangle(side len - 1)

Neil Moore (UK CS) CS 115 Lecture 20 Fall 2015 5 / 12

slide-26
SLIDE 26

A (broken) recursive function

def print triangle(side len): # First solve a simpler version of the problem. print triangle(side len - 1) # Now turn it into the solution to this problem. # (by drawing the last line). print("* " * side len) print()

Neil Moore (UK CS) CS 115 Lecture 20 Fall 2015 5 / 12

slide-27
SLIDE 27

A (broken) recursive function

def print triangle(side len): # First solve a simpler version of the problem. print triangle(side len - 1) # Now turn it into the solution to this problem. # (by drawing the last line). print("* " * side len) print()

But there’s one small problem. . .

Neil Moore (UK CS) CS 115 Lecture 20 Fall 2015 5 / 12

slide-28
SLIDE 28

A (broken) recursive function

def print triangle(side len): # First solve a simpler version of the problem. print triangle(side len - 1) # Now turn it into the solution to this problem. # (by drawing the last line). print("* " * side len) print()

But there’s one small problem. . .

◮ It will never end! ◮ To print a triangle of size 1, first print a triangle of size 0. Neil Moore (UK CS) CS 115 Lecture 20 Fall 2015 5 / 12

slide-29
SLIDE 29

A (broken) recursive function

def print triangle(side len): # First solve a simpler version of the problem. print triangle(side len - 1) # Now turn it into the solution to this problem. # (by drawing the last line). print("* " * side len) print()

But there’s one small problem. . .

◮ It will never end! ◮ To print a triangle of size 1, first print a triangle of size 0. ◮ To do that, first print a triangle of size -1. . . Neil Moore (UK CS) CS 115 Lecture 20 Fall 2015 5 / 12

slide-30
SLIDE 30

A (broken) recursive function

def print triangle(side len): # First solve a simpler version of the problem. print triangle(side len - 1) # Now turn it into the solution to this problem. # (by drawing the last line). print("* " * side len) print()

But there’s one small problem. . .

◮ It will never end! ◮ To print a triangle of size 1, first print a triangle of size 0. ◮ To do that, first print a triangle of size -1. . . ⋆ What? Neil Moore (UK CS) CS 115 Lecture 20 Fall 2015 5 / 12

slide-31
SLIDE 31

A (broken) recursive function

def print triangle(side len): # First solve a simpler version of the problem. print triangle(side len - 1) # Now turn it into the solution to this problem. # (by drawing the last line). print("* " * side len) print()

But there’s one small problem. . .

◮ It will never end! ◮ To print a triangle of size 1, first print a triangle of size 0. ◮ To do that, first print a triangle of size -1. . . ⋆ What? Neil Moore (UK CS) CS 115 Lecture 20 Fall 2015 5 / 12

slide-32
SLIDE 32

The base case

Every recursion must end somewhere.

◮ At some point the problem is so simple we can solve it directly. Neil Moore (UK CS) CS 115 Lecture 20 Fall 2015 6 / 12

slide-33
SLIDE 33

The base case

Every recursion must end somewhere.

◮ At some point the problem is so simple we can solve it directly. ◮ Usually that is when the size of the problem is zero or one. Neil Moore (UK CS) CS 115 Lecture 20 Fall 2015 6 / 12

slide-34
SLIDE 34

The base case

Every recursion must end somewhere.

◮ At some point the problem is so simple we can solve it directly. ◮ Usually that is when the size of the problem is zero or one. ◮ We call this the base case or termination condition. Neil Moore (UK CS) CS 115 Lecture 20 Fall 2015 6 / 12

slide-35
SLIDE 35

The base case

Every recursion must end somewhere.

◮ At some point the problem is so simple we can solve it directly. ◮ Usually that is when the size of the problem is zero or one. ◮ We call this the base case or termination condition. ◮ How do we print a triangle with size zero? Neil Moore (UK CS) CS 115 Lecture 20 Fall 2015 6 / 12

slide-36
SLIDE 36

The base case

Every recursion must end somewhere.

◮ At some point the problem is so simple we can solve it directly. ◮ Usually that is when the size of the problem is zero or one. ◮ We call this the base case or termination condition. ◮ How do we print a triangle with size zero? ⋆ By doing nothing! Neil Moore (UK CS) CS 115 Lecture 20 Fall 2015 6 / 12

slide-37
SLIDE 37

The base case

Every recursion must end somewhere.

◮ At some point the problem is so simple we can solve it directly. ◮ Usually that is when the size of the problem is zero or one. ◮ We call this the base case or termination condition. ◮ How do we print a triangle with size zero? ⋆ By doing nothing!

def print triangle(side len): if side len <= 0 # Base case. pass # Do nothing. else: # Recursive case print triangle(side len - 1) print("* " * side len) print()

Neil Moore (UK CS) CS 115 Lecture 20 Fall 2015 6 / 12

slide-38
SLIDE 38

The base case

Every recursion must end somewhere.

◮ At some point the problem is so simple we can solve it directly. ◮ Usually that is when the size of the problem is zero or one. ◮ We call this the base case or termination condition. ◮ How do we print a triangle with size zero? ⋆ By doing nothing!

def print triangle(side len): if side len <= 0 # Base case. pass # Do nothing. else: # Recursive case print triangle(side len - 1) print("* " * side len) print()

Neil Moore (UK CS) CS 115 Lecture 20 Fall 2015 6 / 12

slide-39
SLIDE 39

Rules for recursion

There are three key requirements for a recursive function to work correctly.

Neil Moore (UK CS) CS 115 Lecture 20 Fall 2015 7 / 12

slide-40
SLIDE 40

Rules for recursion

There are three key requirements for a recursive function to work correctly.

1 Base case: There must be a special case to handle the simplest

versions of the problem directly, without recursion.

◮ Does not call the function again. Neil Moore (UK CS) CS 115 Lecture 20 Fall 2015 7 / 12

slide-41
SLIDE 41

Rules for recursion

There are three key requirements for a recursive function to work correctly.

1 Base case: There must be a special case to handle the simplest

versions of the problem directly, without recursion.

◮ Does not call the function again. 2 Recursive case: There must be a case where the function does call

itself.

Neil Moore (UK CS) CS 115 Lecture 20 Fall 2015 7 / 12

slide-42
SLIDE 42

Rules for recursion

There are three key requirements for a recursive function to work correctly.

1 Base case: There must be a special case to handle the simplest

versions of the problem directly, without recursion.

◮ Does not call the function again. 2 Recursive case: There must be a case where the function does call

itself.

3 Simplification: The recursive call must be on a simpler version of the

problem.

Neil Moore (UK CS) CS 115 Lecture 20 Fall 2015 7 / 12

slide-43
SLIDE 43

Rules for recursion

There are three key requirements for a recursive function to work correctly.

1 Base case: There must be a special case to handle the simplest

versions of the problem directly, without recursion.

◮ Does not call the function again. 2 Recursive case: There must be a case where the function does call

itself.

3 Simplification: The recursive call must be on a simpler version of the

  • problem. That is, it must reduce the size of the problem, bringing you

closer to the base case.

Neil Moore (UK CS) CS 115 Lecture 20 Fall 2015 7 / 12

slide-44
SLIDE 44

Rules for recursion

There are three key requirements for a recursive function to work correctly.

1 Base case: There must be a special case to handle the simplest

versions of the problem directly, without recursion.

◮ Does not call the function again. 2 Recursive case: There must be a case where the function does call

itself.

3 Simplification: The recursive call must be on a simpler version of the

  • problem. That is, it must reduce the size of the problem, bringing you

closer to the base case.

◮ That means the arguments must be changed from the parameters. ◮ If not, you have infinite recursion Neil Moore (UK CS) CS 115 Lecture 20 Fall 2015 7 / 12

slide-45
SLIDE 45

Rules for recursion

There are three key requirements for a recursive function to work correctly.

1 Base case: There must be a special case to handle the simplest

versions of the problem directly, without recursion.

◮ Does not call the function again. 2 Recursive case: There must be a case where the function does call

itself.

3 Simplification: The recursive call must be on a simpler version of the

  • problem. That is, it must reduce the size of the problem, bringing you

closer to the base case.

◮ That means the arguments must be changed from the parameters. ◮ If not, you have infinite recursion

And a few related guidelines: You should check for the base case first.

◮ . . . before making any recursive calls. Neil Moore (UK CS) CS 115 Lecture 20 Fall 2015 7 / 12

slide-46
SLIDE 46

Rules for recursion

There are three key requirements for a recursive function to work correctly.

1 Base case: There must be a special case to handle the simplest

versions of the problem directly, without recursion.

◮ Does not call the function again. 2 Recursive case: There must be a case where the function does call

itself.

3 Simplification: The recursive call must be on a simpler version of the

  • problem. That is, it must reduce the size of the problem, bringing you

closer to the base case.

◮ That means the arguments must be changed from the parameters. ◮ If not, you have infinite recursion

And a few related guidelines: You should check for the base case first.

◮ . . . before making any recursive calls.

The base case is usually, but not always, a problem of size 0 or 1.

Neil Moore (UK CS) CS 115 Lecture 20 Fall 2015 7 / 12

slide-47
SLIDE 47

Rules for recursion

There are three key requirements for a recursive function to work correctly.

1 Base case: There must be a special case to handle the simplest

versions of the problem directly, without recursion.

◮ Does not call the function again. 2 Recursive case: There must be a case where the function does call

itself.

3 Simplification: The recursive call must be on a simpler version of the

  • problem. That is, it must reduce the size of the problem, bringing you

closer to the base case.

◮ That means the arguments must be changed from the parameters. ◮ If not, you have infinite recursion

And a few related guidelines: You should check for the base case first.

◮ . . . before making any recursive calls.

The base case is usually, but not always, a problem of size 0 or 1.

Neil Moore (UK CS) CS 115 Lecture 20 Fall 2015 7 / 12

slide-48
SLIDE 48

About the rules

You can have multiple base cases, as long as there is at least one.

Neil Moore (UK CS) CS 115 Lecture 20 Fall 2015 8 / 12

slide-49
SLIDE 49

About the rules

You can have multiple base cases, as long as there is at least one. Sometimes the base case does nothing.

◮ You could write this using pass (“do nothing”) Neil Moore (UK CS) CS 115 Lecture 20 Fall 2015 8 / 12

slide-50
SLIDE 50

About the rules

You can have multiple base cases, as long as there is at least one. Sometimes the base case does nothing.

◮ You could write this using pass (“do nothing”) ◮ Or you could put the recursive case in an if. ⋆ (“If it’s not the base case, then do something.”) Neil Moore (UK CS) CS 115 Lecture 20 Fall 2015 8 / 12

slide-51
SLIDE 51

About the rules

You can have multiple base cases, as long as there is at least one. Sometimes the base case does nothing.

◮ You could write this using pass (“do nothing”) ◮ Or you could put the recursive case in an if. ⋆ (“If it’s not the base case, then do something.”)

If the function returns something, it should use the value of the recursive call.

Neil Moore (UK CS) CS 115 Lecture 20 Fall 2015 8 / 12

slide-52
SLIDE 52

About the rules

You can have multiple base cases, as long as there is at least one. Sometimes the base case does nothing.

◮ You could write this using pass (“do nothing”) ◮ Or you could put the recursive case in an if. ⋆ (“If it’s not the base case, then do something.”)

If the function returns something, it should use the value of the recursive call. The changes you make to the recursive arguments can be anything:

◮ Often subtraction, division, or shortening a list. Neil Moore (UK CS) CS 115 Lecture 20 Fall 2015 8 / 12

slide-53
SLIDE 53

About the rules

You can have multiple base cases, as long as there is at least one. Sometimes the base case does nothing.

◮ You could write this using pass (“do nothing”) ◮ Or you could put the recursive case in an if. ⋆ (“If it’s not the base case, then do something.”)

If the function returns something, it should use the value of the recursive call. The changes you make to the recursive arguments can be anything:

◮ Often subtraction, division, or shortening a list. ◮ But in some situations, addition or other operations. ◮ The important thing is that it gets closer to a base case. Neil Moore (UK CS) CS 115 Lecture 20 Fall 2015 8 / 12

slide-54
SLIDE 54

About the rules

You can have multiple base cases, as long as there is at least one. Sometimes the base case does nothing.

◮ You could write this using pass (“do nothing”) ◮ Or you could put the recursive case in an if. ⋆ (“If it’s not the base case, then do something.”)

If the function returns something, it should use the value of the recursive call. The changes you make to the recursive arguments can be anything:

◮ Often subtraction, division, or shortening a list. ◮ But in some situations, addition or other operations. ◮ The important thing is that it gets closer to a base case.

The order of recursive calls matters!

◮ What happens if we move the print triangle call after the print? Neil Moore (UK CS) CS 115 Lecture 20 Fall 2015 8 / 12

slide-55
SLIDE 55

About the rules

You can have multiple base cases, as long as there is at least one. Sometimes the base case does nothing.

◮ You could write this using pass (“do nothing”) ◮ Or you could put the recursive case in an if. ⋆ (“If it’s not the base case, then do something.”)

If the function returns something, it should use the value of the recursive call. The changes you make to the recursive arguments can be anything:

◮ Often subtraction, division, or shortening a list. ◮ But in some situations, addition or other operations. ◮ The important thing is that it gets closer to a base case.

The order of recursive calls matters!

◮ What happens if we move the print triangle call after the print? ◮ The triangle is upside-down! Neil Moore (UK CS) CS 115 Lecture 20 Fall 2015 8 / 12

slide-56
SLIDE 56

About the rules

You can have multiple base cases, as long as there is at least one. Sometimes the base case does nothing.

◮ You could write this using pass (“do nothing”) ◮ Or you could put the recursive case in an if. ⋆ (“If it’s not the base case, then do something.”)

If the function returns something, it should use the value of the recursive call. The changes you make to the recursive arguments can be anything:

◮ Often subtraction, division, or shortening a list. ◮ But in some situations, addition or other operations. ◮ The important thing is that it gets closer to a base case.

The order of recursive calls matters!

◮ What happens if we move the print triangle call after the print? ◮ The triangle is upside-down! Neil Moore (UK CS) CS 115 Lecture 20 Fall 2015 8 / 12

slide-57
SLIDE 57

Infinite recursion

What happens if you break one of the rules?

Neil Moore (UK CS) CS 115 Lecture 20 Fall 2015 9 / 12

slide-58
SLIDE 58

Infinite recursion

What happens if you break one of the rules? You may get an infinite recursion. Meaning the function just keeps calling itself “forever”.

Neil Moore (UK CS) CS 115 Lecture 20 Fall 2015 9 / 12

slide-59
SLIDE 59

Infinite recursion

What happens if you break one of the rules? You may get an infinite recursion. Meaning the function just keeps calling itself “forever”. Even worse than an infinite loop!

◮ Every recursive call uses a little bit of memory: ⋆ Parameters, return address. . . ◮ Where are these stored? Neil Moore (UK CS) CS 115 Lecture 20 Fall 2015 9 / 12

slide-60
SLIDE 60

Infinite recursion

What happens if you break one of the rules? You may get an infinite recursion. Meaning the function just keeps calling itself “forever”. Even worse than an infinite loop!

◮ Every recursive call uses a little bit of memory: ⋆ Parameters, return address. . . ◮ Where are these stored? The call stack! Neil Moore (UK CS) CS 115 Lecture 20 Fall 2015 9 / 12

slide-61
SLIDE 61

Infinite recursion

What happens if you break one of the rules? You may get an infinite recursion. Meaning the function just keeps calling itself “forever”. Even worse than an infinite loop!

◮ Every recursive call uses a little bit of memory: ⋆ Parameters, return address. . . ◮ Where are these stored? The call stack! ◮ So eventually an infinite recursion will run out of memory. ⋆ At least crashing your program. Neil Moore (UK CS) CS 115 Lecture 20 Fall 2015 9 / 12

slide-62
SLIDE 62

Infinite recursion

What happens if you break one of the rules? You may get an infinite recursion. Meaning the function just keeps calling itself “forever”. Even worse than an infinite loop!

◮ Every recursive call uses a little bit of memory: ⋆ Parameters, return address. . . ◮ Where are these stored? The call stack! ◮ So eventually an infinite recursion will run out of memory. ⋆ At least crashing your program. ⋆ And possibly the whole operating system! Neil Moore (UK CS) CS 115 Lecture 20 Fall 2015 9 / 12

slide-63
SLIDE 63

Infinite recursion

What happens if you break one of the rules? You may get an infinite recursion. Meaning the function just keeps calling itself “forever”. Even worse than an infinite loop!

◮ Every recursive call uses a little bit of memory: ⋆ Parameters, return address. . . ◮ Where are these stored? The call stack! ◮ So eventually an infinite recursion will run out of memory. ⋆ At least crashing your program. ⋆ And possibly the whole operating system!

Python has built-in checks to avoid crashing the OS with recursion.

◮ When there is too much recursion, it raises an exception:

RuntimeError("Maximum recursion depth exceeded...")

◮ So the program crashes before the OS does. Neil Moore (UK CS) CS 115 Lecture 20 Fall 2015 9 / 12

slide-64
SLIDE 64

Infinite recursion

What happens if you break one of the rules? You may get an infinite recursion. Meaning the function just keeps calling itself “forever”. Even worse than an infinite loop!

◮ Every recursive call uses a little bit of memory: ⋆ Parameters, return address. . . ◮ Where are these stored? The call stack! ◮ So eventually an infinite recursion will run out of memory. ⋆ At least crashing your program. ⋆ And possibly the whole operating system!

Python has built-in checks to avoid crashing the OS with recursion.

◮ When there is too much recursion, it raises an exception:

RuntimeError("Maximum recursion depth exceeded...")

◮ So the program crashes before the OS does. ◮ You can change the limit with sys.setrecursionlimit(1000) ⋆ But then you risk crashing more than just your program! Neil Moore (UK CS) CS 115 Lecture 20 Fall 2015 9 / 12

slide-65
SLIDE 65

Infinite recursion

What happens if you break one of the rules? You may get an infinite recursion. Meaning the function just keeps calling itself “forever”. Even worse than an infinite loop!

◮ Every recursive call uses a little bit of memory: ⋆ Parameters, return address. . . ◮ Where are these stored? The call stack! ◮ So eventually an infinite recursion will run out of memory. ⋆ At least crashing your program. ⋆ And possibly the whole operating system!

Python has built-in checks to avoid crashing the OS with recursion.

◮ When there is too much recursion, it raises an exception:

RuntimeError("Maximum recursion depth exceeded...")

◮ So the program crashes before the OS does. ◮ You can change the limit with sys.setrecursionlimit(1000) ⋆ But then you risk crashing more than just your program! Neil Moore (UK CS) CS 115 Lecture 20 Fall 2015 9 / 12

slide-66
SLIDE 66

Recursive definitions

When solving a problem recursively, it helps to write out the definition of the problem recursively.

Neil Moore (UK CS) CS 115 Lecture 20 Fall 2015 10 / 12

slide-67
SLIDE 67

Recursive definitions

When solving a problem recursively, it helps to write out the definition of the problem recursively. This is usually the hard part.

Neil Moore (UK CS) CS 115 Lecture 20 Fall 2015 10 / 12

slide-68
SLIDE 68

Recursive definitions

When solving a problem recursively, it helps to write out the definition of the problem recursively. This is usually the hard part. Consider the Fibonacci sequence: Fib(0) = 1, Fib(1) = 1, Fib(2) = 2, 3, 5, 8, 13, 21, 34, . . . What’s the pattern?

Neil Moore (UK CS) CS 115 Lecture 20 Fall 2015 10 / 12

slide-69
SLIDE 69

Recursive definitions

When solving a problem recursively, it helps to write out the definition of the problem recursively. This is usually the hard part. Consider the Fibonacci sequence: Fib(0) = 1, Fib(1) = 1, Fib(2) = 2, 3, 5, 8, 13, 21, 34, . . . What’s the pattern? Recursive case: Fib(n) = Fib(n − 1) + Fib(n − 2)

Neil Moore (UK CS) CS 115 Lecture 20 Fall 2015 10 / 12

slide-70
SLIDE 70

Recursive definitions

When solving a problem recursively, it helps to write out the definition of the problem recursively. This is usually the hard part. Consider the Fibonacci sequence: Fib(0) = 1, Fib(1) = 1, Fib(2) = 2, 3, 5, 8, 13, 21, 34, . . . What’s the pattern? Recursive case: Fib(n) = Fib(n − 1) + Fib(n − 2)

◮ When does this definition work? Neil Moore (UK CS) CS 115 Lecture 20 Fall 2015 10 / 12

slide-71
SLIDE 71

Recursive definitions

When solving a problem recursively, it helps to write out the definition of the problem recursively. This is usually the hard part. Consider the Fibonacci sequence: Fib(0) = 1, Fib(1) = 1, Fib(2) = 2, 3, 5, 8, 13, 21, 34, . . . What’s the pattern? Recursive case: Fib(n) = Fib(n − 1) + Fib(n − 2)

◮ When does this definition work? When n ≥ 2. Neil Moore (UK CS) CS 115 Lecture 20 Fall 2015 10 / 12

slide-72
SLIDE 72

Recursive definitions

When solving a problem recursively, it helps to write out the definition of the problem recursively. This is usually the hard part. Consider the Fibonacci sequence: Fib(0) = 1, Fib(1) = 1, Fib(2) = 2, 3, 5, 8, 13, 21, 34, . . . What’s the pattern? Recursive case: Fib(n) = Fib(n − 1) + Fib(n − 2)

◮ When does this definition work? When n ≥ 2.

Base case:

Neil Moore (UK CS) CS 115 Lecture 20 Fall 2015 10 / 12

slide-73
SLIDE 73

Recursive definitions

When solving a problem recursively, it helps to write out the definition of the problem recursively. This is usually the hard part. Consider the Fibonacci sequence: Fib(0) = 1, Fib(1) = 1, Fib(2) = 2, 3, 5, 8, 13, 21, 34, . . . What’s the pattern? Recursive case: Fib(n) = Fib(n − 1) + Fib(n − 2)

◮ When does this definition work? When n ≥ 2.

Base case: actually, there are two!

Neil Moore (UK CS) CS 115 Lecture 20 Fall 2015 10 / 12

slide-74
SLIDE 74

Recursive definitions

When solving a problem recursively, it helps to write out the definition of the problem recursively. This is usually the hard part. Consider the Fibonacci sequence: Fib(0) = 1, Fib(1) = 1, Fib(2) = 2, 3, 5, 8, 13, 21, 34, . . . What’s the pattern? Recursive case: Fib(n) = Fib(n − 1) + Fib(n − 2)

◮ When does this definition work? When n ≥ 2.

Base case: actually, there are two!

◮ Fib(0) = 1 ◮ Fib(1) = 1 Neil Moore (UK CS) CS 115 Lecture 20 Fall 2015 10 / 12

slide-75
SLIDE 75

Recursive definitions

When solving a problem recursively, it helps to write out the definition of the problem recursively. This is usually the hard part. Consider the Fibonacci sequence: Fib(0) = 1, Fib(1) = 1, Fib(2) = 2, 3, 5, 8, 13, 21, 34, . . . What’s the pattern? Recursive case: Fib(n) = Fib(n − 1) + Fib(n − 2)

◮ When does this definition work? When n ≥ 2.

Base case: actually, there are two!

◮ Fib(0) = 1 ◮ Fib(1) = 1

Each recursive call brings us closer to the base cases.

◮ As long as n isn’t negative, anyway. Neil Moore (UK CS) CS 115 Lecture 20 Fall 2015 10 / 12

slide-76
SLIDE 76

Recursive definitions

When solving a problem recursively, it helps to write out the definition of the problem recursively. This is usually the hard part. Consider the Fibonacci sequence: Fib(0) = 1, Fib(1) = 1, Fib(2) = 2, 3, 5, 8, 13, 21, 34, . . . What’s the pattern? Recursive case: Fib(n) = Fib(n − 1) + Fib(n − 2)

◮ When does this definition work? When n ≥ 2.

Base case: actually, there are two!

◮ Fib(0) = 1 ◮ Fib(1) = 1

Each recursive call brings us closer to the base cases.

◮ As long as n isn’t negative, anyway.

Now that we have a recursive definition, writing the code is easy.

Neil Moore (UK CS) CS 115 Lecture 20 Fall 2015 10 / 12

slide-77
SLIDE 77

Recursive definitions

When solving a problem recursively, it helps to write out the definition of the problem recursively. This is usually the hard part. Consider the Fibonacci sequence: Fib(0) = 1, Fib(1) = 1, Fib(2) = 2, 3, 5, 8, 13, 21, 34, . . . What’s the pattern? Recursive case: Fib(n) = Fib(n − 1) + Fib(n − 2)

◮ When does this definition work? When n ≥ 2.

Base case: actually, there are two!

◮ Fib(0) = 1 ◮ Fib(1) = 1

Each recursive call brings us closer to the base cases.

◮ As long as n isn’t negative, anyway.

Now that we have a recursive definition, writing the code is easy.

Neil Moore (UK CS) CS 115 Lecture 20 Fall 2015 10 / 12

slide-78
SLIDE 78

The Fibonacci sequence in code

Fib(0) = 1 Fib(1) = 1 Fib(n) = Fib(n − 1) + Fib(n − 2) where n > 1

Neil Moore (UK CS) CS 115 Lecture 20 Fall 2015 11 / 12

slide-79
SLIDE 79

The Fibonacci sequence in code

Fib(0) = 1 Fib(1) = 1 Fib(n) = Fib(n − 1) + Fib(n − 2) where n > 1

def fibonacci(n): # Base cases. if n == 0 or n == 1: result = 1

Neil Moore (UK CS) CS 115 Lecture 20 Fall 2015 11 / 12

slide-80
SLIDE 80

The Fibonacci sequence in code

Fib(0) = 1 Fib(1) = 1 Fib(n) = Fib(n − 1) + Fib(n − 2) where n > 1

def fibonacci(n): # Base cases. if n == 0 or n == 1: result = 1 else: # Recursive case. result = fibonacci(n - 1) + fibonacci(n - 2) return result

Neil Moore (UK CS) CS 115 Lecture 20 Fall 2015 11 / 12

slide-81
SLIDE 81

The Fibonacci sequence in code

Fib(0) = 1 Fib(1) = 1 Fib(n) = Fib(n − 1) + Fib(n − 2) where n > 1

def fibonacci(n): # Base cases. if n == 0 or n == 1: result = 1 else: # Recursive case. result = fibonacci(n - 1) + fibonacci(n - 2) return result

Neil Moore (UK CS) CS 115 Lecture 20 Fall 2015 11 / 12

slide-82
SLIDE 82

Recursion and the call stack

Every recursive call adds a new entry to the call stack.

◮ When that recursive call returns, the entry is removed. Neil Moore (UK CS) CS 115 Lecture 20 Fall 2015 12 / 12

slide-83
SLIDE 83

Recursion and the call stack

Every recursive call adds a new entry to the call stack.

◮ When that recursive call returns, the entry is removed.

So you’ll have the same function on the call stack many times.

Neil Moore (UK CS) CS 115 Lecture 20 Fall 2015 12 / 12

slide-84
SLIDE 84

Recursion and the call stack

Every recursive call adds a new entry to the call stack.

◮ When that recursive call returns, the entry is removed.

So you’ll have the same function on the call stack many times.

◮ Each instance of the function has its own parameters, local variables,

and return value.

Neil Moore (UK CS) CS 115 Lecture 20 Fall 2015 12 / 12

slide-85
SLIDE 85

Recursion and the call stack

Every recursive call adds a new entry to the call stack.

◮ When that recursive call returns, the entry is removed.

So you’ll have the same function on the call stack many times.

◮ Each instance of the function has its own parameters, local variables,

and return value.

◮ Variables are local to one call to the function.

Let’s observe the call stack in a recursive program using the debugger.

Neil Moore (UK CS) CS 115 Lecture 20 Fall 2015 12 / 12

slide-86
SLIDE 86

Recursion and the call stack

Every recursive call adds a new entry to the call stack.

◮ When that recursive call returns, the entry is removed.

So you’ll have the same function on the call stack many times.

◮ Each instance of the function has its own parameters, local variables,

and return value.

◮ Variables are local to one call to the function.

Let’s observe the call stack in a recursive program using the debugger.

Neil Moore (UK CS) CS 115 Lecture 20 Fall 2015 12 / 12