Www.monash.edu.au 1 prepared from lecture material © 2004 Goodrich & Tamassia COMMONWEALTH OF AUSTRALIA Copyright Regulations 1969 WARNING This material.

Slides:



Advertisements
Similar presentations
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture10.
Advertisements

Spring 2007Shortest Paths1 Minimum Spanning Trees JFK BOS MIA ORD LAX DFW SFO BWI PVD
Shortest Paths1 C B A E D F
CSC 213 Lecture 23: Shortest Path Algorithms. Weighted Graphs Each edge in weighted graph has numerical weight Weights can be distances, building costs,
1 8-ShortestPaths Shortest Paths in a Graph Fundamental Algorithms.
1 Graphs ORD DFW SFO LAX Many slides taken from Goodrich, Tamassia 2004.
Greedy Algorithms Reading Material: Chapter 8 (Except Section 8.5)
Minimum Spanning Trees1 JFK BOS MIA ORD LAX DFW SFO BWI PVD
© 2004 Goodrich, Tamassia Shortest Paths1 Shortest Paths (§ 13.6) Given a weighted graph and two vertices u and v, we want to find a path of minimum total.
Greedy Algorithms Like dynamic programming algorithms, greedy algorithms are usually designed to solve optimization problems Unlike dynamic programming.
Minimum Spanning Trees1 JFK BOS MIA ORD LAX DFW SFO BWI PVD
CSC311: Data Structures 1 Chapter 13: Graphs II Objectives: Graph ADT: Operations Graph Implementation: Data structures Graph Traversals: DFS and BFS Directed.
Shortest Paths1 C B A E D F
© 2004 Goodrich, Tamassia Shortest Paths1 C B A E D F
1 prepared from lecture material © 2004 Goodrich & Tamassia COMMONWEALTH OF AUSTRALIA Copyright Regulations 1969 WARNING This material.
Lecture20: Graph IV Bohyung Han CSE, POSTECH CSED233: Data Structures (2014F)
Graphs – Shortest Path (Weighted Graph) ORD DFW SFO LAX
Weighted Graphs In a weighted graph, each edge has an associated numerical value, called the weight of the edge Edge weights may represent, distances,
Minimum Spanning Trees
Jim Anderson Comp 122, Fall 2003 Single-source SPs - 1 Chapter 24: Single-Source Shortest Paths Given: A single source vertex in a weighted, directed graph.
© 2010 Goodrich, Tamassia Shortest Paths1 C B A E D F
1 Shortest Path Problem Topic 11 ITS033 – Programming & Algorithms C B A E D F Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program,
Shortest Paths C B A E D F
Graphs Part 2. Shortest Paths C B A E D F
SPANNING TREES Lecture 21 CS2110 – Spring
CSC 213 – Large Scale Programming. Today’s Goals  Discuss what is meant by weighted graphs  Where weights placed within Graph  How to use Graph ’s.
Graphs. Data Structure for Graphs. Graph Traversals. Directed Graphs. Shortest Paths. 2 CPSC 3200 University of Tennessee at Chattanooga – Summer 2013.
1 prepared from lecture material © 2004 Goodrich & Tamassia COMMONWEALTH OF AUSTRALIA Copyright Regulations 1969 WARNING This material.
Shortest Paths 1 Chapter 7 Shortest Paths C B A E D F
Lecture 16. Shortest Path Algorithms
CSE 2331 / 5331 Topic 12: Shortest Path Basics Dijkstra Algorithm Relaxation Bellman-Ford Alg.
CSC 213 – Large Scale Programming. Today’s Goals  Discuss what is meant by weighted graphs  Where weights placed within Graph  How to use Graph ’s.
Paths in a Graph : A Brief Tutorial Krishna.V.Palem Kenneth and Audrey Kennedy Professor of Computing Department of Computer Science, Rice University 1.
© 2010 Goodrich, Tamassia Minimum Spanning Trees1 JFK BOS MIA ORD LAX DFW SFO BWI PVD
1 Prim’s algorithm. 2 Minimum Spanning Tree Given a weighted undirected graph G, find a tree T that spans all the vertices of G and minimizes the sum.
1 Weighted Graphs CSC401 – Analysis of Algorithms Lecture Notes 16 Weighted Graphs Objectives: Introduce weighted graphs Present shortest path problems,
Data Structures and Algorithms1 Data Structures and algorithms (IS ZC361) Weighted Graphs – Shortest path algorithms – MST S.P.Vimal BITS-Pilani
Graphs Quebec Toronto Montreal Ottawa 449 km 255 km 200 km 545 km Winnipeg 2075 km 2048 km New York 596 km 790 km 709 km.
CHAPTER 13 GRAPH ALGORITHMS ACKNOWLEDGEMENT: THESE SLIDES ARE ADAPTED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++, GOODRICH, TAMASSIA.
© 2010 Goodrich, Tamassia Shortest Paths1 C B A E D F
© 2010 Goodrich, Tamassia Minimum Spanning Trees1 JFK BOS MIA ORD LAX DFW SFO BWI PVD
1 COMP9024: Data Structures and Algorithms Week Twelve: Graphs (II) Hui Wu Session 1, 2014
Graphs ORD SFO LAX DFW Graphs 1 Graphs Graphs
Graphs 10/24/2017 6:47 AM Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and.
Shortest Paths C B A E D F Shortest Paths
Graphs Part 2.
14 Graph Algorithms Hongfei Yan June 8, 2016.
COMP9024: Data Structures and Algorithms
Shortest Paths C B A E D F Shortest Paths
Minimum Spanning Trees
Shortest Path 6/18/2018 4:22 PM Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia,
Shortest Paths C B A E D F Shortest Paths 1
Minimum Spanning Trees
Shortest Paths C B A E D F Shortest Paths
Chapter 14 Graph Algorithms
Minimum Spanning Tree 11/3/ :04 AM Minimum Spanning Tree
Shortest Paths C B A E D F Shortest Paths
Shortest Paths C B A E D F
Graphs.
Minimum Spanning Trees
Shortest Paths C B A E D F Shortest Paths
Shortest Paths C B A E D F Shortest Paths
Minimum Spanning Trees
Chapter 13 Graph Algorithms
Graphs Part 2.
Graph Algorithms shortest paths, minimum spanning trees, etc.
Copyright © Aiman Hanna All rights reserved
Shortest Paths.
Minimum Spanning Trees
Weighted Graphs C B A E D F Sequences
Presentation transcript:

1 prepared from lecture material © 2004 Goodrich & Tamassia COMMONWEALTH OF AUSTRALIA Copyright Regulations 1969 WARNING This material has been reproduced and communicated to you by or on behalf of Monash University pursuant to Part VB of the Copyright Act 1968 (the Act). The material in this communication may be subject to copyright under the Act. Any further reproduction or communication of this material by you may be the subject of copyright protection under the Act. Do not remove this notice.

FIT2004 Algorithms & Data Structures L14: Path Problems Prepared by: Bernd Meyer from lecture materials © 2004 Goodrich & Tamassia March 2007

3 prepared from lecture material © 2004 Goodrich & Tamassia Weighted Graphs In a weighted graph, each edge has an associated numerical value, called the weight of the edge Edge weights may represent, distances, costs, etc. Example: –In a flight route graph, the weight of an edge represents the distance in miles between the endpoint airports ORD PVD MIA DFW SFO LAX LGA HNL

4 prepared from lecture material © 2004 Goodrich & Tamassia Shortest Paths (Goodrich & Tamassia § 12.6) Given a weighted graph and two vertices u and v, we want to find a path of minimum total weight between u and v. –Length of a path is the sum of the weights of its edges. Example: –Shortest path between Providence and Honolulu Applications –Internet packet routing –Flight reservations –Driving directions ORD PVD MIA DFW SFO LAX LGA HNL

5 prepared from lecture material © 2004 Goodrich & Tamassia Shortest Path Properties Property 1: A subpath of a shortest path is itself a shortest path Property 2: There is a tree of shortest paths from a start vertex to all the other vertices Example: Tree of shortest paths from Providence ORD PVD MIA DFW SFO LAX LGA HNL

6 prepared from lecture material © 2004 Goodrich & Tamassia Dijkstra’s Algorithm The distance of a vertex v from a vertex s is the length of a shortest path between s and v Dijkstra’s algorithm computes the distances of all the vertices from a given start vertex s: single-source all paths Assumptions: –the graph is connected –the edges are undirected –the edge weights are nonnegative We grow a “cloud” of vertices, beginning with s and eventually covering all the vertices We store with each vertex v a label d(v) representing the distance of v from s in the subgraph consisting of the cloud and its adjacent vertices At each step –We add to the cloud the vertex u outside the cloud with the smallest distance label, d(u) –We update the labels of the vertices adjacent to u

7 prepared from lecture material © 2004 Goodrich & Tamassia Edge Relaxation Consider an edge e  (u,z) such that –u is the vertex most recently added to the cloud –z is not in the cloud The relaxation of edge e updates distance d(z) as follows: d(z)  min{d(z),d(u)  weight(e)} d(z)  75 d(u)  z s u d(z)  60 d(u)  z s u e e

8 prepared from lecture material © 2004 Goodrich & Tamassia Example CB A E D F  C B A E D F C B A E D F C B A E D F

9 prepared from lecture material © 2004 Goodrich & Tamassia Example (cont.) CB A E D F CB A E D F

10 prepared from lecture material © 2004 Goodrich & Tamassia Dijkstra’s Algorithm A priority queue stores the vertices outside the cloud –Element: vertex –priority is given by elements distance –replaceKey(l,k) changes the key of an item updating the queue Note that d(.) is only the “best distance so far” and only in the end converges to the true distance Dijkstra is a greedy algorithm Algorithm DijkstraDistances(G, s) Q  new heap-based priority queue for all v  vertices(G) if v  s setDistance(v, 0) else setDistance(v,  ) Q = insert(Q, v) while  Q.isEmpty() u  Q.removeMin() for all e  incidentEdges(G,u) { relax edge e } z  opposite(G,u,e) r  getDistance(u)  weight(e) if r  getDistance(z) setDistance(z,r) replaceKey(Q,z,r)

11 prepared from lecture material © 2004 Goodrich & Tamassia Why Dijkstra’s Algorithm Works Dijkstra’s algorithm is based on the greedy method. It adds vertices by increasing distance. CB A E D F Suppose it didn’t find all shortest distances. Let F be the first wrong vertex the algorithm processed. When the previous node, D, on the true shortest path was considered, its distance was correct. But the edge (D,F) was relaxed at that time! Thus, so long as d(F)>d(D), F’s distance cannot be wrong. That is, there is no wrong vertex.

12 prepared from lecture material © 2004 Goodrich & Tamassia Analysis of Dijkstra’s Algorithm Graph operations –Method incidentEdges is called once for each vertex Label operations –We set/get the distance of vertex z O(deg(z)) times –Setting/getting a label takes O(1) time Priority queue operations –Each vertex is inserted once into and removed once from the priority queue, where each insertion or removal takes O(log n) time –The key of a vertex in the priority queue is modified at most deg(w) times, where each key change takes O(log n) time Dijkstra’s algorithm runs in O((n  m) log n) time provided the graph is represented by the adjacency list structure –Recall that  v deg(v)  2m The running time can also be expressed as O(m log n) since the graph is connected

13 prepared from lecture material © 2004 Goodrich & Tamassia Shortest Paths Tree It is simple to modify Dijkstra’s algorithm to return a tree of shortest paths from the start vertex to all other vertices We store with each vertex a third label: –parent edge in the shortest path tree In the edge relaxation step, we update the parent label Algorithm DijkstraShortestPathsTree(G, s) … for all v  vertices(G) … setParent(v,  ) … for all e  incidentEdges(G,u) { relax edge e } z  opposite(G,u,e) r  getDistance(u)  weight(e) if r  getDistance(z) setDistance(z,r) setParent(z,e) replaceKey(Q, z,r)

14 prepared from lecture material © 2004 Goodrich & Tamassia It Doesn’t Work for Negative-Weight Edges! CB A E D F Dijkstra’s algorithm is based on the greedy method. It adds vertices by increasing distance. C’s true distance is 1 (via A-D-F), but it is already in the cloud with d(C)=5 and will not be reconsidered! If a node with a negative incident edge were to be added late to the cloud, it could mess up distances for vertices already in the cloud.

15 prepared from lecture material © 2004 Goodrich & Tamassia Bellman-Ford Algorithm Works even with negative- weight edges Must assume directed edges (for otherwise we would have negative- weight cycles) Iteration i finds all shortest paths that use i edges. Running time: O(nm). Algorithm BellmanFord(G, s) for all v  vertices(G) if v  s setDistance(v, 0) else setDistance(v,  ) for i  1 to n-1 do for each e  edges(G) { relax edge e } u  origin(G,e) z  opposite(G,u,e) r  getDistance(u)  weight(e) if r  getDistance(z) setDistance(z,r)

16 prepared from lecture material © 2004 Goodrich & Tamassia  -2 Bellman-Ford Example  0     0     

17 prepared from lecture material © 2004 Goodrich & Tamassia Bellman-Ford vs Dijkstra Dijkstra: O(m log n) Bellman Ford: O(mn) Dijkstra is significantly faster (close to a full factor n), so if you know that there are no negative edges, use Dijkstra.

18 prepared from lecture material © 2004 Goodrich & Tamassia DAG-based Algorithm You can be much faster if you have a DAG. Perfrom a topological sort first, then calculate distances in level order. Works only with DAGs Works with negative- weight edges Doesn’t use any fancy data structures Is much faster than Dijkstra’s algorithm Running time: O(n+m). Algorithm DagDistances(G, s) for all v  vertices(G) if v  s setDistance(v, 0) else setDistance(v,  ) Perform a topological sort of the vertices for u  1 to n do {in topological order} for each e  outEdges(G,u) { relax edge e } z  opposite(G,u,e) r  getDistance(u)  weight(e) if r  getDistance(z) setDistance(z,r)

19 prepared from lecture material © 2004 Goodrich & Tamassia  -2 DAG Example  0     0     

20 prepared from lecture material © 2004 Goodrich & Tamassia Bellman-Ford is Dynamic Programming In the way we have developed it, it is easy to overlook that the Bellman Ford algorithm is indeed a dynamic programming approach. We are looking for the optimal (shortest) distances of all nodes from node t. The optimal path must have at most (n-1) edges. This is because there are no negative cycles. Let M[i,v] be the cost of the best path from s to v using at most i edges, let c(v,w) be the edge cost of edge e=(v,w). We can observe either the optimal path only needs (i-1) edges or the optimal path the last edge is e=(v,w) and the optimal path from s to v uses only (i-1) edges

21 prepared from lecture material © 2004 Goodrich & Tamassia Bellman-Ford is Dynamic Programming (cont) We can now clearly see the DP nature of Bellman-Ford. If we use the table-implementation that we are used to: Alg Bellman-Ford for i = 0 to (n-1) M[i,s]=0; for all vertices v other than s M[i,v]=+infinity; for i = 1 to (n-1) // pathlength (maximal number of nodes-1) for all edges e=(v,w)

22 prepared from lecture material © 2004 Goodrich & Tamassia All Pair Shortest Path: Floyd vs Dijkstra We can use a related idea to compute the shortest paths between all pairs of nodes (u,v). This is Floyd’s algorithm. Let D[u,v] be the distance between u and v, and  if there is no edge (u,v). Alg Floyd for all edges e=(u,v) { D[u,v]=c(u,v); } for all vertices v { D[v,v]=0; } for k = 1 to n // intermediate node for i = 1 to n // source node for j = 1 to n // destination node D[i,j]=min(D[i,j],D[i,k]+D[k,j]); The runtime of Floyd is obviously O(n 3 ). We could instead use Dijkstra for each of the n vertices in turn. Dijkstra with Adjacency matrix: O(n 2 ) (how?) - so both are cubic Floyd is simpler --- it has a smaller factor in n 3. If you need a simple (adjacency matrix) implementation, you can use Floyd. Dijkstra with Adjacency list: O(m log n) For large sparse graphs (m << n 2 ) repeated Dijkstra with adjacency lists is superior.

23 prepared from lecture material © 2004 Goodrich & Tamassia Biconnectivity (optional) Reliability is an important problem in networks. If a node is destroyed, can all other nodes still communicate? This relates to biconnectivity: A graph is biconnected if there is no vertex that disconnects the graph into two components. If the graph is not biconnected, a vertex that can disconnect it is called an articulation point. Theorem: Let G be a connected graph with depth-first spanning tree T. The vertex v is an articulation point of G if and only if 1.v is the root of T and v has at least two children 2.v is not the root of T, and for some child w of v in T there is no back edge from and descendant of w to any proper ancestor of v. This seems reasonably obvious (proof left as exercise). How can we use this theorem to design an algorithm that check biconnectivity?

24 prepared from lecture material © 2004 Goodrich & Tamassia Biconnectivity (optional) 1.Perform a DFS on G starting at any vertex v to compute num(v), the preorder number of v. 2.For each vertex w compute low(w), the smallest number num(u) of of any vertex u that is reachable from w (including itself) by following zero or more edges from w to one of its descendants x and then following a back-edge from x to u. low(w) can be computed by a postorder traversal starting from w. When we visit w we have already computed low(z) for each of its children z. low(w) is the minimum of 1.num(w) 2.low(z) for any child z 3.num(x) for any vertex x where a back edge (w, x) exists. 3.Find articulation points: 1.The root of the DFS spanning tree is an articulation point if it has more than two children Assume the root r has two subtrees L(eft) and R(ight). If r is not an AP then there is an edge e bridging from L to R. In this case e would have been traversed in the DFS to visit all nodes in L before returning to the root. So the root would not have the subtree R. Contradiction. 2.Any other vertex v is an AP iff low(w)>=num(v) for some child w of v. low provides us with the first node reachable from D (in preorder numbering) via a back edge. If low(v)>=num(v) then there is no back edge to a predecessor of v, so removing v disconnects the graph

25 prepared from lecture material © 2004 Goodrich & Tamassia Biconnectivity (optional) Right: DFS starting from A, each vertex labeled with num/low. APs are C, D

26 prepared from lecture material © 2004 Goodrich & Tamassia Harder Problems: TSP Not all path problems are as easy as the ones we have seen. Some do not permit any fast solutions. The most famous instance is the travelling salesman problem (TSP). TSP: Find a path through the graph that visits each vertex exactly once (once and only once) and has a total length of less than some constant k. This problem is known to be NP -complete. You will learn about NP -complete problems in FIT2014/FIT3014. For now it has to suffice to say that they are really hard… Given a solution you can check in polynomial time that it is correct, but there is no way to solve such problems in polynomial time from scratch: basically there is no way around having to check every possible path in the worst case. (This is oversimplified… nobody has proven this (yet), but there is an enormous amount of evidence that it is true and practically every computer scientist/mathematician believes it.)