# graph algorithms shortest paths, minimum spanning trees, etc

Post on 30-Dec-2015

53 views

Category:

## Documents

3 download

Tags:

• #### edge eproof

Embed Size (px)

DESCRIPTION

1843. ORD. SFO. 802. 1743. 337. 1233. LAX. DFW. Graph Algorithms shortest paths, minimum spanning trees, etc. Nancy Amato Parasol Lab, Dept. CSE, Texas A&M University - PowerPoint PPT Presentation

TRANSCRIPT

• Graph Algorithms shortest paths, minimum spanning trees, etc.Nancy AmatoParasol Lab, Dept. CSE, Texas A&M University

Acknowledgement: These slides are adapted from slides provided with Data Structures and Algorithms in C++, Goodrich, Tamassia and Mount (Wiley 2004)

ORDDFWSFOLAX802174318431233337http://parasol.tamu.edu

• Graphs*Minimum Spanning TreesJFKBOSMIAORDLAXDFWSFOBWIPVD867270418712588491447401391184946109011212342184662180214641235337

Graphs

• Outline and ReadingMinimum Spanning Trees (13.6)DefinitionsA crucial factThe Prim-Jarnik Algorithm (13.6.2)

Kruskal's Algorithm (13.6.1)

*Graphs

Graphs

• Reminder: Weighted GraphsIn a weighted graph, each edge has an associated numerical value, called the weight of the edgeEdge 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*GraphsORDPVDMIADFWSFOLAXLGAHNL84980213871743184310991120123333725551421205

Graphs

• Minimum Spanning TreeSpanning subgraphSubgraph of a graph G containing all the vertices of GSpanning treeSpanning subgraph that is itself a (free) treeMinimum spanning tree (MST)Spanning tree of a weighted graph with minimum total edge weightApplicationsCommunications networksTransportation networks

*GraphsORDPITATLSTLDENDFWDCA10198632574

Graphs

• Exercise: MSTShow an MSF of the following graph.*GraphsORDPVDMIADFWSFOLAXLGAHNL84980213871743184310991120123333725551421205

Graphs

• Cycle PropertyCycle Property:Let T be a minimum spanning tree of a weighted graph GLet e be an edge of G that is not in T and C let be the cycle formed by e with TFor every edge f of C, weight(f) weight(e) Proof:By contradictionIf weight(f) > weight(e) we can get a spanning tree of smaller weight by replacing e with f*GraphsReplacing f with e yields a better spanning tree

Graphs

• Partition PropertyPartition Property:Consider a partition of the vertices of G into subsets U and VLet e be an edge of minimum weight across the partitionThere is a minimum spanning tree of G containing edge eProof:Let T be an MST of GIf T does not contain e, consider the cycle C formed by e with T and let f be an edge of C across the partitionBy the cycle property, weight(f) weight(e) Thus, weight(f) = weight(e)We obtain another MST by replacing f with e*GraphsUV742857398ef742857398efReplacing f with e yields another MSTUV

Graphs

• Prim-Jarniks Algorithm(Similar to Dijkstras algorithm, for a connected graph)We pick an arbitrary vertex s and we grow the MST as a cloud of vertices, starting from sWe store with each vertex v a label d(v) = the smallest weight of an edge connecting v to a vertex in the cloud *Graphs At each step: We add to the cloud the vertex u outside the cloud with the smallest distance label We update the labels of the vertices adjacent to u

Graphs

• Prim-Jarniks Algorithm (cont.)A priority queue stores the vertices outside the cloudKey: distanceElement: vertexLocator-based methodsinsert(k,e) returns a locator replaceKey(l,k) changes the key of an itemWe store three labels with each vertex:DistanceParent edge in MSTLocator in priority queue*GraphsAlgorithm PrimJarnikMST(G)Q new heap-based priority queues a vertex of Gfor all v G.vertices()if v = ssetDistance(v, 0)else setDistance(v, )setParent(v, )l Q.insert(getDistance(v), v)setLocator(v,l)while Q.isEmpty()u Q.removeMin() for all e G.incidentEdges(u)z G.opposite(u,e)r weight(e)if r < getDistance(z)setDistance(z,r)setParent(z,e) Q.replaceKey(getLocator(z),r)

Graphs

• Example*GraphsBDCAFE7428573980728BDCAFE74285739807257BDCAFE74285739807257BDCAFE742857398072547

Graphs

• Example (contd.)*GraphsBDCAFE742857398032547BDCAFE742857398032547

Graphs

• Exercise: Prims MST algShow how Prims MST algorithm works on the following graph, assuming you start with SFO, I.e., s=SFO.Show how the MST evolves in each iteration (a separate figure for each iteration).*GraphsORDPVDMIADFWSFOLAXLGAHNL84980213871743184310991120123333725551421205

Graphs

• AnalysisGraph operationsMethod incidentEdges is called once for each vertexLabel operationsWe set/get the distance, parent and locator labels of vertex z O(deg(z)) timesSetting/getting a label takes O(1) timePriority queue operationsEach vertex is inserted once into and removed once from the priority queue, where each insertion or removal takes O(log n) timeThe key of a vertex w in the priority queue is modified at most deg(w) times, where each key change takes O(log n) time Prim-Jarniks algorithm runs in O((n + m) log n) time provided the graph is represented by the adjacency list structureRecall that Sv deg(v) = 2mThe running time is O(m log n) since the graph is connected*

• *GraphsKruskals AlgorithmA priority queue stores the edges outside the cloudKey: weightElement: edgeAt the end of the algorithmWe are left with one cloud that encompasses the MSTA tree T which is our MSTAlgorithm KruskalMST(G)for each vertex V in G dodefine a Cloud(v) of {v}

let Q be a priority queue.Insert all edges into Q using their weights as the keyT

while T has fewer than n-1 edges edge e = T.removeMin()Let u, v be the endpoints of eif Cloud(v) Cloud(u) thenAdd edge e to TMerge Cloud(v) and Cloud(u)return T

Graphs

• Data Structure for Kruskal AlgortihmThe algorithm maintains a forest of treesAn edge is accepted it if connects distinct treesWe need a data structure that maintains a partition, i.e., a collection of disjoint sets, with the operations:find(u): return the set storing uunion(u,v): replace the sets storing u and v with their unionGraphs*

• Representation of a PartitionEach set is stored in a sequenceEach element has a reference back to the setoperation find(u) takes O(1) time, and returns the set of which u is a member.in operation union(u,v), we move the elements of the smaller set to the sequence of the larger set and update their referencesthe time for operation union(u,v) is min(nu,nv), where nu and nv are the sizes of the sets storing u and vWhenever an element is processed, it goes into a set of size at least double, hence each element is processed at most log n timesGraphs*

• Partition-Based ImplementationA partition-based version of Kruskals Algorithm performs cloud merges as unions and tests as finds.Graphs*Algorithm Kruskal(G): Input: A weighted graph G. Output: An MST T for G.Let P be a partition of the vertices of G, where each vertex forms a separate set.Let Q be a priority queue storing the edges of G, sorted by their weightsLet T be an initially-empty treewhile Q is not empty do (u,v) Q.removeMinElement() if P.find(u) != P.find(v) thenAdd (u,v) to TP.union(u,v)return TRunning time: O((n+m) log n)

• Graphs*Kruskal ExampleJFKBOSMIAORDLAXDFWSFOBWIPVD867270418712588491447401391184946109011212342184662180214641235337

Graphs

• Example*Graphs

Graphs

• Example*Graphs

Graphs

• Example*Graphs

Graphs

• Example*Graphs

Graphs

• Example*Graphs

Graphs

• Example*Graphs

Graphs

• Example*Graphs

Graphs

• Example*Graphs

Graphs

• Example*Graphs

Graphs

• Example*Graphs

Graphs

• Example*Graphs

Graphs

• Example*Graphs

Graphs

• Graphs*Example1447401391184946109011212342184662180214641235337

Graphs

• Exercise: Kruskals MST algShow how Kruskals MST algorithm works on the following graph.Show how the MST evolves in each iteration (a separate figure for each iteration).*GraphsORDPVDMIADFWSFOLAXLGAHNL84980213871743184310991120123333725551421205

Graphs

• Boruvkas Algorithm Like Kruskals Algorithm, Baruvkas algorithm grows many clouds at once.

Each iteration of the while-loop halves the number of connected compontents in T.The running time is O(m log n).*GraphsAlgorithm BoruvkaMST(G)T V {just the vertices of G}while T has fewer than n-1 edges dofor each connected component C in T doLet edge e be the smallest-weight edge from C to another component in T.if e is not already in T thenAdd edge e to Treturn T

Graphs

• Baruvka Example*Graphs

Graphs

• Example*Graphs

Graphs

• Graphs*Example8491447401391184946109011212342184662180214641235337

Graphs

• Shortest Paths*Graphs

Graphs

• Outline and ReadingWeighted graphs (13.5.1)Shortest path problemShortest path propertiesDijkstras algorithm (13.5.2)AlgorithmEdge relaxationThe Bellman-Ford algorithm Shortest paths in DAGs All-pairs shortest paths *Graphs

Graphs

• Weighted GraphsIn a weighted graph, each edge has an associated numerical value, called the weight of the edgeEdge 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*GraphsORDPVDMIADFWSFOLAXLGAHNL84980213871743184310991120123333725551421205

Graphs

• Shortest Path ProblemGiven a weighted graph and two vertices u and v, we want to find a path of minimum total weight between u and v.Length of a path is the sum of the weights of its edges.Example:Shortest path between Providence and HonoluluApplicationsInternet packet routing Flight reservationsDriving directions*GraphsORDPVDMIADFWSFOLAXLGAHNL84980213871743184310991120123333725551421205

Graphs

• Shortest Path ProblemIf there is no path from v to u, we denote the distance between them by d(v, u)=+What if there is a negative-weight cycle in the

Recommended