Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture20: Graph IV Bohyung Han CSE, POSTECH CSED233: Data Structures (2014F)

Similar presentations


Presentation on theme: "Lecture20: Graph IV Bohyung Han CSE, POSTECH CSED233: Data Structures (2014F)"— Presentation transcript:

1 Lecture20: Graph IV Bohyung Han CSE, POSTECH bhhan@postech.ac.kr CSED233: Data Structures (2014F)

2 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 849 802 1387 1743 1843 1099 1120 1233 337 2555 142

3 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 849 802 1387 1743 1843 1099 1120 1233 337 2555 142

4 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 849 802 1387 1743 1843 1099 1120 1233 337 2555 142

5 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

6 CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Dijkstra’s Algorithm 6

7 CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Edge Relaxation 7 10

8 CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 C B A E D F 4 8 71 25 2 39 Example 8 CB A E D F 0 428 4 8 71 25 2 39 0 328 511 C B A E D F 0 328 58 4 8 71 25 2 39 C B A E D F 0 327 58 4 8 71 25 2 39

9 CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Example 9 CB A E D F 0 327 58 4 8 71 25 2 39 CB A E D F 0 327 58 4 8 71 25 2 39

10 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)

11 CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Analysis of Dijkstra’s Algorithm 11

12 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)

13 CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Why Dijkstra’s Algorithm Work 13 CB A E D F 0 327 58 4 8 71 25 2 39

14 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 0 457 59 4 8 71 25 6 0-8 C’s true distance is 1, but it is already in the cloud with d(C)=5!

15 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)

16 CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Bellman-Ford Example 16 8 -2  0    4 8 71 5 39 0  4  4 8 71 5 39 8 0 4  4 8 71 5 39 6 8 0 4 9 4 8 71 5 39 6

17 CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Bellman-Ford Example 17 -25 0 1 4 4 8 71 -25 39 5 0 9 4 8 71 -25 39 1

18 CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Directed Acyclic Graph (DAG) 18

19 CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 DAG Applications 19 Procedure of a particular task Hasse diagram

20 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

21 CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 An Example of Topological Sort 21

22 CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 An Example of Topological Sort 22 9

23 CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 An Example of Topological Sort 23 9 8

24 CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 An Example of Topological Sort 24 7 9 8

25 CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 An Example of Topological Sort 25 6 7 9 8

26 CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 An Example of Topological Sort 26 5 6 7 9 8

27 CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 An Example of Topological Sort 27 4 5 6 7 9 8

28 CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 An Example of Topological Sort 28 3 4 5 6 7 9 8

29 CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 An Example of Topological Sort 29 2 3 4 5 6 7 9 8

30 CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 An Example of Topological Sort 30 2 3 4 1 5 6 7 9 8

31 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?

32 CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 DAG Example 32  0    4 8 71 -55 -2 39 1 2 4 3 65 8 0  4  4 8 71 -55 39 1 2 4 3 65 -25 0 0 4 4 8 71 -55 -2 39 1 2 4 3 5 0 7 4 8 71 -55 3 9 1 1 2 4 3 65 65

33 33


Download ppt "Lecture20: Graph IV Bohyung Han CSE, POSTECH CSED233: Data Structures (2014F)"

Similar presentations


Ads by Google