1 / 51

Chapter 13 Graph Algorithms

Learn about Prim's and Kruskal's algorithms for finding a minimum spanning tree in a weighted graph, with examples and visualizations.

groverg
Download Presentation

Chapter 13 Graph Algorithms

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 1843 ORD Chapter 13Graph Algorithms SFO 802 1743 337 1233 LAX DFW Acknowledgement: These slides are adapted from slides provided with Data Structures and Algorithms in C++, Goodrich, Tamassia and Mount (Wiley 2004) and slides from Nancy M. Amato

  2. Minimum Spanning Trees 2704 BOS 867 849 PVD ORD 187 740 144 JFK 1846 621 1258 184 802 SFO BWI 1391 1464 337 1090 DFW 946 LAX 1235 1121 MIA 2342

  3. ReminderWeighted 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 849 PVD ORD 1843 142 SFO 802 LGA 1205 1743 337 1387 HNL 2555 1099 1233 LAX 1120 DFW MIA

  4. Minimum Spanning Tree ORD 10 • Spanning subgraph • Subgraph of a graph containing all the vertices of • Spanning tree • Spanning subgraph that is itself a (free) tree • Minimum spanning tree (MST) • Spanning tree of a weighted graph with minimum total edge weight • Applications • Communications networks • Transportation networks 1 PIT DEN 6 7 9 3 DCA STL 4 5 8 2 DFW ATL

  5. ExerciseMST • Show an MST of the following graph. 849 PVD ORD 1843 142 SFO 802 LGA 1205 1743 337 1387 HNL 2555 1099 1233 LAX 1120 DFW MIA

  6. 8 f 4 C 9 6 2 3 e 7 8 7 8 f 4 C 9 6 2 3 e 7 8 7 Cycle Property • Cycle Property: • Let be a minimum spanning tree of a weighted graph • Let be an edge of that is not in and let be the cycle formed by with • For every edge of , • Proof: • By contradiction • If we can get a spanning tree of smaller weight by replacing with Replacing f with e yieldsa better spanning tree

  7. Partition Property U V 7 f 4 9 5 2 • Partition Property: • Consider a partition of the vertices of into subsets and • Let be an edge of minimum weight across the partition • There is a minimum spanning tree of G containing edge • Proof: • Let be an MST of • If does not contain , consider the cycleformed by with and let be an edge of across the partition • By the cycle property, • Thus, • We obtain another MST by replacing with 8 3 8 e 7 Replacing f with e yieldsanother MST U V 7 f 4 9 5 2 8 3 8 e 7

  8. Prim-Jarnik’s Algorithm • We pick an arbitrary vertex and we grow the MST as a cloud of vertices, starting from • We store with each vertex a label the smallest weight of an edge connecting to a vertex in the cloud • At each step: • We add to the cloud the vertex outside the cloud with the smallest distance label • We update the labels of the vertices adjacent to

  9. Prim-Jarnik’s Algorithm • An adaptable priority queue stores the vertices outside the cloud • Key: distance, • Element: vertex • changes the key of an item • We store three labels with each vertex : • Distance • Parent edge in MST • Locator in priority queue Algorithm Input: A weighted connected graph Output: A minimum spanning tree of • Pick any vertex of • ; • for each vertex do • ; • Priority queue of vertices with as the key • whiledo • Add vertex and edge to • for eachdo • if • ; • return

  10. Example 7  7 D 7 D 2 2 B B 4 4 9  9  8 5 5 5 F F 2 2 C C 8 8 3 3 8 8 E E A A 7 7 7 7 0 0 7 7 7 D 2 7 D 2 B 4 B 4 9  5 4 9 5 5 F 2 5 F 2 C 8 C 8 3 3 8 8 E E A 7 7 A 7 0 7 0

  11. Example 7 7 D 2 B 4 4 9 5 5 F 2 C 8 3 8 E A 3 7 7 0 7 D 2 B 4 4 9 5 5 F 2 C 8 3 8 E A 3 7 0

  12. ExercisePrim’s MST algorithm • Show how Prim’s MST algorithm works on the following graph, assuming you start with SFO • Show how the MST evolves in each iteration (a separate figure for each iteration). 849 PVD ORD 1843 142 SFO 802 LGA 1205 1743 337 1387 HNL 2555 1099 1233 LAX 1120 DFW MIA

  13. Analysis • Graph operations • Method is called once for each vertex • Label operations • We set/get the distance, parent and locator labels of vertex times • Setting/getting a label takes time • Priority queue operations • Each vertex is inserted once into and removed once from the priority queue, where each insertion or removal takes time • The key of a vertex in the priority queue is modified at most times, where each key change takes time • Prim-Jarnik’s algorithm runs in time provided the graph is represented by the adjacency list structure • Recall that • The running time is since the graph is connected

  14. Kruskal’s Algorithms • A priority queue stores the edges outside the cloud • Key: weight • Element: edge • At the end of the algorithm • We are left with one cloud that encompasses the MST • A tree T which is our MST Algorithm • for each vertex do • Define a cluster • Initialize a priority queue of edges using the weights as keys • while has fewer than edges do • if then • Add to • Merge and • return

  15. Data Structure for Kruskal’s Algorithm • The algorithm maintains a forest of trees • An edge is accepted it if connects distinct trees • We need a data structure that maintains a partition, i.e., a collection of disjoint sets, with the operations: • : return the set storing u • : replace the sets storing u and v with their union

  16. Representation of a Partition • Each set is stored in a sequence • Each element has a reference back to the set • Operation takes time, and returns the set of which is a member. • In operation , we move the elements of the smaller set to the sequence of the larger set and update their references • The time for operation is , where and are the sizes of the sets storing and • Whenever an element is processed, it goes into a set of size at least double, hence each element is processed at most times

  17. Analysis • A partition-based version of Kruskal’s Algorithm performs cluster merges as unions and tests as finds. • Running time • There will be at most removals from the priority queue - • Each vertex can be merged at most times, as the clouds tend to “double” in size - • Total:

  18. Example 2704 BOS 867 849 PVD ORD 187 740 144 JFK 1846 621 1258 184 802 SFO BWI 1391 1464 337 1090 DFW 946 LAX 1235 1121 MIA 2342

  19. Example

  20. Example

  21. Example

  22. Example

  23. Example

  24. Example

  25. Example

  26. Example

  27. Example

  28. Example

  29. Example

  30. Example

  31. 2704 BOS 867 849 PVD ORD 187 JFK 1258 SFO BWI DFW LAX MIA Example 740 144 1846 621 184 802 1391 1464 337 1090 946 1235 1121 2342

  32. ExerciseKruskal’s MST algorithm • Show how Kruskal’s MST algorithm works on the following graph. • Show how the MST evolves in each iteration (a separate figure for each iteration). 849 PVD ORD 1843 142 SFO 802 LGA 1205 1743 337 1387 HNL 2555 1099 1233 LAX 1120 DFW MIA

  33. 0 A 4 8 2 8 2 3 7 1 B C D 3 9 5 8 2 5 E F Shortest Paths

  34. 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 849 PVD ORD 1843 142 SFO 802 LGA 1205 1743 337 1387 HNL 2555 1099 1233 LAX 1120 DFW MIA

  35. Shortest Path Problem • Given a weighted graph and two vertices and , we want to find a path of minimum total weight between and . • 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 849 PVD ORD 1843 142 SFO 802 LGA 1205 1743 337 1387 HNL 2555 1099 1233 LAX 1120 DFW MIA

  36. Shortest Path Problem • If there is no path from to , we denote the distance between them by • What if there is a negative-weight cycle in the graph? 849 PVD ORD 142 SFO -802 LGA 1205 -1743 1387 HNL 2555 1099 -1233 LAX 1120 DFW MIA

  37. 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 pathsfrom Providence 849 PVD ORD 1843 142 SFO 802 LGA 1205 1743 337 1387 HNL 2555 1099 1233 LAX 1120 DFW MIA

  38. Dijkstra’s Algorithm • The distance of a vertex from a vertex is the length of a shortest path between and • Dijkstra’s algorithm computes the distances of all the vertices from a given start vertex (single-source shortest paths) • Assumptions: • The graph is connected • The edges are undirected • The edge weights are nonnegative • Extremely similar to Prim-Jarnik’s MST Algorithm • We grow a “cloud” of vertices, beginning with and eventually covering all the vertices • We store with each vertex a label representing the distance of from in the subgraph consisting of the cloud and its adjacent vertices • The label is initialized to positive infinity • At each step • We add to the cloud the vertex outside the cloud with the smallest distance label, • We update the labels of the vertices adjacent to , in a process called edge relaxation

  39. Edge Relaxation D[u] = 50 • Consider an edge such that • is the vertex most recently added to the cloud • is not in the cloud • The relaxation of edge updates distance as follows: D[z] = 75 10 e u z s D[y] = 20 55 y D[u] = 50 D[z] = 60 10 e u z s D[y] = 20 55 y

  40. 0 A 4 8 2 8 2 3 7 1 B C D 3 9 5 8 2 5 E F • Pull in one of the vertices with red labels • The relaxation of edges updates the labels of LARGER font size Example 0 A 4 8 2 8 2 4 7 1 B C D 3 9   55 2 5 E F 0 0 A A 4 4 8 8 2 2 3 7 8 2 2 3 7 1 7 1 B C D B C D 3 9 3 9 5 11 8 5 2 5 2 5 E F E F

  41. Example 0 A 4 8 2 7 2 3 7 1 B C D 3 9 5 8 2 5 E F 0 A 4 8 2 7 2 3 7 1 B C D 3 9 5 8 2 5 E F

  42. ExerciseDijkstra’s algorithm • Show how Dijkstra’s algorithm works on the following graph, assuming you start with SFO, i.e., SFO. • Show how the labels are updated in each iteration (a separate figure for each iteration). 849 PVD ORD 1843 142 SFO 802 LGA 1205 1743 337 1387 HNL 2555 1099 1233 LAX 1120 DFW MIA

  43. Dijkstra’s Algorithm • A locator-based priority queue stores thevertices outside the cloud • Key: distance • Element: vertex • We store with each vertex: • distance label • locator in priority queue Algorithm Input: A simple undirected weighted graph with nonnegative edge weights and a source vertex Output: A label for each vertex of , such that is the length of the shorted path from to • ; for each vertex • Let priority queue contain all the vertices of using as the key • while do// iterations • //pull a new vertex in the cloud • // • for each edge do // iterations • //relax edge • if then • //

  44. Analysis • Graph operations • Method incidentEdges is called once for each vertex • Label operations • We set/get the distance and locator labels of vertex times • Setting/getting a label takes time • Priority queue operations • Each vertex is inserted once into and removed once from the priority queue, where each insertion or removal takes time • The key of a vertex in the priority queue is modified at most times, where each key change takes time • Dijkstra’s algorithm runs in time provided the graph is represented by the adjacency list structure • Recall that • The running time can also be expressed as since the graph is connected • The running time can be expressed as a function of ,

  45. Extension • Using the template method pattern, we can extend 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 • Parents are all initialized to null • In the edge relaxation step, we update the parent label as well

  46. Why Dijkstra’s Algorithm Works • Dijkstra’s algorithm is based on the greedy method. It adds vertices by increasing distance. • Proof by contradiction • Suppose it didn’t find all shortest distances. Let be the first wrong vertex the algorithm processed. • When the previous node, , on the true shortest path was considered, its distance was correct. • But the edge was relaxed at that time! • Thus, so long as , ’s distance cannot be wrong. That is, there is no wrong vertex. 0 s 4 8 2 7 2 3 7 1 B C D 3 9 8 5 2 5 E F

  47. Why It Doesn’t Work forNegative-Weight Edges 0 A 4 8 2 8 2 4 • Dijkstra’s algorithm is based on the greedy method. 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. 7 -3 B C D 3 9   2 5 C’s true distance is 1, but it is already in the cloud with ! E F 0 A 4 8 2 0 8 2 7 -3 B C D 3 9 5 11 2 5 E F

  48. Bellman-Ford Algorithm • Works even with negative-weight edges • Must assume directed edges (for otherwise we would have negative-weight cycles) • Iteration finds all shortest paths that use edges. • Running time: • Can be extended to detect a negative-weight cycle if it exists • How? Algorithm • Initialize and for all vertices • fordo • for each do • //relax edge • ; • ifthen

  49. Nodes are labeled with their values Bellman-Ford Example First round 0 0 4 4 8 8 -2 -2 7 1 7 1    8 -2 4 3 9 3 9 -2 5 -2 5     Second round Third round 0 0 4 4 8 8 -2 -2 7 1 7 1 5 -2 -1 5 -2 -1 3 9 3 9 -2 5 -2 5 1 9 1 4

  50. ExerciseBellman-Ford’s algorithm • Show how Bellman-Ford’s algorithm works on the following graph, assuming you start with the top node • Show how the labels are updated in each iteration (a separate figure for each iteration). 0 4 8 -2 7 1    3 9 -5 5  

More Related