Other answers are correct, but they donโt make it clear - where is the big difference between the Fibonacci algorithm and the separation and subjugation algorithms? Indeed, the form of the recursion tree is the same for both classes of functions - it is a binary tree.
The trick to understanding is actually very simple: consider the size of the recursion tree as a function of input size n .
Recall some basic facts about binary trees :
- The number of leaves
n is a binary tree equal to the number of non-leaf nodes plus one. Therefore, the size of the binary tree is 2n-1. - In an ideal binary tree, all non-leaf nodes have two children.
- The height
h for an ideal binary tree with leaves n is log(n) for a random binary tree: h = O(log(n)) and for a degenerate binary tree h = n-1 .
Clearly:
To sort an array of elements n with a recursive algorithm, the recursion tree has n exits . It follows that the width of the tree is n , the height of the tree is O(log(n)) on average, and O(n) in the worst case.
To calculate an element of a sequence Fibonacci k with a recursive algorithm, the recursion tree has k levels (to understand why, consider fib(k) calls fib(k-1) , which calls fib(k-2) , etc.). It follows that the height of the tree is k . To evaluate the lower bound on the width and number of nodes in the recursion tree, keep in mind that since fib(k) also calls fib(k-2) , therefore there is an ideal binary tree of height k/2 in the recursion tree. If it is removed, this ideal subtree will have 2 k / 2 leaf nodes. Thus, the width of the recursion tree is at least O(2^{k/2}) or, equivalently, 2^O(k) .
The most important difference is that:
- for division and conquest algorithms, the input size is the width of the binary tree.
- for the Fibonacci algorithm, the input size is the height of the tree.
Therefore, the number of nodes in the tree is O(n) in the first case, but 2^O(n) in the second. The Fibonacci tree is much larger compared to the size of the input.
You mentioned the Master Theorem ; however, the theorem cannot be applied to analyze the Fibonacci complexity, since it only applies to algorithms where the input is actually divided at each recursion level. Fibonacci does not share the entrance; in fact, functions at level i produce almost twice as much input for the next level i+1 .
source share