CSC 2300 Data Structures & Algorithms April 13, 2007 Chapter 9. Graph Algorithms.

Slides:



Advertisements
Similar presentations
Single Source Shortest Paths
Advertisements

CS 206 Introduction to Computer Science II 04 / 01 / 2009 Instructor: Michael Eckmann.
§3 Shortest Path Algorithms Given a digraph G = ( V, E ), and a cost function c( e ) for e  E( G ). The length of a path P from source to destination.
Topological Sort Topological sort is the list of vertices in the reverse order of their finishing times (post-order) of the depth-first search. Topological.
§2 Topological Sort 〖 Example 〗 Courses needed for a computer science degree at a hypothetical university How shall we convert this list into a graph?
CSE 101- Winter ‘15 Discussion Section January 26th 2015.
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,
The Shortest Path Problem. Shortest-Path Algorithms Find the “shortest” path from point A to point B “Shortest” in time, distance, cost, … Numerous.
CSCI 3160 Design and Analysis of Algorithms Tutorial 2 Chengyu Lin.
1 Shortest Path Algorithms Given a graph G = (V, E) and a distinguished vertex s, find the shortest weighted path from s to every other vertex in G. weighted.
Tirgul 12 Algorithm for Single-Source-Shortest-Paths (s-s-s-p) Problem Application of s-s-s-p for Solving a System of Difference Constraints.
CSC 2300 Data Structures & Algorithms April 17, 2007 Chapter 9. Graph Algorithms.
CS 311 Graph Algorithms. Definitions A Graph G = (V, E) where V is a set of vertices and E is a set of edges, An edge is a pair (u,v) where u,v  V. If.
Minimum Spanning Trees Definition Algorithms –Prim –Kruskal Proofs of correctness.
Chapter 9: Graphs Summary Mark Allen Weiss: Data Structures and Algorithm Analysis in Java Lydia Sinapova, Simpson College.
Shortest Paths Definitions Single Source Algorithms –Bellman Ford –DAG shortest path algorithm –Dijkstra All Pairs Algorithms –Using Single Source Algorithms.
1 8-ShortestPaths Shortest Paths in a Graph Fundamental Algorithms.
Graph Algorithms: Shortest Path We are given a weighted, directed graph G = (V, E), with weight function w: E R mapping.
Shortest Paths Definitions Single Source Algorithms
Shortest Path Algorithm By Weston Vu CS 146. What is Shortest Paths? Shortest Paths is a part of the graph algorithm. It is used to calculate the shortest.
DAST 2005 Tirgul 12 (and more) sample questions. DAST 2005 Q.We’ve seen that solving the shortest paths problem requires O(VE) time using the Belman-Ford.
Tirgul 13. Unweighted Graphs Wishful Thinking – you decide to go to work on your sun-tan in ‘ Hatzuk ’ beach in Tel-Aviv. Therefore, you take your swimming.
More Graph Algorithms Weiss ch Exercise: MST idea from yesterday Alternative minimum spanning tree algorithm idea Idea: Look at smallest edge not.
Dijkstra’s Algorithm Slide Courtesy: Uwash, UT 1.
CSC 2300 Data Structures & Algorithms April 3, 2007 Chapter 9. Graph Algorithms.
CSC 2300 Data Structures & Algorithms March 30, 2007 Chapter 9. Graph Algorithms.
Dijkstra's algorithm.
Graphs – Shortest Path (Weighted Graph) ORD DFW SFO LAX
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures Graph Algorithms: Minimum.
CS 146: Data Structures and Algorithms July 21 Class Meeting
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures Graph Algorithms Shortest-Path.
1 WEEK 9-10 Graphs II Unweighted Shortest Paths Dijkstra’s Algorithm Graphs with negative costs Acyclic Graphs Izmir University of Economics.
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
Dijkstras Algorithm Named after its discoverer, Dutch computer scientist Edsger Dijkstra, is an algorithm that solves the single-source shortest path problem.
Graphs Data Structures and Algorithms A. G. Malamos Reference Algorithms, 2006, S. Dasgupta, C. H. Papadimitriou, and U. V. Vazirani Introduction to Algorithms,Third.
Introduction to Algorithms Jiafen Liu Sept
The single-source shortest path problem (SSSP) input: a graph G = (V, E) with edge weights, and a specific source node s. goal: find a minimum weight (shortest)
Lecture 13 Algorithm Analysis
DATA STRUCTURES AND ALGORITHMS Lecture Notes 10 Prepared by İnanç TAHRALI.
Graphs and Paths Data Structures & Problem Solving Using JAVA Second Edition Mark Allen Weiss Chapter 14 © 2002 Addison Wesley.
Spanning Trees Dijkstra (Unit 10) SOL: DM.2 Classwork worksheet Homework (day 70) Worksheet Quiz next block.
1 GRAPHS – Definitions A graph G = (V, E) consists of –a set of vertices, V, and –a set of edges, E, where each edge is a pair (v,w) s.t. v,w  V Vertices.
CSC317 1 At the same time: Breadth-first search tree: If node v is discovered after u then edge uv is added to the tree. We say that u is a predecessor.
Graph Algorithm Hongfei Yan School of EECS, Peking University 5/21/2014.
CSE 326: Data Structures Lecture #23 Dijkstra and Kruskal (sittin’ in a graph) Steve Wolfman Winter Quarter 2000.
CSE 373: Data Structures and Algorithms Lecture 21: Graphs V 1.
Chapter 9 : Graphs Part II (Minimum Spanning Trees)
CSC317 Shortest path algorithms
Shortest Path Problems
More Graph Algorithms.
CSE373: Data Structures & Algorithms Lecture 18: Dijkstra’s Algorithm
Lecture 13 Algorithm Analysis
Lecture 13 Algorithm Analysis
Shortest Path Problems
Minimum Spanning Tree Algorithms
Lecture 13 Algorithm Analysis
Activity Networks
Chapter 15 Graphs © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
Slide Courtesy: Uwash, UT
Lecture 13 Algorithm Analysis
Fundamental Data Structures and Algorithms
CSE332: Data Abstractions Lecture 17: Shortest Paths
CE 221 Data Structures and Algorithms
CSE 373 Data Structures and Algorithms
Slide Courtesy: Uwash, UT
CSC 380: Design and Analysis of Algorithms
Graph Algorithms: Shortest Path
CSE 417: Algorithms and Computational Complexity
CE 221 Data Structures and Algorithms
Chapter 9: Graphs Shortest Paths
Presentation transcript:

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

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 d v, 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 d w 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 d v, 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 d w 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: 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 d w > d v + c vw. Update d w and p w, and place w on the queue if it is not already there. Do you remember p w ? What is the running time? O(|E|.|V|).

Pseudocode

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: EC 1 = 0, EC w = max (EC v + c vw ). 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: EC 1 = 0, EC w = max (EC v + c vw ). What should be the rules for latest completion times? LC Rules: LC n = EC n, LC v = max (LC w – c vw ). 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: Slack vw = LC w – EC v – c vw. 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.