Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 19: Shortest Paths Shang-Hua Teng. Weighted Directed Graphs Weight on edges for distance 400 2500 1000 1800 800 900.

Similar presentations


Presentation on theme: "Lecture 19: Shortest Paths Shang-Hua Teng. Weighted Directed Graphs Weight on edges for distance 400 2500 1000 1800 800 900."— Presentation transcript:

1 Lecture 19: Shortest Paths Shang-Hua Teng

2 Weighted Directed Graphs Weight on edges for distance 400 2500 1000 1800 800 900

3 Shortest Paths Given a weighted, directed graph G=(V, E) with weight function w: E  R. The weight of path p= is the sum of the weights of its edges: We define the shortest-path weight from u to v by A shortest path from vertex u to vertex v is any path with w(p)=  (u, v) If there is a path from u to v, Otherwise.

4 Variants of Shortest Path Problem Single-source shortest paths problem –Finds all the shortest path of vertices reachable from a single source vertex s Single-destination shortest-path problem –By reversing the direction of each edge in the graph, we can reduce this problem to a single-source problem Single-pair shortest-path problem –No algorithm for this problem are known that run asymptotically faster than the best single-source algorithm in the worst case All-pairs shortest-path problem

5 Applications of Shortest Path Every where!!! –Driving (GPS) –Internet: network routing –Flying: Airline route –Resource allocation –VLSI design: wire routing –Computer Games –Mapquest, Yahoo map program

6 Optimal Substructure of Shortest-Paths Lemma: (Subpath of shortest paths are shortest paths). Let p= be a shortest path from vertex v 1 to v k, and for any i and j such that 1  i  j  k, let p ij = be the sub-path of p from vertex v i to v j. Then p ij is a shortest path from vertex v i to v j.

7 Algorithmic Impact Greedy Dynamic Programming

8 Negative-Weight Edges and Cycles In general, edges might have negative weights What if there is a negative-weight cycle No shortest path can contain a negative cycle Of course, a shortest path cannot contain a positive-weight cycle

9 Graph with Unit Weights Special case: All weights are 1 The single source shortest path problem can be solved by BFS BFS tree explicit gives a shortest path from a source s to any vertex reachable from s

10 Breadth-First Search BFS(G, s) 1.For each vertex u in V – {s}, 2.color[u] = white; d[u] = infty;  [u] = NIL 3.color[s] = GRAY; d[s] = 0;  [s] = NIL; Q = {} 4.ENQUEUE(Q,s) // Q is a FIFO queue 5.while (Q not empty) 6. u = DEQUEUE(Q) 7. for each v  Adj[u] 8. if color[v] = WHITE 9. then color[v] = GREY 10. d[v] = d[u] + 1;  [v] = u 11. ENQUEUE(Q, v); 12. color[u] = BLACK;

11 Breadth-first Search Visited all vertices reachable from the root A spanning tree For any vertex at level i, the spanning tree path from s to i has i edges, and any other path from s to i has at least i edges (shortest path property)

12 Breadth-First Search: the Color Scheme White vertices have not been discovered –All vertices start out white Grey vertices are discovered but not fully explored –They may be adjacent to white vertices Black vertices are discovered and fully explored –They are adjacent only to black and gray vertices Explore vertices by scanning adjacency list of grey vertices

13 Graphs with Non-Negative Weights Shortest Path Tree Can we use a similar idea to generate a shortest path tree which represents the shortest path from s to all vertices that are reachable from s?

14 Dijkstra’s Algorithm Solve the single-source shortest-paths problem on a weighted, directed graph and all edge weights are nonnegative Data structure –S: a set of vertices whose final shortest-path weights have already been determined –Q: a min-priority queue keyed by their d values Idea –Repeatedly select the vertex u  V-S (kept in Q) with the minimum shortest-path estimate, adds u to S, and relaxes all edges leaving u

15 BFS vs Dijkstra’s Algorithm BFS(G, s) | Dijkstra(G,s) 1.For each vertex u in V – {s}, 2.color[u] = white; d[u] = infty;  [u] = NIL 3.color[s] = GRAY; d[s] = 0;  [s] = NIL; Q = {} 4.ENQUEUE(Q,s) | ENQUEUE(Q,(s,d[s])) 5.while (Q not empty) 6. u = DEQUEUE(Q) | u = EXTRACT-MIN(Q) 7. for each v  Adj[u] 8. if color[v] = WHITE | d[v] > d[u] + w(u,v) 9. then color[v] = GREY 10. d[v] = d[u] + 1;  [v] = u | d[v] = d[u] +w(u,v) 11. ENQUEUE(Q, v); | ENQUEUE(Q, (v,d[v])) 12. color[u] = BLACK;

16 Relaxation For each vertex v  V, we maintain an attribute d[v], which is an upper bound on the weight of a shortest path from source s to v. We call d[v] a shortest-path estimate. Possible Predecessor of v in the shortest path

17 Relaxation Relaxing an edge (u, v) consists of testing whether we can improve the shortest path found so far by going through u and, if so, update d[v] and  [v] By Triangle Inequality

18 Dijkstra’s Algorithm

19

20 Properties of Shortest Paths Triangle inequality –For any edge (u,v) in E,  (s,v) <=  (s,u) + w(u,v) Upper bound property –d[v] >=  (s,v) Monotonic property –d[v] never increase No-path property –If v is not reachable then d[v] =  (s,v) = infty

21 Properties of Shortest Paths Convergence property –If (u,v) is on the shortest path from s to v and if d[u] =  (s,u) at any time prior to relaxing (u,v), then d[v] =  (s,v) at all time afterward Path-relaxation property –If p= is the shortest path from s to v k and edges of p are relaxed in order in the index, then d[v k ] =  (s, v k ). This property holds regardless of any other relaxation steps that occur, even if they are intermixed with relaxations of the edges of p

22 Properties of Shortest Paths Predecessor-subgraph property –Once d[v] =  (s,v), the predecessor subgraph is a shortest-paths tree rooted at s

23 Analysis of Dijkstra’s Algorithm Min-priority queue operations –INSERT (line 3) –EXTRACT-MIN( line 5) –DECREASE-KEY(line 8) Time analysis –Line 4-8: while loop  O(V) –Line 7-8: for loop and relaxation  |E| –Running time depends on how to implement min-priority queue Simple array: O(V 2 +E) = O(V 2 ) Binary min-heap: O((V+E)lg V) Fibonacci min-heap: O(VlgV + E)


Download ppt "Lecture 19: Shortest Paths Shang-Hua Teng. Weighted Directed Graphs Weight on edges for distance 400 2500 1000 1800 800 900."

Similar presentations


Ads by Google