05 CME4422 ShortestPathAlgorithms
05 CME4422 ShortestPathAlgorithms
05 CME4422 ShortestPathAlgorithms
Weight of path p = v1 → v2 → … → vk is
k −1
w( p ) = w(vi , vi +1 ) = sum of edge weights on path p
i =1
Shortest Path
◘ Logistic network
where the weight w(i,j) of an edge ij is the cost to go from vertex i to vertex j. If you made
a business agreement with other companies to transport their products, w(i,j) would be a
profit instead of a cost, so you can interpret this weight as a negative cost.
◘ Traffic congestion
where the weights represent traffic conditions in a map (more negative, more
unfavorable) - we could then use this representation to compute optimal distances.
◘ Chemistry
where the weights can be used to represent the heat produced during a chemical
reaction. Nodes: compounds, Edge euv: if compound v can be obtained from u. (You
produce 4 kJ to convert s to a and 2 kJ to convert a to t. You need 5 kJ to get back s
from t.)
◘ Currency exchange market
where nodes represent nations and whose edges represent the percentage gain or loss
of transferring one currency for another. National currencies are sometimes overvalued
or undervalued. The cheapest path from one node to another represents the cheapest
way to transform one currency into another.
Cycles
u v
RELAX(u, v) 5
2
9
if v.d > u.d + w(u,v)
Relax(u,v)
v.d = u.d + w(u,v)
5 7
v.π = u 2
u v
2
5 6
No Relaxation
5 6
2
Properties of Relaxation
Triangle Inequality:
For a given vertex s V and for every edge (u,v) E,
δ(s,v) ≤ δ(s,u) + w(u,v)
Upper-bound property:
We always have v.d ≥ δ(s,v) for all vertices v V .
Once v.d achieves the δ(s,v) value, it never changes.
No-path property:
If there is no path from s to v, then always have v.d =δ(s,v)=∞
Properties of Relaxation
Covergence property:
If s u → v be a shortest path from s to v for some u,v V
and
if u.d=δ(s, u) at ay time proior to Relax(u,v) then v.d=δ(s, v)
all times after Relax(u,v)
Predecessor-subgraph property:
Once v.d=δ(s, v) for all v V, the predecessor-subgraph is a
shortest-paths tree rooted at s.
Single-Source Shortest Path Algorithms
◘ Dijkstra’s Algorithm:
– greedy approach
– similar to BFS
– works for no negative-weight edges
◘ Bellman-Ford Algorithm:
– edge weights can be negative
Dijkstra’s Algorithm
10
9
2 3
s 0 4 6
5 7
2
x y
s x u v y
𝑑𝑖𝑠𝑡(𝑢) 0 ∞ ∞ ∞ ∞ 𝑑𝑖𝑠𝑡(𝑢)
Q 𝜋(𝑢) NULL NULL NULL NULL NULL S 𝜋(𝑢)
Example
u v
1
10
10
9
2 3
s 0 4 6
5 7
5
2
x y
x u v y s
𝑑𝑖𝑠𝑡(𝑢) 5 10 ∞ ∞ 𝑑𝑖𝑠𝑡(𝑢) 0
Q 𝜋(𝑢) s s NULL NULL
S 𝜋(𝑢) NULL
Example
u v
1
8 14
10
9
2 3
s 0 4 6
5 7
5 7
2
x y
y u v s x
𝑑𝑖𝑠𝑡(𝑢) 𝟕 8 14 𝑑𝑖𝑠𝑡(𝑢) 0 5
Q 𝜋(𝑢) x x x
S 𝜋(𝑢) NULL s
Example
u v
1
8 13
10
9
2 3
s 0 4 6
5 7
5 7
2
x y
u v s x y
𝑑𝑖𝑠𝑡(𝑢) 𝟖 13 𝑑𝑖𝑠𝑡(𝑢) 0 5 7
Q 𝜋(𝑢) x y
S 𝜋(𝑢) NULL s x
Example
u v
1
8 9
10
9
2 3
s 0 4 6
5 7
5 7
2
x y
v s x y u
𝑑𝑖𝑠𝑡(𝑢) 𝟗 𝑑𝑖𝑠𝑡(𝑢) 0 5 7 8
Q 𝜋(𝑢) u
S 𝜋(𝑢) NULL s x x
Example
u v
1
8 9
10
9
2 3
s 0 4 6
5 7
5 7
2
x y
s x y u v
𝑑𝑖𝑠𝑡(𝑢) 𝑑𝑖𝑠𝑡(𝑢) 0 5 7 8 9
Q 𝜋(𝑢) S 𝜋(𝑢) NULL s x x u
Backtracking for the shortest path
u v
1
8 9 Backtracking from v
v→u→x→s
10
9 Shortest path from s to v
2 3
s 0 4 6 p=s→x→u→v
5 7 w(p) = 9
5 7
2
x y
s x y u v
shortest path lengths
𝑑𝑖𝑠𝑡(𝑢) 0 5 7 8 9
S 𝜋(𝑢) NULL s x x u
from s to other vertices
Example 2
∞ 30 ∞ 70 69
77
0 ∞ 36 78
∞
∞ 50 49 48 ∞ 59 58
Analysis
◘ Running time depends on implementation of priority queue.
◘ INIT (G,s): Θ(V) time
◘ While loop:
– EXTRACT-MIN(Q) executed |V| times
– RELAX (u,v) executed |E| times
◘ Total Time :
– O(V2) using linear array for priority queue.
– O((V + E) lgV) = O(E lgV) using binary heap.
Negative weighted graph
6 -3
8 7
s 2
-4
7
9
y z
Bellman-Ford Algorithm
5
t x
-2 i s t y x z
-3 0 0
6
8 7
s 0 2
-4
7
9
y z
E (s,y) (s,t) (y,z) (y,x) (t,y) (t,z) (t,x) (x,t) (z,x) (z,s)
w 7 6 9 -3 8 -4 5 -2 7 2
Example
5
t x
6
-2 i s t y x z
-3 0 0
6
1 0 6s 7 s
8 7
s 0 2
-4
7
7
9
y z
E (s,y) (s,t) (y,z) (y,x) (t,y) (t,z) (t,x) (x,t) (z,x) (z,s)
w 7 6 9 -3 8 -4 5 -2 7 2
Example
5
t x
6
-2 4 i s t y x z
-3 0 0
6
1 0 6s 7 s
8 7
s 0 2 2 0 6s 7 s 4y 2 t
-4
7
7 2
9
y z
E (s,y) (s,t) (y,z) (y,x) (t,y) (t,z) (t,x) (x,t) (z,x) (z,s)
w 7 6 9 -3 8 -4 5 -2 7 2
Example
5
t x
2
-2 4 i s t y x z
-3 0 0
6
1 0 6s 7 s
8 7
s 0 2 2 0 6s 7 s 4y 2 t
-4 3 0 2x 7 s 4y 2 t
7
7 2
9
y z
E (s,y) (s,t) (y,z) (y,x) (t,y) (t,z) (t,x) (x,t) (z,x) (z,s)
w 7 6 9 -3 8 -4 5 -2 7 2
Example
5
t x
2
-2 4 i s t y x z
-3 0 0
6
1 0 6s 7 s
8 7
s 0 2 2 0 6s 7 s 4y 2 t
-4 3 0 2x 7 s 4y 2 t
7 4 0 2 x 7 s 4 y -2 t
7 -2
9 shortest path lengths
y z from s to other vertices
E (s,y) (s,t) (y,z) (y,x) (t,y) (t,z) (t,x) (x,t) (z,x) (z,s)
w 7 6 9 -3 8 -4 5 -2 7 2
Backtracking for the shortest path
5
t x
2
-2 4 i s t y x z
-3 4 0 2 x 7 s 4 y -2 t
6
8 7
s 0 2
Backtracking from z
-4 z→t→x→y→s
7
7 -2 Shortest path from s to z
9
y z p=s→y→x→t→z
w(p) = -2
Analysis
O(V)
O(V.E)
O(E)
The corresponding
A 8x8 original maze The simplified graph
graph representation
DC Liaw, CC Kuo, HT Lee, 'A Study of Point-to-Point Routing Problem in Mazes', International Journal of Engineering and Applied Sciences (IJEAS)
ISSN: 2394-3661, Volume-6, Issue-7, July 2019.
All-Pairs Shortest-Paths Algorithms
procedure FloydWarshall(G) {
for i := 1 to n do
for j := 1 to n do
D[i, j] := C[i, j];
S[i ,j] := 0
for i := 1 to n do
D[i; i] := 0;
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] then
S[i, j] := k;
D[i, j] := D[i, k] + D[k, j];
}
Example
Initial Step:
Example
Step-1:
Example
Step-2:
Example
Step-3:
Example
Step-4:
Example
◘ From matrices D(4) and S(4), we can determine the shortest route between every
pair of nodes.
(4)
◘ The shortest distance from node 1 to node 4 is 𝑑14 ═ 1 unit.
(4)
𝑠14 ═ 0 indicates that there is no intermediate node between 1 and 4.
(4)
◘ The shortest distance from node 2 to node 4 is 𝑑24 ═ 6 unit.
(4) (4) (4)
𝑠24 ═ 3 yields the route 2–3–4. 𝑠23 ═ 0, 𝑠34 ═ 1 yields the route 3–1–4,
(4) (4)
𝑠31 ═ 0, 𝑠14 ═ 0. The combined result gives the shortest path 2–3–1–4.
Transitive Closure
◘ In some problems we may just want to know whether there exists a path
from vertex i to vertex j of length one or more in a graph G = (V, E). We
call this the problem of computing the transitive closure of G.
◘ Given a directed graph G = (V, E) with adjacency matrix A, we compute
boolean matrix T (the transitive closure of the adjacency matrix A) such
that T[i, j] is 1 if there is a path from i to j of length 1 or more, and 0
otherwise.
◘ The transitive-closure algorithm is similar to the Floyd-Warshall algorithm
except that it uses the boolean operation and to conclude that if there is
a path from i to k and one from k to j, then there is a path from i to j.
Questions?