Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 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,

Similar presentations


Presentation on theme: "1 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,"— Presentation transcript:

1 1 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. enqueue all vertices which are adjacent to the vertex, and have a distance of infinity from s.

2 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 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 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 5 9.3 Shortest Path Algorithms Result for Fig. 9.20, with weight=1 & start = v 3

6 6 9.3 Shortest Path Algorithms

7 7 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 d v among all the unknown vertices, and declare that the shortest path from s to v is known.

8 8 9.3 Shortest Path Algorithms For each adjacent vertex, w, update d w = d v + c v,w if this new value for d w is an improvement.

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

10 10 9.3 Shortest Path Algorithms Details on Fig. 9.28

11 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 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 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 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 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 16 9.3 Shortest Path Algorithms Total of the running time to find the minimum d v is  (|V| 2 ). Running time for updating d w 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 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 18 9.3 Shortest Path Algorithms

19 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 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 21 9.3 Shortest Path Algorithms

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

23 23 9.3 Shortest Path Algorithms

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

25 25 9.3 Shortest Path Algorithms

26 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) = LC w - EC v - c v,w

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


Download ppt "1 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,"

Similar presentations


Ads by Google