Chapter9 Part1 Weighted Shortest Path
Chapter9 Part1 Weighted Shortest Path
Weighted Shortest
Paths
Weighted Shortest Path
Suppose you are travelling
from Pretoria to Durban
N
13
9
10
P L Z
2 3 3
11
J H D
15
N
13
9
10
P L Z
2 3 3
11
J H D
15
100 1 100 -
D D 100
(A, (A,
100) 100)
while !unknown.empty() {
curr = unknown vertex with min dist
Should we really curr.known = true;
remove it?
Visit all
for (each unknown neighbour w of curr) {
neighbours?
newDist = curr.dist + length(curr, w)
if (newDist < w.dist) {
w.dist = newDist;
w.path = curr; Add vertex back to the
} heap if dist changes?
}
}
Shortest Paths: Label Correcting Algorithm
labelCorrecting(Graph, start){
create vertex pool toBeChecked
for (each vertex v in Graph) { // Initialization
v.dist = INFINITY; // Unknown distance
v.path = null; // Previous node in shortest
path
}
start.dist = 0
Use a queue for the
toBeChecked.enqueue(start); “toBeChecked” pool!
while (!toBeChecked.empty()) {
curr = toBeChecked.dequeue();
for (each neighbour w of curr) { // even the visited ones
newDist = curr.dist + length(curr, w);
if (newDist < v.dist) {
w.dist = newDist;
w.path = curr;
if (!toBeChecked.contains(w))
Put w in the queue if
toBeChecked.enqueue(w);
} w gets updated!
}
}
Complexity?
}
O()
Label Correcting Algorithm with Queue
(a,2) 5
2
a b c (b,7) Works on negative weights,
2 2 -3 9 but not if cycles are present!
4 5 (e,4)
(e,3)
d e f
(a,2) (a,2)
(g,-2)
(b,-1) Iter: Ini 1 2 3 4 5 6 7 8 9
1
-5 curr: t
g Queue a ba b d e c g f ef f
(d,3) :
d d e c g ef e
create vertex set toBeChecked
for (each vertex v in Graph) { e e
c cg gf f
v.dist = INFINITY;
v.path = null;
}
start.dist = 0;
toBeChecked.enqueue(start);
a 0
while (!toBeChecked.empty()) { 2
curr = toBeChecked.dequeue() b
for (each neighbour w of curr) { 7
newDist = curr.dist + length(curr,w);
if (newDist < w.dist) { c
w.dist = newDist; 2
w.path = curr; d
if (!toBeChecked.contains(w)) 2 -1 -2
}
toBeChecked.enqueue(w);
e
} 4 3
} f
3
Shortest Paths: All-Pairs
All the algorithms discussed so far discover the shortest paths from
vertex i to any other vertex in the graph
Can we find all the shortest paths from any vertex to any other
vertex?
We can calculate the adjacency matrix of shortest paths using the
Floyd–Warshall algorithm (named after R. Floyd and S. Warshall)
let dist be a |V| × |V| array of minimum distances
initialized to infinity
for each vertex v
dist[v][v] = 0
for each edge(u,v)
dist[u][v] = weight(u,v) // the weight of the edge(u,v)
FWalgorithm(Matrix dist)
for i = 1 to |V|
for j = 1 to |V|
for k = 1 to |V|
if dist[j][k] > dist[j][i] + dist[i][k]
dist[j][k] = dist[j][i] + dist[i][k]
A B C D
6
A 0 0 3 1
2 3 distance
A B C B 0 3 adjacency
matrix
1 -1 C 0
D D -1 2 0
All-Pairs: Adjacency Matrix
let dist be a |V| × |V| array of minimum distances initialized
to infinity
for each vertex v
dist[v][v] = 0
for each edge(u,v)
dist[u][v] = weight(u,v) // the weight of the edge (u,v)
6 A B C D A B C D
2 3 A 0 A 0 2 6 1
A B C
B 0 B 0 3
1 -1 C 0 C 0
D
D 0 D -1 0
Initialise intersections
to
existing edge weights
6
All-Pairs: Distance Matrix 2 3
A B C
for i = 1 to |V|
for j = 1 to |V|
for k = 1 to |V| 1 -1
if dist[j][k] > dist[j][i] + dist[i][k]
dist[j][k] = dist[j][i] + dist[i][k] D
A B C D A B C D
A 0 2 6 1 A 0 2 5 1
i = 1: A B 0 3 i = 2: B B 0 3
C 0 C 0
D -1 0 D -1 2 0
A B C D A B C D
A 0 2 5 1 A 0 0 3 1
B 0 3 i = 4: B 0 3
i = 3: C
D
C 0 C 0
D -1 2 0 D -1 2 0
All-Pairs: Modified FW Algorithm
public static void allPairs(int [ ][ ] a, int [ ][ ] dist, int [ ]
[ ] path)
{
int n = a.length;
// Initialize distance and path matrices
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
{
dist[i][j] = a[i][j];
path[i][j] = NOT_A_VERTEX;
}
A B C D A B C D
A -1 -1 2 -1 A 0 2 5 1
B=2 B -1 -1 -1 -1 i = 2: B B 0 3
C -1 -1 -1 -1 C 0
D -1 -1 2 -1 D -1 2 0
A B C D A B C D
A -1 4 4 -1 A 0 0 3 1
B -1 -1 -1 -1 i = 4: B 0 3
D=4
D
C -1 -1 -1 -1 C 0
D -1 -1 2 -1 D -1 2 0