Download presentation
Presentation is loading. Please wait.
1
Shortest Paths C B A E D F 3 2 8 5 4 7 1 9 Shortest Paths 1
07/28/16 16:30 07/28/16 16:30 Shortest Paths C B A E D F 3 2 8 5 4 7 1 9 Shortest Paths 1 1
2
Weighted Graphs PVD ORD SFO LGA HNL LAX DFW MIA
Shortest Path 07/28/16 16:30 Weighted Graphs In a weighted graph, each edge has an associated numerical value, called the weight of the edge Edge weights may represent, distances, costs, etc. Example: In a flight route graph, the weight of an edge represents the distance in miles between the endpoint airports 849 PVD 1843 ORD 142 SFO 802 LGA 1743 1205 337 1387 HNL 2555 1099 LAX 1233 DFW 1120 MIA Shortest Paths 2
3
Shortest Paths PVD ORD SFO LGA HNL LAX DFW MIA
07/28/16 16:30 Shortest Paths Given a weighted graph and two vertices u and v, we want to find a path of minimum total weight between u and v. Length of a path is the sum of the weights of its edges. Example: Shortest path between Providence and Honolulu Applications Internet packet routing Flight reservations Driving directions 849 PVD 1843 ORD 142 SFO 802 LGA 1743 1205 337 1387 HNL 2555 1099 LAX 1233 DFW 1120 MIA Shortest Paths 3
4
Shortest Path Properties
07/28/16 16:30 Shortest Path Properties Property 1: A subpath of a shortest path is itself a shortest path Property 2: There is a tree of shortest paths from a start vertex to all the other vertices Example: Tree of shortest paths from Providence 849 PVD 1843 ORD 142 SFO 802 LGA 1743 1205 337 1387 HNL 2555 1099 LAX 1233 DFW 1120 MIA Shortest Paths 4
5
Shortest Path 07/28/16 16:30 Dijkstra’s Algorithm The distance of a vertex v from a vertex s is the length of a shortest path between s and v Dijkstra’s algorithm computes the distances of all the vertices from a given start vertex s Assumptions: the graph is connected the edges are undirected the edge weights are nonnegative We grow a “cloud” of vertices, beginning with s and eventually covering all the vertices We store with each vertex v a label d(v) representing the distance of v from s in the subgraph consisting of the cloud and its adjacent vertices At each step We add to the cloud the vertex u outside the cloud with the smallest distance label, d(u) We update the labels of the vertices adjacent to u Shortest Paths 5
6
Edge Relaxation Consider an edge e (u,z) such that d(z) 75
Shortest Path 07/28/16 16:30 Edge Relaxation Consider an edge e (u,z) such that u is the vertex most recently added to the cloud z is not in the cloud The relaxation of edge e updates distance d(z) as follows: d(z) min{d(z),d(u) weight(e)} d(u) 50 10 d(z) 75 e u s z d(u) 50 10 d(z) 60 u e s z Shortest Paths 6
7
Shortest Path 07/28/16 16:30 Example C B A E D F 3 2 8 5 4 7 1 9 A 4 8 2 8 2 4 7 1 B C D 3 9 2 5 E F A 4 A 4 8 8 2 2 8 2 3 7 2 3 7 1 7 1 B C D B C D 3 9 3 9 5 11 5 8 2 5 2 5 E F E F Shortest Paths 7
8
Example (cont.) A 4 8 2 7 2 3 7 1 B C D 3 9 5 8 2 5 E F A 4 8 2 7 2 3
Shortest Path 07/28/16 16:30 Example (cont.) A 4 8 2 7 2 3 7 1 B C D 3 9 5 8 2 5 E F A 4 8 2 7 2 3 7 1 B C D 3 9 5 8 2 5 E F Shortest Paths 8
9
Shortest Path 07/28/16 16:30 Dijkstra’s Algorithm Algorithm DijkstraDistances(G, s) Q new heap-based priority queue for all v G.vertices() if v s v.setDistance(0) else v.setDistance() l Q.insert(v.getDistance(), v) v.setEntry(l) while Q.empty() l Q.removeMin() u l.getValue() for all e u.incidentEdges() { relax e } z e.opposite(u) r u.getDistance() e.weight() if r z.getDistance() z.setDistance(r) Q.replaceKey(z.getEntry(), r) A heap-based adaptable priority queue with location-aware entries stores the vertices outside the cloud Key: distance Value: vertex Recall that method replaceKey(l,k) changes the key of entry l We store two labels with each vertex: Distance Entry in priority queue Shortest Paths 9
10
Analysis of Dijkstra’s Algorithm
Shortest Path 07/28/16 16:30 Analysis of Dijkstra’s Algorithm Graph operations Method incidentEdges is called once for each vertex Label operations We set/get the distance and locator labels of vertex z O(deg(z)) times Setting/getting a label takes O(1) time Priority queue operations Each vertex is inserted once into and removed once from the priority queue, where each insertion or removal takes O(log n) time The key of a vertex in the priority queue is modified at most deg(w) times, where each key change takes O(log n) time Dijkstra’s algorithm runs in O((n m) log n) time provided the graph is represented by the adjacency list structure Recall that v deg(v) 2m The running time can also be expressed as O(m log n) since the graph is connected Shortest Paths 10
11
Shortest Path 07/28/16 16:30 Shortest Paths Tree Using the template method pattern, we can extend Dijkstra’s algorithm to return a tree of shortest paths from the start vertex to all other vertices We store with each vertex a third label: parent edge in the shortest path tree In the edge relaxation step, we update the parent label Algorithm DijkstraShortestPathsTree(G, s) … for all v G.vertices() v.setParent() for all e u.incidentEdges() { relax edge e } z e.opposite(u) r u.getDistance() e.weight() if r z.getDistance() z.setDistance(r) z.setParent(e) Q.replaceKey(z.getEntry(),r) Shortest Paths 11
12
Why Dijkstra’s Algorithm Works
Shortest Path 07/28/16 16:30 Why Dijkstra’s Algorithm Works Dijkstra’s algorithm is based on the greedy method. It adds vertices by increasing distance. Suppose it didn’t find all shortest distances. Let F be the first wrong vertex the algorithm processed. When the previous node, D, on the true shortest path was considered, its distance was correct But the edge (D,F) was relaxed at that time! Thus, so long as d(F)>d(D), F’s distance cannot be wrong. That is, there is no wrong vertex A 8 4 2 7 2 3 7 1 B C D 3 9 5 8 2 5 E F Shortest Paths 12
13
Why It Doesn’t Work for Negative-Weight Edges
Shortest Path 07/28/16 16:30 Why It Doesn’t Work for Negative-Weight Edges Dijkstra’s algorithm is based on the greedy method. It adds vertices by increasing distance. A 8 4 If a node with a negative incident edge were to be added late to the cloud, it could mess up distances for vertices already in the cloud. 6 7 5 4 7 1 B C D -8 5 9 2 5 E F C’s true distance is 1, but it is already in the cloud with d(C)=5! Shortest Paths 13
14
Bellman-Ford Algorithm (not in book)
Shortest Path 07/28/16 16:30 Bellman-Ford Algorithm (not in book) Works even with negative- weight edges Must assume directed edges (for otherwise we would have negative- weight cycles) Iteration i finds all shortest paths that use i edges. Running time: O(nm). Can be extended to detect a negative-weight cycle if it exists How? Algorithm BellmanFord(G, s) for all v G.vertices() if v s v.setDistance(0) else v.setDistance() for i 1 to n1 do for each e G.edges() { relax edge e } u e.origin() z e.opposite(u) r u.getDistance() e.weight() if r z.getDistance() z.setDistance(r) Shortest Paths 14
15
Nodes are labeled with their d(v) values
Shortest Path 07/28/16 16:30 Bellman-Ford Example Nodes are labeled with their d(v) values 4 4 8 8 -2 -2 8 -2 4 7 1 7 1 3 9 3 9 -2 5 -2 5 4 4 8 8 -2 -2 5 7 1 -1 7 1 8 -2 4 5 -2 -1 1 3 9 3 9 6 9 4 -2 5 -2 5 1 9 Shortest Paths 15
16
DAG-based Algorithm (not in book)
Shortest Path 07/28/16 16:30 DAG-based Algorithm (not in book) Algorithm DagDistances(G, s) for all v G.vertices() if v s v.setDistance(0) else v.setDistance() { Perform a topological sort of the vertices } for u 1 to n do {in topological order} for each e u.outEdges() { relax edge e } z e.opposite(u) r u.getDistance() e.weight() if r z.getDistance() z.setDistance(r) Works even with negative-weight edges Uses topological order Doesn’t use any fancy data structures Is much faster than Dijkstra’s algorithm Running time: O(n+m). Shortest Paths 16
17
Nodes are labeled with their d(v) values
Shortest Path 07/28/16 16:30 DAG Example Nodes are labeled with their d(v) values 1 1 4 4 8 8 -2 -2 4 8 -2 4 4 3 7 2 1 3 7 2 1 3 9 3 9 -5 5 -5 5 6 5 6 5 1 1 4 4 8 8 -2 -2 5 3 7 2 1 4 -1 3 7 2 1 4 8 -2 4 5 -2 -1 9 3 3 9 1 7 4 -5 5 -5 5 1 7 6 5 6 5 Shortest Paths 17 (two steps)
18
Minimum Spanning Trees
Shortest Path Minimum Spanning Tree 07/28/16 16:30 07/28/16 16:30 Minimum Spanning Trees 2704 867 BOS 849 PVD ORD 187 740 144 1846 621 JFK 184 1258 802 SFO 1391 BWI 1464 337 1090 DFW 946 LAX 1235 1121 MIA 2342 Minimum Spanning Trees 18 18
19
Minimum Spanning Trees
Shortest Path 07/28/16 16:30 Minimum Spanning Trees Spanning subgraph Subgraph of a graph G containing all the vertices of G Spanning tree Spanning subgraph that is itself a (free) tree Minimum spanning tree (MST) Spanning tree of a weighted graph with minimum total edge weight Applications Communications networks Transportation networks ORD 10 1 PIT DEN 6 7 9 3 DCA STL 4 8 5 2 DFW ATL Minimum Spanning Trees 19
20
Cycle Property Cycle Property: C C 8 f
Shortest Path 07/28/16 16:30 Cycle Property 8 4 2 3 6 7 9 e C f Cycle Property: Let T be a minimum spanning tree of a weighted graph G Let e be an edge of G that is not in T and let C be the cycle formed by e with T. For every edge f of C, weight(f) weight(e). Proof: By contradiction If weight(f) weight(e) we can get a spanning tree of smaller weight by replacing e with f. Replacing f with e yields a better spanning tree 8 4 2 3 6 7 9 C e f Minimum Spanning Trees 20
21
Partition Property Partition Property: U V 7 f
Shortest Path 07/28/16 16:30 Partition Property U V 7 f Partition Property: Consider a partition of the vertices of G into subsets U and V. Let e be an edge of minimum weight across the partition. There is a minimum spanning tree of G containing edge e. Proof: Let T be an MST of G. If T does not contain e, consider the cycle C formed by e with T and let f be an edge of C across the partition. By the cycle property, weight(f) weight(e). Thus, weight(f) weight(e). We obtain another MST by replacing f with e. 4 9 5 2 8 8 3 e 7 Replacing f with e yields another MST. U V 7 f 4 9 5 2 8 8 3 e 7 Minimum Spanning Trees 21
22
Minimum Spanning Trees
Shortest Path 07/28/16 16:30 Kruskal’s Algorithm Maintain a partition of the vertices into clusters. Initially, single-vertex clusters. Keep an MST for each cluster. Merge “closest” clusters and their MSTs. A priority queue stores the edges outside clusters. Key: weight Element: edge At the end of the algorithm, there are one cluster and MST. Algorithm KruskalMST(G) for each vertex v in G do Create a cluster consisting of v let Q be a priority queue. Insert all edges into Q T {T is the union of the MSTs of the clusters} while T has fewer than n 1 edges do e Q.removeMin().getValue() [u, v] G.endVertices(e) A getCluster(u) B getCluster(v) if A B then Add edge e to T mergeClusters(A, B) return T Minimum Spanning Trees 22
23
Shortest Path 07/28/16 16:30 Example G G 8 8 B 4 B 4 E 9 E 6 9 6 5 1 F 5 1 F C 3 C 3 11 11 2 2 7 H 7 H D D A 10 A 10 G G 8 8 B 4 B 4 9 E E 6 9 6 5 1 F 5 1 F C 3 11 C 3 11 2 2 7 H 7 H D D A A 10 10 Campus Tour 23
24
Example (contd.) four steps two steps G G 8 8 B 4 B 4 E 9 E 6 9 6 5 F
Shortest Path 07/28/16 16:30 Example (contd.) G G 8 8 B 4 B 4 E 9 E 6 9 6 5 F 5 1 1 F C 3 11 C 3 11 2 2 7 H 7 H D D A 10 A 10 four steps two steps G G 8 8 B 4 B 4 E E 9 6 9 6 5 5 1 F 1 F C 3 3 11 C 11 2 2 7 H 7 H D D A A 10 10 Campus Tour 24
25
Minimum Spanning Trees
Shortest Path 07/28/16 16:30 Data Structure for Kruskal’s Algorithm The algorithm maintains a forest of trees. A priority queue extracts the edges by increasing weight. An edge is accepted it if connects distinct trees. We need a union-find data structure, which maintains a collection of disjoint sets, with operations: makeSet(u): create a set consisting of u find(u): return the set storing u union(A, B): replace sets A and B with their union Minimum Spanning Trees 25
26
Minimum Spanning Trees
Shortest Path 07/28/16 16:30 Running Time Cluster merges as unions. Cluster locations as finds. Running time O((n + m) log n) PQ operations O(m log n) UF operations O(n log n) with lists O(n log* n) with trees Algorithm KruskalMST(G) Initialize a partition P for each vertex v in G do P.makeSet(v) let Q be a priority queue. Insert all edges into Q T {T is the union of the MSTs of the clusters} while T has fewer than n 1 edges do e Q.removeMin().getValue() [u, v] G.endVertices(e) A P.find(u) B P.find(v) if A B then Add edge e to T P.union(A, B) return T Minimum Spanning Trees 26
27
Minimum Spanning Trees
Shortest Path 07/28/16 16:30 Prim-Jarnik’s Algorithm Similar to Dijkstra’s algorithm. We pick an arbitrary vertex s and we grow the MST as a cloud of vertices, starting from s. We store with each vertex v label d(v) representing the smallest weight of an edge connecting v to a vertex in the cloud. At each step: We add to the cloud the vertex u outside the cloud with the smallest distance label. We update the labels of the vertices adjacent to u. Minimum Spanning Trees 27
28
Minimum Spanning Trees
Shortest Path 07/28/16 16:30 Prim-Jarnik’s Algorithm (cont.) A heap-based adaptable priority queue with location- aware entries stores the vertices outside the cloud Key: distance Value: vertex Recall that method replaceKey(l,k) changes the key of entry l We store three labels with each vertex: Distance Parent edge in MST Entry in priority queue Algorithm PrimJarnikMST(G) Q new heap-based priority queue s a vertex of G for all v G.vertices() if v s v.setDistance(0) else v.setDistance() v.setParent() l Q.insert(v.getDistance(), v) v.setLocator(l) while Q.empty() l Q.removeMin() u l.getValue() for all e u.incidentEdges() z e.opposite(u) r e.weight() if r z.getDistance() z.setDistance(r) z.setParent(e) Q.replaceKey(z.getEntry(), r) Minimum Spanning Trees 28
29
Minimum Spanning Trees
Shortest Path 07/28/16 16:30 Example 7 7 D 7 D 2 2 B 4 B 4 8 9 5 9 5 5 2 F 2 F C C 8 8 3 3 8 8 E E A 7 A 7 7 7 7 7 7 D 2 7 D 2 B 4 B 4 5 9 5 5 9 4 2 F 5 C 2 F 8 C 8 3 8 3 8 E A E 7 7 A 7 7 Minimum Spanning Trees 29
30
Minimum Spanning Trees
Shortest Path 07/28/16 16:30 Example (contd.) 7 7 D 2 B 4 9 4 5 5 2 F C 8 3 8 E A 3 7 7 7 D 2 B 4 5 9 4 5 2 F C 8 3 8 E A 3 7 Minimum Spanning Trees 30
31
Minimum Spanning Trees
Shortest Path 07/28/16 16:30 Analysis Graph operations Method incidentEdges is called once for each vertex. Label operations Set/get the distance, parent and locator labels of vertex z O(deg(z)) times. Setting/getting a label takes O(1) time. Priority queue operations Each vertex is inserted once into and removed once from the priority queue, where each insertion or removal takes O(log n) time. The key of a vertex w in the priority queue is modified at most deg(w) times, where each key change takes O(log n) time. Prim-Jarnik’s algorithm runs in O((n m) log n) time provided the graph is represented by the adjacency list structure. Recall that v deg(v) 2m The running time is O(m log n) since the graph is connected. Minimum Spanning Trees 31
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.