Shortest Path
HKOI 2006
March 11, 2006
Graph
• Graph G = (V, E)• Vertex/Node V
– Number |V| or simply V– Degree deg[v],in-deg[v],out-deg[v]
• Edge E– Number |E| or simply E– Direction e = (u, v) or {u, v}
– Weight we, wuv
• E ≤ V2 i.e. deg[v] ≤ |V|
Graph Representations
Adjacency Matrix
Adjacency List Edge List
Memory (V2) (V + E) (E)
Check if (u, v) connected
(1) (out-degree[u]) (E)
List Edges from u (V) (out-degree[u]) (E)
List Edges to v (V) (E) (E)
List All Edges (V2) (E) (E)
Breadth First Search
• BFS needs O(V) queue and O(V) set for visiting state information and runs in O(V + E) time
• BFS can find Shortest Path if Graph is NOT weighted
Order of Vertices• BFS works because a Queue ensures a specific
Order
• Define d[v] be the shortest path from s to v
• At any time, vertices are classified into:– Black With known d[v]– Grey With some known path– White Not yet touched
• The Grey vertex with smallest d[v] has potential to become Black
S
T
3
1
31
32
3
S
T
S
T
S
T
S
T
S
T
S
T
S
T
S
T
S
T
S
T
S
T
S
T
Done?
Weighted Graph• Queue does not promise smallest d[v] anymore
• Expanding path caused unnecessary searching of artificial vertices
• We can simply pick the shortest real vertex
• We need “Sorted Queue” which “dequeues” vertices in increasing order of d[v].
• It is called a “Priority Queue” and negation of d[v] is called the Priority of vertex v
0 3
1
31
32
3
0
3
2
3
1
31
32
3
0
3
3
2
5
3
1
31
32
3
Done?
0
3
3
2
4
3
1
31
32
3
Done?
0
3
3
2
4
3
1
31
32
3
0
3
3
2
4
3
1
31
32
3
Done?
Dijkstra’s Algorithm
for-each v, d[v] ← ∞d[s] ← 0Q.Insert(s,d[s])while not Q.Empty() do
u = Q.ExtractMin()for-each v where (u, v) in E
if d[v] > d[u] + wuv thend[v] = d[u] + wuv
Q.Insert(v,d[v])
Lazy Deletion
Dijkstra’s Algorithm
for-each v, d[v] ← ∞d[s] ← 0Q.Insert(s,d[s])while not Q.Empty() do
u = Q.ExtractMin()for-each v where (u, v) in E
if d[v] > d[u] + wuv thend[v] = d[u] + wuv
Q.DecreaseKey(v,d[v])
Implementations of Priority Queue
Insert ExtractMin DecreaseKey
Array (1) (n) (1)
Sorted Array (n) (1) (n)
Binary Heap (log n) (log n) (log n)
Fibonacci Heap (amortized)
(1) (log n) (1)
ComplexityMemory Time
Array (V) (V2)
Array (Lazy) (E) (E2)
Sorted Array (V) (VE)
Binary Heap (V) (E log V)
Binary Heap (Lazy)
(E) (E log V)
Fibonacci Heap (amortized)
(V) (E + V log V)
Another idea
• Consider the following code segment:For each edge (u, v)
If d[v] > d[u] + wuv
d[v] ← d[u] + wuv
• Assume one of the shortest paths is(s, v1, v2, …, vk)
• If d[vi] = its shortest path from s• After this loop, d[vi+1] = its shortest path from s• By MI, After k such loops, found shortest path from s
to vk
Bellman-Ford Algorithm
• All v1, v2, …,vk distinctfor-each v, d[v] ← ∞d[s] ← 0
Do V-1 timesfor-each (u, v) in E
if d[v] > d[u] + wuv then
d[v] ← d[u] + wuv
• O(VE)• Support Negative-weight Edges
Negative Cycle
• A negative cycle is a cycle whose sum of edge weights is negative
• What happens of there are negative cycles in the graph?
• Doesn’t matter if the negative cycle is not reachable from the source
• If a negative cycle is reachable from the source, can we detect it?
• Answer: one more round of relaxations
All-pairs Shortest Path• Sometimes we want to find the distances between any
pair of vertices• Intermediate vertices may shorten the distance
between two vertices• Label the vertices as v1, v2, … , vn
• Let Vk-path be a path which uses only v1, v2, … , vk as intermediate vertices
• A s-t-V1-path must either be– a s-t-V0-path, or– concatenation of a s-v1-V0-path and v1-t-V0-path
• A s-t-V2-path must either be– a s-t-V1-path, or– concatenation of a s-v2-V1-path and v2-t-V1-path
• By MI …
Recurrence Relation
• A s-t-Vk-path must either be
– a s-t-Vk-1-path, or
– concatenation of a s-vk-Vk-1-path and vk-t-Vk-1-path
• dij(k):=length of the shortest vi-vj-Vk-path
• dij(k) = wij if k=0
min(dij(k-1), dik
(k-1) + dkj(k-1) ) if
k>=1
Warshall’s Algorithm
d ← ∞
for-each (u, v) in Ed[u][v] ← wuv
for-each k in Ve ← dfor-each i in V
for-each j in Vif e[i][j] > d[i][k] + d[k][j]
e[i][j] ← d[i][k] + d[k][j]d ← e
• Time Complexity: O(V3)
dij(k) = wij if k=0
min(dij(k-1), dik
(k-1) + dkj(k-1) ) if k>=1
Warshall-Floyd Algorithm
d ← ∞
for-each (u, v) in Ed[u][v] ← wuv
for-each k in Vfor-each i in V
for-each j in Vif d[i][j] > d[i][k] + d[k][j]
d[i][j] ← d[i][k] + d[k][j]
Time Complexity: O(V3)
Summary
• Dijkstra’s Algorithm
• Bellman-Ford Algorithm
• Warshall-Floyd Algorithm
Construction of Shortest Path
• If the whole shortest path from source vertex s to any vertex v is stored during computation, time and space complexities of the algorithms will increase
• For single-source shortest path algorithms, only the parent vertex p[v] of a vertex v is stored
• Shortest path may be constructed based on p[v]• A shortest path tree Gp is formed with root s
• How to contruct shortest paths in Floyd-Warshall algorithm?