0% found this document useful (0 votes)
9 views

Dynamic Programming - Part2

Dynamic programming
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Dynamic Programming - Part2

Dynamic programming
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 27

Solution using Dynamic programming for

1. Knap sack problem


2. Single-Source Shortest Paths -using Bellman-
Ford Algorithm

30-03-2024 Dr.P.V.Bhat, NMAMIT,Nitte 1


The Knapsack Problem and Memory
Functions
• Given n items of known weights w1, . . . , wn and values v1, . . . , vn and a knapsack
of capacity W, find the most valuable subset of the items that fit into the
knapsack.

• To design a DP algorithm, we need to derive a recurrence relation that expresses


a solution in terms of its smaller sub instances.

• Let us consider an instance defined by the first i items, 1≤ i ≤ n, with weights w1, .
. . , wi, values v1, . . . , vi , and knapsack capacity j, 1 ≤ j ≤ W

30-03-2024 Dr.P.V.Bhat, NMAMIT,Nitte 2


• Let V[i, j]be the value of an optimal solution to this instance.
• We can divide all the subsets of the first i items that fit the
knapsack of capacity j into two categories:
– those that do not include the ith item
– and those that do.
• Among the subsets that do not include the ith item,
– the value of an optimal subset is, by definition,
– i.e V[i , j] = V[i − 1, j]
• Among the subsets that do include the ith item (hence, j - wi ≥ 0),
– an optimal subset is made up of this item
– an optimal subset of the first i-1 items that fits into the
knapsack of capacity j - wi .
• The value of such an optimal subset is
• V[i , j] = vi + V[i -1, j – wi ]
30-03-2024 Dr.P.V.Bhat, NMAMIT,Nitte 3
• Thus, optimal solution among all feasible subsets of the first i items
is the maximum of these two values.

• V [i, j ] = max{V [i − 1, j ], vi + V [i − 1, j − wi]} if j − wi ≥ 0,


= V [i − 1, j] if j − wi < 0.
with initial conditions as follows: V [0, j] = 0 for j ≥ 0 and
V [i, 0] = 0 for i ≥ 0
• Our goal is to find V[n,W]

30-03-2024 Dr.P.V.Bhat, NMAMIT,Nitte 4


• It is convenient to define the initial conditions as follows:
• F(0, j) = 0 for j ≥ 0 and F(i, 0) = 0 for i ≥ 0.

• Our goal is to find F(n, W), the maximal value of a subset


of the n given items that fit into the knapsack of capacity
W, and an optimal subset itself.

30-03-2024 Dr.P.V.Bhat, NMAMIT,Nitte 5


Example:

V [i, j ] = max{V [i − 1, j ], vi + V [i − 1, j − wi]} if j − wi ≥ 0,


= V [i − 1, j] if j − wi < 0.
with initial conditions as follows: V [0, j] = 0 for j ≥ 0 and V [i, 0] = 0 for
i≥0

30-03-2024 Dr.P.V.Bhat, NMAMIT,Nitte 6


30-03-2024 Dr.P.V.Bhat, NMAMIT,Nitte 7
Analysis
• The classic dynamic programming approach,, works bottom up: it fills a table with
solutions to all smaller sub problems, each of them is solved only once.

• Drawback: Some unnecessary sub problems are also solved.

• The time efficiency and space efficiency of this algorithm are both in Θ(nW).

• The time needed to find the composition of an optimal solution is in O(n+W).

30-03-2024 Dr.P.V.Bhat, NMAMIT,Nitte 8


• The direct top-down approach to finding a solution to
such a recurrence leads to an algorithm that solves
common sub problems more than once and hence is very
inefficient.

• But bottom up approach fills table with solution to all


smaller sub problems but not often necessary for getting
solution to given problem.
• Hence it is natural to try to combine the strengths of the
top-down and bottom-up approaches.

• The goal is to get a method that solves only sub


problems that are necessary and does so only once. Such
a method exists; it is based on using memory functions.

30-03-2024 Dr.P.V.Bhat, NMAMIT,Nitte 9


• This method solves a given problem in the top-down manner but, in
addition, maintains a table of the kind that would have been used by a
bottom-up dynamic programming algorithm.

• Initially, all the table’s entries are initialized with a special “null” symbol to
indicate that they have not yet been calculated. This technique is called as
virtual initialization

• Thereafter, whenever a new value needs to be calculated, the method


checks the corresponding entry in the table first: if this entry is not “null,” it
is simply retrieved from the table; otherwise, it is computed by the
recursive call whose result is then recorded in the table.

30-03-2024 Dr.P.V.Bhat, NMAMIT,Nitte 10


Algorithm MFKnapsack(i, j )
• Implements the memory function method for the
knapsack problem

• Input: A nonnegative integer i indicating the number of


the first items being considered and a nonnegative
integer j indicating the knapsack capacity

• Output: The value of an optimal feasible subset of the


first i items

• Note: Uses as global variables input arrays Weights[1..n],


Values[1..n], and table F[0..n, 0..W ] whose entries are
initialized with −1’s except for row 0 and column 0
initialised with 0’s
30-03-2024 Dr.P.V.Bhat, NMAMIT,Nitte 11
30-03-2024 Dr.P.V.Bhat, NMAMIT,Nitte 12
30-03-2024 Dr.P.V.Bhat, NMAMIT,Nitte 13
Tracing

30-03-2024 Dr.P.V.Bhat, NMAMIT,Nitte 14


Single-Source Shortest Paths -using Bellman-Ford Algorithm

• This algorithm is used to solve single source shortest path


Problem of a given graph that can have edges with negative
values.
• For example consider a directed graph with a negative edge

• If we use Dijkstra’s algorithm to compute shortest path from


node1 to nodes 2 and 3, we get
• Shortest path from node 1 to 3 is 5
• Shortest path from node 1 to 2 is 7 is 5
• Where as actual Shortest path from node 1 to 3 is 2 and
Shortest path from node 1 to 2 is 7
This shows that we can not use always Dijiksta’s algorithm for
edges with negative edges. In such cases, we use Bellman ford
30-03-2024 algorithm. Dr.P.V.Bhat, NMAMIT,Nitte 15
Bellman-Ford Algorithm
• Like other Dynamic Programming Problems, the algorithm
calculates shortest paths in bottom-up manner.
• It first calculates the shortest distances
– for the shortest paths which have at-most one edge in the
path.
– Then, it calculates shortest paths with at-most 2 edges, and
so on.
• In general, iteration i finds all shortest paths that use i edges.

30-03-2024 Dr.P.V.Bhat, NMAMIT,Nitte 16


• Let distL[u] be the length of a shortest path from
the source vertex v to vertex u under the
constraint that the shortest path contains at most
L edges. Then, distL[u] = cost[v,u], 1≤u≤n
• When there are no cycles of negative length, we
can limit our search for shortest paths to paths
with at most n-1 edges. Hence,distn-1[u]is the
length of an unrestricted shortest path from v to u.
Our goal then is to compute distn-1[u] for all u. This
can be done using the dynamic programming
methodology.
• First, we make the following observations

30-03-2024 Dr.P.V.Bhat, NMAMIT,Nitte 17


• For the shortest path from v to u with at most k, k > 1, edges,
then we get two situations.
• 1. Shortest path that has no more than k-1edges,then
distk[u] = distk-1[u]
• 2.Shortest path with exactly k edges, then
it is made up of a shortest path from v to some vertex j followed by
the edge(j,u).
The path from v to j has k -1 edges, and its length is distk-1[j].
Here all vertices i such that the edge (i,u) is in the graph are
candidates for j.
Since we are interested in a shortest path, the i that minimizes distk-
1[i] + cost[i, u] is the correct value for j.

30-03-2024 Dr.P.V.Bhat, NMAMIT,Nitte 18


Example : Using Bellman ford Algorithm to find
shortest path from source1 to all other vertices

30-03-2024 Dr.P.V.Bhat, NMAMIT,Nitte 19


30-03-2024 Dr.P.V.Bhat, NMAMIT,Nitte 20
30-03-2024 Dr.P.V.Bhat, NMAMIT,Nitte 21
30-03-2024 Dr.P.V.Bhat, NMAMIT,Nitte 22
30-03-2024 Dr.P.V.Bhat, NMAMIT,Nitte 23
30-03-2024 Dr.P.V.Bhat, NMAMIT,Nitte 24
30-03-2024 Dr.P.V.Bhat, NMAMIT,Nitte 25
30-03-2024 Dr.P.V.Bhat, NMAMIT,Nitte 26
Analysis
• Overall complexity is
-O(ne)  For adjacency List
-O(n3)  For adjacency Matrix

30-03-2024 Dr.P.V.Bhat, NMAMIT,Nitte 27

You might also like