...

/

Limitations of Top-Down Dynamic Programming

Limitations of Top-Down Dynamic Programming

In this lesson, we will see some limitations of top-down dynamic programming approach.

Massive amount of recursion

Top-down dynamic programming consists of recursive calls to the subproblems. The answer is not evaluated until all the subproblems have been evaluated; which in turn depends on their own subproblems. We quickly have a huge number of incomplete recursive calls on our program stack. If we keep making these recursive calls and no previous calls have evaluated; we will run out of space on the stack. This will end up in erroneous termination of the program. Run the following code of the Fibonacci numbers algorithm we discussed in the last chapter.

Press + to interact
memo = {} #dictionary for Memoization
def fib(n):
if n == 0: # base case 1
return 0
if n == 1: # base case 2
return 1
elif n in memo: # Check if result for n has already been evaluated
return memo[n] # return the result if it is available
else: # otherwise recursive step
memo[n] = fib(n-1) + fib(n-2) # store the result of n in memoization dictionary
return memo[n] # return the value
print (fib(1000))

The program will terminate ...