| • Science | • People | • Locations | • Timeline |
Optimal substructure means that optimal solutions of subproblems can be used to find the optimal solutions of the overall problem. For example, the shortest path to a goal from a vertex in a graph can be found by first computing the shortest path to the goal from all adjacent vertices, and then using this to pick the best overall path, as shown in Figure 1. In general, we can solve a problem with optimal substructure using a three-step process:
The subproblems are, themselves, solved by dividing them into sub-subproblems, and so on, until we reach some simple case that is easy to solve.
Figure 2. The subproblem graph for the Fibonacci sequence. That it is not a tree but a DAG indicates overlapping subproblems. To say that a problem has overlapping subproblems is to say that the same subproblems are used to solve many different larger problems. For example, in the Fibonacci sequence, F3 = F1 + F2 and F4 = F2 + F3 — computing each number involves computing F2. Because both F3 and F4 are needed to compute F5, a naive approach to computing F5 may end up computing F2 twice or more. This applies whenever overlapping subproblems are present: a naive approach may waste time recomputing optimal solutions to subproblems it has already solved.
In order to avoid this, we instead save the solutions to problems we have already solved. Then, if we need to solve the same problem later, we can retrieve and use our already-computed solution. This approach is called memoization (not memorization, although this term also fits). If we are sure we won't need a particular solution anymore, we can throw it away to save space. In some cases, we can even compute the solutions to subproblems we know that we'll need in advance.
In summary, dynamic programming makes use of:
Dynamic programming usually takes one of two approaches:
Originally, the term dynamic programming only applied to solving certain kinds of operational problems outside the area of computer science, just as linear programming did. In this context it has no particular connection to programming at all; the name is a coincidence. The term was also used in the 1940s by Richard BellmanRichard Bellman ( 1920 1984) was an applied mathematician, celebrated for his invention of dynamic programming in 1953, and important contributions in other fields of mathematics. Bellman studied mathematics at Brooklyn College (B. and the University of W, an American mathematician to describe the process of solving problems where one needs to find the best decisions one after another.
Some functional programming languages, most notably HaskellHaskell is a standardized functional programming language with non-strict semantics, named after the logician Haskell Curry. It was created by a committee formed in the 1980s for the express purpose of defining such a language. The latest semi-official la, automatically memoize the result of a function call with a particular set of arguments, in order to speed up call-by-name evaluation (this mechanism is referred to as call-by-need). This is only possible for a function which has no side-effectA side-effect is any effect other than an intended primary effect. It may or may not be expected. In particular, the term is often used in the medical field with regard to drugs, where the primary effect is that what the drug is usually prescribed for.s, which is usually true in Haskell but seldom true in imperative languages.