Iacono's working set structure

From HandWiki
Revision as of 22:18, 13 June 2021 by imported>Jport (url)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Iacono's working set data structure
Invented 2001
Invented by John Iacono
Asymptotic complexity
in big O notation
Space O(n)
Search O(log w(x))
Insert O(log n)
Delete O(log n)

In computer science, Iacono's working set structure[1] is a comparison based dictionary. It supports insertion, deletion and access operation to maintain a dynamic set of [math]\displaystyle{ n }[/math] elements. The working set of an item [math]\displaystyle{ x }[/math] is the set of elements that have been accessed in the structure since the last time that [math]\displaystyle{ x }[/math] was accessed (or inserted if it was never accessed). Inserting and deleting in the working set structure takes [math]\displaystyle{ O(\log n) }[/math] time while accessing an element [math]\displaystyle{ x }[/math] takes [math]\displaystyle{ O(\log w(x)) }[/math]. Here, [math]\displaystyle{ w(x) }[/math] represents the size of the working set of [math]\displaystyle{ x }[/math].

Structure

An example of a search for [math]\displaystyle{ x }[/math] in the working set structure. After finding [math]\displaystyle{ x }[/math], it is removed from [math]\displaystyle{ T_4 }[/math] and inserted into [math]\displaystyle{ T_1 }[/math]. Finally, a shift from 1 to 4 is performed in which an element is removed from [math]\displaystyle{ T_i }[/math] and inserted into [math]\displaystyle{ T_{i+1} }[/math] for [math]\displaystyle{ 1\leq i \lt 4 }[/math].

To store a dynamic set of [math]\displaystyle{ n }[/math] elements, this structure consists of a series of Red–black trees, or other Self-balancing binary search trees [math]\displaystyle{ T_1, T_2, \ldots, T_k }[/math], and a series of deques (Double-ended queues) [math]\displaystyle{ Q_1, Q_2, \ldots Q_k }[/math], where [math]\displaystyle{ k = \lceil \log\log n\rceil }[/math]. For every [math]\displaystyle{ 1\leq i\leq k }[/math], tree [math]\displaystyle{ T_i }[/math] and deque [math]\displaystyle{ Q_i }[/math] share the same contents and pointers are maintained between their corresponding elements. For every [math]\displaystyle{ i \lt k }[/math], the size of [math]\displaystyle{ T_i }[/math] and [math]\displaystyle{ Q_i }[/math] is [math]\displaystyle{ 2^{2^i} }[/math]. Tree [math]\displaystyle{ T_k }[/math] and deque [math]\displaystyle{ Q_k }[/math] consist of the remaining elements, i.e., their size is [math]\displaystyle{ n - \sum_{i=1}^{k-1} 2^{2^i} }[/math]. Therefore, the number of items in all trees and the number of elements in all deques both add up to [math]\displaystyle{ n }[/math]. Every element that has been inserted in the data structure is stored in exactly one of the trees and its corresponding deque.

Working set Invariant

In the deques of this structure, elements are kept in sorted order according to their working set size. Formally, element [math]\displaystyle{ x }[/math] lies after [math]\displaystyle{ y }[/math] in deque [math]\displaystyle{ Q_i }[/math] if and only if [math]\displaystyle{ w(x)\lt w(y) }[/math]. Moreover, for every [math]\displaystyle{ 1\leq i \lt k }[/math], the elements in deque [math]\displaystyle{ Q_i }[/math] have a smaller working sets than the elements in deque [math]\displaystyle{ Q_{i+1} }[/math]. This property is referred to as the Working set invariant. Every operation in the data structure maintains the Working set invariant.

Operations

The basic operation in this structure is called shift from [math]\displaystyle{ h }[/math] to [math]\displaystyle{ j }[/math], where [math]\displaystyle{ h }[/math] and [math]\displaystyle{ j }[/math] are indices of some trees in the structure. Two cases are considered in a shift from [math]\displaystyle{ h }[/math] to [math]\displaystyle{ j }[/math]: If [math]\displaystyle{ h\lt j }[/math], then for every [math]\displaystyle{ h\leq i \lt j }[/math], taken in increasing order, an item is dequeued from [math]\displaystyle{ Q_i }[/math] and enqueued into [math]\displaystyle{ Q_{i+1} }[/math]. The corresponding item is deleted from [math]\displaystyle{ T_i }[/math] and inserted into [math]\displaystyle{ T_{i+1} }[/math]. The running time of this operation is [math]\displaystyle{ O(\sum_{i=h}^{j} \log |T_i|) = O(\sum_{i=h}^{j} \log 2^{2^i}) = O(2^ j) }[/math]. Analogously, if [math]\displaystyle{ j\lt h }[/math], then for every [math]\displaystyle{ j \lt i \leq h }[/math], taken in decreasing order, an item is dequeued from [math]\displaystyle{ Q_i }[/math] and enqueued into [math]\displaystyle{ Q_{i-1} }[/math]. The corresponding item is deleted from [math]\displaystyle{ T_i }[/math] and inserted into [math]\displaystyle{ T_{i-1} }[/math]. The running time of this operation is [math]\displaystyle{ O(\sum_{i=j}^{h} \log |T_i|) = O(\sum_{i=j}^{h} \log 2^{2^i}) = O(2^ h) }[/math]. Regardless of the case, after a shift operation, the size of [math]\displaystyle{ T_h }[/math] decreases by one whereas the size of [math]\displaystyle{ T_j }[/math] increases by one. Since that elements in the deques are sorted with respect to their working sets sizes, a shift operation maintains the Working set invariant.

Search

To search for an element [math]\displaystyle{ x }[/math], search for [math]\displaystyle{ x }[/math] in [math]\displaystyle{ T_1, T_2, \ldots T_k }[/math], in increasing order, until finding a tree [math]\displaystyle{ T_j }[/math] containing [math]\displaystyle{ x }[/math]. If no tree is found, the search is unsuccessful. If [math]\displaystyle{ x }[/math] is found, it is deleted from [math]\displaystyle{ T_j }[/math] and then inserted into [math]\displaystyle{ T_1 }[/math], i.e., it is moved to the front of the structure. The search finishes by performing a shift from [math]\displaystyle{ 1 }[/math] to [math]\displaystyle{ j }[/math] which restores the size of every tree and every deque to their size prior to the search operation. The running time of this search is [math]\displaystyle{ O(\sum_{i=1}^{j} \log 2^{2^i}) = O(2^ j) }[/math] if the search was successful, or [math]\displaystyle{ O(\sum_{i=j}^{k} \log 2^{2^i}) = O(2^k) = O(\log n) }[/math] otherwise. By the Working set property, every element in trees [math]\displaystyle{ T_1, T_2, \ldots, T_{j-1} }[/math] belongs to the working set of [math]\displaystyle{ x }[/math]. In particular, every element in [math]\displaystyle{ T_{j-1} }[/math] belongs to the working set of [math]\displaystyle{ x }[/math] and hence, [math]\displaystyle{ w(x) \gt |T_{j-1}| = 2^{2^{j-1}} }[/math]. Thus, the running time of a successful search is [math]\displaystyle{ O(2^j) = O(\log 2^{2^{j-1}}) = O(\log w(x)) }[/math].

Insert

Inserting an element [math]\displaystyle{ x }[/math] into the structure is performed by inserting [math]\displaystyle{ x }[/math] into [math]\displaystyle{ T_1 }[/math] and enqueuing it into [math]\displaystyle{ Q_1 }[/math]. Insertion is completed by performing a shift from [math]\displaystyle{ 1 }[/math] to [math]\displaystyle{ k }[/math]. To avoid overflow, if [math]\displaystyle{ |T_k| = 2^{2^k} }[/math] before the shift, i.e., if the last tree is full, then [math]\displaystyle{ k }[/math] is incremented and a new empty [math]\displaystyle{ T_k }[/math] and [math]\displaystyle{ Q_k }[/math] is created. The running time of this operation is dominated by the shift from [math]\displaystyle{ 1 }[/math] to [math]\displaystyle{ k }[/math] whose running time is [math]\displaystyle{ O(2^k) = O(2^{\log\log n}) = O(\log n) }[/math]. Since element [math]\displaystyle{ x }[/math], whose working set is the smallest, is enqueued in [math]\displaystyle{ Q_1 }[/math], the Working set invariant is preserved after the shift.

Delete

Deleting an element [math]\displaystyle{ x }[/math] is done by searching for [math]\displaystyle{ x }[/math] on each tree in the structure, in increasing order, until finding a tree [math]\displaystyle{ T_j }[/math] that contains it (if non is found the deletion is unsuccessful). Item [math]\displaystyle{ x }[/math] is deleted from [math]\displaystyle{ T_j }[/math] and [math]\displaystyle{ Q_j }[/math]. Finally, a shift from [math]\displaystyle{ k }[/math] to [math]\displaystyle{ j }[/math] maintains the size of [math]\displaystyle{ T_j }[/math] equal to [math]\displaystyle{ 2^{2^j} }[/math]. The running time of this operation is [math]\displaystyle{ O(2^k) = O(\log n) }[/math]. The working set invariant is preserved as deleting an element does not change the order of the working set of the elements.

Discussion

Splay trees are self adjusting search trees introduced by Sleator and Tarjan[2] in 1985. Using restructuring heuristic, splay trees are able to achieve insert and delete operations in [math]\displaystyle{ O(\log n) }[/math] amortized time, without storing any balance information at the nodes. Moreover, the Working Set Theorem for splay trees states that the cost to access an element in a splay tree is [math]\displaystyle{ O(\log w(x)) }[/math] amortized. Iacono's workings set structure obtains the same running time for search, insert and delete in the worst-case. Therefore, offering an alternative to splay trees.

References

  1. Iacono, John (2001). "Alternatives to splay trees with O(log n) worst-case access times". Proceedings of the Twelfth Annual ACM-SIAM Symposium on Discrete Algorithms: 516–522. http://cglab.ca/~morin/teaching/5408/refs/i2001.pdf. 
  2. Sleator, Daniel D.; Tarjan, Robert E. (1985), "Self-Adjusting Binary Search Trees", Journal of the ACM 32 (3): 652–686, doi:10.1145/3828.3835, https://www.cs.cmu.edu/~sleator/papers/self-adjusting.pdf