]> mj.ucw.cz Git - saga.git/blob - opt.tex
Decision trees started.
[saga.git] / opt.tex
1 \ifx\endpart\undefined
2 \input macros.tex
3 \fi
4
5 \chapter{Approaching Optimality}
6
7 \section{Soft heaps}
8
9 Recently, Chazelle \cite{chazelle:ackermann} and Pettie \cite{pettie:ackermann}
10 have presented algorithms for the MST with worst-case time complexity
11 $\O(m\timesalpha(m,n))$ on the Pointer machine. We will devote this chapter to their results
12 and especially to another algorithm by Pettie and Ramachandran \cite{pettie:optimal},
13 which is provably optimal.
14
15 At the very heart of all these algorithms lies the \df{soft heap} discovered by
16 Chazelle \cite{chazelle:softheap}. It is a~meldable priority queue, roughly
17 similar to the Vuillemin's binomial heaps \cite{vuillemin:binheap} or Fredman's
18 and Tarjan's Fibonacci heaps \cite{ft:fibonacci}. The soft heaps run faster at
19 the expense of \df{corrupting} a~fraction of the inserted elements by raising
20 their values (the values are however never lowered). This allows for
21 an~trade-off between accuracy and speed controlled by a~parameter~$\varepsilon$.
22 The heap operations take $\O(\log(1/\varepsilon))$ amortized time and at every
23 moment at most~$\varepsilon n$ elements of the~$n$ elements inserted can be
24 corrupted.
25
26 \defnn{Soft heap operations}%
27 The soft heap contains a~set of distinct items from a~totally ordered universe and it
28 supports the following operations:
29 \itemize\ibull
30 \:$\<Create>(\varepsilon)$ --- create an~empty soft heap with the given accuracy parameter~$\varepsilon$,
31 \:$\<Insert>(H,x)$ --- insert a~new item~$x$ to the heap~$H$,
32 \:$\<Meld>(P,Q)$ --- merge two heaps into one, more precisely move all items of a~heap~$Q$
33   to the heap~$P$, destroying~$Q$ in the process (both heaps must have the same~$\varepsilon$),
34 \:$\<DeleteMin>(H)$ --- delete the minimum item of the heap~$H$ and return its value
35   (optionally signalling that the value has been corrupted).
36 \:$\<Explode>(H)$ --- destroy the heap and return a~list of all items contained in it
37   (again optionally marking those corrupted).
38 \endlist
39
40 \examplen{Linear-time selection}
41 We can use soft heaps to select the median (or generally the $k$-th smallest element)
42 of a~sequence. We insert all $n$~elements to a~soft heap with error rate $\varepsilon=1/3$.
43 Then we delete the minimum about $n/3$ times and we remember the current (possibly corrupted)
44 value~$x$ of the last element deleted. This~$x$ is greater or equal than the current
45 values of the previously deleted elements and thus also than their correct values.
46 On the other hand, the current values of the $2n/3$ elements remaining in the heap
47 are greater than~$x$ and at most $n/3$ of them are corrupted. Therefore at least $n/3$
48 elements are greater than~$x$ and at least $n/3$ ones are smaller. Depending on the value
49 of~$k$, we can always throw away one of these parts and recurse on the rest (similarly to
50 the Hoare's Quickselect algorithm \cite{hoare:qselect}). The total time complexity will
51 be $\O(n+(2/3)\cdot n+(2/3)^2\cdot n+\ldots) = \O(n)$. We have obtained a~nice alternative to the standard
52 linear-time selection algorithm by Blum \cite{blum:selection}. The same trick can be used
53 to select a~good pivot in Quicksort \cite{hoare:qsort}, leading to time complexity $\O(n\log n)$
54 in the worst case.
55
56 \defnn{Soft queues}
57 The soft heap is built from \df{soft queues} (we will usually omit the adjective soft
58 in the rest of this section). Each queue has a~shape of a~binary tree.\foot{%
59 Actually, Chazelle defines the queues as binomial trees, but he transforms them in ways that are
60 somewhat counter-intuitive, albeit well-defined. We prefer describing the queues as binary
61 trees with a~special distribution of values. In fact, the original C~code in the Chazelle's
62 paper \cite{chazelle:softheap} uses this representation internally.}
63 Each vertex~$v$ of the tree remembers a~doubly-linked list of items. The
64 item list in every left son will be used only temporarily and it will be kept
65 empty between operations. Only right sons and the root have their lists
66 permanently occupied. The left sons will be called \df{white}, the right ones
67 \df{black.} (See the picture.)
68
69 The first value in every list is called the \df{controlling key} of the vertex,
70 denoted by $\<ckey>(v)$. If the list is empty, we keep the most recently used
71 value or we set $\<ckey>(v)=+\infty$. The \<ckey> obey the standard \df{heap order}
72 --- a~\<ckey> of a~parent is always smaller than the \<ckey>'s of its sons.
73
74 Each vertex is also assigned its \df{rank,} which is a~non-negative integer.
75 The ranks of leaves are always zero, the rank of every internal vertex can be
76 arbitrary, but it must be strictly greater than the ranks of its sons. We
77 define the rank of the whole queue to be equal to the rank of its root vertex and
78 similarly for its \<ckey>.
79
80 A~queue is called \df{complete} if every two vertices joined by an~edge have rank
81 difference exactly one. In other words, it is a~complete binary tree and the
82 ranks correspond to vertex heights.
83
84 \figure{softheap1.eps}{\epsfxsize}{\multicap{A~complete and a~partial soft queue tree\\
85 (black vertices contain items, numbers indicate ranks)}}
86
87 \obs
88 The complete queue of rank~$k$ contains exactly~$2^{k+1}-1$ vertices, $2^k$~of which are
89 black (by induction). Any other queue can be trivially embedded to the complete queue of the same
90 rank, which we will call the \df{master tree} of the queue. This embedding preserves vertex
91 ranks, colors and the ancestor relation.
92
93 The queues have a~nice recursive structure. We can construct a~queue of rank~$k$ by \df{joining}
94 two queues of rank~$k-1$ under a~new root. The root will inherit the item list of one
95 of the original roots and also its \<ckey>. To preserve the heap order, we will choose
96 the one whose \<ckey> is smaller.
97
98 Sometimes, we will also need to split a~queue to smaller queues. We will call this operation
99 \df{dismantling} the queue and it will happen only in cases when the item list in the root
100 is empty. It suffices to remove the leftmost (all white) path going from the root. From
101 a~queue of rank~$k$, we get queues of ranks $0,1,\ldots,k-1$, some of which may be missing
102 if the original queue was not complete.
103
104 \figure{softheap2.eps}{\epsfxsize}{Joining and dismantling of soft queues}
105
106 We will now define the real soft heap and the operations on it.
107
108 \defn A~\df{soft heap} consists of:
109 \itemize\ibull
110 \:a~doubly linked list of soft queues of distinct ranks (in increasing order of ranks),
111   we will call the first queue the \df{head} of the list, the last queue will be its \df{tail};
112 \:\df{suffix minima:} each queue contains a~pointer to the queue with minimum \<ckey>
113 of those following it in the list;
114 \:a~global parameter~$r$: an~even integer to be set depending on~$\varepsilon$.
115 \endlist
116
117 \>We will define the \df{rank} of a~heap as the highest of the ranks of its queues
118 (that is, the rank of the heap's tail).
119
120 The heap always keeps the \df{Rank invariant:} When a~root of any tree has rank~$k$,
121 its leftmost path contains at least~$k/2$ vertices.
122
123 \paran{Operations on soft heaps}
124
125 \em{Melding} of two soft heaps involves merging of their lists of queues. We disassemble
126 the heap of the smaller rank and we insert its queues to the other heap.
127 If two queues of the same rank~$k$ appear in both lists, we \em{join} them to
128 a~single queue of rank~$k+1$ as already described and we propagate the new queue as
129 a~\df{carry} to the next iteration, similarly to addition of binary numbers.
130 Finally we have to update the suffix minima by walking the new list backwards
131 from the last inserted item.
132
133 \em{Creation} of a~new soft heap is trivial, \em{insertion} is handled by creating
134 a~single-element heap and melding it to the destination heap.
135
136 \algn{Creating a~new soft heap}
137 \algo
138 \algin The~parameter~$\varepsilon$ (the accuracy of the heap).
139 \:Allocate memory for a~new heap structure~$H$.
140 \:Initialize the list of queues in~$H$ to an~empty list.
141 \:Set the parameter~$r$ to~$2\lceil\log(1/\varepsilon)\rceil+2$ (to be justified later).
142 \algout A~newly minted soft heap~$H$.
143 \endalgo
144
145 \algn{Melding of two soft heaps}
146 \algo
147 \algin Two soft heaps~$P$ and~$Q$.
148 \:If $\<rank>(P) < \<rank>(Q)$, exchange the item lists of~$P$ and~$Q$.
149 \:$p\=\<head>(P)$.
150 \brk\cmt{Whenever we run into an~end of a~list in this procedure, we assume that
151 there is an~empty queue of infinite rank there.}
152 \:While $Q$ still has some queues:
153 \::$q\=\<head>(Q)$.
154 \::If $\<rank>(p) < \<rank>(q)$, then $p\=$ the successor of~$p$,
155 \::else if $\<rank>(p) > \<rank>(q)$, remove~$q$ from~$Q$ and insert it to~$P$ before~$p$,
156 \::otherwise (the ranks are equal, we need to propagate the carry):
157 \:::$\<carry>\=p$.
158 \:::Remove~$p$ from~$P$ and set $p\=$ the original successor of~$p$.
159 \:::While $\<rank>(q)=\<rank>(\<carry>)$:
160 \::::Remove~$q$ from~$Q$.
161 \::::$\<carry>\=\<join>(q, \<carry>)$.
162 \::::$q\=\<head>(Q)$.
163 \:::Insert~\<carry> before~$q$.
164 \:Update the suffix minima: Walk with~$p$ backwards to the head of~$P$:
165 \::$p'\=\<suffix\_min>$ of the successor of~$p$.
166 \::If $\<ckey>(p) < \<ckey>(p')$, set $\<suffix\_min>(p)\=p$.
167 \::Otherwise set $\<suffix\_min>(p)\=p'$.
168 \:Destroy the heap~$Q$.
169 \algout The merged heap~$P$.
170 \endalgo
171
172 \algn{Insertion of an~element to a~soft heap}
173 \algo
174 \algin A~heap~$H$ and a~new element~$x$.
175 \:Create a~new heap~$H'$ of the same parameters as~$H$. Let~$H'$ contain a~sole queue of rank~0,
176   whose only vertex has the element~$x$ in its item list.
177 \:$\<Meld>(H,H')$.
178 \algout An~updated heap~$H$.
179 \endalgo
180
181 \para
182 So far, the mechanics of the soft heaps were almost identical to the binomial heaps
183 and the reader could rightfully yawn. The things are going to get interesting
184 now as we approach the parts where corruption of items takes place.
185
186 If all item lists contain at most one item equal to the \<ckey> of the particular
187 vertex, no information is lost and the heap order guarantees that the minimum item
188 of every queue stays in its root. We can however allow longer lists and let the items
189 stored in a~single list travel together between the vertices of the tree, still
190 represented by a~common \<ckey>. This data-structural analogue of car pooling will
191 allow the items to travel at a~faster rate, but as only a~single item can be equal
192 to the \<ckey>, all other items will be inevitably corrupted.
193
194 We of course have to be careful about the size of the lists, because we must avoid corrupting
195 too many items. We will control the growth according to the vertex ranks. Vertices
196 with rank at most~$r$ will always contain just a~single item. Above this level,
197 the higher is the rank, the longer list will be allowed.
198
199 \para
200 \em{Deletion of minimum} will be based on this principle. The minimum is easy to locate
201 --- we follow the \<suffix\_min> of the head of the heap to the queue with the minimum
202 \<ckey>. There we look inside the item list of the root of the queue. We remove the \em{last}
203 item from the list (we do not want the \<ckey> to change) and we return it as the minimum.
204 (It is not necessarily the real minimum of all items, but always the minimum of their
205 possibly corrupted values.)
206
207 If the list becomes empty, we \em{refill} it with items from the lower levels of the
208 same queue. This process can be best described recursively: We ask the left son to refill itself
209 (remember that the left son is always white, so there are currently no items there). If the new
210 \<ckey> of the left son is smaller than of the right son, we immediately move the left
211 son's list to its parent. Otherwise, we exchange the sons and move the list from the
212 new left son to the parent. This way we obey the heap order and at the same time we keep
213 the white left son free of items.
214
215 Ocassionally, we repeat this process once again and we concatenate the resulting lists
216 (we append the latter list to the former, using the smaller of the two \<ckey>'s). This
217 makes the lists grow longer and we want to do that roughly on every other level of the
218 tree. The exact condition will be that either the rank of the current vertex is odd,
219 or the difference in ranks between this vertex and its right son is at least two.
220
221 If refilling of the left son fails because there are no more items in that subtree
222 (we report this by setting its \<ckey> to $+\infty$), the current vertex is no longer
223 needed --- the items would just pass through it unmodified. We therefore want to
224 remove it. Instead of deleting it directly, we rather make it point to its former
225 grandsons and we remove the (now orhpaned) original son. This helps us to ensure
226 that both sons always keep the same rank, which will be useful for the analysis.
227
228 When all refilling is done, we update the suffix minima by walking from the current
229 queue to the head of the heap exactly as we did in the \<Meld> procedure.
230
231 Our only remaining worry is that the Rank invariant can be broken after the
232 refilling. When the leftmost path of the tree becomes too short, we just
233 \em{dismantle} the tree as already described and we meld the new trees back to
234 the heap. This is easier to handle when the item list at the root vertex is
235 empty. We will therefore move this check before the refilling of the root list.
236 It will turn out that we have enough time to always walk the leftmost path
237 completely, so no explicit counters are needed.
238
239 Let us translate these ideas to real (pseudo)code:
240
241 \algn{Deleting the minimum item from a~soft heap}
242 \algo
243 \algin A~soft heap~$H$.
244 \:Use \<suffix\_min> of the head queue of~$H$ to locate the queue~$q$ with the smallest \<ckey>.
245 \:Remove the final element~$x$ of the item list in the root of~$q$.
246 \:If the item list is empty:
247 \::Count the vertices on the leftmost path of~$q$.
248 \::If there are less than $\<rank>(q)$ of them:
249 \:::Remove~$q$ from the list of queues.
250 \:::Recalculate the suffix minima as in the \<Meld> procedure.
251 \:::Dismantle~$q$ and create a~heap~$H'$ holding the resulting trees.
252 \:::Meld them back: $\<Meld>(H,H')$.
253 \::Otherwise:
254 \:::Call \<Refill> on the root of~$q$.
255 \:::If $\<ckey>(q)=+\infty$ (no items left), remove the tree~$q$ from~$H$.
256 \:::Recalculate the suffix minima.
257 \algout The deleted minimum item~$x$ (possibly corrupted).
258 \endalgo
259
260 \algn{Refilling the item list of a~vertex}
261 \algo
262 \algin A~soft queue and its vertex~$v$ with an~empty item list.
263 \:Handle trivial cases: If~$v$ has no children or both have $\<ckey>=+\infty$,
264   set $\<ckey>(v)$ to~$+\infty$ and return.
265 \:Let \<left> and~\<right> denote the respective sons of~$v$.
266 \:Recurse: call $\<Refill>(\<left>)$.
267 \:If $\<ckey>(\<left>) > \<ckey>(\<right>)$, swap the sons.
268 \:Move the item list from \<left> to~$v$ (implying $\<ckey>(v)=\<ckey>(\<left>)$).
269 \:If $\<rank>(v) > r$ and either $\<rank>(v)$ is odd or $\<rank>(v) > \<rank>(\<right>)+1$, recurse once more:
270 \::Repeat steps 3--4.
271 \::Append the item list from \<left> to the item list at~$v$.
272 \:Clean up. If $\<ckey>(\<right>) = +\infty$:
273 \::If $\<ckey>(\<left>) = +\infty$, unlink and discard both sons.
274 \::Otherwise relink the sons of \<left> to~$v$ and discard \<left>.
275 \algout A~modified soft queue.
276 \endalgo
277
278 \para
279 \<Explode> is trivial once we know how to recognize the corrupted items. It simply examines
280 all queues in the heap, walks the trees and the item lists of all vertices. It records
281 all items seen, the corrupted ones are those that different from their \<ckey>.
282
283 \paran{Analysis of accuracy}
284 The description of the operations is now complete, so let us analyse their behavior
285 and verify that we have delivered what we promised --- first the accuracy of
286 the structure, then the time complexity of operations. In the whole analysis,
287 we will denote the total number of elements inserted during the history of the
288 structure by~$n$. We will also frequently take advantage of knowing that the
289 threshold~$r$ is even.
290
291 We start by bounding the sizes of the item lists.
292
293 \lemma
294 For every vertex~$v$ of a~soft queue, the size $\ell(v)$ of its item list
295 satisfies:
296 $$\ell(v) \le \max(1, 2^{\lceil \<rank>(v)/2 \rceil - r/2}).$$
297
298 \proof
299 Initially, all item lists contain at most one item, so the ineqality trivially
300 holds. Let us continue by induction. Melds can affect it only in the favorable
301 direction (they ocassionally move an~item list to a~vertex of a~higher rank)
302 and so do deletes (they only remove items from lists). The only potentially
303 dangerous place is the \<Refill> procedure.
304
305 Refilling sometimes just moves items upwards, which is safe, and sometimes it
306 joins two lists into one, which generally is not. When $\<rank>(v) \le r$,
307 no joining takes place and~$\ell(v)$ is still~1. Otherwise we join when either
308 $\<rank>(v)$ is odd or $\<rank>(w) < \<rank>(v)-1$ for any son~$w$ of~$v$ (remember
309 that both sons have the same rank). In both cases, $\lceil\<rank>(w)/2\rceil \le
310 \lceil\<rank>(v)/2\rceil - 1$. By the induction hypothesis, the size of each
311 of the two joined lists is at most $2^{\max(1,\lceil\<rank>(v)/2\rceil - 1 - r/2)}$,
312 so the new list has at most $2^{\lceil\<rank>(v)/2\rceil - r/2}$ items. (The maximum
313 has disappeared since $\<rank>(v)>r$ and therefore the desired bound is at least~2.)
314 \qed
315
316 We will now sum the sizes of the lists over all vertices containing corrupted items.
317
318 \lemma\id{shcorrlemma}%
319 At any given time, the heap contains at most~$n/2^{r-2}$ corrupted items.
320
321 \proof
322 We first prove an~auxiliary claim: The master trees of all queues contain at most~$n$
323 black vertices. This follows by induction: If no deletions have taken place,
324 there are exactly~$n$ black vertices, because insertion adds one black vertex and
325 melding preserves their number. A~deletion affects the master trees only when
326 dismantling takes place and then it only removes a~black vertex.
327
328 An~obvious upper bound on the number of corrupted items is the total size of item
329 lists in all vertices of rank greater than~$r$. We already know from the previous
330 lemma that the list sizes are limited by a~function of the ranks. A~complete tree
331 is obviously the worst case, so we will prove that this lemma holds for the master
332 tree of every queue in the heap. The actual trees can be much sparser, but the
333 above claim guarantees that the total size of the master trees is bounded by the
334 number of insertions properly.
335
336 So let us consider a~complete tree of~rank~$k$. It has exactly $2^{k-i}$ vertices
337 of rank~$i$ and each such vertex contains a~list of at most~$2^{\lceil i/2\rceil - r/2}$
338 items by the previous lemma. Summing over all ranks greater than~$r$, we get that
339 the total number of corrupted items in this tree is at most:
340 $$
341 \sum_{i=r+1}^k 2^{k-i}\cdot 2^{\lceil i/2\rceil - r/2}
342 = 2^{k-r/2} \cdot \sum_{i=r+1}^k 2^{\lceil i/2\rceil - i}
343 \le 2^{k-r/2+1/2} \cdot \sum_{i=r+1}^k 2^{-i/2}
344 \le 2^{k-r} \cdot \sum_{i=0}^\infty 2^{-i/2}.
345 $$
346 The sum of a~geometric series with quotient $2^{-1/2}$ is less than four, so the
347 last expression is less than $2^{k-r+2}$. Since the tree contains $n_k=2^k$ black vertices,
348 this makes less than $n_k/2^{r-2}$ corrupted items as we asserted.
349 \qed
350
351 \paran{Analysis of time complexity}
352 Now we will examine the amortized time complexity of the individual operations.
353 We will show that if we charge $\O(r)$ time against every element inserted, it is enough
354 to cover the cost of all other operations.
355
356 All heap operations use only pointer operations, so it will be easy to derive the time
357 bound in the Pointer machine model. The notable exception is however that the procedures
358 often refer to the ranks, which are integers on the order of $\log n$, so they cannot
359 fit in a~single memory cell. For the time being, we will assume that the ranks can
360 be manipulated in constant time, postponing the proof for later.
361
362 We take a~look at the melds first.
363
364 \lemma\id{shmeld}%
365 The amortized cost of a~meld is $\O(1)$, except for melds induced by dismantling
366 which take $\O(\<rank>(q))$, where $q$~is the queue to be dismantled.
367
368 \proof
369 The real cost of a~meld of heaps $P$ and~$Q$ is linear in the smaller of
370 their ranks, plus the time spent on carry propagation. The latter is easy to
371 dispose of: Every time there is a~carry, the total number of trees in all
372 heaps decreases by one. So it suffices to charge $\O(1)$ against creation of
373 a~tree. An~insert creates one tree, dismantling creates at most $\<rank>(q)$
374 trees, and all other operations alter only the internal structure of trees.
375
376 As for the $\O(\min(\<rank>(P),\<rank>(Q)))$ part, let us assume for a~while that
377 no dismantling ever takes place and consider the \df{meld forest.} It is a~forest
378 whose leaves correspond to the $n$~single-element heaps constructed by \<Insert>
379 and each internal vertex represents a~heap arisen from melding its sons. The left
380 son will be the one with the greater or equal rank. We therefore want to bound
381 the sum of ranks of all right sons.
382
383 For every right son, we will distribute the change for its rank~$k$ among all leaves
384 in its subtree. There are at least $2^k$ such leaves. No leaf ever receives the same
385 rank twice, because the ranks of right sons on every path from the root of the
386 tree to a~leaf are strictly decreasing. (This holds because melding of two heaps
387 always produces a~heap of a~strictly greater rank.) Hence at most~$n/2^k$
388 right sons have rank~$k$ and the total time charged against the leaves is bounded by:
389 $$
390 \sum_{k=0}^{\rm max. rank}k\cdot {n\over 2^k} \le n\cdot\sum_{k=0}^\infty {k\over 2^k} = 2n.
391 $$
392
393 Let us return dismantling to the game. When a~queue is dismantled, melding the parts
394 back to the heap takes $\O(\<rank>(q))$ time. We can therefore let the dismantling pay for it
395 and omit such induced melds from the meld forest. As the rank of a~heap is never increased
396 by induced melds, the above calculation is still a~proper upper bound on the cost
397 of the regular melds.
398 \qed
399
400 Before we estimate the time spent on deletions, we analyse the refills.
401
402 \lemma
403 Every invokation of the \<Refill> procedure takes time $\O(1)$ amortized.
404
405 \proof
406 When \<Refill> is called from the \<DeleteMin> operation, it recurses on a~subtree of the
407 queue. This subtree can be split to the ``lossless'' lower part (rank~$r$ and below)
408 and the upper part where list concatenation and thus also corruption takes place. Whenever
409 we visit the lower part during the recursion, we spend at worst $\O(r)$ time there.
410 We will prove that the total time spent in the upper parts during the whole life of the
411 data structure is $\O(n)$. Since each upper vertex can perform at most two calls to the lower
412 part, the total time spent in the lower parts is $\O(rn)$. All this can be prepaid by the
413 inserts.
414
415 Let us focus on the upper part. There are three possibilities of what can happen
416 when we visit a~vertex:
417
418 \itemize\ibull
419
420 \:We delete it: Every vertex deleted has to have been created at some time in the past.
421 New vertices are created only during inserts and melds (when joining two trees) and
422 we have already shown that these operations have constant amortized complexity. Then the
423 same must hold for deletions.
424
425 \:We recurse twice and concatenate the lists: The lists are disassembled only when
426 they reach the root of the tree, otherwise they are only concatenated. We can easily
427 model the situation by a~binary tree forest similar to the meld forest. There are~$n$
428 leaves and every internal vertex has outdegree two, so the total number of concatenations
429 is at most~$n$. Each of them can be performed in constant time as the list is doubly linked.
430
431 \:We recurse only once: This occurs only if the rank is even and the gap between the
432 rank of this vertex and its sons is equal to~1. It therefore cannot happen twice in a~row,
433 thus it is clearly dominated by the cost of the other possibilities.
434
435 \endlist
436 \>The total cost of all steps in the upper part is therefore $\O(n)$.
437 \qed
438
439 We now proceed with examining the \<DeleteMin> operation.
440
441 \lemma\id{shdelmin}%
442 Every \<DeleteMin> takes $\O(1)$ time amortized.
443
444 \proof
445 Aside from refilling, which is $\O(1)$ by the previous lemma, the \<DeleteMin>
446 takes care of the Rank invariant. This happens by checking the length of the leftmost
447 path and dismantling the tree if the length is too far from the tree's rank~$k$.
448 When the invariant is satisfied, the leftmost path is visited by the subsequent
449 call to \<Refill>, so we can account the check on the refilling.
450
451 When we are dismantling, we have to pay $\O(k)$ for the operation itself and
452 another $\O(k)$ for melding the trees back to the heap. Since we have produced at most
453 $k/2$ subtrees of distinct ranks, some subtree of rank $k/2$ or more must be missing.
454 Its master tree contained at least $2^{k/2}$ vertices which are now permanently gone
455 from the data structure, so we can charge the cost against them.
456 A~single vertex can participate in the master trees of several dismantlings, but their
457 ranks are always strictly increasing. By the same argument as in the proof of
458 Lemma \ref{shmeld} (complexity of \<Meld>), each vertex pays $\O(1)$.
459
460 We must not forget that \<DeleteMin> also has to recalculate the suffix minima.
461 In the worst case, it requires touching $k$~trees. Because of the Rank invariant,
462 this is linear in the size of the
463 leftmost path and therefore it can be also paid for by \<Refill>. (Incidentally,
464 this was the only place where we needed the invariant.)
465 \qed
466
467 \<Explode>s are easy not only to implement, but also to analyse:
468
469 \lemma
470 Every \<Explode> takes $\O(1)$ time amortized.
471
472 \proof
473 As all queues, vertices and items examined by \<Explode> are forever gone from the heap,
474 we can charge the constant time spent on each of them against the operations
475 that have created them.
476 \qed
477
478 It remains to take care of the calculation with ranks:
479
480 \lemma\id{shyards}%
481 Every manipulation with ranks performed by the soft heap operations can be
482 implemented on the Pointer machine in constant amortized time.
483
484 \proof
485 We create a~``yardstick'' --- a~double linked list whose elements represent the possible
486 values of a~rank. Every vertex of a~queue will store its rank as a~pointer to
487 the corresponding ``tick'' of the yardstick. We will extend the list as necessary.
488
489 Comparison of two ranks for equality is then trivial, as is incrementing or decrementing
490 the rank by~1. Testing whether a~rank is odd can be handled by storing an~odd/even
491 flag in every tick. This covers all uses of ranks except for the comparisons for inequality
492 when melding. In step~1 of \<Meld>, we just mark the ticks of the two ranks and walk
493 the yardstick from the beginning until we come across a~mark. Thus we compare the ranks
494 in time proportional to the smaller of them, which is the real cost of the meld anyway.
495 The comparisons in steps 5 and~6 are trickier, but since the ranks of the elements put
496 to~$P$ are strictly increasing, we can start walking the list at the rank of the previous
497 element in~$P$. The cost is then the difference between the current and the previous rank
498 and their sum telescopes, again to the real cost of the meld.
499 \qed
500
501 Now we can put the bits together and laurel our effort with the following theorem:
502
503 \thmn{Performance of soft heaps, Chazelle \cite{chazelle:softheap}}\id{softheap}%
504 A~soft heap with error rate~$\varepsilon$ ($0<\varepsilon\le 1/2$) processes
505 a~sequence of operations starting with an~empty heap and containing $n$~\<Insert>s
506 in time $\O(n\log(1/\varepsilon))$ on the Pointer machine. At every moment, the
507 heap contains at most $\varepsilon n$ corrupted items.
508
509 \proof
510 We set the parameter~$r$ to~$2+2\lceil\log (1/\varepsilon)\rceil$. The rest follows
511 from the analysis above. By Lemma \ref{shcorrlemma}, there are always at most $n/2^{r-2}
512 \le \varepsilon n$ corrupted items in the heap. By Lemma \ref{shmeld}--\ref{shyards},
513 the time spent on all operations in the sequence can be paid for by charging $\O(r)$ time
514 against each \<Insert>. This yields the time bound.
515 \qed
516
517 \rem
518 When we set $\varepsilon = 1/2n$, the soft heap is not allowed to corrupt any
519 items, so it can be used like any usual heap. By the standard lower bound on
520 sorting it therefore requires $\Omega(\log n)$ time per operation, so the
521 time complexity is optimal for this choice of~$\varepsilon$. Chazelle \cite{chazelle:softheap}
522 proves that it is optimal for every choice of~$\varepsilon$.
523
524 The space consumed by the heap need not be linear in the \em{current} number
525 of items, but if a~case where this matters ever occured, we could fix it easily
526 by rebuilding the whole data structure completely after $n/2$ deletes. This
527 increases the number of potentially corrupted items, but at worst twice, so it
528 suffices to decrease~$\varepsilon$ twice.
529
530 %--------------------------------------------------------------------------------
531
532 \section{Robust contractions}
533
534 Having the soft heaps at hand, we would like to use them in a~conventional MST
535 algorithm in place of a~usual heap. The most efficient specimen of a~heap-based
536 algorithm we have seen so far is the Iterated Jarn\'\i{}k's algorithm (\ref{itjar}).
537 It is based on a~simple, yet powerful idea: Run the Jarn\'\i{}k's algorithm with
538 limited heap size, so that it stops when the neighborhood of the tree becomes
539 large. Grow multiple such trees, always starting in a~vertex not visited yet. All
540 these trees are contained in the MST, so by the Contraction lemma
541 (\ref{contlemma}) we can contract each of them to a~single vertex and iterate
542 the algorithm on the resulting graph.
543
544 We can try implanting the soft heap in this algorithm, preferably in the original
545 version without active edges (\ref{jarnik}) as the soft heap lacks the \<Decrease>
546 operation. This honest, but somewhat simple-minded attempt is however doomed to
547 fail. The reason is of course the corruption of items inside the heap, which
548 leads to increase of weights of a~subset of edges. In presence of corrupted
549 edges, most of the theory we have so carefully built breaks down. For example,
550 the Blue lemma (\ref{bluelemma}) now holds only when we consider a~cut with no
551 corrupted edges, with a~possible exception of the lightest edge of the cut.
552 Similarly, the Red lemma (\ref{redlemma}) is true only if the heaviest edge on
553 the cycle is not corrupted.
554
555 There is fortunately some light in this darkness. While the basic structural
556 properties of MST's no longer hold, there is a~weaker form of the Contraction
557 lemma that takes the corrupted edges into account. Before we prove this lemma,
558 we will expand our awareness of subgraphs which can be contracted.
559
560 \defn
561 A~subgraph $C\subseteq G$ is \df{contractible} iff for every pair of edges $e,f\in\delta(C)$\foot{That is,
562 edges with exactly one endpoint in~$C$.} there exists a~path in~$C$ connecting the endpoints
563 of the edges $e,f$ such that all edges on this path are lighter than either $e$ or~$f$.
564
565 \example\id{jarniscont}%
566 Let us see that when we stop the Jarn\'\i{}k's algorithm at some moment and
567 we take a~subgraph~$C$ induced by the constructed tree, this subgraph is contractible.
568 Indeed, when we consider any two distinct edges $uv, xy$ ($u,x\in C$ and $v,y\not\in C$),
569 they enter the algorithm's heap at some time. Without loss of generality $uv$~is the first.
570 Before the algorithm reaches the vertex~$x$, it adds the whole path $ux$ to the tree.
571 As the edge~$uv$ never leaves the heap, all edges on the path $ux$ must be lighter
572 than this edge.
573
574 We can now easily reformulate the Contraction lemma (\ref{contlemma}) in the language
575 of contractible subgraphs. We again assume that we are working with multigraphs
576 and that they need not be connected.
577 Furthermore, we slightly abuse the notation and we omit the explicit bijection
578 between $G-C$ and~$G/C$, so we can write $G=C \cup (G/C)$.
579
580 \lemman{Generalized contraction}
581 When~$C\subseteq G$ is a~contractible subgraph, then $\msf(G)=\msf(C) \cup \msf(G/C)$.
582
583 \proof
584 As both sides of the equality are forests spanning the same graph, it suffices
585 to show that $\msf(G) \subseteq \msf(C)\cup\msf(G/C)$. We know that the edges which
586 does not participate in the MSF are exactly those which are the heaviest on some cycle
587 (this is the Cycle rule \ref{cyclerule}).
588
589 Whenever an~edge~$g$ lies in~$C$, but not in~$\msf(C)$, then $g$~is the heaviest edge
590 on some cycle in~$C$. As this cycle is also contained in~$G$, the edge $g$~does not participate
591 in $\msf(G)$ either.
592
593 Similarly for $g\in (G/C)\setminus\msf(G/C)$: when the cycle does not contain
594 the vertex~$c$ to which we contracted the subgraph~$C$, the cycle is present
595 in~$G$, too. Otherwise we consider the edges $e,f$ adjacent to~$c$ on this cycle.
596 Since $C$~is contractible, there must exist a~path~$P$ in~$C$ connecting the endpoints
597 of~$e$ and~$f$ in~$G$, such that all edges of~$P$ are lighter than either $e$ or~$f$
598 and hence also than~$g$. Expanding~$c$ in the cycle to the path~$P$ then produces
599 a~cycle in~$G$ whose heaviest edge is~$g$.
600 \qed
601
602 We are ready to bring corruption back to the game now and state a~``robust'' version
603 of this lemma. A~notation for corrupted graphs will be handy:
604
605 \nota\id{corrnota}%
606 When~$G$ is a~weighted graph and~$R$ a~subset of its edges, we will use $G\crpt
607 R$ to denote an arbitrary graph obtained from~$G$ by increasing the weights of
608 some of the edges in~$R$. As usually, we will assume that all edge weights in this
609 graph are pairwise distinct. While this is technically not true for the corruption
610 caused by soft heaps, we can easily make the weights unique.
611
612 If~$C$ is a~subgraph of~$G$, we will refer to the edges of~$R$ whose exactly
613 one endpoint lies in~$C$ by~$R^C$ (i.e., $R^C = R\cap \delta(C)$).
614
615 \lemman{Robust contraction}\id{robcont}%
616 Let $G$ be a~weighted graph and $C$~its subgraph contractible in~$G\crpt R$
617 for some set of edges~$R$. Then $\msf(G) \subseteq \msf(C) \cup \msf((G/C) \setminus R^C) \cup R^C$.
618
619 \proof
620 We will modify the proof of the previous lemma. We will again consider all possible types
621 of edges which do not belong to the right-hand side and show that they are the
622 heaviest edges of certain cycles. Every edge~$g$ of~$G$ lies either in~$C$, or in $H=(G/C)\setminus R^C$,
623 or possibly in~$R^C$.
624
625 If $g\in C\setminus\msf(C)$, then the same argument as before applies.
626
627 If $g\in H\setminus\msf(H)$, we consider the cycle in~$H$ on which $e$~is the heaviest.
628 When $c$ (the vertex to which we have contracted~$C$) is outside this cycle, we are finished.
629 Otherwise we observe that the edges $e,f$ adjacent to~$c$ on this cycle cannot be corrupted
630 (they would be in~$R^C$ otherwise, which is impossible). By contractibility of~$C$ there exists
631 a~path~$P$ in~$(G\crpt R)\cup C$ such that all edges of~$P$ are lighter than $e$ or~$f$ and hence
632 also than~$g$. The weights of the edges of~$P$ in the original graph~$G$ cannot be higher than
633 in~$G\crpt R$, so the path~$P$ is lighter than~$g$ even in~$G$ and we can again perform the
634 trick with expanding the vertex~$c$ to~$P$ in the cycle~$C$. Hence $g\not\in\mst(G)$.
635 \qed
636
637 \para
638 Our plan still is to mimic the Iterative Jarn\'\i{}k's algorithm. We will grow a~collection~$\C$
639 of non-overlapping contractible subgraphs and put aside the edges which got corrupted in the process.
640 We recursively compute the MSF of the subgraphs and of the contracted graph. Then we take the
641 union of these MSF's and add the corrupted edges. According to the previous lemma, this does not produce
642 the final MSF, but a~sparser graph containing it, on which we can continue.
643
644 We can formulate the exact partitioning algorithm as follows:
645
646 \algn{Partition a~graph to a~collection of contractible subgraphs}\id{partition}%
647 \algo
648 \algin A~graph~$G$ with an~edge comparison oracle, a~parameter~$t$ controlling the size of the subgraphs,
649   and an~accuracy parameter~$\varepsilon$.
650 \:Mark all vertices as ``live''.
651 \:$\C\=\emptyset$, $R^\C\=\emptyset$. \cmt{Start with an~empty collection and no corrupted edges.}
652 \:While there is a~live vertex~$v_0$:
653 \::$T=\{v_0\}$. \cmt{the tree we currently grow}
654 \::$K=\emptyset$. \cmt{edges known to be corrupted}
655 \::\<Create> a~soft heap with accuracy~$\varepsilon$ and \<Insert> the edges adjacent to~$v_0$ in it.
656 \::While the heap is not empty and $\vert T\vert \le t$:
657 \:::\<DeleteMin> an~edge $uv$ from the heap, assume $u\in T$.
658 \:::If $uv$ was corrupted, add it to~$K$.
659 \:::If $v\in T$, drop the edge and repeat the previous two steps.
660 \:::$T\=T\cup\{v\}$.
661 \:::If $v$ is dead, break out of the current loop.
662 \:::Insert all edges incident with~$v$ to the heap.
663 \::$\C\=\C \cup \{G[T]\}$. \cmt{Record the subgraph induced by the tree.}
664 \::\<Explode> the heap and add all remaining corrupted edges to~$K$.
665 \::$R^\C\=R^\C \cup \{ K^T \}$. \cmt{Record the ``interesting'' corrupted edges.}
666 \::$G\=G\setminus K^T$. \cmt{Remove the corrupted edges from~$G$.}
667 \::Mark all vertices of~$T$ as ``dead''.
668 \algout The collection $\C$ of contractible subgraphs and the set~$R^\C$ of
669 corrupted edges in the neighborhood of these subgraphs.
670 \endalgo
671
672 \thmn{Partitioning to contractible subgraphs, Pettie \cite{pettie:optimal}}\id{partthm}%
673 Given a~weighted graph~$G$ and parameters $\varepsilon$ ($0<\varepsilon\le 1/2$)
674 and~$t$, the Partition algorithm \ref{partition} constructs a~collection
675 $\C=\{C_1,\ldots,C_k\}$ of subgraphs and a~set~$R^\C$ of corrupted edges such that:
676
677 \numlist\ndotted
678 \:All the $C_i$'s and the set~$R^\C$ are edge-disjoint.
679 \:Each $C_i$ contains at most~$t$ vertices.
680 \:Each vertex of~$G$ is contained in at least one~$C_i$.
681 \:The connected components of the union of all $C_i$'s have at least~$t$ vertices each,
682   except perhaps for those which are equal to a~connected component of~$G\setminus R^\C$.
683 \:$\vert R^\C\vert \le 2\varepsilon m$.
684 \:$\msf(G) \subseteq \bigcup_i \msf(C_i) \cup \msf\bigl((G / \bigcup_i C_i) \setminus R^\C\bigr) \cup R^\C$.
685 \:The algorithm runs in time $\O(n+m\log (1/\varepsilon))$.
686 \endlist
687
688 \proof
689 The algorithm grows a~series of trees. A~tree is built from edges adjacent to live vertices
690 and once it is finished, all vertices of the tree die, so no edge is ever reused in another
691 tree. Edges of~$R^\C$ are immediately deleted from the graph, so they never participate
692 in any tree. The algorithm stops only if all vertices are dead, so each vertex must have
693 entered some tree. Each $C_i$ is induced by some tree and the trees have at most~$t$ vertices
694 each, which limits the size of the $C_i$'s as well. This proves claims 1--3.
695
696 Claim~4: We can show that each connected component has $t$~or more vertices as we already did
697 in the proof of Lemma \ref{ijsize}: How can a~new tree stop growing? Either it already
698 has~$t$ vertices, or it joins one of the existing trees (which only increases the
699 size of the component), or the heap becomes empty (which means that the tree spans
700 a~full component of the current graph and hence also of~$G\setminus R^\C$).
701
702 Claim~5: The set~$R^\C$ contains a~subset of edges corrupted by the soft heaps over
703 the course of the algorithm. As every edge is inserted to a~heap at most once
704 in every direction, the heaps can corrupt at worst $2\varepsilon m$ edges altogether.
705
706 We will prove the remaining two claims as separate lemmata.
707 \qed
708
709 \lemman{Correctness of Partition, Claim 6 of Theorem \ref{partthm}}
710 $$\msf(G) \subseteq \bigcup_i \msf(C_i) \cup \msf\biggl( \bigl(G / \bigcup_i C_i \bigr) \setminus R^\C\biggr) \cup R^\C.$$
711
712 \proof
713 By iterating the Robust contraction lemma (\ref{robcont}). Let $K_i$ be the set of edges
714 corrupted when constructing the subgraph~$C_i$ in the $i$-th phase (iteration of the outer
715 loop) of the algorithm, and similarly for the state of the other variables at that time.
716 We will use induction on~$i$ to prove that the lemma holds at the end of every phase.
717
718 At the beginning, the statement is obviously true, even as an~equality.
719 In the $i$-th phase, we construct the subgraph~$C_j$ by running the partial Jarn\'\i{}k's algorithm on the graph
720 $G_i = G\setminus\bigcup_{j<i} K_{\smash j}^{\C_j}$.
721 If it were not for corruption, the subgraph~$C_i$ would be contractible, as we already know from Example \ref{jarniscont}.
722 When the edges in~$K_i$ get corrupted, the subgraph is contractible in some corrupted graph
723 $G_i \crpt K_i$. (We have to be careful as the edges are becoming corrupted gradually,
724 but it is easy to check that it can only improve the situation.) Since $C_i$~is edge-disjoint
725 with the previous trees, it is also a~contractible subgraph of $(G_i / \bigcup_{j<i} C_j) \crpt K_i$.
726 Now we can use the Robust contraction lemma to show that:
727 $$
728 \msf\biggl(\bigl(G / \bigcup_{i<j} \bigr) \setminus \bigcup_{i<j} K_j^{\C_j} \biggr) \subseteq
729 \msf(C_i) \cup \msf\biggl(\bigl(G / \bigcup_{i\le j} \bigr) \setminus \bigcup_{i\le j} K_j^{\C_j} \biggr) \cup K_i^{C_i},
730 $$
731 which completes the induction step.
732 \qed
733
734 \lemman{Efficiency of Partition, Claim 7 of Theorem \ref{partthm}}
735 The Partition algorithm runs in time $\O(n+m\log(1/\varepsilon))$.
736
737 \proof
738 The inner loop (steps 7--13) processes the edges of the current subgraph~$C_i$ and also
739 the edges in its neighborhood $\delta(C_i)$. Steps 6 and~13 insert each such edge to the heap
740 at most once, so steps 8--12 also visit each edge at most once. For every edge, we spend
741 $\O(\log(1/\varepsilon))$ time amortized on inserting it and $\O(1)$ on the other operations
742 (by Theorem \ref{softheap} on performance of the soft heaps).
743
744 Each edge of~$G$ can participate in some $C_i\cup \delta(C_i)$ only twice before both its
745 ends die. The inner loop therefore processes $\O(m)$ edges total, so we spend $\O(m\log(1/\varepsilon))$
746 time there. The rest of the algorithm spends $\O(m)$ time on the other operations with subgraphs
747 and $\O(n)$ on identifying the live vertices.
748 \qed
749
750 %--------------------------------------------------------------------------------
751
752 \section{Decision trees}
753
754 The Pettie's algorithm combines the idea of robust partitioning with optimal decision
755 trees constructed by brute force for very small subgraphs. In this section, we will
756 explain the basics of the decision trees and prove several lemmata which will
757 turn out to be useful for the analysis of time complexity of the whole algorithm.
758
759 Let us consider all computations of a~comparison-based MST algorithm when we
760 run it on a~fixed graph~$G$ with all possible permutations of edge weights.
761 They can be described by a~tree. The root of the tree corresponds to the first
762 comparison performed by the algorithm and depending to its result, the computation
763 continues either in the left subtree or in the right subtree. There it encounters
764 another comparison and so on, until it arrives to a~leaf of the tree where the
765 spanning tree found by the algorithm is recorded.
766
767 Formally, the decision trees are defined as follows:
768
769 \defnn{Decision trees and their complexity}\id{decdef}%
770 A~\df{MSF decision tree} for a~graph~$G$ is a~binary tree. Its internal vertices
771 are labeled with pairs of edges to be compared, each of the two outgoing edges
772 corresponds to one possible result of the comparison.\foot{There is no reason
773 to compare two identical edges.}
774 Leaves of the tree are labeled with spanning trees of the graph~$G$.
775
776 A~\df{computation} of the decision tree on a~specific permutation of edge weights
777 in~$G$ is a~path from the root to a~leaf such that the outcome of every comparison
778 agrees with the edge weights. The result of the computation is the spanning tree
779 assigned to its final leaf.
780 A~decision tree is \df{correct} iff for every permutation the corresponding
781 computation results in the real MSF of~$G$ with the particular weights.
782
783 The \df{time complexity} of a~decision tree is defined as its depth. It therefore
784 bounds the number of comparisons spent on every path. (It need not be equal since
785 some paths need not correspond to an~actual computation --- the sequence of outcomes
786 on the path can be unsatifisfiable.)
787
788 A~decision tree is called \df{optimal} if it is correct and its depth is minimum possible
789 among the correct decision trees for the given graph.
790 We will denote an~arbitrary optimal decision tree for~$G$ by~${\cal D}(G)$ and its
791 complexity by~$D(G)$.
792
793 The \df{decision tree complexity} $D(m,n)$ of the MSF problem is the maximum of~$D(G)$
794 over all graphs~$G$ with $n$~vertices and~$m$ edges.
795
796 \obs
797 Decision trees are the most general comparison-based computation model possible.
798 The only operations which count in the time complexity are the comparisons. All
799 other computation is free, including solving NP-complete problems or having
800 access to an~unlimited source of non-uniform constants. The decision tree
801 complexity is therefore an~obvious lower bound on the time complexity of the
802 problem in all other models.
803
804 The downside is that we do not know any explicit construction of the optimal
805 decision trees, or at least a~non-constructive proof of their complexity.
806 On the other hand, the complexity of any existing comparison-based algorithm
807 can be used as an~upper bound for the decision tree complexity. For example:
808
809 \lemma
810 $D(m,n) \le 4/3 \cdot n^2$.
811
812 \proof
813 Let us count the comparisons performed by the contractive Bor\o{u}vka's algorithm.
814 (\ref{contbor}), tightening up the constants in the original analysis in Theorem
815 \ref{contborthm}. In the first phase, each edge participates in two comparisons
816 (one for each its endpoint), so the algorithm performs at most $2m \le 2{n\choose 2} \le n^2$
817 comparisons. Then the number of vertices drops at least by a~factor of two, so
818 the subsequent phases spend $(n/2)^2, (n/4)^2, \ldots$ comparisons, which sums
819 to less than $n^2\cdot\sum_{i=0}^\infty (1/4)^i = 4/3 \cdot n^2$.
820 \qed
821
822 \para
823 Of course we can get sharper bounds from the better algorithms, but we will first
824 show how to find the optimal trees using brute force. The complexity of the search
825 will be of course enormous, but as we already promised, we will need the optimal
826 trees only for very small subgraphs.
827
828 \lemma
829 Optimal MST decision trees for all graphs on at most~$k$ vertices can be
830 constructed on the Pointer machine in time $\O(2^{2^{k^3}})$.
831
832 \proof
833 There are $2^{k\choose 2} \le 2^{k^2}$ undirected graphs on~$k$ vertices. Any
834 graph on less than~$k$ vertices can be expanded to $k$~vertices by adding isolated
835 vertices, which obviously do not affect the optimal decision tree.
836
837 For every graph, we will try all possible trees of depth at most $4/3\cdot k^2$
838 (we know from the previous lemma that the optimal tree is not deeper). Each such
839 tree has at most $2^{2k^2}$ internal vertices and at most $2^{2k^2}$ leaves.
840 All internal vertices are labeled with comparisons and there are less than $k^4$
841 ways how to select the edges to be compared. Each leaf is labeled with one of the
842 at most $2^{k^2}$ spanning trees. There are therefore at most $(2^{2k^2})^{2^{2k^2}}
843 \le 2^{2k^2\cdot 2^{2k^2}} \le 2^{2^{k^3}}$ candidate trees and we will enumerate them
844 in order of increasing depth.
845
846 \FIXME{Continue with verifying correctness.}
847
848 \qed
849
850
851
852
853 %--------------------------------------------------------------------------------
854
855 \section{An optimal algorithm}
856
857
858
859
860 \endpart