]> mj.ucw.cz Git - saga.git/blob - abstract.tex
BUGS: Little ones to fix
[saga.git] / abstract.tex
1 \input macros.tex
2 \input fonts10.tex
3
4 \finaltrue
5 \hwobble=0mm
6 \advance\hsize by 1cm
7 \advance\vsize by 20pt
8
9 \def\rawchapter#1{\vensure{0.5in}\bigskip\goodbreak
10 \leftline{\chapfont #1}
11 }
12
13 \def\rawsection#1{\medskip\smallskip
14 \leftline{\secfont #1}
15 \nobreak
16 \smallskip
17 \nobreak
18 }
19
20 \def\schapter#1{\chapter{#1}\medskip}
21
22 \schapter{Introduction}
23
24 This thesis tells the story of two well-established problems of algorithmic
25 graph theory: the minimum spanning trees and ranks of permutations. At distance,
26 both problems seem to be simple, boring and already solved, because we have poly\-nom\-ial-time
27 algorithms for them since ages. But when we come closer and seek algorithms that
28 are really efficient, the problems twirl and twist and withstand many a~brave
29 attempt at the optimum solution. They also reveal a~vast and diverse landscape
30 of a~deep and beautiful theory. Still closer, this landscape turns out to be interwoven
31 with the intricate details of various models of computation and even of arithmetics
32 itself.
33
34 We have tried to cover all known important results on both problems and unite them
35 in a~single coherent theory. At many places, we have attempted to contribute our own
36 little stones to this mosaic: several new results, simplifications of existing
37 ones, and last, but not least filling in important details where the original
38 authors have missed some.
39
40 When compared with the earlier surveys on the minimum spanning trees, most
41 notably Graham and Hell \cite{graham:msthistory} and Eisner \cite{eisner:tutorial},
42 this work adds many of the recent advances, the dynamic algorithms and
43 also the relationship with computational models. No previous work covering
44 the ranking problems in their entirety is known.
45
46 We~have tried to stick to the usual notation except where it was too inconvenient.
47 Most symbols are defined at the place where they are used for the first time.
48 To avoid piling up too many symbols at places that speak about a~single fixed graph,
49 this graph is always called~$G$, its set of vertices and edges are denoted by $V$
50 and~$E$ respectively, and we~also use~$n$ for the number of its vertices and $m$~for
51 the number of edges. At places where there could be a~danger of confusion, more explicit notation
52 is used instead.
53
54 \chapter{Minimum Spanning Trees}
55
56 \section{The Problem}
57
58 The problem of finding a minimum spanning tree of a weighted graph is one of the
59 best studied problems in the area of combinatorial optimization since its birth.
60 Its colorful history (see \cite{graham:msthistory} and \cite{nesetril:history} for the full account)
61 begins in~1926 with the pioneering work of Bor\o{u}vka
62 \cite{boruvka:ojistem}\foot{See \cite{nesetril:boruvka} for an English translation with commentary.},
63 who studied primarily an Euclidean version of the problem related to planning
64 of electrical transmission lines (see \cite{boruvka:networks}), but gave an efficient
65 algorithm for the general version of the problem. As it was well before the dawn of graph
66 theory, the language of his paper was complicated, so we will better state the problem
67 in contemporary terminology:
68
69 \proclaim{Problem}Given an undirected graph~$G$ with weights $w:E(G)\rightarrow {\bb R}$,
70 find its minimum spanning tree, defined as follows:
71
72 \defn\id{mstdef}%
73 For a given graph~$G$ with weights $w:E(G)\rightarrow {\bb R}$:
74 \itemize\ibull
75 \:A~subgraph $H\subseteq G$ is called a \df{spanning subgraph} if $V(H)=V(G)$.
76 \:A~\df{spanning tree} of~$G$ is any spanning subgraph of~$G$ that is a tree.
77 \:For any subgraph $H\subseteq G$ we define its \df{weight} $w(H):=\sum_{e\in E(H)} w(e)$.
78 \:A~\df{minimum spanning tree (MST)} of~$G$ is a spanning tree~$T$ such that its weight $w(T)$
79   is the smallest possible among all the spanning trees of~$G$.
80 \:For a disconnected graph, a \df{(minimum) spanning forest (MSF)} is defined as
81   a union of (minimum) spanning trees of its connected components.
82 \endlist
83
84 Bor\o{u}vka's work was further extended by Jarn\'\i{}k \cite{jarnik:ojistem}, again in
85 mostly geometric setting, and he has discovered another efficient algorithm.
86 In the next 50 years, several significantly faster algorithms were published, ranging
87 from the $\O(m\timesbeta(m,n))$ time algorithm by Fredman and Tarjan \cite{ft:fibonacci},
88 over algorithms with inverse-Ackermann type complexity by Chazelle \cite{chazelle:ackermann}
89 and Pettie \cite{pettie:ackermann}, to an~algorithm by Pettie \cite{pettie:optimal}
90 whose time complexity is provably optimal.
91
92 Before we discuss the algorithms, let us review the basic properties of spanning trees.
93 We will mostly follow the theory developed by Tarjan in~\cite{tarjan:dsna} and show
94 that the weights on edges are not necessary for the definition of the MST.
95
96 \defnn{Heavy and light edges}\id{heavy}%
97 Let~$G$ be a~connected graph with edge weights~$w$ and $T$ its spanning tree. Then:
98 \itemize\ibull
99 \:For vertices $x$ and $y$, let $T[x,y]$ denote the (unique) path in~$T$ joining $x$ with~$y$.
100 \:For an edge $e=xy$ we will call $T[e]:=T[x,y]$ the \df{path covered by~$e$} and
101   the edges of this path \df{edges covered by~$e$}.
102 \: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
103   is an~edge $f\in T[e]$ such that $w(f) > w(e)$.
104 \:An edge~$e$ is called \df{$T$-heavy} if it covers a~lighter edge.
105 \endlist
106
107 \thm
108 A~spanning tree~$T$ is minimum iff there is no $T$-light edge.
109
110 \thm
111 If all edge weights are distinct, then the minimum spanning tree is unique.
112
113 \para
114 When $G$ is a graph with distinct edge weights, we will use $\mst(G)$ to denote
115 its unique minimum spanning tree.
116 To simplify the description of MST algorithms, we will assume that the weights
117 of all edges are distinct and that instead of numeric weights we are given a~\df{comparison oracle.}
118 The oracle is a~function that answers questions of type ``Is $w(e)<w(f)$?'' in
119 constant time. This will conveniently shield us from problems with representation
120 of real numbers in algorithms and in the few cases where we need a more concrete
121 input, we will explicitly state so. In case the weights are not distinct, the ties
122 can be broken arbitrarily.
123
124 \section{Classical algorithms}
125
126 The characterization of MST's in terms of light edges makes it easy to develop
127 the Tarjan's Red-Blue meta-algorithm, which is based on the following properties:
128
129 \lemman{Blue lemma, also known as the Cut rule}\id{bluelemma}%
130 The lightest edge of every cut is contained in the MST.
131
132 \lemman{Red lemma, also known as the Cycle rule}\id{redlemma}%
133 An~edge~$e$ is not contained in the MST iff it is the heaviest on some cycle.
134
135 The algorithm repeatedly colors lightest edges of cuts blue and heaviest
136 edges of cycles red. We prove that no matter which order of the colorings
137 we use, the algorithm always stops and the blue edges form the MST.
138
139 All three classical MST algorithms (Bor\o{u}vka's, Jarn\'\i{}k's and Kruskal's)
140 can be then obtained as specializations of this procedure. We also calculate the
141 time complexity of standard implementations of these algorithms.
142
143 \algn{Bor\o{u}vka \cite{boruvka:ojistem}, Choquet \cite{choquet:mst}, Sollin \cite{sollin:mst}, and others}
144 \algo
145 \algin A~graph~$G$ with an edge comparison oracle.
146 \:$T\=$ a forest consisting of vertices of~$G$ and no edges.
147 \:While $T$ is not connected, perform a~\df{Bor\o{u}vka step:}
148 \::For each component $T_i$ of~$T$, choose the lightest edge $e_i$ from the cut
149    separating $T_i$ from the rest of~$T$.
150 \::Add all $e_i$'s to~$T$.
151 \algout Minimum spanning tree~$T$.
152 \endalgo
153
154 \thm
155 The Bor\o{u}vka's algorithm finds the MST in time $\O(m\log n)$.
156
157 \algn{Jarn\'\i{}k \cite{jarnik:ojistem}, Prim \cite{prim:mst}, Dijkstra \cite{dijkstra:mst}}\id{jarnik}%
158 \algo
159 \algin A~graph~$G$ with an edge comparison oracle.
160 \:$T\=$ a single-vertex tree containing an~arbitrary vertex of~$G$.
161 \:While there are vertices outside $T$:
162 \::Pick the lightest edge $uv$ such that $u\in V(T)$ and $v\not\in V(T)$.
163 \::$T\=T+uv$.
164 \algout Minimum spanning tree~$T$.
165 \endalgo
166
167 \thm
168 The Jarn\'\i{}k's algorithm computes the MST of a~given graph in time $\O(m\log n)$.
169
170 \algn{Kruskal \cite{kruskal:mst}}
171 \algo
172 \algin A~graph~$G$ with an edge comparison oracle.
173 \:Sort edges of~$G$ by their increasing weights.
174 \:$T\=\hbox{an empty spanning subgraph}$.
175 \:For all edges $e$ in their sorted order:
176 \::If $T+e$ is acyclic, add~$e$ to~$T$.
177 \::Otherwise drop~$e$.
178 \algout Minimum spanning tree~$T$.
179 \endalgo
180
181 \thm
182 The Kruskal's algorithm finds the MST of the graph given as input in time $\O(m\log n)$.
183 If the edges are already sorted by their weights, the time drops to
184 $\O(m\timesalpha(m,n))$, where $\alpha(m,n)$ is a~certain inverse of the Ackermann's
185 function.
186
187 \section{Contractive algorithms}\id{contalg}%
188
189 While the classical algorithms are based on growing suitable trees, they
190 can be also reformulated in terms of edge contraction. Instead of keeping
191 a~forest of trees, we can keep each tree contracted to a single vertex.
192 This replaces the relatively complex tree-edge incidencies by simple
193 vertex-edge incidencies, potentially speeding up the calculation at the
194 expense of having to perform the contractions.
195 A~contractive version of the Bor\o{u}vka's algorithm is easy to formulate
196 and also to analyse:
197
198 \algn{Contractive version of Bor\o{u}vka's algorithm}\id{contbor}%
199 \algo
200 \algin A~graph~$G$ with an edge comparison oracle.
201 \:$T\=\emptyset$.
202 \:$\ell(e)\=e$ for all edges~$e$. \cmt{Initialize edge labels.}
203 \:While $n(G)>1$:
204 \::For each vertex $v_k$ of~$G$, let $e_k$ be the lightest edge incident to~$v_k$.
205 \::$T\=T\cup \{ \ell(e_1),\ldots,\ell(e_n) \}$.\cmt{Remember labels of all selected edges.}
206 \::Contract all edges $e_k$, inheriting labels and weights.\foot{In other words, we will ask the comparison oracle for the edge $\ell(e)$ instead of~$e$.}
207 \::Flatten $G$ (remove parallel edges and loops).
208 \algout Minimum spanning tree~$T$.
209 \endalgo
210
211 \thm
212 The Contractive Bor\o{u}vka's algorithm finds the MST of the graph given as
213 its input in time $\O(\min(n^2,m\log n))$.
214
215 We also show that this time bound is tight --- we construct an~explicit
216 family of graphs on which the algorithm spends $\Theta(m\log n)$ steps.
217 Given a~planar graph, the algorithm however runs much faster (we get a~linear-time
218 algorithm much simpler than the one of Matsui \cite{matsui:planar}):
219
220 \thm
221 When the input graph is planar, the Contractive Bor\o{u}vka's algorithm runs in
222 time $\O(n)$.
223
224 Graph contractions are indeed a~very powerful tool and they can be used in other MST
225 algorithms as well. The following lemma shows the gist:
226
227 \lemman{Contraction lemma}\id{contlemma}%
228 Let $G$ be a weighted graph, $e$~an arbitrary edge of~$\mst(G)$, $G/e$ the multigraph
229 produced by contracting~$e$ in~$G$, and $\pi$ the bijection between edges of~$G-e$ and
230 their counterparts in~$G/e$. Then $\mst(G) = \pi^{-1}[\mst(G/e)] + e.$
231
232 \chapter{Fine Details of Computation}
233
234 \section{Models and machines}
235
236 Traditionally, computer scientists have been using a~variety of computational models
237 as a~formalism in which their algorithms are stated. If we were studying
238 NP-complete\-ness, we could safely assume that all these models are equivalent,
239 possibly up to polynomial slowdown which is negligible. In our case, the
240 differences between good and not-so-good algorithms are on a~much smaller
241 scale, so we need to state our computation models carefully and develop
242 a repertoire of basic data structures tailor-made for the fine details of the
243 models. In recent decades, most researchers in the area of combinatorial algorithms
244 have been considering the following two computational models, and we will do likewise.
245
246 The \df{Random Access Machine (RAM)} is not a~single coherent model, but rather a~family
247 of closely related machines (See Cook and Reckhow \cite{cook:ram} for one of the usual formal definitions
248 and Hagerup \cite{hagerup:wordram} for a~thorough description of the differences
249 between the RAM variants.) We will consider the variant usually called the \df{Word-RAM.}
250 It allows the ``C-language operators'', i.e., arithmetics and bitwise logical operations,
251 running in constant time on words of a~specified size.
252
253 The \df{Pointer Machine (PM)} also does not seem to have any well established definition.
254 The various kinds of pointer machines are examined by Ben-Amram in~\cite{benamram:pm},
255 but unlike the RAM's they turn out to be equivalent up to constant slowdown.
256 Our formal definition is closely related to the \em{linking automaton} proposed
257 by Knuth in~\cite{knuth:fundalg}.
258
259 \section{Bucket sorting and related techniques}\id{bucketsort}%
260
261 In the Contractive Bor\o{u}vka's algorithm, we needed to contract a~given
262 set of edges in the current graph and then flatten the graph, all this in time $\O(m)$.
263 This can be easily handled on both the RAM and the PM by bucket sorting. We develop
264 a~bunch of pointer-based sorting techniques which can be summarized by the following
265 lemma:
266
267 \lemma
268 Partitioning of a~collection of sequences $S_1,\ldots,S_n$, whose elements are
269 arbitrary pointers and symbols from a~finite alphabet, to equality classes can
270 be performed on the Pointer Machine in time $\O(n + \sum_i \vert S_i \vert)$.
271
272 \para
273 A~direct consequence of this unification is a~linear-time algorithm for subtree
274 isomorphism, significantly simpler than the standard one due to Zemlayachenko (see \cite{zemlay:treeiso}
275 and also Dinitz et al.~\cite{dinitz:treeiso}). When we apply a~similar technique
276 to general graphs, we get the framework of topological graph computation
277 of Buchsbaum et al.~\cite{buchsbaum:verify}.
278
279 \defn
280 A~\df{graph computation} is a~function that takes a~\df{labeled undirected graph} as its input. The labels of
281 vertices and edges can be arbitrary symbols drawn from a~finite alphabet. The output
282 of the computation is another labeling of the same graph. This time, the vertices and
283 edges can be labeled with not only symbols of the alphabet, but also with pointers to the vertices
284 and edges of the input graph, and possibly also with pointers to outside objects.
285 A~graph computation is called \df{topological} if it produces isomorphic
286 outputs for isomorphic inputs. The isomorphism of course has to preserve not only
287 the structure of the graph, but also the labels in the obvious way.
288
289 \defn
290 For a~collection~$\C$ of graphs, we define $\vert\C\vert$ as the number of graphs in
291 the collection and $\Vert\C\Vert$ as their total size, i.e., $\Vert\C\Vert = \sum_{G\in\C} n(G) + m(G)$.
292
293 \thm
294 Suppose that we have a~topological graph computation~$\cal T$ that can be performed in time
295 $T(k)$ for graphs on $k$~vertices. Then we can run~$\cal T$ on a~collection~$\C$
296 of labeled graphs on~$k$ vertices in time $\O(\Vert\C\Vert + (k+s)^{k(k+2)}\cdot (T(k)+k^2))$,
297 where~$s$ is a~constant depending only on the number of symbols used as vertex/edge labels.
298
299 \section{Data structures on the RAM}\id{ramds}%
300
301 There is a~lot of data structures designed specifically for the RAM. These structures
302 take advantage of both indexing and arithmetics and they often surpass the known
303 lower bounds for the same problem on the~PM. In many cases, they achieve constant time
304 per operation, at least when either the magnitude of the values or the size of
305 the data structure is suitably bounded.
306
307 A~classical result of this type is the tree of van Emde Boas~\cite{boas:vebt}
308 which represents a~subset of the integers $\{0,\ldots,U-1\}$. It allows insertion,
309 deletion and order operations (minimum, maximum, successor etc.) in time $\O(\log\log U)$,
310 regardless of the size of the subset. If we replace the heap used in the Jarn\'\i{}k's
311 algorithm (\ref{jarnik}) by this structure, we immediately get an~algorithm
312 for finding the MST in integer-weighted graphs in time $\O(m\log\log w_{max})$,
313 where $w_{max}$ is the maximum weight.
314
315 A~real breakthrough has however been made by Fredman and Willard who introduced
316 the Fusion trees~\cite{fw:fusion}. They again perform membership and predecessor
317 operation on a~set of $n$~integers, but with time complexity $\O(\log_W n)$
318 per operation on a~Word-RAM with $W$-bit words. This of course assumes that
319 each element of the set fits in a~single word. As $W$ must at least~$\log n$,
320 the operations take $\O(\log n/\log\log n)$ time and thus we are able to sort $n$~integers
321 in time~$o(n\log n)$. This was further improved by Han and Thorup \cite{han:detsort,hanthor:randsort}.
322
323 The Fusion trees themselves have very limited use in graph algorithms, but the
324 principles behind them are ubiquitous in many other data structures and these
325 will serve us well and often. We are going to build the theory of Q-heaps,
326 which will later lead to a~linear-time MST algorithm for arbitrary integer weights.
327 Other such structures will help us in building linear-time RAM algorithms for computing the ranks
328 of various combinatorial structures in Chapter~\ref{rankchap}.
329
330 Outside our area, important consequences of RAM data structures include the
331 Thorup's $\O(m)$ algorithm for single-source shortest paths in undirected
332 graphs with positive integer weights \cite{thorup:usssp} and his $\O(m\log\log
333 n)$ algorithm for the same problem in directed graphs \cite{thorup:sssp}. Both
334 algorithms have been then significantly simplified by Hagerup
335 \cite{hagerup:sssp}.
336
337 Despite the progress in the recent years, the corner-stone of all RAM structures
338 is still the representation of combinatorial objects by integers introduced by
339 Fredman and Willard.
340 First of all, we observe that we can encode vectors in integers:
341
342 \notan{Bit strings}\id{bitnota}%
343 We will work with binary representations of natural numbers by strings over the
344 alphabet $\{\0,\1\}$: we will use $\(x)$ for the number~$x$ written in binary,
345 $\(x)_b$ for the same padded to exactly $b$ bits by adding leading zeroes,
346 and $x[k]$ for the value of the $k$-th bit of~$x$ (with a~numbering of bits such that $2^k[k]=1$).
347 The usual conventions for operations on strings will be utilized: When $s$
348 and~$t$ are strings, we write $st$ for their concatenation and
349 $s^k$ for the string~$s$ repeated $k$~times.
350 When the meaning is clear from the context,
351 we will use $x$ and $\(x)$ interchangeably to avoid outbreak of symbols.
352
353 \defn
354 The \df{bitwise encoding} of a~vector ${\bf x}=(x_0,\ldots,x_{d-1})$ of~$b$-bit numbers
355 is an~integer~$x$ such that $\(x)=\(x_{d-1})_b\0\(x_{d-2})_b\0\ldots\0\(x_0)_b$. In other
356 words, $x = \sum_i 2^{(b+1)i}\cdot x_i$. (We have interspersed the elements with \df{separator bits.})
357
358 \para
359 If we want to fit the whole vector in a~single machine word, the parameters $b$ and~$d$ must satisfy
360 the condition $(b+1)d\le W$ (where $W$~is the word size of the machine).
361 By using multiple-precision arithmetics, we can encode all vectors satisfying $bd=\O(W)$.
362 We describe how to translate simple vector manipulations to sequences of $\O(1)$ RAM operations
363 on their codes. For example, we can handle element-wise comparison of vectors, insertion
364 in a~sorted vector or shuffling elements of a~vector according to a~fixed permutation,
365 all in $\O(1)$ time. This also implies that several functions on numbers can be performed
366 in constant time, most notably binary logarithms.
367 The vector operations then serve as building blocks for construction of the Q-heaps. We get:
368
369 \thm
370 Let $W$ and~$k$ be positive integers such that $k=\O(W^{1/4})$. Let~$Q$
371 be a~Q-heap of at most $k$-elements of $W$~bits each. Then we can perform
372 Q-heap operations on~$Q$ (insertion, deletion, search for a~given value and search
373 for the $i$-th smallest element) in constant time on a~Word-RAM with word size~$W$,
374 after spending time $\O(2^{k^4})$ on the same RAM on precomputing of tables.
375
376 \cor
377 For every positive integer~$r$ and $\delta>0$ there exists a~data structure
378 capable of maintaining the minimum of a~set of at most~$r$ word-sized numbers
379 under insertions and deletions. Each operation takes $\O(1)$ time on a~Word-RAM
380 with word size $W=\Omega(r^{\delta})$, after spending time
381 $\O(2^{r^\delta})$ on precomputing of tables.
382
383 \chapter{Advanced MST Algorithms}
384
385 \section{Minor-closed graph classes}\id{minorclosed}%
386
387 The contractive algorithm given in Section~\ref{contalg} has been found to perform
388 well on planar graphs, but in general its time complexity was not linear.
389 Can we find any broader class of graphs where the linear bound holds?
390 The right context turns out to be the minor-closed classes, which are
391 closed under contractions and have bounded density.
392
393 \defn\id{minordef}%
394 A~graph~$H$ is a \df{minor} of a~graph~$G$ (written as $H\minorof G$) iff it can be obtained
395 from a~subgraph of~$G$ by a sequence of simple graph contractions.
396
397 \defn
398 A~class~$\cal C$ of graphs is \df{minor-closed}, when for every $G\in\cal C$ and
399 every minor~$H$ of~$G$, the graph~$H$ lies in~$\cal C$ as well. A~class~$\cal C$ is called
400 \df{non-trivial} if at least one graph lies in~$\cal C$ and at least one lies outside~$\cal C$.
401
402 \example
403 Non-trivial minor-closed classes include:
404 planar graphs,
405 graphs embeddable in any fixed surface (i.e., graphs of bounded genus),
406 graphs embeddable in~${\bb R}^3$ without knots or without interlocking cycles,
407 and graphs of bounded tree-width or path-width.
408
409 \para
410 Many of the nice structural properties of planar graphs extend to
411 minor-closed classes, too (see Lov\'asz \cite{lovasz:minors} for a~nice survey
412 of this theory and Diestel \cite{diestel:gt} for some of the deeper results).
413 For analysis of the contractive algorithm, we will make use of the bounded
414 density of minor-closed classes:
415
416 \defn\id{density}%
417 Let $G$ be a~graph and $\cal C$ be a class of graphs. We define the \df{edge density}
418 $\varrho(G)$ of~$G$ as the average number of edges per vertex, i.e., $m(G)/n(G)$. The
419 edge density $\varrho(\cal C)$ of the class is then defined as the infimum of $\varrho(G)$ over all $G\in\cal C$.
420
421 \thmn{Density of minor-closed classes, Mader~\cite{mader:dens}}
422 Every non-trivial minor-closed class of graphs has finite edge density.
423
424 \thmn{MST on minor-closed classes, Mare\v{s} \cite{mm:mst}}\id{mstmcc}%
425 For any fixed non-trivial minor-closed class~$\cal C$ of graphs, the Contractive Bor\o{u}vka's
426 algorithm (\ref{contbor}) finds the MST of any graph of this class in time
427 $\O(n)$. (The constant hidden in the~$\O$ depends on the class.)
428
429 \paran{Local contractions}\id{nobatch}%
430 The contractive algorithm uses ``batch processing'' to perform many contractions
431 in a single step. It is also possible to perform them one edge at a~time,
432 batching only the flattenings. A~contraction of an edge~$uv$ can be done in time~$\O(\deg(u))$,
433 so we have to make sure that there is a~steady supply of low-degree vertices.
434 It indeed is in minor-closed classes:
435
436 \lemman{Low-degree vertices}\id{lowdeg}%
437 Let $\cal C$ be a graph class with density~$\varrho$ and $G\in\cal C$ a~graph
438 with $n$~vertices. Then at least $n/2$ vertices of~$G$ have degree at most~$4\varrho$.
439
440 This leads to the following algorithm:
441
442 \algn{Local Bor\o{u}vka's Algorithm, Mare\v{s} \cite{mm:mst}}%
443 \algo
444 \algin A~graph~$G$ with an edge comparison oracle and a~parameter~$t\in{\bb N}$.
445 \:$T\=\emptyset$.
446 \:$\ell(e)\=e$ for all edges~$e$.
447 \:While $n(G)>1$:
448 \::While there exists a~vertex~$v$ such that $\deg(v)\le t$:
449 \:::Select the lightest edge~$e$ incident with~$v$.
450 \:::Contract~$e$.
451 \:::$T\=T + \ell(e)$.
452 \::Flatten $G$, removing parallel edges and loops.
453 \algout Minimum spanning tree~$T$.
454 \endalgo
455
456 \thm
457 When $\cal C$ is a minor-closed class of graphs with density~$\varrho$, the
458 Local Bor\o{u}vka's Algorithm with the parameter~$t$ set to~$4\varrho$ 
459 finds the MST of any graph from this class in time $\O(n)$. (The constant
460 in the~$\O$ depends on~the class.)
461
462 \section{Iterated algorithms}\id{iteralg}%
463
464 We have seen that the Jarn\'\i{}k's Algorithm \ref{jarnik} runs in $\Theta(m\log n)$ time.
465 Fredman and Tarjan \cite{ft:fibonacci} have shown a~faster implementation using their Fibonacci
466 heaps, which runs in time $\O(m+n\log n)$. This is $\O(m)$ whenever the density of the
467 input graph reaches $\Omega(\log n)$. This suggests that we could combine the algorithm with
468 another MST algorithm, which identifies a~subset of the MST edges and contracts
469 them to increase the density of the graph. For example, if we perform several Bor\o{u}vka
470 steps and then we run the Jarn\'\i{}k's algorithm, we find the MST in time $\O(m\log\log n)$.
471
472 Actually, there is a~much better choice of the algorithms to combine: use the
473 Jarn\'\i{}k's algorithm with a~Fibonacci heap multiple times, each time stopping it after a~while.
474 A~good choice of the stopping condition is to place a~limit on the size of the heap.
475 We start with an~arbitrary vertex, grow the tree as usually and once the heap gets too large,
476 we conserve the current tree and start with a~different vertex and an~empty heap. When this
477 process runs out of vertices, it has identified a~sub-forest of the MST, so we can
478 contract the edges of~this forest and iterate. This improves the time complexity
479 significantly:
480
481 \thm\id{itjarthm}%
482 The Iterated Jarn\'\i{}k's algorithm finds the MST of the input graph in time
483 $\O(m\timesbeta(m,n))$, where $\beta(m,n):=\min\{ i \mid \log^{(i)}n \le m/n \}$.
484
485 \cor
486 The Iterated Jarn\'\i{}k's algorithm runs in time $\O(m\log^* n)$.
487
488 \paran{Integer weights}%
489 The algorithm spends most of the time in phases which have small heaps. Once the
490 heap grows to $\Omega(\log^{(k)} n)$ for any fixed~$k$, the graph gets dense enough
491 to guarantee that at most~$k$ phases remain. This means that if we are able to
492 construct a~heap of size $\Omega(\log^{(k)} n)$ with constant time per operation,
493 we can get a~linear-time algorithm for MST. This is the case when the weights are
494 integers (we can use the Q-heap trees from Section~\ref{ramds}).
495
496 \thmn{MST for integer weights, Fredman and Willard \cite{fw:transdich}}\id{intmst}%
497 MST of a~graph with integer edge weights can be found in time $\O(m)$ on the Word-RAM.
498
499 \section{Verification of minimality}\id{verifysect}%
500
501 Now we will turn our attention to a~slightly different problem: given a~spanning
502 tree, how to verify that it is minimum? We will show that this can be achieved
503 in linear time and it will serve as a~basis for a~randomized linear-time
504 MST algorithm in the next section.
505
506 MST verification has been studied by Koml\'os \cite{komlos:verify}, who has
507 proven that $\O(m)$ edge comparisons are sufficient, but his algorithm needed
508 super-linear time to find the edges to compare. Dixon, Rauch and Tarjan \cite{dixon:verify}
509 have later shown that the overhead can be reduced
510 to linear time on the RAM using preprocessing and table lookup on small
511 subtrees. Later, King has given a~simpler algorithm in \cite{king:verifytwo}.
512
513 To verify that a~spanning tree~$T$ is minimum, it is sufficient to check that all
514 edges outside~$T$ are $T$-heavy. For each edge $uv\in E\setminus T$, we will
515 find the heaviest edge of the tree path $T[u,v]$ (we will call it the \df{peak}
516 of the path) and compare its weight to $w(uv)$. We have therefore transformed
517 the MST verification to the problem of finding peaks for a~set of \df{query
518 paths} on a~given tree. By a~sequence of further transformations, we can even
519 assume that the given tree is \df{complete branching} (all vertices are on
520 the same level and internal vertices always have outdegree~2) and that the
521 query paths join a~vertex with one of its ancestors.
522
523 Koml\'os has given a~simple algorithm that traverses the complete branching
524 tree recursively. At each moment, it maintains an~array of peaks of the restrictions
525 of the query paths to the subtree below the current vertex. If we account for the
526 comparisons performed by this algorithm carefully and express the bound in terms
527 of the size of the original problem (before all the transformations), we get:
528
529 \thmn{Verification of the MST, Koml\'os \cite{komlos:verify}}\id{verify}%
530 For every weighted graph~$G$ and its spanning tree~$T$, it is sufficient to
531 perform $\O(m)$ comparisons of edge weights to determine whether~$T$ is minimum
532 and to find all $T$-light edges in~$G$.
533
534 It remains to demonstrate that the overhead of the algorithm needed to find
535 the required comparisons and to infer the peaks from their results can be decreased,
536 so that it gets bounded by the number of comparisons and therefore also by $\O(m)$.
537 We will follow the idea of King from \cite{king:verifytwo}, but as we have the power
538 of the RAM data structures from Section~\ref{ramds} at our command, the low-level
539 details will be easier. Still, the construction is rather technical, so we omit
540 it from this abstract and state only the final theorem:
541
542 \thmn{Verification of MST on the RAM}\id{ramverify}%
543 There is a~RAM algorithm which for every weighted graph~$G$ and its spanning tree~$T$
544 determines whether~$T$ is minimum and finds all $T$-light edges in~$G$ in time $\O(m)$.
545
546 \section{A randomized algorithm}\id{randmst}%
547
548 When we analysed the Contractive Bor\o{u}vka's algorithm in Section~\ref{contalg},
549 we observed that while the number of vertices per iteration decreases exponentially,
550 the number of edges generally does not, so we spend $\Theta(m)$ time on every phase.
551 Karger, Klein and Tarjan \cite{karger:randomized} have overcome this problem by
552 combining the Bor\o{u}vka's algorithm with filtering based on random sampling.
553 This leads to a~randomized algorithm which runs in linear expected time.
554
555 The principle of the filtering is simple: Let us consider any spanning tree~$T$
556 of the input graph~$G$. Each edge of~$G$ that is $T$-heavy is the heaviest edge
557 of some cycle, so by the Red lemma it cannot participate in
558 the MST of~$G$. We can therefore discard all $T$-heavy edges and continue with
559 finding the MST on the reduced graph. Of course, not all choices of~$T$ are equally
560 good, but it will soon turn out that when we take~$T$ as the MST of a~randomly selected
561 subgraph, only a~small expected number of edges remains:
562
563 \lemman{Random sampling, Karger \cite{karger:sampling}}\id{samplemma}%
564 Let $H$~be a~subgraph of~$G$ obtained by including each edge independently
565 with probability~$p$. Let further $F$~be the minimum spanning forest of~$H$. Then the
566 expected number of $F$-nonheavy\foot{That is, $F$-light edges and also edges of~$F$ itself.}
567 edges in~$G$ is at most $n/p$.
568
569 \para
570 We will formulate the algorithm as a~doubly-recursive procedure. It alternatively
571 performs steps of the Bor\o{u}vka's algorithm and filtering based on the above lemma.
572 The first recursive call computes the MSF of the sampled subgraph, the second one
573 finds the MSF of the original graph, but without the heavy edges.
574
575 \algn{MSF by random sampling --- the KKT algorithm}\id{kkt}%
576 \algo
577 \algin A~graph $G$ with an~edge comparison oracle.
578 \:Remove isolated vertices from~$G$. If no vertices remain, stop and return an~empty forest.
579 \:Perform two Bor\o{u}vka steps (iterations of Algorithm \ref{contbor}) on~$G$ and
580   remember the set~$B$ of the edges having been contracted.
581 \:Select a~subgraph~$H\subseteq G$ by including each edge independently with
582   probability $1/2$.
583 \:$F\=\msf(H)$ calculated recursively.
584 \:Construct $G'\subseteq G$ by removing all $F$-heavy edges of~$G$.
585 \:$R\=\msf(G')$ calculated recursively.
586 \:Return $R\cup B$.
587 \algout The minimum spanning forest of~$G$.
588 \endalgo
589
590 \>A~careful analysis of this algorithm, based on properties of its recursion tree
591 and on the peak-finding algorithm of the previous section, yields the following time bounds:
592
593 \thm
594 The KKT algorithm runs in time $\O(\min(n^2,m\log n))$ in the worst case on the RAM.
595 The expected time complexity is $\O(m)$.
596
597 \chapter{Approaching Optimality}\id{optchap}%
598
599 \section{Soft heaps}\id{shsect}%
600
601 A~vast majority of MST algorithms that we have encountered so far is based on
602 the Tarjan's Blue rule (Lemma \ref{bluelemma}), the only exception being the
603 randomized KKT algorithm, which also used the Red rule (Lemma \ref{redlemma}). Recently, Chazelle
604 \cite{chazelle:ackermann} and Pettie \cite{pettie:ackermann} have presented new
605 deterministic algorithms for the MST which are also based on the combination of
606 both rules. They have reached worst-case time complexity
607 $\O(m\timesalpha(m,n))$ on the Pointer Machine. We will devote this chapter to
608 their results and especially to another algorithm by Pettie and Ramachandran
609 \cite{pettie:optimal} which is provably optimal.
610
611 At the very heart of all these algorithms lies the \df{soft heap} discovered by
612 Chazelle \cite{chazelle:softheap}. It is a~meldable priority queue, roughly
613 similar to the Vuillemin's binomial heaps \cite{vuillemin:binheap} or Fredman's
614 and Tarjan's Fibonacci heaps \cite{ft:fibonacci}. The soft heaps run faster at
615 the expense of \df{corrupting} a~fraction of the inserted elements by raising
616 their values (the values are however never lowered). This allows for
617 a~trade-off between accuracy and speed, controlled by a~parameter~$\varepsilon$.
618
619 In the thesis, we describe the exact mechanics of the soft heaps and analyse its complexity.
620 The important properties are characterized by the following theorem:
621
622 \thmn{Performance of soft heaps, Chazelle \cite{chazelle:softheap}}\id{softheap}%
623 A~soft heap with error rate~$\varepsilon$ ($0<\varepsilon\le 1/2$) processes
624 a~sequence of operations starting with an~empty heap and containing $n$~\<Insert>s
625 in time $\O(n\log(1/\varepsilon))$ on the Pointer Machine. At every moment, the
626 heap contains at most $\varepsilon n$ corrupted items.
627
628 \section{Robust contractions}
629
630 Having the soft heaps at hand, we would like to use them in a~conventional MST
631 algorithm in place of a~normal heap. We can for example try implanting the soft heap
632 in the Jarn\'i{}k's algorithm, preferably in the earlier
633 version without Fibonacci heaps as the soft heaps lack the \<Decrease> operation.
634 This brave, but somewhat simple-minded attempt is however doomed to
635 fail because of corruption of items inside the soft heap.
636 While the basic structural properties of MST's no longer hold in corrupted graphs,
637 there is a~weaker form of the Contraction lemma that takes the corrupted edges into account.
638 Before we prove this lemma, we expand our awareness of subgraphs which can be contracted.
639
640 \defn
641 A~subgraph $C\subseteq G$ is \df{contractible} iff for every pair of edges $e,f\in\delta(C)$\foot{That is,
642 of~$G$'s edges with exactly one endpoint in~$C$.} there exists a~path in~$C$ connecting the endpoints
643 of the edges $e,f$ such that all edges on this path are lighter than either $e$ or~$f$.
644
645 For example, when we stop the Jarn\'\i{}k's algorithm at some moment and
646 we take a~subgraph~$C$ induced by the constructed tree, this subgraph is contractible.
647 We can now easily reformulate the Contraction lemma (\ref{contlemma}) in the language
648 of contractible subgraphs:
649
650 \lemman{Generalized contraction}
651 When~$C\subseteq G$ is a~contractible subgraph, then $\msf(G)=\msf(C) \cup \msf(G/C)$.
652
653 Let us bring corruption back to the game and state a~``robust'' version
654 of this lemma.
655
656 \nota\id{corrnota}%
657 When~$G$ is a~weighted graph and~$R$ a~subset of its edges, we will use $G\crpt
658 R$ to denote an arbitrary graph obtained from~$G$ by increasing the weights of
659 some of the edges in~$R$.
660 Whenever~$C$ is a~subgraph of~$G$, we will use $R^C$ to refer to the edges of~$R$ with
661 exactly one endpoint in~$C$ (i.e., $R^C = R\cap \delta(C)$).
662
663 \lemman{Robust contraction, Chazelle \cite{chazelle:almostacker}}\id{robcont}%
664 Let $G$ be a~weighted graph and $C$~its subgraph contractible in~$G\crpt R$
665 for some set~$R$ of edges. Then $\msf(G) \subseteq \msf(C) \cup \msf((G/C) \setminus R^C) \cup R^C$.
666
667 \para
668 We will now mimic the Iterated Jarn\'\i{}k's algorithm. We will partition the given graph to a~collection~$\C$
669 of non-overlapping contractible subgraphs called \df{clusters} and we put aside all edges that got corrupted in the process.
670 We recursively compute the MSF of those subgraphs and of the contracted graph. Then we take the
671 union of these MSF's and add the corrupted edges. According to the previous lemma, this does not produce
672 the MSF of~$G$, but a~sparser graph containing it, on which we can continue.
673 %%The following theorem describes the properties of this partition:
674
675 \thmn{Partitioning to contractible clusters, Chazelle \cite{chazelle:almostacker}}\id{partthm}%
676 Given a~weighted graph~$G$ and parameters $\varepsilon$ ($0<\varepsilon\le 1/2$)
677 and~$t$, we can construct a~collection $\C=\{C_1,\ldots,C_k\}$ of clusters and a~set~$R^\C$ of edges such that:
678
679 \numlist\ndotted
680 \:All the clusters and the set~$R^\C$ are mutually edge-disjoint.
681 \:Each cluster contains at most~$t$ vertices.
682 \:Each vertex of~$G$ is contained in at least one cluster.
683 \:The connected components of the union of all clusters have at least~$t$ vertices each,
684   except perhaps for those which are equal to a~connected component of $G\setminus R^\C$.
685 \:$\vert R^\C\vert \le 2\varepsilon m$.
686 \:$\msf(G) \subseteq \bigcup_i \msf(C_i) \cup \msf\bigl((G / \bigcup_i C_i) \setminus R^\C\bigr) \cup R^\C$.
687 \:The construction takes $\O(n+m\log (1/\varepsilon))$ time.
688 \endlist
689
690 \section{Decision trees}\id{dtsect}%
691
692 The Pettie's and Ramachandran's algorithm combines the idea of robust partitioning with optimal decision
693 trees constructed by brute force for very small subgraphs.
694 %%Formally, the decision trees are defined as follows:
695 Let us define them first:
696
697 \defnn{Decision trees and their complexity}\id{decdef}%
698 A~\df{MSF decision tree} for a~graph~$G$ is a~binary tree. Its internal vertices
699 are labeled with pairs of $G$'s edges to be compared, each of the two outgoing tree edges
700 corresponds to one possible result of the comparison.
701 Leaves of the tree are labeled with spanning trees of the graph~$G$.
702
703 A~\df{computation} of the decision tree on a~specific permutation of edge weights
704 in~$G$ is the path from the root to a~leaf such that the outcome of every comparison
705 agrees with the edge weights. The result of the computation is the spanning tree
706 assigned to its final leaf.
707 A~decision tree is \df{correct} iff for every permutation the corresponding
708 computation results in the real MSF of~$G$ with the particular weights.
709
710 The \df{time complexity} of a~decision tree is defined as its depth. It therefore
711 bounds the number of comparisons spent on every path. (It need not be equal since
712 some paths need not correspond to an~actual computation --- the sequence of outcomes
713 on the path could be unsatisfiable.)
714
715 A~decision tree is called \df{optimal} if it is correct and its depth is minimum possible
716 among the correct decision trees for the given graph.
717 We will denote an~arbitrary optimal decision tree for~$G$ by~${\cal D}(G)$ and its
718 complexity by~$D(G)$.
719
720 The \df{decision tree complexity} $D(m,n)$ of the MSF problem is the maximum of~$D(G)$
721 over all graphs~$G$ with $n$~vertices and~$m$ edges.
722
723 \obs
724 Decision trees are the most general deterministic comparison-based computation model possible.
725 The only operations that count in its time complexity are comparisons. All
726 other computation is free, including solving NP-complete problems or having
727 access to an~unlimited source of non-uniform constants. The decision tree
728 complexity is therefore an~obvious lower bound on the time complexity of the
729 problem in all other comparison-based models.
730
731 The downside is that we do not know any explicit construction of the optimal
732 decision trees, nor even a~non-constructive proof of their complexity.
733 On the other hand, the complexity of any existing comparison-based algorithm
734 can be used as an~upper bound on the decision tree complexity. Also, we can
735 construct an~optimal decision tree using brute force:
736
737 \lemma
738 An~optimal MST decision tree for a~graph~$G$ on~$n$ vertices can be constructed on
739 the Pointer Machine in time $\O(2^{2^{4n^2}})$.
740
741 \section{An optimal algorithm}\id{optalgsect}%
742
743 Once we have developed the soft heaps, partitioning and MST decision trees,
744 it is now simple to state the Pettie's and Ramachandran's MST algorithm
745 and prove that it is asymptotically optimal among all MST algorithms in
746 comparison-based models. Several standard MST algorithms from the previous
747 chapters will also play their roles.
748 We will describe the algorithm as a~recursive procedure:
749
750 \algn{Optimal MST algorithm, Pettie and Ramachandran \cite{pettie:optimal}}\id{optimal}%
751 \algo
752 \algin A~connected graph~$G$ with an~edge comparison oracle.
753 \:If $G$ has no edges, return an~empty tree.
754 \:$t\=\lfloor\log^{(3)} n\rfloor$. \cmt{the size of clusters}
755 \:Call the partitioning procedure (\ref{partthm}) on $G$ and $t$ with $\varepsilon=1/8$. It returns
756   a~collection~$\C=\{C_1,\ldots,C_k\}$ of clusters and a~set~$R^\C$ of corrupted edges.
757 \:$F_i \= \mst(C_i)$ for all~$i$, obtained using optimal decision trees.
758 \:$G_A \= (G / \bigcup_i C_i) \setminus R^\C$. \cmt{the contracted graph}
759 \:$F_A \= \msf(G_A)$ calculated by the Iterated Jarn\'\i{}k's algorithm (see Section \ref{iteralg}).
760 \:$G_B \= \bigcup_i F_i \cup F_A \cup R^\C$. \cmt{combine subtrees with corrupted edges}
761 \:Run two Bor\o{u}vka steps (iterations of the Contractive Bor\o{u}vka's algorithm, \ref{contbor}) on~$G_B$,
762   getting a~contracted graph~$G_C$ and a~set~$F_B$ of MST edges.
763 \:$F_C \= \mst(G_C)$ obtained by a~recursive call to this algorithm.
764 \:Return $F_B \cup F_C$.
765 \algout The minimum spanning tree of~$G$.
766 \endalgo
767
768 \>Correctness of this algorithm immediately follows from the Partitioning theorem (\ref{partthm})
769 and from the proofs of the respective algorithms used as subroutines. As for time complexity, we prove:
770
771 \thm
772 The time complexity of the Optimal algorithm is $\Theta(D(m,n))$.
773
774 \paran{Complexity of MST}%
775 As we have already noted, the exact decision tree complexity $D(m,n)$ of the MST problem
776 is still open and so therefore is the time complexity of the optimal algorithm. However,
777 every time we come up with another comparison-based algorithm, we can use its complexity
778 (or more specifically the number of comparisons it performs, which can be even lower)
779 as an~upper bound on the optimal algorithm.
780 The best explicit comparison-based algorithm known to date has been discovered by Chazelle
781 \cite{chazelle:ackermann} and independently by Pettie \cite{pettie:ackermann}. It achieves complexity $\O(m\timesalpha(m,n))$.
782 Using any of these results, we can prove an~Ackermannian upper bound on the
783 optimal algorithm:
784
785 \thm
786 The time complexity of the Optimal algorithm is $\O(m\timesalpha(m,n))$.
787
788 \chapter{Dynamic Spanning Trees}\id{dynchap}%
789
790 \section{Dynamic graph algorithms}
791
792 In many applications, we often need to solve a~certain graph problem for a~sequence of graphs that
793 differ only a~little, so recomputing the solution for every graph from scratch would be a~waste of
794 time. In such cases, we usually turn our attention to \df{dynamic graph algorithms.} A~dynamic
795 algorithm is in fact a~data structure that remembers a~graph. It offers operations that modify the
796 structure of the graph and also operations that query the result of the problem for the current
797 state of the graph. A~typical example of a~problem of this kind is dynamic maintenance of connected
798 components:
799
800 \problemn{Dynamic connectivity}
801 Maintain an~undirected graph under a~sequence of the following operations:
802 \itemize\ibull
803 \:$\<Init>(n)$ --- Create a~graph with $n$~isolated vertices $\{1,\ldots,n\}$.
804 (It is possible to modify the structure to support dynamic addition and removal of vertices, too.)
805 \:$\<Insert>(G,u,v)$ --- Insert an~edge $uv$ to~$G$ and return its unique
806 identifier. This assumes that the edge did not exist yet.
807 \:$\<Delete>(G,e)$ --- Delete an~edge specified by its identifier from~$G$.
808 \:$\<Connected>(G,u,v)$ --- Test if vertices $u$ and~$v$ are in the same connected component of~$G$.
809 \endlist
810
811 \>In this chapter, we will focus on the dynamic version of the minimum spanning forest.
812 This problem seems to be intimately related to the dynamic connectivity. Indeed, all known
813 algorithms for dynamic connectivity maintain some sort of a~spanning forest.
814 This suggests that a~dynamic MSF algorithm could be obtained by modifying the
815 mechanics of the data structure to keep the forest minimum.
816 We however have to answer one important question first: What should be the output of
817 our MSF data structure? Adding an~operation that returns the MSF of the current
818 graph would be of course possible, but somewhat impractical as this operation would have to
819 spend $\Omega(n)$ time on the mere writing of its output. A~better way seems to
820 be making the \<Insert> and \<Delete> operations report the list of modifications
821 of the MSF implied by the change in the graph. It is easy to prove that $\O(1)$
822 modifications always suffice, so we can formulate our problem as follows:
823
824 \problemn{Dynamic minimum spanning forest}
825 Maintain an~undirected graph with distinct weights on edges (drawn from a~totally ordered set)
826 and its minimum spanning forest under a~sequence of the following operations:
827 \itemize\ibull
828 \:$\<Init>(n)$ --- Create a~graph with $n$~isolated vertices $\{1,\ldots,n\}$.
829 \:$\<Insert>(G,u,v,w)$ --- Insert an~edge $uv$ of weight~$w$ to~$G$. Return its unique
830         identifier and the list of additions and deletions of edges in $\msf(G)$.
831 \:$\<Delete>(G,e)$ --- Delete an~edge specified by its identifier from~$G$.
832         Return the list of additions and deletions of edges in $\msf(G)$.
833 \endlist
834
835 \paran{Incremental MSF}%
836 In case only edge insertions are allowed, the problem reduces to finding the heaviest
837 edge (peak) on the tree path covered by the newly inserted edge and replacing the peak
838 if needed. This can be handled quite efficiently by using the Link-Cut trees of Sleator
839 and Tarjan \cite{sleator:trees}. We obtain logarithmic time bound:
840
841 \thmn{Incremental MSF}
842 When only edge insertions are allowed, the dynamic MSF can be maintained in time $\O(\log n)$
843 amortized per operation.
844
845 \section{Dynamic connectivity}
846
847 The fully dynamic connectivity problem has a~long and rich history. In the 1980's, Frederickson \cite{frederickson:dynamic}
848 has used his topological trees to construct a~dynamic connectivity algorithm of complexity $\O(\sqrt m)$ per update and
849 $\O(1)$ per query. Eppstein et al.~\cite{eppstein:sparsify} have introduced a~sparsification technique which can bring the
850 updates down to $\O(\sqrt n)$. Later, several different algorithms with complexity on the order of $n^\varepsilon$
851 were presented by Henzinger and King \cite{henzinger:mst} and also by Mare\v{s} \cite{mares:dga}.
852 A~polylogarithmic time bound was first reached by the randomized algorithm of Henzinger and King \cite{henzinger:randdyn}.
853 The best result known as of now is the $\O(\log^2 n)$ time deterministic algorithm by Holm,
854 de~Lichtenberg and Thorup \cite{holm:polylog}, which will we describe in this section.
855
856 The algorithm will maintain a~spanning forest~$F$ of the current graph~$G$, represented by an~ET-tree
857 which will be used to answer connectivity queries. The edges of~$G\setminus F$ will be stored as~non-tree
858 edges in the ET-tree. Hence, an~insertion of an~edge to~$G$ either adds it to~$F$ or inserts it as non-tree.
859 Deletions of non-tree edges are also easy, but when a~tree edge is deleted, we have to search for its
860 replacement among the non-tree edges.
861
862 To govern the search in an~efficient way, we will associate each edge~$e$ with a~level $\ell(e) \le
863 L = \lfloor\log_2 n\rfloor$. For each level~$i$, we will use~$F_i$ to denote the subforest
864 of~$F$ containing edges of level at least~$i$. Therefore $F=F_0 \supseteq F_1 \supseteq \ldots \supseteq F_L$.
865 We will maintain the following \em{invariants:}
866
867 {\narrower
868 \def\iinv{{\bo I\the\itemcount~}}
869 \numlist\iinv
870 \:$F$~is the maximum spanning forest of~$G$ with respect to the levels. (In other words,
871 if $uv$ is a~non-tree edge, then $u$ and~$v$ are connected in~$F_{\ell(uv)}$.)
872 \:For each~$i$, the components of~$F_i$ have at most $\lfloor n/2^i \rfloor$ vertices each.
873 (This implies that it does not make sense to define~$F_i$ for $i>L$, because it would be empty
874 anyway.)
875 \endlist
876 }
877
878 At the beginning, the graph contains no edges, so both invariants are trivially
879 satisfied. Newly inserted edges enter level~0, which cannot break I1 nor~I2.
880
881 When we delete a~tree edge at level~$\ell$, we split a~tree~$T$ of~$F_\ell$ to two
882 trees $T_1$ and~$T_2$. Without loss of generality, let us assume that $T_1$ is the
883 smaller one. We will try to find the replacement edge of the highest possible
884 level that connects the spanning tree back. From I1, we know that such an~edge cannot belong to
885 a~level greater than~$\ell$, so we start looking for it at level~$\ell$. According
886 to~I2, the tree~$T$ had at most $\lfloor n/2^\ell\rfloor$ vertices, so $T_1$ has
887 at most $\lfloor n/2^{\ell+1} \rfloor$ of them. Thus we can move all level~$\ell$
888 edges of~$T_1$ to level~$\ell+1$ without violating either invariant.
889
890 We now start enumerating the non-tree edges incident with~$T_1$. Each such edge
891 is either local to~$T_1$ or it joins $T_1$ with~$T_2$. We will therefore check each edge
892 whether its other endpoint lies in~$T_2$ and if it does, we have found the replacement
893 edge, so we insert it to~$F_\ell$ and stop. Otherwise we move the edge one level up. (This
894 will be the grist for the mill of our amortization argument: We can charge most of the work on level
895 increases and we know that the level of each edge can reach at most~$L$.)
896
897 If the non-tree edges at level~$\ell$ are exhausted, we try the same in the next
898 lower level and so on. If there is no replacement edge at level~0, the tree~$T$
899 remains disconnected.
900
901 The implementation uses the Eulerian Tour trees of Henzinger and King \cite{henzinger:randdyn}
902 to represent the forests~$F_\ell$ together with the non-tree edges at each particular level.
903 A~simple amortized analysis using the levels yields the following result:
904
905 \thmn{Fully dynamic connectivity, Holm et al.~\cite{holm:polylog}}\id{dyncon}%
906 Dynamic connectivity can be maintained in time $\O(\log^2 n)$ amortized per
907 \<Insert> and \<Delete> and in time $\O(\log n/\log\log n)$ per \<Connected>
908 in the worst case.
909
910 \rem\id{dclower}%
911 An~$\Omega(\log n/\log\log n)$ lower bound for the amortized complexity of the dynamic connectivity
912 problem has been proven by Henzinger and Fredman \cite{henzinger:lowerbounds} in the cell
913 probe model with $\O(\log n)$-bit words. Thorup has answered by a~faster algorithm
914 \cite{thorup:nearopt} that achieves $\O(\log n\log^3\log n)$ time per update and
915 $\O(\log n/\log^{(3)} n)$ per query on a~RAM with $\O(\log n)$-bit words. (He claims
916 that the algorithm runs on a~Pointer Machine, but it uses arithmetic operations,
917 so it does not fit the definition of the PM we use. The algorithm only does not
918 need direct indexing of arrays.) So far, it is not known how to extend this algorithm
919 to fit our needs, so we omit the details.
920
921 \section{Dynamic spanning forests}\id{dynmstsect}%
922
923 Let us turn our attention back to the dynamic MSF.
924 Most of the early algorithms for dynamic connectivity also imply $\O(n^\varepsilon)$
925 algorithms for dynamic maintenance of the MSF. Henzinger and King \cite{henzinger:twoec,henzinger:randdyn}
926 have generalized their randomized connectivity algorithm to maintain the MSF in $\O(\log^5 n)$ time per
927 operation, or $\O(k\log^3 n)$ if only $k$ different values of edge weights are allowed. They have solved
928 the decremental version of the problem first (which starts with a~given graph and only edge deletions
929 are allowed) and then presented a~general reduction from the fully dynamic MSF to its decremental version.
930 We will describe the algorithm of Holm, de Lichtenberg and Thorup \cite{holm:polylog}, who have followed
931 the same path. They have modified their dynamic connectivity algorithm to solve the decremental MSF
932 in $\O(\log^2 n)$ and obtained the fully dynamic MSF working in $\O(\log^4 n)$ per operation.
933
934 \paran{Decremental MSF}%
935 Turning the algorithm from the previous section to the decremental MSF requires only two
936 changes: First, we have to start with the forest~$F$ equal to the MSF of the initial
937 graph. As we require to pay $\O(\log^2 n)$ for every insertion, we can use almost arbitrary
938 MSF algorithm to find~$F$. Second, when we search for an~replacement edge, we need to pick
939 the lightest possible choice. We will therefore use a~weighted version of the ET-trees.
940 We must ensure that the lower levels cannot contain a~lighter replacement edge,
941 but fortunately the light edges tend to ``bubble up'' in the hierarchy of
942 levels. This can be formalized in form of the following invariant:
943
944 {\narrower
945 \def\iinv{{\bo I\the\itemcount~}}
946 \numlist\iinv
947 \itemcount=2
948 \:On every cycle, the heaviest edge has the smallest level.
949 \endlist
950 }
951
952 \>This immediately implies that we always select the right replacement edge:
953
954 \lemma\id{msfrepl}%
955 Let $F$~be the minimum spanning forest and $e$ any its edge. Then among all replacement
956 edges for~$e$, the lightest one is at the maximum level.
957
958 A~brief analysis also shows that the invariant I3 is observed by all operations
959 on the structure. We can conclude:
960
961 \thmn{Decremental MSF, Holm et al.~\cite{holm:polylog}}
962 When we start with a~graph on $n$~vertices with~$m$ edges and we perform a~sequence of
963 edge deletions, the MSF can be initialized in time $\O((m+n)\cdot\log^2 n)$ and then
964 updated in time $\O(\log^2 n)$ amortized per operation.
965
966 \paran{Fully dynamic MSF}%
967 The decremental MSF algorithm can be turned to a~fully dynamic one by a~blackbox
968 reduction of Holm et al.:
969
970 \thmn{MSF dynamization, Holm et al.~\cite{holm:polylog}}
971 Suppose that we have a~decremental MSF algorithm with the following properties:
972 \numlist\ndotted
973 \:For any $a$,~$b$, it can be initialized on a~graph with~$a$ vertices and~$b$ edges.
974 \:Then it executes an~arbitrary sequence of deletions in time $\O(b\cdot t(a,b))$, where~$t$ is a~non-decreasing function.
975 \endlist
976 \>Then there exists a~fully dynamic MSF algorithm for a~graph on $n$~vertices, starting
977 with no edges, that performs $m$~insertions and deletions in amortized time:
978 $$
979 \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.}
980 $$
981
982 \corn{Fully dynamic MSF}\id{dynmsfcorr}%
983 There is a~fully dynamic MSF algorithm that works in time $\O(\log^4 n)$ amortized
984 per operation for graphs on $n$~vertices.
985
986 \paran{Dynamic MSF with limited edge weights}%
987 If the set from which the edge weights are drawn is small, we can take a~different
988 approach. If only two values are allowed, we split the graph to subgraphs $G_1$ and~$G_2$
989 induced by the edges of the respective weights and we maintain separate connectivity
990 structures (together with a~spanning tree) for $G_1$ and $G_2 \cup T_1$ (where $T_1$
991 is a~spanning tree of~$G_1$). We can easily modify the structure for $G_2\cup
992 T_1$ to prefer the edges of~$T_1$. This ensures that the spanning tree of $G_2\cup T_1$
993 will be the MST of the whole~$G$.
994
995 If there are more possible values, we simply iterate this construction: the $i$-th
996 structure contains edges of weight~$i$ and the edges of the spanning tree from the
997 $(i-1)$-th structure. We get:
998
999 \thmn{MSF with limited edge weights}
1000 There is a~fully dynamic MSF algorithm that works in time $\O(k\cdot\log^2 n)$ amortized
1001 per operation for graphs on $n$~vertices with only $k$~distinct edge weights allowed.
1002
1003 \section{Almost minimum trees}\id{kbestsect}%
1004
1005 In some situations, finding the single minimum spanning tree is not enough and we are interested
1006 in the $K$~lightest spanning trees, usually for some small value of~$K$. Katoh, Ibaraki
1007 and Mine \cite{katoh:kmin} have given an~algorithm of time complexity $\O(m\log\beta(m,n) + Km)$,
1008 building on the MST algorithm of Gabow et al.~\cite{gabow:mst}.
1009 Subsequently, Eppstein \cite{eppstein:ksmallest} has discovered an~elegant preprocessing step which allows to reduce
1010 the running time to $\O(m\log\beta(m,n) + \min(K^2,Km))$ by eliminating edges
1011 which are either present in all $K$ trees or in none of them.
1012 We will show a~variant of their algorithm based on the MST verification
1013 procedure of Section~\ref{verifysect}.
1014
1015 In this section, we will require the edge weights to be numeric, because
1016 comparisons are certainly not sufficient to determine the second best spanning tree. We will
1017 assume that our computation model is able to add, subtract and compare the edge weights
1018 in constant time.
1019 Let us focus on finding the second lightest spanning tree first.
1020
1021 \paran{Second lightest spanning tree}%
1022 Suppose that we have a~weighted graph~$G$ and a~sequence $T_1,\ldots,T_z$ of all its spanning
1023 trees. Also suppose that the weights of these spanning trees are distinct and that the sequence
1024 is ordered by weight, i.e., $w(T_1) < \ldots < w(T_z)$ and $T_1 = \mst(G)$. Let us observe
1025 that each tree is similar to at least one of its predecessors:
1026
1027 \lemman{Difference lemma}\id{kbl}%
1028 For each $i>1$ there exists $j<i$ such that $T_i$ and~$T_j$ differ by a~single edge exchange.
1029
1030 \para
1031 This lemma implies that the second best spanning tree~$T_2$ differs from~$T_1$ by a~single
1032 edge exchange. It remains to find which exchange it is, but this can be reduced to finding
1033 peaks of the paths covered by the edges outside~$T_1$, which we already are able to solve
1034 efficiently by the methods of Section~\ref{verifysect}. Therefore:
1035
1036 \lemma
1037 For every graph~$H$ and a~MST $T$ of~$H$, linear time is sufficient to find
1038 edges $e\in T$ and $f\in H\setminus T$ such that $w(f)-w(e)$ is minimum.
1039 (We will call this procedure \df{finding the best exchange in $(H,T)$.})
1040
1041 \cor
1042 Given~$G$ and~$T_1$, we can find~$T_2$ in time $\O(m)$.
1043
1044 \paran{Third lightest spanning tree}%
1045 Once we know~$T_1$ and~$T_2$, how to get~$T_3$? According to the Difference lemma, $T_3$~can be
1046 obtained by a~single exchange from either~$T_1$ or~$T_2$. Therefore we need to find the
1047 best exchange for~$T_2$ and the second best exchange for~$T_1$ and use the better of them.
1048 The latter is not easy to find directly, so we observe:
1049
1050 \obs\id{tbobs}%
1051 The tree $T_3$~can be obtained by a~single edge exchange in either $(G_1,T_1/e)$ or $(G_2,T_2)$:
1052
1053 \itemize\ibull
1054 \:If $T_3 = T_1-e'+f'$ for $e'\ne e$, then $T_3/e = (T_1/e)-e'+f'$ in~$G_1$.
1055 \:If $T_3 = T_1-e+f'$, then $T_3 = T_2 - f + f'$ in~$G_2$.
1056 \:If $T_3 = T_2-e'+f'$, then this exchange is found in~$G_2$.
1057 \endlist
1058
1059 \>Thus we can run the previous algorithm for finding the best edge exchange
1060 on both~$G_1$ and~$G_2$ and find~$T_3$ again in time $\O(m)$.
1061
1062 \paran{Further spanning trees}%
1063 The construction of auxiliary graphs can be iterated to obtain $T_1,\ldots,T_K$
1064 for an~arbitrary~$K$. We will build a~\df{meta-tree} of auxiliary graphs. Each node of this meta-tree
1065 carries a~graph and its minimum spanning tree. The root node contains~$(G,T_1)$,
1066 its sons have $(G_1,T_1/e)$ and $(G_2,T_2)$. When $T_3$ is obtained by an~exchange
1067 in one of these sons, we attach two new leaves to that son and we let them carry the two auxiliary
1068 graphs derived by contracting or deleting the exchanged edge. Then we find the best
1069 edge exchanges among all leaves of the new meta-tree and repeat the process. By Observation \ref{tbobs},
1070 each spanning tree of~$G$ is generated exactly once. The Difference lemma guarantees that
1071 the trees are enumerated in the increasing order. So we get:
1072
1073 \lemma\id{kbestl}%
1074 Given~$G$ and~$T_1$, we can find $T_2,\ldots,T_K$ in time $\O(Km + K\log K)$.
1075
1076 \paran{Invariant edges}%
1077 Our algorithm can be further improved for small values of~$K$ (which seems to be the common
1078 case in most applications) by the reduction of Eppstein \cite{eppstein:ksmallest}.
1079 He has proven that there are many edges of~$T_1$
1080 which are guaranteed to be contained in $T_2,\ldots,T_K$ as well, and likewise there are
1081 many edges of $G\setminus T_1$ which are excluded from all those spanning trees.
1082 When we combine this with the previous construction, we get the following theorem:
1083
1084 \thmn{Finding $K$ lightest spanning trees}\id{kbestthm}%
1085 For a~given graph~$G$ with real edge weights and a~positive integer~$K$, the $K$~best spanning trees can be found
1086 in time $\O(m\timesalpha(m,n) + \min(K^2,Km + K\log K))$.
1087
1088 \chapter{Ranking Combinatorial Structures}\id{rankchap}%
1089
1090 \section{Ranking and unranking}\id{ranksect}%
1091
1092 The techniques for building efficient data structures on the RAM, which we have described
1093 in Section~\ref{ramds}, can be also used for a~variety of problems related
1094 to ranking of combinatorial structures. Generally, the problems are stated
1095 in the following way:
1096
1097 \defn\id{rankdef}%
1098 Let~$C$ be a~set of objects and~$\prec$ a~linear order on~$C$. The \df{rank}
1099 $R_{C,\prec}(x)$ of an~element $x\in C$ is the number of elements $y\in C$ such that $y\prec x$.
1100 We will call the function $R_{C,\prec}$ the \df{ranking function} for $C$ ordered by~$\prec$
1101 and its inverse $R^{-1}_{C,\prec}$ the \df{unranking function} for $C$ and~$\prec$. When the set
1102 and the order are clear from the context, we will use plain~$R(x)$ and $R^{-1}(x)$.
1103 Also, when $\prec$ is defined on a~superset~$C'$ of~$C$, we naturally extend $R_C(x)$
1104 to elements $x\in C'\setminus C$.
1105
1106 \example
1107 Let us consider the set $C_k=\{\0,\1\}^k$ of all binary strings of length~$k$ ordered
1108 lexicographically. Then $R^{-1}(i)$ is the $i$-th smallest element of this set, that
1109 is the number~$i$ written in binary and padded to~$k$ digits (i.e., $\(i)_k$ in the
1110 notation of Section~\ref{ramds}). Obviously, $R(x)$ is the integer whose binary
1111 representation is the string~$x$.
1112
1113 %--------------------------------------------------------------------------------
1114
1115 \section{Ranking of permutations}
1116 \id{pranksect}
1117
1118 One of the most common ranking problems is ranking of permutations on the set~$[n]=\{1,2,\ldots,n\}$.
1119 This is frequently used to create arrays indexed by permutations: for example in Ruskey's algorithm
1120 for finding Hamilton cycles in Cayley graphs (see~\cite{ruskey:ham} and \cite{ruskey:hce})
1121 or when exploring state spaces of combinatorial puzzles like the Loyd's Fifteen \cite{ss:fifteen}.
1122 Many other applications are surveyed by Critani et al.~\cite{critani:rau} and in
1123 most cases, the time complexity of the whole algorithm is limited by the efficiency
1124 of the (un)ranking functions.
1125
1126 The permutations are usually ranked according to their lexicographic order.
1127 In fact, an~arbitrary order is often sufficient if the ranks are used solely
1128 for indexing of arrays. The lexicographic order however has an~additional advantage
1129 of a~nice structure, which allows various operations on permutations to be
1130 performed directly on their ranks.
1131
1132 Na\"\i{}ve algorithms for lexicographic ranking require time $\Theta(n^2)$ in the
1133 worst case \cite{reingold:catp} and even on average~\cite{liehe:raulow}.
1134 This can be easily improved to $O(n\log n)$ by using either a binary search
1135 tree to calculate inversions, or by a divide-and-conquer technique, or by clever
1136 use of modular arithmetic (all three algorithms are described in Knuth
1137 \cite{knuth:sas}). Myrvold and Ruskey \cite{myrvold:rank} mention further
1138 improvements to $O(n\log n/\log \log n)$ by using the RAM data structures of Dietz
1139 \cite{dietz:oal}.
1140
1141 Linear time complexity was reached by Myrvold and Ruskey \cite{myrvold:rank}
1142 for a~non-lexicographic order, which is defined locally by the history of the
1143 data structure.
1144 However, they leave the problem of lexicographic ranking open.
1145 We will describe a~general procedure which, when combined with suitable
1146 RAM data structures, yields a~linear-time algorithm for lexicographic
1147 (un)ranking.
1148
1149 \nota\id{brackets}%
1150 We will view permutations on a~finite set $A\subseteq {\bb N}$ as ordered $\vert A\vert$-tuples
1151 (in other words, arrays) containing every element of~$A$ exactly once. We will
1152 use square brackets to index these tuples: $\pi=(\pi[1],\ldots,\pi[\vert A\vert])$,
1153 and sub-tuples: $\pi[i\ldots j] = (\pi[i],\pi[i+1],\ldots,\pi[j])$.
1154 The lexicographic ranking and unranking functions for the permutations on~$A$
1155 will be denoted by~$L(\pi,A)$ and $L^{-1}(i,A)$ respectively.
1156
1157 \obs\id{permrec}%
1158 Let us first observe that permutations have a simple recursive structure.
1159 If we fix the first element $\pi[1]$ of a~permutation~$\pi$ on the set~$[n]$, the
1160 elements $\pi[2], \ldots, \pi[n]$ form a~permutation on $[n]-\{\pi[1]\} = \{1,\ldots,\pi[1]-1,\pi[1]+1,\ldots,n\}$.
1161 The lexicographic order of two permutations $\pi$ and~$\pi'$ on the original set is then determined
1162 by $\pi[1]$ and $\pi'[1]$ and only if these elements are equal, it is decided
1163 by the lexicographic comparison of permutations $\pi[2\ldots n]$ and $\pi'[2\ldots n]$.
1164 Moreover, when we fix $\pi[1]$, all permutations on the smaller set occur exactly
1165 once, so the rank of $\pi$ is $(\pi[1]-1)\cdot (n-1)!$ plus the rank of
1166 $\pi[2\ldots n]$.
1167
1168 This gives us a~reduction from (un)ranking of permutations on $[n]$ to (un)rank\-ing
1169 of permutations on a $(n-1)$-element set, which suggests a straightforward
1170 algorithm, but unfortunately this set is different from $[n-1]$ and it even
1171 depends on the value of~$\pi[1]$. We could renumber the elements to get $[n-1]$,
1172 but it would require linear time per iteration. To avoid this, we generalize the
1173 problem to permutations on subsets of $[n]$. For a permutation $\pi$ on a~set
1174 $A\subseteq [n]$ of size~$m$, similar reasoning gives a~simple formula:
1175 $$
1176 L((\pi[1],\ldots,\pi[m]),A) = R_A(\pi[1]) \cdot (m-1)! +
1177 L((\pi[2],\ldots,\pi[m]), A\setminus\{\pi[1]\}),
1178 $$
1179 which uses the ranking function~$R_A$ for~$A$. This recursive formula immediately
1180 translates to the following recursive algorithms for both ranking and unranking
1181 (described for example in \cite{knuth:sas}):
1182
1183 \alg $\<Rank>(\pi,i,n,A)$: Compute the rank of a~permutation $\pi[i\ldots n]$ on~$A$.
1184 \id{rankalg}
1185 \algo
1186 \:If $i\ge n$, return~0.
1187 \:$a\=R_A(\pi[i])$.
1188 \:$b\=\<Rank>(\pi,i+1,n,A \setminus \{\pi[i]\})$.
1189 \:Return $a\cdot(n-i)! + b$.
1190 \endalgo
1191
1192 \>We can call $\<Rank>(\pi,1,n,[n])$ for ranking on~$[n]$, i.e., to calculate
1193 $L(\pi,[n])$.
1194
1195 \alg $\<Unrank>(j,i,n,A)$: Return an~array~$\pi$ such that $\pi[i\ldots n]$ is the $j$-th permutation on~$A$.
1196 \id{unrankalg}
1197 \algo
1198 \:If $i>n$, return $(0,\ldots,0)$.
1199 \:$x\=R^{-1}_A(\lfloor j/(n-i)! \rfloor)$.
1200 \:$\pi\=\<Unrank>(j\bmod (n-i)!,i+1,n,A\setminus \{x\})$.
1201 \:$\pi[i]\=x$.
1202 \:Return~$\pi$.
1203 \endalgo
1204
1205 \>We can call $\<Unrank>(j,1,n,[n])$ to calculate $L^{-1}(j,[n])$.
1206
1207 \paran{Representation of sets}%
1208 The most time-consuming parts of the above algorithms are of course operations
1209 on the set~$A$. If we store~$A$ in a~data structure of a~known time complexity, the complexity
1210 of the whole algorithm is easy to calculate:
1211
1212 \lemma\id{ranklemma}%
1213 Suppose that there is a~data structure maintaining a~subset of~$[n]$ under a~sequence
1214 of deletions, which supports ranking and unranking of elements, and that
1215 the time complexity of a~single operation is at most~$t(n)$.
1216 Then lexicographic ranking and unranking of permutations can be performed in time $\O(n\cdot t(n))$.
1217
1218 If we store~$A$ in an~ordinary array, we have insertion and deletion in constant time,
1219 but ranking and unranking in~$\O(n)$, so $t(n)=\O(n)$ and the algorithm is quadratic.
1220 Binary search trees give $t(n)=\O(\log n)$. The data structure of Dietz \cite{dietz:oal}
1221 improves it to $t(n)=O(\log n/\log \log n)$. In fact, all these variants are equivalent
1222 to the classical algorithms based on inversion vectors, because at the time of processing~$\pi[i]$,
1223 the value of $R_A(\pi[i])$ is exactly the number of elements forming inversions with~$\pi[i]$.
1224
1225 To obtain linear time complexity, we will make use of the representation of
1226 vectors by integers on the RAM as developed in Section~\ref{ramds}. We observe
1227 that since the words of the RAM need to be able to hold integers as large as~$n!$,
1228 the word size must be at least $\log n! = \Theta(n\log n)$. Therefore the whole
1229 set~$A$ fits in~$\O(1)$ words and we get:
1230
1231 \thmn{Lexicographic ranking of permutations}
1232 When we order the permutations on the set~$[n]$ lexicographically, both ranking
1233 and unranking can be performed on the RAM in time~$\O(n)$.
1234
1235 \paran{The case of $k$-permutations}%
1236 Our algorithm can be also generalized to lexicographic ranking of
1237 \df{$k$-permutations,} that is of ordered $k$-tuples of distinct elements drawn from the set~$[n]$.
1238 There are $n^{\underline k} = n\cdot(n-1)\cdot\ldots\cdot(n-k+1)$
1239 such $k$-permutations and they have a~recursive structure similar to the one of
1240 the permutations.
1241 Unfortunately, the ranks of $k$-permutations can be much smaller, so we can no
1242 longer rely on the same data structure fitting in a constant number of word-sized integers.
1243 For example, if $k=1$, the ranks are $\O(\log n)$-bit numbers, but the data
1244 structure still requires $\Theta(n\log n)$ bits.
1245
1246 We do a minor side step by remembering the complement of~$A$ instead, that is
1247 the set of the at most~$k$ elements we have already seen. We will call this set~$H$
1248 (because it describes the ``holes'' in~$A$). Since $\Omega(k\log n)$ bits are needed
1249 to represent the rank, the vector representation of~$H$ certainly fits in a~constant
1250 number of words. When we translate the operations on~$A$ to operations on~$H$,
1251 again stored as a~vector, we get:
1252
1253 \thmn{Lexicographic ranking of $k$-permutations}
1254 When we order the $k$-per\-mu\-ta\-tions on the set~$[n]$ lexicographically, both
1255 ranking and unranking can be performed on the RAM in time~$\O(k)$.
1256
1257 \section{Restricted permutations}
1258
1259 Another interesting class of combinatorial objects that can be counted and
1260 ranked are restricted permutations. An~archetypal member of this class are
1261 permutations without a~fixed point, i.e., permutations~$\pi$ such that $\pi(i)\ne i$
1262 for all~$i$. These are also called \df{derangements} or \df{hatcheck permutations.}
1263 We will present a~general (un)ranking method for any class of restricted
1264 permutations and derive a~linear-time algorithm for the derangements from it.
1265
1266 \defn\id{permnota}%
1267 We will fix a~non-negative integer~$n$ and use ${\cal P}$ for the set of
1268 all~permutations on~$[n]$.
1269 A~\df{restriction graph} is a~bipartite graph~$G$ whose parts are two copies
1270 of the set~$[n]$. A~permutation $\pi\in{\cal P}$ satisfies the restrictions
1271 if $(i,\pi(i))$ is an~edge of~$G$ for every~$i$.
1272
1273 \paran{Equivalent formulations}%
1274 We will follow the path unthreaded by Kaplansky and Riordan
1275 \cite{kaplansky:rooks} and charted by Stanley in \cite{stanley:econe}.
1276 We will relate restricted permutations to placements of non-attacking
1277 rooks on a~hollow chessboard.
1278
1279 \defn
1280 A~\df{board} is the grid $B=[n]\times [n]$. It consists of $n^2$ \df{squares.}
1281 A~\df{trace} of a~permutation $\pi\in{\cal P}$ is the set of squares \hbox{$T(\pi)=\{ (i,\pi(i)) ; i\in[n] \}$.}
1282
1283 \obs\id{rooksobs}%
1284 The traces of permutations (and thus the permutations themselves) correspond
1285 exactly to placements of $n$ rooks at the board in a~way such that the rooks do
1286 not attack each other (i.e., there is at most one rook in every row and
1287 likewise in every column; as there are $n$~rooks, there must be exactly one of them in
1288 every row and column). When speaking about \df{rook placements,} we will always
1289 mean non-attacking placements.
1290
1291 Restricted permutations then correspond to placements of rooks on a~board with
1292 some of the squares removed. The \df{holes} (missing squares) correspond to the
1293 non-edges of~$G$, so $\pi\in{\cal P}$ satisfies the restrictions iff
1294 $T(\pi)$ avoids the holes.
1295
1296 Placements of~$n$ rooks (and therefore also restricted permutations) can be
1297 also equated with perfect matchings in the restriction graph~$G$. The edges
1298 of the matching correspond to the squares occupied by the rooks, the condition
1299 that no two rooks share a~row nor column translates to the edges not touching
1300 each other, and the use of exactly~$n$ rooks is equivalent to the matching
1301 being perfect.
1302
1303 There is also a~well-known correspondence between the perfect matchings
1304 in a~bipartite graph and non-zero summands in the formula for the permanent
1305 of the bipartite adjacency matrix~$M$ of the graph. This holds because the
1306 non-zero summands are in one-to-one correspondence with the placements
1307 of~$n$ rooks on the corresponding board. The number of restricted
1308 permutations is therefore equal to the permanent of the matrix~$M$.
1309
1310 The diversity of the characterizations of restricted permutations brings
1311 both good and bad news. The good news is that we can use the
1312 plethora of known results on bipartite matchings. Most importantly, we can efficiently
1313 determine whether there exists at least one permutation satisfying a~given set of restrictions:
1314
1315 \thm
1316 There is an~algorithm which decides in time $\O(n^{1/2}\cdot m)$ whether there exists
1317 a~permutation satisfying a~given restriction graph. The $n$ and~$m$ are the number
1318 of vertices and edges of the restriction graph.
1319
1320 The bad news is that computing the permanent is known to be~$\#\rm P$-complete even
1321 for zero-one matrices (as proven by Valiant \cite{valiant:permanent}).
1322 As a~ranking function for a~set of~matchings can be used to count all such
1323 matchings, we obtain the following theorem:
1324
1325 \thm\id{pcomplete}%
1326 If there is a~polynomial-time algorithm for lexicographic ranking of permutations with
1327 a~set of restrictions which is a~part of the input, then $\rm P=\#P$.
1328
1329 However, the hardness of computing the permanent is the only obstacle.
1330 We show that whenever we are given a~set of restrictions for which
1331 the counting problem is easy (and it is also easy for subgraphs obtained
1332 by deleting vertices), ranking is easy as well. The key will be once again
1333 a~recursive structure, similar to the one we have seen in the case of plain
1334 permutations in \ref{permrec}. We get:
1335
1336 \thmn{Lexicographic ranking of restricted permutations}
1337 Suppose that we have a~family of matrices ${\cal M}=\{M_1,M_2,\ldots\}$ such that $M_n\in \{0,1\}^{n\times n}$
1338 and it is possible to calculate the permanent of~$M'$ in time $\O(t(n))$ for every matrix $M'$
1339 obtained by deletion of rows and columns from~$M_n$. Then there exist algorithms
1340 for ranking and unranking in ${\cal P}_{A,M_n}$ in time $\O(n^4 + n^2\cdot t(n))$
1341 if $M_n$ and an~$n$-element set~$A$ are given as a~part of the input.
1342
1343 Our time bound for ranking of general restricted permutations section is obviously very coarse.
1344 Its main purpose was to demonstrate that many special cases of the ranking problem can be indeed computed in polynomial time.
1345 For most families of restriction matrices, we can do much better. These speedups are hard to state formally
1346 in general (they depend on the structure of the matrices), but we demonstrate them on the
1347 specific case of derangements. We show that each matrix can be sufficiently characterized
1348 by two numbers: the order of the matrix and the number of zeroes in it. We find a~recurrent
1349 formula for the permanent, based on these parameters, which we use to precalculate all
1350 permanents in advance. When we plug it in the general algorithm, we get:
1351
1352 \thmn{Ranking of derangements}%
1353 For every~$n$, the derangements on the set~$[n]$ can be ranked and unranked according to the
1354 lexicographic order in time~$\O(n)$ after spending $\O(n^2)$ on initialization of auxiliary tables.
1355
1356 \schapter{Conclusions}
1357
1358 We have seen the many facets of the minimum spanning tree problem. It has
1359 turned out that while the major question of the existence of a~linear-time
1360 MST algorithm is still open, backing off a~little bit in an~almost arbitrary
1361 direction leads to a~linear solution. This includes classes of graphs with edge
1362 density at least $\lambda_k(n)$ (the $k$-th row inverse of the Ackermann's function) for an~arbitrary fixed~$k$,
1363 minor-closed classes, and graphs whose edge weights are
1364 integers. Using randomness also helps, as does having the edges pre-sorted.
1365
1366 If we do not know anything about the structure of the graph and we are only allowed
1367 to compare the edge weights, we can use the Pettie's MST algorithm.
1368 Its time complexity is guaranteed to be asymptotically optimal,
1369 but we do not know what it really is --- the best what we have is
1370 an~$\O(m\timesalpha(m,n))$ upper bound and the trivial $\Omega(m)$ lower bound.
1371
1372 One thing we however know for sure. The algorithm runs on the weakest of our
1373 computational models ---the Pointer Machine--- and its complexity is linear
1374 in the minimum number of comparisons needed to decide the problem. We therefore
1375 need not worry about the details of computational models, which have contributed
1376 so much to the linear-time algorithms for our special cases. Instead, it is sufficient
1377 to study the complexity of MST decision trees. However, not much is known about these trees so far.
1378
1379 As for the dynamic algorithms, we have an~algorithm which maintains the minimum
1380 spanning forest within poly-logarithmic time per operation.
1381 The optimum complexity is once again undecided --- the known lower bounds are very far
1382 from the upper ones.
1383 The known algorithms run on the Pointer machine and we do not know if using a~stronger
1384 model can help.
1385
1386 For the ranking problems, the situation is completely different. We have shown
1387 linear-time algorithms for three important problems of this kind. The techniques,
1388 which we have used, seem to be applicable to other ranking problems. On the other
1389 hand, ranking of general restricted permutations has turned out to balance on the
1390 verge of $\#{\rm P}$-completeness. All our algorithms run
1391 on the RAM model, which seems to be the only sensible choice for problems of
1392 inherently arithmetic nature. While the unit-cost assumption on arithmetic operations
1393 is not universally accepted, our results imply that the complexity of our algorithm
1394 is dominated by the necessary arithmetics.
1395
1396 Aside from the concrete problems we have solved, we have also built several algorithmic
1397 techniques of general interest: the unification procedures using pointer-based
1398 bucket sorting and the vector computations on the RAM. We hope that they will
1399 be useful in many other algorithms.
1400
1401 \schapter{Bibliography}
1402
1403 \dumpbib
1404
1405 \vfill\eject
1406 \ifodd\pageno\else\hbox{}\fi
1407
1408 \bye