4.4 ***Kruskal and other algorithms ***

Kruskal’s algorithm : Input graph weighted connected Graph G=(V,E,w).

  • Step 1:

    Initialize with D=∅⊂E and M=(V⁢(G),∅).

  • Step 2:

    Select one of the smallest (in terms of weight) edges in E−D. Call it e.

  • Step 3:

    If M∪e does not create a cycle, set M=M∪e.

  • Step 4:

    Set D=D∪e. If |E⁢(M)|<n−1 and D≠E, go to Step 2 else to Step 5.

  • Step 5:

    Output M.

Theorem 4.20.

The output of the Kruskal’s algorithm is a minimal spanning tree if G is connected.

Proof.

The proof consists of two steps - Firtly, we will show that the output M is a spanning tree and then show it is of minimum weight.

Step 1 : M is a spanning tree. Clearly M does not contain a cycle. If M has at least two components C1,C2, then let e an edge with minimal weight between C1 and C2. e exists as G is connected. e would have been added in Step 2 when it was considered. Thus, we derive a contradiction to the fact that M is disconnected. Hence M is connected and acyclic, i.e., a spanning tree.

Step 2 : M has minimum weight. We shall show that the following claim holds by induction : At every stage of the algorithm, there is a minimal spanning tree T such that M⊂T.

The above claim suffices because at the last step, the output M is the M at the end of Step 3 and if it is a subgraph of a spanning tree T, then it must be that M=T as M is a spanning tree from Step 1.

Induction argument : The claim holds in the beginning because M=∅. Assume that the claim holds at some stage of the algorithm for a M and a T.

Now, suppose that the next chosen edge creates a cycle, then M does not change and the claim still holds.

So, let the next chosen edge not create a cycle and hence M becomes M+e. If e∈T, we are done. Suppose e∉T. Then T+e has a cycle C. This cycle contains edges which do not belong to M, since e does not form a cycle in M but does in T. Note that, there exists f∈C−M which has not been considered by the algorithms before and hence it must have weight at least as large as e. If f had been considered before e in the algorithm, M−e+f will not contain a cycle (as M−e+f⊂T) and so f would have been added to M already.

Then T−f+e is a tree by the insertion property and it has the same or less weight as T. If w⁢(f)=w⁢(e), then T−f+e is a minimum spanning tree containing M+e and again the claim holds. If w⁢(f)>w⁢(e), then T−f+e is a spanning tree of strictly smaller weight than T and leads to a contradiction. ∎

Prim-Dijkstra-Jarnik’s algorithm : Input graph weighted connected Graph G=(V,E,w).

  • Step 1:

    Initialize with M=D=∅ and S={v},T=V−S for some v∈V.

  • Step 2:

    Select one of the smallest (in terms of weight) edges in E∩(S×T). Call it e and M=M∪e.

  • Step 3:

    Say e=(v1,v2)∈S×T then set S=S∪{v2} and T=V−S.

  • Step 4:

    If T≠∅, then go to Step 2 else to Step 5.

  • Step 5:

    Output M.

Theorem 4.21.

The output of Prim-Dijkstra-Jarnik’s algorithm is a minimal spanning tree if G is connected.

Proof.

Denote the output by M~. The proof of the fact that M~ is a spanning tree is left as an exercise as the steps were sketched in the class. W e shall only show minimality here.

Claim : As with the proof of Kruskal’s algorithm, we shall show that at every step M⊂M∗ with the latter being a MST.

The claim is trivially true at the initial step when M=∅ and since MST exists. Suppose the claim is true upto some step with M,T,S already determined i.e., M⊂M∗, a MST.

Let e=(u,v) be the edge chosen by the Prim’s algorithm at this stage and suppose e∉M∗. Then, there exists a path P from u to v in M∗. This implies that there exists an edge e′∈P∩(T×S). But since Prim’s algorithm selected e instead of e′, we have that w⁢(e)≤w⁢(e′). But since P+e is a cycle in M∗+e, we can remove any edge from the cycle and still get a spanning tree. Thus, M∗+e−e′ is also a spanning tree and since M∗ is a MST, we have that w⁢(e)≥w⁢(e′). Thus, we get that w⁢(e)=w⁢(e′) and M+e⊂M∗+e−e′, a MST. This proves the claim and completes the proof. ∎

Exercise* 4.22.

The multi-set of weights for a minimal spanning tree is unique i.e., for any s∈ℝ and any two MSTs M,M′, we have that |{e∈M:w⁢(e)=s}|=|{e∈M′:w⁢(e)=s}|.

Exercise 4.23 (Minimal Spanning Forests).

Define and suitably extend these notions to minimal spanning forests.

Exercise* 4.24.

Given a connected graph (G,E) with non-negative edge weights w satisfying the following conditions : w⁢(u,v)=∞ if (u,v)∉E and w⁢(u,v)>0 if u∼v. Fixing a starting vertex x, show that the following algorithm computes the shortest paths from x to all other vertices in G.

  1. Step 1 :

    Let S={x},t⁢(x)=0,t⁢(u)=w⁢(u,x) for u∉S.

  2. Step 2 :

    Select a u∉S such that t⁢(u)=minz∉S⁡t⁢(z). Change S to S∪{u} and update t⁢(z) for z∉S to min⁡{t⁢(z),t⁢(u)+w⁢(u,z)}.

  3. Step 3 :

    Continue Step 2 until S=V⁢(G) and set d⁢(u,x)=t⁢(u) for all u.

Show that d⁢(u,v)=dG⁢(u,v).