From 535bfea08760be035c77b62eba92537e5e07afcf Mon Sep 17 00:00:00 2001 From: Martin Mares Date: Fri, 18 Jan 2008 17:15:47 +0100 Subject: [PATCH] Impl of Boruvka and Jarnik. --- macros.tex | 9 ++- mst.tex | 162 +++++++++++++++++++++++++++++++++++++++++------------ 2 files changed, 131 insertions(+), 40 deletions(-) diff --git a/macros.tex b/macros.tex index f763394..9039787 100644 --- a/macros.tex +++ b/macros.tex @@ -35,6 +35,7 @@ \def\FIXME#1{\>{\bo FIXME:} #1} \def\symdiff{\mathbin{\Delta}} \def\hphantas#1#2{\setbox0=\hbox{#2}\hbox to \wd0{#1\hss}} +\def\o#1{\accent23 #1} % Footnotes \newcount\footcnt @@ -288,19 +289,21 @@ } \def\proclaim#1{\advance\thmcount by 1 -\noindent {\bo #1 \the\chapcount.\the\seccount.\the\thmcount.\enspace}} +\noindent {\bo \the\chapcount.\the\seccount.\the\thmcount.\enspace #1.\enspace}} -\def\theorem{\proclaim{Theorem}} +\def\thm{\proclaim{Theorem}} \def\lemma{\proclaim{Lemma}} \def\defn{\proclaim{Definition}} \def\problem{\proclaim{Problem}} \def\obs{\proclaim{Observation}} \def\rem{\proclaim{Remark}} \def\alg{\proclaim{Algorithm}} +\def\impl{\proclaim{Implementation}} +\def\cor{\proclaim{Corollary}} \def\label#1{{\sl (#1)\/}\enspace} -\def\theoremn{\theorem\label} +\def\thmn{\thm\label} \def\lemman{\lemma\label} \def\defnn{\defn\label} \def\algn{\alg\label} diff --git a/mst.tex b/mst.tex index 357e3b2..bf1df23 100644 --- a/mst.tex +++ b/mst.tex @@ -9,7 +9,7 @@ The problem of finding a minimum spanning tree of a weighted graph is one of the best studied problems in the area of combinatorial optimization since its birth. Its colorful history (see \cite{graham:msthistory} and \cite{nesetril:history} for the full account) -begins in~1926 with the pioneering work of Bor\accent23uvka +begins in~1926 with the pioneering work of Bor\o{u}vka \cite{boruvka:ojistem}\foot{See \cite{nesetril:boruvka} for an English translation with commentary.}, who studied primarily an Euclidean version of the problem related to planning of electrical transmission lines (see \cite{boruvka:networks}), but gave an efficient @@ -34,7 +34,7 @@ For a given graph~$G$ with weights $w:E(G)\rightarrow {\bb R}$: a union of (minimum) spanning trees of its connected components. \endlist -Bor\accent23uvka's work was further extended by Jarn\'\i{}k \cite{jarnik:ojistem}, again in +Bor\o{u}vka's work was further extended by Jarn\'\i{}k \cite{jarnik:ojistem}, again in mostly geometric setting, giving another efficient algorithm. However, when computer science and graph theory started forming in the 1950's and the spanning tree problem was one of the central topics of the flourishing new @@ -144,7 +144,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 -\theorem\thmid{mstthm}% +\thm\thmid{mstthm}% A~spanning tree~$T$ is minimum iff there is no $T$-light edge. \proof @@ -163,7 +163,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. -\theoremn{MST uniqueness} +\thmn{MST uniqueness} If all edge weights are distinct, then the minimum spanning tree is unique. \proof @@ -255,13 +255,13 @@ and $V(G)\setminus M$ contains no blue edges, therefore we can use the blue rule \figure{mst-bez.eps}{295pt}{Configurations in the proof of the Black lemma} -\theoremn{Red-Blue correctness} +\thmn{Red-Blue correctness} For any selection of rules, the Red-Blue procedure stops and the blue edges form the minimum spanning tree of the input graph. \proof To prove that the procedure stops, let us notice that no edge is ever recolored, -so we must run out of black edges after at most $m$ steps. Recoloring +so we must run out of black edges after at most~$m$ steps. Recoloring to the same color is avoided by the conditions built in the rules, recoloring to a different color would mean that the an edge would be both inside and outside~$T_{min}$ due to our Red and Blue lemmata. @@ -274,25 +274,70 @@ lemma all other (red) edges are outside~$T_{min}$, so the blue edges are exactly \section{Classical algorithms} The three classical MST algorithms can be easily stated in terms of the Red-Blue meta-algorithm. +For each of them, we first state the general version of the algorithm, then we prove that +it gives the correct result and finally we discuss the time complexity of various +implementations. -\algn{Kruskal \cite{kruskal:mst}} +\algn{Bor\o{u}vka \cite{boruvka:ojistem}, Choquet \cite{choquet:mst}, Sollin \cite{sollin:mst} and others} \algo \algin A~graph~$G$ with an edge comparison oracle. -\:Sort edges of~$G$ by their increasing weight. -\:$T\=\emptyset$.~~(an empty spanning subgraph) -\:For all edges $e$ in their sorted order: -\::If $T+e$ is acyclic, add~$e$ to~$T$. -\::Otherwise drop~$e$. +\:$T\=$ a forest consisting of vertices of~$G$ and no edges. +\:While $T$ is not connected: +\::For each component $T_i$ of~$T$, choose the lightest edge $e_i$ from the cut + separating $T_i$ from the rest of~$T$. +\::Add all $e_i$'s to~$T$. \algout Minimum spanning tree~$T$. \endalgo +\lemma +Bor\o{u}vka's algorithm returns the MST of the input graph. + \proof -During the course of the algorithm, $T$ is a forest with all edges blue. Adding~$e$ to~$T$ -in step~4 applies the Blue rule on the cut separating two components of~$T$ ($e$ is the lightest, -because all other edges of the cut have not been considered yet). Dropping~$e$ in step~5 corresponds -to the red rule on the cycle found ($e$~must be the heaviest, since all other edges of the -cycle have been already processed). At the end of the algorithm, all edges have been colored, -so~$T$ must be the~MST. +In every iteration of the algorithm, $T$ is a blue subgraph, +because every addition of some edge~$e_i$ to~$T$ is a straightforward +application of the Blue rule. We stop when the blue subgraph is connected, so +we do not need the Red rule to explicitly exclude edges. + +It remains to show that adding the edges simultaneously does not +produce a cycle. Consider the first iteration of the algorithm where $T$ contains some cycle~$C$. Without +loss of generality we can assume that $C=T_1[u_1v_1]\,v_1u_2\,T_2[u_2v_2]\,v_2u_3\,T_3[u_3v_3]\, \ldots \,T_k[u_kv_k]\,v_ku_1$. +Each component $T_i$ has chosen its lightest incident edge~$e_i$ as either the edge $v_iu_{i+1}$ +or $v_{i-1}u_i$ (indexing cyclically). Assume that $e_1=v_1u_2$ (otherwise we reverse the orientation +of the cycle). Then $e_2=v_2u_3$ and $w(e_2)w(e_2)>\ldots>w(e_k)>w(e_1)$, +which is a contradiction. (Note that distinctness of edge weights was crucial here.) +\qed + +\lemma +In each iteration of the algorithm, the number of trees in~$T$ drops at least twice. + +\proof +Each tree gets merged with at least one neighboring trees, so each of the new trees +consists of at least two original trees. +\qed + +\cor +The algorithm stops in $\O(\log n)$ iterations. + +\lemma +Each iteration can be carried out in time $\O(m)$. + +\proof +Following \cite{mm:mst}, +we assign a label to each tree and we keep a mapping from vertices to the +labels of the trees they belong to. We scan all edges, map their endpoints +to the particular trees and for each tree we maintain the lightest incident edge +so far encountered. Instead of merging the trees one by one (which would be too +slow), we build an auxilliary graph whose vertices are labels of the original +trees and edges correspond to the chosen lightest inter-tree edges. We find connected +components of this graph, these determine how the original labels are translated +to the new labels. +\qed + +\thm +Bor\o{u}vka's algorithm finds the MST in time $\O(m\log n)$. + +\proof +Follows from the previous lemmata. \qed \algn{Jarn\'\i{}k \cite{jarnik:ojistem}, Prim \cite{prim:mst}, Dijkstra \cite{dijkstra:mst}} @@ -305,35 +350,72 @@ so~$T$ must be the~MST. \algout Minimum spanning tree~$T$. \endalgo +\lemma +Jarn\'\i{}k's algorithm returns the MST of the input graph. + \proof -In every step of the algorithm, $T$ is always a blue tree. Step~4 corresponds to applying +During the course of the algorithm, $T$ is always a blue tree. Step~4 corresponds to applying the Blue rule to a cut between~$T$ and the rest of the given graph. We need not care about the remaining edges, since for a connected graph the algorithm always stops with the right number of blue edges. \qed -\algn{Bor\accent23uvka \cite{boruvka:ojistem}, Choquet \cite{choquet:mst}, Sollin \cite{sollin:mst} and others} +\impl +The most important part of the algorithm is finding \em{neighboring edges,} i.e., edges +going between $T$ and $V(G)\setminus T$. In the straightforward implementation, +searching for the lightest neighboring edge takes $\Theta(m)$ time, so the whole +algorithm runs in time $\Theta(mn)$. + +We can do much better by using a binary +heap to hold all neighboring edges. In each iteration, we find and delete the +minimum edge from the heap and once we expand the tree, we insert the newly discovered +neighboring edges to the heap while deleting the neighboring edges which become +internal to the new tree. Since there are always at most~$m$ edges in the heap, +each heap operation takes $\O(\log m)=\O(\log n)$ time. For every edge, we perform +at most one insertion and at most one deletion, so we spend $\O(m\log n)$ time in total. +From this, we can conclude: + +\thm +Jarn\'\i{}k's algorithm finds the MST of the graph in time $\O(m\log n)$. + +\rem +We will show several faster implementations in section \secref{fibonacci}. + +\algn{Kruskal \cite{kruskal:mst}, the Greedy algorithm} \algo \algin A~graph~$G$ with an edge comparison oracle. -\:$T\=$ a forest consisting of vertices of~$G$ and no edges. -\:While $T$ is not connected: -\::For each component $T_i$ of~$T$, choose the lightest edge $e_i$ from the cut - separating $T_i$ from the rest of~$T$. -\::Add all $e_i$'s to~$T$. +\:Sort edges of~$G$ by their increasing weight. +\:$T\=\emptyset$. \cmt{an empty spanning subgraph} +\:For all edges $e$ in their sorted order: +\::If $T+e$ is acyclic, add~$e$ to~$T$. +\::Otherwise drop~$e$. \algout Minimum spanning tree~$T$. \endalgo +\lemma +Kruskal's algorithm returns the MST of the input graph. + \proof -Again $T$ is a blue forest, because every addition of some edge~$e_i$ to~$T$ is a straightforward -application of the Blue rule. +In every step, $T$ is a forest of blue trees. Adding~$e$ to~$T$ +in step~4 applies the Blue rule on the cut separating two components of~$T$ ($e$ is the lightest, +because all other edges of the cut have not been considered yet). Dropping~$e$ in step~5 corresponds +to the red rule on the cycle found ($e$~must be the heaviest, since all other edges of the +cycle have been already processed). At the end of the algorithm, all edges have been colored, +so~$T$ must be the~MST. +\qed -It remains to show that adding the edges simultaneously does not -produce a cycle. Consider the first iteration of the algorithm where $T$ contains some cycle~$C$. Without -loss of generality we can assume that $C=T_1[u_1v_1]\,v_1u_2\,T_2[u_2v_2]\,v_2u_3\,T_3[u_3v_3]\, \ldots \,T_k[u_kv_k]\,v_ku_1$. -Each component $T_i$ has chosen its lightest incident edge~$e_i$ as either the edge $v_iu_{i+1}$ -or $v_{i-1}u_i$ (indexing cyclically). Assume that $e_1=v_1u_2$ (otherwise we reverse the orientation -of the cycle). Then $e_2=v_2u_3$ and $w(e_2)w(e_2)>\ldots>w(e_k)>w(e_1)$, -which is a contradiction. (Note that distinctness of edge weights was crucial here.) +\impl +Except for the initial sorting, which in general takes $\O(m\log n)$ time, the only +other non-trivial operation is detection of cycles. This leads to a well known problem +of maintaining equivalence classes (disjoint set union), which we will present as ????? +%%% FIXME: cite + +\thm +Kruskal's algorithm finds the MST of the input graph in time $\O(m\log n)$ +or $\O(m\alpha(n))$ if the edges are already sorted. + +\proof +??? \qed \section{Contractive algorithms} @@ -345,7 +427,7 @@ 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. -We will show a contractive version of the Bor\accent23uvka's algorithm +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. @@ -376,7 +458,7 @@ do. We only have to be more careful in the formulations and proofs, which we preferred to avoid. We also note that most of the algorithms can be run on disconnected graphs with little or no modifications. -\algn{Contracting version of Bor\accent23uvka's algorithm} +\algn{Contracting version of Bor\o{u}vka's algorithm} \algo \algin A~graph~$G$ with an edge comparison oracle. \:$T\=\emptyset$. @@ -389,9 +471,15 @@ disconnected graphs with little or no modifications. \algout Minimum spanning tree~$T$. \endalgo -% G has to be connected +\section{Minor-closed graph classes} + +\section{Using Fibonacci heaps} +\secid{fibonacci} + +% G has to be connected, so m=O(n) % mention Steiner trees % mention matroids % sorted weights +% \O(...) as a set? \endpart -- 2.39.5