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