Dynamic Programming

Dynamic Programming is an algorithmic paradigm that solves a given complex problem by breaking it into subproblems andstores the results of subproblems to avoid computing the same results again. Following are the two main properties of a problem thatsuggest that the given problem can be solved using Dynamic programming.


1) Overlapping Subproblems: Like Divide and Conquer, Dynamic Programming combines solutions to sub-problems. Dynamic Programming is mainly used when solutions of same subproblems are called again and again. In dynamic programming, computed solutions to subproblems are stored  so that these don’t have to be recomputed. There are following two different ways to store the values so that these values can be reused.

a) Memoization: The memoized program for a problem is similar to therecursive version with a small modification that itlooks into a lookup table before computing solutions. We initialize a lookup array with all initial values as NIL. Whenever we need solution to a subproblem, we first look into the lookup table. If the value wanted is there then we just return the value, otherwise we calculate the value and put the result inlookup table so that it can be reused later.

b) Tabulation: The tabulated program for a given problembuilds a table iteratively and returns the wanted entry from table.

Both tabulated and Memoized store the solutions of subproblems. In Memoized version, table is filled on demand while in tabulated version, it is filled iterativelyUnlike the tabulated version, all entries of the lookup table are not necessarily filled in memoized version. So how should us choose between Memoization and Tabulation?


2) Optimal Substructure: A given problems has Optimal Substructure Property if optimal solution of the given problem can be obtained by using optimal solutions of its subproblems.

你可能感兴趣的:(Dynamic Programming)