Module 4
Dynamic programming
Design and Analysis of Algorithm(18CS42)
Lecture Presentation
Divya K S
Dept. of CSE
Contents
1. Definition of Dynamic Programming
2. Transitive closure: Warshall’s Algorithm
3. All pair shortest paths: Warshall’s and Floyd’s Algorithm
Dynamic Programming
1. It is a technique for solving problems with overlapping subproblems.
2. Example: Floyd’s all pair shortest path problem.
Transitive closure -----Graph
E1=(a,b) and E2=(b,c) then P= (a,c)
A B
C
Example for Warshall’s algorithm
Given digraph:
Step 1: Adjacency matrix of the given graph
a b c d
a 0 1 0 0
b 0 0 0 1
c 0 0 0 0
d 1 0 1 0
R(0)=
(a,b),(d,a)
(d,a)-(a,b)---(d,b)
a b c d
a 0 1 0 0
b 0 0 0 1
c 0 0 0 0
d 1 1 1 0
R(1)=
a b c d
a 0 1 0 1
b 0 0 0 1
c 0 0 0 0
d 1 1 1 1
R(2)=
(a,b) (b,d) (d,b)
1. (a,b) (b,d) --(a,d)
2. (d,b) (b,d) ---(d,d)
a b c d
a 0 1 0 1
b 0 0 0 1
c 0 0 0 0
d 1 1 1 1
R(3)=
(d,c)
a b c d
a 1 1 1 1
b 1 1 1 1
c 0 0 0 0
d 1 1 1 1
R(4)=
(a,d) (b,d) (d,a)(d,b)(d,c)(d,d)
1. (a,d)(d,a)--(a,a)
2. (b,d)(d,a)-(b,a)
3. (a,d)(d,c)-(a,c)
4. (b,d)(d,c)-(b,c)
5. (b,d)(d,b)-(b,b)
Pseudocode of warshall’s algorithm
Assignment
Dijkstra’s----Single source shortest path
All pair shortest path--- Floyd’s
A B
C
D
A—B
A—C
A—D
B—A
B—C
B—D
C—A
C—B
C—D
D—A
D—B
D—C
--Dis
All pair shortest path
Problem Definition:
Given a weighted connected graph(undirected or directed), the all-pair shortest-paths problem
asks to find the distances from each vertex to all other vertices.
Distance Matrix:
It is a n-by-n matrix which records the lengths of the shortest paths such that : the element dij
In the ith row and jith column of this matrix indicates the length of the shortest path from the
ith vertex to the jth vertex(1<=I,j<=n).
Floyd’s algorithm
 It is applicable to both directed and undirected graph.
 Input graph is connected weighted graph.
 Weights can be of both positive and negative weights.
 But graph should not contain negative-length cycle.
 Used to solve all pair shortest path.
 Uses warshall’s algorithm idea of transitive closure.
2
-1
-3
Example
Step 1: Distance Matrix
a b c d
a 0 in 3 in
b 2 0 in in
c in 7 0 1
d 6 in in 0
a b c d
a 0 in 3 4
b 2 0 5 in
c in 7 0 1
d 6 in 9 0
Step 2:
D(1)=
D(0)=
D(1)[b,c]:
D(0)[b,c] D(0)[b,a]+D(0)[a,c]
Infini 2+3=5
D(1)[b,d]:
D(0)[b,d] D(0)[b,a]+D(0)[a,d]
Infini 2+infini
D(1)[c,b]:
D(0)[c,b] D(0)[c,a]+D(0)[a,b]
7 ini+ini
D(1)[c,d]:
D(0)[c,d] D(
1
D(1)[d,b]:
D(0)[d,b] D
Ini in
D(1)[d,c]:
D(0)[d,c] D
Ini
a b c d
a 0 10 3 4
b 2 0 5 6
c 7 7 0 1
d 6 16 9 0
a b c d
a 0 in 3
b 2 0 5 in
c 7 0
d in 0
a b c d
a
b
c
d
D(2)=
D(3)=
D(4)=
D(2)[a,c]
D(1)[a,c] D(1)[a,b]+D(1)[b,c]
3 ini+5
Pseudo code
Assignment
1. Write an algorithm for all pair shortest path.

Lecture warshall floyd

  • 1.
    Module 4 Dynamic programming Designand Analysis of Algorithm(18CS42) Lecture Presentation Divya K S Dept. of CSE
  • 2.
    Contents 1. Definition ofDynamic Programming 2. Transitive closure: Warshall’s Algorithm 3. All pair shortest paths: Warshall’s and Floyd’s Algorithm
  • 3.
    Dynamic Programming 1. Itis a technique for solving problems with overlapping subproblems. 2. Example: Floyd’s all pair shortest path problem.
  • 5.
    Transitive closure -----Graph E1=(a,b)and E2=(b,c) then P= (a,c) A B C
  • 6.
    Example for Warshall’salgorithm Given digraph: Step 1: Adjacency matrix of the given graph a b c d a 0 1 0 0 b 0 0 0 1 c 0 0 0 0 d 1 0 1 0 R(0)= (a,b),(d,a) (d,a)-(a,b)---(d,b)
  • 7.
    a b cd a 0 1 0 0 b 0 0 0 1 c 0 0 0 0 d 1 1 1 0 R(1)= a b c d a 0 1 0 1 b 0 0 0 1 c 0 0 0 0 d 1 1 1 1 R(2)= (a,b) (b,d) (d,b) 1. (a,b) (b,d) --(a,d) 2. (d,b) (b,d) ---(d,d) a b c d a 0 1 0 1 b 0 0 0 1 c 0 0 0 0 d 1 1 1 1 R(3)= (d,c) a b c d a 1 1 1 1 b 1 1 1 1 c 0 0 0 0 d 1 1 1 1 R(4)= (a,d) (b,d) (d,a)(d,b)(d,c)(d,d) 1. (a,d)(d,a)--(a,a) 2. (b,d)(d,a)-(b,a) 3. (a,d)(d,c)-(a,c) 4. (b,d)(d,c)-(b,c) 5. (b,d)(d,b)-(b,b)
  • 8.
  • 9.
  • 10.
    Dijkstra’s----Single source shortestpath All pair shortest path--- Floyd’s A B C D A—B A—C A—D B—A B—C B—D C—A C—B C—D D—A D—B D—C --Dis
  • 11.
    All pair shortestpath Problem Definition: Given a weighted connected graph(undirected or directed), the all-pair shortest-paths problem asks to find the distances from each vertex to all other vertices. Distance Matrix: It is a n-by-n matrix which records the lengths of the shortest paths such that : the element dij In the ith row and jith column of this matrix indicates the length of the shortest path from the ith vertex to the jth vertex(1<=I,j<=n).
  • 12.
    Floyd’s algorithm  Itis applicable to both directed and undirected graph.  Input graph is connected weighted graph.  Weights can be of both positive and negative weights.  But graph should not contain negative-length cycle.  Used to solve all pair shortest path.  Uses warshall’s algorithm idea of transitive closure. 2 -1 -3
  • 13.
    Example Step 1: DistanceMatrix a b c d a 0 in 3 in b 2 0 in in c in 7 0 1 d 6 in in 0 a b c d a 0 in 3 4 b 2 0 5 in c in 7 0 1 d 6 in 9 0 Step 2: D(1)= D(0)= D(1)[b,c]: D(0)[b,c] D(0)[b,a]+D(0)[a,c] Infini 2+3=5 D(1)[b,d]: D(0)[b,d] D(0)[b,a]+D(0)[a,d] Infini 2+infini D(1)[c,b]: D(0)[c,b] D(0)[c,a]+D(0)[a,b] 7 ini+ini D(1)[c,d]: D(0)[c,d] D( 1 D(1)[d,b]: D(0)[d,b] D Ini in D(1)[d,c]: D(0)[d,c] D Ini
  • 14.
    a b cd a 0 10 3 4 b 2 0 5 6 c 7 7 0 1 d 6 16 9 0 a b c d a 0 in 3 b 2 0 5 in c 7 0 d in 0 a b c d a b c d D(2)= D(3)= D(4)= D(2)[a,c] D(1)[a,c] D(1)[a,b]+D(1)[b,c] 3 ini+5
  • 15.
  • 16.
    Assignment 1. Write analgorithm for all pair shortest path.