210 likes | 371 Views
Introduction to Algorithms. Minimum Spanning Trees My T. Thai @ UF. Problem. Find a low cost network connecting a set of locations Any pair of locations are connected There is no cycle Some applications: Communication networks Circuit design …. Minimum Spanning Tree (MST) Problem.
E N D
Introduction to Algorithms Minimum Spanning Trees My T. Thai @ UF
Problem • Find a low cost network connecting a set of locations • Any pair of locations are connected • There is no cycle • Some applications: • Communication networks • Circuit design • … My T. Thai mythai@cise.ufl.edu
Minimum Spanning Tree (MST) Problem • Input: Undirected, connected graph G=(V, E), each edge (u, v) E has weight w(u, v) • Output: acyclic subset T E that connects all of the vertices with minimum total weight w(T) = (u,v)Tw(u,v) Bold edges form a Minimum Spanning Tree My T. Thai mythai@cise.ufl.edu
Growing a minimum spanning tree • Suppose A is a subset of some MST • Iteratively add safe edge (u,v) s.t.A {(u,v)} is still a subset of some MST • Generic algorithm: Key problem: How to find safe edges? Note: MST has |V|-1 edges My T. Thai mythai@cise.ufl.edu
Some definitions • A cut (S, V - S) is a partition of vertices into disjoint sets S and V - S • An edge crosses the cut (S, V - S) if it has one end point in S, one end point in V - S • A cut respects a set A of edges if and only if no edge in A crosses the cut, e.g. A is the set of bold edges My T. Thai mythai@cise.ufl.edu
Some definitions • An edge is a light edge crossing a cut if and only if its weight is minimum over all edges crossing the cut, e.g. edge (c, d) • Observation: Any MST has at least one edge connect S and V – S => one cross edge is safe for A My T. Thai mythai@cise.ufl.edu
Find a safe edge Proof: Let T be a MST that includes A • Case 1: (u, v) T => done. • Case 2: (u, v) not in T: • Exist edge (x, y) T cross the cut, (x, y) A • Removing (x, y) breaks T into two components. • Adding (u, v) reconnects 2 components • T´ = T - {(x, y)} {(u, v)} is a spanning tree • w(T´) = w(T) - w(x, y) + w(u, v) w(T) => T’ is a MST => done My T. Thai mythai@cise.ufl.edu
Corollary • In GENERIC-MST • A is a forest containing connected components. Initially, each component is a single vertex. • Any safe edge merges two of these components into one. Each component is a tree. My T. Thai mythai@cise.ufl.edu
Kruskal’s Algorithm • Starts with each vertex in its own component • Repeatedly merges two components into one by choosing a light edge that connects them (i.e., a light edge crossing the cut between them) • Scans the set of edges in monotonically increasing order by weight. • Uses a disjoint-set data structure to determine whether an edge connects vertices in different components My T. Thai mythai@cise.ufl.edu
Disjoint-set data structure • Maintain collection S = {S1, . . . , Sk} of disjoint dynamic (changing over time) sets • Each set is identified by a representative, which is some member of the set • Operations: • MAKE-SET(x): make a new set Si = {x}, and add Si to S • UNION(x, y): if x ∈ Sx , y ∈ Sy, then S ← S − Sx − Sy ∪ {Sx∪Sy} • Representative of new set is any member of Sx ∪ Sy, often the representative of one of Sx and Sy. • Destroys Sx and Sy (since sets must be disjoint). • FIND-SET(x): return representative of set containing x • In Kruskal’s Algorithm, each set is a connected component My T. Thai mythai@cise.ufl.edu
Pseudo code • Running time: O(ElgV) ( is E is sorted) • First for loop: |V| MAKE-SETs • Sort E: O(ElgE) - O(ElgV) • Second for loop: (o(E log V) (chapter 21) My T. Thai mythai@cise.ufl.edu
Example My T. Thai mythai@cise.ufl.edu
My T. Thai mythai@cise.ufl.edu
Prim’s Algorithm • Builds one tree, so A always a tree • Starts from an arbitrary “root” r • At each step, find a light edge crossing cut (VA, V − VA), where VA = vertices that A is incident on. Add this edge to A. My T. Thai mythai@cise.ufl.edu
Prim’s Algorithm • Uses a priority queue Qto find a light edge quickly • Each object in Q is a vertex in V - VA • Key of vis minimum weight of any edge (u, v), where u VA • Then the vertex returned by Extract-Min is vsuch that there exists u VA and (u, v)is light edge crossing (VA, V – VA) • Key of vis if vis not adjacent to any vertex in VA My T. Thai mythai@cise.ufl.edu
Running time:O(E lgV) • Using binary heaps to implement Q • Initialization: O(V) • Building initial queue : O(V) • V Extract-Min’s : O(V lgV) • E Decrease-Key’s : O(E lgV) Note: Using Fibonacci heaps can save time of Decrease-Key operations to constant (chapter 19) => running time: O(E + V lg V) My T. Thai mythai@cise.ufl.edu
Example My T. Thai mythai@cise.ufl.edu
My T. Thai mythai@cise.ufl.edu
Summary • MST T of connected undirect graph G = (V, E): • Is a subgraph of G • Connected • Has V vertices, |V| -1 edges • There is exactly 1 path between a pair of vertices • Deleting any edge of T disconnects T • Kruskal’s algorithm connects disjoint sets of connects vertices until achieve a MST • Run nearly linear time if E is sorted: My T. Thai mythai@cise.ufl.edu
Summary • Prim’s algorithm starts from one vertex and iteratively add vertex one by one until achieve a MST • Faster than Kruskal’s algorithm if the graph is dense O(E + V lg V) vs O(E lg V) My T. Thai mythai@cise.ufl.edu