Kruskal's Algorithm

repeat

find a light edge (u,v) between two unconnected components

A = A {(u,v)}

until all edges have been considered

- Sort the edges by weight
- Use disjoint sets for speed (union by rank and path compression)

MST-Kruskal(G, w) | ; G = (V, E) | |

1 | A = {} | |

2 | foreach v in V | ; O(V) |

3 | MakeSet(v) | |

4 | sort edges E by nondecreasing weight w | ; O(E lg E) |

5 | foreach edge (u,v) in E, in order | ; m = |E| operations |

6 | if FindSet(u) FindSet(v) | ; n = |V| keys |

7 | then A = A {(u,v)} | ; O(m (m,n)) |

8 | Union(u,v) | ; O(E (E,V)) |

9 | return A | ; (E,V) = O(lg E) |

T(V,E) = O(V) + O(E lg E) + O(E lg E), V = O(E)

= O(E lg E)