X-Git-Url: http://mj.ucw.cz/gitweb/?a=blobdiff_plain;f=mst.tex;h=efc4d63238fe433e81007b922e78d9487b477198;hb=3ce379fec86d6348c80aa1d8c77fb64a0dbada13;hp=2a562dbbcb255d3db844bb7c09359624af24c7f3;hpb=27749502e55ddcb33010db5294e13f0ff2728668;p=saga.git diff --git a/mst.tex b/mst.tex index 2a562db..efc4d63 100644 --- a/mst.tex +++ b/mst.tex @@ -24,7 +24,7 @@ find its minimum spanning tree, defined as follows: For a given graph~$G$ with weights $w:E(G)\rightarrow {\bb R}$: \itemize\ibull \:A~subgraph $H\subseteq G$ is called a \df{spanning subgraph} if $V(H)=V(G)$. -\:A~\df{spanning tree} of $G$ is any its spanning subgraph which is a tree. +\:A~\df{spanning tree} of~$G$ is any spanning subgraph of~$G$ that is a tree. \:For any subgraph $H\subseteq G$ we define its \df{weight} $w(H):=\sum_{e\in E(H)} w(e)$. When comparing two weights, we will use the terms \df{lighter} and \df{heavier} in the obvious sense. @@ -41,25 +41,27 @@ spanning tree problem was one of the central topics of the flourishing new disciplines, the previous work was not well known and the algorithms had to be rediscovered several times. -Recently, several significantly faster algorithms were discovered, most notably the -$\O(m\timesbeta(m,n))$-time algorithm by Fredman and Tarjan \cite{ft:fibonacci} and -algorithms with inverse-Ackermann type complexity by Chazelle \cite{chazelle:ackermann} -and Pettie \cite{pettie:ackermann}. +In the next 50 years, several significantly faster algorithms were discovered, ranging +from the $\O(m\timesbeta(m,n))$ time algorithm by Fredman and Tarjan \cite{ft:fibonacci}, +over algorithms with inverse-Ackermann type complexity by Chazelle \cite{chazelle:ackermann} +and Pettie \cite{pettie:ackermann}, to another algorithm by Pettie \cite{pettie:optimal} +whose time complexity is provably optimal. -\FIXME{Write the rest of the history.} - -This chapter attempts to survery the important algorithms for finding the MST and it -also presents several new ones. +In the upcoming chapters, we will explore this colorful universe of MST algorithms. +We will meet the canonical works of the classics, the clever ideas of their successors, +various approaches to the problem including randomization and solving of important +special cases. At several places, we will try to contribute our little stones to this +mosaic. %-------------------------------------------------------------------------------- -\section{Basic properties} +\section{Basic properties}\id{mstbasics}% In this section, we will examine the basic properties of spanning trees and prove -several important theorems to base the algorithms upon. We will follow the theory -developed by Tarjan in~\cite{tarjan:dsna}. +several important theorems which will serve as a~foundation for our MST algorithms. +We will mostly follow the theory developed by Tarjan in~\cite{tarjan:dsna}. -For the whole section, we will fix a graph~$G$ with edge weights~$w$ and all +For the whole section, we will fix a~connected graph~$G$ with edge weights~$w$ and all other graphs will be spanning subgraphs of~$G$. We will use the same notation for the subgraphs as for the corresponding sets of edges. @@ -75,12 +77,13 @@ Let~$T$ be a~spanning tree. Then: the edges of this path \df{edges covered by~$e$}. \: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 is an edge $f\in T[e]$ such that $w(f) > w(e)$. -\:An edge~$e$ is called \df{$T$-heavy} if it is not $T$-light. +\:An edge~$e$ is called \df{$T$-heavy} if it covers a~lighter edge. \endlist \rem -Please note that the above properties also apply to tree edges -which by definition cover only themselves and therefore they are always heavy. +Edges of the tree~$T$ cover only themselves and thus they are neither heavy nor light. +The same can happen if an~edge outside~$T$ covers only edges of the same weight, +but this will be rare because all edge weights will be usually distinct. \lemman{Light edges}\id{lightlemma}% Let $T$ be a spanning tree. If there exists a $T$-light edge, then~$T$ @@ -105,7 +108,7 @@ to any other spanning tree by a sequence of exchanges. \lemman{Exchange property for trees}\id{xchglemma}% Let $T$ and $T'$ be spanning trees of a common graph. Then there exists -a sequence of edge exchanges which transforms $T$ to~$T'$. More formally, +a sequence of edge exchanges that transforms $T$ to~$T'$. More formally, there exists a sequence of spanning trees $T=T_0,T_1,\ldots,T_k=T'$ such that $T_{i+1}=T_i - e_i + e_i^\prime$ where $e_i\in T_i$ and $e_i^\prime\in T'$. @@ -124,7 +127,7 @@ hypothesis to $T^*$ and $T'$ to get the rest of the exchange sequence. \lemman{Monotone exchanges}\id{monoxchg}% Let $T$ be a spanning tree such that there are no $T$-light edges and $T'$ be an arbitrary spanning tree. Then there exists a sequence of edge exchanges -transforming $T$ to~$T'$ such that the weight does not increase in any step. +transforming $T$ to~$T'$ such that the weight does not decrease in any step. \proof We improve the argument from the previous proof, refining the induction step. @@ -146,7 +149,7 @@ $w(f)\ge w(e')$ due to the choice of $e'$ and all other edges on~$C$ are lighter than~$e'$ as $e'$ was not $T$-light. \qed -\thmn{Minimality by order}\id{mstthm}% +\thmn{Minimality of spanning trees}\id{mstthm}% A~spanning tree~$T$ is minimum iff there is no $T$-light edge. \proof @@ -165,7 +168,7 @@ minimum spanning trees according to the Cayley's formula \cite{cayley:trees}). However, as the following theorem shows, this is possible only if the weight function is not injective. -\thmn{MST uniqueness}% +\thmn{Uniqueness of MST}% If all edge weights are distinct, then the minimum spanning tree is unique. \proof @@ -178,41 +181,58 @@ we know that $w(T_1)=w(T_2)$, so the exchange sequence must be empty and indeed $T_1$ and $T_2$ must be identical. \qed -\rem\id{edgeoracle}% -To simplify the description of MST algorithms, we will expect that the weights -of all edges are distinct and that instead of numeric weights (usually accompanied -by problems with representation of real numbers in algorithms) we will be given -a comparison oracle, that is a function which answers questions ``$w(e) +and \. The \ operation tests whether two elements are equivalent and \ +joins two different equivalence classes into one. -\thmn{Incremental connectivity}% -When only edge insertions and connectivity queries are allowed, connected components -can be maintained in $\O(\alpha(n))$ time amortized per operation. +\para +We can maintain the connected components of our forest~$T$ as equivalence classes. When we want +to add an~edge~$uv$, we first call $\(u,v)$ to check if both endpoints of the edge lie in +the same components. If they do not, addition of this edge connects both components into one, +so we perform $\(u,v)$ to merge the equivalence classes. + +Tarjan and van Leeuwen have shown that there is a~data structure for the DSU problem +with surprising efficiency: + +\thmn{Disjoint Set Union, Tarjan and van Leeuwen \cite{tarjan:setunion}}\id{dfu}% +Starting with a~trivial equivalence with single-element classes, a~sequence of operations +comprising of $n$~\s intermixed with $m\ge n$~\s can be processed in time +$\O(m\timesalpha(m,n))$, where $\alpha(m,n)$ is a~certain inverse of the Ackermann's function +(see Definition \ref{ackerinv}). \proof -Proven by Tarjan and van Leeuwen in \cite{tarjan:setunion}. +See \cite{tarjan:setunion}. \qed -\FIXME{Define Ackermann's function. Use $\alpha(m,n)$?} +This completes the following theorem: + +\thm\id{kruskal}% +Kruskal's algorithm finds the MST of a given graph in time $\O(m\log n)$. +If the edges are already sorted by their weights, the time drops to +$\O(m\timesalpha(m,n))$. + +\proof +We spend $\O(m\log n)$ on sorting, $\O(m\timesalpha(m,n))$ on processing the sequence +of \s and \s, and $\O(m)$ on all other work. +\qed \rem -The cost of the operations on components is of course dwarfed by the complexity +The cost of the \ and \ operations is of course dwarfed by the complexity of sorting, so a much simpler (at least in terms of its analysis) data structure would be sufficient, as long as it has $\O(\log n)$ amortized complexity per operation. For example, we can label vertices with identifiers of the -corresponding components and always recolor the smaller of the two components. - -\thm -Kruskal's algorithm finds the MST of a given graph in time $\O(m\log n)$ -or $\O(m\timesalpha(n))$ if the edges are already sorted by their weights. +corresponding components and always relabel the smaller of the two components. -\proof -Follows from the above analysis. -\qed +We will study dynamic maintenance of connected components in more detail in Chapter~\ref{dynchap}. %-------------------------------------------------------------------------------- @@ -477,7 +556,7 @@ Follows from the above analysis. While the classical algorithms are based on growing suitable trees, they can be also reformulated in terms of edge contraction. Instead of keeping -a forest of trees, we can keep each tree contracted to a single vertex. +a~forest of trees, we can keep each tree contracted to a single vertex. This replaces the relatively complex tree-edge incidencies by simple vertex-edge incidencies, potentially speeding up the calculation at the expense of having to perform the contractions. @@ -486,8 +565,8 @@ We will show a contractive version of the Bor\o{u}vka's algorithm in which these costs are carefully balanced, leading for example to a linear-time algorithm for MST in planar graphs. -There are two definitions of edge contraction which differ when an edge of a -triangle is contracted. Either we unify the other two edges to a single edge +There are two definitions of edge contraction that differ when an edge of +a~triangle is contracted. Either we unify the other two edges to a single edge or we keep them as two parallel edges, leaving us with a~multigraph. We will use the multigraph version and we will show that we can easily reduce the multigraph to a simple graph later. (See \ref{contract} for the exact definitions.) @@ -504,14 +583,10 @@ Then $G'$~has the same MST as~$G$. \proof Every spanning tree of~$G'$ is a spanning tree of~$G$. In the other direction: Loops can be never contained in a spanning tree. If there is a spanning tree~$T$ -containing a removed edge~$e$ parallel to an edge~$e'\in G'$, exchaning $e'$ -for~$e$ makes~$T$ lighter. \qed - -\rem Removal of the heavier of a pair of parallel edges can be also viewed -as an application of the Red rule on a two-edge cycle. And indeed it is, the -Red-Blue procedure works on multigraphs as well as on simple graphs and all the -classical algorithms also do. We would only have to be more careful in the -formulations and proofs, which we preferred to avoid. +containing a~removed edge~$e$ parallel to an edge~$e'\in G'$, exchanging $e'$ +for~$e$ makes~$T$ lighter. (This is indeed the multigraph version of the Red +lemma applied to a~two-edge cycle, as we will see in \ref{multimst}.) +\qed \algn{Contractive version of Bor\o{u}vka's algorithm}\id{contbor} \algo @@ -519,58 +594,63 @@ formulations and proofs, which we preferred to avoid. \:$T\=\emptyset$. \:$\ell(e)\=e$ for all edges~$e$. \cmt{Initialize the labels.} \:While $n(G)>1$: -\::For each vertex $v_i$ of~$G$, let $e_i$ be the lightest edge incident to~$v_i$. -\::$T\=T\cup \{ \ell(e_i) \}$. \cmt{Remember labels of all selected edges.} -\::Contract $G$ along all edges $e_i$, inheriting labels and weights.\foot{In other words, we ask the comparison oracle for the edge $\ell(e)$ instead of~$e$.} +\::For each vertex $v_k$ of~$G$, let $e_k$ be the lightest edge incident to~$v_k$. +\::$T\=T\cup \{ \ell(e_k) \}$. \cmt{Remember labels of all selected edges.} +\::Contract all edges $e_k$, inheriting labels and weights.\foot{In other words, we ask the comparison oracle for the edge $\ell(e)$ instead of~$e$.} \::Flatten $G$, removing parallel edges and loops. \algout Minimum spanning tree~$T$. \endalgo +\nota +For the analysis of the algorithm, we will denote the graph considered by the algorithm +at the beginning of the $i$-th iteration by $G_i$ (starting with $G_0=G$) and the number +of vertices and edges of this graph by $n_i$ and $m_i$ respectively. + \lemma\id{contiter}% -Each iteration of the algorithm can be carried out in time~$\O(m)$. +The $i$-th iteration of the algorithm (also called the \df{Bor\o{u}vka step}) can be carried +out in time~$\O(m_i)$. \proof The only non-trivial parts are steps 6 and~7. Contractions can be handled similarly to the unions in the original Bor\o{u}vka's algorithm (see \ref{boruvkaiter}): -We build an auxillary graph containing only the selected edges~$e_i$, find +We build an auxiliary graph containing only the selected edges~$e_k$, find connected components of this graph and renumber vertices in each component to -the identifier of the component. This takes $\O(m)$ time. +the identifier of the component. This takes $\O(m_i)$ time. Flattening is performed by first removing the loops and then bucket-sorting the edges (as ordered pairs of vertex identifiers) lexicographically, which brings parallel -edges together. The bucket sort uses two passes with $n$~buckets, so it takes -$\O(n+m)=\O(m)$. +edges together. The bucket sort uses two passes with $n_i$~buckets, so it takes +$\O(n_i+m_i)=\O(m_i)$. \qed -\thm -The Contractive Bor\o{u}vka's algorithm finds the MST in time $\O(m\log n)$. +\thm\id{contborthm}% +The Contractive Bor\o{u}vka's algorithm finds the MST of the input graph in +time $\O(\min(n^2,m\log n))$. \proof As in the original Bor\o{u}vka's algorithm, the number of iterations is $\O(\log n)$. -Then apply the previous lemma. +When combined with the previous lemma, it gives an~$\O(m\log n)$ upper bound. + +To get the $\O(n^2)$ bound, we observe that the number of trees in the non-contracting +version of the algorithm drops at least by a factor of two in each iteration (Lemma \ref{boruvkadrop}) +and the same must hold for the number of vertices in the contracting version. +Therefore $n_i\le n/2^i$. While the number of edges need not decrease geometrically, +we still have $m_i\le n_i^2$ as the graphs~$G_i$ are simple (we explicitly removed multiple +edges and loops at the end of the previous iteration). Hence the total time spent +in all iterations is $\O(\sum_i n_i^2) = \O(\sum_i n^2/4^i) = \O(n^2)$. \qed -\thmn{\cite{mm:mst}}\id{planarbor}% +\thmn{Contractive Bor\o{u}vka on planar graphs, \cite{mm:mst}}\id{planarbor}% When the input graph is planar, the Contractive Bor\o{u}vka's algorithm runs in time $\O(n)$. \proof -Let us denote the graph considered by the algorithm at the beginning of the $i$-th -iteration by $G_i$ (starting with $G_0=G$) and its number of vertices and edges -by $n_i$ and $m_i$ respectively. As we already know from the previous lemma, -the $i$-th iteration takes $\O(m_i)$ time. We are going to prove that the -$m_i$'s are decreasing geometrically. - -The number of trees in the non-contracting version of the algorithm drops -at least by a factor of two in each iteration (Lemma \ref{boruvkadrop}) and the -same must hold for the number of vertices in the contracting version. -Therefore $n_i\le n/2^i$. - -However, every $G_i$ is planar, because the class of planar graphs is closed -under edge deletion and contraction. The~$G_i$ is also simple as we explicitly removed multiple edges and -loops at the end of the previous iteration. Hence we can use the standard theorem on +Let us refine the previous proof. We already know that $n_i \le n/2^i$. We will +prove that when~$G$ is planar, the $m_i$'s are decreasing geometrically. We know that every +$G_i$ is planar, because the class of planar graphs is closed under edge deletion and +contraction. Moreover, $G_i$~is also simple, so we can use the standard theorem on the number of edges of planar simple graphs (see for example \cite{diestel:gt}) to get $m_i\le 3n_i \le 3n/2^i$. -From this we get that the total time complexity is $\O(\sum_i m_i)=\O(\sum_i n/2^i)=\O(n)$. +The total time complexity of the algorithm is therefore $\O(\sum_i m_i)=\O(\sum_i n/2^i)=\O(n)$. \qed \rem @@ -584,13 +664,13 @@ in section~\ref{minorclosed}. To achieve the linear time complexity, the algorithm needs a very careful implementation, but we defer the technical details to section~\ref{bucketsort}. -\para +\paran{General contractions}% Graph contractions are indeed a~very powerful tool and they can be used in other MST algorithms as well. The following lemma shows the gist: \lemman{Contraction of MST edges}\id{contlemma}% Let $G$ be a weighted graph, $e$~an arbitrary edge of~$\mst(G)$, $G/e$ the multigraph -produced by contracting $G$ along~$e$, and $\pi$ the bijection between edges of~$G-e$ and +produced by contracting~$e$ in~$G$, and $\pi$ the bijection between edges of~$G-e$ and their counterparts in~$G/e$. Then: $$\mst(G) = \pi^{-1}[\mst(G/e)] + e.$$ \proof @@ -611,10 +691,11 @@ which obviously works in multigraphs as well.) \rem In the previous algorithm, the role of the mapping~$\pi^{-1}$ is of course played by the edge labels~$\ell$. -Finally, we will show a family of graphs where the $\O(m\log n)$ bound on time complexity +\paran{A~lower bound}% +Finally, we will show a family of graphs for which the $\O(m\log n)$ bound on time complexity is tight. The graphs do not have unique weights, but they are constructed in a way that the algorithm never compares two edges with the same weight. Therefore, when two such -graphs are monotonely isomorphic (see~\ref{mstiso}), the algorithm processes them in the same way. +graphs are monotonically isomorphic (see~\ref{mstiso}), the algorithm processes them in the same way. \defn A~\df{distractor of order~$k$,} denoted by~$D_k$, is a path on $n=2^k$~vertices $v_1,\ldots,v_n$ @@ -668,4 +749,55 @@ to finish on the remaining complete graph. Each iteration runs on a graph with $ edges as every $H_{a,k}$ contains a complete graph on~$a$ vertices. \qed +%-------------------------------------------------------------------------------- + +\section{Lifting restrictions} + +In order to have a~simple and neat theory, we have introduced several restrictions +on the graphs in which we search for the MST. As in some rare cases we are going to +meet graphs that do not fit into this simplified world, let us quickly examine what +happens when the restrictions are lifted. + +\paran{Disconnected graphs}\id{disconn}% +The basic properties of minimum spanning trees and the algorithms presented in +this chapter apply to minimum spanning forests of disconnected graphs, too. +The proofs of our theorems and the steps of our algorithms are based on adjacency +of vertices and existence of paths, so they are always local to a~single +connected component. The Bor\o{u}vka's and Kruskal's algorithm need no changes, +the Jarn\'\i{}k's algorithm has to be invoked separately for each component. + +We can also extend the notion of light and heavy edges with respect +to a~tree to forests: When an~edge~$e$ connects two vertices lying in the same +tree~$T$ of a~forest~$F$, it is $F$-heavy iff it is $T$-heavy (similarly +for $F$-light). Edges connecting two different trees are always considered +$F$-light. Again, a~spanning forest~$F$ is minimum iff there are no $F$-light +edges. + +\paran{Multigraphs}\id{multimst}% +All theorems and algorithms from this chapter work for multigraphs as well, +only the notation sometimes gets crabbed, which we preferred to avoid. The Minimality +theorem and the Blue rule stay unchanged. The Red rule is naturally extended to +self-loops (which are never in the MST) and two-edge cycles (where the heavier +edge can be dropped) as already suggested in the Flattening lemma (\ref{flattening}). + +\paran{Multiple edges of the same weight}\id{multiweight}% +In case when the edge weights are not distinct, the characterization of minimum +spanning trees using light edges is still correct, but the MST is no longer unique +(as already mentioned, there can be as much as~$n^{n-2}$ MST's). + +In the Red-Blue procedure, we have to avoid being too zealous. The Blue lemma cannot +guarantee that when a~cut contains multiple edges of the minimum weight, all of them +are in the MST. It will however tell that if we pick one of these edges, an~arbitrary +MST can be modified to another MST that contains this edge. Therefore the Blue rule +will change to ``Pick a~cut~$C$ such that it does not contain any blue edge and color +one of its lightest edges blue.'' The Red lemma and the Red rule can be handled +in a~similar manner. The modified algorithm will be then guaranteed to find one of +the possible MST's. + +The Kruskal's and Jarn\'\i{}k's algorithms keep working. This is however not the case of the +Bor\o{u}vka's algorithm, whose proof of correctness in Lemma \ref{borcorr} explicitly referred to +distinct weights and indeed, if they are not distinct, the algorithm will occasionally produce +cycles. To avoid the cycles, the ties in edge weight comparisons have to be broken in a~systematic +way. The same applies to the contractive version of this algorithm. + \endpart