Upcoming SlideShare
×

# chapter24.ppt

2,303 views

Published on

0 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

• Be the first to like this

Views
Total views
2,303
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
64
0
Likes
0
Embeds 0
No embeds

No notes for slide

### chapter24.ppt

1. 1. Introduction to Algorithms Second Edition by Cormen, Leiserson, Rivest & Stein Chapter 24
2. 2. Single-Source Shortest Path In a shortest-paths problem , we are given a weighted, directed graph G = ( V , E ), with weight function w : E -> R mapping edges to real-valued-weights. The weight of path p = v 0, v 1, ..., vk is the sum of the weights of its constituent edges:
3. 3. Single-Source Shortest Path We define the shortest-path weight from u to v by
4. 4. Variant of Single-Source Shortest Path Single-destination shortest-paths problem: Find a shortest path to a given destination vertex t from each vertex v . By reversing the direction of each edge in the graph, we can reduce this problem to a single-source problem.
5. 5. Variant of Single-Source Shortest Path Single-pair shortest-path problem: Find a shortest path from u to v for given vertices u and v . If we solve the single-source problem with source vertex u , we solve this problem also.
6. 6. Variant of Single-Source Shortest Path All-pairs shortest-paths problem: Find a shortest path from u to v for every pair of vertices u and v . This problem can be solved by running a single-source algorithm once from each vertex.
7. 7. Single-Source Shortest Path Negative weight edges: If the graph G = ( V , E ) contains no negative-weight cycles reachable from the source s , then for all v ε V , the shortest-path weight δ ( s , v ) remains well defined, even if it has a negative value.
8. 8. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
9. 9. Single-Source Shortest Path Shortest paths are not necessarily unique.
10. 10. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
11. 11. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
12. 12. Single-Source Shortest Path The process of relaxing an edge ( u , v ) consists of testing whether we can improve the shortest path to v found so far by going through u and, if so, updating d [ v ] and π[ v ].
13. 13. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
14. 14. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
15. 15. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
16. 16. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
17. 17. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
18. 18. Single-Source Shortest Path in Directed Acyclic Graphs <ul><li>The algorithm starts by topologically sorting the dag to impose a linear ordering on the vertices. </li></ul><ul><li>If there is a path from vertex u to vertex v , then u precedes v in the topological sort. </li></ul>
19. 19. Single-Source Shortest Path in Directed Acyclic Graphs <ul><li>We make just one pass over the vertices in the topologically sorted order. </li></ul><ul><li>As we process each vertex, we relax each edge that leaves the vertex. </li></ul>
20. 20. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
21. 21. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
22. 22. Dijkstra’s Algorithm <ul><li>Dijkstra's algorithm solves the single-source shortest-paths problem on a weighted, directed graph G = ( V , E ) for the case in which all edge weights are nonnegative. </li></ul>
23. 23. Dijkstra’s Algorithm <ul><li>The algorithm repeatedly selects the vertex u є V - S </li></ul><ul><li>with the minimum shortest-path estimate, adds u to S , and relaxes all edges leaving u . </li></ul>
24. 24. Dijkstra’s Algorithm <ul><li>In the implementation, we are using a min-priority queue Q of vertices, keyed by their d values. </li></ul>
25. 25. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
26. 26. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.