]> mj.ucw.cz Git - saga.git/blob - dyn.tex
Fixed a typo in properties of ET-sequences
[saga.git] / dyn.tex
1 \ifx\endpart\undefined
2 \input macros.tex
3 \fi
4
5 \chapter{Dynamic Spanning Trees}\id{dynchap}%
6
7 \section{Dynamic graph algorithms}
8
9 In many applications, we often need to solve a~certain graph problem for a~sequence of graphs that
10 differ only a~little, so recomputing the solution for every graph from scratch would be a~waste of
11 time. In such cases, we usually turn our attention to \df{dynamic graph algorithms.} A~dynamic
12 algorithm is in fact a~data structure that remembers a~graph. It offers operations that modify the
13 structure of the graph and also operations that query the result of the problem for the current
14 state of the graph. A~typical example of a~problem of this kind is dynamic maintenance of connected
15 components:
16
17 \problemn{Dynamic connectivity}
18 Maintain an~undirected graph under a~sequence of the following operations:
19 \itemize\ibull
20 \:$\<Init>(n)$ --- Create a~graph with $n$~isolated vertices $\{1,\ldots,n\}$.\foot{%
21 The structure could support dynamic addition and removal of vertices, too,
22 but this is easy to add and infrequently used, so we will rather keep the set
23 of vertices fixed for clarity.}
24 \:$\<Insert>(G,u,v)$ --- Insert an~edge $uv$ to~$G$ and return its unique
25 identifier. This assumes that the edge did not exist yet.
26 \:$\<Delete>(G,e)$ --- Delete an~edge specified by its identifier from~$G$.
27 \:$\<Connected>(G,u,v)$ --- Test if vertices $u$ and~$v$ are in the same connected component of~$G$.
28 \endlist
29
30 \para
31 We have already encountered a~special case of dynamic connectivity when implementing the
32 Kruskal's algorithm in Section \ref{classalg}. At that time, we did not need to delete
33 any edges from the graph, which makes the problem substantially easier. This special
34 case is customarily called an~\df{incremental} or \df{semidynamic} graph algorithm.
35 We mentioned the Disjoint Set Union data structure of Tarjan (Theorem \ref{dfu})
36 which can be used for that: Connected components are represented by equivalence classes.
37 Queries on connectedness translate to \<Find>, edge insertions to \<Find>
38 followed by \<Union> if the new edge joins two different components. This way,
39 a~sequence of $m$~operations starting with an~empty graph on $n$~vertices is
40 processed in time $\O(n+m\timesalpha(m,n))$ and this holds even for the Pointer
41 Machine. Fredman and Saks \cite{fredman:cellprobe} have proven a~matching lower
42 bound in the cell-probe model which is stronger than RAM with $\O(\log n)$-bit
43 words.
44
45 \paran{Dynamic MSF}%
46 In this chapter, we will focus on the dynamic version of the minimum spanning forest.
47 This problem seems to be intimately related to the dynamic connectivity. Indeed, all known
48 algorithms for dynamic connectivity maintain some sort of a~spanning forest. For example, in the
49 incremental algorithm we have just mentioned, this forest is formed by the edges that have
50 triggered the \<Union>s. This suggests that a~dynamic MSF algorithm could be obtained by modifying
51 the mechanics of the data structure to keep the forest minimum. This will really turn out to be true, although we cannot
52 be sure that it will lead to the most efficient solution possible --- as of now, the known lower
53 bounds are very far.
54
55 Incremental MST will be easy to achieve even in the few pages of this section, but making it fully
56 dynamic will require more effort, so we will review some of the required building blocks before
57 going into that.
58
59 We however have to answer one important question first: What should be the output of
60 our MSF data structure? Adding an~operation that returns the MSF of the current
61 graph would be of course possible, but somewhat impractical as this operation would have to
62 spend $\Omega(n)$ time on the mere writing of its output. A~better way seems to
63 be making the \<Insert> and \<Delete> operations report the list of modifications
64 of the MSF implied by the change in the graph.
65
66 Let us see what happens when we \<Insert> an~edge~$e$ to a~graph~$G$ with its minimum spanning
67 forest~$F$, obtaining a~new graph~$G'$ with its MSF~$F'$. If $e$~connects two components of~$G$ (and
68 therefore also of~$F$), we have to add~$e$ to~$F$. Otherwise, one of the following cases happens:
69 Either $e$~is $F$-heavy and thus the forest~$F$ is also the MSF of the new graph. Or it is $F$-light
70 and we have to modify~$F$ by exchanging the heaviest edge~$f$ of the path $F[e]$ with~$e$.
71
72 Correctness of the former case follows immediately from the Minimality Theorem (\ref{mstthm}),
73 because any $F'$-light would be also $F$-light, which is impossible as $F$~was
74 minimum. In the latter case, the edge~$f$ is not contained in~$F'$ because it is the heaviest
75 on the cycle $F[e]+e$ (by the Red lemma, \ref{redlemma}). We can now use the Blue lemma
76 (\ref{bluelemma}) to prove that it should be replaced with~$e$. Consider the tree~$T$
77 of~$F$ that contains both endpoints of the edge~$e$. When we remove~$f$ from~$F$, this tree falls
78 apart to two components $T_1$ and~$T_2$. The edge~$f$ was the lightest in the cut~$\delta_G(T_1)$
79 and $e$~is lighter than~$f$, so $e$~is the lightest in~$\delta_{G'}(T_1)$ and hence $e\in F'$.
80 \looseness=1 %%HACK%%
81
82 A~\<Delete> of an~edge that is not contained in~$F$ does not change~$F$. When we delete
83 an~MSF edge, we have to reconnect~$F$ by choosing the lightest edge of the cut separating
84 the new components (again the Blue lemma in action). If there is no such
85 replacement edge, we have deleted a~bridge, so the MSF has to remain
86 disconnected.
87
88 The idea of reporting differences in the MSF indeed works very well. We can summarize
89 what we have shown by the following lemma and use it to define the dynamic MSF.
90
91 \lemma
92 An~\<Insert> or \<Delete> of an~edge in~$G$ causes at most one edge addition, edge
93 removal or edge exchange in $\msf(G)$.
94
95 \problemn{Dynamic minimum spanning forest}
96 Maintain an~undirected graph with distinct weights on edges (drawn from a~totally ordered set)
97 and its minimum spanning forest under a~sequence of the following operations:
98 \itemize\ibull
99 \:$\<Init>(n)$ --- Create a~graph with $n$~isolated vertices $\{1,\ldots,n\}$.
100 \:$\<Insert>(G,u,v,w)$ --- Insert an~edge $uv$ of weight~$w$ to~$G$. Return its unique
101         identifier and the list of additions and deletions of edges in $\msf(G)$.
102 \:$\<Delete>(G,e)$ --- Delete an~edge specified by its identifier from~$G$.
103         Return the list of additions and deletions of edges in $\msf(G)$.
104 \endlist
105
106 \paran{Incremental MSF}%
107 To obtain an~incremental MSF algorithm, we need to keep the forest in a~data structure that
108 supports search for the heaviest edge on the path connecting a~given pair
109 of vertices. This can be handled efficiently by the Link-Cut trees of Sleator and Tarjan:
110
111 \thmn{Link-Cut Trees, Sleator and Tarjan \cite{sleator:trees}}\id{sletar}%
112 There is a~data structure that represents a~forest of rooted trees on~$n$ vertices.
113 Each edge of the forest has a~weight drawn from a~totally ordered set. The structure
114 supports the following operations in time $\O(\log n)$ amortized:\foot{%
115 The Link-Cut trees can offer a~plethora of other operations, but we do not mention them
116 as they are not needed for our problem.}
117 \itemize\ibull
118 \:$\<Parent>(v)$ --- Return the parent of~$v$ in its tree or \<null> if $v$~is a~root.
119 \:$\<Root>(v)$ --- Return the root of the tree containing~$v$.
120 \:$\<Weight>(v)$ --- Return the weight of the edge $(\<Parent(v)>,v)$.
121 \:$\<PathMax>(v)$ --- Return the vertex~$u$ closest to $\<Root>(v)$ such that the edge
122         $(\<Parent>(u),u)$ is the heaviest of those on the path from the root to~$v$.
123         If more edges have the maximum weight, break the tie arbitrarily.
124         If there is no such edge ($v$~is the root itself), return \<null>.
125 \:$\<Link>(u,v,w)$ --- Connect the trees containing $u$ and~$v$ by an~edge $(u,v)$ of
126         weight~$w$. Assumes that $v~$is a tree root and $u$~lies in a~different tree.
127 \:$\<Cut>(v)$ --- Split the tree containing the non-root vertex $v$ to two trees by
128         removing the edge $(\<Parent>(v),v)$. Returns the weight of this edge.
129 \:$\<Evert>(v)$ --- Modify the orientations of edges to make~$v$ the root of its tree.
130 \endlist
131
132 %% \>Additionally, all edges on the path from~$v$ to $\<Root>(v)$ can be enumerated in
133 %% time $\O(\ell + \log n)$, where $\ell$~is the length of that path. This operation
134 %% (and also the path itself) is called $\<Path>(v)$.
135 %% 
136 %% \>If the weights are real numbers (or in general an~arbitrary group), the $\O(\log n)$
137 %% operations also include:
138 %% 
139 %% \itemize\ibull
140 %% \:$\<PathWeight>(v)$ --- Return the sum of the weights on $\<Path>(v)$.
141 %% \:$\<PathUpdate>(v,x)$ --- Add~$x$ to the weights of all edges on $\<Path>(v)$.
142 %% \endlist
143
144 \proof
145 See \cite{sleator:trees}.
146 \qed
147
148 Once we have this structure, we can turn our ideas on updating of the MSF to
149 an~incremental algorithm:
150
151 \algn{\<Insert> in an~incremental MSF}
152 \algo
153 \algin A~graph~$G$ with its MSF $F$ represented as a~Link-Cut forest, an~edge~$uv$
154 with weight~$w$ to be inserted.
155 \:$\<Evert>(u)$. \cmt{$u$~is now the root of its tree.}
156 \:If $\<Root>(v) \ne u$: \cmt{$u$~and~$v$ lie in different trees.}
157 \::$\<Link>(v,u,w)$. \cmt{Connect the trees.}
158 \::Return ``$uv$ added''.
159 \:Otherwise: \cmt{both are in the same tree}
160 \::$y\=\<PathMax>(v)$.
161 \::$x\=\<Parent>(y)$.  \cmt{Edge~$xy$ is the heaviest on $F[uv]$.}
162 \::If $\<Weight>(y) > w$: \cmt{We have to exchange~$xy$ with~$uv$.}
163 \:::$\<Cut>(y)$, $\<Evert>(v)$, $\<Link>(u,v,w)$.
164 \:::Return ``$uv$~added, $xy$~removed''.
165 \::Otherwise return ``no changes''.
166 \algout The list of changes in~$F$.
167 \endalgo
168
169 \thmn{Incremental MSF}
170 When only edge insertions are allowed, the dynamic MSF can be maintained in time $\O(\log n)$
171 amortized per operation.
172
173 \proof
174 Every \<Insert> performs $\O(1)$ operations on the Link-Cut forest, which take
175 $\O(\log n)$ each by Theorem \ref{sletar}.
176 \qed
177
178 \rem
179 We can easily extend the semidynamic MSF algorithm to allow an~operation commonly called
180 \<Backtrack> --- removal of the most recently inserted edge. It is sufficient to keep the
181 history of all MSF changes in a~stack and reverse the most recent change upon backtrack.
182
183 What are the obstacles to making the structure fully dynamic?
184 Deletion of edges that do not belong to the MSF is trivial (we do not
185 need to change anything) and so is deletion of bridges (we just remove the bridge
186 from the Link-Cut tree, knowing that there is no edge to replace it). The hard part
187 is the search for replacement edges after an~edge belonging to the MSF is deleted.
188
189 This very problem also has to be solved by algorithms for fully dynamic connectivity,
190 we will take a~look at them first.
191
192 %--------------------------------------------------------------------------------
193
194 \section{Eulerian Tour trees}
195
196 An~important stop on the road to fully dynamic algorithms has the name \df{Eulerian Tour trees} or
197 simply \df{ET-trees}. It is a~representation of forests introduced by Henzinger and King
198 \cite{henzinger:randdyn} in their randomized dynamic algorithms. It is similar to the Link-Cut
199 trees, but it is much simpler and instead of path operations it offers efficient operations on
200 subtrees. It is also possible to attach auxiliary data to vertices and edges of the original tree.
201
202 \defn\id{eulseq}%
203 Let~$T$ be a~rooted tree. We will call a~sequence of vertices of~$T$ its \df{Eulerian Tour sequence (ET-sequence)}
204 if it lists the vertices visited by the depth-first traversal of~$T$.
205 More precisely, it can be generated by the following procedure $\<ET>(v)$
206 when it is invoked on the root of the tree:
207 \algo
208 \:Record~$v$ in the sequence.
209 \:For each son~$w$ of~$v$:
210 \::Call $\<ET>(w)$.
211 \::Record~$v$.
212 \endalgo
213 \>A~single tree can have multiple ET-sequences, corresponding to different orders in which the
214 sons can be enumerated in step~2.
215
216 In every ET-sequence, one of the occurrences of each vertex is defined as its \df{active occurrence} and
217 it will be used to store auxiliary data associated with that vertex.
218
219 \obs
220 An~ET-sequence contains a~vertex of degree~$d$ exactly $d$~times except for the root which
221 occurs $d+1$ times. The whole sequence therefore contains $2n-1$ elements. It indeed describes the
222 order of vertices on an~Eulerian tour in the tree with all edges doubled. Let us observe what happens
223 to an~ET-sequence when we modify the tree. (See the picture.)
224
225 When we \em{delete} an~edge $uv$ from the tree~$T$ (let $u$~be the parent of~$v$), the sequence
226 $AuvBvuC$ (with no~$u$ nor~$v$ in~$B$) splits to two sequences $AuC$ and $vBv$.
227 If there was only a~single occurrence of~$v$, then $v$~was a~leaf and thus the sequence
228 transforms from $AuvuC$ to $AuC$ and $v$~alone.
229
230 \em{Changing the root} of the tree~$T$ from~$v$ to~$w$ changes its ET-sequence from $vAwBwCv$ to $wBwCvAw$.
231 If $w$~was a~leaf, the sequence changes from $vAwCv$ to $wCvAw$. If $vw$ was the only edge of~$T$,
232 the sequence $vwv$ becomes $wvw$. Note that this works regardless of the possible presence of~$w$ inside~$B$.
233
234 \em{Joining} the roots of two trees by a~new edge makes their ET-sequences $vAv$ and~$wBw$
235 combine to $vAvwBwv$. Again, we have to handle the cases when $v$ or~$w$ has degree~1 separately:
236 $v$~and~$wBw$ combine to $vwBwv$, and $v$~with~$w$ makes $vwv$.
237
238 \float{\valign{\vfil#\vfil\cr
239 \hbox{\epsfbox{pic/ettree.eps}}\cr
240 \noalign{\qquad\quad}
241   \halign{#\hfil\cr
242     $T_1: 0121034546474308980$,~~$T_2: aba$. \cr
243     $T_1-34: 01210308980, 4546474$. \cr
244     $T_1\hbox{~rooted at~3}: 3454647430898012103$. \cr
245     $T_1+0a+T_2$: $0121034546474308980aba0$. \cr
246   }\cr
247 }}{Trees and their ET-sequences}
248
249 If any of the occurrences that we have removed from the sequence was active, there is always
250 a~new occurrence of the same vertex that can stand in its place and inherit the auxiliary data.
251
252 \para
253 The ET-trees will store the ET-sequences as $(a,b)$-trees with the parameter~$a$ set upon
254 initialization of the structure and with $b=2a$. We know from the standard theorems of $(a,b)$-trees
255 (see for example \cite{clrs}) that the depth of a~tree with $n$~leaves is always $\O(\log_a n)$
256 and that all basic operations including insertion, deletion, search, splitting and joining the trees
257 run in time $\O(b\log_a n)$ in the worst case.
258 \looseness=-1 %%HACK%%
259
260 We will use the ET-trees to maintain a~spanning forest of the dynamic graph. The auxiliary data of
261 each vertex will hold a~list of edges incident with the given vertex, that do not lie in the
262 forest. Such edges are usually called the \df{non-tree edges.}
263
264 \defn
265 \df{Eulerian Tour trees (ET-trees)} are a~data structure that represents a~forest of trees and a~set of non-tree
266 edges associated with the vertices of the forest. To avoid confusion, we will distinguish between
267 \df{original} vertices and edges (of the given trees) and the vertices and edges of the
268 data structure. The structure consists of:
269 \itemize\ibull
270 \:A~collection of $(a,b)$-trees of some fixed parameters $a$ and~$b$.
271         Each such tree corresponds to one of the original trees~$T$. Its
272         leaves (in the usual tree order) correspond to the elements
273         of an~ET-sequence for~$T$. Each two consecutive leaves $u$ and~$v$ are separated
274         by a~unique key stored in an~internal vertex of the $(a,b)$-tree. This key is used to represent
275         the original edge~$uv$. Each original edge is therefore kept in both its orientations.
276 \:Mappings \<act>, \<edge> and \<twin>:
277         \itemize\icirc
278                 \:$\<act>(v)$ maps each original vertex to the leaf containing its active occurrence;
279                 \:$\<edge>(e)$ of an~original edge~$e$ is one of the internal keys representing~it;
280                 \:$\<twin>(k)$ pairs an~internal key~$k$ with the other internal key of the same original edge.
281         \endlist
282 \:A~list of non-tree edges placed in each leaf. The lists are allowed to be non-empty only
283         in the leaves that represent active occurrences of original vertices.
284 \:Boolean \df{markers} in the internal vertices that signal presence of a~non-tree
285         edge anywhere in the subtree rooted at the internal vertex.
286 \:Counters $\<leaves>(v)$ that contain the number of leaves in the subtree rooted at~$v$.
287 \endlist
288
289 \defn
290 The ET-trees support the following operations on the original trees:
291 \itemize\ibull
292 \:\<Create> --- Create a~single-vertex tree.
293 \:$\<Link>(u,v)$ --- Join two different trees by an~edge~$uv$ and return a~unique identifier
294         of this edge.
295 \:$\<Cut>(e)$ --- Split a~tree by removing the edge~$e$ given by its identifier.
296 \:$\<Connected>(u,v)$ --- Test if the vertices $u$ and~$v$ lie in the same tree.
297 \:$\<Size>(v)$ --- Return the number of vertices in the tree containing the vertex~$v$.
298 \:$\<InsertNontree>(v,e)$ --- Add a~non-tree edge~$e$ to the list at~$v$ and return a~unique
299         identifier of this edge.
300 \:$\<DeleteNontree>(e)$ --- Delete a~non-tree edge~$e$ given by its identifier.
301 \:$\<ScanNontree>(v)$ --- Return a~list of non-tree edges associated with the vertices
302         of the $v$'s tree.
303 \endlist
304
305 \impl
306 We will implement the operations on the ET-trees by translating the intended changes of the
307 ET-sequences to operations on the $(a,b)$-trees. The role of identifiers of the original vertices
308 and edges will be of course played by pointers to the respective leaves and internal keys of
309 the $(a,b)$-trees.
310
311 \<Cut> of an~edge splits the $(a,b)$-tree at both internal keys representing the given edge
312 and joins them back in the different order.
313
314 \<Link> of two trees can be accomplished by making both vertices the roots of their trees first
315 and joining the roots by an~edge afterwards. Re-rooting involves splits and joins of $(a,b)$-trees.
316 As we can split at any occurrence of the new root vertex, we will use the active occurrence
317 which we remember. Linking of the roots is translated to joining of the $(a,b)$-trees.
318
319 \<Connected> follows parent pointers from both $u$ and~$v$ to the roots of their trees.
320 Then it checks if the roots are equal.
321
322 \<Size> finds the root~$r$ and returns $\<leaves>(r)$.
323
324 \<InsertNontree> finds the leaf $\<act>(v)$ containing the list of $v$'s non-tree edges
325 and inserts the new edge there. The returned identifier will consist from the pointer to
326 the edge and the vertex in whose list it is stored. Then we have to recalculate the markers
327 on the path from $\<act>(v)$ to the root. \<DeleteNontree> is analogous.
328
329 Whenever any other operation changes a~vertex of the tree, it will also update its marker and
330 counter and, if necessary, the markers and counters on the path to the root.
331
332 \<ScanNontree> traverses the tree recursively from the root, but it does not enter the
333 subtrees whose roots are not marked.
334
335 Analysis of time complexity of the operations is now straightforward:
336
337 \thmn{Eulerian Tour trees, Henzinger and Rauch \cite{henzinger:randdyn}}\id{etthm}%
338 The ET-trees perform the operations \<Link> and \<Cut> in time $\O(a\log_a n)$, \<Create>
339 in $\O(1)$, \<Connected>, \<Size>, \<InsertNontree>, and \<DeleteNontree> in $\O(\log_a n)$, and
340 \<ScanNontree> in $\O(a\log_a n)$ per edge reported. Here $n$~is the number of vertices
341 in the original forest and $a\ge 2$ is an~arbitrary constant.
342
343 \proof
344 We set $b=2a$. Our implementation performs $\O(1)$ operations on the $(a,b)$-trees
345 per operation on the ET-tree, plus $\O(1)$ other work. We apply the standard theorems
346 on the complexity of $(a,b)$-trees \cite{clrs}.
347 \qed
348
349 \examplen{Connectivity acceleration}\id{accel}%
350 In most cases, the ET-trees are used with $a$~constant, but sometimes choosing~$a$ as a~function
351 of~$n$ can also have its beauty. Suppose that there is a~data structure which maintains an~arbitrary
352 spanning forest of a~dynamic graph. Suppose also that the structure works in time $\O(\log^k n)$
353 per operation and that it reports $\O(1)$ changes in the spanning forest for every change
354 in the graph. If we keep the spanning forest in ET-trees with $a=\log n$, the updates of the
355 data structure cost an~additional $\O(\log^2 n / \log\log n)$, but connectivity queries accelerate to $\O(\log
356 n/\log\log n)$.
357
358 \paran{ET-trees with weights}
359 In some cases, we will also need a~representation of weighted graphs and enumerate the non-tree
360 edges in order of their increasing weights (in fact, it will be sufficient to find the
361 lightest one, remove it and iterate). This can be handled by a~minute modification of the
362 ET-trees.
363
364 The tree edges will remember their weight in the corresponding internal keys of the ET-tree.
365 We replace each list of non-tree edges by an~$(a,b)$-tree keeping the edges sorted by weight.
366 We also store the minimum element of that tree separately, so that it can be accessed in constant
367 time. The boolean \em{marker} will then become the minimum weight of a~non-tree edge attached to the
368 particular subtree, which can be recalculated as easy as the markers can. Searching for the
369 lightest non-tree edge then just follows the modified markers.
370
371 The time complexities of all operations therefore remain the same, with a~possible
372 exception of the operations on non-tree edges, to which we have added the burden of
373 updating the new $(a,b)$-trees. This however consists of $\O(1)$ updates per a~single
374 call to \<InsertNontree> or \<DeleteNontree>, which takes $\O(a\log_a n)$ time only.
375 We can therefore conclude:
376
377 \corn{Weighted ET-trees}\id{wtet}%
378 In weighted ET-trees, the operations \<InsertNontree> and \<DeleteNontree> have time
379 complexity $\O(a\log_a n)$. All other operations take the same time as indicated by Theorem
380 \ref{etthm}.
381
382 %--------------------------------------------------------------------------------
383
384 \section{Dynamic connectivity}
385
386 The fully dynamic connectivity problem has a~long and rich history. In the 1980's, Frederickson \cite{frederickson:dynamic}
387 has used his topological trees to construct a~dynamic connectivity algorithm of complexity $\O(\sqrt m)$ per update and
388 $\O(1)$ per query. Eppstein et al.~\cite{eppstein:sparsify} have introduced a~sparsification technique which can bring the
389 updates down to $\O(\sqrt n)$. Later, several different algorithms with complexity on the order of $n^\varepsilon$
390 were presented by Henzinger and King \cite{henzinger:mst} and also by Mare\v{s} \cite{mares:dga}.
391 A~polylogarithmic time bound was first reached by the randomized algorithm of Henzinger and King \cite{henzinger:randdyn}.
392 The best result known as of now is the $\O(\log^2 n)$ time deterministic algorithm by Holm,
393 de~Lichtenberg and Thorup \cite{holm:polylog}, which will we describe in this section.
394
395 The algorithm will maintain a~spanning forest~$F$ of the current graph~$G$, represented by an~ET-tree
396 which will be used to answer connectivity queries. The edges of~$G\setminus F$ will be stored as~non-tree
397 edges in the ET-tree. Hence, an~insertion of an~edge to~$G$ either adds it to~$F$ or inserts it as non-tree.
398 Deletions of non-tree edges are also easy, but when a~tree edge is deleted, we have to search for its
399 replacement among the non-tree edges.
400
401 To govern the search in an~efficient way, we will associate each edge~$e$ with a~level $\ell(e) \le
402 L = \lfloor\log_2 n\rfloor$. For each level~$i$, we will use~$F_i$ to denote the subforest
403 of~$F$ containing edges of level at least~$i$. Therefore $F=F_0 \supseteq F_1 \supseteq \ldots \supseteq F_L$.
404 We will maintain the following \em{invariants:}
405
406 {\narrower
407 \def\iinv{{\bo I\the\itemcount~}}
408 \numlist\iinv
409 \:$F$~is the maximum spanning forest of~$G$ with respect to the levels. (In other words,
410 if $uv$ is a~non-tree edge, then $u$ and~$v$ are connected in~$F_{\ell(uv)}$.)
411 \:For each~$i$, the components of~$F_i$ have at most $\lfloor n/2^i \rfloor$ vertices each.
412 (This implies that it does not make sense to define~$F_i$ for $i>L$, because it would be empty
413 anyway.)
414 \endlist
415 }
416
417 At the beginning, the graph contains no edges, so both invariants are trivially
418 satisfied. Newly inserted edges enter level~0, which cannot break I1 nor~I2.
419
420 When we delete a~tree edge at level~$\ell$, we split a~tree~$T$ of~$F_\ell$ to two
421 trees $T_1$ and~$T_2$. Without loss of generality, let us assume that $T_1$ is the
422 smaller one. We will try to find the replacement edge of the highest possible
423 level that connects the spanning tree back. From I1, we know that such an~edge cannot belong to
424 a~level greater than~$\ell$, so we start looking for it at level~$\ell$. According
425 to~I2, the tree~$T$ had at most $\lfloor n/2^\ell\rfloor$ vertices, so $T_1$ has
426 at most $\lfloor n/2^{\ell+1} \rfloor$ of them. Thus we can move all level~$\ell$
427 edges of~$T_1$ to level~$\ell+1$ without violating either invariant.
428
429 We now start enumerating the non-tree edges incident with~$T_1$. Each such edge
430 is either local to~$T_1$ or it joins $T_1$ with~$T_2$. We will therefore check each edge
431 whether its other endpoint lies in~$T_2$ and if it does, we have found the replacement
432 edge, so we insert it to~$F_\ell$ and stop. Otherwise we move the edge one level up. (This
433 will be the grist for the mill of our amortization argument: We can charge most of the work on level
434 increases and we know that the level of each edge can reach at most~$L$.)
435
436 If the non-tree edges at level~$\ell$ are exhausted, we try the same in the next
437 lower level and so on. If there is no replacement edge at level~0, the tree~$T$
438 remains disconnected.
439
440 \impl
441 For each level~$\ell$, we will use a~separate ET-tree ${\cal E}_\ell$ with~$a$ set to~2,
442 which will represent the forest~$F_\ell$ and the non-tree edges at that particular level.
443 Besides operations on the non-tree edges, we also need to find the tree edges of level~$\ell$
444 when we want to bring them one level up. This can be accomplished either by modifying the ET-trees
445 to attach two lists of edges attached to vertices instead of one, or by using a~second ET-tree.
446
447 \algn{Insertion of an~edge}
448 \algo
449 \algin An~edge $uv$ to insert.
450 \:$\ell(uv) \= 0$.
451 \:Ask the ET-tree ${\cal E}_0$ if $u$ and~$v$ are in the same component. If they are:
452 \::Add $uv$ to the list of non-tree edges in ${\cal E}_0$ at both $u$ and~$v$.
453 \:Otherwise:
454 \::Add $uv$ to~$F_0$.
455 \endalgo
456
457 \algn{Deletion of an~edge}
458 \algo
459 \algin An~edge $uv$ to delete.
460 \:$\ell \= \ell(uv)$.
461 \:If $uv$ is a~non-tree edge:
462 \::Remove $uv$ from the lists of non-tree edges at both $u$ and~$v$ in~${\cal E}_{\ell}$.
463 \:Otherwise:
464 \::Remove $uv$ from~$F_\ell$ and hence also from $F_0,\ldots,F_{\ell-1}$.
465 \::Call $\<Replace>(uv,\ell)$ to get the replacement edge~$f$.
466 \::Insert $f$ to~$F_0,\ldots,F_{\ell(f)}$.
467 \endalgo
468
469 \algn{$\<Replace>(uv,i)$ -- Search for replacement for edge~$uv$ at level~$i$}
470 \algo
471 \algin An~edge~$uv$ to replace and a~level~$i$ such that there is no replacement
472 at levels greater than~$i$.
473 \:Let $T_1$ and~$T_2$ be the trees in~$F_i$ containing $u$ and~$v$ respectively.
474 \:If $n(T_1) > n(T_2)$, swap $T_1$ with~$T_2$.
475 \:Find all level~$i$ edges in~$T_1$ using ${\cal E}_i$ and move them to level~$i+1$.
476 \:Enumerate non-tree edges incident with vertices of~$T_1$ and stored in ${\cal E}_i$.
477   For each edge~$xy$, $x\in T_1$, do:
478 \::If $y\in T_2$, remove~$xy$ from~${\cal E}_i$ and return it to the caller.
479 \::Otherwise increase $\ell(xy)$ by one.
480   \hfil\break
481   This includes deleting~$xy$ from~${\cal E}_i$ and inserting it to~${\cal E}_{i+1}$.
482 \:If $i>0$, call $\<Replace>(xy,i-1)$.
483 \:Otherwise return \<null>.
484 \algout The replacement edge.
485 \endalgo
486
487 \>As foretold, time complexity will be analysed by amortization on the levels.
488
489 \thmn{Fully dynamic connectivity, Holm et al.~\cite{holm:polylog}}\id{dyncon}%
490 Dynamic connectivity can be maintained in time $\O(\log^2 n)$ amortized per
491 \<Insert> and \<Delete> and in time $\O(\log n/\log\log n)$ per \<Connected>
492 in the worst case.
493
494 \proof
495 The direct cost of an~\<Insert> is $\O(\log n)$ for the operations on the ET-trees
496 (by Theorem \ref{etthm}). We will also have the insertion pre-pay all level increases of the new
497 edge. Since the levels never decrease, each edge can be brought a~level up at most
498 $L=\lfloor\log n\rfloor$ times. Every increase costs $\O(\log n)$ on the ET-tree
499 operations, so we pay $\O(\log^2 n)$ for all of them.
500
501 A~\<Delete> costs $\O(\log^2 n)$ directly, as we might have to update all~$L$
502 ET-trees. Additionally, we call \<Replace> up to $L$ times. The initialization of
503 \<Replace> costs $\O(\log n)$ per call, the rest is paid for by the edge level
504 increases.
505
506 To bring the complexity of the operation \<Connected> from $\O(\log n)$ down to $\O(\log n/\log\log n)$,
507 we apply the trick from Example \ref{accel} and store~$F_0$ in an~ET-tree with $a=\max(\lfloor\log n\rfloor,2)$.
508 This does not hurt the complexity of insertions and deletions, but allows for faster queries.
509 \qed
510
511 \rem\id{dclower}%
512 An~$\Omega(\log n/\log\log n)$ lower bound for the amortized complexity of the dynamic connectivity
513 problem has been proven by Henzinger and Fredman \cite{henzinger:lowerbounds} in the cell
514 probe model with $\O(\log n)$-bit words. Thorup has answered by a~faster algorithm
515 \cite{thorup:nearopt} that achieves $\O(\log n\log^3\log n)$ time per update and
516 $\O(\log n/\log^{(3)} n)$ per query on a~RAM with $\O(\log n)$-bit words. (He claims
517 that the algorithm runs on a~Pointer Machine, but it uses arithmetic operations,
518 so it does not fit the definition of the PM we use. The algorithm only does not
519 need direct indexing of arrays.) So far, it is not known how to extend this algorithm
520 to fit our needs, so we omit the details.
521
522 %--------------------------------------------------------------------------------
523
524 \section{Dynamic spanning forests}\id{dynmstsect}%
525
526 Let us turn our attention back to the dynamic MSF.
527 Most of the early algorithms for dynamic connectivity also imply $\O(n^\varepsilon)$
528 algorithms for dynamic maintenance of the MSF. Henzinger and King \cite{henzinger:twoec,henzinger:randdyn}
529 have generalized their randomized connectivity algorithm to maintain the MSF in $\O(\log^5 n)$ time per
530 operation, or $\O(k\log^3 n)$ if only $k$ different values of edge weights are allowed. They have solved
531 the decremental version of the problem first (which starts with a~given graph and only edge deletions
532 are allowed) and then presented a~general reduction from the fully dynamic MSF to its decremental version.
533 We will describe the algorithm of Holm, de Lichtenberg and Thorup \cite{holm:polylog}, who have followed
534 the same path. They have modified their dynamic connectivity algorithm to solve the decremental MSF
535 in $\O(\log^2 n)$ and obtained the fully dynamic MSF working in $\O(\log^4 n)$ per operation.
536
537 \paran{Decremental MSF}%
538 Turning the algorithm from the previous section to the decremental MSF requires only two
539 changes: First, we have to start with the forest~$F$ equal to the MSF of the initial
540 graph. As we require to pay $\O(\log^2 n)$ for every insertion, we can use almost arbitrary
541 MSF algorithm to find~$F$. Second, when we search for an~replacement edge, we need to pick
542 the lightest possible choice. We will therefore use the weighted version of the ET-trees (Corollary \ref{wtet})
543 and scan the lightest non-tree edge incident with the examined tree first. We must ensure
544 that the lower levels cannot contain a~lighter replacement edge, but fortunately the
545 light edges tend to ``bubble up'' in the hierarchy of levels. This can be formalized
546 in form of the following invariant:
547
548 {\narrower
549 \def\iinv{{\bo I\the\itemcount~}}
550 \numlist\iinv
551 \itemcount=2
552 \:On every cycle, the heaviest edge has the smallest level.
553 \endlist
554 }
555
556 \>This immediately implies that we always select the right replacement edge:
557
558 \lemma\id{msfrepl}%
559 Let $F$~be the minimum spanning forest and $e$ any its edge. Then among all replacement
560 edges for~$e$, the lightest one is at the maximum level.
561
562 \proof
563 Let us consider any two edges $f_1$ and~$f_2$ replacing~$e$. By minimality of~$F$ and the Cycle
564 rule (Lemma \ref{redlemma}), each $f_i$ is the heaviest edge on the cycle~$C_i = F[f_i] + f_i$.
565 In a~moment, we will show that the symmetric difference~$C$ of these two cycles is again a~cycle.
566 This implies that if $f_1$ is heavier than~$f_2$, then $f_1$~is the heaviest edge on~$C$, so
567 $\ell(f_1) \le \ell(f_2)$ by I3. Therefore the lightest of all replacement edges must have
568 the maximum level.
569
570 Why is~$C$ a~cycle:
571 Let $F^a$ and~$F^b$ be the trees of~$F-e$ in which the endpoints of~$e$ lie, and for
572 every edge~$g$ going between $F^a$ and~$F^b$ let $g^a$ and~$g^b$ be its respective endpoints.
573 We know that $C_i$ consists of the path $F[f_i^a,e^a]$, the edge~$e$, the path $F[e^b,f_i^b]$,
574 and the edge~$f_i$. Thus~$C$ must contain the paths $F[f_1^a,f_2^a]$ and $F[f_1^b,f_2^b]$ and
575 the edges $f_1$ and~$f_2$, which together form a~simple cycle.
576 \qed
577
578 We now have to make sure that the additional invariant is really observed:
579
580 \lemma\id{ithree}%
581 After every operation, the invariant I3 is satisfied.
582
583 \proof
584 When the structure is freshly initialized, I3 is obviously satisfied, as all edges
585 are at level~0. Sole deletions of edges (both tree and non-tree) cannot violate I3, so we need
586 to check only the replaces, in particular the place when an~edge~$e$ gets its level increased.
587
588 For the violation to happen for the first time, $e$~must be the heaviest on
589 some cycle~$C$, so by the Cycle rule, $e$~must be non-tree. The increase of
590 $\ell(e)$ must therefore take place when~$e$ is considered as a~replacement
591 edge incident with some tree~$T_1$ at level $\ell=\ell(e)$. We will pause the
592 computation just before this increase and we will prove that all other edges
593 of~$C$ already are at levels greater than~$\ell$, so the violation cannot occur.
594
595 Let us first show that for edges of~$C$ incident with~$T_1$. All edges of~$T_1$ itself
596 already are at the higher levels as they were moved there at the very beginning of the
597 search for the replacement edge. The other tree edges incident with~$T_1$ would have
598 lower levels, which is impossible since the invariant would be already violated.
599 Non-tree edges of~$C$ incident with~$T_1$ are lighter than~$e$, so they were already considered
600 as~candidates for the replacement edge, because the algorithm always picks the lightest
601 candidate first. Such edges therefore have been already moved a~level up.
602
603 The case of edges of~$C$ that do not touch~$T_1$ is easy to handle: Such edges do not exist.
604 If they did, at least one more edge of~$C$ besides~$e$ would have to connect~$T_1$ with the other
605 trees of level~$\ell$. We already know that this could not be a~tree edge. If it were a~non-tree
606 edge, it could not have level greater than~$\ell$ by~I1 nor smaller than~$\ell$ by~I3. Therefore
607 it would be a~level~$\ell$ edge lighter than~$e$, and as such it would have been selected as the
608 replacement edge before $e$~was.
609 \qed
610
611 We can conclude:
612
613 \thmn{Decremental MSF, Holm et al.~\cite{holm:polylog}}
614 When we start with a~graph on $n$~vertices with~$m$ edges and we perform a~sequence of
615 edge deletions, the MSF can be initialized in time $\O((m+n)\cdot\log^2 n)$ and then
616 updated in time $\O(\log^2 n)$ amortized per operation.
617
618 \paran{Fully dynamic MSF}%
619 The decremental MSF algorithm can be turned to a~fully dynamic one by a~blackbox
620 reduction whose properties are summarized in the following theorem:
621
622 \thmn{MSF dynamization, Holm et al.~\cite{holm:polylog}}
623 Suppose that we have a~decremental MSF algorithm with the following properties:
624 \numlist\ndotted
625 \:For any $a$,~$b$, it can be initialized on a~graph with~$a$ vertices and~$b$ edges.
626 \:Then it executes an~arbitrary sequence of deletions in time $\O(b\cdot t(a,b))$, where~$t$ is a~non-decreasing function.
627 \endlist
628 \>Then there exists a~fully dynamic MSF algorithm for a~graph on $n$~vertices, starting
629 with no edges, that performs $m$~insertions and deletions in amortized time:
630 $$
631 \O\left( \log^3 n + \sum_{i=1}^{\log m} \sum_{j=1}^i \; t(\min(n,2^j), 2^j) \right) \hbox{\quad per operation.}
632 $$
633
634 \proofsketch
635 The reduction is very technical, but its essence is the following: We maintain a~logarithmic number
636 of decremental structures $A_0,\ldots,A_{\lfloor\log n\rfloor}$ of exponentially increasing sizes. Every non-tree
637 edge is contained in exactly one~$A_i$, tree edges can belong to multiple structures.
638
639 When an~edge is inserted, we union it with some of the $A_i$'s, build a~new decremental structure
640 and amortize the cost of the build over the insertions. Deletes of non-tree edges are trivial.
641 Delete of a~tree edge is performed on all $A_i$'s containing it and the replacement edge is
642 sought among the replacement edges found in these structures. The unused replacement edges then have
643 to be reinserted back to the structure.
644
645 The original reduction of Henzinger et al.~\cite{henzinger:twoec} handles these reinserts by a~mechanism of batch insertions
646 supported by their decremental structure, which is not available in our case. Holm et al.~have
647 replaced it by a~system of auxiliary edges inserted at various places in the structure.
648 We refer to the article \cite{holm:polylog} for details.
649 \qed
650
651 \corn{Fully dynamic MSF}\id{dynmsfcorr}%
652 There is a~fully dynamic MSF algorithm that works in time $\O(\log^4 n)$ amortized
653 per operation for graphs on $n$~vertices.
654
655 \proof
656 Apply the reduction from the previous theorem to the decremental algorithm we have
657 developed. This results in an~algorithm of amortized complexity $\O(\log^4\max(m,n))$ where~$m$
658 is the number of operations performed. This could exceed $\O(\log^4 n)$ if
659 $m$~is very large, but we can rebuild the whole structure after $n^2$~operations,
660 which brings $\log m$ down to $\O(\log n)$. The $\O(n^2\log^4 n)$ cost of the
661 rebuild then incurs only $\O(\log^4 n)$ additional cost on each operation.
662 \qed
663
664 \rem
665 The limitation of MSF structures based on the Holm's algorithm for connectivity
666 to only edge deletions seems to be unavoidable. The invariant I3 could be easily
667 broken for many cycles at once whenever a~very light non-tree edge is inserted.
668 We could try increasing the level of the newly inserted edge, but we would quite
669 likely hit I1 before we managed to skip the levels of all the heaviest edges on the
670 particular cycles.
671
672 On the other hand, if we decided to drop I3, we would encounter different obstacles. The ET-trees can
673 bring the lightest non-tree incident with the current tree~$T_1$, but the lightest replacement edge
674 could also be located in the super-trees of~$T_1$ at the lower levels, which are too large to scan
675 and both I1 and I2 prevent us from charging the time on increasing levels there.
676
677 An~interesting special case in which insertions are possible is when all non-tree
678 edges have the same weight. This leads to the following algorithm for dynamic MSF
679 on~graphs with a~small set of allowed edge weights. It is based on an~idea similar
680 to the $\O(k\log^3 n)$ algorithm of Henzinger and King \cite{henzinger:randdyn},
681 but have adapted it to use the better results on dynamic connectivity we have at hand.
682
683 \paran{Dynamic MSF with limited edge weights}%
684 Let us assume for a~while that our graph has edges of only two different weights (let us say
685 1~and~2). We will forget our rule that all edge weights are distinct for a~moment and we recall
686 the observation in \ref{multiweight} that the basic structural properties of
687 the MST's from Section \ref{mstbasics} still hold.
688
689 We split the graph~$G$ to two subgraphs~$G_1$ and~$G_2$ according to the edge
690 weights. We use one instance~$\C_1$ of the dynamic connectivity algorithm to maintain
691 an~arbitrary spanning forest~$F_1$ of~$G_1$, which is obviously minimum. Then we add
692 another instance~$\C_2$ to maintain a~spanning forest~$F_2$ of the graph $G_2\cup F_1$
693 such that all edges of~$F_1$ are forced to be in~$F_2$. Obviously, $F_2$~is the
694 MSF of the whole graph~$G$ --- if any edge of~$F_1$ were not contained in~$\msf(G)$,
695 we could use the standard exchange argument to create an~even lighter spanning tree.
696
697 When a~weight~2 edge is inserted to~$G$, we insert it to~$\C_2$ and it either enters~$F_2$
698 or becomes a~non-tree edge. Similarly, deletion of a~weight~2 edge is a~pure deletion in~$\C_2$,
699 because such edges can be replaced only by other weight~2 edges.
700
701 Insertion of edges of weight~1 needs more attention: We insert the edge to~$\C_1$. If~$F_1$
702 stays unchanged, we are done. If the new edge enters~$F_1$, we use a~Sleator-Tarjan tree
703 kept for~$F_2$ to check if the new edge covers some tree edge of weight~2. If this is not
704 the case, we insert the new edge to~$\C_2$ and hence also to~$F_2$ and we are done.
705 Otherwise we exchange one of the covered weight~2 edges~$f$ for~$e$ in~$\C_2$. We note
706 that~$e$ can inherit the level of~$f$ and $f$~can become a~non-tree edge without
707 changing its level. This adjustment can be performed in time $\O(\log^2 n)$, including
708 paying for the future level increases of the new edge.
709
710 Deletion of weight~1 edges is once more straightforward. We delete the edge from~$\C_1$.
711 If it has no replacement, we delete it from~$\C_2$ as well. If it has a~replacement,
712 we delete the edge from~$\C_2$ and insert the replacement on its place as described
713 above. We observe than this pair of operations causes an~insertion, deletion or
714 a~replacement in~$\C_2$.
715
716 This way, we can handle every insertion and deletion in time $\O(\log^2 n)$ amortized.
717 This construction can be iterated in an~obvious way: if we have $k$~distinct edge weights,
718 we build $k$~connectivity structures $\C_1,\ldots,\C_k$. The structure~$\C_i$ contains edges of
719 weight~$i$ together with the MSF edges from~$\C_{i-1}$. Bounding the time complexity is then easy:
720
721 \thmn{MSF with limited edge weights}
722 There is a~fully dynamic MSF algorithm that works in time $\O(k\cdot\log^2 n)$ amortized
723 per operation for graphs on $n$~vertices with only $k$~distinct edge weights allowed.
724
725 \proof
726 A~change in the graph~$G$ involving an~edge of weight~$w$ causes a~change in~$\C_w$,
727 which can propagate to~$\C_{w+1}$ and so on, possibly up to~$\C_k$. In each~$\C_i$,
728 we spend time $\O(\log^2 n)$ by updating the connectivity structure according to
729 Theorem \ref{dyncon} and $\O(\log n)$ on operations with the Sleator-Tarjan trees
730 by Theorem \ref{sletar}.
731 \qed
732
733 %--------------------------------------------------------------------------------
734
735 \section{Almost minimum trees}\id{kbestsect}%
736
737 In some situations, finding the single minimum spanning tree is not enough and we are interested
738 in the $K$~lightest spanning trees, usually for some small value of~$K$. Katoh, Ibaraki
739 and Mine \cite{katoh:kmin} have given an~algorithm of time complexity $\O(m\log\beta(m,n) + Km)$,
740 building on the MST algorithm of Gabow et al.~\cite{gabow:mst}.
741 Subsequently, Eppstein \cite{eppstein:ksmallest} has discovered an~elegant preprocessing step which allows to reduce
742 the running time to $\O(m\log\beta(m,n) + \min(K^2,Km))$ by eliminating edges
743 which are either present in all $K$ trees or in none of them.
744 We will show a~variant of their algorithm based on the MST verification
745 procedure of Section~\ref{verifysect}.
746
747 In this section, we will require the edge weights to be numeric, because
748 comparisons are certainly not sufficient to determine the second best spanning tree. We will
749 assume that our computation model is able to add, subtract and compare the edge weights
750 in constant time.
751
752 Let us focus on finding the second lightest spanning tree first.
753
754 \paran{Second lightest spanning tree}%
755 Suppose that we have a~weighted graph~$G$ and a~sequence $T_1,\ldots,T_z$ of all its spanning
756 trees. Also suppose that the weights of these spanning trees are distinct and that the sequence
757 is ordered by weight, i.e., $w(T_1) < \ldots < w(T_z)$ and $T_1 = \mst(G)$. Let us observe
758 that each tree is similar to at least one of its predecessors:
759
760 \lemman{Difference lemma}\id{kbl}%
761 For each $i>1$ there exists $j<i$ such that $T_i$ and~$T_j$ differ by a~single edge exchange.
762
763 \proof
764 We know from the Monotone exchange lemma (\ref{monoxchg}) that $T_1$ can be transformed
765 to~$T_i$ by a~sequence of edge exchanges which never decrease tree weight. The last
766 exchange in this sequence therefore obtains~$T_i$ from a~tree of the desired properties.
767 \qed
768
769 \para
770 This lemma implies that the second best spanning tree~$T_2$ differs from~$T_1$ by a~single
771 edge exchange. It remains to find which exchange it is. Let us consider the exchange
772 of an~edge $f\in E\setminus T_1$ with an~edge $e\in T_1[f]$. We get a~tree $T_1-e+f$
773 of weight $w(T_1)-w(e)+w(f)$. To obtain~$T_2$, we have to find~$e$ and~$f$ such that the
774 difference $w(f)-w(e)$ is the minimum possible. Thus for every~$f$, the edge~$e$~must be always
775 the heaviest on the path $T_1[f]$. We can apply the algorithm from Corollary \ref{rampeaks}
776 and find the heaviest edges (peaks) of all such paths and thus examine all possible choices of~$f$
777 in linear time. So we get:
778
779 \lemma
780 For every graph~$H$ and a~MST $T$ of~$H$, linear time is sufficient to find
781 edges $e\in T$ and $f\in H\setminus T$ such that $w(f)-w(e)$ is minimum.
782
783 \nota
784 We will call this procedure \df{finding the best exchange in $(H,T)$.}
785
786 \cor
787 Given~$G$ and~$T_1$, we can find~$T_2$ in time $\O(m)$.
788
789 \paran{Third lightest spanning tree}%
790 Once we know~$T_1$ and~$T_2$, how to get~$T_3$? According to the Difference lemma, $T_3$~can be
791 obtained by a~single exchange from either~$T_1$ or~$T_2$. Therefore we need to find the
792 best exchange for~$T_2$ and the second best exchange for~$T_1$ and use the better of them.
793 The latter is not easy to find directly, so we will make a~minor side step.
794
795 We know that $T_2$ equals $T_1-e+f$ for some edges $e$ and~$f$. We define two auxiliary graphs:
796 $G_1 := G/e$ and $G_2 := G-e$. The tree~$T_1/e$ is obviously the MST of~$G_1$
797 (by the Contraction lemma) and $T_2$ is the MST of~$G_2$ (all $T_2$-light edges
798 in~$G_2$ would be $T_1$-light in~$G$).
799
800 \obs\id{tbobs}%
801 The tree $T_3$~can be obtained by a~single edge exchange in either $(G_1,T_1/e)$ or $(G_2,T_2)$:
802
803 \itemize\ibull
804 \:If $T_3 = T_1-e'+f'$ for $e'\ne e$, then $T_3/e = (T_1/e)-e'+f'$ in~$G_1$.
805 \:If $T_3 = T_1-e+f'$, then $T_3 = T_2 - f + f'$ in~$G_2$.
806 \:If $T_3 = T_2-e'+f'$, then this exchange is found in~$G_2$.
807 \endlist
808
809 \>Conversely, a~single exchange in $(G_1,T_1/e)$ or in $(G_2,T_2)$ corresponds
810 to an~exchange in either~$(G,T_1)$ or $(G,T_2)$.
811 Even stronger, a~spanning tree~$T$ of~$G$ either contains~$e$ and then $T\sgc
812 e$ is a~spanning tree of~$G_1$, or $T$~doesn't contain~$e$ and so it is
813 a~spanning tree of~$G_2$.
814
815 Thus we can run the previous algorithm for finding the best edge exchange
816 on both~$G_1$ and~$G_2$ and find~$T_3$ again in time $\O(m)$.
817
818 \paran{Further spanning trees}%
819 The construction of auxiliary graphs can be iterated to obtain $T_1,\ldots,T_K$
820 for an~arbitrary~$K$. We will build a~\df{meta-tree} of auxiliary graphs. Each node of this meta-tree
821 carries a~graph\foot{This graph is always derived from~$G$ by a~sequence of edge deletions
822 and contractions. It is tempting to say that it is a~minor of~$G$, but this is not true as we
823 preserve multiple edges.} and its minimum spanning tree. The root node contains~$(G,T_1)$,
824 its sons have $(G_1,T_1/e)$ and $(G_2,T_2)$. When $T_3$ is obtained by an~exchange
825 in one of these sons, we attach two new leaves to that son and we let them carry the two auxiliary
826 graphs derived by contracting or deleting the exchanged edge. Then we find the best
827 edge exchanges among all leaves of the new meta-tree and repeat the process. By Observation \ref{tbobs},
828 each spanning tree of~$G$ is generated exactly once. The Difference lemma guarantees that
829 the trees are enumerated in the increasing order.
830
831 Recalculating the best exchanges in all leaves of the meta-tree after generating each~$T_i$
832 is of course not necessary, because most leaves stay unchanged. We will rather remember
833 the best exchange for each leaf and keep the weight differences of these exchanges in a~heap. In every step, we will
834 delete the minimum from the heap and use the exchange in the particular leaf to generate
835 a~new spanning tree. Then we will create the new leaves, calculate their best exchanges and insert
836 them into the heap. The algorithm is now straightforward and so will be its analysis:
837
838 \algn{Finding $K$ best spanning trees}\id{kbestalg}%
839 \algo
840 \algin A~weighted graph~$G$, its MST~$T_1$ and an~integer $K>0$.
841 \:$R\=$ a~meta tree whose vertices carry triples $(G',T',F')$. Initially
842   it contains just a~root with $(G,T_1,\emptyset)$.
843   \hfil\break
844   \cmt{$G'$ is a~graph, $T'$~is its MST, and~$F'$ is a~set of edges of~$G$
845   that are contracted in~$G'$.}
846 \:$H\=$ a~heap of quadruples $(\delta,r,e,f)$ ordered on~$\delta$, initially empty.
847   \hfil\break
848   \cmt{Each quadruple describes an~exchange of~$e$ for~$f$ in a~leaf~$r$ of~$R$ and $\delta=w(f)-w(e)$
849   is the weight gain of this exchange.}
850 \:Find the best edge exchange in~$(G,T_1)$ and insert it to~$H$.
851 \:$i\= 1$.
852 \:While $i<K$:
853 \::Delete the minimum quadruple $(\delta,r,e,f)$ from~$H$.
854 \::$(G',T',F') \=$ the triple carried by the leaf~$r$.
855 \::$i\=i+1$.
856 \::$T_i\=(T'-e+f) \cup F'$. \cmt{The next spanning tree}
857 \::$r_1\=$ a~new leaf carrying $(G'/e,T'/e,F'+e)$.
858 \::$r_2\=$ a~new leaf carrying $(G'-e,T_i,F')$.
859 \::Attach~$r_1$ and~$r_2$ as sons of~$r$.
860 \::Find the best edge exchanges in~$r_1$ and~$r_2$ and insert them to~$H$.
861 \algout The spanning trees $T_2,\ldots,T_K$.
862 \endalgo
863
864 \lemma\id{kbestl}%
865 Given~$G$ and~$T_1$, we can find $T_2,\ldots,T_K$ in time $\O(Km + K\log K)$.
866
867 \proof
868 Generating each~$T_i$ requires finding the best exchange for two graphs and also $\O(1)$
869 operations on the heap. The former takes $\O(m)$ according to Corollary \ref{rampeaks},
870 and each heap operation takes $\O(\log K)$.
871 \qed
872
873 \rem
874 The meta-tree is not needed for the actual operation of the algorithm --- it suffices
875 to keep its leaves in the heap.
876
877 \paran{Arbitrary weights}%
878 While the assumption that the weights of all spanning trees are distinct has helped us
879 in thinking about the problem, we should not forget that it is somewhat unrealistic.
880 We could refine the proof of our algorithm and demonstrate that the algorithm indeed works
881 without this assumption, but we will rather show that the ties can be broken easily.
882
883 Let~$\delta$ be the minimum positive difference among the weights of all spanning trees
884 of~$G$ and $e_1,\ldots,e_m$ be the edges of~$G$. We observe that it suffices to
885 increase $w(e_i)$ by~$\delta_i = \delta/2^{i+1}$. The cost of every spanning tree
886 has increased by at most $\sum_i\delta_i < \delta/2$, so if $T$~was lighter
887 than~$T'$, it still is. On the other hand, no two trees share the same
888 weight adjustment, so all tree weights are now distinct.
889
890 The exact value of~$\delta$ is not easy to calculate, but closer inspection of the algorithm
891 reveals that it is not needed at all. The only place where the edge weights are examined
892 is when we search for the best exchange. In this case, we compare the differences of
893 pairs of edge weights with each other. Each such difference is therefore adjusted
894 by $\delta\cdot(2^{-i}-2^{-j})$ for some $i,j>1$, which again does not influence comparison
895 of originally distinct differences. If two differences were identical, it is sufficient
896 to look at their values of~$i$ and~$j$, i.e., at the identifiers of the edges.
897
898 \paran{Invariant edges}%
899 Our algorithm can be further improved for small values of~$K$ (which seems to be the common
900 case in most applications) by the reduction of Eppstein \cite{eppstein:ksmallest}.
901 We will observe that there are many edges of~$T_1$
902 which are guaranteed to be contained in $T_2,\ldots,T_K$ as well, and likewise there are
903 many edges of $G\setminus T_1$ which are excluded from all those spanning trees.
904 The idea is the following (again assuming that the tree weights are distinct):
905
906 \defn
907 For an~edge $e\in T_1$, we define its \df{gain} $g(e)$ as the minimum weight gained by exchanging~$e$
908 for another edge. Similarly, we define the gain $G(f)$ for $f\not\in T_1$. Put formally:
909 $$\eqalign{
910 g(e) &:= \min\{ w(f)-w(e) \mid f\in E, e\in T[f] \}, \cr
911 G(f) &:= \min\{ w(f)-w(e) \mid e\in T[f] \}.\cr
912 }$$
913
914 \lemma\id{gaina}%
915 When $t_1,\ldots,t_{n-1}$ are the edges of~$T_1$ in order of increasing gain,
916 the edges $t_K,\ldots,t_{n-1}$ are present in all trees $T_2,\ldots,T_K$.
917
918 \proof
919 The best exchanges in~$T_1$ involving $t_1,\ldots,t_{K-1}$ produce~$K-1$ spanning trees
920 of increasing weights. Any exchange involving $t_K,\ldots,t_n$ produces a~tree
921 which is heavier or equal than all those trees. (We are ascertained by the Monotone exchange lemma
922 that the gain of such exchanges need not be reverted later.)
923 \qed
924
925 \lemma\id{gainb}%
926 When $q_1,\ldots,q_{m-n+1}$ are the edges of $G\setminus T_1$ in order of increasing gain,
927 the edges $q_K,\ldots,q_{m-n+1}$ are not present in any of $T_2,\ldots,T_K$.
928
929 \proof
930 Similar to the previous lemma.
931 \qed
932
933 \para
934 It is therefore sufficient to find $T_2,\ldots,T_K$ in the graph obtained from~$G$ by
935 contracting the edges $t_K,\ldots,t_n$ and deleting $q_K,\ldots,q_{m-n+1}$. This graph
936 has only $\O(K)$ vertices and $\O(K)$ edges. The only remaining hurdle is how to
937 calculate the gains. For edges outside~$T_1$, it again suffices to find the peaks of the
938 covered paths. The gains of MST edges require a~different algorithm, but Tarjan \cite{tarjan:applpc}
939 has shown how to obtain them in time $\O(m\timesalpha(m,n))$.
940
941 When we put the results of this section together, we can conclude:
942
943 \thmn{Finding $K$ lightest spanning trees}\id{kbestthm}%
944 For a~given graph~$G$ with real edge weights and a~positive integer~$K$, the $K$~best spanning trees can be found
945 in time $\O(m\timesalpha(m,n) + \min(K^2,Km + K\log K))$.
946
947 \proof
948 First we find the MST of~$G$ in time $\O(m\timesalpha(m,n))$ using the Pettie's Optimal
949 MST algorithm (Theorem \ref{optthm}). Then we calculate the gains of MST edges by the
950 Tarjan's algorithm from \cite{tarjan:applpc}, again in $\O(m\timesalpha(m,n))$, and
951 the gains of the other edges using our MST verification algorithm (Corollary \ref{rampeaks})
952 in $\O(m)$. We use Lemma \ref{gaina} to identify edges that are required, and Lemma \ref{gainb}
953 to find edges that are superfluous. We contract the former edges, remove the latter ones
954 and run Algorithm \ref{kbestalg} to find the spanning trees. By Lemma \ref{kbestl}, it runs in
955 the desired time.
956
957 If~$K\ge m$, this reduction does not pay off, so we run Algorithm \ref{kbestalg}
958 directly on the input graph.
959 \qed
960
961 \paran{Improvements}%
962 It is an~interesting open question whether the algorithms of Section \ref{verifysect} can
963 be modified to calculate all gains in linear time. The main procedure could be, but it requires having
964 the input reduced to a~balance tree beforehand and here the Bor\o{u}vka trees fail. The Buchsbaum's
965 Pointer-Machine algorithm (\ref{pmverify}) seems to be more promising.
966
967 \paran{Large~$K$}%
968 When $K$~is large, re-running the verification algorithm for every change of the graph
969 is too costly. Frederickson \cite{frederickson:ambivalent} has shown how to find the best
970 swaps dynamically, reducing the overall time complexity of Algorithm \ref{kbestalg}
971 to $\O(Km^{1/2})$ and improving the bound in Theorem \ref{kbestthm} to $\O(m\timesalpha(m,n)
972 + \min( K^{3/2}, Km^{1/2} ))$. It is open if the dynamic data structures of this
973 chapter could be modified to bring the complexity of finding the next tree down
974 to polylogarithmic.
975
976 \paran{Multiple minimum trees}%
977 Another nice application of Theorem \ref{kbestthm} is finding all minimum spanning
978 trees in a~graph that does not have distinct edge weights. We find a~single MST using
979 any of the algorithms of the previous chapters and then we use the enumeration algorithm
980 of this section to find further spanning trees as long as their weights are equal to the minimum.
981
982 We can even use the reduction of the number of edges from Lemmata \ref{gaina} and \ref{gainb}:
983 we start with some fixed~$K$ and when we exhaust all~$K$ trees, we double~$K$ and restart
984 the whole process. The extra time spent on these restarts is dominated by the time of the
985 final pass.
986
987 This finally settles the question that we have asked ourselves in Section \ref{mstbasics},
988 namely whether we lose anything by assuming that all weights are distinct and by searching
989 for just the single minimum tree.
990
991 \endpart