chapter 9: graph algorithms

Download Chapter 9: Graph Algorithms

If you can't read please download the document

Upload: fauve

Post on 23-Mar-2016

65 views

Category:

Documents


1 download

DESCRIPTION

Chapter 9: Graph Algorithms. Graph ADT. Topological Sort. Shortest Path. Maximum Flow. Minimum Spanning Tree. Depth-First Search. Undecidability. NP-Completeness. CS 340. Page 143. 4. 3. 6. 6. 5. 2. 2. 3. 3. 4. 5. The Graph Abstract Data Type. - PowerPoint PPT Presentation

TRANSCRIPT

PowerPoint Presentation

Chapter 9: Graph AlgorithmsGraph ADTTopological SortCS 340Page 143Shortest PathMaximum FlowMinimum Spanning TreeDepth-First SearchUndecidabilityNP-Completeness

CS 340Page 144The Graph Abstract Data TypeA 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.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)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)34242356365

CS 340Page 145Graph Representation Option 1: Adjacency MatrixABDHEGFCABDHEGFCABDHEGFC34242356365The Problem: Most graphs are sparse (i.e., most vertex pairs are not edges), so the memory requirement is excessive: (V2).ABCDEFGHA11000010B10110000C01000000D01000000E00001010F00000010G10001101H00000010ABCDEFGHA01010000B00010000C01000000D00001110E00100000F10000010G00000001H00001000ABCDEFGHA36B46CD523E3F4G2H5

CS 340Page 146Graph Representation Option 2: Adjacency ListsABDHEGFCABDHEGFCABDHEGFC34242356365ABCDEFGHAABBEGAGBCGEGDFHABCDEFGHBB3HG624CA353542DEGG6EABCDEFGHBEBGCAEDFGDH

CS 340Page 147Topological SortA 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 left:CS 111, MATH 120, CS 140, MATH 125, CS 150, MATH 224, CS 234, CS 240, ECE 282, CS 312, CS 325, MATH 150, MATH 152, STAT 380, CS 321, MATH 250, MATH 321, CS 314, MATH 423, 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 125CS 140CS 150MATH 152CS 240CS 234MATH 224CS 340STAT 380CS 438CS 454CS 456CS 325CS 499CS 434CS 312CS 482CS 330CS 423CS 447MATH 150CS 314CS 425CS 321ECE 483ECE 282ECE 381ECE 482CS 111MATH 120MATH 250MATH 321MATH 423CS 382

CS 340Page 148Topological Sort AlgorithmPlace 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.GAHIEFDCB03104030021302002020110201010101010013114130Indegree of A:Indegree of B:Indegree of C:Indegree of D:Indegree of E:Indegree of F:Indegree of G:Indegree of H:Indegree of I:Output:AEIBDGCH0F

CS 340Page 149Shortest Paths

The shortest path between two locations is dependent on the function that is used to measure a paths cost.Does the path cost less if the total distance traveled is minimized?Does the path cost less if the amount of traffic encountered is minimized?Does the path cost less if the amount of boring scenery is minimized?In general, then, how do you find the shortest path from a specified vertex to every other vertex in a directed graph?

CS 340Page 150Shortest Path Algorithm: Unweighted GraphsGAHIEFCBD(0,-)(0,-)(1,A)(1,A)(1,A)(2,B)(2,E)(2,E)(3,F)GAHIEFCBD(0,-)(1,A)(1,A)(1,A)GAHIEFCBD(0,-)(1,A)(1,A)(1,A)(2,B)(2,E)(2,E)GAHIEFCBD(0,-)(1,A)(1,A)(1,A)(2,B)(2,E)(2,E)(3,F)(4,D)GAHIEFCBDUse 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. Algorithms time complexity: O(E+V).

CS 340Page 151Shortest Path Algorithm:Weighted Graphs With No Negative WeightsUse Dijkstras 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. Algorithms time complexity: O(E+V2) = O(V2).GAHIEFCBD137681921168225343(0,-)(,-)(,-)(,-)(,-)(,-)(,-)(,-)(,-)(0,-)(7,A)(26,B)(6,A)(19,E)(8,A)(22,E)(,-)(,-)GAHIEFCBD137681921168225343(0,-)(7,A)(27,E)(6,A)(19,E)(8,A)(22,E)(,-)(,-)GAHIEFCBD137681921168225343(0,-)(7,A)(26,B)(6,A)(18,H)(8,A)(16,G)(,-)(,-)GAHIEFCBD137681921168225343(0,-)(7,A)(,-)(6,A)(,-)(8,A)(,-)(,-)(,-)GAHIEFCBD137681921168225343(0,-)(7,A)(26,B)(6,A)(19,E)(8,A)(16,G)(,-)(,-)GAHIEFCBD137681921168225343

CS 340Page 152Note 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 paths cost.(0,-)(7,A)(25,D)(6,A)(18,H)(8,A)(16,G)(20,F)(23,D)GAHIEFCBD137681921168225343(0,-)(7,A)(26,B)(6,A)(18,H)(8,A)(16,G)(20,F)(,-)GAHIEFCBD137681921168225343(0,-)(7,A)(25,D)(6,A)(18,H)(8,A)(16,G)(20,F)(23,D)GAHIEFCBD137681921168225343(0,-)(7,A)(25,D)(6,A)(18,H)(8,A)(16,G)(20,F)(23,D)GAHIEFCBD137681921168225343

CS 340Page 153Shortest Path Algorithm:Weighted Graphs With Negative WeightsUse a variation of Dijkstras 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. Algorithms time complexity: O(EV).GAHIEFCBD13252615-27-96116-9-3-48(0,-)(,-)(,-)(,-)(,-)(,-)(,-)(,-)(,-)(0,-)(13,A)(28,B)(25,A)(20,B)(26,A)(,-)(,-)(,-)GAHIEFCBD13252615-27-96116-9-3-48(0,-)(13,A)(,-)(25,A)(23,E)(26,A)(,-)(,-)(,-)GAHIEFCBD13252615-27-96116-9-3-48(0,-)(13,A)(28,B)(25,A)(20,B)(26,A)(31,F)(26,F)(,-)GAHIEFCBD13252615-27-96116-9-3-48(0,-)(13,A)(,-)(25,A)(,-)(26,A)(,-)(,-)(,-)GAHIEFCBD13252615-27-96116-9-3-48(0,-)(13,A)(28,B)(25,A)(20,B)(26,A)(32,G)(,-)(,-)GAHIEFCBD13252615-27-96116-9-3-48

CS 340Page 154Note 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.(0,-)(13,A)(28,B)(25,A)(20,B)(26,A)(31,F)(26,F)(,-)GAHIEFCBD13252615-27-96116-9-3-48(0,-)(13,A)(17,D)(22,H)(20,B)(26,A)(31,F)(26,F)(34,D)GAHIEFCBD13252615-27-96116-9-3-48(0,-)(13,A)(17,D)(22,H)(20,B)(26,A)(31,F)(26,F)(34,D)GAHIEFCBD13252615-27-96116-9-3-48(0,-)(13,A)(17,D)(22,H)(20,B)(26,A)(31,F)(26,F)(34,D)GAHIEFCBD13252615-27-96116-9-3-48(0,-)(13,A)(28,B)(22,H)(20,B)(26,A)(31,F)(26,F)(,-)GAHIEFCBD13252615-27-96116-9-3-48(0,-)(13,A)(17,D)(22,H)(20,B)(26,A)(31,F)(26,F)(34,D)GAHIEFCBD13252615-27-96116-9-3-48

CS 340Page 155Maximum FlowAssume 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 edges 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.GAIEFCBDH3527181917211681223351514301221In 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 Bs outgoing capacity exceeds its incoming capacity by 5, so some of its outgoing capacity will have to be wasted.

CS 340Page 156Maximum Flow AlgorithmTo 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 GraphFlow GraphResidual GraphGAIEFCBDH3527181917211681223351514301221GAIEFCBDH0000000000000000GAIEFCBDH3527181917211681223351514301221GAIEFCBDH3527181917211681223351514301221GAIEFCBDH2100002100021000000GAIEFCBDH14271819170168122351514301221212121

CS 340Page 157Original GraphFlow GraphResidual GraphGAIEFCBDH12181214101819001682151430421212117171717GAIEFCBDH4171701018500168122151430124352121173114GAIEFCBDH01201201065048243141843521211717173114121212122335271819172116835151430211212GAIEFCBDH21170017210002117000017GAIEFCBDH3527181917211681223351514301221GAIEFCBDH35171214172112002131120121217GAIEFCBDH3527181917211681223351514301221GAIEFCBDH351701417210002131000017GAIEFCBDH

CS 340Page 158Original GraphFlow GraphResidual GraphGAIEFCBDH3527181917211681223351514301221GAIEFCBDH3525121417211288213112620417GAIEFCBDH444425480265000236108352121171731141212122088GAIEFCBDH3527181917211681223351514301221GAIEFCBDH352516141721168122131121024817GAIEFCBDH041781202250000243264352121172531141681224812Thus, the maximum flow for the graph is 76.

CS 340Page 159Minimum Spanning Tree: Kruskals Algorithm

When a large set of vertices must be interconnected (i.e., spanning) and there are several possible means of doing so, redundancy can be eliminated (i.e., tree) and costs can be reduced (i.e., minimum) by employing a minimum spanning tree.Kruskals Algorithm accomplishes this by just selecting minimum-cost edges as long as they dont form cycles.BADCEFGHBADCEFGH3BADCEFGH546433ORIGINAL GRAPHBADCEFGH5476747869383BADCEFGH33BADCEFGH433BADCEFGH4433BADCEFGH54433MINIMUM SPANNING TREEBADCEFGH5464733

CS 340Page 160Minimum Spanning Tree: Prims AlgorithmAn alternative to Kruskals Algorithm is Prims Algorithm, a variation of Dijkstras Algorithm that starts with a minimum-cost edge, and builds the tree by adding minimum-cost edges as long as they dont create cycles. Like Kruskals Algorithm, Prims Algorithm is O(ElogV)BADCEFGHBADCEFGH3BADCEFGH543BADCEFGH546433BADCEFGH5463MINIMUM SPANNING TREEBADCEFGH5464733BADCEFGH43BADCEFGH54643ORIGINAL GRAPHBADCEFGH5476747869383

CS 340Page 161Depth-First SearchA 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.ABCFGHDEOriginal GraphABCFGHDEDepth-First Search(Solid lines are part of depth-first spanning tree; dashed lines are visits to previously marked vertices)Such a traversal provides a means by which several significant features of a graph can be determined.ABCFGHDEDepth-First Spanning Tree

CS 340Page 162Depth-First Search Application: Articulation PointsA 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 graphs 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 vs visit number. (In this example: nodes B, C, E, and G.)Original GraphBEHCFGADJIDepth-First Search (with nodes numbered as theyre visited)26845713109Nodes also marked with lowest-numbered vertex reachable via zero or more tree edges, followed by at most one back edge2/16/68/64/15/57/61/13/110/79/7

CS 340Page 163Depth-First Search Application: Euler CircuitsAn 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.)Original GraphBCDHIFAGEJSplicing 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)HIFBGHANote that this traversal takes O(E+V) time.After Removing First DFS Cycle: BCDFBBCDHIFAGEJAfter Removing Second DFS Cycle: ABGHABCDHIFAGEJAfter Removing Fourth DFS Cycle: FEJFBCDHIFAGEJAfter Removing Third DFS Cycle: FHIFBCDHIFAGEJ

CS 340Page 164Depth-First Search Application: Strong ComponentsA subgraph of a directed graph is a strong component of the graph if there is a path in the subgraph from every vertex in the subgraph to every other vertex in the subgraph.In network applications, such a subgraph could be used to ensure intercommunication.A depth-first search can be used to find the strong components of a graph.The trees in the final depth-first spanning forest form the set of strong components.In this example, the strong components are: { C }, { B, F, I, E }, { A }, { D, G, K, J }, and { H }.Number the vertices according to a depth-first postorder traversal, and reverse the edges.6511132104879Depth-first search of the revised graph, always starting at the vertex with the highest number.6511132104879Original Directed GraphABCHIFDEGKJFirst leg of depth-first searchABCHIFDEGKJSecond leg of depth-first searchABCHIFDEGKJThird leg of depth-first searchABCHIFDEGKJ

CS 340Page 165Undecidable ProblemsYESNOloop foreverhaltProgram H

Program PProgram XSome problems are difficult to solve on a computer, but some are impossible!Program H

Program PInput IYES (if P halts on I)NO (if P loops forever on I)If this program H exists, then lets make it the subroutine for program X, which takes any program P, and runs it through subroutine H as both the program and the input. If the result from the subroutine is yes, the program X enters an infinite loop; otherwise it halts.Note that if we ran program X with itself as its input, then it would halt only if it loops forever, and it would loop forever only if it halts!?!?This contradiction means that our assumption that we could build program H was false, i.e., that the halting problem is undecidable.Example: The Halting ProblemWed like to build a program H that will test any other program P and any input I and answer yes if P will terminate normally on input I and no if P will get stuck in an infinite loop on input I.

CS 340Page 166P and NP ProblemsA 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.Example: SatisfiabilityGiven a set of n boolean variables b1, b2, , bn and a boolean function f(b1, b2, , bn).Problem: Are there values that can be assigned to the variables so that the function would evaluate to TRUE?For example, is the function:(b1 OR b2) AND (b3 OR NOT b4) ever TRUE?To try every combination takes exponential time, but a nondeterministic solution is polynomial-time:for (i=1; i