1 / 22

Chapter 13: Graphs

Chapter 13: Graphs. Graph ADT. Topological Sort. Shortest Path. Maximum Flow. Minimum Spanning Tree. Depth-First Search. P And NP Problems. CS 240. 237. 4. 3. 6. 6. 5. Undirected, unweighted , unconnected, loopless graph (length of longest simple path: 2).

kamala
Télécharger la présentation

Chapter 13: Graphs

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. Chapter 13: Graphs Graph ADT Topological Sort Shortest Path Maximum Flow Minimum Spanning Tree Depth-First Search P And NP Problems CS 240 237

  2. 4 3 6 6 5 Undirected, unweighted, unconnected, loopless graph (length of longest simple path: 2) Directed, unweighted, acyclic, weakly connected, loopless graph (length of longest simple path: 6) Directed, unweighted, cyclic, strongly connected, loopless graph (length of longest simple cycle: 7) 2 2 3 3 4 5 Undirected, unweighted, connected graph, with loops (length of longest simple path: 4) Directed, weighted, cyclic, weakly connected, loopless graph (weight of longest simple cycle: 27) Graph Definition A graph G = (V, E) consists of a set of vertices, V, and a set of edges, E, each of which is a pair of vertices. If the edges are ordered pairs of vertices, then the graph is directed. Graphs are relatively unstructured, but they provide a platform for solving some of the most sophisticated problems in computer science. CS 240 238

  3. C C C 4 3 B B B A A A 6 6 5 E E E D D D 2 2 3 F F F 3 H H H 4 5 G G G A A A B B B C C C D D D E E E F F F G G G H H H A A A  1 0 1 1 3 0 0  0 6 1 0 0  0 0  1 0   0 0 B B B 1  0 0 0  0 1 4  1 1 0 6 0  0 0 0 0   0 0 C C C 0 0  1 1  0 0   0 0  0 0 0  0 0  0 0  0 D D D  0 0 5 1 0  0 0  0 0 2 0 1 1 0  3 0 1 0 0  E E E  0 0 0  0 0 1  0 0  0  1  0 0 0  1 0 3 0 F F F  1 0  0 0 0  0  0 0  0 0 0  0 1 4 1  0 0 G G G 1 0 2 0  0  0 0 0 0  1  0 1 0  0 0   1 1 H H H 0  0  0 0 0  0 0  0 0 1  0  0 1 5 0 0  0 Graph Representations Adjacency Matrix: The Problem: Most graphs are sparse (i.e., most vertex pairs are not edges), so the memory requirement is excessive (i.e., V2). CS 240 239

  4. C 4 3 B A 6 C C 6 5 B B E A A D 2 2 3 E E F 3 D D H 4 5 F F G H H A A B G G A B D G A B 3 D 6 E 6 B A C D B D B C 4 C B C B C E F B 5 E 2 D B D G D G 3 E E G E C E H 3 F G F A F G G 4 G A E F H G G H A 2 H G H E H G 5 Graph Representations Adjacency List: CS 240 240

  5. Topological Sort A topological sort of an acyclic directed graph orders the vertices so that if there is a path from vertex u to vertex v, then vertex v appears after vertex u in the ordering. MATH 120 MATH 125 CS 140 One topological sort of the course prerequisite graph at right: MATH 120, CS 111, MATH 125, CS 140, MATH 150, CS 150, MATH 224, CS 240, CS 234, MATH 152, CS 312, ECE 282, STAT 380, CS 321, MATH 250, MATH 321, CS 314, MATH 423, CS 325, CS 340, CS 425, ECE 381, CS 434, ECE 482, CS 330, CS 382, CS 423, CS 438, CS 454, CS 447, CS 499, CS 482, CS 456, ECE 483 MATH 150 MATH 224 ECE 282 CS 111 CS 150 ECE 381 MATH 152 MATH 423 ECE 482 MATH 250 CS 234 CS 240 STAT 380 ECE 483 MATH 321 CS 312 CS 321 CS 325 CS 482 CS 340 CS 434 CS 382 CS 314 CS 330 CS 425 CS 447 CS 423 CS 499 CS 438 CS 456 CS 454 CS 240 241

  6. B C A E F D G H I Topological Sort Algorithm • Place all indegree-zero vertices in a list. • While the list is non-empty: • Output an element v in the indegree-zero list. • For each vertex w with (v,w) in the edge set E: • Decrement the indegree of w by one. • Place w in the indegree-zero list if its new indegree is 0. Indegree of A: 0 Indegree of B: 1 0 0 0 Indegree of C: 3 3 2 2 1 0 0 Indegree of D: 1 1 1 0 0 Indegree of E: 1 0 Indegree of F: 4 4 3 2 2 1 1 1 0 Indegree of G: 1 0 0 0 0 0 Indegree of H: 3 3 2 1 1 1 0 0 Indegree of I: 0 0 0 Output: A E I B D G C H F CS 240 242

  7. Shortest Path Problem Network Routing How do you find the shortest path from a specified vertex to every other vertex in the directed graph? Game Pathfinding Web-Based Mapping CS 240 Robotic Maneuvering 243

  8. (1,A) (1,A) (2,B) B B B B B C C C C C (1,A) (1,A) (2,E) (0,-) (0,-) (0,-) A A A A A E E E E E F F F F F D D D D D G G G G G H H H H H I I I I I (1,A) (1,A) (2,E) (1,A) (2,B) (1,A) (2,B) (1,A) (2,E) (1,A) (2,E) (0,-) (3,F) (0,-) (3,F) (1,A) (1,A) (4,D) (2,E) (2,E) Shortest Path Algorithms Case 1: Unweighted Graphs Use a breadth-first search, i.e., starting at the specified vertex, mark each adjacent vertex with its distance and its predecessor, until all vertices are marked. Algorithm’s time complexity: O(E+V). CS 240 244

  9. (7,A) (27,E) (7,A) (,-) (,-) (,-) 19 19 19 19 19 19 B B B B B B C C C C C C (,-) 7 7 7 7 7 7 (6,A) (19,E) (,-) 21 21 21 21 21 21 5 5 5 5 5 5 (6,A) (,-) (,-) (,-) (,-) (0,-) (0,-) (0,-) 6 6 6 6 6 6 13 13 13 13 13 13 2 2 2 2 2 2 A A A A A A E E E E E E F F F F F F D D D D D D 8 8 8 8 8 8 16 16 16 16 16 16 2 2 2 2 2 2 4 4 4 4 4 4 3 3 3 3 3 3 (8,A) (,-) G G G G G G H H H H H H I I I I I I (8,A) (,-) (,-) (,-) 8 8 8 8 8 8 3 3 3 3 3 3 (22,E) (,-) (,-) (7,A) (26,B) (7,A) (7,A) (26,B) (26,B) (,-) (6,A) (18,H) (,-) (,-) (6,A) (19,E) (6,A) (19,E) (0,-) (0,-) (0,-) (8,A) (,-) (8,A) (,-) (8,A) (,-) (16,G) (16,G) (22,E) Case 2: Weighted Graphs With No Negative Weights Use Dijkstra’s Algorithm, i.e., starting at the specified vertex, finalize the unfinalized vertex whose current cost is minimal, and update each vertex adjacent to the finalized vertex with its (possibly revised) cost and predecessor, until all vertices are finalized. Algorithm’s time complexity: O(E+V2) = O(V2). CS 240 245

  10. (7,A) (7,A) (7,A) (25,D) (25,D) (26,B) (20,F) (20,F) (20,F) (6,A) (18,H) (6,A) (18,H) (6,A) (18,H) (0,-) (0,-) (0,-) (8,A) (23,D) (8,A) (23,D) (8,A) (,-) 19 19 19 (16,G) (16,G) (16,G) B B B B C C C C 7 7 7 21 21 21 5 5 5 6 6 6 13 13 13 2 2 2 A A A A E E E E F F F F D D D D (7,A) (25,D) 19 8 8 8 16 16 16 2 2 2 4 4 4 3 3 3 7 G G G G H H H H I I I I 21 5 (20,F) (6,A) (18,H) 8 8 8 3 3 3 (0,-) 6 13 2 8 16 2 4 3 (8,A) (23,D) 8 3 (16,G) Note that this algorithm would not work for graphs with negative weights, since a vertex cannot be finalized when there might be some negative weight in the graph which would reduce a particular path’s cost. CS 240 246

  11. Case 3: Weighted Graphs With Negative Weights Use a variation of Dijkstra’s Algorithm without using the concept of vertex finalization, i.e., starting at the specified vertex, update each vertex adjacent to the current vertex with its (possibly revised) cost and predecessor, placing each revised vertex in a queue. Continue until the queue is empty. Algorithm’s time complexity: O(EV). (,-) (13,A) (13,A) (,-) (,-) (,-) 15 15 15 B C B C B C 13 13 13 -9 -9 -9 7 (,-) 7 (,-) 7 (,-) (,-) (,-) (25,A) (,-) (25,A) (23,E) (0,-) 25 -2 6 (0,-) 25 -2 6 (0,-) 25 -2 6 A E F D A E F D A E F D 26 11 -4 8 26 11 -4 8 26 11 -4 8 -9 -9 -9 G H I G H I G H I (,-) (,-) (26,A) (,-) (26,A) (,-) 6 -3 6 -3 6 -3 (,-) (,-) (,-) (13,A) (13,A) (13,A) (28,B) (28,B) (28,B) 15 15 15 B C B C B C 13 13 13 -9 -9 -9 7 (,-) 7 (,-) 7 (25,A) (20,B) (25,A) (20,B) (25,A) (20,B) (26,F) (0,-) 25 -2 6 (0,-) 25 -2 6 (0,-) 25 -2 6 A E F D A E F D A E F D 26 11 -4 8 26 11 -4 8 26 11 -4 8 -9 -9 -9 G H I G H I G H I (26,A) (,-) (26,A) (,-) (26,A) (,-) 6 -3 6 -3 6 -3 (,-) (32,G) (31,F) CS 240 247

  12. (13,A) (13,A) (13,A) (28,B) (28,B) (17,D) 15 15 15 B C B C B C 13 13 13 -9 -9 -9 7 7 7 (25,A) (20,B) (26,F) (22,H) (20,B) (26,F) (22,H) (20,B) (26,F) (0,-) 25 -2 6 (0,-) 25 -2 6 (0,-) 25 -2 6 A E F D A E F D A E F D 26 11 -4 8 26 11 -4 8 26 11 -4 8 -9 -9 -9 G H I G H I G H I (26,A) (,-) (26,A) (,-) (26,A) (34,D) 6 -3 6 -3 6 -3 (31,F) (31,F) (31,F) (13,A) (13,A) (13,A) (17,D) (17,D) (17,D) 15 15 15 B C B C B C 13 13 13 -9 -9 -9 7 7 7 (22,H) (20,B) (26,F) (22,H) (20,B) (26,F) (22,H) (20,B) (26,F) (0,-) 25 -2 6 (0,-) 25 -2 6 (0,-) 25 -2 6 A E F D A E F D A E F D 26 11 -4 8 26 11 -4 8 26 11 -4 8 -9 -9 -9 G H I G H I G H I (26,A) (34,D) (26,A) (34,D) (26,A) (34,D) 6 -3 6 -3 6 -3 (31,F) (31,F) (31,F) Note that this algorithm would not work for graphs with negative-cost cycles. For example, if edge IH in the above example had cost -5 instead of cost -3, then the algorithm would loop indefinitely. CS 240 248

  13. Maximum Flow Problem When paths have limited capacity, how do you maximize the overall benefit of the routes that are taken? Airline Routing Network Capacity Planning Image Segmentation CS 240 249

  14. 19 B C 35 21 35 21 27 17 23 A E F D 16 18 12 14 12 30 G H I 8 15 Maximum Flow Algorithm Assume that the directed graph G = (V, E) has edge capacities assigned to each edge, and that two vertices s and t have been designated the source and sink nodes, respectively. We wish to maximize the “flow” from s to t by determining how much of each edge’s capacity can be used so that at each vertex, the total incoming flow equals the total outgoing flow. This problem relates to such practical applications as Internet routing and automobile traffic control. In the graph at left, for instance, the total of the incoming capacities for node C is 40 while the total of the outgoing capacities for node C is 35. Obviously, the maximum flow for this graph will have to “waste” some of the incoming capacity at node C. Conversely, node B’s outgoing capacity exceeds its incoming capacity by 5, so some of its outgoing capacity will have to be wasted. CS 240 250

  15. To find a maximum flow, keep track of a flow graph and a residual graph, which keep track of which paths have been added to the flow. Keep choosing paths which yield maximal increases to the flow; add these paths to the flow graph, subtract them from the residual graph, and add their reverse to the residual graph. 19 0 19 B C B C B C 35 0 35 21 35 0 0 21 35 21 0 21 27 17 23 0 0 0 27 17 23 A E F D A E F D A E F D 16 0 16 18 12 14 0 0 0 18 12 14 12 0 12 30 0 30 G H I G H I G H I 8 15 0 0 8 15 19 0 19 B C B C B C 21 35 21 14 21 21 35 21 0 0 35 21 0 21 27 17 23 0 0 21 27 17 2 A E F D A E F D A E F D 16 0 16 21 18 12 14 0 0 0 18 12 14 12 0 12 30 0 30 G H I G H I G H I 8 15 0 0 8 15 Original Graph Flow Graph Residual Graph CS 240 251

  16. Original Graph Flow Graph Residual Graph 19 0 19 B C B C B C 21 17 35 21 14 21 21 35 21 17 0 18 17 21 17 4 27 17 23 17 17 21 10 0 A E F D A E F D 2 A E F D 16 0 17 16 21 17 12 12 18 14 0 0 0 18 12 14 0 12 30 0 30 G H I G H I G H I 8 15 0 0 8 15 14 19 14 B C B C B C 5 35 31 35 35 0 21 21 35 21 31 0 4 17 21 17 4 27 17 23 17 17 21 10 0 A E F D A E F D 2 A E F D 16 0 17 16 21 17 12 12 18 14 0 0 0 18 12 14 0 12 30 0 30 G H I G H I G H I 8 15 0 0 8 15 14 19 14 B C B C B C 5 35 31 35 35 0 21 21 35 21 31 0 4 17 21 17 4 27 17 23 17 17 21 10 0 A E F D A E F D 2 A E F D 16 12 17 4 21 17 12 12 12 18 14 12 0 0 6 12 14 12 0 12 30 12 18 12 12 G H I G H I G H I 8 15 0 12 8 3 12 CS 240 252

  17. Original Graph Flow Graph Residual Graph 14 19 14 B C B C B C 5 35 31 35 35 0 21 21 35 21 31 0 4 17 21 17 4 27 17 23 25 17 21 2 0 2 A E F D A E F D A E F D 16 12 25 4 21 17 20 18 12 14 12 8 6 6 4 6 12 4 8 4 30 20 8 10 12 12 8 G H I G H I G H I 8 15 8 12 0 3 12 8 14 19 14 B C B C B C 5 35 31 35 35 0 21 21 35 21 31 0 4 17 21 17 4 27 17 23 25 17 21 2 0 2 A E F D A E F D A E F D 16 16 25 0 21 17 24 18 12 14 16 12 10 2 0 2 12 8 4 8 30 24 12 6 16 8 12 G H I G H I G H I 8 15 8 12 0 3 12 8 Thus, the maximum flow for the graph is 76. CS 240 253

  18. B C B C D E H 3 D E H A A F G F G 6 B C B C B C 5 5 3 D E H 3 D E H A A 3 D E H A 4 4 4 F G F G F G 6 6 6 B C B C B C 5 4 5 4 5 4 3 D E H 3 D E H 3 D E 7 H A A A 3 3 4 4 4 F G F G F G MINIMUM SPANNING TREE Minimum Spanning Trees An alternative to Kruskal’s Algorithm (which develops a minimum spanning tree via forests) is Prim’s Algorithm, which is just a variation of Dijkstra’s Algorithm. Starting with a minimum-cost edge, it builds the tree by adding minimum-cost edges as long as they don’t create cycles. Like Kruskal’s, Prim’s Algorithm is O(ElogV) 6 B C 5 7 4 8 3 D 8 E 7 H A 6 3 9 4 F G 7 ORIGINAL GRAPH CS 240 254

  19. A B A B C A B C C D E D E E F G H F G H G Original Graph Depth-First Search (Solid lines are part of depth-first spanning tree; dashed lines are visits to previously marked vertices) D H F Depth-First Spanning Tree Depth-First Search A convenient means to traverse a graph is to use a depth-first search, which recursively visits and marks the vertices until all of them have been traversed. Such a traversal provides a means by which several significant features of a graph can be determined. CS 240 255

  20. B E H 2 6 8 2/1 6/6 8/6 A D G J 1 3 7 10 1/1 3/1 7/6 10/7 C F I 4 5 9 4/1 5/5 9/7 Depth-First Search (with nodes numbered as they’re visited) Original Graph Nodes also marked with lowest-numbered vertex reachable via zero or more tree edges, followed by at most one back edge Depth-First Search Application: Articulation Points A vertex v in a graph G is called an articulation point if its removal from G would cause the graph to be disconnected. Such vertices would be considered “critical” in applications like networks, where articulation points are the only means of communication between different portions of the network. A depth-first search can be used to find all of a graph’s articulation points. The only articulation points are the root (if it has more than one child) and any other node v with a child whose “low” number is at least as large as v’s “visit” number. (In this example: nodes B, C, E, and G.) CS 240 256

  21. B C D B C D B C D A G F E A G F E A G F E H I J H I J H I J Original Graph After Removing First DFS Cycle: BCDFB After Removing Second DFS Cycle: ABGHA B C D B C D A G F E A G F E H I J H I J After Removing Third DFS Cycle: FHIF After Removing Fourth DFS Cycle: FEJF Depth-First Search Application: Euler Circuits An Euler circuit of a graph G is a cycle that visits every edge exactly once. Such a cycle could be useful in applications like networks, where it could provide an efficient means of testing whether each network link is up. A depth-first search can be used to find an Euler circuit of a graph, if one exists. (Note: An Euler circuit exists only if the graph is connected and every vertex has even degree.) Splicing the first two cycles yields cycle A(BCDFB)GHA. Splicing this cycle with the third cycle yields ABCD(FHIF)BGHA. Splicing this cycle with the fourth cycle yields ABCD(FEJF)HIFBGHA Note that this traversal takes O(E+V) time. CS 240 257

  22. P and NP Problems A problem is said to be a P problem if it can be solved with a deterministic, polynomial-time algorithm. (Deterministic algorithms have each step clearly specified.) A problem is said to be an NP problem if it can be solved with a nondeterministic, polynomial-time algorithm. In essence, at a critical point in the algorithm, a decision must be made, and it is assumed that a magical “choice” function always chooses correctly. A nondeterministic polynomial-time solution: totalWorth = 0; totalWeight = 0; for (i=1; i<=n; i++) { b[i] = choice(TRUE,FALSE); if (b[i]==TRUE) { totalWorth += p[i]; totalWeight += w[i]; } } if ((totalWorth >= T) && (totalWeight <= M)) cout << “YAHOO! I’M RICH!”; else cout << “@#$&%!”; Example: The Knapsack Problem Given a set of n valuable jewels J1, J2, …, Jn with respective weights w1, w2, …, wn, and respective prices p1, p2, …, pn, as well as a knapsack capable of supporting a total weight of M. Problem: Is there a way to pack at least T dollars worth of jewels, without exceeding the weight capacity of the knapsack? (It’s not as easy as it sounds; three lightweight $1000 jewels might be preferable to one heavy $2500 jewel, for instance.) CS 240 258

More Related