Applications of shortest

MST Lemma

Let G = (V, E) be a connected, undirected graph with
real-value weights on the edges. Let A be a viable
subset of E (i.e. a subset of some MST), let (S, V-S) be
any cut that respects A, and let (u,v) be a light edge
crossing this cut. Then, the edge is safe for A.
 Point of Lemma: Greedy strategy works!
Basics of Kruskal’s Algorithm

It works by attempting to add edges to the A 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 of A will induce a
forest on the vertices and the trees of this forest are
merged together until we have a single tree containing
all vertices.
Detecting a Cycle



We can perform a DFS on subgraph induced by the edges of A,
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 in the sets;
the sets will be vertices in each tree of A (stored as a simple list of
edges).
MST-Kruskal(G, w)
1.
2.
3.
4.
5.
6.
7.
8.
9.
A
// initially A is empty
for each vertex v  V[G]
// line 2-3 takes O(V) time
do Create-Set(v)
// create set for each vertex
sort the edges of E by nondecreasing weight w
for each edge (u,v)  E, in order by nondecreasing weight
do if Find-Set(u)  Find-Set(v) // u&v on different trees
then A  A  {(u,v)}
Union(u,v)
return A
Total running time is O(E lg E).
Example: Kruskal’s Algorithm
Figure 4: Kruskal’s Algorithm
Analysis of Kruskal’s

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)
Correctness

Consider the edge (u, v) that the algorithm seeks to
add next, and suppose that this edge does not
induce a cycle in A. Let A’ denote the tree of the
forest A that contains vertex u. Consider the cut
(A’, V-A’). Every edge crossing the cut is not in A,
and so this cut respects A, and (u, v) is the light
edge across the cut (because any lighter edge would
have been considered earlier by the algorithm).
Thus, by the MST Lemma, (u,v) is safe.
Intuition behind Prim’s Algortihm

Consider the set of vertices S currently part of the
tree, and its complement (V-S). We have a cut of the
graph and the current set of tree edges A is respected
by this cut.
 Which edge should we add next? Light edge!
Basics of Prim ’s Algorithm

It works by building the tree up 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 edges A forms a single tree. S =
vertices of A.
– At each step, a light edge connecting a vertex in S to a
vertex in V- S is 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?
Implementation: Priority Queue

Priority queue implemented using 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 of u’s key
value to new_key

All the vertices that are not in the S (the vertices of
the edges in A) reside in a priority queue Q based on a
key field. When the algorithm terminates, Q is empty.
A = {(v, [v]): v  V - {r}}
MST-Prim(G, w, r)
1. Q  V[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 of r to be NIL
6. while Q  
// until all vertices in MST
7.
do u  Extract-Min(Q)
// vertex with lightest edge
8.
for each v  adj[u]
9.
do if v  Q and w(u,v) < key[v]
10.
then [v]  u
11.
key[v]  w(u,v) // new lighter edge out of v
12.
decrease_Key(Q, v, key[v])
Example: Prim’s Algorithm
Analysis of Prim’s

Extracting the vertex from the queue: O(lg n)

For each incident edge, decreasing the key of the
neighboring vertex: O(lg n) where n = |V|

The other steps are constant time.

The overall running time is, where e = |E|
T(n) = uV(lg n + deg(u) lg n) = uV (1+ deg(u)) lg n
= lg n (n + 2e) = O((n + e) lg n)
Essentially same as Kruskal’s: O((n+e) lg n) time