tree recursion - university of california, berkeleycs61a/fa18/assets/... · (demo) • if two...
TRANSCRIPT
![Page 1: Tree Recursion - University of California, Berkeleycs61a/fa18/assets/... · (Demo) • If two implementations are equally clear, then shorter is usually better • In this case, the](https://reader036.vdocument.in/reader036/viewer/2022071009/5fc77ab46f3ca2019f24f3ac/html5/thumbnails/1.jpg)
Tree Recursion
![Page 2: Tree Recursion - University of California, Berkeleycs61a/fa18/assets/... · (Demo) • If two implementations are equally clear, then shorter is usually better • In this case, the](https://reader036.vdocument.in/reader036/viewer/2022071009/5fc77ab46f3ca2019f24f3ac/html5/thumbnails/2.jpg)
Announcements
![Page 3: Tree Recursion - University of California, Berkeleycs61a/fa18/assets/... · (Demo) • If two implementations are equally clear, then shorter is usually better • In this case, the](https://reader036.vdocument.in/reader036/viewer/2022071009/5fc77ab46f3ca2019f24f3ac/html5/thumbnails/3.jpg)
Order of Recursive Calls
![Page 4: Tree Recursion - University of California, Berkeleycs61a/fa18/assets/... · (Demo) • If two implementations are equally clear, then shorter is usually better • In this case, the](https://reader036.vdocument.in/reader036/viewer/2022071009/5fc77ab46f3ca2019f24f3ac/html5/thumbnails/4.jpg)
The Cascade Function
(Demo)
4Interactive Diagram
![Page 5: Tree Recursion - University of California, Berkeleycs61a/fa18/assets/... · (Demo) • If two implementations are equally clear, then shorter is usually better • In this case, the](https://reader036.vdocument.in/reader036/viewer/2022071009/5fc77ab46f3ca2019f24f3ac/html5/thumbnails/5.jpg)
The Cascade Function
(Demo)
4Interactive Diagram
![Page 6: Tree Recursion - University of California, Berkeleycs61a/fa18/assets/... · (Demo) • If two implementations are equally clear, then shorter is usually better • In this case, the](https://reader036.vdocument.in/reader036/viewer/2022071009/5fc77ab46f3ca2019f24f3ac/html5/thumbnails/6.jpg)
The Cascade Function
(Demo)
4Interactive Diagram
![Page 7: Tree Recursion - University of California, Berkeleycs61a/fa18/assets/... · (Demo) • If two implementations are equally clear, then shorter is usually better • In this case, the](https://reader036.vdocument.in/reader036/viewer/2022071009/5fc77ab46f3ca2019f24f3ac/html5/thumbnails/7.jpg)
The Cascade Function
•Each cascade frame is from a different call to cascade.
(Demo)
4Interactive Diagram
![Page 8: Tree Recursion - University of California, Berkeleycs61a/fa18/assets/... · (Demo) • If two implementations are equally clear, then shorter is usually better • In this case, the](https://reader036.vdocument.in/reader036/viewer/2022071009/5fc77ab46f3ca2019f24f3ac/html5/thumbnails/8.jpg)
The Cascade Function
•Each cascade frame is from a different call to cascade.
•Until the Return value appears, that call has not completed.
(Demo)
4Interactive Diagram
![Page 9: Tree Recursion - University of California, Berkeleycs61a/fa18/assets/... · (Demo) • If two implementations are equally clear, then shorter is usually better • In this case, the](https://reader036.vdocument.in/reader036/viewer/2022071009/5fc77ab46f3ca2019f24f3ac/html5/thumbnails/9.jpg)
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)
4Interactive Diagram
![Page 10: Tree Recursion - University of California, Berkeleycs61a/fa18/assets/... · (Demo) • If two implementations are equally clear, then shorter is usually better • In this case, the](https://reader036.vdocument.in/reader036/viewer/2022071009/5fc77ab46f3ca2019f24f3ac/html5/thumbnails/10.jpg)
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)
4Interactive Diagram
![Page 11: Tree Recursion - University of California, Berkeleycs61a/fa18/assets/... · (Demo) • If two implementations are equally clear, then shorter is usually better • In this case, the](https://reader036.vdocument.in/reader036/viewer/2022071009/5fc77ab46f3ca2019f24f3ac/html5/thumbnails/11.jpg)
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)
4Interactive Diagram
![Page 12: Tree Recursion - University of California, Berkeleycs61a/fa18/assets/... · (Demo) • If two implementations are equally clear, then shorter is usually better • In this case, the](https://reader036.vdocument.in/reader036/viewer/2022071009/5fc77ab46f3ca2019f24f3ac/html5/thumbnails/12.jpg)
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)
4Interactive Diagram
![Page 13: Tree Recursion - University of California, Berkeleycs61a/fa18/assets/... · (Demo) • If two implementations are equally clear, then shorter is usually better • In this case, the](https://reader036.vdocument.in/reader036/viewer/2022071009/5fc77ab46f3ca2019f24f3ac/html5/thumbnails/13.jpg)
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)
4Interactive Diagram
![Page 14: Tree Recursion - University of California, Berkeleycs61a/fa18/assets/... · (Demo) • If two implementations are equally clear, then shorter is usually better • In this case, the](https://reader036.vdocument.in/reader036/viewer/2022071009/5fc77ab46f3ca2019f24f3ac/html5/thumbnails/14.jpg)
Two Definitions of Cascade
5
(Demo)
![Page 15: Tree Recursion - University of California, Berkeleycs61a/fa18/assets/... · (Demo) • If two implementations are equally clear, then shorter is usually better • In this case, the](https://reader036.vdocument.in/reader036/viewer/2022071009/5fc77ab46f3ca2019f24f3ac/html5/thumbnails/15.jpg)
Two Definitions of Cascade
5
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 16: Tree Recursion - University of California, Berkeleycs61a/fa18/assets/... · (Demo) • If two implementations are equally clear, then shorter is usually better • In this case, the](https://reader036.vdocument.in/reader036/viewer/2022071009/5fc77ab46f3ca2019f24f3ac/html5/thumbnails/16.jpg)
Two Definitions of Cascade
5
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 17: Tree Recursion - University of California, Berkeleycs61a/fa18/assets/... · (Demo) • If two implementations are equally clear, then shorter is usually better • In this case, the](https://reader036.vdocument.in/reader036/viewer/2022071009/5fc77ab46f3ca2019f24f3ac/html5/thumbnails/17.jpg)
Two Definitions of Cascade
5
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 18: Tree Recursion - University of California, Berkeleycs61a/fa18/assets/... · (Demo) • If two implementations are equally clear, then shorter is usually better • In this case, the](https://reader036.vdocument.in/reader036/viewer/2022071009/5fc77ab46f3ca2019f24f3ac/html5/thumbnails/18.jpg)
Two Definitions of Cascade
5
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 19: Tree Recursion - University of California, Berkeleycs61a/fa18/assets/... · (Demo) • If two implementations are equally clear, then shorter is usually better • In this case, the](https://reader036.vdocument.in/reader036/viewer/2022071009/5fc77ab46f3ca2019f24f3ac/html5/thumbnails/19.jpg)
Two Definitions of Cascade
5
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 20: Tree Recursion - University of California, Berkeleycs61a/fa18/assets/... · (Demo) • If two implementations are equally clear, then shorter is usually better • In this case, the](https://reader036.vdocument.in/reader036/viewer/2022071009/5fc77ab46f3ca2019f24f3ac/html5/thumbnails/20.jpg)
Example: Inverse Cascade
![Page 21: Tree Recursion - University of California, Berkeleycs61a/fa18/assets/... · (Demo) • If two implementations are equally clear, then shorter is usually better • In this case, the](https://reader036.vdocument.in/reader036/viewer/2022071009/5fc77ab46f3ca2019f24f3ac/html5/thumbnails/21.jpg)
Inverse Cascade
Write a function that prints an inverse cascade:
7
![Page 22: Tree Recursion - University of California, Berkeleycs61a/fa18/assets/... · (Demo) • If two implementations are equally clear, then shorter is usually better • In this case, the](https://reader036.vdocument.in/reader036/viewer/2022071009/5fc77ab46f3ca2019f24f3ac/html5/thumbnails/22.jpg)
1 12 123123412312 1
Inverse Cascade
Write a function that prints an inverse cascade:
7
![Page 23: Tree Recursion - University of California, Berkeleycs61a/fa18/assets/... · (Demo) • If two implementations are equally clear, then shorter is usually better • In this case, the](https://reader036.vdocument.in/reader036/viewer/2022071009/5fc77ab46f3ca2019f24f3ac/html5/thumbnails/23.jpg)
1 12 123123412312 1
Inverse Cascade
Write a function that prints an inverse cascade:
7
1 12 123123412312 1
def inverse_cascade(n): grow(n) print(n) shrink(n)
![Page 24: Tree Recursion - University of California, Berkeleycs61a/fa18/assets/... · (Demo) • If two implementations are equally clear, then shorter is usually better • In this case, the](https://reader036.vdocument.in/reader036/viewer/2022071009/5fc77ab46f3ca2019f24f3ac/html5/thumbnails/24.jpg)
1 12 123123412312 1
Inverse Cascade
Write a function that prints an inverse cascade:
7
def f_then_g(f, g, n): if n: f(n) g(n)
1 12 123123412312 1
def inverse_cascade(n): grow(n) print(n) shrink(n)
![Page 25: Tree Recursion - University of California, Berkeleycs61a/fa18/assets/... · (Demo) • If two implementations are equally clear, then shorter is usually better • In this case, the](https://reader036.vdocument.in/reader036/viewer/2022071009/5fc77ab46f3ca2019f24f3ac/html5/thumbnails/25.jpg)
1 12 123123412312 1
Inverse Cascade
Write a function that prints an inverse cascade:
7
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 123123412312 1
def inverse_cascade(n): grow(n) print(n) shrink(n)
![Page 26: Tree Recursion - University of California, Berkeleycs61a/fa18/assets/... · (Demo) • If two implementations are equally clear, then shorter is usually better • In this case, the](https://reader036.vdocument.in/reader036/viewer/2022071009/5fc77ab46f3ca2019f24f3ac/html5/thumbnails/26.jpg)
1 12 123123412312 1
Inverse Cascade
Write a function that prints an inverse cascade:
7
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 123123412312 1
def inverse_cascade(n): grow(n) print(n) shrink(n)
![Page 27: Tree Recursion - University of California, Berkeleycs61a/fa18/assets/... · (Demo) • If two implementations are equally clear, then shorter is usually better • In this case, the](https://reader036.vdocument.in/reader036/viewer/2022071009/5fc77ab46f3ca2019f24f3ac/html5/thumbnails/27.jpg)
Tree Recursion
![Page 28: Tree Recursion - University of California, Berkeleycs61a/fa18/assets/... · (Demo) • If two implementations are equally clear, then shorter is usually better • In this case, the](https://reader036.vdocument.in/reader036/viewer/2022071009/5fc77ab46f3ca2019f24f3ac/html5/thumbnails/28.jpg)
Tree Recursion
Tree-shaped processes arise whenever executing the body of a recursive function makes more than one recursive call
9
![Page 29: Tree Recursion - University of California, Berkeleycs61a/fa18/assets/... · (Demo) • If two implementations are equally clear, then shorter is usually better • In this case, the](https://reader036.vdocument.in/reader036/viewer/2022071009/5fc77ab46f3ca2019f24f3ac/html5/thumbnails/29.jpg)
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
9
![Page 30: Tree Recursion - University of California, Berkeleycs61a/fa18/assets/... · (Demo) • If two implementations are equally clear, then shorter is usually better • In this case, the](https://reader036.vdocument.in/reader036/viewer/2022071009/5fc77ab46f3ca2019f24f3ac/html5/thumbnails/30.jpg)
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
9
![Page 31: Tree Recursion - University of California, Berkeleycs61a/fa18/assets/... · (Demo) • If two implementations are equally clear, then shorter is usually better • In this case, the](https://reader036.vdocument.in/reader036/viewer/2022071009/5fc77ab46f3ca2019f24f3ac/html5/thumbnails/31.jpg)
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
9
![Page 32: Tree Recursion - University of California, Berkeleycs61a/fa18/assets/... · (Demo) • If two implementations are equally clear, then shorter is usually better • In this case, the](https://reader036.vdocument.in/reader036/viewer/2022071009/5fc77ab46f3ca2019f24f3ac/html5/thumbnails/32.jpg)
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
9
![Page 33: Tree Recursion - University of California, Berkeleycs61a/fa18/assets/... · (Demo) • If two implementations are equally clear, then shorter is usually better • In this case, the](https://reader036.vdocument.in/reader036/viewer/2022071009/5fc77ab46f3ca2019f24f3ac/html5/thumbnails/33.jpg)
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
9
![Page 34: Tree Recursion - University of California, Berkeleycs61a/fa18/assets/... · (Demo) • If two implementations are equally clear, then shorter is usually better • In this case, the](https://reader036.vdocument.in/reader036/viewer/2022071009/5fc77ab46f3ca2019f24f3ac/html5/thumbnails/34.jpg)
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
9
![Page 35: Tree Recursion - University of California, Berkeleycs61a/fa18/assets/... · (Demo) • If two implementations are equally clear, then shorter is usually better • In this case, the](https://reader036.vdocument.in/reader036/viewer/2022071009/5fc77ab46f3ca2019f24f3ac/html5/thumbnails/35.jpg)
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
9
![Page 36: Tree Recursion - University of California, Berkeleycs61a/fa18/assets/... · (Demo) • If two implementations are equally clear, then shorter is usually better • In this case, the](https://reader036.vdocument.in/reader036/viewer/2022071009/5fc77ab46f3ca2019f24f3ac/html5/thumbnails/36.jpg)
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
9
![Page 37: Tree Recursion - University of California, Berkeleycs61a/fa18/assets/... · (Demo) • If two implementations are equally clear, then shorter is usually better • In this case, the](https://reader036.vdocument.in/reader036/viewer/2022071009/5fc77ab46f3ca2019f24f3ac/html5/thumbnails/37.jpg)
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
9
![Page 38: Tree Recursion - University of California, Berkeleycs61a/fa18/assets/... · (Demo) • If two implementations are equally clear, then shorter is usually better • In this case, the](https://reader036.vdocument.in/reader036/viewer/2022071009/5fc77ab46f3ca2019f24f3ac/html5/thumbnails/38.jpg)
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
9
![Page 39: Tree Recursion - University of California, Berkeleycs61a/fa18/assets/... · (Demo) • If two implementations are equally clear, then shorter is usually better • In this case, the](https://reader036.vdocument.in/reader036/viewer/2022071009/5fc77ab46f3ca2019f24f3ac/html5/thumbnails/39.jpg)
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
9
![Page 40: Tree Recursion - University of California, Berkeleycs61a/fa18/assets/... · (Demo) • If two implementations are equally clear, then shorter is usually better • In this case, the](https://reader036.vdocument.in/reader036/viewer/2022071009/5fc77ab46f3ca2019f24f3ac/html5/thumbnails/40.jpg)
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
9
![Page 41: Tree Recursion - University of California, Berkeleycs61a/fa18/assets/... · (Demo) • If two implementations are equally clear, then shorter is usually better • In this case, the](https://reader036.vdocument.in/reader036/viewer/2022071009/5fc77ab46f3ca2019f24f3ac/html5/thumbnails/41.jpg)
A Tree-Recursive Process
The computational process of fib evolves into a tree structure
10
![Page 42: Tree Recursion - University of California, Berkeleycs61a/fa18/assets/... · (Demo) • If two implementations are equally clear, then shorter is usually better • In this case, the](https://reader036.vdocument.in/reader036/viewer/2022071009/5fc77ab46f3ca2019f24f3ac/html5/thumbnails/42.jpg)
A Tree-Recursive Process
The computational process of fib evolves into a tree structure
10
fib(5)
![Page 43: Tree Recursion - University of California, Berkeleycs61a/fa18/assets/... · (Demo) • If two implementations are equally clear, then shorter is usually better • In this case, the](https://reader036.vdocument.in/reader036/viewer/2022071009/5fc77ab46f3ca2019f24f3ac/html5/thumbnails/43.jpg)
A Tree-Recursive Process
The computational process of fib evolves into a tree structure
10
fib(5)
fib(3)
![Page 44: Tree Recursion - University of California, Berkeleycs61a/fa18/assets/... · (Demo) • If two implementations are equally clear, then shorter is usually better • In this case, the](https://reader036.vdocument.in/reader036/viewer/2022071009/5fc77ab46f3ca2019f24f3ac/html5/thumbnails/44.jpg)
A Tree-Recursive Process
The computational process of fib evolves into a tree structure
10
fib(5)
fib(4)fib(3)
![Page 45: Tree Recursion - University of California, Berkeleycs61a/fa18/assets/... · (Demo) • If two implementations are equally clear, then shorter is usually better • In this case, the](https://reader036.vdocument.in/reader036/viewer/2022071009/5fc77ab46f3ca2019f24f3ac/html5/thumbnails/45.jpg)
A Tree-Recursive Process
The computational process of fib evolves into a tree structure
10
fib(5)
fib(4)fib(3)
fib(1)
1
fib(2)
fib(0) fib(1)
0 1
![Page 46: Tree Recursion - University of California, Berkeleycs61a/fa18/assets/... · (Demo) • If two implementations are equally clear, then shorter is usually better • In this case, the](https://reader036.vdocument.in/reader036/viewer/2022071009/5fc77ab46f3ca2019f24f3ac/html5/thumbnails/46.jpg)
A Tree-Recursive Process
The computational process of fib evolves into a tree structure
10
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 47: Tree Recursion - University of California, Berkeleycs61a/fa18/assets/... · (Demo) • If two implementations are equally clear, then shorter is usually better • In this case, the](https://reader036.vdocument.in/reader036/viewer/2022071009/5fc77ab46f3ca2019f24f3ac/html5/thumbnails/47.jpg)
A Tree-Recursive Process
The computational process of fib evolves into a tree structure
10
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 48: Tree Recursion - University of California, Berkeleycs61a/fa18/assets/... · (Demo) • If two implementations are equally clear, then shorter is usually better • In this case, the](https://reader036.vdocument.in/reader036/viewer/2022071009/5fc77ab46f3ca2019f24f3ac/html5/thumbnails/48.jpg)
A Tree-Recursive Process
The computational process of fib evolves into a tree structure
10
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 49: Tree Recursion - University of California, Berkeleycs61a/fa18/assets/... · (Demo) • If two implementations are equally clear, then shorter is usually better • In this case, the](https://reader036.vdocument.in/reader036/viewer/2022071009/5fc77ab46f3ca2019f24f3ac/html5/thumbnails/49.jpg)
A Tree-Recursive Process
The computational process of fib evolves into a tree structure
10
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 50: Tree Recursion - University of California, Berkeleycs61a/fa18/assets/... · (Demo) • If two implementations are equally clear, then shorter is usually better • In this case, the](https://reader036.vdocument.in/reader036/viewer/2022071009/5fc77ab46f3ca2019f24f3ac/html5/thumbnails/50.jpg)
A Tree-Recursive Process
The computational process of fib evolves into a tree structure
10
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 51: Tree Recursion - University of California, Berkeleycs61a/fa18/assets/... · (Demo) • If two implementations are equally clear, then shorter is usually better • In this case, the](https://reader036.vdocument.in/reader036/viewer/2022071009/5fc77ab46f3ca2019f24f3ac/html5/thumbnails/51.jpg)
A Tree-Recursive Process
The computational process of fib evolves into a tree structure
10
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 Recursion - University of California, Berkeleycs61a/fa18/assets/... · (Demo) • If two implementations are equally clear, then shorter is usually better • In this case, the](https://reader036.vdocument.in/reader036/viewer/2022071009/5fc77ab46f3ca2019f24f3ac/html5/thumbnails/52.jpg)
A Tree-Recursive Process
The computational process of fib evolves into a tree structure
10
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 Recursion - University of California, Berkeleycs61a/fa18/assets/... · (Demo) • If two implementations are equally clear, then shorter is usually better • In this case, the](https://reader036.vdocument.in/reader036/viewer/2022071009/5fc77ab46f3ca2019f24f3ac/html5/thumbnails/53.jpg)
A Tree-Recursive Process
The computational process of fib evolves into a tree structure
10
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 Recursion - University of California, Berkeleycs61a/fa18/assets/... · (Demo) • If two implementations are equally clear, then shorter is usually better • In this case, the](https://reader036.vdocument.in/reader036/viewer/2022071009/5fc77ab46f3ca2019f24f3ac/html5/thumbnails/54.jpg)
A Tree-Recursive Process
The computational process of fib evolves into a tree structure
10
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 Recursion - University of California, Berkeleycs61a/fa18/assets/... · (Demo) • If two implementations are equally clear, then shorter is usually better • In this case, the](https://reader036.vdocument.in/reader036/viewer/2022071009/5fc77ab46f3ca2019f24f3ac/html5/thumbnails/55.jpg)
A Tree-Recursive Process
The computational process of fib evolves into a tree structure
10
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 Recursion - University of California, Berkeleycs61a/fa18/assets/... · (Demo) • If two implementations are equally clear, then shorter is usually better • In this case, the](https://reader036.vdocument.in/reader036/viewer/2022071009/5fc77ab46f3ca2019f24f3ac/html5/thumbnails/56.jpg)
A Tree-Recursive Process
The computational process of fib evolves into a tree structure
10
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 Recursion - University of California, Berkeleycs61a/fa18/assets/... · (Demo) • If two implementations are equally clear, then shorter is usually better • In this case, the](https://reader036.vdocument.in/reader036/viewer/2022071009/5fc77ab46f3ca2019f24f3ac/html5/thumbnails/57.jpg)
A Tree-Recursive Process
The computational process of fib evolves into a tree structure
10
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 Recursion - University of California, Berkeleycs61a/fa18/assets/... · (Demo) • If two implementations are equally clear, then shorter is usually better • In this case, the](https://reader036.vdocument.in/reader036/viewer/2022071009/5fc77ab46f3ca2019f24f3ac/html5/thumbnails/58.jpg)
A Tree-Recursive Process
The computational process of fib evolves into a tree structure
10
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 Recursion - University of California, Berkeleycs61a/fa18/assets/... · (Demo) • If two implementations are equally clear, then shorter is usually better • In this case, the](https://reader036.vdocument.in/reader036/viewer/2022071009/5fc77ab46f3ca2019f24f3ac/html5/thumbnails/59.jpg)
A Tree-Recursive Process
The computational process of fib evolves into a tree structure
10
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 Recursion - University of California, Berkeleycs61a/fa18/assets/... · (Demo) • If two implementations are equally clear, then shorter is usually better • In this case, the](https://reader036.vdocument.in/reader036/viewer/2022071009/5fc77ab46f3ca2019f24f3ac/html5/thumbnails/60.jpg)
A Tree-Recursive Process
The computational process of fib evolves into a tree structure
10
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 Recursion - University of California, Berkeleycs61a/fa18/assets/... · (Demo) • If two implementations are equally clear, then shorter is usually better • In this case, the](https://reader036.vdocument.in/reader036/viewer/2022071009/5fc77ab46f3ca2019f24f3ac/html5/thumbnails/61.jpg)
A Tree-Recursive Process
The computational process of fib evolves into a tree structure
10
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 Recursion - University of California, Berkeleycs61a/fa18/assets/... · (Demo) • If two implementations are equally clear, then shorter is usually better • In this case, the](https://reader036.vdocument.in/reader036/viewer/2022071009/5fc77ab46f3ca2019f24f3ac/html5/thumbnails/62.jpg)
A Tree-Recursive Process
The computational process of fib evolves into a tree structure
10
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 Recursion - University of California, Berkeleycs61a/fa18/assets/... · (Demo) • If two implementations are equally clear, then shorter is usually better • In this case, the](https://reader036.vdocument.in/reader036/viewer/2022071009/5fc77ab46f3ca2019f24f3ac/html5/thumbnails/63.jpg)
A Tree-Recursive Process
The computational process of fib evolves into a tree structure
10
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 64: Tree Recursion - University of California, Berkeleycs61a/fa18/assets/... · (Demo) • If two implementations are equally clear, then shorter is usually better • In this case, the](https://reader036.vdocument.in/reader036/viewer/2022071009/5fc77ab46f3ca2019f24f3ac/html5/thumbnails/64.jpg)
Repetition in Tree-Recursive Computation
11
![Page 65: Tree Recursion - University of California, Berkeleycs61a/fa18/assets/... · (Demo) • If two implementations are equally clear, then shorter is usually better • In this case, the](https://reader036.vdocument.in/reader036/viewer/2022071009/5fc77ab46f3ca2019f24f3ac/html5/thumbnails/65.jpg)
Repetition in Tree-Recursive Computation
This process is highly repetitive; fib is called on the same argument multiple times
11
![Page 66: Tree Recursion - University of California, Berkeleycs61a/fa18/assets/... · (Demo) • If two implementations are equally clear, then shorter is usually better • In this case, the](https://reader036.vdocument.in/reader036/viewer/2022071009/5fc77ab46f3ca2019f24f3ac/html5/thumbnails/66.jpg)
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
11
![Page 67: Tree Recursion - University of California, Berkeleycs61a/fa18/assets/... · (Demo) • If two implementations are equally clear, then shorter is usually better • In this case, the](https://reader036.vdocument.in/reader036/viewer/2022071009/5fc77ab46f3ca2019f24f3ac/html5/thumbnails/67.jpg)
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
11
(We will speed up this computation dramatically in a few weeks by remembering results)
![Page 68: Tree Recursion - University of California, Berkeleycs61a/fa18/assets/... · (Demo) • If two implementations are equally clear, then shorter is usually better • In this case, the](https://reader036.vdocument.in/reader036/viewer/2022071009/5fc77ab46f3ca2019f24f3ac/html5/thumbnails/68.jpg)
Example: Counting Partitions
![Page 69: Tree Recursion - University of California, Berkeleycs61a/fa18/assets/... · (Demo) • If two implementations are equally clear, then shorter is usually better • In this case, the](https://reader036.vdocument.in/reader036/viewer/2022071009/5fc77ab46f3ca2019f24f3ac/html5/thumbnails/69.jpg)
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.
13
![Page 70: Tree Recursion - University of California, Berkeleycs61a/fa18/assets/... · (Demo) • If two implementations are equally clear, then shorter is usually better • In this case, the](https://reader036.vdocument.in/reader036/viewer/2022071009/5fc77ab46f3ca2019f24f3ac/html5/thumbnails/70.jpg)
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.
13
count_partitions(6, 4)
![Page 71: Tree Recursion - University of California, Berkeleycs61a/fa18/assets/... · (Demo) • If two implementations are equally clear, then shorter is usually better • In this case, the](https://reader036.vdocument.in/reader036/viewer/2022071009/5fc77ab46f3ca2019f24f3ac/html5/thumbnails/71.jpg)
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.
13
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 72: Tree Recursion - University of California, Berkeleycs61a/fa18/assets/... · (Demo) • If two implementations are equally clear, then shorter is usually better • In this case, the](https://reader036.vdocument.in/reader036/viewer/2022071009/5fc77ab46f3ca2019f24f3ac/html5/thumbnails/72.jpg)
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.
13
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 73: Tree Recursion - University of California, Berkeleycs61a/fa18/assets/... · (Demo) • If two implementations are equally clear, then shorter is usually better • In this case, the](https://reader036.vdocument.in/reader036/viewer/2022071009/5fc77ab46f3ca2019f24f3ac/html5/thumbnails/73.jpg)
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.
13
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 74: Tree Recursion - University of California, Berkeleycs61a/fa18/assets/... · (Demo) • If two implementations are equally clear, then shorter is usually better • In this case, the](https://reader036.vdocument.in/reader036/viewer/2022071009/5fc77ab46f3ca2019f24f3ac/html5/thumbnails/74.jpg)
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.
13
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 75: Tree Recursion - University of California, Berkeleycs61a/fa18/assets/... · (Demo) • If two implementations are equally clear, then shorter is usually better • In this case, the](https://reader036.vdocument.in/reader036/viewer/2022071009/5fc77ab46f3ca2019f24f3ac/html5/thumbnails/75.jpg)
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.
14
count_partitions(6, 4)
![Page 76: Tree Recursion - University of California, Berkeleycs61a/fa18/assets/... · (Demo) • If two implementations are equally clear, then shorter is usually better • In this case, the](https://reader036.vdocument.in/reader036/viewer/2022071009/5fc77ab46f3ca2019f24f3ac/html5/thumbnails/76.jpg)
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.
14
• Recursive decomposition: finding simpler instances of the problem.
count_partitions(6, 4)
![Page 77: Tree Recursion - University of California, Berkeleycs61a/fa18/assets/... · (Demo) • If two implementations are equally clear, then shorter is usually better • In this case, the](https://reader036.vdocument.in/reader036/viewer/2022071009/5fc77ab46f3ca2019f24f3ac/html5/thumbnails/77.jpg)
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.
14
• Recursive decomposition: finding simpler instances of the problem.
• Explore two possibilities:
count_partitions(6, 4)
![Page 78: Tree Recursion - University of California, Berkeleycs61a/fa18/assets/... · (Demo) • If two implementations are equally clear, then shorter is usually better • In this case, the](https://reader036.vdocument.in/reader036/viewer/2022071009/5fc77ab46f3ca2019f24f3ac/html5/thumbnails/78.jpg)
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.
14
• Recursive decomposition: finding simpler instances of the problem.
• Explore two possibilities:
•Use at least one 4
count_partitions(6, 4)
![Page 79: Tree Recursion - University of California, Berkeleycs61a/fa18/assets/... · (Demo) • If two implementations are equally clear, then shorter is usually better • In this case, the](https://reader036.vdocument.in/reader036/viewer/2022071009/5fc77ab46f3ca2019f24f3ac/html5/thumbnails/79.jpg)
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.
14
• 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 80: Tree Recursion - University of California, Berkeleycs61a/fa18/assets/... · (Demo) • If two implementations are equally clear, then shorter is usually better • In this case, the](https://reader036.vdocument.in/reader036/viewer/2022071009/5fc77ab46f3ca2019f24f3ac/html5/thumbnails/80.jpg)
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.
14
• 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 81: Tree Recursion - University of California, Berkeleycs61a/fa18/assets/... · (Demo) • If two implementations are equally clear, then shorter is usually better • In this case, the](https://reader036.vdocument.in/reader036/viewer/2022071009/5fc77ab46f3ca2019f24f3ac/html5/thumbnails/81.jpg)
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.
14
• 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 82: Tree Recursion - University of California, Berkeleycs61a/fa18/assets/... · (Demo) • If two implementations are equally clear, then shorter is usually better • In this case, the](https://reader036.vdocument.in/reader036/viewer/2022071009/5fc77ab46f3ca2019f24f3ac/html5/thumbnails/82.jpg)
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.
14
• 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 83: Tree Recursion - University of California, Berkeleycs61a/fa18/assets/... · (Demo) • If two implementations are equally clear, then shorter is usually better • In this case, the](https://reader036.vdocument.in/reader036/viewer/2022071009/5fc77ab46f3ca2019f24f3ac/html5/thumbnails/83.jpg)
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.
14
• 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 84: Tree Recursion - University of California, Berkeleycs61a/fa18/assets/... · (Demo) • If two implementations are equally clear, then shorter is usually better • In this case, the](https://reader036.vdocument.in/reader036/viewer/2022071009/5fc77ab46f3ca2019f24f3ac/html5/thumbnails/84.jpg)
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.
14
• 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 85: Tree Recursion - University of California, Berkeleycs61a/fa18/assets/... · (Demo) • If two implementations are equally clear, then shorter is usually better • In this case, the](https://reader036.vdocument.in/reader036/viewer/2022071009/5fc77ab46f3ca2019f24f3ac/html5/thumbnails/85.jpg)
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.
14
• 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 86: Tree Recursion - University of California, Berkeleycs61a/fa18/assets/... · (Demo) • If two implementations are equally clear, then shorter is usually better • In this case, the](https://reader036.vdocument.in/reader036/viewer/2022071009/5fc77ab46f3ca2019f24f3ac/html5/thumbnails/86.jpg)
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.
14
• 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 87: Tree Recursion - University of California, Berkeleycs61a/fa18/assets/... · (Demo) • If two implementations are equally clear, then shorter is usually better • In this case, the](https://reader036.vdocument.in/reader036/viewer/2022071009/5fc77ab46f3ca2019f24f3ac/html5/thumbnails/87.jpg)
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.
14
• 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 88: Tree Recursion - University of California, Berkeleycs61a/fa18/assets/... · (Demo) • If two implementations are equally clear, then shorter is usually better • In this case, the](https://reader036.vdocument.in/reader036/viewer/2022071009/5fc77ab46f3ca2019f24f3ac/html5/thumbnails/88.jpg)
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.
14
• 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 89: Tree Recursion - University of California, Berkeleycs61a/fa18/assets/... · (Demo) • If two implementations are equally clear, then shorter is usually better • In this case, the](https://reader036.vdocument.in/reader036/viewer/2022071009/5fc77ab46f3ca2019f24f3ac/html5/thumbnails/89.jpg)
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.
14
• 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 90: Tree Recursion - University of California, Berkeleycs61a/fa18/assets/... · (Demo) • If two implementations are equally clear, then shorter is usually better • In this case, the](https://reader036.vdocument.in/reader036/viewer/2022071009/5fc77ab46f3ca2019f24f3ac/html5/thumbnails/90.jpg)
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.
14
• 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 91: Tree Recursion - University of California, Berkeleycs61a/fa18/assets/... · (Demo) • If two implementations are equally clear, then shorter is usually better • In this case, the](https://reader036.vdocument.in/reader036/viewer/2022071009/5fc77ab46f3ca2019f24f3ac/html5/thumbnails/91.jpg)
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.
15
• 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 92: Tree Recursion - University of California, Berkeleycs61a/fa18/assets/... · (Demo) • If two implementations are equally clear, then shorter is usually better • In this case, the](https://reader036.vdocument.in/reader036/viewer/2022071009/5fc77ab46f3ca2019f24f3ac/html5/thumbnails/92.jpg)
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.
15
• 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 93: Tree Recursion - University of California, Berkeleycs61a/fa18/assets/... · (Demo) • If two implementations are equally clear, then shorter is usually better • In this case, the](https://reader036.vdocument.in/reader036/viewer/2022071009/5fc77ab46f3ca2019f24f3ac/html5/thumbnails/93.jpg)
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.
15
• 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 94: Tree Recursion - University of California, Berkeleycs61a/fa18/assets/... · (Demo) • If two implementations are equally clear, then shorter is usually better • In this case, the](https://reader036.vdocument.in/reader036/viewer/2022071009/5fc77ab46f3ca2019f24f3ac/html5/thumbnails/94.jpg)
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.
15
• 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 95: Tree Recursion - University of California, Berkeleycs61a/fa18/assets/... · (Demo) • If two implementations are equally clear, then shorter is usually better • In this case, the](https://reader036.vdocument.in/reader036/viewer/2022071009/5fc77ab46f3ca2019f24f3ac/html5/thumbnails/95.jpg)
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.
15
• 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 96: Tree Recursion - University of California, Berkeleycs61a/fa18/assets/... · (Demo) • If two implementations are equally clear, then shorter is usually better • In this case, the](https://reader036.vdocument.in/reader036/viewer/2022071009/5fc77ab46f3ca2019f24f3ac/html5/thumbnails/96.jpg)
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.
15
• 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 97: Tree Recursion - University of California, Berkeleycs61a/fa18/assets/... · (Demo) • If two implementations are equally clear, then shorter is usually better • In this case, the](https://reader036.vdocument.in/reader036/viewer/2022071009/5fc77ab46f3ca2019f24f3ac/html5/thumbnails/97.jpg)
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.
15
• 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 98: Tree Recursion - University of California, Berkeleycs61a/fa18/assets/... · (Demo) • If two implementations are equally clear, then shorter is usually better • In this case, the](https://reader036.vdocument.in/reader036/viewer/2022071009/5fc77ab46f3ca2019f24f3ac/html5/thumbnails/98.jpg)
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.
15
• 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 99: Tree Recursion - University of California, Berkeleycs61a/fa18/assets/... · (Demo) • If two implementations are equally clear, then shorter is usually better • In this case, the](https://reader036.vdocument.in/reader036/viewer/2022071009/5fc77ab46f3ca2019f24f3ac/html5/thumbnails/99.jpg)
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.
15
• 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 100: Tree Recursion - University of California, Berkeleycs61a/fa18/assets/... · (Demo) • If two implementations are equally clear, then shorter is usually better • In this case, the](https://reader036.vdocument.in/reader036/viewer/2022071009/5fc77ab46f3ca2019f24f3ac/html5/thumbnails/100.jpg)
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.
15
• 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 101: Tree Recursion - University of California, Berkeleycs61a/fa18/assets/... · (Demo) • If two implementations are equally clear, then shorter is usually better • In this case, the](https://reader036.vdocument.in/reader036/viewer/2022071009/5fc77ab46f3ca2019f24f3ac/html5/thumbnails/101.jpg)
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.
15
• 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 102: Tree Recursion - University of California, Berkeleycs61a/fa18/assets/... · (Demo) • If two implementations are equally clear, then shorter is usually better • In this case, the](https://reader036.vdocument.in/reader036/viewer/2022071009/5fc77ab46f3ca2019f24f3ac/html5/thumbnails/102.jpg)
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.
15
• 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 103: Tree Recursion - University of California, Berkeleycs61a/fa18/assets/... · (Demo) • If two implementations are equally clear, then shorter is usually better • In this case, the](https://reader036.vdocument.in/reader036/viewer/2022071009/5fc77ab46f3ca2019f24f3ac/html5/thumbnails/103.jpg)
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.
15
• 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 104: Tree Recursion - University of California, Berkeleycs61a/fa18/assets/... · (Demo) • If two implementations are equally clear, then shorter is usually better • In this case, the](https://reader036.vdocument.in/reader036/viewer/2022071009/5fc77ab46f3ca2019f24f3ac/html5/thumbnails/104.jpg)
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.
15
• 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 105: Tree Recursion - University of California, Berkeleycs61a/fa18/assets/... · (Demo) • If two implementations are equally clear, then shorter is usually better • In this case, the](https://reader036.vdocument.in/reader036/viewer/2022071009/5fc77ab46f3ca2019f24f3ac/html5/thumbnails/105.jpg)
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.
15
• 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)
Interactive Diagram