1 / 27

9.3 Shortest Path Algorithms

9.3 Shortest Path Algorithms. Improvement Use a queue to keep all vertices whose shortest distance to s is known. Initialize d v to  for all vertices, except s Set d s to 0 and enqueue s. While the queue is not empty remove the first vertex in the queue.

aine
Télécharger la présentation

9.3 Shortest Path Algorithms

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 9.3 Shortest Path Algorithms • Improvement • Use a queue to keep all vertices whose shortest distance to s is known. • Initialize dvto for all vertices, except s • Set ds to 0 and enqueue s. • While the queue is not empty • remove the first vertex in the queue. • enqueue all vertices which are adjacent to the vertex, and have a distance of infinity from s.

  2. 9.3 Shortest Path Algorithms • known not used • Running time is (|E|+|V|). /* Fig. 9.18 Unweighted shortest path algorithm */ void Unweighted (Table T) /* Assume T is initialized */ { Queue Q; Vertex V, W; Q = CreateQueue (NumVertex);

  3. 9.3 Shortest Path Algorithms MakeEmpty (Q); /* Enqueue the start vertex S */ Enqueue (S, Q); while (!IsEmpty (Q)) { V = Dequeue {Q); T [V].Known = True; /* Not really needed */

  4. 9.3 Shortest Path Algorithms for each W adjacent to V if (T [W].Dist == Infinity) { T [W].Dist = T [V].Dist + 1; T [W].Path = V; Enqueue (W, Q); } } DisposeQueue (Q); /* Free the memory */ }

  5. 9.3 Shortest Path Algorithms • Result for Fig. 9.20, with weight=1 & start = v3

  6. 9.3 Shortest Path Algorithms

  7. 9.3 Shortest Path Algorithms Dijkstra’s Algorithm (for single-source weighted graphs) • A greedy algorithm, solving a problem by stages by doing what appears to be the best thing at each stage. • Select a vertex v, which has the smallest dv among all the unknown vertices, and declare that the shortest path from s to v is known.

  8. 9.3 Shortest Path Algorithms • For each adjacent vertex, w, update dw = dv + cv,w if this new value for dw is an improvement.

  9. 9.3 Shortest Path Algorithms Application of Dijkstra’s algorithm for Fig. 9.20

  10. 9.3 Shortest Path Algorithms Details on Fig. 9.28

  11. 9.3 Shortest Path Algorithms /* Fig. 9.29 Declarations for Dijkstra’s Algorithm typedef int Vertex; struct TableEntry { List Header; /* Adjacency list */ int Known; DistType Dist; Vertex Path; } #defin NotAVertex (-1) tyepdef struct TableEntry Table [NumVertex];

  12. 9.3 Shortest Path Algorithms /* Fig. 9.30 Table initialization routine */ void InitTable (Vertex Start, Graph G, Table T) { int i; ReadGraph (G, T); for (i=0; i<NumVertex; i++) { T [i].Known = False; T [i].Dist = Infinity; T [i].Path = NotAVertex; } T [Start].Dist = 0; }

  13. 9.3 Shortest Path Algorithms /* Fig. 9.32 Pseudocode - Dijkstra’s Algorithm */ void Dijkstra (Table T) { Vertex V, W; for (;;;) { V = smallest unknown distance vertex; if (V == NotAVertex) break; T [V].Known = True; for each W adjacent to V

  14. 9.3 Shortest Path Algorithms if (!T [W].Known) if (T [V].Dist + Cvw < T [W].Dist) { /* update W */ Decrease (T [W].Dist to T [V].Dist + Cvw); T [W].Path = V; } } }

  15. 9.3 Shortest Path Algorithms /* Fig. 9.31 Routine to print actual shortest path */ /* Assume the path exists */ void PrintPath (Vertex V, Table T) { if (T [V].Path != NotAVertex) { PrintPath (T [V].Path, T) printf (" to"); } printf ("%v", V); /* %v is pseudocode */ }

  16. 9.3 Shortest Path Algorithms • Total of the running time to find the minimum dv is (|V|2). • Running time for updating dw is constant per update, for a total of (|E|). • Total running time is (|E|+|V|2). • For a sparse graph, |E| = (|V|), the algorithm is slow. It’s best to keep the distances in a priority queue.

  17. 9.3 Shortest Path Algorithms Critical Path Analysis (An Acyclic Graph) • Given an activity-node graph • The edges represent precedence relationships: (v, w) means that activity v must be completed before activity w may begin. • Activities independent of each other can be performed in parallel.

  18. 9.3 Shortest Path Algorithms

  19. 9.3 Shortest Path Algorithms • Convert the activity-node graph to an event-node graph. • Each event corresponds to the completion of an activity and all its dependent activities. • Events reachable from a node v in the event-node graph may not commence until after the event v is completed.

  20. 9.3 Shortest Path Algorithms • Dummy edges and nodes may need to be inserted in the case where an activity depends on several others, to avoid introducing false dependencies (or false lack of dependencies).

  21. 9.3 Shortest Path Algorithms

  22. 9.3 Shortest Path Algorithms • Earliest completion time • Length of the longest path from the first event to the last event • Let ECi be the earliest completion time for node i • Compute for all vertices by their topological order.

  23. 9.3 Shortest Path Algorithms

  24. 9.3 Shortest Path Algorithms • Latest starting time • Latest starting time • Let LCi be the latest time event i must be completed without affecting the final completion time. • Computed by reverse topological order.

  25. 9.3 Shortest Path Algorithms

  26. 9.3 Shortest Path Algorithms • Slack time for each edge is the amount of time that the completion of the corresponding activity can be delayed without affecting the overall completion. Slack (v,w) = LCw - ECv - cv,w

  27. 9.3 Shortest Path Algorithms • A critical path consists entirely of zero-slack edges.

More Related