Kruskal for maximum spanning forest: Difference between revisions
No edit summary |
|||
Line 59: | Line 59: | ||
==Complexity== | ==Complexity== | ||
'''Statement:''' The asymptotic complexity is in <math>\mathcal{O}(m \cdot T(n) + | '''Statement:''' The asymptotic complexity is in <math>\mathcal{O}(m \cdot ( T(n) + \log m )</math> in the worst case, where <math>n = \vert V \vert</math> and <math>m = \vert E \vert</math>, and <math>T(n)</math> is the complexity of a single [[union-find]] operation. | ||
'''Proof:''' The summand <math>\mathcal{O}(m\cdot log\cdot m)</math> results from sorting all edges in the preprocessing. The main loop has <math>m</math> iterations, and each iteration takes <math>\mathcal{O}(T(n))</math> time. | '''Proof:''' The summand <math>\mathcal{O}(m\cdot log\cdot m)</math> results from sorting all edges in the preprocessing. The main loop has <math>m</math> iterations, and each iteration takes <math>\mathcal{O}(T(n))</math> time. |
Revision as of 18:11, 18 September 2015
General information
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