Kruskal for maximum spanning forest
Algorithmic problem: Maximum spanning forest
Prerequisites:
Type of algorithm: loop
Auxiliary data:
- A sorted sequence
whose items are the edges of . The edges are sorted in descending order, that is, the edge with the largest value of is the first element of the sequence (ties arbitrarily broken). Below, denotes the -th edge in . - A union-find data structure
on .
Abstract view
Invariant: After
- The undirected graph
, where is the set of all selected edges immediately after the -th iteration, is cycle-free; in particular, it is maximally cycle-free in the sense that, for , adding an edge would close a cycle in . - For the undirected graph
, there is a maximum spanning forest of such that . - The sets of
are the connected components of .
Variant:
Break condition:
Induction basis
Abstract view:
is initialized by , so each node is a singleton in .
Implementation: Obvious.
Proof: Nothing to show.
Induction step
Abstract view: If inserting
Implementation:
- Let
denote the endnodes of , that is, . - If
and belong to different sets in :- Insert
in the result. - Unite the sets of
and in .
- Insert
Correctness: The first and third invariants are obvious. So we focus on the second invariant. We may safely set
- either
and is inserted in the -th iteration - or
and is rejected in the -th iteration.
Note that the case that
As
Complexity
Statement: The asymptotic complexity is in
Proof: The summand