The efficiency of the "algorithm" Fibonacci sequence

Source: Internet
Author: User
Tags repetition

Click this link: (Fibonacci series) is a very well-known form of a series in mathematics, whether it is the mathematical circle or the programming circle is not all in it to explain the idea of recursive invocation, the mathematical formula is as follows:

The Fibonacci Sequence program is now programmed to get the value of the Fibonacci sequence in the nth position by passing in the value N.

As explained in the textbook, by "recursive invocation" to implement the method, the code implementation can be designed to:

Public Long Fibonacci (int n)
{
	if (n <= 0)
		return 0;
	if (n = = 1)
		return 1;
        Recursive implementation calls
      return Fibonacci (n-1) + Fibonacci (n-2);
}
Basically three lines of code, the implementation of this classic sequence algorithm, but if asked a "recursive" is the best strategy to implement this algorithm. The answer is not necessarily.


As the figure shows, the schematic diagram of Fibonacci is achieved by recursion:


As the diagram shows, only the schematic of the calculation of F (10), and only the 4 layers, there is a lot of repetition of the same result, if the calculation f (100), this simple recursive implementation will have serious performance problems. That is, as the value of n increases, the number of repetitions increases sharply in order to find the final f (n). The time complexity of using recursion to calculate this method is incremented in exponential way of N.


Problem Analysis:

(1) The recursive call to achieve "Fibonacci sequence" slow, because the calculation process of a large number of intermediate volume of repetition, the reason for repetition, in the calculation of the process from the top down, the calculation of each number, combined with the above, all need to go down the layer to request a complete two-fork tree values, similar to high school physics learned " The chain reaction of nuclear fission ", that's the problem.


Solution Ideas:

(1) In conjunction with the above diagram, the use of a bottom-up calculation strategy, you can ensure that the same number does not need to be repeated to calculate, because the middle of the variable value, has been calculated and saved, it could be quickly used, if the search does not exist, then to calculate. F (2) is calculated from F (0) and F (1), and F (2) is calculated from F (1) and F (3) ... and finally down, the time complexity of the calculation is O (n).


Optimization scenarios:

Public Long Fibonacci (int n)
{
	int result[2] = {0,1}
	if (n < 2)
		return result[n];
		
	Long fibnminusone = 1;
	Long fibnminustwo = 0;
	Long fibn = 0;
	
	for (int i=2; i <= n; i++)
	{
		fibn = Fibnminusone + fibnminustwo;
		Fibnminustwo = Fibnminusone;
		Fibnminusone = fibn;
	}
	
	return fibn;
}

As shown in the code above, combined with the "Fibonacci sequence" schematic, is no longer calculated from the top down, but from the bottom up, which will omit a large number of intermediate repetitions of the calculation process, but also optimize the "recursive" implementation of the results of the method.


Written at the end:

The "Fibonacci" realization of the graph, known as the golden Ratio graph, is beautiful, as pictured is the Fibonacci Helix:


That's all.



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.