1 / 20

Introduction to Algorithms

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.

bonnie
Télécharger la présentation

Introduction to Algorithms

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Introduction to Algorithms Minimum Spanning Trees My T. Thai @ UF

  2. 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

  3. 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

  4. 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

  5. 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

  6. 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

  7. 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

  8. 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

  9. 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

  10. 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

  11. 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

  12. Example My T. Thai mythai@cise.ufl.edu

  13. My T. Thai mythai@cise.ufl.edu

  14. 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

  15. 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

  16. 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

  17. Example My T. Thai mythai@cise.ufl.edu

  18. My T. Thai mythai@cise.ufl.edu

  19. 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

  20. 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

More Related