Dynamic programming is a method for solving a complex problem by breaking it down into a collection of simpler subproblems, solving each of those subproblems just once, and storing their solutions using a memory-based data structure (array, map,etc). Each of the subproblem solutions is indexed in some way, typically based on the values of its input parameters, so as to facilitate its lookup. So the next time the same subproblem occurs, instead of recomputing its solution, one simply looks up the previously computed solution, thereby saving computation time. This technique of storing solutions to subproblems instead of recomputing them is called memoization.



Here’s brilliant explanation to explain the concept behind dynamic programming to a kid.

*writes down “1+1+1+1+1+1+1+1 =” on a sheet of paper*

“What’s that equal to?”

*counting* “Eight!”

*writes down another “1+” on the left*

“What about that?”

*quickly* “Nine!”

“How’d you know it was nine so fast?”

“You just added one more”

“So you didn’t need to recount because you remembered there were eight! Dynamic

Programming is just a fancy way to say ‘remembering stuff to save time later'”

There are two key attributes that a problem must have in order for dynamic programming to be applicable: optimal substructure and overlapping sub-problems.

1. Optimal substructure

Dynamic programming simplify a complicated problem by breaking it down into simpler sub-problems in a recursive manner. A problem that can be solved optimally by breaking it into sub-problems and then recursively finding the optimal solutions to the sub-problems is said to have optimal substructure. In other words, solution to a given optimization problem can be obtained by the combination of optimal solutions to its sub-problems.

For example, the shortest path p from a vertex u to a vertex v in a given graph exhibits optimal substructure: take any intermediate vertex w on this shortest path p. If p is truly the shortest path, then it can be split into sub-paths p 1 from u to w and p 2 from w to v such that these, in turn, are indeed the shortest paths between the corresponding vertices.

2. Overlapping sub-problems

A problem is said to have overlapping subproblems if the problem can be broken down into subproblems which are reused several times or a recursive algorithm for the problem solves the same subproblem over and over rather than always generating new subproblems.

For example, the problem of computing the Fibonacci sequence exhibits overlapping subproblems. The problem of computing the nth Fibonacci number F(n), can be broken down into the subproblems of computing F(n – 1) and F(n – 2), and then adding the two. The subproblem of computing F(n – 1) can itself be broken down into a subproblem that involves computing F(n – 2). Therefore the computation of F(n – 2) is reused, and the Fibonacci sequence thus exhibits overlapping subproblems. Dynamic programming takes account of this fact and solves each sub-problem only once. This can be achieved in either of two ways –

Top-down approach (Memoization): This is the direct fall-out of the recursive formulation of any problem. If the solution to any problem can be formulated recursively using the solution to its sub-problems, and if its sub-problems are overlapping, then one can easily memoize or store the solutions to the sub-problems in a table. Whenever we attempt to solve a new sub-problem, we first check the table to see if it is already solved. If the sub-problem is already solved, we can use it’s solution directly, otherwise we solve the sub-problem and add its solution to the table.

Bottom-up approach (Tabulation): Once we formulate the solution to a problem recursively as in terms of its sub-problems, we can try reformulating the problem in a bottom-up fashion: try solving the sub-problems first and use their solutions to build-on and arrive at solutions to bigger sub-problems. This is also usually done in a tabular form by iteratively generating solutions to bigger and bigger sub-problems by using the solutions to small sub-problems. For example, if we already know the values of F(i – 1) and F(i – 2), we can directly calculate the value of F(i).



If a problem can be solved by combining optimal solutions to non-overlapping sub-problems, the strategy is called “divide and conquer” instead. This is why merge sort and quick sort are not classified as dynamic programming problems.

Let’s consider a naive implementation of a function finding the n’th member of the Fibonacci sequence-

// Function to find n'th Fibonacci number int fib(int n) { if (n <= 1) return n; return fib(n - 1) + fib(n - 2); } 1 2 3 4 5 6 7 8 // Function to find n'th Fibonacci number int fib ( int n ) { if ( n <= 1 ) return n ; return fib ( n - 1 ) + fib ( n - 2 ) ; }

Notice that if we call, say, fib(5) , we produce a call tree that calls the function on the same value many different times:





In particular, fib(3) was calculated two times and fib(2) was calculated three times from scratch. In larger examples, many more subproblems are recalculated, leading to an exponential time algorithm.

Now, suppose we have a simple map object, lookup, which maps each value of fib that has already been calculated to its result, and we modify our function to use it and update it. The resulting function requires only O(n) time instead of exponential time (but requires O(n) space):

C++ #include <iostream> #include <unordered_map> using namespace std; // Function to find n'th Fibonacci number int fib(int n, auto &lookup) { if (n <= 1) return n; // if sub-problem is seen for the first time if (lookup.find(n) == lookup.end()) lookup[n] = fib(n - 1, lookup) + fib(n - 2, lookup); return lookup[n]; } int main() { unordered_map<int, int> lookup; cout << fib(8, lookup); return 0; } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 #include <iostream> #include <unordered_map> using namespace std ; // Function to find n'th Fibonacci number int fib ( int n , auto &lookup ) { if ( n <= 1 ) return n ; // if sub-problem is seen for the first time if ( lookup . find ( n ) == lookup . end ( ) ) lookup [ n ] = fib ( n - 1 , lookup ) + fib ( n - 2 , lookup ) ; return lookup [ n ] ; } int main ( ) { unordered_map < int , int > lookup ; cout << fib ( 8 , lookup ) ; return 0 ; } Download Run Code Java import java.util.HashMap; import java.util.Map; class Main { // Function to find nth Fibonacci number public static int fib(int n, Map<Integer, Integer> lookup) { if (n <= 1) { return n; } // if sub-problem is seen for the first time if (!lookup.containsKey(n)) { int val = fib(n - 1, lookup) + fib(n - 2, lookup); lookup.put(n, val); } return lookup.get(n); } public static void main(String[] args) { int n = 8; Map<Integer, Integer> lookup = new HashMap<>(); System.out.println(fib(n, lookup)); } } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 import java . util . HashMap ; import java . util . Map ; class Main { // Function to find nth Fibonacci number public static int fib ( int n , Map < Integer , Integer > lookup ) { if ( n <= 1 ) { return n ; } // if sub-problem is seen for the first time if ( ! lookup . containsKey ( n ) ) { int val = fib ( n - 1 , lookup ) + fib ( n - 2 , lookup ) ; lookup . put ( n , val ) ; } return lookup . get ( n ) ; } public static void main ( String [ ] args ) { int n = 8 ; Map < Integer , Integer > lookup = new HashMap <> ( ) ; System . out . println ( fib ( n , lookup ) ) ; } } Download Run Code Python # Function to find nth Fibonacci number def fib(n, lookup): if n <= 1: return n # if sub-problem is seen for the first time if n not in lookup: val = fib(n - 1, lookup) + fib(n - 2, lookup) lookup[n] = val return lookup[n] if __name__ == '__main__': n = 8 lookup = {} print(fib(n, lookup)) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 # Function to find nth Fibonacci number def fib ( n , lookup ) : if n <= 1 : return n # if sub-problem is seen for the first time if n not in lookup : val = fib ( n - 1 , lookup ) + fib ( n - 2 , lookup ) lookup [ n ] = val return lookup [ n ] if __name__ == '__main__' : n = 8 lookup = { } print ( fib ( n , lookup ) ) Download Run Code



Note that we can also use array instead of map. Check implementation here.

As already discussed, this technique of saving values that have already been calculated is called memoization; this is the top-down approach, since we first break the problem into subproblems and then calculate and store values.

In the bottom-up approach, we calculate the smaller values of fib first, then build larger values from them. This method also uses O(n) time since it contains a loop that repeats n-1 times, but it only takes constant O(1) space, in contrast to the top-down approach which requires O(n) space to store the map.

C++ #include <iostream> #include <unordered_map> using namespace std; // Function to find n'th Fibonacci number int fib(int n) { if (n <= 1) return n; int previousFib = 0, currentFib = 1; for (int i = 0; i < n - 1; i++) { int newFib = previousFib + currentFib; previousFib = currentFib; currentFib = newFib; } return currentFib; } int main() { cout << fib(8); return 0; } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 #include <iostream> #include <unordered_map> using namespace std ; // Function to find n'th Fibonacci number int fib ( int n ) { if ( n <= 1 ) return n ; int previousFib = 0 , currentFib = 1 ; for ( int i = 0 ; i < n - 1 ; i ++ ) { int newFib = previousFib + currentFib ; previousFib = currentFib ; currentFib = newFib ; } return currentFib ; } int main ( ) { cout << fib ( 8 ) ; return 0 ; } Download Run Code Output:



21

Java class Main { // Function to find n'th fibonacci number public static int fib(int n) { if (n <= 1) { return n; } int previousFib = 0, currentFib = 1; for (int i = 0; i < n - 1; i++) { int newFib = previousFib + currentFib; previousFib = currentFib; currentFib = newFib; } return currentFib; } public static void main(String[] args) { System.out.print(fib(8)); } } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 class Main { // Function to find n'th fibonacci number public static int fib ( int n ) { if ( n <= 1 ) { return n ; } int previousFib = 0 , currentFib = 1 ; for ( int i = 0 ; i < n - 1 ; i ++ ) { int newFib = previousFib + currentFib ; previousFib = currentFib ; currentFib = newFib ; } return currentFib ; } public static void main ( String [ ] args ) { System . out . print ( fib ( 8 ) ) ; } } Download Run Code Output:



21

Python # Function to find n'th fibonacci number def fib(n): if n <= 1: return n previousFib = 0 currentFib = 1 for i in range(n - 1): newFib = previousFib + currentFib previousFib = currentFib currentFib = newFib return currentFib if __name__ == '__main__': print(fib(8)) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 # Function to find n'th fibonacci number def fib ( n ) : if n <= 1 : return n previousFib = 0 currentFib = 1 for i in range ( n - 1 ) : newFib = previousFib + currentFib previousFib = currentFib currentFib = newFib return currentFib if __name__ == '__main__' : print ( fib ( 8 ) ) Download Run Code Output:



21







In both examples, we only calculate fib(2) once, and then use it to calculate both fib(4) and fib(3) , instead of computing it every time either of them is evaluated.



Source: https://en.wikipedia.org/wiki/Dynamic_programming









(99 votes, average: 4.76 out of 5)

Loading...

Thanks for reading.

