117
Tree Recursion

Tree Recursioncs61a/fa20/assets/slides/... · 2021. 1. 6. · 3 * factorial(2) 2 * factorial(1) 1 * factorial(0) def factorial(n): if n == 0: return 1 else: return n * factorial(n-1)

  • Upload
    others

  • View
    17

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Tree Recursioncs61a/fa20/assets/slides/... · 2021. 1. 6. · 3 * factorial(2) 2 * factorial(1) 1 * factorial(0) def factorial(n): if n == 0: return 1 else: return n * factorial(n-1)

Tree Recursion

Page 2: Tree Recursioncs61a/fa20/assets/slides/... · 2021. 1. 6. · 3 * factorial(2) 2 * factorial(1) 1 * factorial(0) def factorial(n): if n == 0: return 1 else: return n * factorial(n-1)

Announcements

Page 3: Tree Recursioncs61a/fa20/assets/slides/... · 2021. 1. 6. · 3 * factorial(2) 2 * factorial(1) 1 * factorial(0) def factorial(n): if n == 0: return 1 else: return n * factorial(n-1)

Recursive Factorial

Page 4: Tree Recursioncs61a/fa20/assets/slides/... · 2021. 1. 6. · 3 * factorial(2) 2 * factorial(1) 1 * factorial(0) def factorial(n): if n == 0: return 1 else: return n * factorial(n-1)

if n == 0 n! = 1

if n > 0 n! = n x (n-1) x (n-2) x ... x1

factorial (!)

Page 5: Tree Recursioncs61a/fa20/assets/slides/... · 2021. 1. 6. · 3 * factorial(2) 2 * factorial(1) 1 * factorial(0) def factorial(n): if n == 0: return 1 else: return n * factorial(n-1)

def factorial(n): fact = 1 i = 1 while i <= n: fact *= i i += 1 return fact

factorial(5)

1 = 1*12 = 2*1!6 = 3*2!24 = 4*3!120 = 5*4!

Page 6: Tree Recursioncs61a/fa20/assets/slides/... · 2021. 1. 6. · 3 * factorial(2) 2 * factorial(1) 1 * factorial(0) def factorial(n): if n == 0: return 1 else: return n * factorial(n-1)

if n == 0 n! = 1

if n > 0 n! = n x (n-1)!

base case

recursive case

factorial (!)

Page 7: Tree Recursioncs61a/fa20/assets/slides/... · 2021. 1. 6. · 3 * factorial(2) 2 * factorial(1) 1 * factorial(0) def factorial(n): if n == 0: return 1 else: return n * factorial(n-1)

3 * factorial(2) 2 * factorial(1) 1 * factorial(0)

def factorial(n): if n == 0: return 1 else: return n * factorial(n-1)

factorial(3)

Page 8: Tree Recursioncs61a/fa20/assets/slides/... · 2021. 1. 6. · 3 * factorial(2) 2 * factorial(1) 1 * factorial(0) def factorial(n): if n == 0: return 1 else: return n * factorial(n-1)

Order of Recursive Calls

Page 9: Tree Recursioncs61a/fa20/assets/slides/... · 2021. 1. 6. · 3 * factorial(2) 2 * factorial(1) 1 * factorial(0) def factorial(n): if n == 0: return 1 else: return n * factorial(n-1)

The Cascade Function

(Demo)

9http://pythontutor.com/composingprograms.html#code=def%20cascade%28n%29%3A%20%20%20%20%0A%20%20%20%20if%20n%20%3C%2010%3A%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20print%28n%29%20%20%20%20%0A%20%20%20%20else%3A%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20print%28n%29%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20cascade%28n//10%29%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20print%28n%29%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20%0Acascade%28123%29&cumulative=true&curInstr=0&mode=display&origin=composingprograms.js&py=3&rawInputLstJSON=%5B%5D

Page 10: Tree Recursioncs61a/fa20/assets/slides/... · 2021. 1. 6. · 3 * factorial(2) 2 * factorial(1) 1 * factorial(0) def factorial(n): if n == 0: return 1 else: return n * factorial(n-1)

The Cascade Function

(Demo)

9http://pythontutor.com/composingprograms.html#code=def%20cascade%28n%29%3A%20%20%20%20%0A%20%20%20%20if%20n%20%3C%2010%3A%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20print%28n%29%20%20%20%20%0A%20%20%20%20else%3A%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20print%28n%29%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20cascade%28n//10%29%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20print%28n%29%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20%0Acascade%28123%29&cumulative=true&curInstr=0&mode=display&origin=composingprograms.js&py=3&rawInputLstJSON=%5B%5D

Page 11: Tree Recursioncs61a/fa20/assets/slides/... · 2021. 1. 6. · 3 * factorial(2) 2 * factorial(1) 1 * factorial(0) def factorial(n): if n == 0: return 1 else: return n * factorial(n-1)

The Cascade Function

(Demo)

9http://pythontutor.com/composingprograms.html#code=def%20cascade%28n%29%3A%20%20%20%20%0A%20%20%20%20if%20n%20%3C%2010%3A%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20print%28n%29%20%20%20%20%0A%20%20%20%20else%3A%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20print%28n%29%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20cascade%28n//10%29%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20print%28n%29%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20%0Acascade%28123%29&cumulative=true&curInstr=0&mode=display&origin=composingprograms.js&py=3&rawInputLstJSON=%5B%5D

Page 12: Tree Recursioncs61a/fa20/assets/slides/... · 2021. 1. 6. · 3 * factorial(2) 2 * factorial(1) 1 * factorial(0) def factorial(n): if n == 0: return 1 else: return n * factorial(n-1)

The Cascade Function

• Each cascade frame is from a different call to cascade.

(Demo)

9http://pythontutor.com/composingprograms.html#code=def%20cascade%28n%29%3A%20%20%20%20%0A%20%20%20%20if%20n%20%3C%2010%3A%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20print%28n%29%20%20%20%20%0A%20%20%20%20else%3A%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20print%28n%29%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20cascade%28n//10%29%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20print%28n%29%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20%0Acascade%28123%29&cumulative=true&curInstr=0&mode=display&origin=composingprograms.js&py=3&rawInputLstJSON=%5B%5D

Page 13: Tree Recursioncs61a/fa20/assets/slides/... · 2021. 1. 6. · 3 * factorial(2) 2 * factorial(1) 1 * factorial(0) def factorial(n): if n == 0: return 1 else: return n * factorial(n-1)

The Cascade Function

• Each cascade frame is from a different call to cascade.

• Until the Return value appears, that call has not completed.

(Demo)

9http://pythontutor.com/composingprograms.html#code=def%20cascade%28n%29%3A%20%20%20%20%0A%20%20%20%20if%20n%20%3C%2010%3A%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20print%28n%29%20%20%20%20%0A%20%20%20%20else%3A%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20print%28n%29%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20cascade%28n//10%29%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20print%28n%29%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20%0Acascade%28123%29&cumulative=true&curInstr=0&mode=display&origin=composingprograms.js&py=3&rawInputLstJSON=%5B%5D

Page 14: Tree Recursioncs61a/fa20/assets/slides/... · 2021. 1. 6. · 3 * factorial(2) 2 * factorial(1) 1 * factorial(0) def factorial(n): if n == 0: return 1 else: return n * factorial(n-1)

The Cascade Function

• Each cascade frame is from a different call to cascade.

• Until the Return value appears, that call has not completed.

• Any statement can appear before or after the recursive call.

(Demo)

9http://pythontutor.com/composingprograms.html#code=def%20cascade%28n%29%3A%20%20%20%20%0A%20%20%20%20if%20n%20%3C%2010%3A%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20print%28n%29%20%20%20%20%0A%20%20%20%20else%3A%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20print%28n%29%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20cascade%28n//10%29%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20print%28n%29%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20%0Acascade%28123%29&cumulative=true&curInstr=0&mode=display&origin=composingprograms.js&py=3&rawInputLstJSON=%5B%5D

Page 15: Tree Recursioncs61a/fa20/assets/slides/... · 2021. 1. 6. · 3 * factorial(2) 2 * factorial(1) 1 * factorial(0) def factorial(n): if n == 0: return 1 else: return n * factorial(n-1)

The Cascade Function

• Each cascade frame is from a different call to cascade.

• Until the Return value appears, that call has not completed.

• Any statement can appear before or after the recursive call.

(Demo)

9http://pythontutor.com/composingprograms.html#code=def%20cascade%28n%29%3A%20%20%20%20%0A%20%20%20%20if%20n%20%3C%2010%3A%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20print%28n%29%20%20%20%20%0A%20%20%20%20else%3A%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20print%28n%29%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20cascade%28n//10%29%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20print%28n%29%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20%0Acascade%28123%29&cumulative=true&curInstr=0&mode=display&origin=composingprograms.js&py=3&rawInputLstJSON=%5B%5D

Page 16: Tree Recursioncs61a/fa20/assets/slides/... · 2021. 1. 6. · 3 * factorial(2) 2 * factorial(1) 1 * factorial(0) def factorial(n): if n == 0: return 1 else: return n * factorial(n-1)

The Cascade Function

• Each cascade frame is from a different call to cascade.

• Until the Return value appears, that call has not completed.

• Any statement can appear before or after the recursive call.

(Demo)

9http://pythontutor.com/composingprograms.html#code=def%20cascade%28n%29%3A%20%20%20%20%0A%20%20%20%20if%20n%20%3C%2010%3A%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20print%28n%29%20%20%20%20%0A%20%20%20%20else%3A%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20print%28n%29%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20cascade%28n//10%29%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20print%28n%29%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20%0Acascade%28123%29&cumulative=true&curInstr=0&mode=display&origin=composingprograms.js&py=3&rawInputLstJSON=%5B%5D

Page 17: Tree Recursioncs61a/fa20/assets/slides/... · 2021. 1. 6. · 3 * factorial(2) 2 * factorial(1) 1 * factorial(0) def factorial(n): if n == 0: return 1 else: return n * factorial(n-1)

The Cascade Function

• Each cascade frame is from a different call to cascade.

• Until the Return value appears, that call has not completed.

• Any statement can appear before or after the recursive call.

(Demo)

9http://pythontutor.com/composingprograms.html#code=def%20cascade%28n%29%3A%20%20%20%20%0A%20%20%20%20if%20n%20%3C%2010%3A%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20print%28n%29%20%20%20%20%0A%20%20%20%20else%3A%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20print%28n%29%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20cascade%28n//10%29%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20print%28n%29%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20%0Acascade%28123%29&cumulative=true&curInstr=0&mode=display&origin=composingprograms.js&py=3&rawInputLstJSON=%5B%5D

Page 18: Tree Recursioncs61a/fa20/assets/slides/... · 2021. 1. 6. · 3 * factorial(2) 2 * factorial(1) 1 * factorial(0) def factorial(n): if n == 0: return 1 else: return n * factorial(n-1)

The Cascade Function

• Each cascade frame is from a different call to cascade.

• Until the Return value appears, that call has not completed.

• Any statement can appear before or after the recursive call.

(Demo)

9http://pythontutor.com/composingprograms.html#code=def%20cascade%28n%29%3A%20%20%20%20%0A%20%20%20%20if%20n%20%3C%2010%3A%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20print%28n%29%20%20%20%20%0A%20%20%20%20else%3A%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20print%28n%29%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20cascade%28n//10%29%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20print%28n%29%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20%0Acascade%28123%29&cumulative=true&curInstr=0&mode=display&origin=composingprograms.js&py=3&rawInputLstJSON=%5B%5D

Page 19: Tree Recursioncs61a/fa20/assets/slides/... · 2021. 1. 6. · 3 * factorial(2) 2 * factorial(1) 1 * factorial(0) def factorial(n): if n == 0: return 1 else: return n * factorial(n-1)

Two Definitions of Cascade

10

(Demo)

Page 20: Tree Recursioncs61a/fa20/assets/slides/... · 2021. 1. 6. · 3 * factorial(2) 2 * factorial(1) 1 * factorial(0) def factorial(n): if n == 0: return 1 else: return n * factorial(n-1)

Two Definitions of Cascade

10

def cascade(n): if n < 10: print(n) else: print(n) cascade(n//10) print(n)

def cascade(n): print(n) if n >= 10: cascade(n//10) print(n)

(Demo)

Page 21: Tree Recursioncs61a/fa20/assets/slides/... · 2021. 1. 6. · 3 * factorial(2) 2 * factorial(1) 1 * factorial(0) def factorial(n): if n == 0: return 1 else: return n * factorial(n-1)

Two Definitions of Cascade

10

def cascade(n): if n < 10: print(n) else: print(n) cascade(n//10) print(n)

def cascade(n): print(n) if n >= 10: cascade(n//10) print(n)

(Demo)

• If two implementations are equally clear, then shorter is usually better

Page 22: Tree Recursioncs61a/fa20/assets/slides/... · 2021. 1. 6. · 3 * factorial(2) 2 * factorial(1) 1 * factorial(0) def factorial(n): if n == 0: return 1 else: return n * factorial(n-1)

Two Definitions of Cascade

10

def cascade(n): if n < 10: print(n) else: print(n) cascade(n//10) print(n)

def cascade(n): print(n) if n >= 10: cascade(n//10) print(n)

(Demo)

• If two implementations are equally clear, then shorter is usually better

• In this case, the longer implementation is more clear (at least to me)

Page 23: Tree Recursioncs61a/fa20/assets/slides/... · 2021. 1. 6. · 3 * factorial(2) 2 * factorial(1) 1 * factorial(0) def factorial(n): if n == 0: return 1 else: return n * factorial(n-1)

Two Definitions of Cascade

10

def cascade(n): if n < 10: print(n) else: print(n) cascade(n//10) print(n)

def cascade(n): print(n) if n >= 10: cascade(n//10) print(n)

(Demo)

• If two implementations are equally clear, then shorter is usually better

• In this case, the longer implementation is more clear (at least to me)

• When learning to write recursive functions, put the base cases first

Page 24: Tree Recursioncs61a/fa20/assets/slides/... · 2021. 1. 6. · 3 * factorial(2) 2 * factorial(1) 1 * factorial(0) def factorial(n): if n == 0: return 1 else: return n * factorial(n-1)

Two Definitions of Cascade

10

def cascade(n): if n < 10: print(n) else: print(n) cascade(n//10) print(n)

def cascade(n): print(n) if n >= 10: cascade(n//10) print(n)

(Demo)

• If two implementations are equally clear, then shorter is usually better

• In this case, the longer implementation is more clear (at least to me)

• When learning to write recursive functions, put the base cases first

• Both are recursive functions, even though only the first has typical structure

Page 25: Tree Recursioncs61a/fa20/assets/slides/... · 2021. 1. 6. · 3 * factorial(2) 2 * factorial(1) 1 * factorial(0) def factorial(n): if n == 0: return 1 else: return n * factorial(n-1)

Example: Inverse Cascade

Page 26: Tree Recursioncs61a/fa20/assets/slides/... · 2021. 1. 6. · 3 * factorial(2) 2 * factorial(1) 1 * factorial(0) def factorial(n): if n == 0: return 1 else: return n * factorial(n-1)

Inverse Cascade

Write a function that prints an inverse cascade:

12

Page 27: Tree Recursioncs61a/fa20/assets/slides/... · 2021. 1. 6. · 3 * factorial(2) 2 * factorial(1) 1 * factorial(0) def factorial(n): if n == 0: return 1 else: return n * factorial(n-1)

1 12 123 1234 123 12 1

Inverse Cascade

Write a function that prints an inverse cascade:

12

Page 28: Tree Recursioncs61a/fa20/assets/slides/... · 2021. 1. 6. · 3 * factorial(2) 2 * factorial(1) 1 * factorial(0) def factorial(n): if n == 0: return 1 else: return n * factorial(n-1)

1 12 123 1234 123 12 1

Inverse Cascade

Write a function that prints an inverse cascade:

12

1 12 123 1234 123 12 1

def inverse_cascade(n): grow(n) print(n) shrink(n)

Page 29: Tree Recursioncs61a/fa20/assets/slides/... · 2021. 1. 6. · 3 * factorial(2) 2 * factorial(1) 1 * factorial(0) def factorial(n): if n == 0: return 1 else: return n * factorial(n-1)

1 12 123 1234 123 12 1

Inverse Cascade

Write a function that prints an inverse cascade:

12

def f_then_g(f, g, n): if n: f(n) g(n)

1 12 123 1234 123 12 1

def inverse_cascade(n): grow(n) print(n) shrink(n)

Page 30: Tree Recursioncs61a/fa20/assets/slides/... · 2021. 1. 6. · 3 * factorial(2) 2 * factorial(1) 1 * factorial(0) def factorial(n): if n == 0: return 1 else: return n * factorial(n-1)

1 12 123 1234 123 12 1

Inverse Cascade

Write a function that prints an inverse cascade:

12

grow = lambda n: f_then_g(grow, print, n//10) shrink = lambda n: f_then_g(print, shrink, n//10)

def f_then_g(f, g, n): if n: f(n) g(n)

1 12 123 1234 123 12 1

def inverse_cascade(n): grow(n) print(n) shrink(n)

Page 31: Tree Recursioncs61a/fa20/assets/slides/... · 2021. 1. 6. · 3 * factorial(2) 2 * factorial(1) 1 * factorial(0) def factorial(n): if n == 0: return 1 else: return n * factorial(n-1)

1 12 123 1234 123 12 1

Inverse Cascade

Write a function that prints an inverse cascade:

12

grow = lambda n: f_then_g(grow, print, n//10) shrink = lambda n: f_then_g(print, shrink, n//10)

def f_then_g(f, g, n): if n: f(n) g(n)

1 12 123 1234 123 12 1

def inverse_cascade(n): grow(n) print(n) shrink(n)

Page 32: Tree Recursioncs61a/fa20/assets/slides/... · 2021. 1. 6. · 3 * factorial(2) 2 * factorial(1) 1 * factorial(0) def factorial(n): if n == 0: return 1 else: return n * factorial(n-1)

Tree Recursion

Page 33: Tree Recursioncs61a/fa20/assets/slides/... · 2021. 1. 6. · 3 * factorial(2) 2 * factorial(1) 1 * factorial(0) def factorial(n): if n == 0: return 1 else: return n * factorial(n-1)

Tree Recursion

Tree-shaped processes arise whenever executing the body of a recursive function makes more than one recursive call

14

Page 34: Tree Recursioncs61a/fa20/assets/slides/... · 2021. 1. 6. · 3 * factorial(2) 2 * factorial(1) 1 * factorial(0) def factorial(n): if n == 0: return 1 else: return n * factorial(n-1)

Tree Recursion

http://en.wikipedia.org/wiki/File:Fibonacci.jpg

Tree-shaped processes arise whenever executing the body of a recursive function makes more than one recursive call

14

Page 35: Tree Recursioncs61a/fa20/assets/slides/... · 2021. 1. 6. · 3 * factorial(2) 2 * factorial(1) 1 * factorial(0) def factorial(n): if n == 0: return 1 else: return n * factorial(n-1)

Tree Recursion

http://en.wikipedia.org/wiki/File:Fibonacci.jpg

0, 1, 2, 3, 4, 5, 6, 7, 8,n:

Tree-shaped processes arise whenever executing the body of a recursive function makes more than one recursive call

14

Page 36: Tree Recursioncs61a/fa20/assets/slides/... · 2021. 1. 6. · 3 * factorial(2) 2 * factorial(1) 1 * factorial(0) def factorial(n): if n == 0: return 1 else: return n * factorial(n-1)

Tree Recursion

http://en.wikipedia.org/wiki/File:Fibonacci.jpg

0, 1, 2, 3, 4, 5, 6, 7, 8,n:

0, 1, 1, 2, 3, 5, 8, 13, 21,fib(n):

Tree-shaped processes arise whenever executing the body of a recursive function makes more than one recursive call

14

Page 37: Tree Recursioncs61a/fa20/assets/slides/... · 2021. 1. 6. · 3 * factorial(2) 2 * factorial(1) 1 * factorial(0) def factorial(n): if n == 0: return 1 else: return n * factorial(n-1)

Tree Recursion

http://en.wikipedia.org/wiki/File:Fibonacci.jpg

0, 1, 2, 3, 4, 5, 6, 7, 8,n:

0, 1, 1, 2, 3, 5, 8, 13, 21,fib(n):

... , 35

Tree-shaped processes arise whenever executing the body of a recursive function makes more than one recursive call

14

Page 38: Tree Recursioncs61a/fa20/assets/slides/... · 2021. 1. 6. · 3 * factorial(2) 2 * factorial(1) 1 * factorial(0) def factorial(n): if n == 0: return 1 else: return n * factorial(n-1)

Tree Recursion

http://en.wikipedia.org/wiki/File:Fibonacci.jpg

0, 1, 2, 3, 4, 5, 6, 7, 8,n:

0, 1, 1, 2, 3, 5, 8, 13, 21,fib(n): ... , 9,227,465

... , 35

Tree-shaped processes arise whenever executing the body of a recursive function makes more than one recursive call

14

Page 39: Tree Recursioncs61a/fa20/assets/slides/... · 2021. 1. 6. · 3 * factorial(2) 2 * factorial(1) 1 * factorial(0) def factorial(n): if n == 0: return 1 else: return n * factorial(n-1)

Tree Recursion

http://en.wikipedia.org/wiki/File:Fibonacci.jpg

0, 1, 2, 3, 4, 5, 6, 7, 8,n:

0, 1, 1, 2, 3, 5, 8, 13, 21,fib(n): ... , 9,227,465

... , 35

def fib(n):

Tree-shaped processes arise whenever executing the body of a recursive function makes more than one recursive call

14

Page 40: Tree Recursioncs61a/fa20/assets/slides/... · 2021. 1. 6. · 3 * factorial(2) 2 * factorial(1) 1 * factorial(0) def factorial(n): if n == 0: return 1 else: return n * factorial(n-1)

Tree Recursion

http://en.wikipedia.org/wiki/File:Fibonacci.jpg

0, 1, 2, 3, 4, 5, 6, 7, 8,n:

0, 1, 1, 2, 3, 5, 8, 13, 21,fib(n): ... , 9,227,465

... , 35

def fib(n): if n == 0:

Tree-shaped processes arise whenever executing the body of a recursive function makes more than one recursive call

14

Page 41: Tree Recursioncs61a/fa20/assets/slides/... · 2021. 1. 6. · 3 * factorial(2) 2 * factorial(1) 1 * factorial(0) def factorial(n): if n == 0: return 1 else: return n * factorial(n-1)

Tree Recursion

http://en.wikipedia.org/wiki/File:Fibonacci.jpg

0, 1, 2, 3, 4, 5, 6, 7, 8,n:

0, 1, 1, 2, 3, 5, 8, 13, 21,fib(n): ... , 9,227,465

... , 35

def fib(n): if n == 0: return 0

Tree-shaped processes arise whenever executing the body of a recursive function makes more than one recursive call

14

Page 42: Tree Recursioncs61a/fa20/assets/slides/... · 2021. 1. 6. · 3 * factorial(2) 2 * factorial(1) 1 * factorial(0) def factorial(n): if n == 0: return 1 else: return n * factorial(n-1)

Tree Recursion

http://en.wikipedia.org/wiki/File:Fibonacci.jpg

0, 1, 2, 3, 4, 5, 6, 7, 8,n:

0, 1, 1, 2, 3, 5, 8, 13, 21,fib(n): ... , 9,227,465

... , 35

def fib(n): if n == 0: return 0 elif n == 1:

Tree-shaped processes arise whenever executing the body of a recursive function makes more than one recursive call

14

Page 43: Tree Recursioncs61a/fa20/assets/slides/... · 2021. 1. 6. · 3 * factorial(2) 2 * factorial(1) 1 * factorial(0) def factorial(n): if n == 0: return 1 else: return n * factorial(n-1)

Tree Recursion

http://en.wikipedia.org/wiki/File:Fibonacci.jpg

0, 1, 2, 3, 4, 5, 6, 7, 8,n:

0, 1, 1, 2, 3, 5, 8, 13, 21,fib(n): ... , 9,227,465

... , 35

def fib(n): if n == 0: return 0 elif n == 1: return 1

Tree-shaped processes arise whenever executing the body of a recursive function makes more than one recursive call

14

Page 44: Tree Recursioncs61a/fa20/assets/slides/... · 2021. 1. 6. · 3 * factorial(2) 2 * factorial(1) 1 * factorial(0) def factorial(n): if n == 0: return 1 else: return n * factorial(n-1)

Tree Recursion

http://en.wikipedia.org/wiki/File:Fibonacci.jpg

0, 1, 2, 3, 4, 5, 6, 7, 8,n:

0, 1, 1, 2, 3, 5, 8, 13, 21,fib(n): ... , 9,227,465

... , 35

def fib(n): if n == 0: return 0 elif n == 1: return 1 else:

Tree-shaped processes arise whenever executing the body of a recursive function makes more than one recursive call

14

Page 45: Tree Recursioncs61a/fa20/assets/slides/... · 2021. 1. 6. · 3 * factorial(2) 2 * factorial(1) 1 * factorial(0) def factorial(n): if n == 0: return 1 else: return n * factorial(n-1)

Tree Recursion

http://en.wikipedia.org/wiki/File:Fibonacci.jpg

0, 1, 2, 3, 4, 5, 6, 7, 8,n:

0, 1, 1, 2, 3, 5, 8, 13, 21,fib(n): ... , 9,227,465

... , 35

def fib(n): if n == 0: return 0 elif n == 1: return 1 else: return fib(n-2) + fib(n-1)

Tree-shaped processes arise whenever executing the body of a recursive function makes more than one recursive call

14

Page 46: Tree Recursioncs61a/fa20/assets/slides/... · 2021. 1. 6. · 3 * factorial(2) 2 * factorial(1) 1 * factorial(0) def factorial(n): if n == 0: return 1 else: return n * factorial(n-1)

A Tree-Recursive Process

The computational process of fib evolves into a tree structure

15

Page 47: Tree Recursioncs61a/fa20/assets/slides/... · 2021. 1. 6. · 3 * factorial(2) 2 * factorial(1) 1 * factorial(0) def factorial(n): if n == 0: return 1 else: return n * factorial(n-1)

A Tree-Recursive Process

The computational process of fib evolves into a tree structure

15

fib(5)

Page 48: Tree Recursioncs61a/fa20/assets/slides/... · 2021. 1. 6. · 3 * factorial(2) 2 * factorial(1) 1 * factorial(0) def factorial(n): if n == 0: return 1 else: return n * factorial(n-1)

A Tree-Recursive Process

The computational process of fib evolves into a tree structure

15

fib(5)

fib(3)

Page 49: Tree Recursioncs61a/fa20/assets/slides/... · 2021. 1. 6. · 3 * factorial(2) 2 * factorial(1) 1 * factorial(0) def factorial(n): if n == 0: return 1 else: return n * factorial(n-1)

A Tree-Recursive Process

The computational process of fib evolves into a tree structure

15

fib(5)

fib(4)fib(3)

Page 50: Tree Recursioncs61a/fa20/assets/slides/... · 2021. 1. 6. · 3 * factorial(2) 2 * factorial(1) 1 * factorial(0) def factorial(n): if n == 0: return 1 else: return n * factorial(n-1)

A Tree-Recursive Process

The computational process of fib evolves into a tree structure

15

fib(5)

fib(4)fib(3)

fib(1)

1

fib(2)

fib(0) fib(1)

0 1

Page 51: Tree Recursioncs61a/fa20/assets/slides/... · 2021. 1. 6. · 3 * factorial(2) 2 * factorial(1) 1 * factorial(0) def factorial(n): if n == 0: return 1 else: return n * factorial(n-1)

A Tree-Recursive Process

The computational process of fib evolves into a tree structure

15

fib(5)

fib(4)

fib(3)

fib(1)

1

fib(2)

fib(0) fib(1)

0 1

fib(2)

fib(0) fib(1)

0 1

fib(3)

fib(1)

1

fib(2)

fib(0) fib(1)

0 1

Page 52: Tree Recursioncs61a/fa20/assets/slides/... · 2021. 1. 6. · 3 * factorial(2) 2 * factorial(1) 1 * factorial(0) def factorial(n): if n == 0: return 1 else: return n * factorial(n-1)

A Tree-Recursive Process

The computational process of fib evolves into a tree structure

15

fib(5)

fib(4)

fib(3)

fib(1)

1

fib(2)

fib(0) fib(1)

0 1

fib(2)

fib(0) fib(1)

0 1

fib(3)

fib(1)

1

fib(2)

fib(0) fib(1)

0 1

Page 53: Tree Recursioncs61a/fa20/assets/slides/... · 2021. 1. 6. · 3 * factorial(2) 2 * factorial(1) 1 * factorial(0) def factorial(n): if n == 0: return 1 else: return n * factorial(n-1)

A Tree-Recursive Process

The computational process of fib evolves into a tree structure

15

fib(5)

fib(4)

fib(3)

fib(1)

1

fib(2)

fib(0) fib(1)

0 1

fib(2)

fib(0) fib(1)

0 1

fib(3)

fib(1)

1

fib(2)

fib(0) fib(1)

0 1

Page 54: Tree Recursioncs61a/fa20/assets/slides/... · 2021. 1. 6. · 3 * factorial(2) 2 * factorial(1) 1 * factorial(0) def factorial(n): if n == 0: return 1 else: return n * factorial(n-1)

A Tree-Recursive Process

The computational process of fib evolves into a tree structure

15

fib(5)

fib(4)

fib(3)

fib(1)

1

fib(2)

fib(0) fib(1)

0 1

fib(2)

fib(0) fib(1)

0 1

fib(3)

fib(1)

1

fib(2)

fib(0) fib(1)

0 1

Page 55: Tree Recursioncs61a/fa20/assets/slides/... · 2021. 1. 6. · 3 * factorial(2) 2 * factorial(1) 1 * factorial(0) def factorial(n): if n == 0: return 1 else: return n * factorial(n-1)

A Tree-Recursive Process

The computational process of fib evolves into a tree structure

15

fib(5)

fib(4)

fib(3)

fib(1)

1

fib(2)

fib(0) fib(1)

0 1

fib(2)

fib(0) fib(1)

0 1

fib(3)

fib(1)

1

fib(2)

fib(0) fib(1)

0 1

Page 56: Tree Recursioncs61a/fa20/assets/slides/... · 2021. 1. 6. · 3 * factorial(2) 2 * factorial(1) 1 * factorial(0) def factorial(n): if n == 0: return 1 else: return n * factorial(n-1)

A Tree-Recursive Process

The computational process of fib evolves into a tree structure

15

fib(5)

fib(4)

fib(3)

fib(1)

1

fib(2)

fib(0) fib(1)

0 1

fib(2)

fib(0) fib(1)

0 1

fib(3)

fib(1)

1

fib(2)

fib(0) fib(1)

0 1

Page 57: Tree Recursioncs61a/fa20/assets/slides/... · 2021. 1. 6. · 3 * factorial(2) 2 * factorial(1) 1 * factorial(0) def factorial(n): if n == 0: return 1 else: return n * factorial(n-1)

A Tree-Recursive Process

The computational process of fib evolves into a tree structure

15

fib(5)

fib(4)

fib(3)

fib(1)

1

fib(2)

fib(0) fib(1)

0 1

fib(2)

fib(0) fib(1)

0 1

fib(3)

fib(1)

1

fib(2)

fib(0) fib(1)

0 1

Page 58: Tree Recursioncs61a/fa20/assets/slides/... · 2021. 1. 6. · 3 * factorial(2) 2 * factorial(1) 1 * factorial(0) def factorial(n): if n == 0: return 1 else: return n * factorial(n-1)

A Tree-Recursive Process

The computational process of fib evolves into a tree structure

15

fib(5)

fib(4)

fib(3)

fib(1)

1

fib(2)

fib(0) fib(1)

0 1

fib(2)

fib(0) fib(1)

0 1

fib(3)

fib(1)

1

fib(2)

fib(0) fib(1)

0 1

Page 59: Tree Recursioncs61a/fa20/assets/slides/... · 2021. 1. 6. · 3 * factorial(2) 2 * factorial(1) 1 * factorial(0) def factorial(n): if n == 0: return 1 else: return n * factorial(n-1)

A Tree-Recursive Process

The computational process of fib evolves into a tree structure

15

fib(5)

fib(4)

fib(3)

fib(1)

1

fib(2)

fib(0) fib(1)

0 1

fib(2)

fib(0) fib(1)

0 1

fib(3)

fib(1)

1

fib(2)

fib(0) fib(1)

0 1

Page 60: Tree Recursioncs61a/fa20/assets/slides/... · 2021. 1. 6. · 3 * factorial(2) 2 * factorial(1) 1 * factorial(0) def factorial(n): if n == 0: return 1 else: return n * factorial(n-1)

A Tree-Recursive Process

The computational process of fib evolves into a tree structure

15

fib(5)

fib(4)

fib(3)

fib(1)

1

fib(2)

fib(0) fib(1)

0 1

fib(2)

fib(0) fib(1)

0 1

fib(3)

fib(1)

1

fib(2)

fib(0) fib(1)

0 1

Page 61: Tree Recursioncs61a/fa20/assets/slides/... · 2021. 1. 6. · 3 * factorial(2) 2 * factorial(1) 1 * factorial(0) def factorial(n): if n == 0: return 1 else: return n * factorial(n-1)

A Tree-Recursive Process

The computational process of fib evolves into a tree structure

15

fib(5)

fib(4)

fib(3)

fib(1)

1

fib(2)

fib(0) fib(1)

0 1

fib(2)

fib(0) fib(1)

0 1

fib(3)

fib(1)

1

fib(2)

fib(0) fib(1)

0 1

Page 62: Tree Recursioncs61a/fa20/assets/slides/... · 2021. 1. 6. · 3 * factorial(2) 2 * factorial(1) 1 * factorial(0) def factorial(n): if n == 0: return 1 else: return n * factorial(n-1)

A Tree-Recursive Process

The computational process of fib evolves into a tree structure

15

fib(5)

fib(4)

fib(3)

fib(1)

1

fib(2)

fib(0) fib(1)

0 1

fib(2)

fib(0) fib(1)

0 1

fib(3)

fib(1)

1

fib(2)

fib(0) fib(1)

0 1

Page 63: Tree Recursioncs61a/fa20/assets/slides/... · 2021. 1. 6. · 3 * factorial(2) 2 * factorial(1) 1 * factorial(0) def factorial(n): if n == 0: return 1 else: return n * factorial(n-1)

A Tree-Recursive Process

The computational process of fib evolves into a tree structure

15

fib(5)

fib(4)

fib(3)

fib(1)

1

fib(2)

fib(0) fib(1)

0 1

fib(2)

fib(0) fib(1)

0 1

fib(3)

fib(1)

1

fib(2)

fib(0) fib(1)

0 1

Page 64: Tree Recursioncs61a/fa20/assets/slides/... · 2021. 1. 6. · 3 * factorial(2) 2 * factorial(1) 1 * factorial(0) def factorial(n): if n == 0: return 1 else: return n * factorial(n-1)

A Tree-Recursive Process

The computational process of fib evolves into a tree structure

15

fib(5)

fib(4)

fib(3)

fib(1)

1

fib(2)

fib(0) fib(1)

0 1

fib(2)

fib(0) fib(1)

0 1

fib(3)

fib(1)

1

fib(2)

fib(0) fib(1)

0 1

Page 65: Tree Recursioncs61a/fa20/assets/slides/... · 2021. 1. 6. · 3 * factorial(2) 2 * factorial(1) 1 * factorial(0) def factorial(n): if n == 0: return 1 else: return n * factorial(n-1)

A Tree-Recursive Process

The computational process of fib evolves into a tree structure

15

fib(5)

fib(4)

fib(3)

fib(1)

1

fib(2)

fib(0) fib(1)

0 1

fib(2)

fib(0) fib(1)

0 1

fib(3)

fib(1)

1

fib(2)

fib(0) fib(1)

0 1

Page 66: Tree Recursioncs61a/fa20/assets/slides/... · 2021. 1. 6. · 3 * factorial(2) 2 * factorial(1) 1 * factorial(0) def factorial(n): if n == 0: return 1 else: return n * factorial(n-1)

A Tree-Recursive Process

The computational process of fib evolves into a tree structure

15

fib(5)

fib(4)

fib(3)

fib(1)

1

fib(2)

fib(0) fib(1)

0 1

fib(2)

fib(0) fib(1)

0 1

fib(3)

fib(1)

1

fib(2)

fib(0) fib(1)

0 1

Page 67: Tree Recursioncs61a/fa20/assets/slides/... · 2021. 1. 6. · 3 * factorial(2) 2 * factorial(1) 1 * factorial(0) def factorial(n): if n == 0: return 1 else: return n * factorial(n-1)

A Tree-Recursive Process

The computational process of fib evolves into a tree structure

15

fib(5)

fib(4)

fib(3)

fib(1)

1

fib(2)

fib(0) fib(1)

0 1

fib(2)

fib(0) fib(1)

0 1

fib(3)

fib(1)

1

fib(2)

fib(0) fib(1)

0 1

Page 68: Tree Recursioncs61a/fa20/assets/slides/... · 2021. 1. 6. · 3 * factorial(2) 2 * factorial(1) 1 * factorial(0) def factorial(n): if n == 0: return 1 else: return n * factorial(n-1)

A Tree-Recursive Process

The computational process of fib evolves into a tree structure

15

fib(5)

fib(4)

fib(3)

fib(1)

1

fib(2)

fib(0) fib(1)

0 1

fib(2)

fib(0) fib(1)

0 1

fib(3)

fib(1)

1

fib(2)

fib(0) fib(1)

0 1

(Demo)

Page 69: Tree Recursioncs61a/fa20/assets/slides/... · 2021. 1. 6. · 3 * factorial(2) 2 * factorial(1) 1 * factorial(0) def factorial(n): if n == 0: return 1 else: return n * factorial(n-1)

Repetition in Tree-Recursive Computation

16

Page 70: Tree Recursioncs61a/fa20/assets/slides/... · 2021. 1. 6. · 3 * factorial(2) 2 * factorial(1) 1 * factorial(0) def factorial(n): if n == 0: return 1 else: return n * factorial(n-1)

Repetition in Tree-Recursive Computation

This process is highly repetitive; fib is called on the same argument multiple times

16

Page 71: Tree Recursioncs61a/fa20/assets/slides/... · 2021. 1. 6. · 3 * factorial(2) 2 * factorial(1) 1 * factorial(0) def factorial(n): if n == 0: return 1 else: return n * factorial(n-1)

Repetition in Tree-Recursive Computation

fib(5)

fib(3)

fib(1)

1

fib(4)

fib(2)

fib(0) fib(1)

0 1

fib(2)

fib(0) fib(1)

0 1

fib(3)

fib(1)

1

fib(2)

fib(0) fib(1)

0 1

This process is highly repetitive; fib is called on the same argument multiple times

16

Page 72: Tree Recursioncs61a/fa20/assets/slides/... · 2021. 1. 6. · 3 * factorial(2) 2 * factorial(1) 1 * factorial(0) def factorial(n): if n == 0: return 1 else: return n * factorial(n-1)

Repetition in Tree-Recursive Computation

fib(5)

fib(3)

fib(1)

1

fib(4)

fib(2)

fib(0) fib(1)

0 1

fib(2)

fib(0) fib(1)

0 1

fib(3)

fib(1)

1

fib(2)

fib(0) fib(1)

0 1

This process is highly repetitive; fib is called on the same argument multiple times

16

(We will speed up this computation dramatically in a few weeks by remembering results)

Page 73: Tree Recursioncs61a/fa20/assets/slides/... · 2021. 1. 6. · 3 * factorial(2) 2 * factorial(1) 1 * factorial(0) def factorial(n): if n == 0: return 1 else: return n * factorial(n-1)

Example: Towers of Hanoi

Page 74: Tree Recursioncs61a/fa20/assets/slides/... · 2021. 1. 6. · 3 * factorial(2) 2 * factorial(1) 1 * factorial(0) def factorial(n): if n == 0: return 1 else: return n * factorial(n-1)

1 2 3

n = 1: move disk from post 1 to post 2

Towers of Hanoi

Page 75: Tree Recursioncs61a/fa20/assets/slides/... · 2021. 1. 6. · 3 * factorial(2) 2 * factorial(1) 1 * factorial(0) def factorial(n): if n == 0: return 1 else: return n * factorial(n-1)

1 2 3

n = 1: move disk from post 1 to post 2

Towers of Hanoi

Page 76: Tree Recursioncs61a/fa20/assets/slides/... · 2021. 1. 6. · 3 * factorial(2) 2 * factorial(1) 1 * factorial(0) def factorial(n): if n == 0: return 1 else: return n * factorial(n-1)

1 2 3

n = 1: move disk from post 1 to post 2

Towers of Hanoi

Page 77: Tree Recursioncs61a/fa20/assets/slides/... · 2021. 1. 6. · 3 * factorial(2) 2 * factorial(1) 1 * factorial(0) def factorial(n): if n == 0: return 1 else: return n * factorial(n-1)

def move_disk(disk_number, from_peg, to_peg): print("Move disk " + str(disk_number) + " from peg " \ + str(from_peg) + " to peg " + str(to_peg) + ".")

def solve_hanoi(n, start_peg, end_peg): if n == 1: move_disk(n, start_peg, end_peg) else:

Page 78: Tree Recursioncs61a/fa20/assets/slides/... · 2021. 1. 6. · 3 * factorial(2) 2 * factorial(1) 1 * factorial(0) def factorial(n): if n == 0: return 1 else: return n * factorial(n-1)

def move_disk(disk_number, from_peg, to_peg): print("Move disk " + str(disk_number) + " from peg " \ + str(from_peg) + " to peg " + str(to_peg) + ".")

def solve_hanoi(n, start_peg, end_peg): if n == 1: move_disk(n, start_peg, end_peg) else: spare_peg = 6 - start_peg - end_peg solve_hanoi(n - 1, start_peg, spare_peg) move_disk(n, start_peg, end_peg) solve_hanoi(n - 1, spare_peg, end_peg)

Page 79: Tree Recursioncs61a/fa20/assets/slides/... · 2021. 1. 6. · 3 * factorial(2) 2 * factorial(1) 1 * factorial(0) def factorial(n): if n == 0: return 1 else: return n * factorial(n-1)

3

1 2 3

2

1

hanoi(3,1,2)

def solve_hanoi(n, start_peg, end_peg): if n == 1: move_disk(n, start_peg, end_peg) else: spare_peg = 6 - start_peg - end_peg solve_hanoi(n - 1, start_peg, spare_peg) move_disk(n, start_peg, end_peg) solve_hanoi(n - 1, spare_peg, end_peg)

Page 80: Tree Recursioncs61a/fa20/assets/slides/... · 2021. 1. 6. · 3 * factorial(2) 2 * factorial(1) 1 * factorial(0) def factorial(n): if n == 0: return 1 else: return n * factorial(n-1)

Example: Counting Partitions

Page 81: Tree Recursioncs61a/fa20/assets/slides/... · 2021. 1. 6. · 3 * factorial(2) 2 * factorial(1) 1 * factorial(0) def factorial(n): if n == 0: return 1 else: return n * factorial(n-1)

Counting Partitions

The number of partitions of a positive integer n, using parts up to size m, is the number of ways in which n can be expressed as the sum of positive integer parts up to m in increasing order.

25

Page 82: Tree Recursioncs61a/fa20/assets/slides/... · 2021. 1. 6. · 3 * factorial(2) 2 * factorial(1) 1 * factorial(0) def factorial(n): if n == 0: return 1 else: return n * factorial(n-1)

Counting Partitions

The number of partitions of a positive integer n, using parts up to size m, is the number of ways in which n can be expressed as the sum of positive integer parts up to m in increasing order.

25

count_partitions(6, 4)

Page 83: Tree Recursioncs61a/fa20/assets/slides/... · 2021. 1. 6. · 3 * factorial(2) 2 * factorial(1) 1 * factorial(0) def factorial(n): if n == 0: return 1 else: return n * factorial(n-1)

Counting Partitions

The number of partitions of a positive integer n, using parts up to size m, is the number of ways in which n can be expressed as the sum of positive integer parts up to m in increasing order.

25

count_partitions(6, 4)

3 + 3 = 6

1 + 1 + 2 + 2 = 6

2 + 4 = 6

1 + 1 + 4 = 6

1 + 2 + 3 = 61 + 1 + 1 + 3 = 62 + 2 + 2 = 6

1 + 1 + 1 + 1 + 2 = 61 + 1 + 1 + 1 + 1 + 1 = 6

Page 84: Tree Recursioncs61a/fa20/assets/slides/... · 2021. 1. 6. · 3 * factorial(2) 2 * factorial(1) 1 * factorial(0) def factorial(n): if n == 0: return 1 else: return n * factorial(n-1)

Counting Partitions

The number of partitions of a positive integer n, using parts up to size m, is the number of ways in which n can be expressed as the sum of positive integer parts up to m in increasing order.

25

count_partitions(6, 4)

3 + 3 = 6

1 + 1 + 2 + 2 = 6

2 + 4 = 6

1 + 1 + 4 = 6

1 + 2 + 3 = 61 + 1 + 1 + 3 = 62 + 2 + 2 = 6

1 + 1 + 1 + 1 + 2 = 61 + 1 + 1 + 1 + 1 + 1 = 6

Page 85: Tree Recursioncs61a/fa20/assets/slides/... · 2021. 1. 6. · 3 * factorial(2) 2 * factorial(1) 1 * factorial(0) def factorial(n): if n == 0: return 1 else: return n * factorial(n-1)

Counting Partitions

The number of partitions of a positive integer n, using parts up to size m, is the number of ways in which n can be expressed as the sum of positive integer parts up to m in increasing order.

25

count_partitions(6, 4)

3 + 3 = 6

1 + 1 + 2 + 2 = 6

2 + 4 = 6

1 + 1 + 4 = 6

1 + 2 + 3 = 61 + 1 + 1 + 3 = 62 + 2 + 2 = 6

1 + 1 + 1 + 1 + 2 = 61 + 1 + 1 + 1 + 1 + 1 = 6

Page 86: Tree Recursioncs61a/fa20/assets/slides/... · 2021. 1. 6. · 3 * factorial(2) 2 * factorial(1) 1 * factorial(0) def factorial(n): if n == 0: return 1 else: return n * factorial(n-1)

Counting Partitions

The number of partitions of a positive integer n, using parts up to size m, is the number of ways in which n can be expressed as the sum of positive integer parts up to m in increasing order.

25

count_partitions(6, 4)

3 + 3 = 6

1 + 1 + 2 + 2 = 6

2 + 4 = 6

1 + 1 + 4 = 6

1 + 2 + 3 = 61 + 1 + 1 + 3 = 62 + 2 + 2 = 6

1 + 1 + 1 + 1 + 2 = 61 + 1 + 1 + 1 + 1 + 1 = 6

Page 87: Tree Recursioncs61a/fa20/assets/slides/... · 2021. 1. 6. · 3 * factorial(2) 2 * factorial(1) 1 * factorial(0) def factorial(n): if n == 0: return 1 else: return n * factorial(n-1)

Counting Partitions

The number of partitions of a positive integer n, using parts up to size m, is the number of ways in which n can be expressed as the sum of positive integer parts up to m in non-decreasing order.

26

count_partitions(6, 4)

Page 88: Tree Recursioncs61a/fa20/assets/slides/... · 2021. 1. 6. · 3 * factorial(2) 2 * factorial(1) 1 * factorial(0) def factorial(n): if n == 0: return 1 else: return n * factorial(n-1)

Counting Partitions

The number of partitions of a positive integer n, using parts up to size m, is the number of ways in which n can be expressed as the sum of positive integer parts up to m in non-decreasing order.

26

• Recursive decomposition: finding simpler instances of the problem.

count_partitions(6, 4)

Page 89: Tree Recursioncs61a/fa20/assets/slides/... · 2021. 1. 6. · 3 * factorial(2) 2 * factorial(1) 1 * factorial(0) def factorial(n): if n == 0: return 1 else: return n * factorial(n-1)

Counting Partitions

The number of partitions of a positive integer n, using parts up to size m, is the number of ways in which n can be expressed as the sum of positive integer parts up to m in non-decreasing order.

26

• Recursive decomposition: finding simpler instances of the problem.

• Explore two possibilities:

count_partitions(6, 4)

Page 90: Tree Recursioncs61a/fa20/assets/slides/... · 2021. 1. 6. · 3 * factorial(2) 2 * factorial(1) 1 * factorial(0) def factorial(n): if n == 0: return 1 else: return n * factorial(n-1)

Counting Partitions

The number of partitions of a positive integer n, using parts up to size m, is the number of ways in which n can be expressed as the sum of positive integer parts up to m in non-decreasing order.

26

• Recursive decomposition: finding simpler instances of the problem.

• Explore two possibilities:

• Use at least one 4

count_partitions(6, 4)

Page 91: Tree Recursioncs61a/fa20/assets/slides/... · 2021. 1. 6. · 3 * factorial(2) 2 * factorial(1) 1 * factorial(0) def factorial(n): if n == 0: return 1 else: return n * factorial(n-1)

Counting Partitions

The number of partitions of a positive integer n, using parts up to size m, is the number of ways in which n can be expressed as the sum of positive integer parts up to m in non-decreasing order.

26

• Recursive decomposition: finding simpler instances of the problem.

• Explore two possibilities:

• Use at least one 4

• Don't use any 4

count_partitions(6, 4)

Page 92: Tree Recursioncs61a/fa20/assets/slides/... · 2021. 1. 6. · 3 * factorial(2) 2 * factorial(1) 1 * factorial(0) def factorial(n): if n == 0: return 1 else: return n * factorial(n-1)

Counting Partitions

The number of partitions of a positive integer n, using parts up to size m, is the number of ways in which n can be expressed as the sum of positive integer parts up to m in non-decreasing order.

26

• Recursive decomposition: finding simpler instances of the problem.

• Explore two possibilities:

• Use at least one 4

• Don't use any 4

count_partitions(6, 4)

Page 93: Tree Recursioncs61a/fa20/assets/slides/... · 2021. 1. 6. · 3 * factorial(2) 2 * factorial(1) 1 * factorial(0) def factorial(n): if n == 0: return 1 else: return n * factorial(n-1)

Counting Partitions

The number of partitions of a positive integer n, using parts up to size m, is the number of ways in which n can be expressed as the sum of positive integer parts up to m in non-decreasing order.

26

• Recursive decomposition: finding simpler instances of the problem.

• Explore two possibilities:

• Use at least one 4

• Don't use any 4

count_partitions(6, 4)

Page 94: Tree Recursioncs61a/fa20/assets/slides/... · 2021. 1. 6. · 3 * factorial(2) 2 * factorial(1) 1 * factorial(0) def factorial(n): if n == 0: return 1 else: return n * factorial(n-1)

Counting Partitions

The number of partitions of a positive integer n, using parts up to size m, is the number of ways in which n can be expressed as the sum of positive integer parts up to m in non-decreasing order.

26

• Recursive decomposition: finding simpler instances of the problem.

• Explore two possibilities:

• Use at least one 4

• Don't use any 4

• Solve two simpler problems:

count_partitions(6, 4)

Page 95: Tree Recursioncs61a/fa20/assets/slides/... · 2021. 1. 6. · 3 * factorial(2) 2 * factorial(1) 1 * factorial(0) def factorial(n): if n == 0: return 1 else: return n * factorial(n-1)

Counting Partitions

The number of partitions of a positive integer n, using parts up to size m, is the number of ways in which n can be expressed as the sum of positive integer parts up to m in non-decreasing order.

26

• Recursive decomposition: finding simpler instances of the problem.

• Explore two possibilities:

• Use at least one 4

• Don't use any 4

• Solve two simpler problems:

• count_partitions(2, 4)

count_partitions(6, 4)

Page 96: Tree Recursioncs61a/fa20/assets/slides/... · 2021. 1. 6. · 3 * factorial(2) 2 * factorial(1) 1 * factorial(0) def factorial(n): if n == 0: return 1 else: return n * factorial(n-1)

Counting Partitions

The number of partitions of a positive integer n, using parts up to size m, is the number of ways in which n can be expressed as the sum of positive integer parts up to m in non-decreasing order.

26

• Recursive decomposition: finding simpler instances of the problem.

• Explore two possibilities:

• Use at least one 4

• Don't use any 4

• Solve two simpler problems:

• count_partitions(2, 4)

count_partitions(6, 4)

Page 97: Tree Recursioncs61a/fa20/assets/slides/... · 2021. 1. 6. · 3 * factorial(2) 2 * factorial(1) 1 * factorial(0) def factorial(n): if n == 0: return 1 else: return n * factorial(n-1)

Counting Partitions

The number of partitions of a positive integer n, using parts up to size m, is the number of ways in which n can be expressed as the sum of positive integer parts up to m in non-decreasing order.

26

• Recursive decomposition: finding simpler instances of the problem.

• Explore two possibilities:

• Use at least one 4

• Don't use any 4

• Solve two simpler problems:

• count_partitions(2, 4)

• count_partitions(6, 3)

count_partitions(6, 4)

Page 98: Tree Recursioncs61a/fa20/assets/slides/... · 2021. 1. 6. · 3 * factorial(2) 2 * factorial(1) 1 * factorial(0) def factorial(n): if n == 0: return 1 else: return n * factorial(n-1)

Counting Partitions

The number of partitions of a positive integer n, using parts up to size m, is the number of ways in which n can be expressed as the sum of positive integer parts up to m in non-decreasing order.

26

• Recursive decomposition: finding simpler instances of the problem.

• Explore two possibilities:

• Use at least one 4

• Don't use any 4

• Solve two simpler problems:

• count_partitions(2, 4)

• count_partitions(6, 3)

count_partitions(6, 4)

Page 99: Tree Recursioncs61a/fa20/assets/slides/... · 2021. 1. 6. · 3 * factorial(2) 2 * factorial(1) 1 * factorial(0) def factorial(n): if n == 0: return 1 else: return n * factorial(n-1)

Counting Partitions

The number of partitions of a positive integer n, using parts up to size m, is the number of ways in which n can be expressed as the sum of positive integer parts up to m in non-decreasing order.

26

• Recursive decomposition: finding simpler instances of the problem.

• Explore two possibilities:

• Use at least one 4

• Don't use any 4

• Solve two simpler problems:

• count_partitions(2, 4)

• count_partitions(6, 3)

• Tree recursion often involves exploring different choices.

count_partitions(6, 4)

Page 100: Tree Recursioncs61a/fa20/assets/slides/... · 2021. 1. 6. · 3 * factorial(2) 2 * factorial(1) 1 * factorial(0) def factorial(n): if n == 0: return 1 else: return n * factorial(n-1)

Counting Partitions

The number of partitions of a positive integer n, using parts up to size m, is the number of ways in which n can be expressed as the sum of positive integer parts up to m in non-decreasing order.

26

• Recursive decomposition: finding simpler instances of the problem.

• Explore two possibilities:

• Use at least one 4

• Don't use any 4

• Solve two simpler problems:

• count_partitions(2, 4)

• count_partitions(6, 3)

• Tree recursion often involves exploring different choices.

count_partitions(6, 4)

Page 101: Tree Recursioncs61a/fa20/assets/slides/... · 2021. 1. 6. · 3 * factorial(2) 2 * factorial(1) 1 * factorial(0) def factorial(n): if n == 0: return 1 else: return n * factorial(n-1)

Counting Partitions

The number of partitions of a positive integer n, using parts up to size m, is the number of ways in which n can be expressed as the sum of positive integer parts up to m in non-decreasing order.

26

• Recursive decomposition: finding simpler instances of the problem.

• Explore two possibilities:

• Use at least one 4

• Don't use any 4

• Solve two simpler problems:

• count_partitions(2, 4)

• count_partitions(6, 3)

• Tree recursion often involves exploring different choices.

count_partitions(6, 4)

Page 102: Tree Recursioncs61a/fa20/assets/slides/... · 2021. 1. 6. · 3 * factorial(2) 2 * factorial(1) 1 * factorial(0) def factorial(n): if n == 0: return 1 else: return n * factorial(n-1)

Counting Partitions

The number of partitions of a positive integer n, using parts up to size m, is the number of ways in which n can be expressed as the sum of positive integer parts up to m in non-decreasing order.

26

• Recursive decomposition: finding simpler instances of the problem.

• Explore two possibilities:

• Use at least one 4

• Don't use any 4

• Solve two simpler problems:

• count_partitions(2, 4)

• count_partitions(6, 3)

• Tree recursion often involves exploring different choices.

count_partitions(6, 4)

Page 103: Tree Recursioncs61a/fa20/assets/slides/... · 2021. 1. 6. · 3 * factorial(2) 2 * factorial(1) 1 * factorial(0) def factorial(n): if n == 0: return 1 else: return n * factorial(n-1)

Counting Partitions

The number of partitions of a positive integer n, using parts up to size m, is the number of ways in which n can be expressed as the sum of positive integer parts up to m in increasing order.

27

• Recursive decomposition: finding simpler instances of the problem.

• Explore two possibilities:

• Use at least one 4

• Don't use any 4

• Solve two simpler problems:

• count_partitions(2, 4)

• count_partitions(6, 3)

• Tree recursion often involves exploring different choices.

Page 104: Tree Recursioncs61a/fa20/assets/slides/... · 2021. 1. 6. · 3 * factorial(2) 2 * factorial(1) 1 * factorial(0) def factorial(n): if n == 0: return 1 else: return n * factorial(n-1)

Counting Partitions

The number of partitions of a positive integer n, using parts up to size m, is the number of ways in which n can be expressed as the sum of positive integer parts up to m in increasing order.

27

• Recursive decomposition: finding simpler instances of the problem.

• Explore two possibilities:

• Use at least one 4

• Don't use any 4

• Solve two simpler problems:

• count_partitions(2, 4)

• count_partitions(6, 3)

• Tree recursion often involves exploring different choices.

def count_partitions(n, m):

Page 105: Tree Recursioncs61a/fa20/assets/slides/... · 2021. 1. 6. · 3 * factorial(2) 2 * factorial(1) 1 * factorial(0) def factorial(n): if n == 0: return 1 else: return n * factorial(n-1)

Counting Partitions

The number of partitions of a positive integer n, using parts up to size m, is the number of ways in which n can be expressed as the sum of positive integer parts up to m in increasing order.

27

• Recursive decomposition: finding simpler instances of the problem.

• Explore two possibilities:

• Use at least one 4

• Don't use any 4

• Solve two simpler problems:

• count_partitions(2, 4)

• count_partitions(6, 3)

• Tree recursion often involves exploring different choices.

def count_partitions(n, m):

else:

Page 106: Tree Recursioncs61a/fa20/assets/slides/... · 2021. 1. 6. · 3 * factorial(2) 2 * factorial(1) 1 * factorial(0) def factorial(n): if n == 0: return 1 else: return n * factorial(n-1)

Counting Partitions

The number of partitions of a positive integer n, using parts up to size m, is the number of ways in which n can be expressed as the sum of positive integer parts up to m in increasing order.

27

• Recursive decomposition: finding simpler instances of the problem.

• Explore two possibilities:

• Use at least one 4

• Don't use any 4

• Solve two simpler problems:

• count_partitions(2, 4)

• count_partitions(6, 3)

• Tree recursion often involves exploring different choices.

def count_partitions(n, m):

else: with_m = count_partitions(n-m, m)

Page 107: Tree Recursioncs61a/fa20/assets/slides/... · 2021. 1. 6. · 3 * factorial(2) 2 * factorial(1) 1 * factorial(0) def factorial(n): if n == 0: return 1 else: return n * factorial(n-1)

Counting Partitions

The number of partitions of a positive integer n, using parts up to size m, is the number of ways in which n can be expressed as the sum of positive integer parts up to m in increasing order.

27

• Recursive decomposition: finding simpler instances of the problem.

• Explore two possibilities:

• Use at least one 4

• Don't use any 4

• Solve two simpler problems:

• count_partitions(2, 4)

• count_partitions(6, 3)

• Tree recursion often involves exploring different choices.

def count_partitions(n, m):

else: with_m = count_partitions(n-m, m) without_m = count_partitions(n, m-1)

Page 108: Tree Recursioncs61a/fa20/assets/slides/... · 2021. 1. 6. · 3 * factorial(2) 2 * factorial(1) 1 * factorial(0) def factorial(n): if n == 0: return 1 else: return n * factorial(n-1)

Counting Partitions

The number of partitions of a positive integer n, using parts up to size m, is the number of ways in which n can be expressed as the sum of positive integer parts up to m in increasing order.

27

• Recursive decomposition: finding simpler instances of the problem.

• Explore two possibilities:

• Use at least one 4

• Don't use any 4

• Solve two simpler problems:

• count_partitions(2, 4)

• count_partitions(6, 3)

• Tree recursion often involves exploring different choices.

def count_partitions(n, m):

else: with_m = count_partitions(n-m, m) without_m = count_partitions(n, m-1) return with_m + without_m

Page 109: Tree Recursioncs61a/fa20/assets/slides/... · 2021. 1. 6. · 3 * factorial(2) 2 * factorial(1) 1 * factorial(0) def factorial(n): if n == 0: return 1 else: return n * factorial(n-1)

Counting Partitions

The number of partitions of a positive integer n, using parts up to size m, is the number of ways in which n can be expressed as the sum of positive integer parts up to m in increasing order.

27

• Recursive decomposition: finding simpler instances of the problem.

• Explore two possibilities:

• Use at least one 4

• Don't use any 4

• Solve two simpler problems:

• count_partitions(2, 4)

• count_partitions(6, 3)

• Tree recursion often involves exploring different choices.

def count_partitions(n, m):

else: with_m = count_partitions(n-m, m) without_m = count_partitions(n, m-1) return with_m + without_m

Page 110: Tree Recursioncs61a/fa20/assets/slides/... · 2021. 1. 6. · 3 * factorial(2) 2 * factorial(1) 1 * factorial(0) def factorial(n): if n == 0: return 1 else: return n * factorial(n-1)

Counting Partitions

The number of partitions of a positive integer n, using parts up to size m, is the number of ways in which n can be expressed as the sum of positive integer parts up to m in increasing order.

27

• Recursive decomposition: finding simpler instances of the problem.

• Explore two possibilities:

• Use at least one 4

• Don't use any 4

• Solve two simpler problems:

• count_partitions(2, 4)

• count_partitions(6, 3)

• Tree recursion often involves exploring different choices.

def count_partitions(n, m):

else: with_m = count_partitions(n-m, m) without_m = count_partitions(n, m-1) return with_m + without_m

Page 111: Tree Recursioncs61a/fa20/assets/slides/... · 2021. 1. 6. · 3 * factorial(2) 2 * factorial(1) 1 * factorial(0) def factorial(n): if n == 0: return 1 else: return n * factorial(n-1)

Counting Partitions

The number of partitions of a positive integer n, using parts up to size m, is the number of ways in which n can be expressed as the sum of positive integer parts up to m in increasing order.

27

• Recursive decomposition: finding simpler instances of the problem.

• Explore two possibilities:

• Use at least one 4

• Don't use any 4

• Solve two simpler problems:

• count_partitions(2, 4)

• count_partitions(6, 3)

• Tree recursion often involves exploring different choices.

def count_partitions(n, m): if n == 0:

else: with_m = count_partitions(n-m, m) without_m = count_partitions(n, m-1) return with_m + without_m

Page 112: Tree Recursioncs61a/fa20/assets/slides/... · 2021. 1. 6. · 3 * factorial(2) 2 * factorial(1) 1 * factorial(0) def factorial(n): if n == 0: return 1 else: return n * factorial(n-1)

Counting Partitions

The number of partitions of a positive integer n, using parts up to size m, is the number of ways in which n can be expressed as the sum of positive integer parts up to m in increasing order.

27

• Recursive decomposition: finding simpler instances of the problem.

• Explore two possibilities:

• Use at least one 4

• Don't use any 4

• Solve two simpler problems:

• count_partitions(2, 4)

• count_partitions(6, 3)

• Tree recursion often involves exploring different choices.

def count_partitions(n, m): if n == 0: return 1

else: with_m = count_partitions(n-m, m) without_m = count_partitions(n, m-1) return with_m + without_m

Page 113: Tree Recursioncs61a/fa20/assets/slides/... · 2021. 1. 6. · 3 * factorial(2) 2 * factorial(1) 1 * factorial(0) def factorial(n): if n == 0: return 1 else: return n * factorial(n-1)

Counting Partitions

The number of partitions of a positive integer n, using parts up to size m, is the number of ways in which n can be expressed as the sum of positive integer parts up to m in increasing order.

27

• Recursive decomposition: finding simpler instances of the problem.

• Explore two possibilities:

• Use at least one 4

• Don't use any 4

• Solve two simpler problems:

• count_partitions(2, 4)

• count_partitions(6, 3)

• Tree recursion often involves exploring different choices.

def count_partitions(n, m): if n == 0: return 1 elif n < 0:

else: with_m = count_partitions(n-m, m) without_m = count_partitions(n, m-1) return with_m + without_m

Page 114: Tree Recursioncs61a/fa20/assets/slides/... · 2021. 1. 6. · 3 * factorial(2) 2 * factorial(1) 1 * factorial(0) def factorial(n): if n == 0: return 1 else: return n * factorial(n-1)

Counting Partitions

The number of partitions of a positive integer n, using parts up to size m, is the number of ways in which n can be expressed as the sum of positive integer parts up to m in increasing order.

27

• Recursive decomposition: finding simpler instances of the problem.

• Explore two possibilities:

• Use at least one 4

• Don't use any 4

• Solve two simpler problems:

• count_partitions(2, 4)

• count_partitions(6, 3)

• Tree recursion often involves exploring different choices.

def count_partitions(n, m): if n == 0: return 1 elif n < 0: return 0

else: with_m = count_partitions(n-m, m) without_m = count_partitions(n, m-1) return with_m + without_m

Page 115: Tree Recursioncs61a/fa20/assets/slides/... · 2021. 1. 6. · 3 * factorial(2) 2 * factorial(1) 1 * factorial(0) def factorial(n): if n == 0: return 1 else: return n * factorial(n-1)

Counting Partitions

The number of partitions of a positive integer n, using parts up to size m, is the number of ways in which n can be expressed as the sum of positive integer parts up to m in increasing order.

27

• Recursive decomposition: finding simpler instances of the problem.

• Explore two possibilities:

• Use at least one 4

• Don't use any 4

• Solve two simpler problems:

• count_partitions(2, 4)

• count_partitions(6, 3)

• Tree recursion often involves exploring different choices.

def count_partitions(n, m): if n == 0: return 1 elif n < 0: return 0 elif m == 0:

else: with_m = count_partitions(n-m, m) without_m = count_partitions(n, m-1) return with_m + without_m

Page 116: Tree Recursioncs61a/fa20/assets/slides/... · 2021. 1. 6. · 3 * factorial(2) 2 * factorial(1) 1 * factorial(0) def factorial(n): if n == 0: return 1 else: return n * factorial(n-1)

Counting Partitions

The number of partitions of a positive integer n, using parts up to size m, is the number of ways in which n can be expressed as the sum of positive integer parts up to m in increasing order.

27

• Recursive decomposition: finding simpler instances of the problem.

• Explore two possibilities:

• Use at least one 4

• Don't use any 4

• Solve two simpler problems:

• count_partitions(2, 4)

• count_partitions(6, 3)

• Tree recursion often involves exploring different choices.

def count_partitions(n, m): if n == 0: return 1 elif n < 0: return 0 elif m == 0: return 0

else: with_m = count_partitions(n-m, m) without_m = count_partitions(n, m-1) return with_m + without_m

Page 117: Tree Recursioncs61a/fa20/assets/slides/... · 2021. 1. 6. · 3 * factorial(2) 2 * factorial(1) 1 * factorial(0) def factorial(n): if n == 0: return 1 else: return n * factorial(n-1)

Counting Partitions

The number of partitions of a positive integer n, using parts up to size m, is the number of ways in which n can be expressed as the sum of positive integer parts up to m in increasing order.

27

• Recursive decomposition: finding simpler instances of the problem.

• Explore two possibilities:

• Use at least one 4

• Don't use any 4

• Solve two simpler problems:

• count_partitions(2, 4)

• count_partitions(6, 3)

• Tree recursion often involves exploring different choices.

def count_partitions(n, m): if n == 0: return 1 elif n < 0: return 0 elif m == 0: return 0

else: with_m = count_partitions(n-m, m) without_m = count_partitions(n, m-1) return with_m + without_m

(Demo)pythontutor.com/composingprograms.html#code=def%20count_partitions%28n,%20m%29%3A%0A%20%20%20%20if%20n%20%3D%3D%200%3A%0A%20%20%20%20%20%20%20%20return%201%0A%20%20%20%20elif%20n%20<%200%3A%0A%20%20%20%20%20%20%20%20return%200%0A%20%20%20%20elif%20m%20%3D%3D%200%3A%0A%20%20%20%20%20%20%20%20return%200%0A%20%20%20%20else%3A%0A%20%20%20%20%20%20%20%20with_m%20%3D%20count_partitions%28n-m,%20m%29%20%0A%20%20%20%20%20%20%20%20without_m%20%3D%20count_partitions%28n,%20m-1%29%0A%20%20%20%20%20%20%20%20return%20with_m%20%2B%20without_m%0A%20%20%20%20%20%20%20%20%0Aresult%20%3D%20count_partitions%285,%203%29%0A%0A#%201%20%2B%201%20%2B%201%20%2B%201%20%2B%201%20%3D%205%0A#%201%20%2B%201%20%2B%201%20%2B%202%20%2B%20%20%20%3D%205%0A#%201%20%2B%202%20%2B%202%20%2B%20%20%20%20%20%20%20%3D%205%0A#%201%20%2B%201%20%2B%203%20%2B%20%20%20%20%20%20%20%3D%205%0A#%202%20%2B%203%20%2B%20%20%20%20%20%20%20%20%20%20%20%3D%205&mode=display&origin=composingprograms.js&cumulative=false&py=3&rawInputLstJSON=[]&curInstr=0