Module_3
Module_3
DYNAMIC
PROGRAMMING
Syllabus
The General method- All pairs shortest path- Optimal binary Search
tree- Multistage graphs.
Dynamic Programming
The definition of dynamic programming says that it is a technique for
solving a complex problem by first breaking into a collection of simpler
subproblems, solving each subproblem just once, and then storing their
solutions to avoid repetitive computations. The method was developed
by Richard Bellman in the 1950s and has found applications in numerous
fields, from aerospace engineering to economics.
5 2 2 0
1
3 -3 0
4 2 3
-3
2
Floyd’s Algorithm 5
1 5 1 2 3
A =1
0
0 4 5 Vertex 1 can be
4 2 3 intermediate
2 2 0
-3 node
2 3 -3 0
1 2 3
1 0 4 5 A1[2,3] = min( A0[2,3], A0[2,1]+A0[1,3] )
A1 = = min (, 2+5)
2 2 0 7
=7
3 -3 0
Floyd’s Algorithm 6
1 2 3
1 5 A1 = 1 0 4 5
4 2 3 2 2 0 7 Vertices 2 can be
2 -3 3 -3 0 intermediate
1 2 3
1 0 4 5
A = 2 A2[1,3] = min( A1[1,3], A1[1,2]+A1[2,3] )
2 2 0 7 = min (5, 4+7)
3 -1 -3 0 =5
Floyd’s Algorithm 7
1 5 A2 = 1 2 3
1 0 4 5
4 2 3
-3
2 2 0 7 Vertices 3 can
2 3 -1 -3 0
be intermediate
1 2 3
1 0 2 5 A3[1,2] = min(A2[1,2], A2[1,3]+A2[3,2] )
A = 3
2 2 0 7 = min (4, 5+(-3))
3 -1 -3 0 =2
Floyd’s Algorithm 8
• On the k-th iteration, the algorithm determines
shortest paths between every pair of vertices i, j
that use only vertices among 1,…,k as intermediate
• D(k)[i,j] = min {D(k-1)[i,j], D(k-1)[i,k] + D(k-1)[k,j]}
Time efficiency O(n3)
Optimal Binary search trees
In binary search tree, the nodes in the left subtree have lesser value than the root node and
the nodes in the right subtree have greater value than the root node
Reduce the cost of a binary search tree is known as an optimal binary search tree.
If the keys are 10, 20, 30, 40, 50, 60, 70
The cost required for searching an element depends on the comparisons to be made to
search an element.
In the third case, the number of comparisons is less because the height of the tree is less, so it's a
balanced binary search tree or height-balanced binary search tree.
To find the optimal binary search tree, we will determine the frequency of searching a key.
Let's assume that frequencies associated with the keys 10, 11, 12 are 2, 1, 6.
The above trees have different frequencies.
The tree with the lowest frequency would be considered the optimal binary search tree.
The tree with the cost 13 is the lowest, so it would be considered as the optimal binary search
tree.
Cost =13
Dynamic Approach
Consider the below table, which contains the keys and
frequencies. Table starts from 0 and is
suitable for formula
j
First, we will calculate the values where j-i is equal
to zero.
0
Therefore, c[0, 0] = 0, c[1 , 1] = 0, c[2,2] = 0, c[3,3] = 0, c[4,4] =0
0
Now we will calculate the values where j-i equal to 1.
The cost of c[1,2] is 2 (The key is 20, and the cost corresponding to key 20 is 2).
The cost of c[2,3] is 6 (The key is 30, and the cost corresponding to key 30 is 6)
The cost of c[3,4] is 3 (The key is 40, and the cost corresponding to key 40 is 3)
Now we will calculate the values where j-i = 2
1
When j=2, i=0 then j-i = 2
•When i=0 and j=2, then keys 10 and 20. There are two possible trees that can be made out
from these two keys shown below:
In the first binary tree, cost would be: 4*1 + 2*2 = 8
20 In the second binary tree, cost would be: 2*1+ 4*2 = 10
The minimum cost is 8; therefore, c[0,2] = 8
• When i=2 and j=4, we will consider the keys at 3 and 4, i.e., 30
and 40. There are two possible trees that can be made out from
these two keys shown as below:
The minimum cost is 12, therefore, c[2,4] = 12 C[2, 4] = min{c[2,2] + c[3,4], c[2,3] + c[4,4]}+ w[2,4]
= min{0+3, 6+0} + 6 = min{3,6}+9= 3+9=12
Now we will calculate the values when j-i = 3
• When i=0, j=3 then we will consider three keys, i.e., 10, 20, and 30.
20 is the root node, 30 is the right 30 is the root node, 20 is the left 30 is the root node, 10 is the left
child of node 20, and 10 is the left child of node 30, and 10 is the left child of node 30 and 20 is the right
child of node 20. child of node 20. child of node 10.
Cost is: 1*2 + 4*2 + 6*2 = 22 Cost is : 1*6 + 2*2 + 3*4 = 22 Cost would be: 1*6 + 2*4 + 3*2 = 20
Therefore, the minimum cost is 20 which is the 3rd root. So, c[0,3] is equal to 20.
= min{12, 5, 10} + 11
In this case, consider four keys, i.e., 10, 20, 30 and 40. The frequencies of 10, 20, 30 and 40 are 4, 2, 6 and 3 respectively.
Cost[12] = 0 ⇒ Cost[5,12] = 0.
V 1 2 3 4 5 6 7 8 9 10 11 12
Cost 0
d 12
Stage 4:
Cost[4,9] = 4 2
3 4
Cost[4,10] = 2
5
Cost[4,11] = 5 1
V 1 2 3 4 5 6 7 8 9 10 11 12
Cost 4 2 5 0
d 12 12 12 12
Stage 3:
V 1 2 3 4 5 6 7 8 9 10 11 12
Cost 7 9 18 15 7 5 7 4 2 5 0
d 7 6 8 8 10 10 10 12 12 12 12
V 1 2 3 4 5 6 7 8 9 10 11 12
Cost 16 7 9 18 15 7 5 7 4 2 5 0
d 2/3 7 6 8 8 10 10 10 12 12 12 12
2
3 4
Stage 1:
Vertex 1 is connected to vertices 2, 3, 4 and 5: 5
1
Cost[1] = min{ c[1, 2] + Cost[2], c[1, 3] +
Cost[3], c[1, 4] + Cost[4], c[1, 5] + Cost[5]}
= min{ 9 + 7, 7 + 9, 3 + 18, 2 + 15 }
= min { 16, 16, 21, 17 } = 16