-2

I am a beginner to python, and was following this video for learning dynamic programming. For the case of finding a Fibonacci series, the tutor introduced memoization to improve the performance of the recursive algorithm. The complete code is as follows

    import time

def fibonacci1(n):
    '''This uses the naive fibonacci algorithm. '''
    if n < 2:
        return n
    else:
        return fibonacci1(n-1) + fibonacci1(n-2)

def fibonacci2(n):
    ''' This function uses a memo of computed values to speed up the process'''
    memo = {}
    if n in memo:
        return memo[n]
    if n < 2:
        return n
    f = fibonacci2(n-1) + fibonacci2(n-2)
    memo[n] = f
    return f

def screen(i,N):
    if i==1:
        return fibonacci1(N)
    if i == 2:
        return fibonacci2(N)

N = int(input("Enter a number: "))

for i in range(2):
    t0 = time.time()
    fib = screen(i+1,N)
    print("The "+ str(N) +"th fibonacci number is {}".format(fib))
    t1 = time.time()
    print("Time taken is : {}s".format(t1-t0))

But this is the output I received: FibonacciOutput

Can somebody help me with this?

4 Answers4

1

What's happening here is that your memo is a local variable, which you're assigning an empty dict into at the beginning of each run of fibonacci2. Thus, you will never find n inside of memo.

There are various ways of accomplishing what you're trying to do. One of them (not the greatest, but at least simple to understand) is using a global variable:

memo = {}
def fibonacci2(n):
    ''' This function uses a memo of computed values to speed up the process'''
    global memo
    if n in memo:
        return memo[n]
    if n < 2:
        return n
    f = fibonacci2(n-1) + fibonacci2(n-2)
    memo[n] = f
    return f

Here we defined memo outside of the function, so it only gets defined one time. The global memo line indicates that the memo we're using in our function was actually defined outside of it.

Blue Star
  • 1,932
  • 1
  • 10
  • 11
1

From your code:

    memo = {}
    if n in memo:

How could n possibly be in the dict that you created empty right before?

I'd do it like this:

def fibonacci2(n, memo={}):
    ...
superb rain
  • 5,300
  • 2
  • 11
  • 25
0

It looks like it resets memo to the empty dictionary every time it invokes fibonacci2. So it adds some overhead of doing dict reads and writes but doesn't actually add memoization to the algorithm. If you just move memo = {} up a couple lines so its values are persistent, it appears to help quite a bit.

Nathan Pierson
  • 5,461
  • 1
  • 12
  • 30
0

Declare memo = {} outside of the fibonacci2(n) function as global variable because when you declare memo inside the fibonacci2() it will always be initialized empty when function calls. You can do like this

memo = {}
def fibonacci2(n):
    ''' This function uses a memo of computed values to speed up the process'''
   
    if n in memo:
        return memo[n]
    if n < 2:
        return n
    f = fibonacci2(n-1) + fibonacci2(n-2)
    memo[n] = f
    return f
Hridoy_089
  • 318
  • 3
  • 11