Introduction to Algorithms Minimum Spanning Trees My T. Thai @ UF
Jan 12, 2016
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. [email protected]
2
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)T w(u,v)
My T. [email protected]
3
Bold edges form a Minimum Spanning Tree
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:
My T. [email protected]
4
Key problem: How to find safe edges?
Note: MST has |V|-1 edges
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. [email protected]
5
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. [email protected]
6
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. [email protected]
7
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. [email protected]
8
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. [email protected]
9
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 componentMy T. Thai
10
Pseudo code
Running time: O(E lg V) ( is E is sorted) First for loop: |V| MAKE-SETs Sort E: O(E lg E) - O(E lg V) Second for loop: (o(E log V) (chapter 21)
My T. [email protected]
11
My T. [email protected]
13
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. [email protected]
14
Prim’s Algorithm
Uses a priority queue Q to find a light edge quickly
Each object in Q is a vertex in V - VA
Key of v is minimum weight of any edge (u, v), where u VA
Then the vertex returned by Extract-Min is v such that there exists u VA and (u, v) is light edge crossing (VA, V – VA)
Key of v is if v is not adjacent to any vertex in VA My T. Thai
15
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
16
My T. [email protected]
18
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. [email protected]
19
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. [email protected]
20