Memoization fibonacci algorithm in python

后端 未结 3 1040
执笔经年
执笔经年 2020-12-11 07:04

I have this memoization technique to reduce the number of calls getting a Fibonacci sequence number:

def fastFib(n, memo):
    global numCalls
    numCalls          


        
3条回答
  •  天命终不由人
    2020-12-11 07:07

    You should return memo[n] always, not only on unseccesful look up (last line of fastFib()):

    def fastFib(n, memo):
        global numCalls
        numCalls += 1
        print 'fib1 called with', n
        if not n in memo:
            memo[n] = fastFib(n-1, memo) + fastFib(n-2, memo)
        #this should be outside of the if clause:
        return memo[n] #<<<<<< THIS
    

    The number of calls is reduced this way, because for each value of n you actually compute and recurse from at most once, limitting the number of recursive calls to O(n) (upper bound of 2n invokations), instead of recomputing the same values over and over again, effectively making exponential number of recursive calls.

    A small example for fib(5), where each line is a recursive invokation:

    Naive approach:

    f(5) = 
    f(4) + f(3) = 
    f(3) + f(2) + f(3) =
    f(2) + f(1) + f(2) + f(3) =
    f(1) + f(0) + f(1) + f(2) + f(3) = (base clauses) = 
    1 + f(0) + f(1) + f(2) + f(3) = 
    2 + f(1) + f(2) + f(3) =
    3 + f(2) + f(3) = 
    3 + f(1) + f(0) + f(3) = 
    3 + 1 + f(0) + f(3) = 
    5 + f(3) = 
    5 + f(2) + f(1)  =
    5 + f(1) + f(0) + f(1) =
    5 + 1 + f(0) + f(1) =
    5 + 2 + f(1) =
    8
    

    Now, if you use memoization, you don't need to recalculate a lot of things (like f(2), which was calculated 3 times) and you get:

    f(5) = 
    f(4) + f(3) = 
    f(3) + f(2) + f(3) =
    f(2) + f(1) + f(2) + f(3) =
    f(1) + f(0) + f(1) + f(2) + f(3) = (base clauses) = 
    1 + f(0) + f(1) + f(2) + f(3) = 
    2 + f(1) + f(2) + f(3) =
    3 + f(2) + f(3) =  {f(2) is already known}
    3 + 2 + f(3) = {f(3) is already known}
    5 + 3  = 
    8
    

    As you can see, the second is shorter than the first, and the bigger the number (n) becomes, the more significant this difference is.

提交回复
热议问题