Upload
chandan-choudhury
View
214
Download
0
Embed Size (px)
Citation preview
7/31/2019 MST.ppt_0
1/31
MST
MST
Many of the slides are from Prof. Plaisteds resources at University of North Carolina at Chapel Hill
7/31/2019 MST.ppt_0
2/31
Motivation: Minimum
Spanning Trees
To minimize the length of a connecting network, it
never pays to have cycles.
The resulting connection graph is connected,
undirected, and acyclic, i.e., afree tree (sometimes
called simply a tree).
This is the minimum spanning tree orMST
problem.
7/31/2019 MST.ppt_0
3/31
Formal Definition of MST
Given a connected, undirected, graph G = (V, E), a
spanning tree is an acyclic subset of edges T Ethat
connects all the vertices together.
Assuming G is weighted, we define the costof a spanning
tree Tto be the sum of edge weights in the spanning tree
w(T) = (u,v)Tw(u,v)
A minimum spanning tree (MST) is a spanning tree of
minimum weight.
7/31/2019 MST.ppt_0
4/31
Figure1 : Examples of MST
Not 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.
7/31/2019 MST.ppt_0
5/31
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 ofG that includes all
terminals. MST is just a SMT with V=V.
Figure 2: Steiner Minimum Tree
7/31/2019 MST.ppt_0
6/31
Generic Approaches
Two greedy algorithms for computing MSTs:
Kruskals Algorithm
Prims Algorithm
7/31/2019 MST.ppt_0
7/31
Facts about (Free) Trees
A tree with n vertices has exactly n-1 edges
(|E| = |V| - 1)
There exists a unique path between any two vertices of a
tree
Adding any edge to a tree creates a unique cycle; breaking
any edge on this cycle restores a tree
For details see CLRS Appendix B.5
7/31/2019 MST.ppt_0
8/31
Intuition Behind Greedy MST
We maintain in a subset of edgesA, which will initiallybe empty, and we will add edges one at a time, until
equals the MST. We say that a subsetA Eis viable ifA
is a subset of edges in some MST. We say that an edge
(u,v) E-A issafe ifA{(u,v)} is viable. Basically, the choice (u,v) is a safe choice to add so thatA
can still be extended to form an MST. Note that ifA is
viable it cannot contain a cycle. A generic greedyalgorithm operates by repeatedly adding anysafe edge to
the current spanning tree.
7/31/2019 MST.ppt_0
9/31
Generic-MST (G, w)
1. A //A trivially satisfies invariant
// lines 2-4 maintain the invariant
2. whileA does not form a spanning tree
3. do find an edge (u,v) that is safe forA
4. AA {(u,v)}
5. returnA //A is now a MST
7/31/2019 MST.ppt_0
10/31
Definitions
A 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 Sand the other is in V-S. Given a subset of
edges A, we say that a cut respects A if no edge in Acrosses the cut.
An edge ofEis 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).
7/31/2019 MST.ppt_0
11/31
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.
7/31/2019 MST.ppt_0
12/31
MST Lemma
Let G = (V, E)be a connected, undirected graph
with real-value weights on the edges. LetA be a
viable subset ofE(i.e. a subset of some MST), let
(S, V-S) be any cut that respectsA, and let (u,v)
be a light edge crossing this cut. Then, the edge
is safe forA.
7/31/2019 MST.ppt_0
13/31
Proof of MST Lemma
Must show thatA {(u,v)} is a subset of some
MST
Method:
1. Find arbitrary MST TcontainingA
2. Use a cut-and-paste technique to find another MST T
that containsA {(u,v)}
This cut-and-paste idea is an important proof
technique
7/31/2019 MST.ppt_0
14/31
Figure
Figure 3: MST Lemma
7/31/2019 MST.ppt_0
15/31
Step 1
Let Tbe any MST forG containingA.
We know such a tree exists becauseA is viable.
If(u, v)is in Tthen we are done.
7/31/2019 MST.ppt_0
16/31
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 Tthatcrosses the cut.
The edge (x, y)is not inA (because the cut respectsA).By removing (x,y) we restore a spanning tree, T.
Now must show
Tis a minimum spanning tree
A {(u,v)} is a subset ofT
7/31/2019 MST.ppt_0
17/31
Conclusion of Proof
Tis 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 Tis also a
minimum spanning tree.
A {(u,v)} T: Remember that (x, y) is not inA. Thus
A T - {(x, y)}, and thus
A {(u,v)} T - {(x, y)} {(u,v)} = T
7/31/2019 MST.ppt_0
18/31
MST Lemma: Reprise
Let G = (V, E)be a connected, undirected graph with
real-value weights on the edges. LetA be a viable subset
ofE(i.e. a subset of some MST), let (S, V-S) be any cut
that respectsA, and let (u,v) be a light edge crossing thiscut. Then, the edge is safe forA.
Point of Lemma: Greedy strategy works!
7/31/2019 MST.ppt_0
19/31
Basics of Kruskals Algorithm
Attempts to add edges toA 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 ofA will induce a forest
on the vertices and the trees of this forest are mergedtogether until we have a single tree containing all vertices.
7/31/2019 MST.ppt_0
20/31
Detecting a Cycle
We can perform a DFS on subgraph induced by the edgesofA, 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 u.Union(u, v): Merge the sets containing u and v.
Each can be performed in O(lg n) time.
The vertices of the graph will be elements to be stored inthe sets; the sets will be vertices in each tree ofA (storedas a simple list of edges).
7/31/2019 MST.ppt_0
21/31
MST-Kruskal(G, w)
1. A // initially A is empty
2. for each vertex v V[G] // line 2-3 takes O(|V|) time
3. do Create-Set(v) // create set for each vertex
4. sort the edges ofEby nondecreasing weight w5. for each edge (u,v) E, in order bynondecreasing weight
6. do if Find-Set(u) Find-Set(v) // u&v on different trees
7. then AA {(u,v)}
8. Union(u,v)
9. returnA
Total running time is O(|E| lg |E|)=O(|E| lg |V|).
7/31/2019 MST.ppt_0
22/31
Example: Kruskals Algorithm
Figure 4: Kruskals Algorithm
7/31/2019 MST.ppt_0
23/31
Analysis of Kruskal
Lines 1-3 (initialization): O(V)
Line 4 (sorting): O(E lg E)
Lines 6-8 (set-operation): O(E log E)
Total: O(E log E)
7/31/2019 MST.ppt_0
24/31
Correctness of Kruskal
Idea: Show that every edge added is a safe edge forA
Assume (u, v) is next edge to be added toA. Will not create a cycle
LetAdenote the tree of the forestA that containsvertex u. Consider the cut (A, V-A). This cut respectsA (why?)
and (u, v) is the light edge across the cut (why?)
Thus, by the MST Lemma, (u,v) is safe.
7/31/2019 MST.ppt_0
25/31
Intuition behind Prims Algorithm
Consider the set of vertices Scurrently part of the tree, andits complement (V-S). We have a cut of the graph and the
current set of tree edgesA is respected by this cut.
Which edge should we add next? Light edge!
7/31/2019 MST.ppt_0
26/31
Basics of Prim s Algorithm
It works by adding leaves on at a time to the current
tree.
Start with the root vertex r(it can be any vertex).At any
time, the subset of edgesA forms a single tree. S= vertices
ofA.
At each step, a light edge connecting a vertex in Sto a
vertex in V- Sis added to the tree.
The tree grows until it spans all the vertices in V.
Implementation Issues:
How to update the cut efficiently?
How to determine the light edge quickly?
7/31/2019 MST.ppt_0
27/31
Implementation: Priority Queue
Priority queue implemented using min-heap can support
the following operations in O(lg n) time: Insert (Q, u, key): Insert u with the key value key in Q
u = Extract_Min(Q): Extract the item with minimum key value
in Q
Decrease_Key(Q, u, new_key): Decrease the value ofus key
value to new_key
All the vertices that are notin the S(the vertices of the
edges inA) reside in a priority queue Q based on a keyfield. When the algorithm terminates, Q is empty. A =
{(v, [v]): v V - {r}}
7/31/2019 MST.ppt_0
28/31
Example: Prims Algorithm
7/31/2019 MST.ppt_0
29/31
MST-Prim(G, w, r)
1. QV[G]
2. for each vertex u Q // initialization: O(V) time
3. do key[u]
4. key[r]0 // start at the root
5. [r]NIL // set parent ofrto be NIL
6. while Q // until all vertices in MST
7. do uExtract-Min(Q) // vertex with lightest edge
8. for each v adj[u]
9. do ifv Q and w(u,v) < key[v]
10. then [v]u
11. key[v]w(u,v) // new lighter edge out ofv
12. decrease_Key(Q, v, key[v])
7/31/2019 MST.ppt_0
30/31
Analysis of Prim
Extracting the vertex from the queue: O(lg |V|)
For each incident edge, decreasing the key of the
neighboring vertex: O(lg |V|)
The other steps are constant time.
The overall running time is, where
T(n) = uV(lg |V| + deg(u)lg |V|)
= uV(1+ deg(u))lg |V|
= lg |V| (|V| + 2|E|) = O(|E| lg |V|)
Essentially same as Kruskals
If use Fibonacci Heap, O(|E| + |V| lg |V|).
7/31/2019 MST.ppt_0
31/31
Correctness of Prim
Again, show that every edge added is a safe edge forA
Assume (u, v) is next edge to be added toA.
Consider the cut (A, V-A).
This cut respectsA (why?) and (u, v) is the light edge across the cut (why?)
Thus, by the MST Lemma, (u,v) is safe.