5 \chapter{Dynamic Spanning Trees}\id{dynchap}%
7 \section{Dynamic graph algorithms}
9 In many applications, we often need to solve a~certain graph problem for a~sequence
10 of graphs that differ only a~little, so recomputing the solution from scratch for
11 every graph would be a~waste of time. In such cases, we usually turn our attention
12 to \df{dynamic graph algorithms.} A~dynamic algorithm is in fact a~data structure
13 that remembers a~graph and offers operations that modify the structure of the graph
14 and also operations that query the result of the problem for the current state
15 of the graph. A~typical example of a~problem of this kind is dynamic
16 maintenance of connected components:
18 \problemn{Dynamic connectivity}
19 Maintain an~undirected graph under a~sequence of the following operations:
21 \:$\<Init>(n)$ --- Create a~graph with $n$~isolated vertices $\{1,\ldots,n\}$.\foot{%
22 The structure could support dynamic addition and removal of vertices, too,
23 but this is easy to add and infrequently used, so we will rather keep the set
24 of vertices fixed for clarity.}
25 \:$\<Insert>(G,u,v)$ --- Insert an~edge $uv$ to~$G$ and return its unique
26 identifier. This assumes that the edge did not exist yet.
27 \:$\<Delete>(G,e)$ --- Delete an~edge specified by its identifier from~$G$.
28 \:$\<Connected>(G,u,v)$ --- Test if $u$ and~$v$ are in the same connected component of~$G$.
32 We have already encountered a~special case of dynamic connectivity when implementing the
33 Kruskal's algorithm in Section \ref{classalg}. At that time, we did not need to delete
34 any edges from the graph, which makes the problem substantially easier. This special
35 case is customarily called an~\df{incremental} or \df{semidynamic} graph algorithm.
36 We mentioned the Disjoint Set Union data structure of Tarjan and van Leeuwen (Theorem \ref{dfu})
37 which can be used for that: Connected components are represented as an~equivalence classes.
38 Queries on connectedness translate to \<Find>, edge insertions to \<Find>
39 followed by \<Union> if the new edge joins two different components. This way,
40 a~sequence of $m$~operations starting with an~empty graph on $n$~vertices is
41 processed in time $\O(n+m\timesalpha(m,n))$ and this holds even for the Pointer
42 Machine. Fredman and Saks \cite{fredman:cellprobe} have proven a~matching lower
43 bound in the cell-probe model which is stronger than RAM with $\O(\log n)$-bit
46 The edges that have triggered the \<Union>s form a~spanning forest of the current graph.
47 So far, all known algorithms for dynamic connectivity maintain some sort of a~spanning
48 tree. This suggests that a~dynamic MST algorithm could be obtained by modifying the
49 dynamic connectivity algorithms. This will indeed turn out to be true. Semidynamic MST
50 is easy to achieve even in the few pages of this section, but making it fully dynamic will require
51 more effort, so we will review some of the required building blocks before going into that.
53 We however have to answer one important question first: What should be the output of
54 our MSF data structure? Adding an~operation that would return the MSF of the current
55 graph is of course possible, but somewhat impractical as this operation has to
56 spend $\Omega(n)$ time on the mere writing of its output. A~better way seems to
57 be making the \<Insert> and \<Delete> operations report the list of modifications
58 of the MSF implied by the change in the graph.
60 Let us see what happens when we \<Insert> an~edge~$e$ to a~graph~$G$ with its minimum spanning
61 forest~$F$, obtaining a~new graph~$G'$ with its MSF~$F'$. If $e$~connects two components of~$G$ (and
62 therefore also of~$F$), we have to add~$e$ to~$F$. Otherwise, one of the following cases happens:
63 Either $e$~is $F$-heavy and so the forest~$F$ is also the MSF of the new graph. Or it is $F$-light
64 and we have to modify~$F$ by exchanging the heaviest edge~$f$ of the path $F[e]$ with~$e$.
66 Correctness of the former case follows immediately from the Theorem on Minimality by order
67 (\ref{mstthm}), because all $F'$-light would be also $F$-light, which is impossible as $F$~was
68 minimum. In the latter case, the edge~$f$ is not contained in~$F'$ because it is the heaviest
69 on the cycle $F[e]+e$ (by the Red lemma, \ref{redlemma}). We can now use the Blue lemma
70 (\ref{bluelemma}) to prove that it should be replaced with~$e$. Consider the tree~$T$
71 of~$F$ that contains both endpoints of the edge~$e$. When we remove~$f$ from~$F$, this tree falls
72 apart to two components $T_1$ and~$T_2$. The edge~$f$ was the lightest edge of the cut~$\delta_G(T_1)$
73 and $e$~is lighter than~$f$, so $e$~is the lightest in~$\delta_{G'}(T_1)$ and hence $e\in F'$.
75 A~\<Delete> of an~edge that is not contained in~$F$ does not change~$F$. When we delete
76 an~MSF edge, we have to reconnect~$F$ by choosing the lightest edge of the cut separating
77 the new components (again the Blue lemma in action). If there is no such
78 replacement edge, we have deleted a~bridge, so the MSF has to remain
81 The idea of reporting differences in the MSF indeed works very well. We can summarize
82 what we have shown in the following lemma and use it to define the dynamic MSF.
85 An~\<Insert> or \<Delete> of an~edge in~$G$ causes at most one edge addition, edge
86 removal or edge exchange in $\msf(G)$.
88 \problemn{Dynamic minimum spanning forest}
89 Maintain an~undirected graph with distinct weights on edges (drawn from a~totally ordered set)
90 and its minimum spanning forest under a~sequence of the following operations:
92 \:$\<Init>(n)$ --- Create a~graph with $n$~isolated vertices $\{1,\ldots,n\}$.
93 \:$\<Insert>(G,u,v)$ --- Insert an~edge $uv$ to~$G$. Return its unique
94 identifier and the list of additions and deletions of edges in $\msf(G)$.
95 \:$\<Delete>(G,e)$ --- Delete an~edge specified by its identifier from~$G$.
96 Return the list of additions and deletions of edges in $\msf(G)$.
99 \paran{Semidynamic MSF}%
100 To obtain a~semidynamic MSF algorithm, we need to keep the forest in a~data structure that
101 supports search for the heaviest edge on the path connecting a~given pair
102 of vertices. This can be handled efficiently by the Link-Cut trees of Sleator and Tarjan:
104 \thmn{Link-Cut Trees, Sleator and Tarjan \cite{sleator:trees}}\id{sletar}%
105 There is a~data structure that represents a~forest of rooted trees on~$n$ vertices.
106 Each edge of the forest has a~weight drawn from a~totally ordered set. The structure
107 supports the following operations in time $\O(\log n)$ amortized:\foot{%
108 The Link-Cut trees offer many other operations, but we do not mention them
109 as they are not needed in our application.}
111 \:$\<Parent>(v)$ --- Return the parent of~$v$ in its tree or \<null> if $v$~is a~root.
112 \:$\<Root>(v)$ --- Return the root of the tree containing~$v$.
113 \:$\<Weight>(v)$ --- Return the weight of the edge $(\<Parent(v)>,v)$.
114 \:$\<PathMax>(v)$ --- Return the vertex~$w$ closest to $\<Root>(v)$ such that the edge
115 $(\<Parent>(w),w)$ is the heaviest of those on the path from the root to~$v$.
116 If more edges have the maximum weight, break the tie arbitrarily.
117 If there is no such edge ($v$~is the root itself), return \<null>.
118 \:$\<Link>(u,v,x)$ --- Connect the trees containing $u$ and~$v$ by an~edge $(u,v)$ of
119 weight~$x$. Assumes that $u~$is a tree root and $v$~lies in a~different tree.
120 \:$\<Cut>(v)$ --- Split the tree containing the non-root vertex $v$ to two trees by
121 removing the edge $(\<Parent>(v),v)$. Returns the weight of this edge.
122 \:$\<Evert>(v)$ --- Modify the orientations of the edges in the tree containing~$v$
123 to make~$v$ the tree's root.
126 %% \>Additionally, all edges on the path from~$v$ to $\<Root>(v)$ can be enumerated in
127 %% time $\O(\ell + \log n)$, where $\ell$~is the length of that path. This operation
128 %% (and also the path itself) is called $\<Path>(v)$.
130 %% \>If the weights are real numbers (or in general an~arbitrary group), the $\O(\log n)$
131 %% operations also include:
134 %% \:$\<PathWeight>(v)$ --- Return the sum of the weights on $\<Path>(v)$.
135 %% \:$\<PathUpdate>(v,x)$ --- Add~$x$ to the weights of all edges on $\<Path>(v)$.
139 See \cite{sleator:trees}.
142 Once we have this structure, we can turn our ideas on updating of the MSF to
143 an~incremental algorithm:
145 \algn{\<Insert> in a~semidynamic MSF}
147 \algin A~graph~$G$ with its MSF $F$ represented as a~Link-Cut forest, an~edge~$uv$
148 with weight~$w$ to be inserted.
149 \:$\<Evert>(u)$. \cmt{$u$~is now the root of its tree.}
150 \:If $\<Root>(v) \ne u$: \cmt{$u$~and~$v$ lie in different trees.}
151 \::$\<Link>(u,v,w)$. \cmt{Connect the trees.}
152 \::Return ``$uv$ added''.
153 \:Otherwise: \cmt{both are in the same tree}
154 \::$y\=\<PathMax>(v)$.
155 \::$x\=\<Parent>(y)$. \cmt{Edge~$xy$ is the heaviest on $F[uv]$.}
156 \::If $\<Weight>(y) > w$: \cmt{We have to exchange~$xy$ with~$uv$.}
157 \:::$\<Cut>(y)$, $\<Evert>(v)$, $\<Link>(v,y,w)$.
158 \:::Return ``$uv$~added, $xy$~removed''.
159 \::Otherwise return ``no changes''.
160 \algout The list of changes in~$F$.
163 \thmn{Incremental MSF}
164 When only edge insertions are allowed, the dynamic MSF can be maintained in time $\O(\log n)$
165 amortized per operation.
168 Every \<Insert> performs $\O(1)$ operations on the Link-Cut forest, which take
169 $\O(\log n)$ each by Theorem \ref{sletar}.
173 We can easily extend the semidynamic MSF algorithm to allow an~operation commonly called
174 \<Backtrack> --- removal of the most recently inserted edge. It is sufficient to keep the
175 history of all MSF changes in a~stack and reverse the most recent change upon backtrack.
177 What are the obstacles to making the structure fully dynamic?
178 Deletion of edges that do not belong to the MSF is trivial (we do not
179 need to change anything) and so is deletion of bridges (we just remove the bridge
180 from the Link-Cut tree, knowing that there is no edge to replace it). The hard part
181 is the search for replacement edges after an~edge of the MSF is deleted.
183 As this very problem has to be solved by algorithms for fully dynamic connectivity,
184 we will take a~look on them first.
186 %--------------------------------------------------------------------------------
188 \section{Eulerian Tour trees}