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

Module_3

Module 3 covers dynamic programming, focusing on techniques like the Floyd-Warshall algorithm for finding all pairs shortest paths in weighted graphs and constructing optimal binary search trees. It explains how dynamic programming solves complex problems by breaking them into simpler subproblems and storing their solutions to improve efficiency. The document includes examples illustrating the application of these algorithms in reducing computational costs.

Uploaded by

bharathsekar9009
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

Module_3

Module 3 covers dynamic programming, focusing on techniques like the Floyd-Warshall algorithm for finding all pairs shortest paths in weighted graphs and constructing optimal binary search trees. It explains how dynamic programming solves complex problems by breaking them into simpler subproblems and storing their solutions to improve efficiency. The document includes examples illustrating the application of these algorithms in reducing computational costs.

Uploaded by

bharathsekar9009
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 32

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.

For example, if we write simple recursive solution for Fibonacci Numbers,


we get exponential time complexity and if we optimize it by storing
solutions of subproblems, time complexity reduces to linear.
All pairs shortest path
(Floyd-Warshall algorithm)
The all pair shortest path algorithm is also known as Floyd-Warshall
algorithm is used to find all pair shortest path problem from a given
weighted graph.
As a result of this algorithm, it will generate a matrix, which will
represent the minimum distance from any node to all other nodes in
the graph.
Example
1 2 3
1 0 4 5
W=A =
0

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

A1[3,2] = min( A0[3,2], A0[3,1]+A0[1,2] )


= min (-3,+4)
= min (-3,)
= -3

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

A2[3,1] = min( A1[3,1], A1[3,2]+A1[2,1] )


= min (, -3+2)
= -1

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

A3[2,1] = min(A2[2,1], A2[2,3]+A2[3,1] )


= min (2, 7+ (-1))
=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

The frequency and key-value determine the overall cost of searching a


node.
The overall cost of searching a node should be less.

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

• All the nodes on the left subtree are smaller


than the value of the root node, and all the
nodes on the right subtree are larger than
the value of the root node.

• The maximum time required to search a


node is equal to the minimum height of the
tree = log(n)
How many binary search trees can be made from the given number of keys
5 number of trees
10, 20, 30 are the keys
The Formula for calculating the number of trees: can be created

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.

When i=0, j=0, then j-i = 0

When i = 1, j=1, then j-i = 0


j
When i = 2, j=2, then j-i = 0 0

When i = 3, j=3, then j-i = 0 0

When i = 4, j=4, then j-i = 0 0

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.

When j=1, i=0 then j-i = 1

When j=2, i=1 then j-i = 1

When j=3, i=2 then j-i = 1

When j=4, i=3 then j-i = 1


In this case, we will consider only one key.
Now to calculate the cost, we will consider only the
jth value.
The cost of c[0,1] is 4 (The key is 10, and the cost corresponding to key 10 is 4).

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 j=3, i=1 then j-i = 2

When j=4, i=2 then j-i = 2


C[i, j] = min{ c[i,k-1] + c[k,j] } + w[I,j]
i<k<=j 4
w[0, 4] = Σ
In this case, we will consider two keys. f(i) i=1

•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

C[0, 2] = {c[0,0] + c[1,2]}+ w[0,2] = {0 + 2} + 6= 8


10 C[0, 2] = {c[0,1] + c[2,2]}+ w[0,2] = {4 + 0} + 6= 10
C[0, 2] = min{c[0,0] + c[1,2], c[0,1] + c[2,2]}+ w[0,2]
= min{0+2, 4+0} + 6 = min{2,4}+6 = 2+6 =8
• When i=1 and j=3, then keys 20 and 30. There are two C[1, 3] = min{c[1,1] + c[2,3], c[1,2] + c[3,3]}+ w[1,3]
possible trees that can be made out from these two keys = min{0+6, 2+0} + 6 = min{6,2}+8= 2+8 =10
shown below:

In the first binary tree, cost would be: 1*2 + 2*6 = 14

In the second binary tree, cost would be: 1*6 + 2*2 = 10

The minimum cost is 10; therefore, c[1,3] = 10

• 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:

In the first binary tree, cost would be: 1*6 + 2*3 = 12

In the second binary tree, cost would be: 1*3 + 2*6 = 15

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 j=3, i=0 then j-i = 3

When j=4, i=1 then j-i = 3

• When i=0, j=3 then we will consider three keys, i.e., 10, 20, and 30.

The following are the trees that can be made if 10 is


considered as a root node.

10 is the root node, 30 is the


10 is the root node, 20 is right child of node 10, and 20 is
the right child of node 10, the left child of node 20.
and 30 is the right child of
node 20. Cost is: 1*4 + 2*6 + 3*2 = 22
Cost is: 1*4 + 2*2 + 3*6 = 26
20 is considered as the root node 30 is considered as the root node

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.

C[0, 3] = min{c[0,0] + c[1,3], c[0,1] + c[2,3], c[0,2] + c[3,3]}+ w[0,3]


= min{0+10, 4+6,8+0 } + 6 = min{10,10,8}+= 8+12 = 12
• When i=1 and j=4 then we will consider the keys
20, 30, 40

c[1,4] = min{ c[1,1] + c[2,4], c[1,2] + c[3,4], c[1,3] + c[4,4] } + w[1,4]


= min{0+12, 2+3, 10+0}+ 11

= min{12, 5, 10} + 11

The minimum value is 5; therefore, c[1,4] = 5+11 = 16

C[i, j] = min{ c[i,k-1] + c[k,j] } + w[I,j]


i<k<=j
•Now we will calculate the values when j-i = 4 c[0,4] = min{ c[0,0] + c[1,4], c[0,1] + c[2,4], c[0,2] + c[3,4],
c[0,3] + c[4,4] } + w[0,4]
= min{0+16, 4+12, 8+3, 20+0}+ 15
When j=4 and i=0 then j-i = 4 = min{16, 16, 11, 20} + 15 = 11+15 = 26

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.

w[0, 4] = 4 + 2 + 6 + 3 = 15 If we consider 20 as the root node then

C[0,4] = min{c[0,1] + c[2,4]} + w[0,4]


If we consider 10 as the root node then
= min{4 + 12} + 15
C[0, 4] = min {c[0,0] + c[1,4]}+ w[0,4]
= 16 + 15 = 31
= min {0 + 16} + 15= 31
If we consider 40 as the root node then,
If we consider 30 as the root node
C[0,4] = min{c[0,3] + c[4,4]} + w[0,4]
then,
C[0,4] = min{c[0,2] + c[3,4]}
= min{20 + 0} + 15
+w[0,4]
= min {8 + 3} + 15
= 35
= 26
In the above cases, we have observed that
26 is the minimum cost; therefore, c[0,4] is The optimal binary tree can be created as:
equal to 26. r[0,4]

General formula for calculating the


minimum cost is:

C[i,j] = min{c[i, k-1] + c[k,j]} + w(i,j)


Multistage graph
Example: Find minimum path cost between vertex s and t for following multistage
graph using dynamic programming.
Solution to multistage graph using dynamic programming is constructed as,

Cost[j] = min{c[j, r] + cost[r]}


where, c[j, r] is the weight of edge <j, r> and cost[r] is the cost of moving from end vertex to vertex r.

Here, number of stages k = 5, number of vertices n = 12, source s = 1 and target t = 12


2
3 4
Stage 5:
1
Initialization: 5

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:

Vertex 6 is connected to vertices 9 and 10:


Cost[6] = min{ c[6, 10] + Cost[10], c[6, 9] +
Cost[9] }
= min{5 + 2, 6 + 4} = min{7, 10} = 7 2
3 4
Vertex 7 is connected to vertices 9 and 10:
Cost[7] = min{ c[7, 10] + Cost[10], c[7, 9] +
5
Cost[9] } 1
= min{3 + 2, 4 + 4} = min{5, 8} = 5

Vertex 8 is connected to vertex 10 and 11:


Cost[8] = min{ c[8, 11] + Cost[11], c[8, 10] +
Cost[10] }
= min{6 + 5, 5 + 2} = min{11, 7} = 7
V 1 2 3 4 5 6 7 8 9 10 11 12
Cost 7 5 7 4 2 5 0
d 10 10 10 12 12 12 12
Stage 2:
Vertex 5 is connected to vertices 7 and 8:
Vertex 2 is connected to vertices6, 7 and 8: Cost[5] = min{ c[5, 7] + Cost[7], c[5, 8] +
Cost[2] = min{ c[2, 6] + Cost[6], c[2, 7] + Cost[7], Cost[8] }
c[2, 8] + Cost[8] } 2 = min{11 + 5, 8 + 7} = min{16, 15} = 15
= min{4 + 7, 2 + 5, 1 + 7} = min{11, 7, 8} = 7
3 4

Vertex 3 is connected to vertices 6and 7:


Cost[3] = min{ c[3, 6] + Cost[6], c[3, 7] + 5
Cost[7] } 1
= min{2 + 7, 7 + 5} = min{9, 12} = 9

Vertex 4 is connected to vertex 8:


Cost[4] = c[4, 8] + Cost[8] = 11 + 7 = 18

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

Cost of the path


is : 9 + 2 + 3 + 2
= 16

Minimum cost path is : 1 – 2 – 7 – 10 – 12


Exercise

1) Find minimum path cost between 2) Find the shortest paths


vertex s and t for following multistage between all pairs of vertices
graph using dynamic programming. in the given graph

You might also like