Post on 14-Apr-2018
7/27/2019 Numeral Systems and Data Structures
1/34
Amr Elmasry Number systems and Data Structures (1)
Number Systems
and Data Structures
Amr Elmasry
Alexandria University
7/27/2019 Numeral Systems and Data Structures
2/34
Amr Elmasry Number systems and Data Structures (2)
Outline
Some existing number systems:PropertiesData-structural applications
The extended regular system:Properties
Implementation
The strictly regular system:PropertiesImplementation
In-place counters:
PropertiesImplementation
Open issues and conclusions
7/27/2019 Numeral Systems and Data Structures
3/34
Amr Elmasry Number systems and Data Structures (3)
References
A. Elmasry and J. Katajainen, In-place binary counters, 38th Inter-
national Symposium on Mathematical Foundations of Computer
Science (2013), Klosterneuburg, Austria, LNCS 8087, 349360.
A. Elmasry and J. Katajainen, Worst-case optimal priority queues via
extended regular counters, 7th International Computer Science
Symposium in Russia (2012), Nizhny Novgorod, Russia, LNCS
7353, 130142.
A. Elmasry, C. Jensen and J. Katajainen, Strictly regular number
system and data structures, 12th Scandinavian Symposium and
Workshops on Algorithm Theory (2010), Bergen, Norway, LNCS
6139, 2637.
7/27/2019 Numeral Systems and Data Structures
4/34
Amr Elmasry Number systems and Data Structures (4)
Why another number system?
In the decimal number system a single increment may incur many
digit changes!
1 1 1 1 1 1 1 1
9 9 9 9 9 9 9 9+ 1
1 0 0 0 0 0 0 0 0
The binary number system has the same problem.
We look for a system where increment and decrement of a digit can
be done in constant number of digit flips.
7/27/2019 Numeral Systems and Data Structures
5/34
Amr Elmasry Number systems and Data Structures (5)
What is the most economical system?
Let d be a positive integer.
rep(d):
d0, d1, . . . , dk1
(d0 is the least significant digit)
val(d):k1
i=0
di wi
b-ary: wi = bi
Develop a number system for which
| {di | i {0, 1, . . . , k 1}} | is as small as possible for all d;
an increment at any position i (increment(d, i)) generates as few
digit changes as possible in the worst case; and
a decrement at any position i (decrement(d, i)) generates as few
digit changes as possible in the worst case.
7/27/2019 Numeral Systems and Data Structures
6/34
Amr Elmasry Number systems and Data Structures (6)
Full repertoire of operations
increment(d, i): Assert that i {0, 1, . . . , k}. Perform ++di resulting in
d, i.e. val(d) = val(d) + wi. Make d
valid without changing its
value. We refer to increment(d, 0) as ++.
decrement(d, i): Assert that i {0, 1, . . . , k 1}. Perform --di resulting
in d, i.e. val(d) = val(d) wi
. Make d valid without changing
its value. We refer to decrement(d, 0) as --.
cut(d, i): Cut rep(d) into two valid sequences having the same value as
the numbers corresponding to d0, d1, . . . , di1 and
di, di+1, . . . , dk1
.
concatenate(d,d): Concatenate rep(d) and rep(d) into one valid se-
quence that has the same value as
d0, d1, . . . , dk1, d0, d1, . . . , dk1
.
add(d,d): Construct a valid sequence d such that val(d) = val(d) +
val(d).
7/27/2019 Numeral Systems and Data Structures
7/34
Amr Elmasry Number systems and Data Structures (7)
Classic Number systems
Binary: di {0, 1}; wi = 2i
Canonical skew binary: di {0, 1}; wi = 2i+1 1 and the first non-
zero digit may be a 2. Every sequence of digits is of the form0(|2)(0|1). [Myers 83]
Redundant binary: di
{0, 1, 2}; wi
= 2i
Redundant regular (RR): di {0, 1, 2}; wi = 2i; Every sequence of
digits is of the form
0 | 1 | 012
. [Clancy and Knuth 77]
Extended regular system: di {0, 1, 2, 3}; Every 3 is preceded by atleast one {0, 1} before the previous 3 or running out of digits, and
every 0 is preceded by at least one {2, 3} before the previous 0 orrunning out of digits. [Clancy and Knuth 77; Kaplan, Shafrir andTarjan 02]]
Zeroless regular: di {1, 2, 3}; wi = 2i; Every sequence of digits is
of the form
1 | 2 | 123
[Brodal 95]
7/27/2019 Numeral Systems and Data Structures
8/34 Amr Elmasry Number systems and Data Structures (8)
Properties
Increments involve constant digit flips for the RR system.
Increments and decrements only at d0 involve constant digit flips
in the skew-binary system.
Increments and decrements involve 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 extended RR binary
system is at most 2k.
The value of a k-digit number in the zeroless RR binary system
is at least 2k 1 (the exponentiality property).
7/27/2019 Numeral Systems and Data Structures
9/34 Amr Elmasry Number systems and Data Structures (9)
From number systems to data structures
Define the notion of the rank.
There would be dj objects of rank j in the data structure.
The size of an object of rank j is sj
sj = wj ? perfect components. In general, sj wj(for skew binomial queues 2j sj 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.
7/27/2019 Numeral Systems and Data Structures
10/34 Amr Elmasry Number systems and Data Structures (10)
Data-structural applications (1)
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 and 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: both meld and decrease in
constant worst-case cost. [Brodal 96]
7/27/2019 Numeral Systems and Data Structures
11/34 Amr Elmasry Number systems and Data Structures (11)
Data-structural applications (2)
Using two RR-systems back-to-back: purely functional catenable
deques in constant worst-case cost. [Kaplan and 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]
Using the strictly regular system: improving Brodals priority queue
with constant meld. [Elmasry et al. 10]
Using the extended RR system: improving Brodals priority queue
with constant meld and decrease. [Elmasry and Katajainen 12]
7/27/2019 Numeral Systems and Data Structures
12/34 Amr Elmasry Number systems and Data Structures (12)
Application: Binomial queues
N = 11 10 = 1011 2 292
7
6
19
48
10 14
9 26
51 min
Primitives
join:
+
Br Br Br+1split:
+
Br+1 Br Br
Worst-case bounds
find-min: O(1)
insert/borrow: (lg N) O(1)
meld: two queues of size M,N,
O(lg M + lg N)
delete-min: meld
7/27/2019 Numeral Systems and Data Structures
13/34 Amr Elmasry Number systems and Data Structures (13)
Outline
Some existing number systems:PropertiesData-structural applications
The extended regular system:Properties
Implementation
The strictly regular system:PropertiesImplementation
In-place counters:
PropertiesImplementation
Open issues and conclusions
7/27/2019 Numeral Systems and Data Structures
14/34 Amr Elmasry Number systems and Data Structures (14)
Extended regular system
Blocks: 123 023 210 310
0 and 3 are distinguishing digits for the blocks
fix-carry : 3 x 1 (x + 1) (no change in value)
Example: d = 1222232221031121101
increment(d, 2) : d = 1232232221031121101 ???
fix-carry(d, 5) : d = 1232213221031121101
increment(d, 4) : d = 1232313221031121101 ???
fix-carry(d, 4) : d = 1232123221031121101
7/27/2019 Numeral Systems and Data Structures
15/34 Amr Elmasry Number systems and Data Structures (15)
Extended regular system
fix-borrow : 0 x 2 (x 1) (no change in value)
decrement(d, 15) : d = 1232123221031120101 ???
fix-borrow(d, 17) : d = 123212322103112012
decrement(d, 11), decrement(d, 11), decrement(d, 11) :d = 123212322100112012
fix-borrow(d, 11) : d = 123212322102012012
A fix-carry corresponds to combining (joining) two structures of the
same size (rank) into a bigger one.
A fix-borrow corresponds to dividing (splitting) a structure into two
smaller structures of equal size (rank).
7/27/2019 Numeral Systems and Data Structures
16/34 Amr Elmasry Number systems and Data Structures (16)
Implementation
Every digit has a pointer to the distinguished digit of its block.
If the digit is not in a block its pointer points to an arbitrary digit.
It works because:
1. An unnecessary fix is not harmful (when a block is destroyed, we
do not need to change the pointers).
2. A block extends by one digit from the front.
3. A newly created block consists of two digits.
7/27/2019 Numeral Systems and Data Structures
17/34
Amr Elmasry Number systems and Data Structures (17)
Increments [Elmasry and Katajainen 12]
Algorithm fix-carry(d,j)
1: assert 0 j k 1 and dj = 32: dj 13: ++dj+14: if dj+1 = 35: fj j + 1 // a new block of two digits6: else7: fj fj+1 // extending a possible block from the beginning
Algorithm increment(d, i)
1: if di = 32: fix-carry(d,i) // either this fix is executed3: j fi4: if j k 1 and dj = 3
5: fix-carry(d,j)6: ++di7: if di = 38: fix-carry(d,i) // or this fix
7/27/2019 Numeral Systems and Data Structures
18/34
Amr Elmasry Number systems and Data Structures (18)
Decrements [Elmasry and Katajainen 12]
Algorithm fix-borrow(d,j)
1: assert 0 j < k 1 and dj = 02: dj 23: --dj+14: if dj+1 = 05: fj j + 1 // a new block of two digits6: else7: fj fj+1 // extending a possible block from the beginning
Algorithm decrement(d, i)
1: if di = 02: fix-borrow(d,i) // either this fix is executed3: j fi4: if j < k 1 and dj = 0
5: fix-borrow(d,j)6: --di7: if i < k 1 and di = 08: fix-borrow(d,i) // or this fix
7/27/2019 Numeral Systems and Data Structures
19/34
Amr Elmasry Number systems and Data Structures (19)
Outline
Some existing number systems:PropertiesData-structural applications
The extended regular system:Properties
Implementation
The strictly regular system:PropertiesImplementation
In-place counters:
PropertiesImplementation
Open issues and conclusions
7/27/2019 Numeral Systems and Data Structures
20/34
Amr Elmasry Number systems and Data Structures (20)
Strictly regular system [Elmasry et al. 10]
Digits: di {0, 1, 2}; wi = 2i
Strict regularity: The sequence from the least-significant to the most-
significant digit is of the form
1+ | 012
| 01+
Extreme digits: 0 and 2
a) 1111111 yes
b) 11011211101 yes
c) 1201 no
d) 1110101 no
7/27/2019 Numeral Systems and Data Structures
21/34
Amr Elmasry Number systems and Data Structures (21)
Increment example
Notation: Digit di to be increased is displayed in red. da is the first
extreme digit after di, k is a non-negative integer, denotes any
combination of 1+ and 012 blocks, and any combination of
1+ and 012 blocks followed by at most one 01+ block.
Initial configuration: 0111211k
Action: di 2; da da 2; da+1 da+1 + 1
Final configuration: 0121021k
Remark: This is one of 19 cases considered in our correctness proof.
7/27/2019 Numeral Systems and Data Structures
22/34
Amr Elmasry Number systems and Data Structures (22)
General algorithm
fix-carry(d, i): Assert that di 2. Perform di di 2 and di+1
di+1 + 1.
Algorithm increment(d, i)
1: ++di2: Let db be the first extreme digit before di, db {0, 2, undefined}
3: Let da be the first extreme digit after di, da {0, 2, undefined}
4: if di = 3 or (di = 2 and db = 0)
5: fix-carry(d, i)
6: else if da = 2
7: fix-carry(d, a)
7/27/2019 Numeral Systems and Data Structures
23/34
Amr Elmasry Number systems and Data Structures (23)
Properties
Increments, decrements, catenations, and cuts involve O(1) digit
changes in the worst case
Addition of two k-digit numbers involve at most k carry propaga-
tions
The sum of digits of a k-digit number is either k or k 1 (com-
pactness property)
The value of a k-digit number is at least k 1 where is the
golden ratio (exponentiality property)
7/27/2019 Numeral Systems and Data Structures
24/34
Amr Elmasry Number systems and Data Structures (24)
Outline
Some existing number systems:PropertiesData-structural applications
The extended regular system:Properties
Implementation
The strictly regular system:PropertiesImplementation
In-place counters:
PropertiesImplementation
Open issues and conclusions
7/27/2019 Numeral Systems and Data Structures
25/34
Amr Elmasry Number systems and Data Structures (25)
In-place counters [Elmasry and Katajainen13]
Digits: (a) d0 is a basket of 1s,
call their number x; (b) there
may exist at most one such
that d = 2; (c) di {0, 1}
for all i = 0 and i =
Weights: wi = 2i
Rules: (a) d is of the form
((1(0|1)) x | ((1(0|1)) 20x
(b)k1
i=0 di lg(1 + val(d))
Operations: ++, --, and add
Our system is as the regular sys-
tem, but we only allow at mostone 2, except that x can become
as high as lg(1 + val(d)).
7/27/2019 Numeral Systems and Data Structures
26/34
Amr Elmasry Number systems and Data Structures (26)
Current knowledge
Binary system
Representation: k + O(lg k) bits
++/--: (k/w) worst-case time
add: k1, k2 bits, ((k1 + k2)/w)
worst-case time
Our in-place system
Representation: k + O(lg k) bits
++/--: O(1) worst-case time
add: k1, k2 bits, O((k1 + k2)/w)
worst-case time
Extended regular system
Representation: O(k) words
increment/decrement: O(1) digit
changes in the worst case
add: k1, k2 bits, O(min {k1, k2})digit changes in the worst
case (open how to use word-
level parallelism)
Known space-economic solu-
tions
analysed in the bit-probe
model
rely on Gray codes can-not be used in data-structural
applications [Rahman and
Munro 10]
7/27/2019 Numeral Systems and Data Structures
27/34
Amr Elmasry Number systems and Data Structures (27)
Model of computation
A word RAM with the operations available in the C pro-
gramming language; time means the number of word op-
erations executed:
an infinite array a for storing data
a constant number of variables
N = 14
w = 40123
10101110
workspace
01100011 14a
if a counter uses N bits, these must be kept in the firstN/w locations of a
the word size w lg(1 + N), where N is the problem
size.
7/27/2019 Numeral Systems and Data Structures
28/34
Amr Elmasry Number systems and Data Structures (28)
Actual representation
50 10 when only ++ is supported
1 0 1 1 0 b5 b4 b3 b2 b1 b0
= 6 (length of the representation)
x = 2 (size of the basket of 1s)carry = 1 (indicates that there is a 2)
= 2 (position of the 2, if any)
= 2 (number of zeros)
= 2 (normal mode)
7/27/2019 Numeral Systems and Data Structures
29/34
Amr Elmasry Number systems and Data Structures (29)
Key ideas
Basket x
not too bigk1
i=0 di
lg(1 + val(d))
not too small x > 0 if
val(d) = 0
First non-zero digit
position may be lost
1 0 0 0 1 x
11 0 0 0 0 (x + 1)
Modes
search mode: double speed
borrow mode: double speed
normal mode
1 0 0 0 0 x our
: position up to which the post-
poned borrows should be exe-
cuted
: position up to which the
search has reached when search-
ing for the first non-zero digit
7/27/2019 Numeral Systems and Data Structures
30/34
Amr Elmasry Number systems and Data Structures (30)
++ in our system
Algorithm ++
1: if = (normal mode)
2: if there is a 2 (ignore x)
3: fix it
4: else if no 2 and x 2
5: x x 2
6: ++d17: else (other modes)
8: + 2
9: x x + 1
1 0 2 0 0 2+ 1
1 1 0 0 0 3
7/27/2019 Numeral Systems and Data Structures
31/34
Amr Elmasry Number systems and Data Structures (31)
The idea for --
Implement -- as the reverse of ++. However, this approachfails when there is a long borrow sequence.
Solution:
Keep a basket of 1s (x)
Search for the first di, di = 0 and i 1, in double speed
Fix the borrows in double speed (until meets )
Take 1 from the basket in connection with every --.
Key: x #0s in the front of the first non-zero digit
Note: We should be able to support ++s when we are not
in the normal mode. Whenever > , it means that there
have been more --s than ++s since we switched to the
search mode.
7/27/2019 Numeral Systems and Data Structures
32/34
Amr Elmasry Number systems and Data Structures (32)
Outline
Some existing number systems:PropertiesData-structural applications
The extended regular system:Properties
Implementation
The strictly regular system:PropertiesImplementation
In-place counters:
PropertiesImplementation
Open issues and conclusions
7/27/2019 Numeral Systems and Data Structures
33/34
Amr Elmasry Number systems and Data Structures (33)
Open issues
How can you support additions, cuts, concatenations in the ex-tended regular system?
Do you know how to efficiently support increment and decrementwith fewer digits? We can do it with 3 digits, but we cannot
implement it except if operations are done on d0 or dk1.
Do you know how to efficiently support increment and decrementof d0 with the minimum number of bits? To represent numbersup to 2n 1, we know how to do that with n + 1 bits using Graycodes, it is impossible with n bits. For a practical implementation
with binary weights, we know how to do it with n + O(lg n) bits.
Using other weights (for example, ternary weights)
7/27/2019 Numeral Systems and Data Structures
34/34
Conclusions
Number systems are important and interesting.
Despite being a classical topic, several issues are still unresolved.
The relationship between number systems and data structures is
efficacious.
Applications in hardware design are to be investigated.