Lecture20: Graph IV Bohyung Han CSE, POSTECH CSED233: Data Structures (2014F)
CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Weighted Graph Properties Each edge has an associated numerical value, 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 2 ORD PVD MIA DFW SFO LAX LGA HNL
CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Shortest Paths What is the shortest path? A path of minimum total weight between two vertices Length of a path is the sum of the weights of its edges. Applications Internet packet routing Flight reservations Driving directions 3 ORD PVD MIA DFW SFO LAX LGA HNL
CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Shortest Paths Properties A subpath of a shortest path is itself a shortest path There is a tree of shortest paths from a start vertex to all the other vertices Example: Tree of shortest paths from Providence 4 ORD PVD MIA DFW SFO LAX LGA HNL
CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Dijkstra’s Algorithm Problem definition Find the shortest path from a starting vertex to all other vertices. Assumptions: The graph is connected. The edges are undirected. The edge weights are nonnegative. Methodology We grow a “cloud” of vertices, beginning with a starting vertex and eventually covering all the vertices. Solve for vertices close to starting vertex: Neighbors are easy to determine. Add an edge one by one Find the path to each vertex one by one Iteratively expand the set of nodes where the shortest path is known. 5
CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Dijkstra’s Algorithm 6
CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Edge Relaxation 7 10
CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 C B A E D F Example 8 CB A E D F C B A E D F C B A E D F
CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Example 9 CB A E D F CB A E D F
CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Dijkstra’s Algorithm Heap-based priority queue Stores the vertices outside the cloud Key: distance Value: vertex replaceKey(l,k) : changes the key of entry l We store two labels with each vertex: Distance Entry in priority queue 10 Algorithm DijkstraDistances(G, s) Q new heap-based priority queue for all v G.vertices() if v = s v.setDistance(0) else v.setDistance( ) l Q.insert(v.getDistance(), v) v.setEntry(l) while Q.empty() l Q.removeMin() u l.getValue() for all e u.incidentEdges() // relax e z e.opposite(u) r u.getDistance() + e.weight() if r < z.getDistance() z.setDistance(r) Q.replaceKey(z.getEntry(), r)
CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Analysis of Dijkstra’s Algorithm 11
CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Shortest Paths Tree Extension of Dijkstra’s algorithm Return a tree of shortest paths from the start vertex to all other vertices Store a third label: parent edge in the shortest path tree In the edge relaxation step, we update the parent label. 12 Algorithm DijkstraShortestPathsTree(G, s) … for all v G.vertices() … v.setParent( ) … for all e u.incidentEdges() // relax edge e z e.opposite(u) r u.getDistance() + e.weight() if r < z.getDistance() z.setDistance(r) z.setParent(e) Q.replaceKey(z.getEntry(),r)
CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Why Dijkstra’s Algorithm Work 13 CB A E D F
CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Why It Doesn’t Work for Negative-Weight Edges Greedy algorithm: It adds vertices by increasing distance. 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. 14 CB A E D F C’s true distance is 1, but it is already in the cloud with d(C)=5!
CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Bellman-Ford Algorithm 15 Algorithm BellmanFord(G, s) for all v G.vertices() if v = s v.setDistance(0) else v.setDistance( ) for i 1 to n - 1 do for each e G.edges() // relax edge e u e.origin() z e.opposite(u) r u.getDistance() + e.weight() if r < z.getDistance() z.setDistance(r)
CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Bellman-Ford Example 0 4
CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Bellman-Ford Example
CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Directed Acyclic Graph (DAG) 18
CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 DAG Applications 19 Procedure of a particular task Hasse diagram
CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Topological Sort 20 Algorithm TopologicalSort(G) H G // Temporary copy of G n G.numVertices() while H is not empty do Let v be a vertex with no outgoing edges Label v n n n - 1 Remove v from H
CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 An Example of Topological Sort 21
CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 An Example of Topological Sort 22 9
CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 An Example of Topological Sort
CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 An Example of Topological Sort
CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 An Example of Topological Sort
CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 An Example of Topological Sort
CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 An Example of Topological Sort
CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 An Example of Topological Sort
CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 An Example of Topological Sort
CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 An Example of Topological Sort
CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 DAG-based Shortest Path Algorithm 31 Algorithm DagDistances(G, s) for all v G.vertices() if v = s v.setDistance(0) else v.setDistance( ) // Perform a topological sort of the vertices for u 1 to n do // in topological order for each e u.outEdges() // relax edge e z e.opposite(u) r u.getDistance() + e.weight() if r < z.getDistance() z.setDistance(r) Why is this faster?
CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 DAG Example 32 0 4
33