04Chapter-Four Dynamic Programming
04Chapter-Four Dynamic Programming
• Principle of Optimality
• O/I Knapsack
problems
algorithms.
By “inefficient”, we mean that the same recursive call is made over and
over.
In DP, If same sub problem is solved several times, we can use table to
store result of a sub problem the first time it is computed and thus never
DP is applicable when the sub problems are dependent, that is, when sub
a) Overlapping sub-problems
b) Optimal substructure.
a. 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.
b. 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
sub-structure 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.
• The standard All Pair Shortest Path algorithms like Floyd-Warshall
and Bellman-Ford are typical examples of Dynamic Programming.
Divide and Conquer vs DP
Divide and-Conquer:
– divide and conquer combines the solutions of the sub-problems to obtain the
– inefficient because the same common sub-problems have to be solved many times.
– It is Recursive.
– It is a top-down approach
• DP uses the result of the sub-problems to find the optimum solution of the
main problem.
• It is non Recursive
Greedy Method
Dynamic Programming
• It gives local “ Optimal
It gives “Global Optimal
Solution”
Solution”
• Print in time makes “local
It makes decision “ smart
optimization”
recursion ”
• More efficient as compared to
Less efficient as compared too
dynamic programing
greedy technique
• Example:-Fractional Knapsack
Example:- 0/1 Knapsack
Principle of Optimality
The dynamic Programming works on a principle of optimality.
Principle of optimality says that a problem can be solved by
sequence of decisions to get the optimal solution.
It follows Memoization Problem
What is Memoization?
is a technique for improving the performance of recursive algorithms..
Memoization ensures that method does not execute more than once for
same inputs by storing the results in the data structure(Usually
Hashtable or HashMap or Array ). ...
Let's understand with the help of Fibonacci example. Here is sample
fibonacci series.
Example : Fibonacci Sequence
programming.
– The solution then lets us solve the next subproblem, and so forth. We
iteratively solve all subproblems in this way until we’ve solved all
subproblems, thus finding the solution to the original problem.
ii. Memoization: is a top-down approach
– It starts with the highest-level sub-problems (the ones closest to the
original problem), and recursively calls the next sub-problem, and the
next
Memoization vs tabulation
• Both tabulation and memoization store the answers to sub-problems as they are
solved.
• They both operate on the same trade off: sacrifice space for time savings by
caching answers to solved problems.
• However, they differ subtly in the way that they use these stored
values.
– Since tabulation is bottom up, every sub-problem must be answered to
generate the final answer, so the order in which we choose to solve sub-
problems is important.
– tabulation has to look through the entire search space; memoization does not.
– tabulation requires careful ordering of the sub-problems
is; memoization doesn't care much about the order of recursive calls.
Applications of Dynamic Programming Approach
• In 0-1 Knapsack, items cannot be broken which means the thief should take
the item as a whole or should leave it.
• This is reason behind calling it as 0-1 Knapsack.
• Problem-
For the given set of items and knapsack capacity = 5 kg, find the
optimal solution for the 0/1 knapsack problem making use of
dynamic programming approach.
• Consider-
• n = 4 m = 5 kg
• (w1, w2, w3, w4) = (2, 3, 4, 5)
• (p1, p2, p3, p4) = (3, 4, 5, 6)
Solution
• Given-
• Knapsack capacity (m) = 5 kg
• Number of items (n) = 4
• Step-01:
• Draw a table say ‘T’ with (n+1) = 4 + 1 = 5 number of rows and
(w+1) = 5 + 1 = 6 number of columns.
• Fill all the boxes of 0th row and 0th column with 0.
Solution…
• Step-02: Start filling the table row wise top to bottom from
left to right using the formula-
• T(i,j)=max{T(i-1,j),T([i-1,j-w[i])+p[i]}
• Finding T(1,1)-
• We have,
i=1
j=1
(value)i = (value)1 = 3
(weight)i = (weight)1 = 2
Substituting the values, we get-
T(1,1) = max { T(1-1 , 1) , 3 + T(1-1 , 1-2) }
T(1,1) = max { T(0,1) , 3 + T(0,-1) }
T(1,1) = T(0,1) { Ignore T(0,-1) }
T(1,1) = 0
Solution…
• Similarly, compute all the entries.
• After all the entries are computed and filled in the table, we get the
following table-
• The last entry represents the maximum possible value that can be put into
the knapsack.
• So, maximum possible value that can be put into the knapsack = 7.
Solution…
• Identifying Items To Be Put Into Knapsack-
• Following Step-03,
• We mark the rows labelled “1” and “2”.
• Thus, items that must be put into the knapsack to obtain the
maximum value 7 are-
• Item-1 and Item-2
0-1 knapsack Problem: Exercise
• A thief enters a house for robbing it. He can carry a maximal
weight of 8 kg into his bag. There are 4 items in the house
with the following weights and values.
• weights= {3,4,6,5}
• Profits(value)={2,3,1,4}
• What items should thief take if he/she either takes the item
completely or leaves it completely?
All pairs shortest path
• The all pair shortest path algorithm is also known as Floyd-
Warshall algorithm is used to find all pair shortest path problem
from a given weighted graph.
• Solved by Dynamic programming,
• The idea of Dynamic programming is the problem should be solved
by sequence of discussion.
• This is similar to single source shortest path that is Dijkstra
algorithm
• But Dijkstra find the shortest path from one of the source vertices.
06/02/2025
All pairs shortest path problem Algorithm
Example: All pair shortest path problem
• Given a weighted digraph G=(V,E) with weight. Determine
the length of shortest path between all pair of vertices in G.
Here we assume that these are no cycles with zero and
negative cost.
Solution…
Exercise #2
• Given a weighted digraph G=(V,E) with weight. Determine
the length of shortest path between all pair of vertices in G.
Here we assume that these are no cycles with zero and
negative cost.
Reading Assignment
• Travelling Salesman Problem
• Depth First Search
• Matrix Chain Multiplication
• Longest Common Subsequence
• Reliability Design