]> mj.ucw.cz Git - saga.git/blob - mst.tex
Fibonacci heaps.
[saga.git] / mst.tex
1 \ifx\endpart\undefined
2 \input macros.tex
3 \fi
4
5 \chapter{Minimum Spanning Trees}
6
7 \section{The Problem}
8
9 The problem of finding a minimum spanning tree of a weighted graph is one of the
10 best studied problems in the area of combinatorial optimization since its birth.
11 Its colorful history (see \cite{graham:msthistory} and \cite{nesetril:history} for the full account)
12 begins in~1926 with the pioneering work of Bor\o{u}vka
13 \cite{boruvka:ojistem}\foot{See \cite{nesetril:boruvka} for an English translation with commentary.},
14 who studied primarily an Euclidean version of the problem related to planning
15 of electrical transmission lines (see \cite{boruvka:networks}), but gave an efficient
16 algorithm for the general version of the problem. As it was well before the dawn of graph
17 theory, the language of his paper was complicated, so we will better state the problem
18 in contemporary terminology:
19
20 \proclaim{Problem}Given an undirected graph~$G$ with weights $w:E(G)\rightarrow {\bb R}$,
21 find its minimum spanning tree, defined as follows:
22
23 \defn\id{mstdef}%
24 For a given graph~$G$ with weights $w:E(G)\rightarrow {\bb R}$:
25 \itemize\ibull
26 \:A~subgraph $H\subseteq G$ is called a \df{spanning subgraph} if $V(H)=V(G)$.
27 \:A~\df{spanning tree} of $G$ is any its spanning subgraph which is a tree.
28 \:For any subgraph $H\subseteq G$ we define its \df{weight} $w(H):=\sum_{e\in E(H)} w(e)$.
29   When comparing two weights, we will use the terms \df{lighter} and \df{heavier} in the
30   obvious sense.
31 \:A~\df{minimum spanning tree (MST)} of~$G$ is a spanning tree~$T$ such that its weight $w(T)$
32   is the smallest possible of all the spanning trees of~$G$.
33 \:For a disconnected graph, a \df{(minimum) spanning forest (MSF)} is defined as
34   a union of (minimum) spanning trees of its connected components.
35 \endlist
36
37 Bor\o{u}vka's work was further extended by Jarn\'\i{}k \cite{jarnik:ojistem}, again in
38 mostly geometric setting, giving another efficient algorithm. However, when
39 computer science and graph theory started forming in the 1950's and the
40 spanning tree problem was one of the central topics of the flourishing new
41 disciplines, the previous work was not well known and the algorithms had to be
42 rediscovered several times.
43
44 Recently, several significantly faster algorithms were discovered, most notably the
45 $\O(m\beta(m,n))$-time algorithm by Fredman and Tarjan \cite{ft:fibonacci} and
46 algorithms with inverse-Ackermann type complexity by Chazelle \cite{chazelle:ackermann}
47 and Pettie \cite{pettie:ackermann}.
48
49 \FIXME{Write the rest of the history.}
50
51 This chapter attempts to survery the important algorithms for finding the MST and it
52 also presents several new ones.
53
54 %--------------------------------------------------------------------------------
55
56 \section{Basic Properties}
57
58 In this section, we will examine the basic properties of spanning trees and prove
59 several important theorems to base the algorithms upon. We will follow the theory
60 developed by Tarjan in~\cite{tarjan:dsna}.
61
62 For the whole section, we will fix a graph~$G$ with edge weights~$w$ and all
63 other graphs will be spanning subgraphs of~$G$. We will use the same notation
64 for the subgraphs as for the corresponding sets of edges.
65
66 First of all, let us show that the weights on edges are not necessary for the
67 definition of the MST. We can formulate an equivalent characterization using
68 an ordering of edges instead.
69
70 \defnn{Heavy and light edges}\id{heavy}%
71 Let~$T$ be a~spanning tree. Then:
72 \itemize\ibull
73 \:For vertices $x$ and $y$, let $T[x,y]$ denote the (unique) path in~$T$ joining $x$ and~$y$.
74 \:For an edge $e=xy$ we will call $T[e]:=T[x,y]$ the \df{path covered by~$e$} and
75   the edges of this path \df{edges covered by~$e$}.
76 \:An edge~$e$ is called \df{light with respect to~$T$} (or just \df{$T$-light}) if it covers a heavier edge, i.e., if there
77   is an edge $f\in T[e]$ such that $w(f) > w(e)$.
78 \:An edge~$e$ is called \df{$T$-heavy} if it is not $T$-light.
79 \endlist
80
81 \rem
82 Please note that the above properties also apply to tree edges
83 which by definition cover only themselves and therefore they are always heavy.
84
85 \lemman{Light edges}\id{lightlemma}%
86 Let $T$ be a spanning tree. If there exists a $T$-light edge, then~$T$
87 is not minimum.
88
89 \proof
90 If there is a $T$-light edge~$e$, then there exists an edge $e'\in T[e]$ such
91 that $w(e')>w(e)$. Now $T-e'$ is a forest of two trees with endpoints of~$e$
92 located in different components, so adding $e$ to this forest must restore
93 connectivity and $T':=T-e'+e$ is another spanning tree with weight $w(T')
94 = w(T)-w(e')+w(e) < w(T)$. Hence $T$ could not have been minimum.
95 \qed
96
97 \figure{mst2.eps}{278pt}{An edge exchange as in the proof of Lemma~\ref{lightlemma}}
98
99 The converse of this lemma is also true and to prove it, we will once again use
100 technique of transforming trees by \df{exchanges} of edges. In the proof of the
101 lemma, we have made use of the fact that whenever we exchange an edge~$e$ of
102 a spanning tree for another edge~$f$ covered by~$e$, the result is again
103 a spanning tree. In fact, it is possible to transform any spanning tree
104 to any other spanning tree by a sequence of exchanges.
105
106 \lemman{Exchange property for trees}\id{xchglemma}%
107 Let $T$ and $T'$ be spanning trees of a common graph. Then there exists
108 a sequence of edge exchanges which transforms $T$ to~$T'$. More formally,
109 there exists a sequence of spanning trees $T=T_0,T_1,\ldots,T_k=T'$ such that
110 $T_{i+1}=T_i - e_i + e_i^\prime$ where $e_i\in T_i$ and $e_i^\prime\in T'$.
111
112 \proof
113 By induction on $d(T,T'):=\vert T\symdiff T'\vert$. When $d(T,T')=0$,
114 both trees are identical and no exchanges are needed. Otherwise, the trees are different,
115 but as they are of the same size, there must exist an edge $e'\in T'\setminus T$.
116 The cycle $T[e']+e'$ cannot be wholly contained in~$T'$, so there also must
117 exist an edge $e\in T[e']\setminus T'$. Exchanging $e$ for~$e'$ yields a spanning
118 tree $T^*:=T-e+e'$ such that $d(T^*,T')=d(T,T')-2$ and we can apply the induction
119 hypothesis to $T^*$ and $T'$ to get the rest of the exchange sequence.
120 \qed
121
122 \figure{mst1.eps}{295pt}{One step of the proof of Lemma~\ref{xchglemma}}
123
124 \lemman{Monotone exchanges}\id{monoxchg}%
125 Let $T$ be a spanning tree such that there are no $T$-light edges and $T'$
126 be an arbitrary spanning tree. Then there exists a sequence of edge exchanges
127 transforming $T$ to~$T'$ such that the weight does not increase in any step.
128
129 \proof
130 We improve the argument from the previous proof, refining the induction step.
131 When we exchange $e\in T$ for $e'\in T'\setminus T$ such that $e\in T[e']$,
132 the weight never drops, since $e'$ is not a $T$-light edge and therefore
133 $w(e') \ge w(e)$, so $w(T^*)=w(T)-w(e)+w(e')\ge w(T)$.
134
135 To keep the induction going, we have to make sure that there are still no light
136 edges with respect to~$T^*$. In fact, it is enough to avoid such edges in
137 $T'\setminus T^*$, since these are the only edges considered by the induction
138 steps. To accomplish that, we replace the so far arbitrary choice of $e'\in T'\setminus T$
139 by picking the lightest such edge.
140
141 Now consider an edge $f\in T'\setminus T^*$. We want to show that $f$ is not
142 $T^*$-light, i.e., that it is heavier than all edges on $T^*[f]$. The path $T^*[f]$ is
143 either equal to the original path $T[f]$ (if $e\not\in T[f]$) or to $T[f] \symdiff C$,
144 where $C$ is the cycle $T[e']+e'$. The former case is trivial, in the latter one
145 $w(f)\ge w(e')$ due to the choice of $e'$ and all other edges on~$C$ are lighter
146 than~$e'$ as $e'$ was not $T$-light.
147 \qed
148
149 \thmn{Minimality by order}\id{mstthm}%
150 A~spanning tree~$T$ is minimum iff there is no $T$-light edge.
151
152 \proof
153 If~$T$ is minimum, then by Lemma~\ref{lightlemma} there are no $T$-light
154 edges.
155 Conversely, when $T$ is a spanning tree without $T$-light edges
156 and $T_{min}$ is an arbitrary minimum spanning tree, then according to the Monotone
157 exchange lemma (\ref{monoxchg}) there exists a non-decreasing sequence
158 of exchanges transforming $T$ to $T_{min}$, so $w(T)\le w(T_{min})$
159 and thus $T$~is also minimum.
160 \qed
161
162 In general, a single graph can have many minimum spanning trees (for example
163 a complete graph on~$n$ vertices and unit edge weights has $n^{n-2}$
164 minimum spanning trees according to the Cayley's formula \cite{cayley:trees}).
165 However, as the following theorem shows, this is possible only if the weight
166 function is not injective.
167
168 \thmn{MST uniqueness}%
169 If all edge weights are distinct, then the minimum spanning tree is unique.
170
171 \proof
172 Consider two minimum spanning trees $T_1$ and~$T_2$. According to the previous
173 theorem, there are no light edges with respect to neither of them, so by the
174 Monotone exchange lemma (\ref{monoxchg}) there exists a sequence of non-decreasing
175 edge exchanges going from $T_1$ to $T_2$. As all edge weights all distinct,
176 these edge exchanges must be in fact strictly increasing. On the other hand,
177 we know that $w(T_1)=w(T_2)$, so the exchange sequence must be empty and indeed
178 $T_1$ and $T_2$ must be identical.
179 \qed
180
181 \rem\id{edgeoracle}%
182 To simplify the description of MST algorithms, we will expect that the weights
183 of all edges are distinct and that instead of numeric weights (usually accompanied
184 by problems with representation of real numbers in algorithms) we will be given
185 a comparison oracle, that is a function which answers questions ``$w(e)<w(f)$?'' in
186 constant time. In case the weights are not distinct, we can easily break ties by
187 comparing some unique edge identifiers and according to our characterization of
188 minimum spanning trees, the unique MST of the new graph will still be a MST of the
189 original graph. In the few cases where we need a more concrete input, we will
190 explicitly state so.
191
192 \nota\id{mstnota}%
193 When $G$ is a graph with distinct edge weights, we will use $\mst(G)$ to denote
194 its unique minimum spanning tree.
195
196 Another useful consequence is that whenever two graphs are isomorphic and the
197 isomorphism preserves weight order, the isomorphism applies to their MST's
198 as well:
199
200 \defn
201 A~\df{monotone isomorphism} of two weighted graphs $G_1=(V_1,E_1,w_1)$ and
202 $G_2=(V_2,E_2,w_2)$ is a bijection $\pi: V_1\rightarrow V_2$ such that
203 for each $u,v\in V_1: uv\in E_1 \Leftrightarrow \pi(u)\pi(v)\in E_2$ and
204 for each $e,f\in E_1: w_1(e)<w_1(f) \Leftrightarrow w_2(\pi[e]) < w_2(\pi[f])$.
205
206 \lemman{MST of isomorphic graphs}\id{mstiso}%
207 Let~$G_1$ and $G_2$ be two weighted graphs with unique edge weights and $\pi$
208 their monotone isomorphism. Then $\mst(G_2) = \pi[\mst(G_1)]$.
209
210 \proof
211 The isomorphism~$\pi$ maps spanning trees onto spanning trees and it preserves
212 the relation of covering. Since it is monotone, it preserves the property of
213 being a light edge (an~edge $e\in E(G_1)$ is $T$-light $\Leftrightarrow$
214 the edge $\pi[e]\in E(G_2)$ is~$f[T]$-light). Therefore by Theorem~\ref{mstthm}, $T$
215 is the MST of~$G_1$ if and only if $\pi[T]$ is the MST of~$G_2$.
216 \qed
217
218 %--------------------------------------------------------------------------------
219
220 \section{The Red-Blue Meta-Algorithm}
221
222 Most MST algorithms can be described as special cases of the following procedure
223 (again following \cite{tarjan:dsna}):
224
225 \algn{Red-Blue Meta-Algorithm}\id{rbma}%
226 \algo
227 \algin A~graph $G$ with an edge comparison oracle (see \ref{edgeoracle})
228 \:In the beginning, all edges are colored black.
229 \:Apply rules as long as possible:
230 \::Either pick a cut~$C$ such that its lightest edge is not blue \hfil\break and color this edge blue, \cmt{Blue rule}
231 \::Or pick a cycle~$C$ such that its heaviest edge is not red \hfil\break and color this edge \hphantas{red.}{blue.} \cmt{Red rule}
232 \algout Minimum spanning tree of~$G$ consisting of edges colored blue.
233 \endalgo
234
235 \rem
236 This procedure is not a proper algorithm, since it does not specify how to choose
237 the rule to apply. We will however prove that no matter how the rules are applied,
238 the procedure always stops and gives the correct result. Also, it will turn out
239 that each of the classical MST algorithms can be described as a specific way
240 of choosing the rules in this procedure, which justifies the name meta-algorithm.
241
242 \nota
243 We will denote the unique minimum spanning tree of the input graph by~$T_{min}$.
244 We intend to prove that this is also the output of the procedure.
245
246 \lemman{Blue lemma}%
247 When an edge is colored blue in any step of the procedure, it is contained in the minimum spanning tree.
248
249 \proof
250 By contradiction. Let $e$ be an edge painted blue as the lightest edge of a cut~$C$.
251 If $e\not\in T_{min}$, then there must exist an edge $e'\in T_{min}$ which is
252 contained in~$C$ (take any pair of vertices separated by~$C$, the path
253 in~$T_{min}$ joining these vertices must cross~$C$ at least once). Exchanging
254 $e$ for $e'$ in $T_{min}$ yields an even lighter spanning tree since
255 $w(e)<w(e')$. \qed
256
257 \lemman{Red lemma}%
258 When an edge is colored red in any step of the procedure, it is not contained in the minimum spanning tree.
259
260 \proof
261 Again by contradiction. Assume that $e$ is an edge painted red as the heaviest edge
262 of a cycle~$C$ and that $e\in T_{min}$. Removing $e$ causes $T_{min}$ to split to two
263 components, let us call them $T_x$ and $T_y$. Some vertices of~$C$ now lie in $T_x$,
264 the others in $T_y$, so there must exist in edge $e'\ne e$ such that its endpoints
265 lie in different components. Since $w(e')<w(e)$, exchanging $e$ for~$e'$ yields
266 a lighter spanning tree than $T_{min}$.
267 \qed
268
269 \figure{mst-rb.eps}{289pt}{Proof of the Blue (left) and Red (right) lemma}
270
271 \lemman{Black lemma}%
272 As long as there exists a black edge, at least one rule can be applied.
273
274 \proof
275 Assume that $e=xy$ be a black edge. Let us denote $M$ the set of vertices
276 reachable from~$x$ using only blue edges. If $y$~lies in~$M$, then $e$ together
277 with some blue path between $x$ and $y$ forms a cycle and it must be the heaviest
278 edge on this cycle. This holds because all blue edges have been already proven
279 to be in $T_{min}$ and there can be no $T_{min}$-light edges (see Theorem~\ref{mstthm}).
280 In this case we can apply the red rule.
281
282 On the other hand, if $y\not\in M$, then the cut formed by all edges between $M$
283 and $V(G)\setminus M$ contains no blue edges, therefore we can use the blue rule.
284 \qed
285
286 \figure{mst-bez.eps}{295pt}{Configurations in the proof of the Black lemma}
287
288 \thmn{Red-Blue correctness}%
289 For any selection of rules, the Red-Blue procedure stops and the blue edges form
290 the minimum spanning tree of the input graph.
291
292 \proof
293 To prove that the procedure stops, let us notice that no edge is ever recolored,
294 so we must run out of black edges after at most~$m$ steps. Recoloring
295 to the same color is avoided by the conditions built in the rules, recoloring to
296 a different color would mean that the an edge would be both inside and outside~$T_{min}$
297 due to our Red and Blue lemmata.
298
299 When no further rules can be applied, the Black lemma guarantees that all edges
300 are colored, so by the Blue lemma all blue edges are in~$T_{min}$ and by the Red
301 lemma all other (red) edges are outside~$T_{min}$, so the blue edges are exactly~$T_{min}$.
302 \qed
303
304 %--------------------------------------------------------------------------------
305
306 \section{Classical algorithms}
307
308 The three classical MST algorithms can be easily stated in terms of the Red-Blue meta-algorithm.
309 For each of them, we first show the general version of the algorithm, then we prove that
310 it gives the correct result and finally we discuss the time complexity of various
311 implementations.
312
313 \algn{Bor\o{u}vka \cite{boruvka:ojistem}, Choquet \cite{choquet:mst}, Sollin \cite{sollin:mst} and others}
314 \algo
315 \algin A~graph~$G$ with an edge comparison oracle.
316 \:$T\=$ a forest consisting of vertices of~$G$ and no edges.
317 \:While $T$ is not connected:
318 \::For each component $T_i$ of~$T$, choose the lightest edge $e_i$ from the cut
319    separating $T_i$ from the rest of~$T$.
320 \::Add all $e_i$'s to~$T$.
321 \algout Minimum spanning tree~$T$.
322 \endalgo
323
324 \lemma\id{boruvkadrop}%
325 In each iteration of the algorithm, the number of trees in~$T$ drops at least twice.
326
327 \proof
328 Each tree gets merged with at least one of its neighbors, so each of the new trees
329 contains two or more original trees.
330 \qed
331
332 \cor
333 The algorithm stops in $\O(\log n)$ iterations.
334
335 \lemma
336 Bor\o{u}vka's algorithm outputs the MST of the input graph.
337
338 \proof
339 In every iteration of the algorithm, $T$ is a blue subgraph,
340 because every addition of some edge~$e_i$ to~$T$ is a straightforward
341 application of the Blue rule. We stop when the blue subgraph is connected, so
342 we do not need the Red rule to explicitly exclude edges.
343
344 It remains to show that adding the edges simultaneously does not
345 produce a cycle. Consider the first iteration of the algorithm where $T$ contains a~cycle~$C$. Without
346 loss of generality we can assume that $C=T_1[u_1v_1]\,v_1u_2\,T_2[u_2v_2]\,v_2u_3\,T_3[u_3v_3]\, \ldots \,T_k[u_kv_k]\,v_ku_1$.
347 Each component $T_i$ has chosen its lightest incident edge~$e_i$ as either the edge $v_iu_{i+1}$
348 or $v_{i-1}u_i$ (indexing cyclically). Assume that $e_1=v_1u_2$ (otherwise we reverse the orientation
349 of the cycle). Then $e_2=v_2u_3$ and $w(e_2)<w(e_1)$ and we can continue in the same way,
350 getting $w(e_1)>w(e_2)>\ldots>w(e_k)>w(e_1)$, which is a contradiction.
351 (Note that distinctness of edge weights was crucial here.)
352 \qed
353
354 \lemma\id{boruvkaiter}%
355 Each iteration can be carried out in time $\O(m)$.
356
357 \proof
358 We assign a label to each tree and we keep a mapping from vertices to the
359 labels of the trees they belong to. We scan all edges, map their endpoints
360 to the particular trees and for each tree we maintain the lightest incident edge
361 so far encountered. Instead of merging the trees one by one (which would be too
362 slow), we build an auxilliary graph whose vertices are the labels of the original
363 trees and edges correspond to the chosen lightest inter-tree edges. We find connected
364 components of this graph, these determine how are the original labels translated
365 to the new labels.
366 \qed
367
368 \thm
369 Bor\o{u}vka's algorithm finds the MST in time $\O(m\log n)$.
370
371 \proof
372 Follows from the previous lemmata.
373 \qed
374
375 \algn{Jarn\'\i{}k \cite{jarnik:ojistem}, Prim \cite{prim:mst}, Dijkstra \cite{dijkstra:mst}}\id{jarnik}%
376 \algo
377 \algin A~graph~$G$ with an edge comparison oracle.
378 \:$T\=$ a single-vertex tree containing an~arbitrary vertex of~$G$.
379 \:While there are vertices outside $T$:
380 \::Pick the lightest edge $uv$ such that $u\in V(T)$ and $v\not\in V(T)$.
381 \::$T\=T+uv$.
382 \algout Minimum spanning tree~$T$.
383 \endalgo
384
385 \lemma
386 Jarn\'\i{}k's algorithm computers the MST of the input graph.
387
388 \proof
389 If~$G$ is connected, the algorithm always stops. Let us prove that in every step of
390 the algorithm, $T$ is always a blue tree. Step~4 corresponds to applying
391 the Blue rule to the cut $\delta(T)$ separating~$T$ from the rest of the given graph. We need not care about
392 the remaining edges, since for a connected graph the algorithm always stops with the right
393 number of blue edges.
394 \qed
395
396 \impl
397 The most important part of the algorithm is finding \em{neighboring edges,} i.e., edges
398 of the cut $\delta(T)$. In a~straightforward implementation,
399 searching for the lightest neighboring edge takes $\Theta(m)$ time, so the whole
400 algorithm runs in time $\Theta(mn)$.
401
402 We can do much better by using a binary
403 heap to hold all neighboring edges. In each iteration, we find and delete the
404 minimum edge from the heap and once we expand the tree, we insert the newly discovered
405 neighboring edges to the heap while deleting the neighboring edges which become
406 internal to the new tree. Since there are always at most~$m$ edges in the heap,
407 each heap operation takes $\O(\log m)=\O(\log n)$ time. For every edge, we perform
408 at most one insertion and at most one deletion, so we spend $\O(m\log n)$ time in total.
409 From this, we can conclude:
410
411 \thm
412 Jarn\'\i{}k's algorithm finds the MST of a~given graph in time $\O(m\log n)$.
413
414 \rem
415 We will show several faster implementations in section \ref{fibonacci}.
416
417 \algn{Kruskal \cite{kruskal:mst}, the Greedy algorithm}
418 \algo
419 \algin A~graph~$G$ with an edge comparison oracle.
420 \:Sort edges of~$G$ by their increasing weight.
421 \:$T\=\emptyset$. \cmt{an empty spanning subgraph}
422 \:For all edges $e$ in their sorted order:
423 \::If $T+e$ is acyclic, add~$e$ to~$T$.
424 \::Otherwise drop~$e$.
425 \algout Minimum spanning tree~$T$.
426 \endalgo
427
428 \lemma
429 Kruskal's algorithm returns the MST of the input graph.
430
431 \proof
432 In every step, $T$ is a forest of blue trees. Adding~$e$ to~$T$
433 in step~4 applies the Blue rule on the cut separating some pair of components of~$T$ ($e$ is the lightest,
434 because all other edges of the cut have not been considered yet). Dropping~$e$ in step~5 corresponds
435 to the Red rule on the cycle found ($e$~must be the heaviest, since all other edges of the
436 cycle have been already processed). At the end of the algorithm, all edges are colored,
437 so~$T$ must be the~MST.
438 \qed
439
440 \impl
441 Except for the initial sorting, which in general takes $\Theta(m\log m)$ time, the only
442 other non-trivial operation is the detection of cycles. What we need is a data structure
443 for maintaining connected components, which supports queries and edge insertion.
444 (This is also known under the name Disjoint Set Union problem, i.e., maintenance
445 of an~equivalence relation on a~set with queries on whether two elements are equivalent
446 and the operation of joining two equivalence classes into one.)
447 The following theorem shows that it can be done with surprising efficiency.
448
449 \thmn{Incremental connectivity}%
450 When only edge insertions and connectivity queries are allowed, connected components
451 can be maintained in $\O(\alpha(n))$ time amortized per operation.
452
453 \proof
454 Proven by Tarjan and van Leeuwen in \cite{tarjan:setunion}.
455 \qed
456
457 \FIXME{Define Ackermann's function. Use $\alpha(m,n)$?}
458
459 \rem
460 The cost of the operations on components is of course dwarfed by the complexity
461 of sorting, so a much simpler (at least in terms of its analysis) data
462 structure would be sufficient, as long as it has $\O(\log n)$ amortized complexity
463 per operation. For example, we can label vertices with identifiers of the
464 corresponding components and always recolor the smaller of the two components.
465
466 \thm
467 Kruskal's algorithm finds the MST of a given graph in time $\O(m\log n)$
468 or $\O(m\timesalpha(n))$ if the edges are already sorted by their weights.
469
470 \proof
471 Follows from the above analysis.
472 \qed
473
474 %--------------------------------------------------------------------------------
475
476 \section{Contractive algorithms}
477
478 While the classical algorithms are based on growing suitable trees, they
479 can be also reformulated in terms of edge contraction. Instead of keeping
480 a forest of trees, we can keep each tree contracted to a single vertex.
481 This replaces the relatively complex tree-edge incidencies by simple
482 vertex-edge incidencies, potentially speeding up the calculation at the
483 expense of having to perform the contractions.
484
485 We will show a contractive version of the Bor\o{u}vka's algorithm
486 in which these costs are carefully balanced, leading for example to
487 a linear-time algorithm for MST in planar graphs.
488
489 There are two definitions of edge contraction which differ when an edge of a
490 triangle is contracted. Either we unify the other two edges to a single edge
491 or we keep them as two parallel edges, leaving us with a~multigraph. We will
492 use the multigraph version and we will show that we can easily reduce the multigraph
493 to a simple graph later. (See \ref{contract} for the exact definitions.)
494
495 We only need to be able to map edges of the contracted graph to the original
496 edges, so each edge will carry a unique label $\ell(e)$ that will be preserved by
497 contractions.
498
499 \lemman{Flattening a multigraph}\id{flattening}%
500 Let $G$ be a multigraph and $G'$ its subgraph such that all loops have been
501 removed and each bundle of parallel edges replaced by its lightest edge.
502 Then $G'$~has the same MST as~$G$.
503
504 \proof
505 Every spanning tree of~$G'$ is a spanning tree of~$G$. In the other direction:
506 Loops can be never contained in a spanning tree. If there is a spanning tree~$T$
507 containing a removed edge~$e$ parallel to an edge~$e'\in G'$, exchaning $e'$
508 for~$e$ makes~$T$ lighter. \qed
509
510 \rem Removal of the heavier of a pair of parallel edges can be also viewed
511 as an application of the Red rule on a two-edge cycle. And indeed it is, the
512 Red-Blue procedure works on multigraphs as well as on simple graphs and all the
513 classical algorithms also do. We would only have to be more careful in the
514 formulations and proofs, which we preferred to avoid.
515
516 \algn{Contractive version of Bor\o{u}vka's algorithm}\id{contbor}
517 \algo
518 \algin A~graph~$G$ with an edge comparison oracle.
519 \:$T\=\emptyset$.
520 \:$\ell(e)\=e$ for all edges~$e$. \cmt{Initialize the labels.}
521 \:While $n(G)>1$:
522 \::For each vertex $v_i$ of~$G$, let $e_i$ be the lightest edge incident to~$v_i$.
523 \::$T\=T\cup \{ \ell(e_i) \}$. \cmt{Remember labels of all selected edges.}
524 \::Contract $G$ along all edges $e_i$, inheriting labels and weights.\foot{In other words, we ask the comparison oracle for the edge $\ell(e)$ instead of~$e$.}
525 \::Flatten $G$, removing parallel edges and loops.
526 \algout Minimum spanning tree~$T$.
527 \endalgo
528
529 \lemma\id{contiter}%
530 Each iteration of the algorithm can be carried out in time~$\O(m)$.
531
532 \proof
533 The only non-trivial parts are steps 6 and~7. Contractions can be handled similarly
534 to the unions in the original Bor\o{u}vka's algorithm (see \ref{boruvkaiter}):
535 We build an auxillary graph containing only the selected edges~$e_i$, find
536 connected components of this graph and renumber vertices in each component to
537 the identifier of the component. This takes $\O(m)$ time.
538
539 Flattening is performed by first removing the loops and then bucket-sorting the edges
540 (as ordered pairs of vertex identifiers) lexicographically, which brings parallel
541 edges together. The bucket sort uses two passes with $n$~buckets, so it takes
542 $\O(n+m)=\O(m)$.
543 \qed
544
545 \thm
546 The Contractive Bor\o{u}vka's algorithm finds the MST in time $\O(m\log n)$.
547
548 \proof
549 As in the original Bor\o{u}vka's algorithm, the number of iterations is $\O(\log n)$.
550 Then apply the previous lemma.
551 \qed
552
553 \thmn{\cite{mm:mst}}\id{planarbor}%
554 When the input graph is planar, the Contractive Bor\o{u}vka's algorithm runs in
555 time $\O(n)$.
556
557 \proof
558 Let us denote the graph considered by the algorithm at the beginning of the $i$-th
559 iteration by $G_i$ (starting with $G_0=G$) and its number of vertices and edges
560 by $n_i$ and $m_i$ respectively. As we already know from the previous lemma,
561 the $i$-th iteration takes $\O(m_i)$ time. We are going to prove that the
562 $m_i$'s are decreasing geometrically.
563
564 The number of trees in the non-contracting version of the algorithm drops
565 at least by a factor of two in each iteration (Lemma \ref{boruvkadrop}) and the
566 same must hold for the number of vertices in the contracting version.
567 Therefore $n_i\le n/2^i$.
568
569 However, every $G_i$ is planar, because the class of planar graphs is closed
570 under edge deletion and contraction. The~$G_i$ is also simple as we explicitly removed multiple edges and
571 loops at the end of the previous iteration. Hence we can use the standard theorem on
572 the number of edges of planar simple graphs (see for example \cite{diestel:gt}) to get $m_i\le 3n_i \le 3n/2^i$.
573 From this we get that the total time complexity is $\O(\sum_i m_i)=\O(\sum_i n/2^i)=\O(n)$.
574 \qed
575
576 \rem
577 There are several other possibilities how to find the MST of a planar graph in linear time.
578 For example, Matsui \cite{matsui:planar} has described an algorithm based on simultaneously
579 working on the graph and its topological dual. We will show one more linear algorithm soon. The advantage
580 of our approach is that we do not need to construct the planar embedding explicitly.
581
582 \rem
583 To achieve the linear time complexity, the algorithm needs a very careful implementation.
584 Specifically, when we represent the graph using adjacency lists, whose heads are stored
585 in an array indexed by vertex identifiers, we must renumber the vertices in each iteration.
586 Otherwise, unused elements could end up taking most of the space in the arrays and the scans of these
587 arrays would have super-linear cost with respect to the size of the current graph~$G_i$.
588
589 \rem
590 The algorithm can be also implemented on the pointer machine. Representation of graphs
591 by pointer structures easily avoids the aforementioned problems with sparse arrays,
592 but we need to handle the bucket sorting somehow. We can create a small data structure
593 for every vertex and use a pointer to this structure as a unique identifier of the vertex.
594 We will also keep a list of all vertex structures. During the bucket sort, each vertex
595 structure will contain a pointer to the corresponding bucket and the vertex list will
596 define the order of vertices (which can be arbitrary).
597
598 Graph contractions are indeed a~very powerful tool and they can be used in other MST
599 algorithms as well. The following lemma shows the gist:
600
601 \lemman{Contraction of MST edges}\id{contlemma}%
602 Let $G$ be a weighted graph, $e$~an arbitrary edge of~$\mst(G)$, $G/e$ the multigraph
603 produced by contracting $G$ along~$e$, and $\pi$ the bijection between edges of~$G-e$ and
604 their counterparts in~$G/e$. Then: $$\mst(G) = \pi^{-1}[\mst(G/e)] + e.$$
605
606 \proof
607 % We seem not to need this lemma for multigraphs...
608 %If there are any loops or parallel edges in~$G$, we can flatten the graph. According to the
609 %Flattening lemma (\ref{flattening}), the MST stays the same and if we remove a parallel edge
610 %or loop~$f$, then $\pi(f)$ would be removed when flattening~$G/e$, so $f$ never participates
611 %in a MST.
612 The right-hand side of the equality is a spanning tree of~$G$, let us denote it by~$T$ and
613 the MST of $G/e$ by~$T'$. If $T$ were not minimum, there would exist a $T$-light edge~$f$ in~$G$
614 (by Theorem \ref{mstthm}). If the path $T[f]$ covered by~$f$ does not contain~$e$,
615 then $\pi[T[f]]$ is a path covered by~$\pi(f)$ in~$T'$. Otherwise $\pi(T[f]-e)$ is such a path.
616 In both cases, $f$ is $T'$-light, which contradicts the minimality of~$T'$. (We do not have
617 a~multigraph version of the theorem, but the side we need is a~straightforward edge exchange,
618 which obviously works in multigraphs as well.)
619 \qed
620
621 \rem
622 In the previous algorithm, the role of the mapping~$\pi^{-1}$ is of course played by the edge labels~$\ell$.
623
624 Finally, we will show a family of graphs where the $\O(m\log n)$ bound on time complexity
625 is tight. The graphs do not have unique weights, but they are constructed in a way that
626 the algorithm never compares two edges with the same weight. Therefore, when two such
627 graphs are monotonely isomorphic (see~\ref{mstiso}), the algorithm processes them in the same way.
628
629 \defn
630 A~\df{distractor of order~$k$,} denoted by~$D_k$, is a path on $n=2^k$~vertices $v_1,\ldots,v_n$
631 where each edge $v_iv_{i+1}$ has its weight equal to the number of trailing zeroes in the binary
632 representation of the number~$i$. The vertex $v_1$ is called a~\df{base} of the distractor.
633
634 \rem
635 Alternatively, we can use a recursive definition: $D_0$ is a single vertex, $D_{k+1}$ consists
636 of two disjoint copies of~$D_k$ joined by an edge of weight~$k$.
637
638 \figure{distractor.eps}{\epsfxsize}{A~distractor $D_3$ and its evolution (bold edges are contracted)}
639
640 \lemma
641 A~single iteration of the contractive algorithm reduces~$D_k$ to a graph isomorphic with~$D_{k-1}$.
642
643 \proof
644 Each vertex~$v$ of~$D_k$ is incident with a single edge of weight~1. The algorithm therefore
645 selects all weight~1 edges and contracts them. This produces a graph which is
646 exactly $D_{k-1}$ with all weights increased by~1, which does not change the relative order of edges.
647 \qed
648
649 \defn
650 A~\df{hedgehog}~$H_{a,k}$ is a graph consisting of $a$~distractors $D_k^1,\ldots,D_k^a$ of order~$k$
651 together with edges of a complete graph on the bases of the distractors. These additional edges
652 have arbitrary weights, but heavier than the edges of all distractors.
653
654 \figure{hedgehog.eps}{\epsfxsize}{A~hedgehog $H_{5,2}$ (quills bent to fit in the picture)}
655
656 \lemma
657 A~single iteration of the contractive algorithm reduces~$H_{a,k}$ to a graph isomorphic with $H_{a,k-1}$.
658
659 \proof
660 Each vertex is incident with an edge of some distractor, so the algorithm does not select
661 any edge of the complete graph. Contraction therefore reduces each distractor to a smaller
662 distractor (modulo an additive factor in weight) and leaves the complete graph intact.
663 This is monotonely isomorphic to $H_{a,k-1}$.
664 \qed
665
666 \thmn{Lower bound for Contractive Bor\o{u}vka}%
667 For each $n$ there exists a graph on $\Theta(n)$ vertices and $\Theta(n)$ edges
668 such that the Contractive Bor\o{u}vka's algorithm spends time $\Omega(n\log n)$ on it.
669
670 \proof
671 Consider the hedgehog $H_{a,k}$ for $a=\lceil\sqrt n\rceil$ and $k=\lceil\log_2 a\rceil$.
672 It has $a\cdot 2^k = \Theta(n)$ vertices and ${a \choose 2} + a\cdot 2^k = \Theta(a^2) + \Theta(a^2) = \Theta(n)$ edges
673 as we wanted.
674
675 By the previous lemma, the algorithm proceeds through a sequence of hedgehogs $H_{a,k},
676 H_{a,k-1}, \ldots, H_{a,0}$ (up to monotone isomorphism), so it needs a logarithmic number of iterations plus some more
677 to finish on the remaining complete graph. Each iteration runs on a graph with $\Omega(n)$
678 edges as every $H_{a,k}$ contains a complete graph on~$a$ vertices.
679 \qed
680
681 %--------------------------------------------------------------------------------
682
683 \section{Minor-closed graph classes}
684
685 The contracting algorithm given in the previous section has been found to perform
686 well on planar graphs, but in the general case its time complexity was not linear.
687 Can we find any broader class of graphs where the algorithm is still efficient?
688 The right context turns out to be the minor-closed graph classes, which are
689 closed under contractions and have bounded density.
690
691 \defn
692 A~graph~$H$ is a \df{minor} of a~graph~$G$ iff it can be obtained
693 from a subgraph of~$G$ by a sequence of simple graph contractions (see \ref{simpcont}).
694
695 \defn
696 A~class~$\cal C$ of graphs is \df{minor-closed}, when for every $G\in\cal C$ and
697 its every minor~$H$, the graph~$H$ lies in~$\cal C$ as well. A~class~$\cal C$ is called
698 \df{non-trivial} if at least one graph lies in~$\cal C$ and at least one lies outside~$\cal C$.
699
700 \example
701 Non-trivial minor-closed classes include planar graphs and more generally graphs
702 embeddable in any fixed surface. Many nice properties of planar graphs extend
703 to these classes, too, most notably the linearity of the number of edges.
704
705 \defn\id{density}%
706 Let $\cal C$ be a class of graphs. We define its \df{edge density} $\varrho(\cal C)$
707 to be the infimum of all~$\varrho$'s such that $m(G) \le \varrho\cdot n(G)$
708 holds for every $G\in\cal C$.
709
710 \thmn{Density of minor-closed classes}
711 A~minor-closed class of graphs has finite edge density if and only if it is
712 a non-trivial class.
713
714 \proof
715 See Theorem 6.1 in \cite{nesetril:minors}, which also lists some other equivalent conditions.
716 \qed
717
718 \thmn{MST on minor-closed classes \cite{mm:mst}}\id{mstmcc}%
719 For any fixed non-trivial minor-closed class~$\cal C$ of graphs, Algorithm \ref{contbor} finds
720 the MST of any graph in this class in time $\O(n)$. (The constant hidden in the~$\O$
721 depends on the class.)
722
723 \proof
724 Following the proof for planar graphs (\ref{planarbor}), we denote the graph considered
725 by the algorithm at the beginning of the $i$-th iteration by~$G_i$ and its number of vertices
726 and edges by $n_i$ and $m_i$ respectively. Again the $i$-th phase runs in time $\O(m_i)$
727 and $n_i \le n/2^i$, so it remains to show a linear bound for the $m_i$'s.
728
729 Since each $G_i$ is produced from~$G_{i-1}$ by a sequence of edge contractions,
730 all $G_i$'s are minors of~$G$.\foot{Technically, these are multigraph contractions,
731 but followed by flattening, so they are equivalent to contractions on simple graphs.}
732 So they also belong to~$\cal C$ and by the previous theorem $m_i\le \varrho({\cal C})\cdot n_i$.
733 \qed
734
735 \rem\id{nobatch}%
736 The contractive algorithm uses ``batch processing'' to perform many contractions
737 in a single step. It is also possible to perform contractions one edge at a~time,
738 batching only the flattenings. A~contraction of an edge~$uv$ can be done
739 in time~$\O(\deg(u))$ by removing all edges incident with~$u$ and inserting them back
740 with $u$ replaced by~$v$. Therefore we need to find a lot of vertices with small
741 degrees. The following lemma shows that this is always the case in minor-closed
742 classes.
743
744 \lemman{Low-degree vertices}\id{lowdeg}%
745 Let $\cal C$ be a graph class with density~$\varrho$ and $G\in\cal C$ a~graph
746 with $n$~vertices. Then at least $n/2$ vertices of~$G$ have degree at most~$4\varrho$.
747
748 \proof
749 Assume the contrary: Let there be at least $n/2$ vertices with degree
750 greater than~$4\varrho$.  Then $\sum_v \deg(v) > n/2
751 \cdot 4\varrho = 2\varrho n$, which is in contradiction with the number
752 of edges being at most $\varrho n$.
753 \qed
754
755 \rem
756 The proof can be also viewed
757 probabilistically: let $X$ be the degree of a vertex of~$G$ chosen uniformly at
758 random. Then ${\bb E}X \le 2\varrho$, hence by the Markov's inequality
759 ${\rm Pr}[X > 4\varrho] < 1/2$, so for at least $n/2$ vertices~$v$ we have
760 $\deg(v)\le 4\varrho$.
761
762 \algn{Local Bor\o{u}vka's Algorithm \cite{mm:mst}}%
763 \algo
764 \algin A~graph~$G$ with an edge comparison oracle and a~parameter~$t\in{\bb N}$.
765 \:$T\=\emptyset$.
766 \:$\ell(e)\=e$ for all edges~$e$.
767 \:While $n(G)>1$:
768 \::While there exists a~vertex~$v$ such that $\deg(v)\le t$:
769 \:::Select the lightest edge~$e$ incident with~$v$.
770 \:::Contract~$G$ along~$e$.
771 \:::$T\=T + \ell(e)$.
772 \::Flatten $G$, removing parallel edges and loops.
773 \algout Minimum spanning tree~$T$.
774 \endalgo
775
776 \thm
777 When $\cal C$ is a minor-closed class of graphs with density~$\varrho$, the
778 Local Bor\o{u}vka's Algorithm with the parameter~$t$ set to~$4\varrho$ 
779 finds the MST of any graph from this class in time $\O(n)$. (The constant
780 in the~$\O$ depends on~the class.)
781
782 \proof
783 Let us denote by $G_i$, $n_i$ and $m_i$ the graph considered by the
784 algorithm at the beginning of the $i$-th iteration of the outer loop,
785 and the number of its vertices and edges respectively. As in the proof
786 of the previous algorithm (\ref{mstmcc}), we observe that all the $G_i$'s
787 are minors of the graph~$G$ given as the input.
788
789 For the choice $t=4\varrho$, the Lemma on low-degree vertices (\ref{lowdeg})
790 guarantees that at least $n_i/2$ edges get selected in the $i$-th iteration.
791 Hence at least a half of the vertices participates in contractions, so
792 $n_i\le 3/4\cdot n_{i-1}$. Therefore $n_i\le n\cdot (3/4)^i$ and the algorithm terminates
793 after $\O(\log n)$ iterations.
794
795 Each selected edge belongs to $\mst(G)$, because it is the lightest edge of
796 the trivial cut $\delta(v)$ (see the Blue Rule in \ref{rbma}).
797 The steps 6 and~7 therefore correspond to the operation
798 described by the Lemma on contraction of MST edges (\ref{contlemma}) and when
799 the algorithm stops, $T$~is indeed the minimum spanning tree.
800
801 It remains to analyse the time complexity of the algorithm. Since $G_i\in{\cal C}$, we have
802 $m_i\le \varrho n_i \le \varrho n/2^i$.
803 We will show that the $i$-th iteration is carried out in time $\O(m_i)$.
804 Steps 5 and~6 run in time $\O(\deg(v))=\O(t)$ for each~$v$, so summed
805 over all $v$'s they take $\O(tn_i)$, which is linear for a fixed class~$\cal C$.
806 Flattening takes $\O(m_i)$, as already noted in the analysis of the Contracting
807 Bor\o{u}vka's Algorithm (see \ref{contiter}).
808
809 The whole algorithm therefore runs in time $\O(\sum_i m_i) = \O(\sum_i n/2^i) = \O(n)$.
810 \qed
811
812 \rem
813 For planar graphs, we can get a sharper version of the low-degree lemma,
814 showing that the algorithm works with $t=8$ as well (we had $t=12$ as
815 $\varrho=3$). While this does not change the asymptotic time complexity
816 of the algorithm, the constant-factor speedup can still delight the hearts of
817 its practical users.
818
819 \lemman{Low-degree vertices in planar graphs}%
820 Let $G$ be a planar graph with $n$~vertices. Then at least $n/2$ vertices of~$v$
821 have degree at most~8.
822
823 \proof
824 It suffices to show that the lemma holds for triangulations (if there
825 are any edges missing, the situation can only get better) with at
826 least 3 vertices. Since $G$ is planar, $\sum_v \deg(v) < 6n$.
827 The numbers $d(v):=\deg(v)-3$ are non-negative and $\sum_v d(v) < 3n$,
828 so by the same argument as in the proof of the general lemma, for at least $n/2$
829 vertices~$v$ it holds that $d(v) < 6$, hence $\deg(v) \le 8$.
830 \qed
831
832 \rem\id{hexa}%
833 The constant~8 in the previous lemma is the best we can have.
834 Consider a $k\times k$ triangular grid. It has $n=k^2$ vertices, $\O(k)$ of them
835 lie on the outer face and have degrees at most~6, the remaining $n-\O(k)$ interior
836 vertices have degree exactly~6. Therefore the number of faces~$f$ is $6/3\cdot n=2n$,
837 ignoring terms of order $\O(k)$. All interior triangles can be properly colored with
838 two colors, black and white. Now add a~new vertex inside each white face and connect
839 it to all three vertices on the boundary of that face. This adds $f/2 \approx n$
840 vertices of degree~3 and it increases the degrees of the original $\approx n$ interior
841 vertices to~9, therefore about a half of the vertices of the new planar graph
842 has degree~9.
843
844 \figure{hexangle.eps}{\epsfxsize}{The construction from Remark~\ref{hexa}}
845
846 %--------------------------------------------------------------------------------
847
848 \section{Using Fibonacci heaps}
849 \id{fibonacci}
850
851 We have seen that the Jarn\'\i{}k's Algorithm \ref{jarnik} runs in $\O(m\log n)$ time
852 (and this bound can be easily shown to be tight). Fredman and Tarjan have shown a~faster
853 implementation in~\cite{ft:fibonacci} using their Fibonacci heaps. In this section,
854 we convey their results and we show several interesting consequences.
855
856 The previous implementation of the algorithm used a binary heap to store all neighboring
857 edges of the cut~$\delta(T)$. Instead of that, we will remember the vertices adjacent
858 to~$T$ and for each such vertex~$v$ we will keep the lightest edge~$uv$ such that $u$~lies
859 in~$T$. We will call these edges \df{active edges} and keep them in a~heap, ordered by weight.
860
861 When we want to extend~$T$ by the lightest edge of~$\delta(T)$, it is sufficient to
862 find the lightest active edge~$uv$ and add this edge to~$T$ together with a new vertex~$v$.
863 Then we have to update the active edges as follows. The edge~$uv$ has just ceased to
864 be active. We scan all neighbors~$w$ of the vertex~$v$. When $w$~is in~$T$, no action
865 is needed. If $w$~is outside~$T$ and it was not adjacent to~$T$ (there is no active edge
866 remembered for it so far), we set the edge~$vw$ as active. Otherwise we check the existing
867 active edge for~$w$ and replace it by~$vw$ if the new edge is lighter.
868
869 The following algorithm shows how these operations translate to insertions, decreases
870 and deletions on the heap.
871
872 \algn{Jarn\'\i{}k with active edges, Fredman and Tarjan \cite{ft:fibonacci}}\id{jarniktwo}%
873 \algo
874 \algin A~graph~$G$ with an edge comparison oracle.
875 \:$v_0\=$ an~arbitrary vertex of~$G$.
876 \:$T\=$ a tree containing just the vertex~$v_0$.
877 \:$H\=$ a~heap of active edges stored as pairs $(u,v)$ where $u\in T,v\not\in T$, ordered by the weights $w(vw)$, initially empty.
878 \:$A\=$ an~auxiliary array mapping vertices outside~$T$ to their active edges in the heap; initially all elements undefined.
879 \:\<Insert> all edges incident with~$v_0$ to~$H$ and update~$A$ accordingly.
880 \:While $H$ is not empty:
881 \::$(u,v)\=\<DeleteMin>(H)$.
882 \::$T\=T+uv$.
883 \::For all edges $vw$ such that $w\not\in T$:
884 \:::If there exists an~active edge~$A(w)$:
885 \::::If $vw$ is lighter than~$A(w)$, \<Decrease> $A(w)$ to~$(v,w)$ in~$H$.
886 \:::If there is no such edge, then \<Insert> $(v,w)$ to~$H$ and set~$A(w)$.
887 \algout Minimum spanning tree~$T$.
888 \endalgo
889
890 \thmn{Fibonacci heaps} The~Fibonacci heap performs the following operations
891 with the indicated amortized time complexity:
892 \itemize\ibull
893 \:\<Insert> (insertion of a~new element) in $\O(1)$,
894 \:\<Decrease> (decreasing value of an~existing element) in $\O(1)$,
895 \:\<Merge> (merging of two heaps into one) in $\O(1)$,
896 \:\<DeleteMin> (deletion of the minimal element) in $\O(\log n)$,
897 \:\<Delete> (deletion of an~arbitrary element) in $\O(\log n)$,
898 \endlist
899 \>where $n$ is the maximum number of elements present in the heap at the time of
900 the operation.
901
902 \proof
903 See Fredman and Tarjan \cite{ft:fibonacci} for both the description of the Fibonacci
904 heap and the proof of this theorem.
905 \qed
906
907 \thm
908 Algorithm~\ref{jarniktwo} with a~Fibonacci heap finds the MST of the input graph in time~$\O(m+n\log n)$.
909
910 \proof
911 The algorithm always stops, because every edge enters the heap~$H$ at most once.
912 As it selects exactly the same edges as the original Jarn\'\i{}k's algorithm,
913 it gives the correct answer.
914
915 The time complexity is $\O(m)$ plus the cost of the heap operations. The algorithm
916 performs at most one \<Insert> or \<Decrease> per edge and exactly one \<DeleteMin>
917 per vertex and there are at most $n$ elements in the heap at any given time,
918 so by the previous theorem the operations take $\O(m+n\log n)$ time in total.
919 \qed
920
921 \cor
922 For graphs with edge density at least $\log n$, this algorithm runs in linear time.
923
924 \rem
925 We can consider using other kinds of heaps which have the property that inserts
926 and decreases are faster than deletes. Of course, the Fibonacci heaps are asymptotically
927 optimal (by the standard $\Omega(n\log n)$ lower bound on sorting by comparisons, see
928 for example \cite{clrs}), so the other data structures can improve only
929 multiplicative constants or offer an~easier implementation.
930
931 A~nice example is a~\df{$d$-regular heap} --- a~variant of the usual binary heap
932 in the form of a~complete $d$-regular tree. \<Insert>, \<Decrease> and other operations
933 involving bubbling the values up spend $\O(1)$ time at a~single level, so they run
934 in~$\O(\log_d n)$ time. \<Delete> and \<DeleteMin> require bubbling down, which incurs
935 comparison with all~$d$ sons at every level, so they run in~$\O(d\log_d n)$.
936 With this structure, the time complexity of the whole algorithm
937 is $\O(nd\log_d n + m\log_d n)$, which suggests setting $d=m/n$, giving $\O(m\log_{m/n}n)$.
938 This is still linear for graphs with density at~least~$n^{1+\varepsilon}$.
939
940 Another possibility is to use the 2-3-heaps \cite{takaoka:twothree} or Trinomial
941 heaps \cite{takaoka:trinomial}. Both have the same asymptotic complexity as Fibonacci
942 heaps (the latter even in worst case, but it does not matter here) and their
943 authors claim implementation advantages.
944
945 \FIXME{Mention Thorup's Fibonacci-like heaps for integers?}
946
947
948
949 % G has to be connected, so m=O(n)
950 % mention Steiner trees
951 % mention matroids
952 % sorted weights
953 % \O(...) as a set?
954 % impedance mismatch in terminology: contraction of G along e vs. contraction of e.
955 % use \delta(X) notation
956 % mention disconnected graphs
957
958 \endpart