Download presentation

Presentation is loading. Please wait.

Published byJefferson Gascoigne Modified over 2 years ago

1
© DEEDS 2008Graph Algorithms1 Remarks on Dijkstra Determining the minimum marginal element (code line: v = the vertex in M 2 with minimum v.L; ) accounts for the substantial cost of the algorithm: An implementation that uses lists requires to traverse the interlinking for each vertex once: time complexity O(|V| 2 ) An implementation that uses a priority queue for the margin instead of a list has a time complexity of O(|E| log |V|) Implementations with Fibonacci-heaps give O(|V|log|V|+|E|) |V| max eE c(e) is allowed instead of If the vertex weights are all 1, the Moore‘ Algorithm performs better (it is linear)

2
© DEEDS 2008Graph Algorithms2 Variants Dijkstra‘s Algorithm can be enhanced: In addition to the length of the shortest path the algorithm could save the path itself Carry the predecessor vertex along the shortest path Collecting these edges results in a spanning tree Find the shortest path to a particular vertex Abort the algorithm, as soon as this vertex was moved to M 1 Dijkstra‘ algorithm does not work in a setting where negative vertex weights are allowed. Then use the Bellmann-Ford Algorithm p. 588

3
© DEEDS 2008Graph Algorithms3 Negative Edges Why negative edges? Energy consumption: uphill vs. downhill Currency exchange: find cycles that “produce” money Search Google for “arbitrage” and see for yourselves… Instead of minimal sum, we have maximal product

4
© DEEDS 2008Graph Algorithms4 Negative Weight Edges Graph G=(V, E), for all vertices vV reachable from source s, the distance is δ(a,b) = 5 δ(a,h) = + ∞ δ(a,c) = ? δ(a,f) = ? δ(a,c) = min {,,,…} δ(a,f) = min{,,,…} a b d f e h i j 5 7 2 c 2 g 3 -6

5
© DEEDS 2008Graph Algorithms5 c b a e d 0 8 2 7 5 10 8 76 21 2 3 -8 Dijkstra ?! Why doesn’t Dijkstra’s algorithm work for negative weights? c b a e d f 0 8 2 7 5 9 10 8 76 21 2 3 9 f 9

6
© DEEDS 2008Graph Algorithms6 Bellmann-Ford Solves the shortest path problem even with negative edges The graph must not contain negative cycles! Only works for directed graphs However: negative cycles seldom appear in real-life problems

7
© DEEDS 2008Graph Algorithms7 Bellman-Ford Algorithm 0 ∞ ∞ ∞ ∞ 6 7 2 -2 -3 -4 7 9 s t x z y 6 7 4 2 2 -2 5 8 Round 1Round 2Round 3Round 4 Given: graph G = (V, E) with s, v V and w : E R Problem: Solve single-source shortest-paths problem in the general case, returning FALSE if negative cycles exists Strategy: Similar with Dijkstra’s algorithm, but relax all edges at each step, not just the neigbors… Algorithm BellmanFord(G, s) for all v V if (v s) d[v] = 0else d[v] = for i 1 to |V|-1 do do for each e = (v,u) E if (d[u] > d[v]+w(v,u)) d[u] d[v]+w(v,u) for each e = (v,u) E do if (d[u] > d[v] w(u,v)) return FALSE return TRUE Relaxation order: (t,x),(t,y),(t,z),(y,x),(y,z),(z,x),(z,s),(x,t),(s,t),(s,y) p. 588

8
© DEEDS 2008Graph Algorithms8 BF Algorithm Works even with negative-weight edges Must assume directed edges (otherwise we would have negative-weight cycles) Each iteration i finds all shortest paths of length i (lines 3-6) If “improvements” can be made, negative cycles exist (line 7 to 10) Proof in Cormen p 590 Running time: Line 3: O(|V|) Line 4: O(|E|) Total: O(|V||E|) Dijkstra: at least O(|E|log|V|) Algorithm BellmanFord(G, s) 1. 1.for all v V 2.if (v s) d[v] = 0else d[v] = 3. for i 1 to |V|-1 do 4. do for each e = (u,v) E 5. if (d[v] > d[u]+w(u,v)) 6. d[v] d[u]+w(u,v) 7. for each e = (u,v) E 8. do if (d[v] > d[u] weight(u,v)) 9. return FALSE 10. return TRUE

9
© DEEDS 2008Graph Algorithms9 Dijkstra Variants Is Dijkstra the most suitable algorithm for finding the shortest path in geographical applications, such as route planning or computer games?

10
© DEEDS 2008Graph Algorithms10 Competition: A* A* - (pronounced A-star) Similar to Dijkstra but each node also contains information about the minimum distance to the destination. For geographical applications this typically is the beeline (there is no shorter path!) The algorithm first searches the nodes which sum of computed distance (from start) and minimal (approximated) distance to source

11
© DEEDS 2008Graph Algorithms11 A-Star Algorithm (simplified) /* Nodes labeledds(v)=(computed) distance from start, dd(v)=(approximated) distance to destination, M 3 implicitly defined as V \ ( M 1 M 2 ) */ for (all v V) { /*Initialization*/ dd(v) = beelines; if ( v s ) ds(v) = ; } M 1 = { s }; M 2 = { }; ds(s) = 0; for (all v s.neighbors( ) ) {/* fill M 2 initially */ M 2 = M 2 { v }; ds(v) = c( (s, v) ); } while ( M 2 { } && destination M 1 ) { v = The node in M 2 with minimal (ds(v) + dd(v)); M 2 = M 2 \ { v }; M 1 = M 2 { v }; /* move v */ for (all w v.neighbours( ) ) { if ( w M 3 ) { /* move neighbors and adapt ds(w) */ M 2 = M 2 { w }; ds(w) = ds(v) + c( (v, w) ); } else if ( ds(w) > ds(v) + c( (v, w) ) ) ds(w) = ds(v) + c( (v,w) ); }

12
© DEEDS 2008Graph Algorithms12 Example Destination: E dd(v) marked in green=beeline

13
© DEEDS 2008Graph Algorithms13 red: ds(v) in brackets: ds(v)+dd(v) Example

14
© DEEDS 2008Graph Algorithms14 Example

15
© DEEDS 2008Graph Algorithms15 Example

16
© DEEDS 2008Graph Algorithms16 Destination reached! No other path is shorter, since the minimal distance (in brackets) is already shorter than the actual distance to the destination! Example A B C D E F G H I K L M O P N X Y Z 0 112 79 110 90 39 37 56 87 30 72 109 94 50 55 100 43 66 0 (87) 43 (152) 40 (150) 34 (113) 65 (121) 55 (110) 80 (110) 109 (148) 146 (196) 100 (143) 138 (175) 111 (111)

17
© DEEDS 2008Graph Algorithms17 A-Star The actual algorithm is optimized by using a priority queue structure Complexity: the same as Dijkstra, but better average case execution time Why not always use A-Star? Needs a known destination Needs an approximated minimal distance

Similar presentations

Presentation is loading. Please wait....

OK

Lecture 13 Algorithm Analysis

Lecture 13 Algorithm Analysis

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google