Upload
tashya-stokes
View
62
Download
4
Embed Size (px)
DESCRIPTION
All-Pairs Shortest Paths. Find the distance between every pair of vertices in a weighted graph G. From A one may reach C faster by reaching by traveling from A to B and then from B to C. All-Pairs Shortest Paths. We can make n calls to Dijkstra’s algorithm O(n(n+m)log n) time. - PowerPoint PPT Presentation
Citation preview
All-Pairs Shortest Paths• Find the distance between every pair of vertices in
a weighted graph G.
All-Pairs Shortest Paths
• We can make n calls to Dijkstra’s algorithm – O(n(n+m)log n) time
From A one may reach C faster by reaching by traveling from A to B and then from B to C.
Alternatively ... Use dynamic programming: The Floyd’s Algorithm
O(n3) time.
Floyd’s Shortest Path Algorithm
• Idea #1: Number the vertices 1, 2, …, n.• Idea #2: Shortest path between vertex i
and vertex j without passing through any other vertex is weight(edge(i,j)).– Let it be D0(i,j).
• Idea #3: If Dk(i,j) is the shortest path between vertex i and vertex j using vertices numbered 1, 2, …, k, as intermediate vertices, then Dn(i,j) is the solution to the shortest path problem between vertex i and vertex j.
k
j
i
Uses only verticesnumbered 1,…,k-1 Uses only vertices
numbered 1,…,k-1
Uses only vertices numbered 1,…,k(compute weight of this edge)
Assume that we have Dk-1(i,j) for all i, and j.We wish to compute Dk(i,j).What are the possibilities? Choose between(1) a path through vertex k.
Path Length: Dk(i,j) = Dk-1(i,k) + Dk-1(k,j). (2) Skip vertex k altogether.
Path Length: Dk(i,j) = Dk-1(i,j).
Floyd’s Shortest Path Algorithm
k
j
i
Uses only verticesnumbered 1,…,k-1 Uses only vertices
numbered 1,…,k-1
Uses only vertices numbered 1,…,k(compute weight of this edge)
otherwise
edge an is ),( if),(
if00, jijiji vvvvedgeweight
ji
D
1,
1,
1,, ,min k
jkkki
kji
kji DDDD
Floyd’s Shortest Path Algorithm
Example:
4
1
3
5
2
1
1 1
8
2
43
8
0 8 3 1
8 0 4 2
3 4 0 1 1
1 1 0 8
2 1 8 0
D0
Floyd’s Shortest Path Algorithm
Example:
4
1
3
5
2
1
1 1
8
2
43
8
0 8 3 1
8 0 4 9 2
3 4 0 1 1
1 9 1 0 8
2 1 8 0
D1
Floyd’s Shortest Path Algorithm
Example:
4
1
3
5
2
1
1 1
8
2
43
8
0 8 3 1 10
8 0 4 9 2
3 4 0 1 1
1 9 1 0 8
10 2 1 8 0
D2
Floyd’s Shortest Path Algorithm
Example:
4
1
3
5
2
1
1 1
8
2
43
8
0 7 3 1 4
7 0 4 5 2
3 4 0 1 1
1 5 1 0 2
4 2 1 2 0
D3
Floyd’s Shortest Path Algorithm
Example:
4
1
3
5
2
1
1 1
8
2
43
8
0 6 2 1 3
6 0 4 5 2
2 4 0 1 1
1 5 1 0 2
3 2 1 2 0
D4
Floyd’s Shortest Path Algorithm
Example:
4
1
3
5
2
1
1 1
8
2
43
8
0 5 2 1 3
5 0 3 4 2
2 3 0 1 1
1 4 1 0 2
3 2 1 2 0
D5
Floyd’s Shortest Path Algorithm
Algorithm AllPair(G) {assumes vertices 1,…,n} for all vertex pairs (i,j)
if i jd0[i,i] 0
else if (i,j) is an edge in Gd0[i,j] weight of edge (i,j)
else d0[i,j] + for k 1 to n do
for i 1 to n do for j 1 to n do
dk[i,j] min(dk-1[i,j], dk-1[i,k]+dk-1[k,j])
Floyd’s Shortest Path Algorithm
O(n+m)or
O(n2)
O(n3)
Floyd’s Shortest Path Algorithm
Observation: dk [i,k] = dk-1[i,k] dk[k,j] = dk-1[k,j]
Algorithm AllPair(G) {assumes vertices 1,…,n} for all vertex pairs (i,j)
if i jd[i,i] 0
else if (i,j) is an edge in Gd[i,j] weight of edge (i,j)
else d[i,j] + for k 1 to n do
for i 1 to n do for j 1 to n do
d[i,j] min(d[i,j], d[i,k]+d[k,j])
Floyd’s Shortest Path Algorithm
Algorithm AllPair(G) {assumes vertices 1,…,n} for all vertex pairs (i,j) path[i,j] null
if i jd[i,i] 0
else if (i,j) is an edge in Gd[i,j] weight of edge (i,j)
else d[i,j] + for k 1 to n do
for i 1 to n do for j 1 to n do
if (d[i,k]+d[k,j] < d[i,j]) path[i,j] vertex k d[i,j] d[i,k]+d[k,j]
Floyd’s Shortest Path Algorithm
• Can be applied to Directed Graphs
• Can accept –ve edges as long as there are no –ve cycles.
Floyd’s Shortest Path Algorithm
Transitive Closure• Transitive Relation:
A relation R on a set A is called transitive if and only if for any a, b, and c in A, whenever <a, b> R , and <b, c> R , <a, c> R
Transitive Closure• Given a graph G, the
transitive closure of G is the G* such that– G* has the same
vertices as G– if G has a path from u
to v (u v), G* has a edge from u to v
B
A
D
C
E
B
A
D
C
E
G
G*The transitive closure providesreachability information about a graph.
Computing the Transitive Closure
• We can perform BFS starting at each vertex– O(n(n+m))
If there's a way to get from A to B and from B to C, then there's a way to get from A to C.
Alternatively ... Use dynamic programming: Warshall’s Algorithm
Floyd-Warshall Transitive Closure
• Idea #1: Number the vertices 1, 2, …, n.• Idea #2: Consider paths that use only
vertices numbered 1, 2, …, k, as intermediate vertices:
k
j
i
Uses only verticesnumbered 1,…,k-1 Uses only vertices
numbered 1,…,k-1
Uses only vertices numbered 1,…,k(add this edge if it’s not already in)
Example:
4
1
3
20 1 0 1
1 0 1 0
0 1 0 1
1 0 1 0
A0
Warshall’s Transitive Closure
Example:
4
1
3
20 1 0 1
1 0 1 1
0 1 0 1
1 1 1 0
A1
Warshall’s Transitive Closure
Example:
4
1
3
20 1 1 1
1 0 1 1
1 1 0 1
1 1 1 0
A2
Warshall’s Transitive Closure
Example:
4
1
3
20 1 1 1
1 0 1 1
1 1 0 1
1 1 1 0
A3
Warshall’s Transitive Closure
Example:
4
1
3
20 1 1 1
1 0 1 1
1 1 0 1
1 1 1 0
A4
Warshall’s Transitive Closure
Warshall’s Algorithm• Warshall’s algorithm numbers
the vertices of G as v1 , …, vn and computes a series of graphs G0, …, Gn
– G0=G
– Gk has a edge (vi, vj) if G has a path from vi to vj with intermediate vertices in the set {v1 , …, vk}
• We have that Gn = G*
• In phase k, graph Gk is computed from Gk 1
• Running time: O(n3), assuming areAdjacent is O(1) (e.g., adjacency matrix)
Algorithm FloydWarshall(G)Input graph GOutput transitive closure G* of Gi 1for all v G.vertices()
denote v as vi
i i + 1G0 Gfor k 1 to n do
Gk Gk 1
for i 1 to n (i k) dofor j 1 to n (j i, k) do
if Gk 1.areAdjacent(vi, vk) Gk 1.areAdjacent(vk, vj)
if Gk.areAdjacent(vi, vj) Gk.insertEdge(vi, vj , k)
return Gn
Shortest Path Algorithm for DAGs
• Use Dijkstra’s algorithm– Time: O((n+m) log n)
• Use Topological sort based algorithm– Time: O(n+m).
Directed Graph With –ve weight
ORDPVD
MCODFW
SFO
LAX
LGA
HNL
$170
-$100
$277-$200
-$100
$210
$224
$246
$68
$511
$28
$2
61
0
Directed Graph With –ve weight
ORD4
PVD3
MCO1
DFW5
SFO8
LAX6
LGA2
HNL7
$170
-$100
$277-$200
-$100
$210
$224
$246
$68
$511
$28
$2
61
0
Directed Graph With –ve weight
ORD4
PVD3
MCO1
DFW5
SFO8
LAX6
LGA2
HNL7
$170
-$100
$277-$200
-$100
$210
$224
$246
$68
$511
$28
$2
61
0
$210
$224
$224
Directed Graph With –ve weight
ORD4
PVD3
MCO1
DFW5
SFO8
LAX6
LGA2
HNL7
$170
-$100
$277-$200
-$100
$210
$224
$246
$68
$511
$28
$2
61
0
$210
$224
$261
Directed Graph With –ve weight
ORD4
PVD3
MCO1
DFW5
SFO8
LAX6
LGA2
HNL7
$170
-$100
$277-$200
-$100
$210
$224
$246
$68
$511
$28
$2
61
0
$210
$224
$238
Directed Graph With –ve weight
ORD4
PVD3
MCO1
DFW5
SFO8
LAX6
LGA2
HNL7
$170
-$100
$277-$200
-$100
$210
$224
$246
$68
$511
$28
$2
61
0
$210
$224
$238$408
Directed Graph With –ve weight
ORD4
PVD3
MCO1
DFW5
SFO8
LAX6
LGA2
HNL7
$170
-$100
$277-$200
-$100
$210
$224
$246
$68
$511
$28
$2
61
0
$210
$224
$238$408
$208
$308
Directed Graph With –ve weight
ORD4
PVD3
MCO1
DFW5
SFO8
LAX6
LGA2
HNL7
$170
-$100
$277-$200
-$100
$210
$224
$246
$68
$511
$28
$2
61
0
$210
$224
$238$408
$208
$308
Directed Graph With –ve weight
ORD4
PVD3
MCO1
DFW5
SFO8
LAX6
LGA2
HNL7
$170
-$100
$277-$200
-$100
$210
$224
$246
$68
$511
$28
$2
61
0
$210
$224
$238$408
$208
$268
$719
Directed Graph With –ve weight
ORD4
PVD3
MCO1
DFW5
SFO8
LAX6
LGA2
HNL7
$170
-$100
$277-$200
-$100
$210
$224
$246
$68
$511
$28
$2
61
0
$210
$224
$238$408
$208
$268
$719
Directed Graph With –ve weight
ORD4
PVD3
MCO1
DFW5
SFO8
LAX6
LGA2
HNL7
$170
-$100
$277-$200
-$100
$210
$224
$246
$68
$511
$28
$2
61
0
$210
$224
$238$408
$208
$268
$719
DAG-based Algorithm
• Works even with negative-weight edges
• Uses topological order• Doesn’t use any fancy
data structures• Is much faster than
Dijkstra’s algorithm• Running time: O(n+m).
Algorithm DagDistances(G, s)for all v G.vertices()
v.parent nullif v s
v.distance 0else
v.distance Perform a topological sort of the verticesfor u 1 to n do {in topological order}
for each e G.outEdges(u)w G.opposite(u,e)r getDistance(u) weight(e)if r getDistance(w)
w.distance r w.parent u