Dynamic Programming
Dynamic Programming
Dynamic
Dynamic Programming
Programming
t does more work on sub problems and hence It solves sub problems only once and then stores in the table.
has more time consumption
For example: Merge Sort & Binary Search etc. For example: Matrix Multiplication.
Applications of dynamic programming
• 0/1 knapsack problem
• All pair Shortest path problem
• Reliability design problem
• Longest common subsequence (LCS)
• Flight control and robotics control
• Time-sharing: It schedules the job to maximize CPU
usage
0/1 Knapsack Problem
• Knapsack is basically means bag. A bag of given
capacity. We want to pack n items in your
luggage. o The ith item is worth vi dollars and
weight wi pounds. o Take as valuable a load as
possible, but cannot exceed W pounds. o vi wi
W are integers.
• 1. W ≤ capacity 2. Value ← Max
Unbounded Knapsack (Repetition of items allowed)
Decreasing order of P i /W i is X1, X4, X3, X5, X2 X1 --> profit = 15 and weight = 2
Including X4 --> profit = 15 + 16 and weight = 2 + 4 = 6 Including X3 --> profit = 40
and weight = 9 Now weight left = 3 Weight of X5 = 6 --> half of X5 can be included.
Profit = 40 + 17/2 = 48.5.
• 2. A thief wants to rob a store. He is carrying a bag of capacity W. The store has
‘n’ items. Its weight is given by the ‘wt’ array and its value by the ‘val’ array. He
can either include an item in its knapsack or exclude it but can’t partially have
it as a fraction. We need to find the maximum value of items that the thief can
steal.
• Solution:
Bellman Ford Algorithm
• The Bellman-Ford algorithm is a way to find single source shortest
paths in a graph with negative edge weights (but no negative cycles).
The second for loop in this algorithm also detects negative cycles.
• The first for loop relaxes each of the edges in the graph n − 1 times. We
claim that after n − 1 iterations, the distances are guaranteed to be
correct. Overall, the algorithm takes O(mn) time.
• Dynamic Programming is used in the Bellman-Ford algorithm. It begins
with a starting vertex and calculates the distances between other
vertices that a single edge can reach. It then searches for a path with
two edges, and so on. The Bellman-Ford algorithm uses the bottom-up
approach.
• Bellman-Ford detects negative cycles, i.e. if there is
a negative cycle reachable from the source s, then
for some edge (u, v), dn−1(v) > dn−1(u) + w(u, v).
• If the graph has no negative cycles, then the
distance estimates on the last iteration are equal
to the true shortest distances. That is, dn−1(v) =
δ(s, v) for all vertices v.
• Why Should You Be Cautious With Negative Weights?
• Choose path value 0 for the source vertex and infinity for all other
vertices.
• If the new calculated path length is less than the previous path length,
go to the source vertex's neighbouring Edge and relax the path length of
the adjacent Vertex.
• This procedure must be repeated V-1 times, where V is the number of
vertices in total. This happened because, in the worst-case scenario,
any vertex's path length can be changed N times to an even shorter
path length.
• This procedure must be repeated V-1 times, where V is the number of
vertices in total. This happened because, in the worst-case scenario,
any vertex's path length can be changed N times to an even shorter
path length.
• As a result, after V-1 iterations, you find your new path lengths and
can determine in case the graph has a negative cycle or not.