ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
OrderMaintenanceList.h File Reference

The order maintenance of exercise 8.11 of Morin (his Sequence), beyond the book: a list of elements with addAfter( e , v ), which adds a new element right after \( e \) (at the front if \( e \) is nil), in \( O( 1 ) \) amortized time, remove( e ), in \( O( 1 ) \) time, and testBefore( a , b ), whether \( a \) comes before \( b \), in \( O( 1 ) \) time. More...

#include "ADS/Contract.h"
#include <array>
#include <cmath>
#include <cstddef>
#include <cstdint>
#include <utility>
#include "ADS/sequence/impl/OrderMaintenanceList.tpp"

Go to the source code of this file.

Classes

class  ADS::OrderMaintenanceList< T >
 a list of items of type T whose order between two elements is tested in \( O( 1 ) \) by labels on two levels More...

Namespaces

namespace  ADS

Detailed description

The order maintenance of exercise 8.11 of Morin (his Sequence), beyond the book: a list of elements with addAfter( e , v ), which adds a new element right after \( e \) (at the front if \( e \) is nil), in \( O( 1 ) \) amortized time, remove( e ), in \( O( 1 ) \) time, and testBefore( a , b ), whether \( a \) comes before \( b \), in \( O( 1 ) \) time.

It is the structure of Bender, Cole, Demaine, Farach-Colton and Zito, a list labeling on two levels (see Sequences by rank and order maintenance):

  • the elements are cut into runs of consecutive elements, the buckets, of at most \( B = 64 \), and each element has a local label below \( 2^{63} \), which increases along its bucket; a new element takes the middle of the gap between its neighbors in the bucket, and the bucket is labeled again with equal gaps, in \( O( B ) \), when that gap is empty; a bucket of \( B + 1 \) elements splits into two halves;
  • the buckets have labels below \( 2^{63} \) too, which increase along the list of the buckets; a new bucket takes the middle of the gap after the one it follows, and when the gap is empty the smallest range of \( 2^i \) labels aligned on \( 2^i \) around it whose buckets, the new one included, are at most \( ( 2 / T )^i \) is labeled again with equal gaps, \( T = 1.4 \) (their first algorithm), which writes \( O( \log m ) \) amortized labels for \( m \) buckets;
  • testBefore() compares the labels of the buckets, and the local labels when the bucket is the same.

A split happens once in \( B / 2 \) insertions into a bucket at least, which pay its \( O( B + \log m ) \) labels, since \( \log_2 m < 63 < B \); a removal unlinks its element, and its bucket when it empties, with no relabeling. Hence an insertion costs \( O( 1 ) \) amortized and a removal \( O( 1 ) \) in the worst case. The labels of the buckets hold at most \( ( 2 / T )^{63} \), about \( 5.8 \cdot 10^9 \), buckets. The scapegoat tree whose labels encode the paths, the solution that the exercise has in mind, takes \( O( \log n ) \) amortized (see the theory page). An element stays valid until it is removed.

The contracts [see Contract.h]: the elements given are elements of this list, which holds fewer than \( 5.8 \cdot 10^9 \) buckets; the invariant, checked on the buckets that an operation touches, is that the labels increase, the buckets hold from 1 to \( B \) elements and the elements point to their bucket. The operations are defined in OrderMaintenanceList.tpp.

See also
M. A. Bender, R. Cole, E. D. Demaine, M. Farach-Colton, J. Zito, Two simplified algorithms for maintaining order in a list, Algorithms - ESA 2002, Lecture Notes in Computer Science 2461, 152-164, 2002, doi:10.1007/3-540-45749-6_17
P. F. Dietz, D. D. Sleator, Two algorithms for maintaining order in a list, Proceedings of the 19th ACM Symposium on Theory of Computing (STOC), 365-372, 1987, doi:10.1145/28395.28434
P. Morin, Open Data Structures (in C++), Edition 0.1G, 2013, https://opendatastructures.org/ods-cpp.pdf, exercise 8.11
Author
Donato Meoli
Dipartimento di Informatica
Università di Pisa