HOME

TheInfoList



OR:

In
computer science Computer science is the study of computation, automation, and information. Computer science spans theoretical disciplines (such as algorithms, theory of computation, information theory, and automation) to Applied science, practical discipli ...
, 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. Dynamic Programming: Overlapping Subproblems, Optimal Substructure
MIT Video. For example, the problem of computing the
Fibonacci sequence In mathematics, the Fibonacci numbers, commonly denoted , form a integer sequence, sequence, the Fibonacci sequence, in which each number is the sum of the two preceding ones. The sequence commonly starts from 0 and 1, although some authors start ...
exhibits overlapping subproblems. The problem of computing the ''n''th
Fibonacci number In mathematics, the Fibonacci numbers, commonly denoted , form a sequence, the Fibonacci sequence, in which each number is the sum of the two preceding ones. The sequence commonly starts from 0 and 1, although some authors start the sequence from ...
''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. A naive
recursive Recursion (adjective: ''recursive'') occurs when a thing is defined in terms of itself or of its type. Recursion is used in a variety of disciplines ranging from linguistics to logic. The most common application of recursion is in mathematics ...
approach to such a problem generally fails due to an exponential complexity. If the problem also shares an
optimal substructure In computer science, a problem is said to have optimal substructure if an optimal solution can be constructed from optimal solutions of its subproblems. This property is used to determine the usefulness of greedy algorithms for a problem.{{cite boo ...
property,
dynamic programming Dynamic programming is both a mathematical optimization method and a computer programming method. The method was developed by Richard Bellman in the 1950s and has found applications in numerous fields, from aerospace engineering to economics. I ...
is a good way to work it out.


Fibonacci sequence example in C

Consider the following C code: #include #define N 5 static int fibMem int fibonacci(int n) void printFibonacci() int main(void) /* Output: fibonacci(1): 1 fibonacci(2): 1 fibonacci(3): 2 fibonacci(4): 3 fibonacci(5): 5 */ When executed, the fibonacci function computes the value of some of the numbers in the sequence many times over, following a pattern which can be visualized by this diagram: f(5) = f(4) + f(3) = 5 , , , f(3) = f(2) + f(1) = 2 , , , , , f(1) = 1 , , , f(2) = 1 , f(4) = f(3) + f(2) = 3 , , , f(2) = 1 , f(3) = f(2) + f(1) = 2 , , , f(1) = 1 , f(2) = 1 However, we can take advantage of
memoization In computing, memoization or memoisation is an optimization technique used primarily to speed up computer programs by storing the results of expensive function calls and returning the cached result when the same inputs occur again. Memoization ...
and change the fibonacci function to make use of fibMem like so: int fibonacci(int n) This is much more efficient because if the value r has already been calculated for a certain n and stored in fibMem - 1/code>, the function can just return the stored value rather than making more recursive function calls. This results in a pattern which can be visualized by this diagram: f(5) = f(4) + f(3) = 5 , , f(4) = f(3) + f(2) = 3 , , f(3) = f(2) + f(1) = 2 , , , f(1) = 1 , f(2) = 1 The difference may not seem too significant with an N of 5, but as its value increases, the complexity of the original fibonacci function increases exponentially, whereas the revised version increases more linearly.


See also

*
Dynamic programming Dynamic programming is both a mathematical optimization method and a computer programming method. The method was developed by Richard Bellman in the 1950s and has found applications in numerous fields, from aerospace engineering to economics. I ...


References

{{DEFAULTSORT:Overlapping Subproblem Dynamic programming