32
Chapter 6 Supplement: Recursion with Python Mr. Dave Clausen La Cañada High School

Chapter 6 Supplement: Recursion with Python Mr. Dave Clausen La Cañada High School

Embed Size (px)

Citation preview

Page 1: Chapter 6 Supplement: Recursion with Python Mr. Dave Clausen La Cañada High School

Chapter 6 Supplement: Recursion

with PythonMr. Dave Clausen

La Cañada High School

Page 2: Chapter 6 Supplement: Recursion with Python Mr. Dave Clausen La Cañada High School

2Mr. Dave Clausen

Recursion: DefinitionsRecursion

The process of a subprogram (function) calling itself.A clearly defined stopping state must exist.

The well defined termination of the recursive process.

Any recursive subprogram could be rewritten using iteration (for, while)

Recursive stepA step in the recursive process that solves a similar

problem of a smaller size and will eventually lead to a termination of the process.

Page 3: Chapter 6 Supplement: Recursion with Python Mr. Dave Clausen La Cañada High School

3Mr. Dave Clausen

Recursive Problems in MathematicsFactorials

The definition of factorials is as follows:0! = 1; 1! = 1; for n>1, n! = n*(n-1)*(n-2)*…2*1To find n! you can calculate n*(n-1)!Each step solves a similar problem of smaller size.

Fibonacci sequenceThe first term is 1, the second term is also 1, and

every successive term is the sum of the previous two terms.

An Arithmetic Series using sigma notation1

n

x

x

Page 4: Chapter 6 Supplement: Recursion with Python Mr. Dave Clausen La Cañada High School

4Mr. Dave Clausen

Recursive Example 1First recursion example (Do not compile or run, there is no stopping state)

recursion1.py

def Example (value):print(“Hello, value = ” + str(value))

Example (value + 1)

Page 5: Chapter 6 Supplement: Recursion with Python Mr. Dave Clausen La Cañada High School

5Mr. Dave Clausen

Recursive Example 2Second Example

recursion2.py

def Example2 (value):print(“Hello, value = ” + str(value))if (value < 5): Example2 (value + 1)

Page 6: Chapter 6 Supplement: Recursion with Python Mr. Dave Clausen La Cañada High School

6Mr. Dave Clausen

Example 2 Animation: 1

1

Value

TRUE

Value < 5

Hello, value = 1

Monitor

print("Hello value = ", str(value)) if(value < 5): Example2(value+1)

Page 7: Chapter 6 Supplement: Recursion with Python Mr. Dave Clausen La Cañada High School

7Mr. Dave Clausen

Example 2 Animation: 2

2

Value

TRUE

Value < 5

Hello, value = 1

Hello, value = 2

Monitor

print("Hello value = ", str(value)) if(value < 5): Example2(value+1)

print("Hello value = ", str(value)) if(value < 5): Example2(value+1)

Page 8: Chapter 6 Supplement: Recursion with Python Mr. Dave Clausen La Cañada High School

8Mr. Dave Clausen

Example 2 Animation: 3

3

Value

TRUE

Value < 5

Hello, value = 1

Hello, value = 2

Hello, value = 3

Monitor

print("Hello value = ", str(value)) if(value < 5): Example2(value+1)

print("Hello value = ", str(value)) if(value < 5): Example2(value+1)

print("Hello value = ", str(value)) if(value < 5): Example2(value+1)

Page 9: Chapter 6 Supplement: Recursion with Python Mr. Dave Clausen La Cañada High School

9Mr. Dave Clausen

Example 2 Animation: 4

4

Value

TRUE

Value < 5

Hello, value = 1

Hello, value = 2

Hello, value = 3

Hello, value = 4

Monitor

print("Hello value = ", str(value)) if(value < 5): Example2(value+1)

print("Hello value = ", str(value)) if(value < 5): Example2(value+1)

print("Hello value = ", str(value)) if(value < 5): Example2(value+1)

print("Hello value = ", str(value)) if(value < 5): Example2(value+1)

Page 10: Chapter 6 Supplement: Recursion with Python Mr. Dave Clausen La Cañada High School

10Mr. Dave Clausen

Example 2 Animation: 5

5

Value

FALSE

Value < 5

Hello, value = 1

Hello, value = 2

Hello, value = 3

Hello, value = 4

Hello, value = 5

Monitor

print("Hello value = ", str(value)) if(value < 5): Example2(value+1)

print("Hello value = ", str(value)) if(value < 5): Example2(value+1)

print("Hello value = ", str(value)) if(value < 5): Example2(value+1)

print("Hello value = ", str(value)) if(value < 5): Example2(value+1)

print("Hello value = ", str(value)) if(value < 5): Example2(value+1)

Page 11: Chapter 6 Supplement: Recursion with Python Mr. Dave Clausen La Cañada High School

11Mr. Dave Clausen

Example 2 Animation: 6

5

Value

FALSE

Value < 5

Hello, value = 1

Hello, value = 2

Hello, value = 3

Hello, value = 4

Hello, value = 5

Monitor

Page 12: Chapter 6 Supplement: Recursion with Python Mr. Dave Clausen La Cañada High School

12Mr. Dave Clausen

Tail-recursive Algorithms

Tail-recursive algorithms perform no work after the recursive call.Examples 1 and 2 are tail recursive.

Page 13: Chapter 6 Supplement: Recursion with Python Mr. Dave Clausen La Cañada High School

13Mr. Dave Clausen

Recursive Example 3Third Example (not tail recursive)

recursion3.py

def Example3 (value):

print(“Hello, value = ” + str(value))

if (value < 5):

Example3 (value + 1)

print(“Goodbye, value = ” + str(value))

Page 14: Chapter 6 Supplement: Recursion with Python Mr. Dave Clausen La Cañada High School

14Mr. Dave Clausen

Example 3

1Value

Hello, value = 1Monitor

print("Hello value = ", str(value))

if(value < 5):

Example3(value+1)

print("Goodbye value = ", str(value))

Page 15: Chapter 6 Supplement: Recursion with Python Mr. Dave Clausen La Cañada High School

15Mr. Dave Clausen

Example 3

2Value

Hello, value = 1

Hello, value = 2

Monitor

print("Hello value = ", str(value))

if(value < 5):

Example3(value+1)

print("Goodbye value = ", str(value))

print("Hello value = ", str(value))

if(value < 5):

Example3(value+1)

print("Goodbye value = ", str(value))

Page 16: Chapter 6 Supplement: Recursion with Python Mr. Dave Clausen La Cañada High School

16Mr. Dave Clausen

Example 3

3Value

Hello, value = 1

Hello, value = 2

Hello, value = 3

Monitor

print("Hello value = ", str(value))

if(value < 5):

Example3(value+1)

print("Goodbye value = ", str(value))

print("Hello value = ", str(value))

if(value < 5):

Example3(value+1)

print("Goodbye value = ", str(value))

print("Hello value = ", str(value))

if(value < 5):

Example3(value+1)

print("Goodbye value = ", str(value))

Page 17: Chapter 6 Supplement: Recursion with Python Mr. Dave Clausen La Cañada High School

17Mr. Dave Clausen

Example 3

4Value

Hello, value = 1

Hello, value = 2

Hello, value = 3

Hello, value = 4

Monitor

print("Hello value = ", str(value))

if(value < 5):

Example3(value+1)

print("Goodbye value = ", str(value))

print("Hello value = ", str(value))

if(value < 5):

Example3(value+1)

print("Goodbye value = ", str(value))

print("Hello value = ", str(value))

if(value < 5):

Example3(value+1)

print("Goodbye value = ", str(value))

print("Hello value = ", str(value))

if(value < 5):

Example3(value+1)

print("Goodbye value = ", str(value))

Page 18: Chapter 6 Supplement: Recursion with Python Mr. Dave Clausen La Cañada High School

18Mr. Dave Clausen

Example 3

5Value

Hello, value = 1

Hello, value = 2

Hello, value = 3

Hello, value = 4

Hello, value = 5

Monitor

print("Hello value = ", str(value))

if(value < 5):

Example3(value+1)

print("Goodbye value = ", str(value))

print("Hello value = ", str(value))

if(value < 5):

Example3(value+1)

print("Goodbye value = ", str(value))

print("Hello value = ", str(value))

if(value < 5):

Example3(value+1)

print("Goodbye value = ", str(value))

print("Hello value = ", str(value))

if(value < 5):

Example3(value+1)

print("Goodbye value = ", str(value))

print("Hello value = ", str(value))

if(value < 5):

Example3(value+1)

print("Goodbye value = ", str(value))

Page 19: Chapter 6 Supplement: Recursion with Python Mr. Dave Clausen La Cañada High School

19Mr. Dave Clausen

Example 3

5Value

Hello, value = 1

Hello, value = 2

Hello, value = 3

Hello, value = 4

Hello, value = 5

Goodbye, value = 5

Monitor

print("Hello value = ", str(value))

if(value < 5):

Example3(value+1)

print("Goodbye value = ", str(value))

print("Hello value = ", str(value))

if(value < 5):

Example3(value+1)

print("Goodbye value = ", str(value))

print("Hello value = ", str(value))

if(value < 5):

Example3(value+1)

print("Goodbye value = ", str(value))

print("Goodbye value = ", str(value))

print("Hello value = ", str(value))

if(value < 5):

Example3(value+1)

print("Goodbye value = ", str(value))

Page 20: Chapter 6 Supplement: Recursion with Python Mr. Dave Clausen La Cañada High School

20Mr. Dave Clausen

Example 3

4Value

Hello, value = 1

Hello, value = 2

Hello, value = 3

Hello, value = 4

Hello, value = 5

Goodbye, value = 5

Goodbye, value = 4

Monitor

print("Hello value = ", str(value))

if(value < 5):

Example3(value+1)

print("Goodbye value = ", str(value))

print("Hello value = ", str(value))

if(value < 5):

Example3(value+1)

print("Goodbye value = ", str(value))

print("Goodbye value = ", str(value))

print("Hello value = ", str(value))

if(value < 5):

Example3(value+1)

print("Goodbye value = ", str(value))

Page 21: Chapter 6 Supplement: Recursion with Python Mr. Dave Clausen La Cañada High School

21Mr. Dave Clausen

Example 3

3Value

Hello, value = 1

Hello, value = 2

Hello, value = 3

Hello, value = 4

Hello, value = 5

Goodbye, value = 5

Goodbye, value = 4

Goodbye, value = 3

Monitor

print("Hello value = ", str(value))

if(value < 5):

Example3(value+1)

print("Goodbye value = ", str(value))

print("Goodbye value = ", str(value))

print("Hello value = ", str(value))

if(value < 5):

Example3(value+1)

print("Goodbye value = ", str(value))

Page 22: Chapter 6 Supplement: Recursion with Python Mr. Dave Clausen La Cañada High School

22Mr. Dave Clausen

Example 3

2Value

Hello, value = 1

Hello, value = 2

Hello, value = 3

Hello, value = 4

Hello, value = 5

Goodbye, value = 5

Goodbye, value = 4

Goodbye, value = 3

Goodbye, value = 2

Monitor

print("Goodbye value = ", str(value))

print("Hello value = ", str(value))

if(value < 5):

Example3(value+1)

print("Goodbye value = ", str(value))

Page 23: Chapter 6 Supplement: Recursion with Python Mr. Dave Clausen La Cañada High School

23Mr. Dave Clausen

Example 3

1Value

Hello, value = 1

Hello, value = 2

Hello, value = 3

Hello, value = 4

Hello, value = 5

Goodbye, value = 5

Goodbye, value = 4

Goodbye, value = 3

Goodbye, value = 2

Goodbye, value = 1

Monitor

print("Goodbye value = ", str(value))

Page 24: Chapter 6 Supplement: Recursion with Python Mr. Dave Clausen La Cañada High School

24Mr. Dave Clausen

Example 3

1Value

Hello, value = 1

Hello, value = 2

Hello, value = 3

Hello, value = 4

Hello, value = 5

Goodbye, value = 5

Goodbye, value = 4

Goodbye, value = 3

Goodbye, value = 2

Goodbye, value = 1

Monitor

Page 25: Chapter 6 Supplement: Recursion with Python Mr. Dave Clausen La Cañada High School

25Mr. Dave Clausen

Recursive Example 4Arithmetic Series (sigma) Example

RecursiveSum.py

int sigma(int n){

if(n <= 1)return n;

elsereturn n + sigma(n-1);

}

n

x

x1

Page 26: Chapter 6 Supplement: Recursion with Python Mr. Dave Clausen La Cañada High School

26Mr. Dave Clausen

Sigma Example Function CallsIf we call the sigma function with the

statement, sum=sigma(5)the else portion of the first function calls the

function again, return 5 + sigma(4);at this point the value of sigma (4) must be

computedthis calls return 4 + sigma (3)which calls return 3 + sigma(2)then return 2 + sigma(1)finally this returns the value of 1.

Page 27: Chapter 6 Supplement: Recursion with Python Mr. Dave Clausen La Cañada High School

27Mr. Dave Clausen

Visualizing the sigma Function Calls

return 5 + sigma(4)return 4 + sigma(3)

return 3 + sigma(2)return 2 + sigma(1)

return 1at the end of the recursive calls the steps are reversed for

assigning the values.return 1

return(2+1) (=3)

return(3+3) (=6)

return(4+6) (=10)

return(5+10) (=15)

Page 28: Chapter 6 Supplement: Recursion with Python Mr. Dave Clausen La Cañada High School

28Mr. Dave Clausen

StacksImagine a stack as a pile of trays in

a cafeteria. The last tray put on the stack is the first one taken off of the stack.This is what occurs in memory when

a subprogram calls itself.A stack is a dynamic data structure

where access can be made only from one end. This is called a Last In First Out (LIFO) structure.

Page 29: Chapter 6 Supplement: Recursion with Python Mr. Dave Clausen La Cañada High School

29Mr. Dave Clausen

Stack AnimationLevel 5 n: 1 Sigma:

Level 4: n: 2 Sigma:

Level 3: n: 3 Sigma:

Level 2: n: 4 Sigma:

Level 1: n: 5 Sigma:

1

3

6

10

15

Page 30: Chapter 6 Supplement: Recursion with Python Mr. Dave Clausen La Cañada High School

30Mr. Dave Clausen

The Costs and Benefits of RecursionCosts

A recursive process that requires N recursive calls uses N+1 units of stack memory and processor time to manage the process.

An equivalent iterative process always uses one stack of memory and processor time to manage the method call, therefore, recursion requires more memory and processor power than the non-recursive process.

BenefitsShort, simple & elegant solutions using divide &

conquer algorithms (solve the problem by repeatedly dividing it into simpler problems.)

Page 31: Chapter 6 Supplement: Recursion with Python Mr. Dave Clausen La Cañada High School

31Mr. Dave Clausen

Some Recursive Algorithms

The Binary Search

The Quick Sort

The Merge Sort

Page 32: Chapter 6 Supplement: Recursion with Python Mr. Dave Clausen La Cañada High School

32Mr. Dave Clausen

M. C. Escher & Recursion

M.C. Escher “Drawing Hands” lithographIllustrates the concept of Recursion