Number Systems and Data structures Amr Elmasry A. Elmasry, C. Jensen and J. Katajainen, The strictly-regular number system and worst-case efficient data structures. A. Elmasry, C. Jensen and J. Katajainen, The magic of a number system. TexPoint fonts used in EMF. Read the TexPoint manual before you delete this box.: AAA
Elmasry, Amr 2 Number Systems and Data Structures Outline Some existing number systems Properties Data-structural applications The strictly-regular system Properties Application The quinary-skew system Properties Application
Elmasry, Amr 3 Number Systems and Data Structures What is a Number System? x = b-ary w j = b j w j = b j+1 – 1 (skew) Examples: Binary: d j є {0,1}, w j = 2 j Redundant binary: d j є {0,1,2}, w j = 2 j (Canonical) skew binary: The first non-zero digit may be a 2. 0* (ε | 2) (0|1)*, w j = 2 j+1 – 1 [Myers 83] Redundant-regular (RR) binary: Every 2 is preceded by at least one 0. (0 | 1 | 01*2)* [Clancy & Knuth 77]
Elmasry, Amr 4 Number Systems and Data Structures Operations Extended-RR system: d j є {0,1,2,3}.Every 3 is preceded by at least one {0,1}, and every 0 is preceded by at least one {2,3}. [Clancy & Knuth 77 - Kaplan et al. 02] Zeroless systems: d j ≠ 0 Others: d k-1 = 2 Operations: increment(x,j): s val(x) + w j decrement(x,j): s val(x) – w j add(x,y): s val(x) + val(y) cut(x,j): cut x into two valid sequences. concatenate(x,y): concatenate x and z and return a valid sequence.
Elmasry, Amr 5 Number Systems and Data Structures Properties Increments involve amortized constant digit flips in binary system. But, worst-case constant digit flips for the RR system. Increments and decrements only at d 0 involve constant digit flips in the skew-binary system. Increments and decrements involve worst-case constant digit flips when using two back-to back RR system. Increments and decrements involve worst-case constant digit flips for the extended-RR system. The sum of digits of a k-digit number in the RR binary system is at most k. The sum of digits of a k-digit number in the zeroless-RR binary system is at most 2k. The sum of digits of a k-digit number in the extended-RR binary system is at most 2k. The value of a k-digit number in the zeroless RR binary system is at least 2 k -1 (the exponentiality property).
Elmasry, Amr 6 Number Systems and Data Structures How does it Work? Example: RR-binary system (least-significant digit first) Increment least significant bit + fix least significant 2 fix-carry: 2 x 0 (x+1) Keep track of a list of 2’s from least to most significant. For general increments use Brodal’s guides.
Elmasry, Amr 7 Number Systems and Data Structures From Number Systems to Data Structures Define the notion of the rank. There would be d j objects of rank j in the data structure. The size of an object of rank j is s j s j = w j perfect components In general, s j ≤ w j (for skew binomial queues 2 j ≤ s j ≤ 2 j+1 – 1 ) fix-carry resembles a join of b objects of rank j to one of rank j+1. fix-borrow resembles a split of an object of rank j to b objects of rank j-1.
Elmasry, Amr 8 Number Systems and Data Structures Worst-Case Efficient Data Structures Finger trees. [Guibas et al. 77] Using the RR-binary system: binomial queues with constant worst-case insertion cost. [Carlsson et al. 88] Using the skew-binary system: skew binomial queues with constant worst-case insertion cost. [Brodal & Okasaky 96] Using the zeroless-RR system: a priority queue that supports meld in constant worst-case cost. [Brodal 95] Using two RR-systems back-to-back with d j ≥ 2: both meld and decrease in constant worst-case cost. [Brodal 96] Using two RR-systems back-to-back: purely functional catenable deques in constant worst-case cost per operation. [Kaplan & Tarjan 95] Using the extended-RR system: Fat heaps. [Kaplan et al. 02] Using the extended-RR system: worst-case efficient priority queue with the working-set property. [Elmasry 06]
Elmasry, Amr 9 Number Systems and Data Structures The Strictly-Regular System Every 2 is preceded by a 0, and every 0 is preceded by a 2, except for the last block that starts with a 0. (1 + | 01*2)* (ε | 01*) Primitives: fix-carry: 2 x 0 (x+1) fix-borrow: 0 x 2 (x-1) Deciding which digit to fix depends on the value of the current digit (digit under operation) and the next extreme digit (0 or 2). Properties: Increments, decrements, catenations and cuts involve worst-case constant digit flips. All can be implemented in worst-case constant time once you know the closest extreme digit. Addition of k-digit numbers involve k+O(1) carry propagations. The sum of digits of a k-digit number is either k or k-1 (compactness). The value of a k-digit number is at least Φ k (exponentiality).
Elmasry, Amr 10 Number Systems and Data Structures Strictly-Regular Trees The rank of a node equals the number of its children. The ranks of the children of a node form a strictly-regular sequence. Properties: A strictly-regular tree of rank r has at least 2 r nodes. Adding a subtree with a non-larger rank or detaching a subtree is done in worst-case constant time if one can access the trees whose ranks resemble the adjacent extreme digits. Splitting and concatenating sequences of children can be done worst-case efficiently. Application: Improving the constant factors for the number of comparisons of data structures. For meldable heaps (no decrease) delete-min requires at most 2 lg n +O(1) comparisons instead of 7 lg n + O(1).
Elmasry, Amr 11 Number Systems and Data Structures The Skew Five-Symbol Number System d j є {0,1,2,3,4}, w j = 2 j+1 – 1 increment by 1: 1.Increase d 0 by 1. 2.Find the smallest j where d j є {3,4}. 3.If j exists, perform a fix for d j. fix d j : 1.Decrease d j by 3. 2.Increase d j+1 by 1. 3.If j≠0, Increase d j -1 by 2. A fix does not change the value of a number.
Elmasry, Amr 12 Number Systems and Data Structures Properties Numbers from one to thirty: 1, 2, 01, 11, 21, 02, 12, 22, 03, 301, 111, 211, 021, 121, 221, 031, 302, 112, 212, 022, 122, 222, 032, 303,113, 2301, 0401, 3111,1211, 2211 A decrement is implemented as an inverse of an increment, by using an undo stack to remember where the fixes were performed. Properties: (Any number is composed of a sequence of blocks each ends with a 3 or 4 and the last subsequence is the tail) The body of a block ending with 4 comprises either 0 or 12*1. The body of a block ending with 3 comprises either 0, 12*1, or 2*. Each 4, 23 and 33 is followed by either 0 or 1. There can be at most one 0 in the tail, which must be its first digit. The sum of digits of a k-digit number is at most 2k.
Elmasry, Amr 13 Number Systems and Data Structures Application The fix is done in constant worst-case time: The minimum root is detached from its tree and attached as the root of the other two trees forming a larger tree. Application: A priority queue can be implemented as a forest of complete binary trees having worst-case costs: O(1) insert and O(lg n) deletemin.
Elmasry, Amr 14 Number Systems and Data Structures Conclusions and Future Work: Conclusions Number systems are interesting. The connection between number systems and worst-case-efficient data structures is efficacious. Two new number systems were introduced. Future work New number systems with applications. More applications for the strictly-regular system. Extending the strictly-regular system to be ternary (b-ary). Is it possible to implement general increments and decrements in worst-case constant cost for the strictly-regular binary system? Supporting decrements for the skew five-symbol system without the usage of the undo stack.