]> mj.ucw.cz Git - saga.git/blob - dyn.tex
Continuing with the intro to dynamic algorithms.
[saga.git] / dyn.tex
1 \ifx\endpart\undefined
2 \input macros.tex
3 \fi
4
5 \chapter{Dynamic Spanning Trees}\id{dynchap}%
6
7 \section{Dynamic graph algorithms}
8
9 In many applications, we often need to solve a~certain graph problem for a~sequence
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 (let's say by insertion and deletion of edges) and also operations that query the
15 result of the problem for the current state of the graph.
16 A~typical example of a~problem solved by such algorithms is dynamic maintenance of
17 connected components:
18
19 \problemn{Dynamic connectivity}
20 Maintain an~undirected graph under a~sequence of the following operations:
21 \itemize\ibull
22 \:$\<Init>(n)$ --- create a~graph with $n$~isolated vertices $\{1,\ldots,n\}$,\foot{%
23 The structure could support dynamic addition and removal of vertices, too,
24 but this is easy to add and infrequently used, so we will rather define
25 the structure with a~fixed set of vertices for clarity.}
26 \:$\<Insert>(G,u,v)$ --- insert an~edge $uv$ to~$G$ and return its unique
27 identifier (assuming that the edge did not exist yet),
28 \:$\<Delete>(G,e)$ --- delete an~edge specified by its identifier from~$G$,
29 \:$\<Connected>(G,u,v)$ --- test if $u$ and~$v$ are in the same connected component of~$G$.
30 \endlist
31
32 \para
33 We have already encountered a~special case of dynamic connectivity when implementing the
34 Kruskal's algorithm in Section \ref{classalg}. At that time, we did not need to delete
35 any edges from the graph, which makes the problem substantially easier. This special
36 case is sometimes called an~\df{incremental} or \df{semidynamic} graph algorithm.
37 We mentioned the Union-Find data structure of Tarjan and van Leeuwen \cite{tarjan:setunion}
38 which can be used for that: queries on connectedness translate to \<Find>, edge
39 insertions to \<Find> followed by \<Union> if the edge connects two different
40 components. This way, a~sequence of $m$~operations starting with an~empty graph
41 on $n$~vertices is processed in time $\O(m\timesalpha(m,n))$ and this works even
42 on the Pointer Machine. Fredman and Saks \cite{fredman:cellprobe} have proven
43 a~matching lower bound in the cell-probe model which is stronger than RAM with
44 $\O(\log n)$-bit words.
45
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 before the end of this section, but making it fully dynamic will require
51 more effort, so we will review some of the existing data structures in the meantime.
52
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 to make the \<Insert> and \<Delete> operations report the list of modifications
58 of the MSF caused by the change in the graph.
59
60 Let us see what happens when we \<Insert> an~edge~$e$ to a~graph~$G$ with a~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$.
65
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$ containing 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'$.
74
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. If there is no such replacement edge, we have deleted a~bridge,
78 so the MSF has to remain disconnected.
79
80 The idea of reporting differences in the MSF indeed works very well. We can summarize
81 what we have shown by the following lemma:
82
83 \lemma
84 An~\<Insert> or \<Delete> of an~edge in~$G$ causes at most one edge addition, edge
85 removal or edge exchange in $\msf(G)$.
86
87 \paran{Semidynamic MSF}%
88 To obtain a~semidynamic MSF algorithm, we need to keep the forest in a~data structure which
89 supports search for the heaviest edge on the path connecting a~given pair
90 of vertices. This can be handled efficiently by the Link-Cut trees of Sleator and Tarjan:
91
92 \thmn{Link-Cut Trees, Sleator and Tarjan \cite{sleator:trees}}
93 There is a~data structure that represents a~forest of rooted trees on~$n$ vertices.
94 Each edge of the forest has a~weight drawn from a~totally ordered set. The structure
95 supports the following operations in time $\O(\log n)$ amortized:
96
97 XXX
98
99 \proof
100 See \cite{sleator:trees}.
101 \qed
102
103
104
105 %--------------------------------------------------------------------------------
106
107 \section{ET trees}
108
109
110 \endpart