1 / 20

Chapter 13 Graphs

Chapter 13 Graphs. The graph ADT, while relatively unstructured, provides a platform for solving some of the most sophisticated problems in computer science. Graph Abstract Data Types. Topological Sort. Minimum Path. Maximum Flow. Spanning Tree. Depth-First Search. 4. 3. 6. 6. 5.

essien
Download Presentation

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 The graph ADT, while relatively unstructured, provides a platform for solving some of the most sophisticated problems in computer science. • Graph Abstract Data Types • Topological Sort • Minimum Path • Maximum Flow • Spanning Tree • Depth-First Search Chapter 13 - Graphs

  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. Chapter 13 - Graphs

  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 0 1  1 3 1  0 0 1 6 0 0  0  0 0 1 0  0  0 B B B  1 0 0  0 4 1 0 1  1 0 0 6  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 1 5 0 0  0 0 0  0 2 1 1 0  3 0 1 0  0 E E E 0 0  0  0 0 1   0 0  1 0  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 0 2 1 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   1 0 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). Chapter 13 - Graphs

  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 Adjacency List: Chapter 13 - Graphs

  5. MATH 120 MATH 150 CS 140 CS 150 MATH 152 STAT 380 CS 240 MATH 223 CS 275 CS 325 CS 250 CS 312 CS 434 CS 321 CS 482 CS 314 CS 407 CS 330 ECE 382 CS 416 CS 438 ECE 481 CS 423 CS 444 CS 454 ECE 482 CS 447 CS 456 ECE 483 CS 425 CS 499 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. One topological sort of the course prerequisite graph at right: MATH 120, MATH 150, CS 140, MATH 223, CS 150, CS 240, MATH 152, CS 275, CS 312, CS 321, CS 250, CS 314, CS 425, ECE 382, CS 434, ECE 483, CS 330, STAT 380, CS 407, CS 423, CS 416, CS 325, CS 438, CS 454, CS 447, CS 499, CS 482, CS 456, CS 444, ECE 481, ECE 482 Chapter 13 - Graphs

  6. Indegree of A: 0 - - - - - - - - - B C 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 - - - - - A E F D 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 - - - - G H I 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 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. Chapter 13 - Graphs

  7. (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 How do you find the shortest path from a specified vertex to every other vertex in the directed graph? 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). Chapter 13 - Graphs

  8. (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) Shortest-Path Algorithms (Continued) 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). Chapter 13 - Graphs

  9. (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. Chapter 13 - Graphs

  10. (,-) (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) Shortest-Path Algorithms (Continued) 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). Chapter 13 - Graphs

  11. (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. Chapter 13 - Graphs

  12. 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. Chapter 13 - Graphs

  13. 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 Maximum-Flow Algorithm (Continued) 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. Original Graph Flow Graph Residual Graph Chapter 13 - Graphs

  14. 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 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 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 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 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 18 12 14 12 0 0 6 12 14 12 12 0 12 30 12 18 12 12 G H I G H I G H I 8 15 0 12 8 3 12 Original Graph Flow Graph Residual Graph Chapter 13 - Graphs

  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 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 Original Graph Flow Graph Residual Graph Thus, the maximum flow for the graph is 76. Chapter 13 - Graphs

  16. 6 B C B C B C 5 7 4 8 3 D 8 E 7 H A D E H 3 D E H A A 6 3 9 4 F G F G F G 7 ORIGINAL GRAPH 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) Chapter 13 - Graphs

  17. 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. Chapter 13 - Graphs

  18. 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 Original Graph Depth-First Search (with nodes numbered as they’re visited) Nodes also marked with lowest-numbered vertex reachable via zero or more tree edges, followed by at most one back edge 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.) Chapter 13 - Graphs

  19. 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: FEJF After Removing Fourth DFS Cycle: FHIF 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(FEJF)BGHA. Splicing this cycle with the fourth cycle yields ABCD(FHIF)EJFBGHA Note that this traversal takes O(E+V) time. Chapter 13 - Graphs

  20. 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.) Chapter 13 - Graphs

More Related