DAA - Unit V - Dynamic Programming
DAA - Unit V - Dynamic Programming
Algorithms
• 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
Knapsack Problem
1. Among the subsets that do not include the ith item, because wi > j the
value of an optimal subset is F(i − 1, j).
2. Among the subsets that do include the ith item because wi < = j, an
optimal subset is made up of this item and an optimal subset of the first i −
1 items that fits into the knapsack of capacity j − wi .
Therefore the value of an optimal solution among all feasible subsets of the
first I items is
F(1,j) = V1 if w1 ≤ j
=0 if w1 >j
Knapsack Problem consider the instance given by the following data:
Solution
The transitive closure of a digraph can be generated with the help of depth
first search or breadth-first search. Performing either traversal starting at
the ith vertex gives the information about the vertices reachable from it and
hence the columns that contain 1’s in the ith row of the transitive closure.
Thus, doing such a traversal for every vertex as a starting point yields the
transitive closure in its entirety. Drawback of this is that this method
traverses the same digraph several times.
• R(1) contains the information about paths that can use the first vertex as
intermediate; thus, with more freedom, so to speak, it may contain more
1’s than R(0)
• In general, each subsequent matrix in series has one more vertex to use
as intermediate for its paths than its predecessor and hence may,
contain more 1’s.
• The last matrix in the series, R(n), reflects paths that can use all n
vertices of the digraph as intermediate and hence is nothing other than
the digraph’s transitive closure.
Working of the Algorithm
we can compute all the elements of each matrix R(k) from its immediate
predecessor R(k−1) in series.
Let r(k) ij , the element in the ith row and jth column of matrix R(k), be equal to
1. This means that there exists a path from the ith vertex vi to the jth vertex
vj with each intermediate vertex numbered not higher than k:
Following is the formula for generating the elements of matrix R(k) from the
elements of matrix R(k−1):
.
Pseudo Code of Warshalls Algorithm
Floyds Algorithm or All Pair shortest Path Algorithm
Specifically, the element d(k) ij in the ith row and the jth column of matrix
D(k) (i, j = 1, 2, . . . , n, k = 0, 1, . . . , n) is equal to the length of the
shortest path among all paths from the ith vertex to the jth vertex with
each intermediate vertex, if any, numbered not higher than k.
Let d(k) ij be the element in the ith row and the jth column of matrix D(k).
This means that d(k) ij is equal to the length of the shortest path among all
paths from the ith vertex vi to the jth vertex vj with their intermediate
vertices numbered not higher than k:
vi, a list of intermediate vertices each numbered not higher than k, vj .
Floyds Algorithm Pseudo Code
Example
• The Fibonacci numbers are the elements of the sequence 1, 1,
2, 3, 5, 8, 13, 21, 34, . . . , can be defined by the simple
recurrence F(n) = F(n − 1) + F(n − 2) for n > 1. and two initial
conditions F(1) = 1, F(2) = 1.
F(n)
F(n-1) + F(n-2)
if(n = 1 OR n = 2) return 1
return F(n-1) + F(n-2)
Example
• So we can simply fill elements of a one-dimensional array with
the n + 1 consecutive values of F(n) by starting, in view of initial
conditions with 1 and 1 and using above equation as the rule
for producing all the other elements.
F(5)
F(4) + F(3)
F(2) F(1) …
Algorithm Fibonacci_DP_BottomUp(n)
//Computes nth Fibonacci Number using
// bottom-up approach of Dynamic Programming
//Input: positive integer n
//Output: nth Fibonacci Number
F[1] ← F[2] ← 1
for i ← 3 to n
F[i] ← F[i-1] + F[i-2]
return F[n]
Computing 6th Fibonacci number:
F(6) = F(5) + F(4)
F(1) 1
F(5) = F(4) + F(3)
F(4) = F(3) + F(2) F(2) 1
F(3) = F(2) + F(1)
F(2) = 1 F(3) -1
F(1) = 1 F(4) -1
F(5) -1
F(6) -1
Computing 6th Fibonacci number:
F(6) = F(5) + F(4)
F(1) 1
F(5) = F(4) + F(3)
F(4) = F(3) + F(2) F(2) 1
F(3) = 1 + 1 = 2
F(2) = 1 F(3) 2
F(1) = 1 F(4) -1
F(5) -1
F(6) -1
Computing 6th Fibonacci number:
F(6) = 5 + 3 = 8
F(1) 1
F(5) = 3 + 2 = 5
F(4) = 2 + 1 = 3 F(2) 1
F(3) = 1 + 1 = 2
F(2) = 1 F(3) 2
F(1) = 1 F(4) 3
F(5) 5
F(6) 8
Algorithm Fibonacci_DP_TopDown(n, F)
//Computes nth Fibonacci Number using a table
// to avoid recomputing subproblems
//Input: positive integer n and array F where
// F[i] is either ith Fibonacci Number or -1
// indicating it’s not yet computed.
//Output: nth Fibonacci Number
Eg
If n=8
(10110110 is one of them, but 11100111 is not.)
Q: How many bit strings of length 8 does not
have consecutive two zeros.
(10110110 is one of them, but 11100111 is not.)