MSTMotivation: Minimum Spanning TreesFormal Definition of MSTFigure1 : Examples of MSTSteiner Minimum Trees (SMT)Generic ApproachesFacts about (Free) TreesIntuition Behind Greedy MSTGeneric-MST (G, w)DefinitionsWhen is an Edge Safe?MST LemmaProof of MST LemmaFigureStep 1Constructing T’Conclusion of ProofMST Lemma: RepriseBasics of Kruskal’s AlgorithmDetecting a CycleMST-Kruskal(G, w)Example: Kruskal’s AlgorithmAnalysis of KruskalCorrectness of KruskalIntuition behind Prim’s AlgorithmBasics of Prim ’s AlgorithmImplementation: Priority QueueExample: Prim’s AlgorithmMST-Prim(G, w, r)Analysis of PrimCorrectness of PrimMSTMSTMany of the slides are from Prof. Plaisted’s resources at University of North Carolina at Chapel HillMotivation: Minimum Spanning TreesTo minimize the length of a connecting network, it never pays to have cycles.The resulting connection graph is connected, undirected, and acyclic, i.e., a free tree (sometimes called simply a tree).This is the minimum spanning tree or MST problem.Formal Definition of MSTGiven a connected, undirected, graph G = (V, E), a spanning tree is an acyclic subset of edges T E that connects all the vertices together. Assuming G is weighted, we define the cost of a spanning tree T to be the sum of edge weights in the spanning treew(T) = (u,v)T w(u,v)A minimum spanning tree (MST) is a spanning tree of minimum weight.Figure1 : Examples of MSTNot only do the edges sum to the same value, but the same set of edge weights appear in the two MSTs. NOTE: An MST may not be unique.Steiner Minimum Trees (SMT)Given a undirected graph G = (V, E) with edge weights and a subset of vertices V’ V, called terminals. We wish to compute a connected acyclic subgraph of G that includes all terminals. MST is just a SMT with V’ =V.Figure 2: Steiner Minimum TreeGeneric ApproachesTwo greedy algorithms for computing MSTs:»Kruskal’s Algorithm »Prim’s AlgorithmFacts about (Free) TreesA tree with n vertices has exactly n-1 edges (|E| = |V| - 1)There exists a unique path between any two vertices of a treeAdding any edge to a tree creates a unique cycle; breaking any edge on this cycle restores a treeFor details see CLRS Appendix B.5Intuition Behind Greedy MSTWe maintain in a subset of edges A, which will initially be empty, and we will add edges one at a time, until equals the MST. We say that a subset A E is viable if A is a subset of edges in some MST. We say that an edge (u,v) E-A is safe if A{(u,v)} is viable. Basically, the choice (u,v) is a safe choice to add so that A can still be extended to form an MST. Note that if A is viable it cannot contain a cycle. A generic greedy algorithm operates by repeatedly adding any safe edge to the current spanning tree.Generic-MST (G, w)1. A // A trivially satisfies invariant// lines 2-4 maintain the invariant2. while A does not form a spanning tree3. do find an edge (u,v) that is safe for A4. A A {(u,v)}5. return A // A is now a MSTDefinitionsA cut (S, V-S) is just a partition of the vertices into 2 disjoint subsets. An edge (u, v) crosses the cut if one endpoint is in S and the other is in V-S. Given a subset of edges A, we say that a cut respects A if no edge in A crosses the cut.An edge of E is a light edge crossing a cut, if among all edges crossing the cut, it has the minimum weight (the light edge may not be unique if there are duplicate edge weights).When is an Edge Safe?If we have computed a partial MST, and we wish to know which edges can be added that do NOT induce a cycle in the current MST, any edge that crosses a respecting cut is a possible candidate.Intuition says that since all edges crossing a respecting cut do not induce a cycle, then the lightest edge crossing a cut is a natural choice.MST LemmaLet G = (V, E) be a connected, undirected graph with real-value weights on the edges. Let A be a viable subset of E (i.e. a subset of some MST), let (S, V-S) be any cut that respects A, and let (u,v) be a light edge crossing this cut. Then, the edge is safe for A.Proof of MST LemmaMust show that A {(u,v)} is a subset of some MSTMethod:1. Find arbitrary MST T containing A2. Use a cut-and-paste technique to find another MST T that contains A {(u,v)}This cut-and-paste idea is an important proof techniqueFigureFigure 3: MST LemmaStep 1Let T be any MST for G containing A.»We know such a tree exists because A is viable.If (u, v) is in T then we are done.Constructing T’If (u, v) is not in T, then add it to T, thus creating a cycle. Since u and v are on opposite sides of the cut, and since any cycle must cross the cut an even number of times, there must be at least one other edge (x, y) in T that crosses the cut.The edge (x, y) is not in A (because the cut respects A). By removing (x,y) we restore a spanning tree, T’.Now must show»T’ is a minimum spanning tree»A {(u,v)} is a subset of T’Conclusion of ProofT’ is an MST: We have w(T’) = w(T) - w(x,y) + w(u,v) Since (u,v) is a light edge crossing the cut, we have w(u,v) w(x,y). Thus w(T’) w(T). So T’ is also a minimum spanning tree.A {(u,v)} T’: Remember that (x, y) is not in A. Thus A T - {(x, y)}, and thus A {(u,v)} T - {(x, y)} {(u,v)} = T’MST Lemma: RepriseLet G = (V, E) be a connected, undirected graph with real-value weights on the edges. Let A be a viable subset of E (i.e. a subset of some MST), let (S, V-S) be any cut that respects A, and let (u,v) be a light edge crossing this cut. Then, the edge is safe for A.Point of Lemma: Greedy strategy works!Basics of Kruskal’s AlgorithmAttempts to add edges to A in increasing order of weight (lightest edge first) »If the next edge does not induce a cycle among the current set of edges, then it is added to A.»If it does, then this edge is passed over, and we consider the next edge in order.»As this algorithm runs, the edges of A will induce a forest on the vertices and the trees of this forest are merged together until we have a single tree containing all vertices.Detecting a CycleWe can perform a DFS on subgraph induced by the edges of A, but this takes too much time.Use “disjoint set UNION-FIND” data structure. This data structure supports 3 operations:Create-Set(u): create a set containing u.Find-Set(u): Find the set that contains
View Full Document