190 likes | 336 Views
CSC 2300 Data Structures & Algorithms. April 13, 2007 Chapter 9. Graph Algorithms. Today. Dijkstra’s Algorithm Negative Edge Costs Acyclic Graphs Critical Path. Recall Dijkstra’s Algorithm. After v 6 is declared known and algorithm terminates:. Stages of Dijkstra’s Algorithm.
E N D
CSC 2300Data Structures & Algorithms April 13, 2007 Chapter 9. Graph Algorithms
Today • Dijkstra’s Algorithm • Negative Edge Costs • Acyclic Graphs • Critical Path
Recall Dijkstra’s Algorithm • After v6 is declared known and algorithm terminates:
Pseudocode of Dijkstra’s Algorithm • Running time?
Running Time • Running time depends on how vertices are handled. • If we sequentially scan the vertices to find the minimum dv, each phase will take O(|V|) time to find the minimum. The time to find the minima is then O(|V|2). • The time to update dw is constant per update, and there is at most one update per edge for a total of O(|E|). • Total running time is O(|E| + |V|2) = O(|V|2). • If the graph is dense, with |E| = Θ(|V|2), the algorithm is optimal. • What if the graph were sparse, with |E| = Θ(|V|)?
Sparse Graph • Recall: If we sequentially scan the vertices to find the minimum dv, each phase will take O(|V|) time to find the minimum. The time to find the minima is then O(|V|2). • How can we do better? • Keep the distances in a priority queue. • Select vertex v: deleteMin. • Update w’s distance: decreaseKey. • Why is it difficult to update the distances? • The find operation in priority queues. • Is there an alternate method? • Insert w and the new value dw into the priority queue every time w’s distance changes. Thus, there may be more than one representative for each vertex. • How to make sure that alternate method will work correctly?
Correctness • Does Dijkstra’s algorithm always give the correct solution? • Yes, as long as no edge has a negative cost.
Animation and Proof • See this site for animation and proof: http://www.cs.auckland.ac.nz/software/AlgAnim/dijkstra.html • Proof is by contradiction. • Let us explain how it works.
Variants • Dijkstra’s algorithm solves which shortest path problem? • One source to all other nodes. • What are some possible variants? • Homework problem: one destination from all other nodes. • Another variant: from one given node to another. • Can you come up with an example that has multiple nodes but needs just one step? • Can you come up with an example that requires us to go through all the other nodes?
Negative Edge Costs • If a graph has negative edge costs, does Dijkstra’s algorithm work? • If not, can you show with an example? • Proposed scheme: add a constant Δ to each edge cost, to remove all negative edges. • Does this procedure work? • If not, can you show with an example?
Negative Edge Costs • What must we do to develop an algorithm that works? • Allow our algorithm to change its mind. • Forget about the concept of known vertices. • Start by placing s on a queue. • At each stage, dequeue a vertex v. • Find all vertices w adjacent to v such that dw > dv + cvw. • Update dw and pw, and place w on the queue if it is not already there. • Do you remember pw? • What is the running time? • O(|E|.|V|).
Acyclic Graph • If the graph is acyclic, we can improve Dijkstra’s algorithm by changing the order in which the vertices are declared known. • How? • Select vertices in topological order. • The algorithm can be performed in one pass, since the selections and updates can take place at the same time as the topological sort. • The selection works because when a vertex is selected, its distance can no longer be lowered. Why not? • There is no need for a priority queue. Why not? Consider example. • The running time is O(|E|+|V|).
Critical Path Analysis • Each node represents an activity that must be performed, along with the time it takes to complete the activity. • Called an activity-node graph. • Possible application: construction projects. • Some important questions: • What is the earliest completion time for the project? • Which activities can be delayed, and for how long, without affecting the minimum completion time?
Activity-node and Event-node Graphs • What is the role of the dummy nodes?
Earliest Completion Times • How to calculate the earliest completion time of the project? • Find the length of the longest path from the first event to the last event. • What if there are positive-cost cycles? • Rules: EC1 = 0, ECw = max (ECv + cvw ). • Can you suggest an algorithm? • Graph is acyclic. Can you suggest an improvement? • Topological ordering of the nodes.
Latest Completion Times • These are earliest completion times: • Now, we want the latest times that each event can finish without affecting the final completion time. • How to calculate the latest completion time of the events? • EC Rules: EC1 = 0, ECw = max (ECv + cvw ). • What should be the rules for latest completion times? • LC Rules: LCn = ECn, LCv = max (LCw – cvw ). • In which order should we compute latest completion times?
Slack Times • Slack time for each node is the amount of time that completion at a node can be delayed without delaying the overall completion. • Formula: Slackvw = LCw – ECv – cvw. • Some activities have zero slack. These are crtitical activities, which must finish on time. • There is at least one path consisting entirely of zero-slack edges. Such a path is a critical path.