...
/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.
We'll cover the following...
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 Memoizationdef fib(n):if n == 0: # base case 1return 0if n == 1: # base case 2return 1elif n in memo: # Check if result for n has already been evaluatedreturn memo[n] # return the result if it is availableelse: # otherwise recursive stepmemo[n] = fib(n-1) + fib(n-2) # store the result of n in memoization dictionaryreturn memo[n] # return the valueprint (fib(1000))
The program will terminate ...