Two number systems and one application Amr Elmasry 1) and Claus Jensen 2) Jyrki Katajainen 1) 1) University of Copenhagen 2) The Royal Library These slides are available at http://www.cphstl.dk c � Performance Engineering Laboratory ARCO meeting, Malm¨ o, 5 April 2011 (1)
Application min 1 element per node Representation of a priority queue: An ordered collection of pointer- based perfect binary heaps, 2 i +1 − 1 elements for i = 0 , 1 , . . . Operations: find - min , insert , borrow (What is this?), delete (Is delete - min missing?), meld Credit: [Williams 1964] c � Performance Engineering Laboratory ARCO meeting, Malm¨ o, 5 April 2011 (2)
Number system Digit set: d i ∈ { 0 , 1 , . . . } � ( d 0 least significant, Representation of a number: � d 0 , d 1 , . . . , d ℓ − 1 d ℓ − 1 � = 0) Weight set: { w i | i ∈ { 0 , 1 , . . . }} ℓ − 1 � Decimal value: d i × w i i =0 Operations: increment , decrement , addition , cut , catenation c � Performance Engineering Laboratory ARCO meeting, Malm¨ o, 5 April 2011 (3)
Some number systems Decimal: d i ∈ { 0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 } ; w i = 10 i [al-Khw¯ arizm ¯ ı 825] Unary: d i ∈ { 1 } ; w i = 1 Binary: d i ∈ { 0 , 1 } ; w i = 2 i Redundant binary: d i ∈ { 0 , 1 , 2 } ; w i = 2 i Skew binary: d i ∈ { 0 , 1 , 2 } ; w i = 2 i +1 − 1 Regular binary: d i ∈ { 0 , 1 , 2 } ; w i = 2 i ; Every string of digits is of the � ∗ [Clancy & Knuth 1977] � 0 | 1 | 01 ∗ 2 form Zeroless regular: d i ∈ { 1 , 2 , 3 } ; w i = 2 i ; Every string of digits is of � ∗ [Brodal 1995] � 1 | 2 | 12 ∗ 3 the form c � Performance Engineering Laboratory ARCO meeting, Malm¨ o, 5 April 2011 (4)
Link (1) digit d i at position i (1) d i heaps of size w i (2) increment (2) insert (3) decrement (3) borrow (4) addition (4) meld (5) digit transfer 2 w i +1 = w i +1 (5) siftdown (6) d i = O (1) ∀ i (6) O (lg n ) heaps; n : #elements Credit: [Vuillemin 1978] c � Performance Engineering Laboratory ARCO meeting, Malm¨ o, 5 April 2011 (5)
Canonical skew � of digits, A positive integer n is represented as a string � d 0 , d 1 , . . . , d ℓ − 1 least-significant digit first, such that • d i ∈ { 0 , 1 , 2 } ∀ i ∈ { 0 , 1 , . . . , ℓ − 1 } , and d ℓ − 1 � = 0, • if d j = 2, then d i = 0 ∀ i ∈ { 0 , 1 , . . . , j − 1 } , • w i = 2 i +1 − 1 ∀ i ∈ { 0 , 1 , . . . , ℓ − 1 } , and ℓ − 1 � • the decimal value of n is d i w i . i =0 Example: Unique representation of integer 52 10 position i 0 1 2 3 4 digit d i 0 2 0 1 1 weight w i 1 3 7 15 31 Credit: [Myers 1983] c � Performance Engineering Laboratory ARCO meeting, Malm¨ o, 5 April 2011 (6)
Increment/insert Algorithm increment ( � d 0 , d 1 , . . . , d ℓ − 1 � ) (1) siftdown 1: let d j be the first non-zero digit, if any 2: if d j exists and d j = 2 3: d j ← 0 4: increase d j +1 by 1 (1) 5: else (2) Add a new node to the 6: increase d 0 by 1 (2) collection Extra: Update the min pointer, if necessary (3) at most 2 digit changes (3) O (lg n ) worst-case time; n : #elements Credit: [Bansal et al. 2003] c � Performance Engineering Laboratory ARCO meeting, Malm¨ o, 5 April 2011 (7)
Decrement/borrow Algorithm decrement ( � d 0 , d 1 , . . . , d ℓ − 1 � ) � is not empty (1) Remove the root of the 1: assert � d 0 , d 1 , . . . , d ℓ − 1 smallest heap 2: let d j be the first non-zero digit (2) Add its subtrees, if any, to 3: decrease d j by 1 (1) the collection 4: if j � = 0 5: d j − 1 ← 2 (2) (3) at most 2 digit changes (3) O (1) worst-case time Problem: The min pointer may be invalidated! Solution: Swap the root with the next root, or with its own left child before the removal c � Performance Engineering Laboratory ARCO meeting, Malm¨ o, 5 April 2011 (8)
Delete remove (1) Borrow a node (2) Replace with (3) Perform siftdown or siftup for (4) Update the min pointer, if necessary (5) O (lg n ) worst-case time; n : #elements c � Performance Engineering Laboratory ARCO meeting, Malm¨ o, 5 April 2011 (9)
Summary Structure Array-based Canonical Regular Operation binary heap skew skew find - min O (1) O (1) O (1) O (lg n ) O (lg n ) O (1) insert O (1) O (1) O (1) borrow O (lg n ) O (lg n ) O (lg n ) delete O ((lg n ) 2 ) O ((lg m ) 2 ) meld O ( n ) worst-case performance; m ≤ n Open: Is faster decrease or meld possible? (Amortized bounds match- ing those achievable for Fibonacci heaps can be obtained, except for decrease ; this is not shown in our paper though.) c � Performance Engineering Laboratory ARCO meeting, Malm¨ o, 5 April 2011 (10)
Regular skew Cost of a digit change: O ( j ) at position j Discretization: Initially, j bricks at position j , i.e. b j = j Digit set: d i ∈ { 0 , 1 , 2 } ∀ i ; when b k > 0, d k is said to form a wall (1 or 2) of b k bricks Incremental digit changes: Remove some bricks from some walls in addition to the normal actions; do not transfer digits across any walls Representation of a number: Otherwise an integer is represented as in any skew system Credit: [Carlsson et al. 1988] c � Performance Engineering Laboratory ARCO meeting, Malm¨ o, 5 April 2011 (11)
Regularity conditions Preceding 0: 02 or 01 or 02 ( d 0 can be a 2) Absorbing 0: 21 ∗ 0 or 21 ∗ 0, this 0 should not be preceding ( d ℓ − 1 can be a 2 or 2) Critical 0: If d k forms a wall, then (i) ∃ j < k − 1 such that d j = 0, and b k ≤ j + 1, or (ii) ∃ j < k − 1 such that d j = 0, d j is absorbing, and b k ≤ j Example: One possible representation of integer 143 10 position i 0 1 2 3 4 5 critical 0 d 1 d 2 digit d i 2 0 0 1 0 2 variable b i 1 3 c � Performance Engineering Laboratory ARCO meeting, Malm¨ o, 5 April 2011 (12)
Increment/insert Algorithm transfer ( � d 0 , d 1 , . . . , d ℓ − 1 � , j ) 1: assert d j = 2 2: d j ← 0 3: increase d j +1 by 1 (1) 4: b j +1 ← j + 1 (2) (1) Add a heap to the collection (2) Initiate siftdown (3) Advance siftdown one level Algorithm increment ( � d 0 , d 1 , . . . , d ℓ − 1 � ) downwards (or do nothing if 1: let d k be the first wall, if any the heap order has already been 2: let d j be the first 2 for which b j = 0, if any reestablished) 3: if d j exists and ( d k does not exist or j < k ) Extra: Update the min pointer, 4: transfer ( � d 0 , d 1 , . . . , d ℓ − 1 � , j ) if necessary 5: reduce b j +1 by 1 (3) 6: else c � Performance Engineering Laboratory ARCO meeting, Malm¨ o, 5 April 2011 (13) 7: increase d 0 by 1 (1)
Proof of correctness Case 1 of 12: d j is the first digit that equals 2, is not a wall, precedes any wall, j � = 0, d j +1 = 0, and d j +1 is critical. In such a case, a transfer is initiated at d j . Before the operation, d j − 1 = 0. After the operation, d j = 0 and d j +1 becomes a wall with b j +1 = j . At this point, d j − 1 is the critical 0 for the wall d j +1 . . . . Case 2 of 12: d j is the first digit that equals 2, is not a wall, precedes any wall, j � = 0, d j +1 = 0, and d j +1 is not critical. . . . Case 3 of 12: d j is the first digit that equals 2, is not a wall, precedes any wall, j � = 0, and d j +1 = 1. . . . Case 4 of 12: d 0 = 2, d 1 = 0, and d 1 is critical. For such a case, the created wall d 1 is immediately dismantled. . . . Case 5 of 12: . . . c � Performance Engineering Laboratory ARCO meeting, Malm¨ o, 5 April 2011 (14)
Addition/meld Algorithm addition ( � d 0 , . . . , d k − 1 � , � e 0 , . . . , e ℓ − 1 � ) 1: assert k ≤ ℓ 2: for i ∈ { 0 , 1 , . . . , k − 1 } (1) Finish siftdown , if any 3: repeat d i times (2) Add a heap to the collection 4: if b i > 0 5: reduce b i to 0 (1) 6: arbitrary-increment ( � e 0 , . . . , e ℓ − 1 � , i ) (2) 7: return � e 0 , e 1 , . . . , e ℓ − 1 � (3) O ((lg m ) 2 ) worst-case time; (3) O ( k ) digit changes m, n : #elements, m ≤ n c � Performance Engineering Laboratory ARCO meeting, Malm¨ o, 5 April 2011 (15)
Further reading Elmasry, Jensen, and Katajainen, Two skew number systems and one application, Theory of Computing Systems , (invited) Elmasry, Jensen, and Katajainen, Strictly-regular number system and data structures, Proceedings of 12th Scandinavian Symposium and Workshops on Algorithm Theory , Lecture Notes in Computer Science, Springer-Verlag (2010) c � Performance Engineering Laboratory ARCO meeting, Malm¨ o, 5 April 2011 (16)
Recommend
More recommend