outline and reading
play

Outline and Reading Weighted graphs (7.1) Shortest Paths Shortest - PDF document

Outline and Reading Weighted graphs (7.1) Shortest Paths Shortest path problem Shortest path properties Dijkstras algorithm (7.1.1) 0 A 4 8 Algorithm 2 8 2 3 Edge relaxation 7 1 B C D The Bellman-Ford


  1. Outline and Reading Weighted graphs (§7.1) Shortest Paths � Shortest path problem � Shortest path properties Dijkstra’s algorithm (§7.1.1) 0 A 4 8 � Algorithm 2 8 2 3 � Edge relaxation 7 1 B C D The Bellman-Ford algorithm (§7.1.2) 3 9 5 8 2 5 Shortest paths in dags (§7.1.3) E F All-pairs shortest paths (§7.2.1) Shortest Paths 1 Shortest Paths 2 Weighted Graphs Shortest Path Problem Given a weighted graph and two vertices u and v , we want to In a weighted graph, each edge has an associated numerical find a path of minimum total weight between u and v. value, called the weight of the edge � Length of a path is the sum of the weights of its edges. Edge weights may represent, distances, costs, etc. Example: Example: � Shortest path between Providence and Honolulu � In a flight route graph, the weight of an edge represents the Applications distance in miles between the endpoint airports � Internet packet routing � Flight reservations 8 4 9 PVD 3 4 � Driving directions 1 8 ORD 142 8 4 9 PVD 3 SFO 4 ORD 1 8 142 SFO 802 1205 1743 LGA 802 337 1205 1743 LGA 1387 337 1099 2555 1387 HNL 1233 1099 2555 LAX HNL 1120 DFW 1233 LAX 1120 MIA DFW MIA Shortest Paths 3 Shortest Paths 4 Shortest Path Properties Dijkstra’s Algorithm Property 1: The distance of a vertex We grow a “ cloud ” of vertices, A subpath of a shortest path is itself a shortest path v from a vertex s is the beginning with s and eventually Property 2: length of a shortest path covering all the vertices There is a tree of shortest paths from a start vertex to all the other between s and v We store with each vertex v a vertices Dijkstra’s algorithm label d ( v ) representing the Example: computes the distances distance of v from s in the Tree of shortest paths from Providence of all the vertices from a subgraph consisting of the cloud 9 8 4 PVD given start vertex s and its adjacent vertices 3 8 4 ORD 1 142 Assumptions: At each step SFO 802 � the graph is connected � We add to the cloud the vertex 1205 1743 LGA 337 u outside the cloud with the 1387 � the edges are 1099 2555 smallest distance label, d ( u ) HNL undirected 1233 LAX � We update the labels of the 1120 � the edge weights are DFW vertices adjacent to u MIA nonnegative Shortest Paths 5 Shortest Paths 6

  2. Edge Relaxation Example 0 0 A 4 A 4 Consider an edge e = ( u,z ) 8 8 such that 2 2 d ( u ) = 50 d ( z ) = 75 8 2 4 8 2 3 10 � u is the vertex most recently 7 1 7 1 B C D B C D e u added to the cloud z s 3 9 3 9 ∞ ∞ � z is not in the cloud 5 8 2 5 2 5 E F E F The relaxation of edge e updates distance d ( z ) as 0 0 follows: A 4 A 4 8 8 d ( u ) = 50 d ( z ) ← min{ d ( z ) ,d ( u ) + weight ( e )} d ( z ) = 60 2 2 10 8 2 3 7 2 3 e u 7 1 7 1 B C D B C D s z 3 9 3 9 5 11 5 8 2 5 2 5 E F E F Shortest Paths 7 Shortest Paths 8 Example (cont.) Dijkstra’s Algorithm Algorithm DijkstraDistances ( G, s ) A priority queue stores 0 Q ← new heap-based priority queue A the vertices outside the 4 8 for all v ∈ G.vertices () cloud 2 if v = s 7 2 3 � Key: distance 7 1 setDistance ( v, 0) B C D � Element: vertex else 3 9 Locator-based methods setDistance ( v, ∞ ) 5 8 2 5 l ← Q.insert ( getDistance ( v ) , v ) � insert ( k,e ) returns a E F setLocator ( v,l ) locator 0 while ¬ Q.isEmpty () A � replaceKey ( l,k ) changes 4 8 u ← Q.removeMin () the key of an item 2 for all e ∈ G.incidentEdges ( u ) We store two labels 7 2 3 7 1 { relax edge e } B C D with each vertex: z ← G.opposite ( u,e ) � Distance (d(v) label) r ← getDistance ( u ) + weight ( e ) 3 9 5 8 � locator in priority if r < getDistance ( z ) 2 5 E F queue setDistance ( z,r ) Q.replaceKey ( getLocator ( z ) ,r ) Shortest Paths 9 Shortest Paths 10 Analysis Extension Graph operations Using the template Algorithm DijkstraShortestPathsTree ( G, s ) � Method incidentEdges is called once for each vertex method pattern, we … Label operations can extend Dijkstra’s algorithm to return a � We set/get the distance and locator labels of vertex z O (deg( z )) times for all v ∈ G.vertices () tree of shortest paths � Setting/getting a label takes O (1) time … from the start vertex Priority queue operations setParent ( v, ∅ ) to all other vertices � Each vertex is inserted once into and removed once from the priority … We store with each queue, where each insertion or removal takes O (log n ) time vertex a third label: for all e ∈ G.incidentEdges ( u ) � The key of a vertex in the priority queue is modified at most deg( w ) � parent edge in the { relax edge e } times, where each key change takes O (log n ) time shortest path tree z ← G.opposite ( u,e ) Dijkstra’s algorithm runs in O (( n + m ) log n ) time provided the r ← getDistance ( u ) + weight ( e ) In the edge relaxation graph is represented by the adjacency list structure if r < getDistance ( z ) step, we update the � Recall that Σ v deg( v ) = 2 m setDistance ( z,r ) parent label setParent ( z,e ) The running time can also be expressed as O ( m log n ) since the Q.replaceKey ( getLocator ( z ) ,r ) graph is connected Shortest Paths 11 Shortest Paths 12

  3. Why Dijkstra’s Algorithm Why It Doesn’t Work for Works Negative-Weight Edges Dijkstra’s algorithm is based on the greedy Dijkstra’s algorithm is based on the greedy method. It adds vertices by increasing distance. method. It adds vertices by increasing distance. � Suppose it didn’t find all shortest distances. Let F be the first wrong 0 0 A A 4 4 vertex the algorithm processed. 8 8 � If a node with a negative � When the previous node, D, on the 2 6 incident edge were to be added 7 2 3 7 5 4 7 1 7 1 true shortest path was considered, B C D B C D late to the cloud, it could mess its distance was correct. up distances for vertices already 3 9 0 -8 5 8 5 9 � But the edge (D,F) was relaxed at in the cloud. 2 5 2 5 E F E F that time! � Thus, so long as d(F)>d(D), F’s distance cannot be wrong. That is, C’s true distance is 1, but there is no wrong vertex. it is already in the cloud with d(C)=5! Shortest Paths 13 Shortest Paths 14 Bellman-Ford Algorithm Bellman-Ford Example Nodes are labeled with their d(v) values Works even with negative- Algorithm BellmanFord ( G, s ) weight edges 0 0 4 4 for all v ∈ G.vertices () 8 8 Must assume directed if v = s -2 -2 edges (for otherwise we setDistance ( v, 0) 8 -2 4 7 1 7 1 ∞ ∞ ∞ ∞ ∞ ∞ else would have negative- setDistance ( v, ∞ ) weight cycles) 3 9 3 9 for i ← 1 to n-1 do Iteration i finds all shortest -2 5 -2 5 ∞ ∞ ∞ ∞ for each e ∈ G.edges () paths that use i edges. { relax edge e } Running time: O(nm). u ← G.origin ( e ) Can be extended to detect z ← G.opposite ( u,e ) 0 0 4 4 8 8 r ← getDistance ( u ) + weight ( e ) a negative-weight cycle if it -2 -2 if r < getDistance ( z ) exists 5 7 1 -1 7 1 setDistance ( z,r ) 8 -2 4 5 -2 -1 � How? 1 3 9 3 9 6 9 4 -2 5 -2 5 ∞ ∞ 1 9 Shortest Paths 15 Shortest Paths 16 1 DAG-based Algorithm DAG Example Nodes are labeled with their d(v) values 1 1 Algorithm DagDistances ( G, s ) 0 0 4 4 for all v ∈ G.vertices () 8 8 Works even with if v = s -2 -2 negative-weight edges setDistance ( v, 0) 8 -2 4 4 4 3 7 2 1 3 7 2 1 Uses topological order ∞ ∞ ∞ ∞ ∞ ∞ else Doesn’t use any fancy setDistance ( v, ∞ ) 3 9 3 9 data structures Perform a topological sort of the vertices -5 5 -5 5 ∞ ∞ ∞ ∞ for u ← 1 to n do {in topological order} Is much faster than 6 5 6 5 for each e ∈ G.outEdges ( u ) Dijkstra’s algorithm { relax edge e } 1 1 Running time: O(n+m). z ← G.opposite ( u,e ) 0 0 4 4 8 8 r ← getDistance ( u ) + weight ( e ) -2 -2 if r < getDistance ( z ) 5 4 4 3 7 2 1 -1 3 7 2 1 setDistance ( z,r ) 8 -2 4 5 -2 -1 9 3 3 9 0 4 1 7 -5 5 -5 5 ∞ ∞ 1 7 6 5 6 5 (two steps) Shortest Paths 17 Shortest Paths 18

  4. All-Pairs Shortest Paths Find the distance Algorithm AllPair ( G ) {assumes vertices 1,…, n } between every pair of for all vertex pairs (i,j) vertices in a weighted if i = j D 0 [ i,i ] ← 0 directed graph G. else if ( i,j ) is an edge in G We can make n calls to D 0 [ i,j ] ← weight of edge ( i,j ) Dijkstra’s algorithm (if no else negative edges), which D 0 [ i,j ] ← + ∞ takes O(nmlog n) time. for k ← 1 to n do Likewise, n calls to for i ← 1 to n do Bellman-Ford would take for j ← 1 to n do O(n 2 m) time. D k [ i,j ] ← min{ D k-1 [ i,j ], D k-1 [ i,k ]+ D k-1 [ k,j ]} return D n We can achieve O(n 3 ) time using dynamic Uses only vertices numbered 1,…,k i programming (similar to (compute weight of this edge) the Floyd-Warshall j Uses only vertices algorithm). numbered 1,…,k-1 Uses only vertices k numbered 1,…,k-1 Shortest Paths 19

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend