13.3 YFastTrie: A Doubly-Logarithmic Time SSet

The XFastTrie is a vast--even exponential--improvement over the BinaryTrie in terms of query time, but the $ \ensuremath{\mathrm{add}(\ensuremath{\mathit{x}})}$ and $ \ensuremath{\mathrm{remove}(\ensuremath{\mathit{x}})}$ operations are still not terribly fast. Furthermore, the space usage, $ O(\ensuremath{\ensuremath{\ensuremath{\mathit{n}}}}\cdot\ensuremath{\ensuremath{\ensuremath{\mathit{w}}}})$, is higher than the other SSet implementations described in this book, which all use $ O(\ensuremath{\ensuremath{\ensuremath{\mathit{n}}}})$ space. These two problems are related; if $ \ensuremath{\ensuremath{\mathit{n}}}$ $ \ensuremath{\mathrm{add}(\ensuremath{\mathit{x}})}$ operations build a structure of size $ \ensuremath{\ensuremath{\ensuremath{\mathit{n}}}}\cdot\ensuremath{\ensuremath{\ensuremath{\mathit{w}}}}$, then the $ \ensuremath{\mathrm{add}(\ensuremath{\mathit{x}})}$ operation requires at least on the order of $ \ensuremath{\ensuremath{\mathit{w}}}$ time (and space) per operation.

The YFastTrie, discussed next, simultaneously improves the space and speed of XFastTries. A YFastTrie uses an XFastTrie, $ \ensuremath{\ensuremath{\mathit{xft}}}$, but only stores $ O(\ensuremath{\ensuremath{\ensuremath{\mathit{n}}}}/\ensuremath{\ensuremath{\ensuremath{\mathit{w}}}})$ values in $ \ensuremath{\ensuremath{\mathit{xft}}}$. In this way, the total space used by $ \ensuremath{\ensuremath{\mathit{xft}}}$ is only $ O(\ensuremath{\ensuremath{\ensuremath{\mathit{n}}}})$. Furthermore, only one out of every $ \ensuremath{\ensuremath{\mathit{w}}}$ $ \ensuremath{\mathrm{add}(\ensuremath{\mathit{x}})}$ or $ \ensuremath{\mathrm{remove}(\ensuremath{\mathit{x}})}$ operations in the YFastTrie results in an $ \ensuremath{\mathrm{add}(\ensuremath{\mathit{x}})}$ or $ \ensuremath{\mathrm{remove}(\ensuremath{\mathit{x}})}$ operation in $ \ensuremath{\ensuremath{\mathit{xft}}}$. By doing this, the average cost incurred by calls to $ \ensuremath{\ensuremath{\mathit{xft}}}$'s $ \ensuremath{\mathrm{add}(\ensuremath{\mathit{x}})}$ and $ \ensuremath{\mathrm{remove}(\ensuremath{\mathit{x}})}$ operations is only constant.

The obvious question becomes: If $ \ensuremath{\ensuremath{\mathit{xft}}}$ only stores $ \ensuremath{\ensuremath{\mathit{n}}}$/ $ \ensuremath{\ensuremath{\mathit{w}}}$ elements, where do the remaining $ \ensuremath{\ensuremath{\ensuremath{\mathit{n}}}}(1-1/\ensuremath{\ensuremath{\ensuremath{\mathit{w}}}})$ elements go? These elements move into secondary structures, in this case an extended version of treaps (Section 7.2). There are roughly $ \ensuremath{\ensuremath{\mathit{n}}}$/ $ \ensuremath{\ensuremath{\mathit{w}}}$ of these secondary structures so, on average, each of them stores $ O(\ensuremath{\ensuremath{\ensuremath{\mathit{w}}}})$ items. Treaps support logarithmic time SSet operations, so the operations on these treaps will run in $ O(\log \ensuremath{\ensuremath{\ensuremath{\mathit{w}}}})$ time, as required.

More concretely, a YFastTrie contains an XFastTrie, $ \ensuremath{\ensuremath{\mathit{xft}}}$, that contains a random sample of the data, where each element appears in the sample independently with probability $ 1/\ensuremath{\ensuremath{\ensuremath{\mathit{w}}}}$. For convenience, the value $ 2^{\ensuremath{\ensuremath{\ensuremath{\mathit{w}}}}}-1$, is always contained in $ \ensuremath{\ensuremath{\mathit{xft}}}$. Let $ \ensuremath{\ensuremath{\ensuremath{\mathit{x}}}}_0<\ensuremath{\ensuremath{\e...
...{\mathit{x}}}}_1<\cdots<\ensuremath{\ensuremath{\ensuremath{\mathit{x}}}}_{k-1}$ denote the elements stored in $ \ensuremath{\ensuremath{\mathit{xft}}}$. Associated with each element, $ \ensuremath{\ensuremath{\ensuremath{\mathit{x}}}}_i$, is a treap, $ \ensuremath{\ensuremath{\ensuremath{\mathit{t}}}}_i$, that stores all values in the range $ \ensuremath{\ensuremath{\ensuremath{\mathit{x}}}}_{i-1}+1,\ldots,\ensuremath{\ensuremath{\ensuremath{\mathit{x}}}}_i$. This is illustrated in Figure 13.7.

Figure 13.7: A YFastTrie containing the values 0, 1, 3, 4, 6, 8, 9, 10, 11, and 13.
\includegraphics[scale=0.90909]{figs-python/yfast}

The $ \ensuremath{\mathrm{find}(\ensuremath{\mathit{x}})}$ operation in a YFastTrie is fairly easy. We search for $ \ensuremath{\ensuremath{\mathit{x}}}$ in $ \ensuremath{\ensuremath{\mathit{xft}}}$ and find some value $ \ensuremath{\ensuremath{\ensuremath{\mathit{x}}}}_i$ associated with the treap $ \ensuremath{\ensuremath{\ensuremath{\mathit{t}}}}_i$. We then use the treap $ \ensuremath{\mathrm{find}(\ensuremath{\mathit{x}})}$ method on $ \ensuremath{\ensuremath{\ensuremath{\mathit{t}}}}_i$ to answer the query. The entire method is a one-liner:
\begin{leftbar}
\begin{flushleft}
\hspace*{1em} \ensuremath{\mathrm{find}(\ensur...
...suremath{\mathrm{find}(\ensuremath{\mathit{x}})}\\
\end{flushleft}\end{leftbar}
The first $ \ensuremath{\mathrm{find}(\ensuremath{\mathit{x}})}$ operation (on $ \ensuremath{\ensuremath{\mathit{xft}}}$) takes $ O(\log\ensuremath{\ensuremath{\ensuremath{\mathit{w}}}})$ time. The second $ \ensuremath{\mathrm{find}(\ensuremath{\mathit{x}})}$ operation (on a treap) takes $ O(\log r)$ time, where $ r$ is the size of the treap. Later in this section, we will show that the expected size of the treap is $ O(\ensuremath{\ensuremath{\ensuremath{\mathit{w}}}})$ so that this operation takes $ O(\log \ensuremath{\ensuremath{\ensuremath{\mathit{w}}}})$ time.13.1

Adding an element to a YFastTrie is also fairly simple--most of the time. The $ \ensuremath{\mathrm{add}(\ensuremath{\mathit{x}})}$ method calls $ \ensuremath{\ensuremath{\mathit{xft}}.\mathrm{find}(\ensuremath{\mathit{x}})}$ to locate the treap, $ \ensuremath{\ensuremath{\mathit{t}}}$, into which $ \ensuremath{\ensuremath{\mathit{x}}}$ should be inserted. It then calls $ \ensuremath{\ensuremath{\mathit{t}}.\mathrm{add}(\ensuremath{\mathit{x}})}$ to add $ \ensuremath{\ensuremath{\mathit{x}}}$ to $ \ensuremath{\ensuremath{\mathit{t}}}$. At this point, it tosses a biased coin that comes up as heads with probability $ 1/\ensuremath{\ensuremath{\ensuremath{\mathit{w}}}}$ and as tails with probability $ 1-1/\ensuremath{\ensuremath{\ensuremath{\mathit{w}}}}$. If this coin comes up heads, then $ \ensuremath{\ensuremath{\mathit{x}}}$ will be added to $ \ensuremath{\ensuremath{\mathit{xft}}}$.

This is where things get a little more complicated. When $ \ensuremath{\ensuremath{\mathit{x}}}$ is added to $ \ensuremath{\ensuremath{\mathit{xft}}}$, the treap $ \ensuremath{\ensuremath{\mathit{t}}}$ needs to be split into two treaps, $ \ensuremath{\ensuremath{\mathit{t_1}}}$ and $ \ensuremath{t}'$. The treap $ \ensuremath{\ensuremath{\mathit{t_1}}}$ contains all the values less than or equal to $ \ensuremath{\ensuremath{\mathit{x}}}$; $ \ensuremath{t}'$ is the original treap, $ \ensuremath{\ensuremath{\mathit{t}}}$, with the elements of $ \ensuremath{\ensuremath{\mathit{t_1}}}$ removed. Once this is done, we add the pair $ \ensuremath{(\ensuremath{\mathit{x}},\ensuremath{\mathit{t_1}})}$ to $ \ensuremath{\ensuremath{\mathit{xft}}}$. Figure 13.8 shows an example.
\begin{leftbar}
\begin{flushleft}
\hspace*{1em} \ensuremath{\mathrm{add}(\ensure...
...eturn}} \ensuremath{\ensuremath{\mathit{false}}}\\
\end{flushleft}\end{leftbar}

Figure 13.8: Adding the values 2 and 6 to a YFastTrie. The coin toss for 6 came up heads, so 6 was added to $ \ensuremath{\ensuremath{\mathit{xft}}}$ and the treap containing $ 4,5,6,8,9$ was split.
\includegraphics[scale=0.90909]{figs-python/yfast-add}
Adding $ \ensuremath{\ensuremath{\mathit{x}}}$ to $ \ensuremath{\ensuremath{\mathit{t}}}$ takes $ O(\log \ensuremath{\ensuremath{\ensuremath{\mathit{w}}}})$ time. Exercise 7.12 shows that splitting $ \ensuremath{\ensuremath{\mathit{t}}}$ into $ \ensuremath{\ensuremath{\mathit{t_1}}}$ and $ \ensuremath{t}'$ can also be done in $ O(\log \ensuremath{\ensuremath{\ensuremath{\mathit{w}}}})$ expected time. Adding the pair ( $ \ensuremath{\ensuremath{\mathit{x}}}$, $ \ensuremath{\ensuremath{\mathit{t_1}}}$) to $ \ensuremath{\ensuremath{\mathit{xft}}}$ takes $ O(\ensuremath{\ensuremath{\ensuremath{\mathit{w}}}})$ time, but only happens with probability $ 1/\ensuremath{\ensuremath{\ensuremath{\mathit{w}}}}$. Therefore, the expected running time of the $ \ensuremath{\mathrm{add}(\ensuremath{\mathit{x}})}$ operation is

$\displaystyle O(\log\ensuremath{\ensuremath{\ensuremath{\mathit{w}}}}) + \frac{...
...{w}}}}) = O(\log \ensuremath{\ensuremath{\ensuremath{\mathit{w}}}}) \enspace .
$

The $ \ensuremath{\mathrm{remove}(\ensuremath{\mathit{x}})}$ method undoes the work performed by $ \ensuremath{\mathrm{add}(\ensuremath{\mathit{x}})}$. We use $ \ensuremath{\ensuremath{\mathit{xft}}}$ to find the leaf, $ \ensuremath{\ensuremath{\mathit{u}}}$, in $ \ensuremath{\ensuremath{\mathit{xft}}}$ that contains the answer to $ \ensuremath{\ensuremath{\mathit{xft}}.\mathrm{find}(\ensuremath{\mathit{x}})}$. From $ \ensuremath{\ensuremath{\mathit{u}}}$, we get the treap, $ \ensuremath{\ensuremath{\mathit{t}}}$, containing $ \ensuremath{\ensuremath{\mathit{x}}}$ and remove $ \ensuremath{\ensuremath{\mathit{x}}}$ from $ \ensuremath{\ensuremath{\mathit{t}}}$. If $ \ensuremath{\ensuremath{\mathit{x}}}$ was also stored in $ \ensuremath{\ensuremath{\mathit{xft}}}$ (and $ \ensuremath{\ensuremath{\mathit{x}}}$ is not equal to $ 2^{\ensuremath{\ensuremath{\ensuremath{\mathit{w}}}}}-1$) then we remove $ \ensuremath{\ensuremath{\mathit{x}}}$ from $ \ensuremath{\ensuremath{\mathit{xft}}}$ and add the elements from $ \ensuremath{\ensuremath{\mathit{x}}}$'s treap to the treap, $ \ensuremath{\ensuremath{\mathit{t_2}}}$, that is stored by $ \ensuremath{\ensuremath{\mathit{u}}}$'s successor in the linked list. This is illustrated in Figure 13.9.
\begin{leftbar}
\begin{flushleft}
\hspace*{1em} \ensuremath{\mathrm{remove}(\ens...
...{return}} \ensuremath{\ensuremath{\mathit{ret}}}\\
\end{flushleft}\end{leftbar}

Figure 13.9: Removing the values 1 and 9 from a YFastTrie in Figure 13.8.
\includegraphics[scale=0.90909]{figs-python/yfast-remove}
Finding the node $ \ensuremath{\ensuremath{\mathit{u}}}$ in $ \ensuremath{\ensuremath{\mathit{xft}}}$ takes $ O(\log\ensuremath{\ensuremath{\ensuremath{\mathit{w}}}})$ expected time. Removing $ \ensuremath{\ensuremath{\mathit{x}}}$ from $ \ensuremath{\ensuremath{\mathit{t}}}$ takes $ O(\log\ensuremath{\ensuremath{\ensuremath{\mathit{w}}}})$ expected time. Again, Exercise 7.12 shows that merging all the elements of $ \ensuremath{\ensuremath{\mathit{t}}}$ into $ \ensuremath{\ensuremath{\mathit{t_2}}}$ can be done in $ O(\log\ensuremath{\ensuremath{\ensuremath{\mathit{w}}}})$ time. If necessary, removing $ \ensuremath{\ensuremath{\mathit{x}}}$ from $ \ensuremath{\ensuremath{\mathit{xft}}}$ takes $ O(\ensuremath{\ensuremath{\ensuremath{\mathit{w}}}})$ time, but $ \ensuremath{\ensuremath{\mathit{x}}}$ is only contained in $ \ensuremath{\ensuremath{\mathit{xft}}}$ with probability $ 1/\ensuremath{\ensuremath{\ensuremath{\mathit{w}}}}$. Therefore, the expected time to remove an element from a YFastTrie is $ O(\log \ensuremath{\ensuremath{\ensuremath{\mathit{w}}}})$.

Earlier in the discussion, we delayed arguing about the sizes of treaps in this structure until later. Before finishing this chapter, we prove the result we need.

Lemma 13..1   Let $ \ensuremath{\ensuremath{\mathit{x}}}$ be an integer stored in a YFastTrie and let $ \ensuremath{\ensuremath{\ensuremath{\mathit{n}}}}_\ensuremath{\ensuremath{\ensuremath{\mathit{x}}}}$ denote the number of elements in the treap, $ \ensuremath{\ensuremath{\mathit{t}}}$, that contains $ \ensuremath{\ensuremath{\mathit{x}}}$. Then $ \mathrm{E}[\ensuremath{\ensuremath{\ensuremath{\mathit{n}}}}_\ensuremath{\ensu...
...uremath{\mathit{x}}}}] \le 2\ensuremath{\ensuremath{\ensuremath{\mathit{w}}}}-1$.

Proof. Refer to Figure 13.10. Let $ \ensuremath{\ensuremath{\ensuremath{\mathit{x}}}}_1<\ensuremath{\ensuremath{\e...
...ath{\ensuremath{\mathit{x}}}}_\ensuremath{\ensuremath{\ensuremath{\mathit{n}}}}$ denote the elements stored in the YFastTrie. The treap $ \ensuremath{\ensuremath{\mathit{t}}}$ contains some elements greater than or equal to $ \ensuremath{\ensuremath{\mathit{x}}}$. These are $ \ensuremath{\ensuremath{\ensuremath{\mathit{x}}}}_i,\ensuremath{\ensuremath{\e...
...it{x}}}}_{i+1},\ldots,\ensuremath{\ensuremath{\ensuremath{\mathit{x}}}}_{i+j-1}$, where $ \ensuremath{\ensuremath{\ensuremath{\mathit{x}}}}_{i+j-1}$ is the only one of these elements in which the biased coin toss performed in the $ \ensuremath{\mathrm{add}(\ensuremath{\mathit{x}})}$ method turned up as heads. In other words, $ \mathrm{E}[j]$ is equal to the expected number of biased coin tosses required to obtain the first heads.13.2 Each coin toss is independent and turns up as heads with probability $ 1/\ensuremath{\ensuremath{\ensuremath{\mathit{w}}}}$, so $ \mathrm{E}[j]\le\ensuremath{\ensuremath{\ensuremath{\mathit{w}}}}$. (See Lemma 4.2 for an analysis of this for the case $ \ensuremath{\ensuremath{\ensuremath{\mathit{w}}}}=2$.)

Similarly, the elements of $ \ensuremath{\ensuremath{\mathit{t}}}$ smaller than $ \ensuremath{\ensuremath{\mathit{x}}}$ are $ \ensuremath{\ensuremath{\ensuremath{\mathit{x}}}}_{i-1},\ldots,\ensuremath{\ensuremath{\ensuremath{\mathit{x}}}}_{i-k}$ where all these $ k$ coin tosses turn up as tails and the coin toss for $ \ensuremath{\ensuremath{\ensuremath{\mathit{x}}}}_{i-k-1}$ turns up as heads. Therefore, $ \mathrm{E}[k]\le\ensuremath{\ensuremath{\ensuremath{\mathit{w}}}}-1$, since this is the same coin tossing experiment considered in the preceding paragraph, but one in which the last toss is not counted. In summary, $ \ensuremath{\ensuremath{\ensuremath{\mathit{n}}}}_\ensuremath{\ensuremath{\ensuremath{\mathit{x}}}}=j+k$, so

$\displaystyle \mathrm{E}[\ensuremath{\ensuremath{\ensuremath{\mathit{n}}}}_\ens...
...] \le 2\ensuremath{\ensuremath{\ensuremath{\mathit{w}}}}-1 \enspace . \qedhere $

$ \qedsymbol$

Figure 13.10: The number of elements in the treap $ \ensuremath{\ensuremath{\mathit{t}}}$ containing $ \ensuremath{\ensuremath{\mathit{x}}}$ is determined by two coin tossing experiments.
\includegraphics[width=\textwidth ]{figs-python/yfast-sample}

Lemma 13.1 was the last piece in the proof of the following theorem, which summarizes the performance of the YFastTrie:

Theorem 13..3   A YFastTrie implements the SSet interface for $ \ensuremath{\ensuremath{\mathit{w}}}$-bit integers. A YFastTrie supports the operations $ \ensuremath{\mathrm{add}(\ensuremath{\mathit{x}})}$, $ \ensuremath{\mathrm{remove}(\ensuremath{\mathit{x}})}$, and $ \ensuremath{\mathrm{find}(\ensuremath{\mathit{x}})}$ in $ O(\log \ensuremath{\ensuremath{\ensuremath{\mathit{w}}}})$ expected time per operation. The space used by a YFastTrie that stores $ \ensuremath{\ensuremath{\mathit{n}}}$ values is $ O(\ensuremath{\ensuremath{\ensuremath{\mathit{n}}}}+\ensuremath{\ensuremath{\ensuremath{\mathit{w}}}})$.

The $ \ensuremath{\ensuremath{\mathit{w}}}$ term in the space requirement comes from the fact that $ \ensuremath{\ensuremath{\mathit{xft}}}$ always stores the value $ 2^\ensuremath{\ensuremath{\ensuremath{\mathit{w}}}}-1$. The implementation could be modified (at the expense of adding some extra cases to the code) so that it is unnecessary to store this value. In this case, the space requirement in the theorem becomes $ O(\ensuremath{\ensuremath{\ensuremath{\mathit{n}}}})$.



Footnotes

... time.13.1
This is an application of Jensen's Inequality: If $ \mathrm{E}[r]=\ensuremath{\ensuremath{\ensuremath{\mathit{w}}}}$, then $ \mathrm{E}[\log r]
\le \log w$.
... heads.13.2
This analysis ignores the fact that $ j$ never exceeds $ \ensuremath{\ensuremath{\ensuremath{\mathit{n}}}}-i+1$. However, this only decreases $ \mathrm{E}[j]$, so the upper bound still holds.
opendatastructures.org