The Fibonacci sequence still has this kind of notation, you know?

Source: Internet
Author: User
Tags repetition
Baidu under the "Fibonacci retracement", there are many answers, but not satisfactory, the first is too difficult to understand the copy, followed by performance and recursion almost.

when it comes to the Fibonacci sequence, whether it's a novice program or a skilled veteran, the first thing to think about is a recursive notation. Then, the technical veteran and the program rookie different place, will think of the recursive results saved up to reduce the repetition of the calculation. These are normal operations, but have you ever thought that the Fibonacci sequence can be written in a non-recursive way?
Baidu under the "Fibonacci retracement", there are many answers, but not satisfactory, the first is too difficult to understand the copy, followed by performance and recursion almost. At first I also wanted to write myself, as long as the call stack to simulate recursive calls is not OK, but the idea is a bit of a take for granted, the program is also very complex. What do we do? At this point, the tree's depth-first traversal can be useful.
First, we define the tree nodes:
public class node        {public            node (long value, bool visited)            {                value = value;                visited = visited;            }            Public long Value {get; set;} Store the value of the node public            bool visited {get; set;}        }

And then we'll be happy on the ground DFS stack notation

  public static long FBLC (int n) {stack<node> s = new stack<node> ();            S.push (New Node (n, false));            Long sum = 0;            long[] Childrenresultmemo = new long[n+1];            Childrenresultmemo[0] = 1;            CHILDRENRESULTMEMO[1] = 1;            Long children = 0;                                 while (S.any ()) {var cur = s.pop (); if (cur. visited = = False) {if (childrenresultmemo[cur. Value] = = 0) {cur.                            visited = true; if (childrenresultmemo[cur. VALUE-1]! = 0 && childrenresultmemo[cur. VALUE-2]! = 0) {var result = Childrenresultmemo[cur. VALUE-1] + childrenresultmemo[cur.                                VALUE-2]; Childrenresultmemo[cur.                                Value] = result;     sum + = result;                           S.push (cur);                                } else {s.push (cur); S.push (New Node (cur).                                Value-1, false)); S.push (New Node (cur).                            Value-2, false)); }} else {sum + = children Resultmemo[cur.                                               value];//Save subtree result optimization, there will be a special case to handle}}        } return sum; }

The core idea of the above algorithm is to traverse the stack, pop out of the top of the stack element, if it has been visited (visited is true), skip (the code above due to the use of preserving subtree results of optimization, there will be a special case to deal with, the following will be detailed); Marks the current parent node's visited as true, represents the accessed, pushes to the stack, and then pushes its child nodes to the stack.

If you follow this line of thought, the code written will not be the same as above, the code is much less and much more concise, but the algorithm complexity will be similar to the recursive method, because there is a repetition of computation.

What to do, there is only one solution, space for time, the results of the subtree is stored, if the corresponding subtree has been calculated to have results, it is no longer down a layer of depth traversal, direct use of the results. We saved the subtree result in an array:

long[] Childrenresultmemo = new long[n+1];

Usually if the subtree already has a result, it should logically be accessed. But there is a special case, the first subtree 0 and subtree 1:

Childrenresultmemo[0] = 1;childrenresultmemo[1] = 1;

Simply add the result in the branch of this special case:

Sum + = Childrenresultmemo[cur. Value];

Well, is this a very rare notation? In fact, the process of evaluating the Fibonacci sequence is like a tree's depth-first traversal. So as long as the implementation of the depth-first traversal, it is completely feasible.

Related Article

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.