csc 213

of 29 /29
CSC 213 Lecture 24: Minimum Spanning Trees

Author: audra

Post on 15-Jan-2016

38 views

Category:

Documents


0 download

Embed Size (px)

DESCRIPTION

CSC 213. Lecture 24: Minimum Spanning Trees. Announcements. Final exam is: Thurs. 5/11 from 8:30-10:30AM in Old Main 205. Spanning subgraph Subgraph including every vertex Spanning tree Spanning subgraph that is also a tree Minimum spanning tree - PowerPoint PPT Presentation

TRANSCRIPT

  • CSC 213Lecture 24: Minimum Spanning Trees

    Minimum Spanning Trees

  • AnnouncementsFinal exam is: Thurs. 5/11 from 8:30-10:30AM in Old Main 205

    Minimum Spanning Trees

  • Minimum Spanning Tree (MST)Spanning subgraphSubgraph including every vertexSpanning treeSpanning subgraph that is also a treeMinimum spanning treeSpanning tree with minimum total edge weightORDPITATLSTLDENDFWDCA10198632574

    Minimum Spanning Trees

  • Cycle PropertyCycle Property:MSTs do not contain cyclesSo weight of each edge in MST is lower than weight of edge which completes cycleProof:e is edge in G & not in MSTC is cycle formed when adding e to MSTFor every edge f in C, weight(f) weight(e) If weight(f) > weight(e), then exists a smaller spanning tree with e instead of fReplacing f with e yields a better spanning tree

    Minimum Spanning Trees

  • Partition PropertyPartition Property:Given partitions of vertices, minimum weight edge is in a MST connecting partitionsProof:e is edge with smallest weight connecting partitions, but not in MSTf connects partitions in MSTIf MST does not contain e, consider cycle formed by including e with MSTBy cycle property, weight(f) weight(e)But since e has smallest weight, weight(f) = weight(e)Create another MST by replacing f with eUV742857398ef742857398efReplacing f with e yields another MSTUV

    Minimum Spanning Trees

  • Kruskals AlgorithmAdd edges to MST greedilyPriority queue contains edges outside the cloudElement: edgeKey: edges weightOnly want edges that will not make a cycleUnfortunately, detecting cycles can be hard

    Minimum Spanning Trees

  • Data Structs for Kruskal AlgorithmAlgorithm maintains forest of treesEdge accepted when connecting treesUse partition (i.e., collection of disjoint sets):find(u): return set containing uunion(u,v): replace sets with u & v with their union

    Minimum Spanning Trees

  • Partition RepresentationEach set is SequenceEach element refers to its setmakeSet(u) takes O(1) timefind(u) takes O(1) timeunion(u,v) moves elements of smaller set to Sequence of larger set and updates referencesBy having union move smaller set into large set, elements are processed only when set size doublesn calls to union() takes O(log n) time

    Minimum Spanning Trees

  • Kruskals AlgorithmAlgorithm KruskalMST(Graph G)Q new PriorityQueue()T new Graph()P new Partition()for each vertex v in GP.makeSet(v)T.insertVertex(v)for each edge e in GQ.insert(e.getWeight(), e);while T.edges().getSize() < n-1Edge e = Q.removeMin()Assign u, v to G.endpoints(e)if P.find(u) P.find(v) thenT.insertEdge(e) P.union(P.find(u),P.find(v))return T

    Minimum Spanning Trees

  • Kruskal ExampleJFKBOSMIAORDLAXDFWSFOBWIPVD867270418712588491447401391184946109011212342184662180214641235337

    Minimum Spanning Trees

  • Example

    Minimum Spanning Trees

  • Example

    Minimum Spanning Trees

  • Example

    Minimum Spanning Trees

  • Example

    Minimum Spanning Trees

  • Example

    Minimum Spanning Trees

  • Example

    Minimum Spanning Trees

  • Example

    Minimum Spanning Trees

  • Example

    Minimum Spanning Trees

  • Example

    Minimum Spanning Trees

  • Example

    Minimum Spanning Trees

  • Example

    Minimum Spanning Trees

  • Example

    Minimum Spanning Trees

  • Example1447401391184946109011212342184662180214641235337

    Minimum Spanning Trees

  • Prim-Jarniks AlgorithmSimilar to Dijkstras algorithmPick arbitrary vertex s and grow MST as cloud of verticesLabel each vertex v with d(v) --- smallest weight of edge connecting v to vertex in cloud At each step:Add vertex u with smallest distance to cloudUpdate labels of vertices adjacent to u

    Minimum Spanning Trees

  • Prim-Jarniks Algorithm (cont.)Priority queue stores vertices outside of cloudKey: distanceElement: vertexLocator-based methodsinsert(k,e) returns locator replaceKey(l,k) changes items keyStore three labels with each vertexDistance from cloudEdge connecting vertex to cloud (MST edge)Locator in priority queue

    Minimum Spanning Trees

  • Prim-Jarniks Algorithm (cont.)Algorithm PrimJarnikMST(Graph G)Q new PriorityQueue()s G.vertices().elemAtRank(0)for each vertex v in Gif v = sv.setDistance(0)else v.setDistance()v.setParent(null)l Q.insert(getDistance(v), v)setLocator(v,l)while !Q.isEmpty()u Q.removeMin() for each edge e in G.incidentEdges(u)z G.opposite(u,e)r e.getWeight()if r < z.getDistance()z.setDistance(r)z.setParent(e) Q.replaceKey(z.getLocator(),r)

    Minimum Spanning Trees

  • ExampleBDCAFE7428573980728BDCAFE74285739807257BDCAFE74285739807257BDCAFE742857398072547

    Minimum Spanning Trees

  • Example (contd.)BDCAFE742857398032547BDCAFE742857398032547

    Minimum Spanning Trees

  • Prim-Jarniks AnalysisOperationsincidentEdges called once per vertexSet/Get each vertexs labels O(deg(v)) timesPriority queue operationsEach vertex inserted once and removed onceEach insertion/removal takes O(log n)Vertex ws key modified O(deg(w)) timesEach key change takes O(log n) time Algorithm takes O((n + m) log n) time using adjacency list structure

    Minimum Spanning Trees

    Minimum Spanning TreeMinimum Spanning TreeMinimum Spanning TreeMinimum Spanning TreeMinimum Spanning TreeMinimum Spanning TreeMinimum Spanning TreeMinimum Spanning TreeMinimum Spanning TreeMinimum Spanning TreeMinimum Spanning TreeMinimum Spanning TreeMinimum Spanning TreeMinimum Spanning TreeMinimum Spanning TreeMinimum Spanning TreeMinimum Spanning TreeMinimum Spanning TreeMinimum Spanning TreeMinimum Spanning TreeMinimum Spanning TreeMinimum Spanning TreeMinimum Spanning TreeMinimum Spanning TreeMinimum Spanning TreeMinimum Spanning TreeMinimum Spanning TreeMinimum Spanning TreeMinimum Spanning Tree