Van Emde Boas tree



































Van Emde Boas tree

Type
Non-binary tree
Invented
1975
Invented by

Peter van Emde Boas
Asymptotic complexity
in big O notation
Space

O(M)
Search

O(log log M)
Insert

O(log log M)
Delete

O(log log M)

A Van Emde Boas tree (or Van Emde Boas priority queue; Dutch pronunciation: [vɑn 'ɛmdə 'boːɑs]), also known as a vEB tree, is a tree data structure which implements an associative array with m-bit integer keys. It performs all operations in O(log m) time, or equivalently in O(log log M) time, where M = 2m is the maximum number of elements that can be stored in the tree. The M is not to be confused with the actual number of elements stored in the tree, by which the performance of other tree data-structures is often measured. The vEB tree has good space efficiency when it contains a large number of elements, as discussed below. It was invented by a team led by Dutch computer scientist Peter van Emde Boas in 1975.[1]




Contents






  • 1 Supported operations


  • 2 How it works


    • 2.1 FindNext


    • 2.2 Insert


    • 2.3 Delete


    • 2.4 Discussion




  • 3 See also


  • 4 References


    • 4.1 Further reading







Supported operations


A vEB supports the operations of an ordered associative array, which includes the usual associative array operations along with two more order operations, FindNext and FindPrevious:[2]




  • Insert: insert a key/value pair with an m-bit key


  • Delete: remove the key/value pair with a given key


  • Lookup: find the value associated with a given key


  • FindNext: find the key/value pair with the smallest key which is greater than a given k


  • FindPrevious: find the key/value pair with the largest key which is smaller than a given k


A vEB tree also supports the operations Minimum and Maximum, which return the minimum and maximum element stored in the tree respectively.[3] These both run in O(1) time, since the minimum and maximum element are stored as attributes in each tree.



How it works



Example Van Emde Boas tree

An example Van Emde Boas tree with dimension 5 and the root's aux structure after 1, 2, 3, 5, 8 and 10 have been inserted.


For the sake of simplicity, let log2m = k for some integer k. Define M = 2m. A vEB tree


.mw-parser-output .monospaced{font-family:monospace,monospace}

T over the universe {0, ..., M−1} has a root node that stores an array



T.children of length M.



T.children[i] is a pointer to a vEB tree that is responsible for the values {iM, ..., (i+1)M−1}. Additionally, T stores two values



T.min and



T.max as well as an auxiliary vEB tree



T.aux.


Data is stored in a vEB tree as follows: The smallest value currently in the tree is stored in T.min and largest value is stored in T.max. Note that T.min is not stored anywhere else in the vEB tree, while T.max is. If T is empty then we use the convention that T.max=−1 and T.min=M. Any other value x is stored in the subtree T.children[i] where i = ⌊x/M. The auxiliary tree T.aux keeps track of which children are non-empty, so T.aux contains the value j if and only if T.children[j] is non-empty.



FindNext


The operation FindNext(T, x) that searches for the successor of an element x in a vEB tree proceeds as follows: If x<T.min then the search is complete, and the answer is T.min. If x≥T.max then the next element does not exist, return M. Otherwise, let i = x/M. If x<T.children[i].max then the value being searched for is contained in T.children[i] so the search proceeds recursively in T.children[i]. Otherwise, we search for the value i in T.aux. This gives us the index j of the first subtree that contains an element larger than x. The algorithm then returns T.children[j].min. The element found on the children level needs to be composed with the high bits to form a complete next element.


function FindNext(T, x).
if x < T.min then
return T.min
if x ≥ T.max then // no next element
return M
i = floor(x/M)
lo = x mod M
hi = x − lo
if lo < T.children[i].max then
return hi + FindNext(T.children[i], lo)
return hi + T.children[FindNext(T.aux, i)].min
end

Note that, in any case, the algorithm performs O(1) work and then possibly recurses on a subtree over a universe of size M1/2 (an m/2 bit universe). This gives a recurrence for the running time of T(m)=T(m/2)+O(1){displaystyle T(m)=T(m/2)+O(1)}T(m)=T(m/2) + O(1), which resolves to O(log m) = O(log log M).



Insert


The call insert(T, x) that inserts a value x into a vEB tree T operates as follows:



  1. If T is empty then we set
    T.min = T.max = x and we are done.

  2. Otherwise, if
    x<T.min then we insert
    T.min into the subtree
    i responsible for
    T.min and then set
    T.min = x. If
    T.children[i] was previously empty, then we also insert
    i into
    T.aux

  3. Otherwise, if
    x>T.max then we insert
    x into the subtree
    i responsible for
    x and then set
    T.max = x. If
    T.children[i] was previously empty, then we also insert
    i into
    T.aux

  4. Otherwise,
    T.min< x < T.max so we insert
    x into the subtree
    i responsible for
    x. If
    T.children[i] was previously empty, then we also insert
    i into
    T.aux.


In code:


function Insert(T, x)
if T.min > T.max then // T is empty
T.min = T.max = x;
return
if x < T.min then
swap(x, T.min)
if x > T.max then
T.max = x
i = floor(x / M)
lo = x mod M
Insert(T.children[i], lo)
if T.children[i].min == T.children[i].max then
Insert(T.aux, i)
end

The key to the efficiency of this procedure is that inserting an element into an empty vEB tree takes O(1) time. So, even though the algorithm sometimes makes two recursive calls, this only occurs when the first recursive call was into an empty subtree. This gives the same running time recurrence of T(m)=T(m/2)+O(1){displaystyle T(m)=T(m/2)+O(1)}T(m)=T(m/2) + O(1) as before.



Delete


Deletion from vEB trees is the trickiest of the operations. The call Delete(T, x) that deletes a value x from a vEB tree T operates as follows:



  1. If
    T.min = T.max = x then x is the only element stored in the tree and we set
    T.min = M and
    T.max = −1 to indicate that the tree is empty.

  2. Otherwise, if
    x == T.min then we need to find the second-smallest value y in the vEB tree, delete it from its current location, and set
    T.min=y. The second-smallest value y is
    T.children[T.aux.min].min, so it can be found in O(1) time. We delete y from the subtree that contains it.

  3. If
    x≠T.min and
    x≠T.max then we delete x from the subtree
    T.children[i] that contains x.

  4. If
    x == T.max then we will need to find the second-largest value y in the vEB tree and set
    T.max=y. We start by deleting x as in previous case. Then value y is either
    T.min or
    T.children[T.aux.max].max, so it can be found in O(1) time.

  5. In any of the above cases, if we delete the last element x or y from any subtree
    T.children[i] then we also delete i from
    T.aux


In code:


function Delete(T, x)
if T.min == T.max == x then
T.min = M
T.max = −1
return
if x == T.min then
hi = T.aux.min * M
j = T.aux.min
T.min = x = hi + T.children[j].min
i = floor(x / M)
lo = x mod M
Delete(T.children[i], lo)
if T.children[i] is empty then
Delete(T.aux, i)
if x == T.max then
if T.aux is empty then
T.max = T.min
else
hi = T.aux.max * M
j = T.aux.max
T.max = hi + T.children[j].max
end

Again, the efficiency of this procedure hinges on the fact that deleting from a vEB tree that contains only one element takes only constant time. In particular, the last line of code only executes if x was the only element in T.children[i] prior to the deletion.



Discussion


The assumption that log m is an integer is unnecessary. The operations x/M and x mod M can be replaced by taking only higher-order m/2⌉ and the lower-order m/2⌋ bits of x, respectively. On any existing machine, this is more efficient than division or remainder computations.


The implementation described above uses pointers and occupies a total space of O(M) = O(2m). This can be seen as follows. The recurrence is S(M)=O(M)+(M+1)⋅S(M){displaystyle S(M)=O({sqrt {M}})+({sqrt {M}}+1)cdot S({sqrt {M}})}S(M)=O({sqrt  {M}})+({sqrt  {M}}+1)cdot S({sqrt  {M}}).
Resolving that would lead to S(M)∈(1+M)log⁡log⁡M+log⁡log⁡M⋅O(M){displaystyle S(M)in (1+{sqrt {M}})^{log log M}+log log Mcdot O({sqrt {M}})}S(M)in (1+{sqrt  {M}})^{{log log M}}+log log Mcdot O({sqrt  {M}}).
One can, fortunately, also show that S(M) = M−2 by induction.[4]


In practical implementations, especially on machines with shift-by-k and find first zero instructions, performance can further be improved by switching to a bit array once m equal to the word size (or a small multiple thereof) is reached. Since all operations on a single word are constant time, this does not affect the asymptotic performance, but it does avoid the majority of the pointer storage and several pointer dereferences, achieving a significant practical savings in time and space with this trick.


An obvious optimization of vEB trees is to discard empty subtrees. This makes vEB trees quite compact when they contain many elements, because no subtrees are created until something needs to be added to them. Initially, each element added creates about log(m) new trees containing about m/2 pointers all together. As the tree grows, more and more subtrees are reused, especially the larger ones. In a full tree of 2m elements, only O(2m) space is used. Moreover, unlike a binary search tree, most of this space is being used to store data: even for billions of elements, the pointers in a full vEB tree number in the thousands.


However, for small trees the overhead associated with vEB trees is enormous: on the order of M. This is one reason why they are not popular in practice. One way of addressing this limitation is to use only a fixed number of bits per level, which results in a trie. Alternatively, each table may be replaced by a hash table, reducing the space to O(n log M) (where n is the number of elements stored in the data structure) at the expense of making the data structure randomized. Other structures, including y-fast tries and x-fast tries have been proposed that have comparable update and query times and also use randomized hash tables to reduce the space to O(n) or O(n log M).



See also



  • Integer sorting

  • Predecessor problem

  • Fusion tree

  • Tango tree

  • X-fast trie

  • Y-fast trie



References





  1. ^ Peter van Emde Boas: Preserving order in a forest in less than logarithmic time (Proceedings of the 16th Annual Symposium on Foundations of Computer Science 10: 75-84, 1975)


  2. ^ Gudmund Skovbjerg Frandsen: Dynamic algorithms: Course notes on van Emde Boas trees (PDF) (University of Aarhus, Department of Computer Science)


  3. ^ Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest, and Clifford Stein. Introduction to Algorithms, Third Edition. MIT Press, 2009. .mw-parser-output cite.citation{font-style:inherit}.mw-parser-output .citation q{quotes:"""""""'""'"}.mw-parser-output .citation .cs1-lock-free a{background:url("//upload.wikimedia.org/wikipedia/commons/thumb/6/65/Lock-green.svg/9px-Lock-green.svg.png")no-repeat;background-position:right .1em center}.mw-parser-output .citation .cs1-lock-limited a,.mw-parser-output .citation .cs1-lock-registration a{background:url("//upload.wikimedia.org/wikipedia/commons/thumb/d/d6/Lock-gray-alt-2.svg/9px-Lock-gray-alt-2.svg.png")no-repeat;background-position:right .1em center}.mw-parser-output .citation .cs1-lock-subscription a{background:url("//upload.wikimedia.org/wikipedia/commons/thumb/a/aa/Lock-red-alt-2.svg/9px-Lock-red-alt-2.svg.png")no-repeat;background-position:right .1em center}.mw-parser-output .cs1-subscription,.mw-parser-output .cs1-registration{color:#555}.mw-parser-output .cs1-subscription span,.mw-parser-output .cs1-registration span{border-bottom:1px dotted;cursor:help}.mw-parser-output .cs1-ws-icon a{background:url("//upload.wikimedia.org/wikipedia/commons/thumb/4/4c/Wikisource-logo.svg/12px-Wikisource-logo.svg.png")no-repeat;background-position:right .1em center}.mw-parser-output code.cs1-code{color:inherit;background:inherit;border:inherit;padding:inherit}.mw-parser-output .cs1-hidden-error{display:none;font-size:100%}.mw-parser-output .cs1-visible-error{font-size:100%}.mw-parser-output .cs1-maint{display:none;color:#33aa33;margin-left:0.3em}.mw-parser-output .cs1-subscription,.mw-parser-output .cs1-registration,.mw-parser-output .cs1-format{font-size:95%}.mw-parser-output .cs1-kern-left,.mw-parser-output .cs1-kern-wl-left{padding-left:0.2em}.mw-parser-output .cs1-kern-right,.mw-parser-output .cs1-kern-wl-right{padding-right:0.2em}
    ISBN 978-0-262-53305-8. Chapter 20: The van Emde Boas tree, pp. 531–560.



  4. ^ Rex, A. "Determining the space complexity of van Emde Boas trees". Retrieved 2011-05-27.




Further reading


.mw-parser-output .refbegin{font-size:90%;margin-bottom:0.5em}.mw-parser-output .refbegin-hanging-indents>ul{list-style-type:none;margin-left:0}.mw-parser-output .refbegin-hanging-indents>ul>li,.mw-parser-output .refbegin-hanging-indents>dl>dd{margin-left:0;padding-left:3.2em;text-indent:-3.2em;list-style:none}.mw-parser-output .refbegin-100{font-size:100%}


  • Erik Demaine, Sam Fingeret, Shravas Rao, Paul Christiano. Massachusetts Institute of Technology. 6.851: Advanced Data Structures (Spring 2012). Lecture 11 notes. March 22, 2012.


  • Van Emde Boas, P.; Kaas, R.; Zijlstra, E. (1976). "Design and implementation of an efficient priority queue". Mathematical Systems Theory. 10: 99–127. doi:10.1007/BF01683268.










Popular posts from this blog

Florida Star v. B. J. F.

Danny Elfman

Retrieve a Users Dashboard in Tumblr with R and TumblR. Oauth Issues