8

In the classic text by Abelson/Sussman, Structure and Interpretation of Computer Programs, in Section 1.2.2 on tree recursion and the Fibonacci sequence, they show this image:

The tree-recursive process generated in computing for the 5th Fibonacci number

The tree-recursive process generated in computing for the 5th Fibonacci number

Then they write: "Notice that the entire computation of (fib 3) - almost half the work - is duplicated. In fact, it is not hard to show that the number of times the procedure will compute (fib 1) or (fib 0) (the number of leaves in the above tree, in general) is precisely Fib(n + 1)."

I understand that they're making a point about tree-recursion and how this classic case of the Fibonacci tree-recursion is inefficient because the recursive function calls itself twice:

The tree-recursive function for computing a Fibonacci number

The tree-recursive function for computing a Fibonacci number

My question is, why is it obvious (i.e. "not hard to show") that the number of leaves is equal to the next Fibonacci number in the sequence? I can see visually that it is the case, but I'm not seeing the connection as to why the number of leaves (the reduced down fib 1 and fib 0 calculations) should be an indicator for the next Fibonacci number (in this case 8, which is Fib 6, i.e. the 6th Fibonacci number, i.e. Fib n+1 where n is 5).

It is obvious how the Fibonacci sequence is computed - the sum of the previous two numbers in the sequence yields the current number, but why does the number of leaves precisely equal the next number in the sequence? What is the connection there (other than the obvious, that looking at it and adding up the 1 and 0 leaves does, in fact, yield a total count of 8 in this case, which is the next (6th) Fibonacci number, and so on)?

sicpfan
  • 81
  • 3

5 Answers5

5

"Not hard to show" is harder than "obvious".

Use induction with two base cases.
Let's call the number of computations in Fib(x), Fib01(x).
Then,

Fib01(0) = 1 by definition, which is Fib(1) 
Fib01(1) = 1 by definition, which is Fib(2)

Now assume that Fib01(k) = Fib(k+1) for k < n:

Fib01(n) = Fib01(n-1) + Fib01(n-2) 
         = Fib(n) + Fib(n-1) 
         = Fib(n+1) by definition

QED.

molbdnilo
  • 64,751
  • 3
  • 43
  • 82
4

The number of n=1 clauses must be equal to fib(n), because that is the only place a non-zero number comes from, and if the sum of some number of 1s is equal to fib(n), there must be fib(n) of them.

Since fib(n+1) = fib(n) + fib(n-1), we just need to show that there are fib(n-1) leaves computing fib(0). It's less obvious to me how to show this, but perhaps it falls inductively out of the previous case?


Perhaps a simpler approach is to just do the whole thing inductively, then.

For our base cases:

  • N=0: there are fib(N+1)=fib(1)=1 leaves in the tree. Proof by inspection.
  • N=1: there are fib(N+1)=fib(2)=1 leaves in the tree. Proof by inspection.

Induction step: to compute fib(N) for an arbitrary N, we compute fib(N-1) once, and fib(N-2) once, and add their results. By induction, there are fib(N) leaves in the tree coming from our computation of fib(N-1), and fib(N-1) leaves in the tree coming from our computation of fib(N-2).

There are therefore fib(N) + fib(N-1) leaves in our overall tree, which is equal to fib(N+1). QED.

amalloy
  • 89,153
  • 8
  • 140
  • 205
2

We can prove this by extrapolation.

The number of leaves for Fib(0) = 1. The number of leaves for Fib(1) = 1.

Now, the expression Fib(2) is basically the sum of Fib(1) + Fib(0), i.e., Fib(2) = Fib(1) + Fib(0). So from the tree itself, you can see that the number of leaves for Fib(2) is equal to the sum of leaves in case of Fib(1) and Fib(0). Therefore, the number of leaves for Fib(2) is equal to 2.

Next, for Fib(3) the number of leaves will be sum of leaves for Fib(2) and Fib(1), i.e., 2 + 1 = 3

As you must have observed by now, this follows a pattern similar to Fibonacci series. Infact if we define the number of leaves for Fib(n) to be FibLeaves(n), then we can see that this series is Fib(n) shifted left by 1 space.

Fib(n) = 0, 1, 1, 2, 3, 5, 8, 13, 21, ..

FibLeaves(n) = 1, 1, 2, 3, 5, 8, 13, 21, ..

And thus, the number of leaves will be equal to Fib(n + 1)

Ayush Poddar
  • 106
  • 5
1

Look at it this way:

  1. It is true that we can generate a part of the fibonnaci sequence by picking any two consecutive terms from anywhere in the full fibonnaci sequence and following the rules of generating the next term

i.e Full fibonnaci sequence = 0,1,1,2,3,5,8,13,21....

So if I picked any two consecutive terms e.x 3 and 5, and followed the rules of generating the next term in the sequence, I would generate a part of the fibonnaci sequence

i.e Part of fibonnaci sequence = 3,5,8,13,21,34...

  1. It is true that the number of leaves for a term is equal to the sum of the number of leaves of its two previous terms. This rule is the same as that for generating a term in the fibonnaci sequence

So lets try to get the number of leaves for the second term i.e number of leaves for zeroth term + number of leaves for first term

The number of leaves for the zeroth term and first term is 1

The number of leaves for the second term becomes 1 + 1 = 2

Now, 1,1 are consecutive terms from the full fibonacci sequence and the rule for getting the number of leaves is the same as the rule for getting a term in the fibonacci sequence

Israel
  • 77
  • 7
0

Nice question! It is immediately obvious (after a moment's thought), because the number of leaves in a binary tree node is the sum of respective numbers for its two branches, and, vacuously, 1 for leaves -- which is the definition of Fibonacci numbers ... with this specific shape of a tree.

Implicit in the above imprecise general statement is the proof by induction that

   N(0) = 1
   N(1) = 1
   N(n+2) = N(n+1) + N(n)

which directly maps onto that statement, making it specific and concrete!

Will Ness
  • 70,110
  • 9
  • 98
  • 181