420 likes | 567 Views
This document explores various algorithms for finding the shortest paths in graphs. It covers Dijkstra’s Algorithm, suitable for non-negative weights, and the Bellman-Ford Algorithm, capable of handling negative weights. The analysis extends to the Warshall-Floyd Algorithm, which computes shortest paths for all pairs of vertices. Key concepts include graph representations, priority queues, relaxation techniques, and cycle detection. The document provides time and space complexity analyses, guiding readers on optimal algorithm selection based on graph characteristics.
E N D
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|
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 NOTweighted
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 3 2 1 3 T 3 3 1
S T
S T
S T
S T
S T
S T
S T
S T
S T
S T
S T
Done? S T
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 2 1 3 3 3 1
0 3 3 2 1 3 3 2 3 1
Done? 0 3 3 2 1 3 5 3 2 3 1 3
Done? 0 3 3 2 1 3 4 3 2 3 1 3
0 3 3 2 1 3 4 3 2 3 1 3
Done? 0 3 3 2 1 3 4 3 2 3 1 3
Dijkstra’s Algorithm Lazy Deletion for-each v, d[v] ← ∞ d[s] ← 0 Q.Insert(s,d[s]) whilenot Q.Empty() do u = Q.ExtractMin() for-eachvwhere (u, v) in E if d[v] > d[u] + wuvthen d[v] = d[u] + wuv Q.Insert(v,d[v])
Dijkstra’s Algorithm for-each v, d[v] ← ∞ d[s] ← 0 Q.Insert(s,d[s]) whilenot Q.Empty() do u = Q.ExtractMin() for-eachvwhere (u, v) in E if d[v] > d[u] + wuvthen d[v] = d[u] + wuv Q.DecreaseKey(v,d[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 distinct for-each v, d[v] ← ∞ d[s] ← 0 Do V-1 times for-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 dij(k) = wij if k=0 min(dij(k-1), dik(k-1) + dkj(k-1) ) if k>=1 d ← ∞ for-each (u, v) in E d[u][v] ← wuv for-eachk in V e ← d for-eachiin V for-eachjin V if e[i][j] > d[i][k] + d[k][j] e[i][j] ← d[i][k] + d[k][j] d ← e • Time Complexity: O(V3)
Warshall-Floyd Algorithm d ← ∞ for-each (u, v) in E d[u][v] ← wuv for-eachk in V for-eachiin V for-eachjin V if 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?