You are on page 1of 60

Dynamic Programming

---------------
Warshall Algorithm
Warshall Algorithm
R[b,b] OR ( R[b,a] AND R[a,b] )
0 OR ( 0 AND 1 )
0 OR 0

a b c d
a 0 1 0 0
b 0
c 0
d 1
Warshall Algorithm
R[b,c] OR ( R[b,a] AND R[a,c] )
0 OR ( 0 AND 0 )
0 OR 0

a b c d
a 0 1 0 0
b 0 0
c 0
d 1
Warshall Algorithm
R[b,d] OR ( R[b,a] AND R[a,d] )
1 OR ( 0 AND 0 )
1 OR 0

a b c d
a 0 1 0 0
b 0 0 0
c 0
d 1
Warshall Algorithm
R[c,b] OR ( R[c,a] AND R[a,b] )
0 OR ( 0 AND 1 )
0 OR 0

a b c d
a 0 1 0 0
b 0 0 0 1
c 0
d 1
Warshall Algorithm
R[c,c] OR ( R[c,a] AND R[a,c] )
0 OR ( 0 AND 0 )
0 OR 0

a b c d
a 0 1 0 0
b 0 0 0 1
c 0 0
d 1
Warshall Algorithm
R[c,d] OR ( R[c,a] AND R[a,d] )
0 OR ( 0 AND 0 )
0 OR 0

a b c d
a 0 1 0 0
b 0 0 0 1
c 0 0 0
d 1
Warshall Algorithm
R[d,b] OR ( R[d,a] AND R[a,b] )
0 OR ( 1 AND 1 )
0 OR 1

a b c d
a 0 1 0 0
b 0 0 0 1
c 0 0 0 0
d 1
Warshall Algorithm
R[d,c] OR ( R[d,a] AND R[a,c] )
1 OR ( 1 AND 0 )
1 OR 0

a b c d
a 0 1 0 0
b 0 0 0 1
c 0 0 0 0
d 1 1
Warshall Algorithm
R[d,d] OR ( R[d,a] AND R[a,d] )
0 OR ( 1 AND 0 )
0 OR 0

a b c d
a 0 1 0 0
b 0 0 0 1
c 0 0 0 0
d 1 1 1
Warshall Algorithm

a b c d
a 0 1 0 0
b 0 0 0 1
c 0 0 0 0
d 1 1 1 0
Warshall Algorithm

a b c d
a 0 1 0 0
b 0 0 0 1
c 0 0 0 0
d 1 1 1 0
Warshall Algorithm

a b c d
a 1
b 0 0 0 1
c 0
d 1
Warshall Algorithm
R[a,a] OR ( R[a,b] AND R[b,a] )
0 OR ( 1 AND 0 )
0 OR 0

a b c d
a 1
b 0 0 0 1
c 0
d 1
Warshall Algorithm
R[a,c] OR ( R[a,b] AND R[b,c] )
0 OR ( 1 AND 0 )
0 OR 0

a b c d
a 0 1
b 0 0 0 1
c 0
d 1
Warshall Algorithm
R[a,d] OR ( R[a,b] AND R[b,d] )
0 OR ( 1 AND 1 )
0 OR 1

Now check One by One


All other values
a b c d
a 0 1 0
b 0 0 0 1
c 0
d 1
Warshall Algorithm

Now check One by One


All other values

a b c d
a 0 1 0 1
b 0 0 0 1
c 0 0 0 0
d 1 1 1 1
Warshall Algorithm

a b c d
a 0 1 0 1
b 0 0 0 1
c 0 0 0 0
d 1 1 1 1
Warshall Algorithm
R[a,a] OR ( R[a,c] AND R[c,a] )
0 OR ( 0 AND 0 )
0 OR 0

a b c d
a 0
b 0
c 0 0 0 0
d 1
Warshall Algorithm
R[a,b] OR ( R[a,c] AND R[c,b] )
1 OR ( 0 AND 0 )
1 OR 0

a b c d
a 0 0
b 0
c 0 0 0 0
d 1
Warshall Algorithm
R[a,d] OR ( R[a,c] AND R[c,d] )
1 OR ( 0 AND 0 )
1 OR 0

Now check One by One


All other values
a b c d
a 0 1 0
b 0
c 0 0 0 0
d 1 0
Warshall Algorithm

Now check One by One


All other values

a b c d
a 0 1 0 1
b 0 0 0 1
c 0 0 0 0
d 1 1 1 1
Warshall Algorithm

a b c d
a 0 1 0 1
b 0 0 0 1
c 0 0 0 0
d 1 1 1 1
Warshall Algorithm
R[a,a] OR ( R[a,d] AND R[d,a] )
0 OR ( 1 AND 1 )
0 OR 1

a b c d
a 1
b 1
c 0
d 1 1 1 1
Warshall Algorithm
R[a,b] OR ( R[a,d] AND R[d,b] )
1 OR ( 1 AND 1 )
1 OR 1

Now check One by One


All other values
a b c d
a 1 1
b 1
c 0
d 1 1 1 1
Warshall Algorithm

Now check One by One


All other values

a b c d
a 1 1 1 1
b 1 1 1 1
c 0 0 0 0
d 1 1 1 1
Warshall Algorithm
Warshall Algorithm
Warshall Algorithm
Floyd Algorithm
 Floyd Algorithm is a famous algorithm.
 It is used to solve All Pairs Shortest Path
Problem.
 It computes the shortest path between
every pair of vertices of the given graph.
 Floyd Algorithm is an example of
dynamic programming approach.
Floyd Algorithm
 Consider the following directed weighted graph

 Using Floyd Algorithm, find the shortest path


distance between every pair of vertices
Floyd Algorithm- Solution
 Step-01:

 Remove all the self loops and parallel


edges (keeping the lowest weight edge)
from the graph.
 In the given graph, there are neither self
edges nor parallel edges.
Floyd Algorithm- Solution
 Step-02:
 Write the initial distance matrix.
 It represents the distance between every pair of vertices in
the form of given weights.
 For diagonal elements (representing self-loops), distance
value = 0.
 For vertices having a direct edge between them, distance
value = weight of that edge.
 For vertices having no direct edge between them, distance
value = ∞.
Floyd Algorithm- Solution
 Initial distance matrix for the given graph is
Floyd Algorithm- Solution
D[2,3] D[2,1] + D[1,3]
1 < ∞ +∞

1 2 3 4
1 0 8 1

2 0

3 4 0
4 0

Floyd Algorithm- Solution
D[2,4] D[2,1] + D[1,4]
∞ = ∞ + 1

1 2 3 4
1 0 8 1

2 0 1

3 4 0
4 0

Floyd Algorithm- Solution

D[3,2] D[3,1] + D[1,2]


∞ > 4 + 8

1 2 3 4
1 0 8 1

2 0 1
∞ ∞
3 4 0
4 0

Floyd Algorithm- Solution
D[3,4] D[3,1] + D[1,4]
∞ > 4 + 1

1 2 3 4
1 0 8 1

2 0 1
∞ ∞
3 4 12 0
4 0

Floyd Algorithm- Solution
D[4,2] D[4,1] + D[1,2]
2 < ∞ + 8

1 2 3 4
1 0 8 1

2 0 1
∞ ∞
3 4 12 0 5
4 0

Floyd Algorithm- Solution
D[4,3] D[4,1] + D[1,3]
9 < ∞ + ∞

1 2 3 4
1 0 8 1

2 0 1
∞ ∞
3 4 12 0 5
4 2 0

Floyd Algorithm- Solution
D[4,3] D[4,1] + D[1,3]
9 < ∞ + ∞

1 2 3 4
1 0 8 1

2 0 1
∞ ∞
3 4 12 0 5
4 2 9 0

Floyd Algorithm- Solution

1 2 3 4
1 0 8 1

2 0 1
∞ ∞
3 4 12 0 5
4 2 9 0

Floyd Algorithm- Solution

1 2 3 4
1 0 8 1

2 0 1
∞ ∞
3 4 12 0 5
4 2 9 0

Floyd Algorithm- Solution
D[1,3] D[1,2] + D[2,3]
∞ > 8 + 1

1 2 3 4
1 0 8

2 0 1
∞ ∞
3 12 0
4 2 0
Floyd Algorithm- Solution
D[1,4] D[1,2] + D[2,4]
1 > 8 + ∞
Now check One by One
All other values

1 2 3 4
1 0 8
9
2 0 1
∞ ∞
3 12 0
4 2 0
Floyd Algorithm- Solution

Now check One by One


All other values

1 2 3 4
1 0 8
9 1
2 0 1 ∞

3 4 12 0 5
4 2 3 0

Floyd Algorithm- Solution

1 2 3 4
1 0 8
9 1
2 0 1 ∞

3 4 12 0 5
4 2 3 0

Floyd Algorithm- Solution

1 2 3 4
1 0 8
9 1
2 0 1 ∞

3 4 12 0 5
4 2 3 0

Floyd Algorithm- Solution
D[1,2] D[1,3] + D[3,2]
8 < 9 + 12

1 2 3 4
1 0
9
2 0 1
3 4 12 0 5
4 3 0
Floyd Algorithm- Solution
D[1,4] D[1,3] + D[3,4]
1 < 9 + 5

Now check One by One


All other values

1 2 3 4
1 0 8 9
2 0 1
3 4 12 0 5
4 3 0
Floyd Algorithm- Solution

Now check One by One


All other values

1 2 3 4
1 0 8 9 1
2 5 0 1 6
3 4 12 0 5
4 7 2 3 0
Floyd Algorithm- Solution

1 2 3 4
1 0 8 9 1
2 5 0 1 6
3 4 12 0 5
4 7 2 3 0
Floyd Algorithm- Solution

1 2 3 4
1 0 8 9 1
2 5 0 1 6
3 4 12 0 5
4 7 2 3 0
Floyd Algorithm- Solution
2 2 2
D [1,2] D [1,4] + D [4,2]
8 > 1 + 2

1 2 3 4
1 0
1
2 0 6
3 0 5
4 7 2 3 0
Floyd Algorithm- Solution
D[1,3] D[1,4] + D[4,3]
9 > 1 + 3

Now check One by One


All other values

1 2 3 4
1 0 3 1
2 0 6
3 0 5
4 7 2 3 0
Floyd Algorithm- Solution

Now check One by One


All other values

1 2 3 4
1 0 3 4 1
2 5 0 1 6
3 4 7 0 5
4 7 2 3 0
Floyd Algorithm- Solution

The last matrix D4 represents the shortest path


distance between every pair of vertices
Floyd Algorithm

 Taking into account the lengths of the


shortest paths in both subsets leads to
the following recurrence:
Floyd Algorithm

You might also like