Chapter Four
Chapter Four
Programming
Chapter Four
Introduction
• Dynamic programming approach is similar to divide and conquer in breaking
down the problem into smaller and yet smaller possible sub-problems.
• But unlike divide and conquer, these sub-problems are not solved
independently.
• Rather, results of these smaller sub-problems are remembered and used for
similar or overlapping sub-problems.
• Mostly, dynamic programming algorithms are used for solving optimization
problems.
• Before solving the in-hand sub-problem, dynamic algorithm will try to
examine the results of the previously solved sub-problems.
• The solutions of sub-problems are combined in order to achieve the best
optimal final solution.
• This paradigm is thus said to be using Bottom-up approach.
Cont’d…
• So we can conclude that −
• The problem should be able to be divided into smaller overlapping
sub-problem.
• Final optimum solution can be achieved by using an optimum solution
of smaller sub-problems.
• Dynamic algorithms use memorization.
Cont’d…
• However, in a problem, two main properties can suggest that the given
problem can be solved using Dynamic Programming. They are −
• Overlapping Sub-Problems
• Similar to Divide-and-Conquer approach, Dynamic Programming also combines
solutions to sub-problems. It is mainly used where the solution of one sub-
problem is needed repeatedly.
• The computed solutions are stored in a table, so that these don’t have to be
re-computed. Hence, this technique is needed where overlapping sub-problem
exists.
• For example, Binary Search does not have overlapping sub-problem. Whereas
recursive program of Fibonacci numbers have many overlapping sub-problems.
Optimal Sub-Structure
• A given problem has Optimal Substructure Property, if the optimal
solution of the given problem can be obtained using optimal solutions
of its sub-problems.
• For example, the Shortest Path problem has the following optimal
substructure property −
• If a node x lies in the shortest path from a source node u to
destination node v, then the shortest path from u to v is the
combination of the shortest path from u to x, and the shortest path
from x to v.
Steps of Dynamic Programming
Approach
• Dynamic Programming algorithm is designed using the following four
steps −
• Characterize the structure of an optimal solution.
• Recursively define the value of an optimal solution.
• Compute the value of an optimal solution, typically in a bottom-up
fashion.
• Construct an optimal solution from the computed information.
Examples
Profit 2 found in 3rd and 2nd row, we make 3rd row 0 because it is gained by 2nd row.
The total profit={profit of x2+ profit
x4=2+8=10}