dynamic programming - wanivipin.files.wordpress.com€¦ · dynamic programming dynamic programming...
TRANSCRIPT
DYNAMIC PROGRAMMING
Unit-4
1
Prepared By Mr. Vipin K. Wani Assistant Professor SOCSE Sandip University
Dynamic programming
Dynamic programming is used where we have problems, which can be divided into
similar sub-problems, so that their results can be re-used. Mostly, these algorithms
are used for optimization. Before solving the in-hand sub-problem, dynamic algorithm
will try to examine the results of the previously solved sub-problems.
Dynamic Programming algorithm solves each sub-problem just once and then saves its
answer in a table, thereby avoiding the work of re-computing the answer every time.
It guarantees if there is a solution which can be determined by dynamic programming
then it will be Optimal.
This approach is much faster than divide and conqure and Greedy method.
It avoids repetition of solution while solving the problems.
2
Prepared By Mr. Vipin K. Wani Assistant Professor SOCSE Sandip University
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.
3
Prepared By Mr. Vipin K. Wani Assistant Professor SOCSE Sandip University
Principle of Optimality
The principle of optimality state that, the solution obtained in every sub
steps must be optimal, then and only then dynamic programming
guarantees the optimal solution, else it is difficult to determine the
optimal solution using dynamic programming.
4
Prepared By Mr. Vipin K. Wani Assistant Professor SOCSE Sandip University
Applications of Dynamic Programming
Flody Algorithm
Knapsack Problem
Optimal Binary search Tree (OBST)
Matrix Chain Multiplication
Longest Common Subsequence
Travelling Salesman Problem
5
Prepared By Mr. Vipin K. Wani Assistant Professor SOCSE Sandip University
Solution Using Flody Algorithm
Floyd-Warshall Algorithm is an algorithm for finding the shortest path
between all the pairs of vertices in a weighted graph. This algorithm works
for both the directed and undirected weighted graphs. But, it does not work
for the graphs with negative cycles.
6
Prepared By Mr. Vipin K. Wani Assistant Professor SOCSE Sandip University
Find the all pair shortest path for the given graph using flody algorithm
7
Prepared By Mr. Vipin K. Wani Assistant Professor SOCSE Sandip University
Construct Cost Matrix for the given graph
1
32
45
2 3
1
42
0 3 ∞ 52 0 ∞ 4∞ 1 0 ∞∞ ∞ 2 0
A0=
If there is no direct edge between two vertices then distance will be considered ∞.
Solution using flody algorithm
Now, Constrct a matrix A1 using matrix A0. The elements in the first column
and the first row are left as they are. The remaining cells are filled in the
following way.
Let k be the intermediate vertex in the shortest path from source to
destination. In this step, k is the first vertex. A[i][j] is filled with (A[i][k] +
A[k][j]) if (A[i][j] > A[i][k] + A[k][j]).
In this step, k is vertex 1. We cacluate the distance from source vertex to
destination vertex through this vertex k.
8
Prepared By Mr. Vipin K. Wani Assistant Professor SOCSE Sandip University
Solution Using Flody Algorithm9
Prepared By Mr. Vipin K. Wani Assistant Professor SOCSE Sandip University
1
32
45
2 3
1
42
0 3 ∞ 52 0 9 4∞ 1 0 ∞∞ ∞ 2 0
A1=
Construct A1 by Considering Vertex 1 as a Intermediate
Solution Using Flody Algorithm10
Prepared By Mr. Vipin K. Wani Assistant Professor SOCSE Sandip University
1
32
45
2 3
1
42
0 3 ∞ 52 0 9 4∞ 1 0 ∞∞ ∞ 2 0
A1=
Construct A1 by Considering Vertex 1 as a Intermediate
For example: For A1[2, 4], the direct distance from vertex 2 to 4 is 4 and the sum of
the distance from vertex 2 to 4 through vertex (ie. from vertex 2 to 1 and from
vertex 1 to 4) is 7. Since 4 < 7, A0[2, 4] is filled with 4.
Solution Using Flody Algorithm11
Prepared By Mr. Vipin K. Wani Assistant Professor SOCSE Sandip University
1
32
45
2 3
1
42
0 3 7 52 0 9 43 1 0 5∞ ∞ 2 0
A2=
In a similar way, A2 is created using A1. The elements in the second column and
the second row are left as they are.
In this step, k is the second vertex (i.e. vertex 2).
Solution Using Flody Algorithm12
Prepared By Mr. Vipin K. Wani Assistant Professor SOCSE Sandip University
1
32
45
2 3
1
42
0 3 7 52 0 9 43 1 0 55 3 2 0
A3=
In a similar way, A3 is created using A2. The elements in the second column and
the second row are left as they are.
In this step, k is the second vertex (i.e. vertex 3).
Solution Using Flody Algorithm13
Prepared By Mr. Vipin K. Wani Assistant Professor SOCSE Sandip University
1
32
45
2 3
1
42
0 3 7 52 0 6 43 1 0 55 3 2 0
A4=
In a similar way, A4 is created using A3. The elements in the second column and
the second row are left as they are.
In this step, k is the second vertex (i.e. vertex 3).
A4 gives the shortest path between each pair of vertices.
0/1 Knapsack Problem
Prepared By Mr. Vipin K. Wani Assistant Professor SOCSE Sandip University
14
The knapsack problem is a problem in combinatorialoptimization, Given a set of items, each with a weight and avalue, determine the number of each item to include in acollection so that the total weight is less than or equal to agiven limit and the total value is as large as possible.
The problem often arises in resource allocation where thedecision makers have to choose from a set of non-divisibleprojects or tasks under a fixed budget or time constraint,respectively.
0/1 Knapsack Problem
Prepared By Mr. Vipin K. Wani Assistant Professor SOCSE Sandip University
15
0/1 Knapsack Problem
Prepared By Mr. Vipin K. Wani Assistant Professor SOCSE Sandip University
16
0/1 Knapsack Problem
Prepared By Mr. Vipin K. Wani Assistant Professor SOCSE Sandip University
17
For the given instance of a knapsack problem find the optimal solution usingbacktracking approach if n=3 and M=6.
To Solve the Problem using dynamic programming approach perform following steps.
i Pi Wi
1 1 2
2 2 3
3 5 4
0/1 Knapsack Solution
Prepared By Mr. Vipin K. Wani Assistant Professor SOCSE Sandip University
18
To find out the solution divide the problem in small steps and find the solution for everysmall steps.Consider the profit wait pair for every solution step and keep picking and adding nextprofit wait pair to existing solution till all objects are considered.Initial consider Knapsack is Empty so Solution set will be
S0= { (0, 0)}
Now find the Complement of current solution by picking the next (P, W) pair and addingit to existing solution, Ignore the duplicate entries. So
S01 = {(1 ,2)} Where (1 ,2) is P & W Pair of first object.
Now get the next level of Solution by merging S0 & S01 Both previous levels solutions
S1= {(0,0), (1, 2)}
0/1 Knapsack Solution
Prepared By Mr. Vipin K. Wani Assistant Professor SOCSE Sandip University
19
Find the component of current solution by picking new object and adding it to each pairof current solution.
S10 = {(2 ,3), (3, 5)}
Now get the next level of Solution by merging S1 & S10 Both previous levels solutions
S2= {(0,0), (1, 2), (2 ,3), (3, 5)}
Find the component of current solution by picking new object and adding it to each pairof current solution.
S20 = {(5,4), (6, 6), (7 ,7), (8, 9)}
Now get the next level of Solution by merging S2 & S20 Both previous levels solutions
S3= {(0,0), (1, 2), (2 ,3), (3, 5), (5,4), (6, 6), (7 ,7), (8, 9)}
0/1 Knapsack Solution
Prepared By Mr. Vipin K. Wani Assistant Professor SOCSE Sandip University
20
Now as all objects are considered in a solution set now apply the purging rule.
Purging Rule: If in a final solution set any two (Pi, Wi) and (Pj, Wj) exist such that Pi <= Pjand Wi>=Wj then ignore the pair (Pi, Wi) from final solution.
Final Solution isS3= {(0,0), (1, 2), (2 ,3), (3, 5), (5,4), (6, 6), (7 ,7), (8, 9)}
In Above solution Consider Pair (3,5) and (5,4) apply purging rule as 3<=5 and 5>=4 sodiscard the pair (3,5) so after purging rule final solution will be.
S3= {(0,0), (1, 2), (2 ,3), (5,4), (6, 6), (7 ,7), (8, 9)}
Now for Given Instance of problem M=6 so find out the P & W pair whose Wait will beequivalent to M.
Is such pair Exist? Yes (6, 6)
0/1 Knapsack Solution
Prepared By Mr. Vipin K. Wani Assistant Professor SOCSE Sandip University
21Is such pair Exist? Yes (6, 6)
Now try to identify in which sub solution or after adding which object the pair (6,6) isgenerated.
So it is Generated in state S20 after adding 3rd (5, 4) pair. So It indicates that we will add
3rd Object in Knapsack so set X3=1.
So now after adding 3rd object in knapsack let us find the capacity available in knapsack.So let us find by subtracting 3rd (P, W) pair from pair (6, 6)
So (6-5, 6- 4)= (1,2) so now let us find whether such pair is available in final solution?
Yes so let us determine in which state and after adding which object this pair hasgenerated?
It is Generated in state S01 after adding First Pair (1, 2) so which indicate add this pair in
knapsack. So Set X1=1
0/1 Knapsack Solution
Prepared By Mr. Vipin K. Wani Assistant Professor SOCSE Sandip University
22So now after adding 3rd and 1st object in knapsack let us find the capacity available inknapsack.So let us find by subtracting 1st (P, W) pair from pair (1, 2)
So (1-1, 2- 2)= (0, 0) so Which indicates Knapsack is full and there is no space for secondobject So set X2=0
So The solution is={1,0,1}
And the Profit gain is=5+1=6
Optimal Binary Search Tree (OBST)
Prepared By Mr. Vipin K. Wani Assistant Professor SOCSE Sandip University
23
an optimal binary search tree, sometimes called a weight-balanced binary tree, is
a binary search tree which provides the smallest possible search time for a given
sequence of accesses.
In OBST for each internal node all the keys in the left sub-tree are less than the keys in
the node, and all the keys in the right sub-tree are greater.
When we know the frequency of searching each one of the keys, it is quite easy to
compute the expected cost of accessing each node in the tree.
An optimal binary search tree is a BST, which has minimal expected cost of locating
each node.
In Simple words OBST Allows us to place the information which we required
frequently closer to root node and information which we required rarely will be
placed away from the root node.
Optimal Binary Search Tree (OBST)
Prepared By Mr. Vipin K. Wani Assistant Professor SOCSE Sandip University
24
Terminology Related to OBST:
1. Identifiers [a1, a2, a3,……………an]
2. Probability of Successful search Pi
3. Probability of Unsuccessful search Qi
4. Weight of the tree Wij
5. Cost of the tree Cij
6. Root of the tree Rij
Optimal Binary Search Tree (OBST)
Prepared By Mr. Vipin K. Wani Assistant Professor SOCSE Sandip University
25
To Solve the OBST problem Use the following cases:
1. Case:1 If (i==j)
a. Wij=qi,
b. Cij =0
c. Rij=0
2. Case:2 If (j==i+1)
a. Wij=qi, + qj + Pj
b. Cij =qi, + qj + Pj
c. Rij=J
Optimal Binary Search Tree (OBST)
Prepared By Mr. Vipin K. Wani Assistant Professor SOCSE Sandip University
26
To Solve the OBST problem Use the following cases:
3. Case:3 If (For Any value of I & J)
a. Wij=Wij-1 + pj, +qj
b. Cij = i< K < j (Cik-1+ Ckj)+ Wij
c. Rij=K
min
Optimal Binary Search Tree (OBST)
Prepared By Mr. Vipin K. Wani Assistant Professor SOCSE Sandip University
27
Construct OBST for the given instances, If N=4, Set of Identifiers {a1, a2, a3,
a4}= {do, if, int, while }, P(1:4)={p1,p2,p3,p4}={3,3,1,1} and Q(0:4)=
{q0,q1,q2,q3,q4}={2,3,1,1,1} Construct a Optimal Binary Search Tree .
Solution: To find a Solution and to construct Optimal Binary Search Tree
follow the three cases mention in previous slide and Calculate Wij, Cij & Rij for
each case and each instance.
Solution for Optimal Binary Search Tree (OBST)
Prepared By Mr. Vipin K. Wani Assistant Professor SOCSE Sandip University
28
a. Consider Case 1: If (i==j) then Wij=qi, Cij =0 & Rij=0
1. If i=j=0 then
W00=q0=2, C00=0 & R00=0
2. If i=j=1 then
W11=q1=3, C11=0 & R11=0
3. If i=j=2 then
W22=q2=1, C22=0 & R22=0
4. If i=j=3 then
W33=q3=1, C33=0 & R33=0
5. If i=j=4 then
W44=q4=1, C44=0 & R44=0
Solution for Optimal Binary Search Tree (OBST)
Prepared By Mr. Vipin K. Wani Assistant Professor SOCSE Sandip University
29
b. Consider Case 2: If (j==i+1) then Wij=qi, + qj + Pj, Cij =qi, + qj + Pj & Rij=J
1. If i=0 and j=1 then
W01=q0 + q1 + P1 = 2+3+3=8 ,
C01= q0 + q1 + P1 = 2+3+3=8 &
R01=j=1
2. If i=1 and j=2 then
W12=q1 + q2 + P2 = 3+1+3=7 ,
C12=q1 + q2 + P2 = 3+1+3=7 &
R12=j=2
Solution for Optimal Binary Search Tree (OBST)
Prepared By Mr. Vipin K. Wani Assistant Professor SOCSE Sandip University
30
3. If i=2 and j=3 then
W23=q2 + q3 + P3 = 1+1+1=3 ,
C23=q2 + q3 + P3 = 1+1+1=3 , &
R23=j=3
4. If i=3 and j=4 then
W34=q3 + q4 + P4 = 1+1+1=3 ,
C34=q3 + q4 + P4 = 1+1+1=3 , &
R34=j=4
Solution for Optimal Binary Search Tree (OBST)
Prepared By Mr. Vipin K. Wani Assistant Professor SOCSE Sandip University
31
c. Consider Case 3: If (Any value of I & J) then Wij=Wij-1 + pj, +qj , Rij=K &
Cij = i< K <= j (Cik-1+ Ckj)+ Wij
1. If i=0 and j=i+2=2 then
W02=W01 + P2 + q2 = 8+3+1=12 ,
Now as i<k<j so K =1 or K=2
So Assume K=1 So
C02=(C00+C12) + W02= 0+7+12=19 , &
So Assume K=2 So
C02=(C01+C22) + W02= 8+0+12=20 ,
So Consider C02=19 (Min) & R02=K=1
min
min
Solution for Optimal Binary Search Tree (OBST)
Prepared By Mr. Vipin K. Wani Assistant Professor SOCSE Sandip University
322. If i=1 and j=i+2=3 then
W13=W12 + P3 + q3 = 7+1+1=9 ,
Now as i<k<j so K =2 or K=3
So Assume K=2 So
C13=(C11+C23) + W13 = 0+3+9=12 , &
So Assume K=3 So
C13=(C12+C33) + W13= 7+0+9=16 ,
So Consider C13=12 (Min) & R13=K=2
min
Solution for Optimal Binary Search Tree (OBST)
Prepared By Mr. Vipin K. Wani Assistant Professor SOCSE Sandip University
333. If i=2 and j=i+2=4 then
W24=W23 + P4 + q4 = 3+1+1=5 ,
Now as i<k<j so K =3 or K=4
So Assume K=3 So
C24=(C22+C34) + W24 = 0+3+5=8 , &
So Assume K=4 So
C24=(C23+C44) + W24= 3+0+5=8 ,
So Consider C24=8 (Min) & R24=K=3 (Min)
min
Solution for Optimal Binary Search Tree (OBST)
Prepared By Mr. Vipin K. Wani Assistant Professor SOCSE Sandip University
341. If i=0 and j=i+3=3 then
W03=W02 + P3 + q3 = 12+1+1=14 ,
Now as i<k<j so K =1 or K=2 or K=3
So Assume K=1 So
C03=(C00+C13) + W03 = 0+12+14=26 , &
Now Assume K=2 So
C03=(C01+C23) + W03= 8+3+14=25 ,
Now Assume K=3 So
C03=(C02+C33) + W03= 19+0+14=33 ,
So Consider C03=25 (Min) & R03=K=2 (Min)
min
Solution for Optimal Binary Search Tree (OBST)
Prepared By Mr. Vipin K. Wani Assistant Professor SOCSE Sandip University
352. If i=1 and j=i+3=4 then
W14=W13 + P4 + q4 = 9+1+1=11 ,
Now as i<k<j so K =2 or K=3 or K=4
So Assume K=2 So
C14=(C11+C24) + W14 = 0+8+11=19 , &
Now Assume K=3 So
C14=(C12+C34) + W14= 7+3+11=21 ,
Now Assume K=4 So
C14=(C13+C44) + W14= 12+0+11=23 ,
So Consider C14=19 (Min) & R14=K=2
min
Solution for Optimal Binary Search Tree (OBST)
Prepared By Mr. Vipin K. Wani Assistant Professor SOCSE Sandip University
361. If i=0 and j=i+4=4 then
W04=W03 + P4 + q4 = 14+1+1=16 ,
Now as i<k<j so K=1or K =2 or K=3 or K=4
So Assume K=1 So
C04=(C00+C14) + W04 = 0+19+16=35 , &
Now Assume K=2 So
C04=(C01+C24) + W04= 8+8+16=32 ,
Now Assume K=3 So
C04=(C02+C34) + W04= 19+3+16=38 ,
Now Assume K=4 So
C04=(C03+C44) + W04= 25+0+16=41 ,
So Consider C14=32 (Min) & R14=K=2
min
Solution for Optimal Binary Search Tree (OBST)
Prepared By Mr. Vipin K. Wani Assistant Professor SOCSE Sandip University
37
Weight Table
Solution for Optimal Binary Search Tree (OBST)
Prepared By Mr. Vipin K. Wani Assistant Professor SOCSE Sandip University
38
Root / Cost Table
Solution for Optimal Binary Search Tree (OBST)
Prepared By Mr. Vipin K. Wani Assistant Professor SOCSE Sandip University
39
Now to construct the Tree remember following Rules
1. root=R0N
2. Rij=K
3. Left Child= Rik-1
4. Right Child= Rkj
So Now Construct tree using above Root /cost table
Root Node=R0N= R04= 2
So which means identifier a2 i.e. if will be root
And Rij=K i.e. R04= K=2 i.e i=0 & j=4
Solution for Optimal Binary Search Tree (OBST)
Prepared By Mr. Vipin K. Wani Assistant Professor SOCSE Sandip University
40
Left Child= Rik-1
Right Child= Rkj
So Now i=0 & j=4 & k=2
So Left Child= Rik-1 = R02-1 = R01 = 1
So which means identifier a1 i.e. do will be left child of root node which is if.
Right Child= Rkj = R24 = 3
So which means identifier a3 i.e. int will be right child of root node which is if.
if
do int
Solution for Optimal Binary Search Tree (OBST)
Prepared By Mr. Vipin K. Wani Assistant Professor SOCSE Sandip University
41
Now Consider Node do as Root and let us find its left & right child.
Root Node= R01 = K = 1 i.e. i=0 & j=1
So Left Child= Rik-1 = R00 = 0
So which means Left Child of Root Node is NULL.
Right Child= Rkj = R11 = 0
So which means Right Child of Root Node is NULL.
if
do int
NULL NULL
Solution for Optimal Binary Search Tree (OBST)
Prepared By Mr. Vipin K. Wani Assistant Professor SOCSE Sandip University
42
Now Consider Node int as Root and let us find its left & right child.
Root Node= R24 = K = 3 i.e. i=2 & j=4
So Left Child= Rik-1 = R22 = 0
So which means Left Child of Root Node is NULL.
Right Child= Rkj = R34 = 4
So which means Right Child of Root Node is NULL.
So which means identifier a4 i.e. while will be right child of root node which is int.
Solution for Optimal Binary Search Tree (OBST)
Prepared By Mr. Vipin K. Wani Assistant Professor SOCSE Sandip University
43
So the Expected OBST is as follow.
if
do int
NULL NULL NULL while
Thank You…!
Prepared By Mr. Vipin K. Wani Assistant Professor SOCSE Sandip University
44