So, I read the Kruskal algorithm from CLRS, and guess what? there are a lotta things to be remembered!
So, the implementation (pseudo code) of this algorithm is something like:
MST-KRUSKAL($G$,$w$)
Implementation of sets is done via disjoint-set forests (section 21.3), which use union-by-rank and path-compression optimizations, to get a total running time of $O(m \alpha(n))$ where, $m$ is the total number of MAKE-SET, FIND-SET and UNION operations, and $n$ are the total MAKE-SET operations (which, obviously are always less than $m$).So, the implementation (pseudo code) of this algorithm is something like:
MST-KRUSKAL($G$,$w$)
- $A = \phi$
- for each vertex $v \in G.V$
- MAKE-SET($v$)
- sort the edges of $G.E$ into nondecreasing order by weight $w$
- for each edge $(u,v) \in G.E$, taken in nondecreasing order by weight
- if FIND-SET($u$) $\neq$ FIND-SET($v$)
- $A = A \cup \{(u,v)\}$
- UNION$(u,v)$
- return $A$
Now, Kruskal algorithm basically connects two components of the graph $G$, via a light edge. This light edge is obviously safe to add, by using the corollary 23.2. So, initially there are $|V|$ trees, and at the end of the Algorithm, there is just one tree left, connecting all the vertices.
A word about the running time:
- Sorting in line 4 takes $O(E log E)$ time
- Since there are $|V|$ MAKE-SET operations ($n = |V|$), and $m = |V|+|E|$(not considering the constants multiplied to $|E|$), therefore running time comes out to be $$(|V|+|E|)\alpha(|V|)\\ = |E| \alpha(|V|)$$
- So, the total running time is $$O(|E|log|E|+|E|\alpha(|V|)) \\= O(|E|log|E|)$$
No comments:
Post a Comment