CS : Algorithms Chapter 4 Shortest Paths Credit: Dr. George Bebis
Shortest Path Problems How can we find the shortest route between two points on a road map? Model the problem as a graph problem: Road map is a weighted graph: vertices = cities edges = road segments between cities edge weights = road distances Goal: find a shortest path between two vertices (cities)
Shortest Path Problem Input: Directed graph G = (V, E) Weight function w : E R s 0 Weight of path p = v 0, v,..., v k w( p) k i w( v i, v i ) Shortest-path weight from u to v: p 9 δ(u, v) = min w(p) : u v if there exists a path from u to v t y 6 4 7 6 x z otherwise Note: there might be multiple shortest paths from u to v
Negative-Weight Edges Negative-weight edges may form negative-weight cycles If such cycles are reachable from the source, then δ(s, v) is not properly defined! Keep going around the cycle, and get w(s, v) = - for all v on the cycle s 0 a -4 c d g y 6 b 8 4-7 e -6 f Therefore, negative-weight edges will not be considred here 4
Cycles Can shortest paths contain cycles? No! Negative-weight cycles Shortest path is not well defined (we will not consider this case) If there is a positive-weight cycle, we can get a shorter path by removing the cycle. Zero-weight cycles? No reason to use them Can remove them and obtain a path with the same weight
Shortest-Paths Notation For each vertex v V: δ(s, v): shortest-path weight d[v]: shortest-path weight estimate Initially, d[v]= d[v] δ(s,v) as algorithm progresses [v] = predecessor of v on a shortest path from s If no predecessor, [v] = NIL induces a tree shortest-path tree s 0 t 6 9 y 4 6 x z 7
Initialization Alg.: INITIALIZE-SINGLE-SOURCE(V, s). for each v V. do d[v]. [v] NIL 4. d[s] 0 All the shortest-paths algorithms start with INITIALIZE-SINGLE-SOURCE 7
Relaxation Step Relaxing an edge (u, v) = testing whether we can improve the shortest path to v found so far by going through u If d[v] > d[u] + w(u, v) we can improve the shortest path to v d[v]=d[u]+w(u,v) [v] u s u v 9 s u 6 After relaxation: d[v] d[u] + w(u, v) v RELAX(u, v, w) RELAX(u, v, w) u v 7 u v 6 no change
Dijkstra s Algorithm Single-source shortest path problem: No negative-weight edges: w(u, v) > 0, (u, v) E Each edge is relaxed only once! Maintains two sets of vertices: d[v]=δ (s, v) d[v]>δ (s, v) 9
Dijkstra s Algorithm (cont.) Vertices in (V S) reside in a min-priority queue Keys in Q are estimates of shortest-path weights d[u] Repeatedly select a vertex u (V S), with the minimum shortestpath estimate d[u] Relax all edges leaving u STEPS ) Extract a vertex u from Q (i.e., u has the highest priority) ) Insert u to S ) Relax all edges leaving u 4) Update Q
Dijkstra (G, w, s) s 0 S=<> Q=<s,t,x,z,y> S=<s> Q=<y,t,x,z> 0 t y 9 7 x 4 6 z s 0 0 t 0 y 9 7 x 4 6 z
Example (cont.) 0 t 0 8 4 9 x 0 t 8 4 9 x s 0 4 6 s 0 4 6 7 y 7 z 7 y 7 z S=<s,y> Q=<z,t,x> S=<s,y,z> Q=<t,x>
Example (cont.) s 0 0 t 8 9 4 6 7 7 y S=<s,y,z,t> Q=<x> 9 x z s 0 0 t 8 9 4 6 7 7 y S=<s,y,z,t,x> Q=<> 9 x z
Dijkstra (G, w, s). INITIALIZE-SINGLE-SOURCE(V, s) (V). S s. Q V[G] 4. while Q O(V) build min-heap Executed O(V) times. do u EXTRACT-MIN(Q) O(lgV) O(VlgV) 6. S S {u} 7. for each vertex v Adj[u] O(E) times (max) 8. do RELAX(u, v, w) O(ElgV) 9. Update Q (DECREASE_KEY) O(lgV) Running time: O(VlgV + ElgV) = O(ElgV)
Dijkstra s SSSP Algorithm (adjacency matrix) a f L [.] = 0 S b c 4 d new L[i] = Min{ L[i], L[k] + W[k, i] } e a 0 b 0 c 0 d 0 4 e 4 0 f 0 where k is the newly-selected intermediate node and W[.] is the distance between k and i
SSSP cont. b a d c new L[i] = Min{ L[i], L[k] + W[k, i] } where k is the newly-selected intermediate node and W[.] is the distance between k and i 4 f e L [.] = 0 new L [.] = 0 a 0 b 0 c 0 d 0 4 e 4 0 f 0
b a d c new L[i] = Min{ L[i], L[k] + W[k, i] } where k is the newly-selected intermediate node and W[.] is the distance between k and i 4 f e L [.] = 0 new L [.] = 0 a 0 b 0 c 0 d 0 4 e 4 0 f 0
a f L [.] = 0 new L [.] = 0 4 b c 4 e a 0 b 0 d c 0 d 0 4 e 4 0 f 0
a f L [.] = 0 4 new L [.] = 0 4 b c 4 e a 0 b 0 d c 0 d 0 4 e 4 0 f 0
a f L [.] = 0 4 new L [.] = 0 4 b c 4 e a 0 b 0 d c 0 d 0 4 e 4 0 f 0 Running time: O(V ) (adjacency matrix) Which one is better? O(ElgV) (adjacency list)
All-Pairs Shortest Paths Given: Directed graph G = (V, E) Weight function w : E R Compute: The shortest paths between all pairs of vertices in a graph Result: an n n matrix of shortest-path distances δ(u, v) 4 8 7 4 6 9 We can run Dijkstra s algorithm once from each vertex: O(VElgV) with binary heap and adjacency-list representation if the graph is dense O(V lgv) We can achieve O(V ) by using an adjacency-matrix.
Problem We are given a directed graph G=(V, E) on which each edge (u,v) has an associated value r(u,v), which is a real number in the range 0 r(u,v) that represents the reliability of a communication channel from vertex u to vertex v. We interpret r(u,v) as the probability that the channel from u to v will not fail, and we assume that these probabilities are independent. Give an efficient algorithm to find the most reliable path between two given vertices.
Problem (cont.) r(u,v) = Pr(channel from u to v will not fail) Assuming that the probabilities are independent, the reliability of a path p=<v,v,,v k > is: r(v,v ) r(v,v ) r(v k-,v k ) Solution : modify Dijkstra s algorithm Perform relaxation as follows: if d[v] < d[u] w(u,v) then d[v] = d[u] w(u,v) Use EXTRACT_MAX instead of EXTRACT_MIN
Problem (cont.) Solution : use Dijkstra s algorithm without any modifications! We want to find the channel with the highest reliability, i.e., max r( u, v) p ( u, v) p But Dijkstra s algorithm computes Take the log p ( u, v) p min w( u, v) lg(max r( u, v)) max lg( r( u, v)) p ( u, v) p p ( u, v) p
Problem (cont.) Turn this into a minimization problem by taking the negative: p p ( u, v) p ( u, v) p min lg( r( u, v)) min lg( r( u, v)) Run Dijkstra s algorithm using w( u, v) lg( r( u, v))