0% found this document useful (0 votes)
12 views23 pages

L14 Warshall

The document summarizes algorithms for finding all-pair shortest paths in a graph: 1) It describes Dijkstra's algorithm and Bellman-Ford algorithm for finding single-source shortest paths with running times of O(ElgV) and O(VE) respectively. 2) It proposes running either algorithm from each vertex to solve the all-pairs problem in O(V^2E) or O(VElgV) time. 3) It introduces the Floyd-Warshall algorithm, a dynamic programming approach that computes shortest paths between all pairs of vertices in O(V^3) time without any elaborate data structures.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views23 pages

L14 Warshall

The document summarizes algorithms for finding all-pair shortest paths in a graph: 1) It describes Dijkstra's algorithm and Bellman-Ford algorithm for finding single-source shortest paths with running times of O(ElgV) and O(VE) respectively. 2) It proposes running either algorithm from each vertex to solve the all-pairs problem in O(V^2E) or O(VElgV) time. 3) It introduces the Floyd-Warshall algorithm, a dynamic programming approach that computes shortest paths between all pairs of vertices in O(V^3) time without any elaborate data structures.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 23

Design & Analysis of Algorithms

CSE 304

All Pairs of Shortest Path


All-Pairs Shortest Paths
• Given: 2
3 4
– Directed graph G = (V, E)
8
1 3
– Weight function w : E → R 2
1
-4 7 -5
• Compute:
5 4
6
– The shortest paths between all pairs
of vertices in a graph
– Representation of the result: an
n × n matrix of shortest-path
distances δ(u, v)

2
Dijkstra (G, w, s)
1. INITIALIZE-SINGLE-SOURCE(V, s) (V)
2. S ← 
3. Q ← V[G] O(V) build min-heap

4. while Q   Executed O(V) times

5. do u ← EXTRACT-MIN(Q) O(lgV)

6. S ← S  {u}
7. for each vertex v  Adj[u]
8. do RELAX(u, v, w) O(E) times; O(lgV)

Running time: O(VlgV + ElgV) = O(ElgV)


3
BELLMAN-FORD(V, E, w, s)
1. INITIALIZE-SINGLE-SOURCE(V, s) (V)
2. for i ← 1 to |V| - 1 O(V)
O(VE)
3. do for each edge (u, v)  E O(E)
4. do RELAX(u, v, w)
5. for each edge (u, v)  E O(E)
6. do if d[v] > d[u] + w(u, v)
7. then return FALSE
8. return TRUE

Running time: O(VE)


4
All-Pairs Shortest Paths - Solutions
• Run BELLMAN-FORD once from each vertex:
– O(V2E), which is O(V4) if the graph is dense
(E = (V2))

• If no negative-weight edges, could run


Dijkstra’s algorithm once from each vertex:
– O(VElgV) with binary heap, O(V3lgV) if the graph is
dense

• We can solve the problem in O(V3), with no


elaborate data structures
5
All-Pairs Shortest Paths
• Assume the graph (G) is given as adjacency
2
4
matrix of weights 3
8
– W = (wij), n x n matrix, |V| = n 1
2
3

– Vertices numbered 1 to n -4 7
1
-5

0 if i = j 5
6
4

wij = weight of (i, j) if i  j , (i, j)  E


∞ if i  j , (i, j)  E
• Output the result in an n x n matrix
D = (dij), where dij = δ(i, j)
• Solve the problem using dynamic programming
6
Optimal Substructure of a Shortest Path
at most m edges
• All subpaths of a shortest

11
j
path are shortest paths i
k

• Let p: a shortest path p


at most m - 1 edges
from vertex i to j that
p’
contains at most m edges • If i  j: p = i kj
– p’ has at most m-1
• If i = j
edges
– w(p) = 0 and p has no – p’ is a shortest path
edges δ(i, j) = δ(i, k) + wkj
7
Recursive Solution
• lij(m) = weight of shortest path i j that contains at
at most m edges
most m edges
• m = 0: lij(0) = 0 if i = j 
11
j

if i  j
 i
k

• m  1: l (m)
= min { lij
(m-1) , min {l (m-1) + w } }
ik kj
ij 1kn
= min {lik(m-1) + wkj}
1kn

– Shortest path from i to j with at most m – 1 edges


– Shortest path from i to j containing at most m edges,
considering all possible predecessors (k) of j
8
Computing the Shortest Paths
• m = 1: lij(1) = wij L(1) = W
– The path between i and j is restricted to 1 edge

• Given W = (wij), compute: L(1), L(2), …, L(n-1), where


L(m) = (lij(m))
• L(n-1) contains the actual shortest-path weights
Given L(m-1) and W  compute L(m)
– Extend the shortest paths computed so far by one more edge
• If the graph has no negative cycles: all simple shortest
paths contain at most n - 1 edges
= lij(n-1)
δ(i, j) = lij(n-1) and lij(n), lij(n+1). . .
9
The Floyd-Warshall Algorithm
• Given: 2
3 4
– Directed, weighted graph G = (V, E)
8
– Negative-weight edges may be 1
2
3

present -4 7
1
-5
– No negative-weight cycles could be 5
6
4
present in the graph
• Compute:
– The shortest paths between all pairs
of vertices in a graph

10
The Structure of a Shortest Path
3
• Vertices in G are given by 6
0.5
1 2
V = {1, 2, …, n} 3 4
2 1
2
• Consider a path p = v1, v2, …, vl
5
– An intermediate vertex of p is any
vertex in the set {v2, v3, …, vl-1}

– E.g.: p = 1, 2, 4, 5: {2, 4}

p = 2, 4, 5: {4}

11
The Structure of a Shortest Path
• For any pair of vertices i, j  V, consider all
paths from i to j whose intermediate vertices
are all drawn from a subset {1, 2, …, k}
– Find p, a minimum-weight path from these paths
p1
pu j
i

pt

No vertex on these paths has index > k

12
Example
dij(k) = the weight of a shortest path from vertex i
to vertex j with all intermediary vertices drawn
from {1, 2, …, k}
• d13(0) = 6

• d13(1) = 6 6
3
0.5
1 2
• d 13
(2)
= 5 3 4
2 1
• d13(3) = 5

• d13(4) = 4.5
13
The Structure of a Shortest Path
• k is not an intermediate vertex of path p
k
– Shortest path from i to j with intermediate j
i
vertices from {1, 2, …, k} is a shortest path
from i to j with intermediate vertices from {1,
2, …, k - 1}

• k is an intermediate vertex of path p


– p1 is a shortest path from i to k p1 
k p2
j
– p2 is a shortest path from k to j i
– k is not intermediary vertex of p1, p2

– p1 and p2 are shortest paths from i to k with


vertices from {1, 2, …, k - 1}
14
A Recursive Solution (cont.)
dij(k) = the weight of a shortest path from vertex i to
vertex j with all intermediary vertices drawn from
{1, 2, …, k}

• k1
• Case 1: k is not an intermediate k
vertex of path p i j

• dij(k) = dij(k-1)

15
A Recursive Solution (cont.)
dij(k) = the weight of a shortest path from vertex i to
vertex j with all intermediary vertices drawn from
{1, 2, …, k}

• k1
• Case 2: k is an intermediate

k
vertex of path p i
j

• dij(k) = dik(k-1) + dkj(k-1)

16
Computing the Shortest Path Weights

• dij(k) = wij if k = 0
min {dij(k-1) , dik(k-1) + dkj(k-1) } if k  1
• The final solution: D(n) = (dij(n)):
dij(n) = (i, j)  i, j  V
j j

+ (k, j)

i i
(i, k)
D(k-1) D(k)

17
The Floyd-Warshall algorithm

Floyd-Warshall(W[1..n][1..n])
01 D W // D(0)
02 for k 1 to n do // compute D(k)
03 for i 1 to n do
04 for j 1 to n do
05 if D[i][k] + D[k][j]  D[i][j] then
06 D[i][j] D[i][k] + D[k][j]
07 return D

Running Time: O(n3)

18
Computing predecessor matrix
 How do we compute the predecessor matrix?
 Initialization:
(0) nil if i  j or wij  
p (i, j )  
i if i  j and wij  
– Updating:

Floyd-Warshall(W[1..n][1..n])
01 …
02 for k 1 to n do // compute D(k)
03 for i 1 to n do
04 for j 1 to n do
05 if D[i][k] + D[k][j]  D[i][j] then
06 D[i][j] D[i][k] + D[k][j]
07 P[i][j] P[k][j]
08 return D

19
Example dij(k) = min {dij(k-1) , dik(k-1) + dkj(k-1) }
D(0) = W D(1) 1 2 3 4 5
2 1 2 3 4 5

3 4
1 0 3 8  -4 1 0 3 8  -4
8 2  0  1 7 2  0  1 7
1 3
2
3  4 0   3  4 0  
1
-4 7 -5
4 2  -5 0  4 2 5 -5 0 -2
5 4
6 5    6 0 5    6 0
D (2)
D(3) D(4)
1 2 3 4 5
1 0 3 8 4 -4 0 3 8 4 -4 0 3 -1 4 -4
2  0  1 7  0  1 7 3 0 -4 1 -1
3  4 0 5 11  4 0 5 11 7 4 0 5 3
4 2 5 -5 0 -2 2 -1 -5 0 -2 2 -1 -5 0 -2
5    6 0    6 0 8 5 1 6 0 20
Transitive closure of the graph
• Input:
– Un-weighted graph G: W[i][j] = 1, if (i,j)E, W[i][j] = 0
otherwise.
• Output:
– T[i][j] = 1, if there is a path from i to j in G, T[i][j] = 0
otherwise.
• Algorithm:
– Just run Floyd-Warshall with weights 1, and make T[i]
[j] = 1, whenever D[i][j] < 
– More efficient: use only Boolean operators

21
Transitive closure algorithm

Transitive-Closure(W[1..n][1..n])
01 T W // T(0)
02 for k 1 to n do // compute T(k)
03 for i 1 to n do
04 for i 1 to n do
05 T[i][j] T[i][j] (T[i][k]  T[k][j])
06 return T

22
Readings
• Chapters 25

23

You might also like