RecursionTrees - ETH Z · Fibonacci - Recursion Tree 12 fib(4): Fibonacci-number VS functioncalls...
Transcript of RecursionTrees - ETH Z · Fibonacci - Recursion Tree 12 fib(4): Fibonacci-number VS functioncalls...
![Page 1: RecursionTrees - ETH Z · Fibonacci - Recursion Tree 12 fib(4): Fibonacci-number VS functioncalls n=4: 3 9 1 0 1 1 0 Many more functioncalls! Fibonaccigrowth: fib(n)~𝑐𝑛 (for](https://reader033.fdocuments.net/reader033/viewer/2022060509/5f24c2209310cf0aa079cd50/html5/thumbnails/1.jpg)
Recursion Trees
![Page 2: RecursionTrees - ETH Z · Fibonacci - Recursion Tree 12 fib(4): Fibonacci-number VS functioncalls n=4: 3 9 1 0 1 1 0 Many more functioncalls! Fibonaccigrowth: fib(n)~𝑐𝑛 (for](https://reader033.fdocuments.net/reader033/viewer/2022060509/5f24c2209310cf0aa079cd50/html5/thumbnails/2.jpg)
Recursion Trees
•Visualize call structure
2
![Page 3: RecursionTrees - ETH Z · Fibonacci - Recursion Tree 12 fib(4): Fibonacci-number VS functioncalls n=4: 3 9 1 0 1 1 0 Many more functioncalls! Fibonaccigrowth: fib(n)~𝑐𝑛 (for](https://reader033.fdocuments.net/reader033/viewer/2022060509/5f24c2209310cf0aa079cd50/html5/thumbnails/3.jpg)
Recursion Trees
•Visualize call structure
•Example: fnc(8)
3
unsigned int fnc (unsigned int n) {
...
return fnc(n/2) + fnc(n/2);
}
![Page 4: RecursionTrees - ETH Z · Fibonacci - Recursion Tree 12 fib(4): Fibonacci-number VS functioncalls n=4: 3 9 1 0 1 1 0 Many more functioncalls! Fibonaccigrowth: fib(n)~𝑐𝑛 (for](https://reader033.fdocuments.net/reader033/viewer/2022060509/5f24c2209310cf0aa079cd50/html5/thumbnails/4.jpg)
Fibonacci Tree Problem
![Page 5: RecursionTrees - ETH Z · Fibonacci - Recursion Tree 12 fib(4): Fibonacci-number VS functioncalls n=4: 3 9 1 0 1 1 0 Many more functioncalls! Fibonaccigrowth: fib(n)~𝑐𝑛 (for](https://reader033.fdocuments.net/reader033/viewer/2022060509/5f24c2209310cf0aa079cd50/html5/thumbnails/5.jpg)
Fibonacci - Recursion Tree
5
// POST: return value is the n-th
// Fibonacci number F(n)
ifmp::integer fib (const unsigned int n) {
if (n == 0) return 0;
if (n == 1) return 1;
return fib(n-1) + fib(n-2); // n > 1
}
fib(4):
![Page 6: RecursionTrees - ETH Z · Fibonacci - Recursion Tree 12 fib(4): Fibonacci-number VS functioncalls n=4: 3 9 1 0 1 1 0 Many more functioncalls! Fibonaccigrowth: fib(n)~𝑐𝑛 (for](https://reader033.fdocuments.net/reader033/viewer/2022060509/5f24c2209310cf0aa079cd50/html5/thumbnails/6.jpg)
Fibonacci - Recursion Tree
6
// POST: return value is the n-th
// Fibonacci number F(n)
ifmp::integer fib (const unsigned int n) {
if (n == 0) return 0;
if (n == 1) return 1;
return fib(n-1) + fib(n-2); // n > 1
}
fib(4):
1 0
1 1 0
![Page 7: RecursionTrees - ETH Z · Fibonacci - Recursion Tree 12 fib(4): Fibonacci-number VS functioncalls n=4: 3 9 1 0 1 1 0 Many more functioncalls! Fibonaccigrowth: fib(n)~𝑐𝑛 (for](https://reader033.fdocuments.net/reader033/viewer/2022060509/5f24c2209310cf0aa079cd50/html5/thumbnails/7.jpg)
Fibonacci - Recursion Tree
7
fib(4):
1 0
1 1 0
Fibonacci-number VS function calls
n=4:
![Page 8: RecursionTrees - ETH Z · Fibonacci - Recursion Tree 12 fib(4): Fibonacci-number VS functioncalls n=4: 3 9 1 0 1 1 0 Many more functioncalls! Fibonaccigrowth: fib(n)~𝑐𝑛 (for](https://reader033.fdocuments.net/reader033/viewer/2022060509/5f24c2209310cf0aa079cd50/html5/thumbnails/8.jpg)
Fibonacci - Recursion Tree
8
fib(4):
1 0
1 1 0
Fibonacci-number VS function calls
n=4: 3
![Page 9: RecursionTrees - ETH Z · Fibonacci - Recursion Tree 12 fib(4): Fibonacci-number VS functioncalls n=4: 3 9 1 0 1 1 0 Many more functioncalls! Fibonaccigrowth: fib(n)~𝑐𝑛 (for](https://reader033.fdocuments.net/reader033/viewer/2022060509/5f24c2209310cf0aa079cd50/html5/thumbnails/9.jpg)
Fibonacci - Recursion Tree
9
fib(4):
Fibonacci-number VS function calls
n=4: 3 9
1 0
1 1 0
![Page 10: RecursionTrees - ETH Z · Fibonacci - Recursion Tree 12 fib(4): Fibonacci-number VS functioncalls n=4: 3 9 1 0 1 1 0 Many more functioncalls! Fibonaccigrowth: fib(n)~𝑐𝑛 (for](https://reader033.fdocuments.net/reader033/viewer/2022060509/5f24c2209310cf0aa079cd50/html5/thumbnails/10.jpg)
Fibonacci - Recursion Tree
10
fib(4):
Fibonacci-number VS function calls
n=4: 3 9
1 0
1 1 0
Many morefunction calls!
![Page 11: RecursionTrees - ETH Z · Fibonacci - Recursion Tree 12 fib(4): Fibonacci-number VS functioncalls n=4: 3 9 1 0 1 1 0 Many more functioncalls! Fibonaccigrowth: fib(n)~𝑐𝑛 (for](https://reader033.fdocuments.net/reader033/viewer/2022060509/5f24c2209310cf0aa079cd50/html5/thumbnails/11.jpg)
Fibonacci - Recursion Tree
11
fib(4):
Fibonacci-number VS function calls
n=4: 3 9
1 0
1 1 0
Many morefunction calls!
Fibonacci growth:
fib(n)~ 𝑐𝑛
(for 𝑛 large, c golden ratio)
n fib(n)
5 5
10 55
20 6’765
40 102’334’155
80 23’416’728’348’467’685
![Page 12: RecursionTrees - ETH Z · Fibonacci - Recursion Tree 12 fib(4): Fibonacci-number VS functioncalls n=4: 3 9 1 0 1 1 0 Many more functioncalls! Fibonaccigrowth: fib(n)~𝑐𝑛 (for](https://reader033.fdocuments.net/reader033/viewer/2022060509/5f24c2209310cf0aa079cd50/html5/thumbnails/12.jpg)
Fibonacci - Recursion Tree
12
fib(4):
Fibonacci-number VS function calls
n=4: 3 9
1 0
1 1 0
Many morefunction calls!
Fibonacci growth:
fib(n)~ 𝑐𝑛
(for 𝑛 large, c golden ratio)
n fib(n)
5 5
10 55
20 6’765
40 102’334’155
80 23’416’728’348’467’685
Requiresunbelievably
many recursivecalls!
![Page 13: RecursionTrees - ETH Z · Fibonacci - Recursion Tree 12 fib(4): Fibonacci-number VS functioncalls n=4: 3 9 1 0 1 1 0 Many more functioncalls! Fibonaccigrowth: fib(n)~𝑐𝑛 (for](https://reader033.fdocuments.net/reader033/viewer/2022060509/5f24c2209310cf0aa079cd50/html5/thumbnails/13.jpg)
The Problem
•Problem: Same computation multiple times
13
![Page 14: RecursionTrees - ETH Z · Fibonacci - Recursion Tree 12 fib(4): Fibonacci-number VS functioncalls n=4: 3 9 1 0 1 1 0 Many more functioncalls! Fibonaccigrowth: fib(n)~𝑐𝑛 (for](https://reader033.fdocuments.net/reader033/viewer/2022060509/5f24c2209310cf0aa079cd50/html5/thumbnails/14.jpg)
The Problem
•Problem: Same computation multiple times
14
1 0
1 1 0
![Page 15: RecursionTrees - ETH Z · Fibonacci - Recursion Tree 12 fib(4): Fibonacci-number VS functioncalls n=4: 3 9 1 0 1 1 0 Many more functioncalls! Fibonaccigrowth: fib(n)~𝑐𝑛 (for](https://reader033.fdocuments.net/reader033/viewer/2022060509/5f24c2209310cf0aa079cd50/html5/thumbnails/15.jpg)
The Problem
•Problem: Same computation multiple times
•Gets worse as n increases :-(
15
1 0
1 1 0
![Page 16: RecursionTrees - ETH Z · Fibonacci - Recursion Tree 12 fib(4): Fibonacci-number VS functioncalls n=4: 3 9 1 0 1 1 0 Many more functioncalls! Fibonaccigrowth: fib(n)~𝑐𝑛 (for](https://reader033.fdocuments.net/reader033/viewer/2022060509/5f24c2209310cf0aa079cd50/html5/thumbnails/16.jpg)
The Problem
•Not all recursive functions are this inefficient.
16
![Page 17: RecursionTrees - ETH Z · Fibonacci - Recursion Tree 12 fib(4): Fibonacci-number VS functioncalls n=4: 3 9 1 0 1 1 0 Many more functioncalls! Fibonaccigrowth: fib(n)~𝑐𝑛 (for](https://reader033.fdocuments.net/reader033/viewer/2022060509/5f24c2209310cf0aa079cd50/html5/thumbnails/17.jpg)
The Problem
•Not all recursive functions are this inefficient.
•Example:
17
unsigned int fnc (unsigned int n) {
...
return fnc(n/2) + fnc(n/2);
}
![Page 18: RecursionTrees - ETH Z · Fibonacci - Recursion Tree 12 fib(4): Fibonacci-number VS functioncalls n=4: 3 9 1 0 1 1 0 Many more functioncalls! Fibonaccigrowth: fib(n)~𝑐𝑛 (for](https://reader033.fdocuments.net/reader033/viewer/2022060509/5f24c2209310cf0aa079cd50/html5/thumbnails/18.jpg)
The Problem
•Not all recursive functions are this inefficient.
•Example:
18
unsigned int fnc (unsigned int n) {
...
return fnc(n/2) + fnc(n/2);
}
2n - 1
recursive calls in total
![Page 19: RecursionTrees - ETH Z · Fibonacci - Recursion Tree 12 fib(4): Fibonacci-number VS functioncalls n=4: 3 9 1 0 1 1 0 Many more functioncalls! Fibonaccigrowth: fib(n)~𝑐𝑛 (for](https://reader033.fdocuments.net/reader033/viewer/2022060509/5f24c2209310cf0aa079cd50/html5/thumbnails/19.jpg)
The Problem
•Not all recursive functions are this inefficient.
•Example:
19
unsigned int fnc (unsigned int n) {
...
return fnc(n/2) + fnc(n/2);
}
2n - 1
recursive calls in total
Number of Recursive Calls
n fib fnc
5 > 5 9
10 > 55 19
20 > 6’765 39
40 > 102’334’155 79
80 > 23’416’728’348’467’685 159
![Page 20: RecursionTrees - ETH Z · Fibonacci - Recursion Tree 12 fib(4): Fibonacci-number VS functioncalls n=4: 3 9 1 0 1 1 0 Many more functioncalls! Fibonaccigrowth: fib(n)~𝑐𝑛 (for](https://reader033.fdocuments.net/reader033/viewer/2022060509/5f24c2209310cf0aa079cd50/html5/thumbnails/20.jpg)
The Problem
•Not all recursive functions are this inefficient.
•Example:
20
unsigned int fnc (unsigned int n) {
...
return fnc(n/2) + fnc(n/2);
}
2n - 1
recursive calls in total
Number of Recursive Calls
n fib fnc
5 > 5 9
10 > 55 19
20 > 6’765 39
40 > 102’334’155 79
80 > 23’416’728’348’467’685 159
Mindblowingdifference!
![Page 21: RecursionTrees - ETH Z · Fibonacci - Recursion Tree 12 fib(4): Fibonacci-number VS functioncalls n=4: 3 9 1 0 1 1 0 Many more functioncalls! Fibonaccigrowth: fib(n)~𝑐𝑛 (for](https://reader033.fdocuments.net/reader033/viewer/2022060509/5f24c2209310cf0aa079cd50/html5/thumbnails/21.jpg)
The Problem
•Reason: 𝑛/2 falls much faster than 𝑛 − 1 and 𝑛 − 2• 𝑛/2 sub-tree of height: log2(𝑛)• 𝑛 − 1, 𝑛 − 2 sub-tree of height: 𝑛 − 1, 𝑛 − 2
21
log28
=3