Sie sind auf Seite 1von 34

Amr Elmasry Number systems and Data Structures (1)

Number Systems
and Data Structures
Amr Elmasry
Alexandria University
Amr Elmasry Number systems and Data Structures (2)
Outline
Some existing number systems:
Properties
Data-structural applications
The extended regular system:
Properties
Implementation
The strictly regular system:
Properties
Implementation
In-place counters:
Properties
Implementation
Open issues and conclusions
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.
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 ips.
Amr Elmasry Number systems and Data Structures (5)
What is the most economical system?
Let d be a positive integer.
rep(d):

d
0
, d
1
, . . . , d
k1

(d
0
is the least signicant digit)
val (d):
k1

i=0
d
i
w
i
b-ary: w
i
= b
i
Develop a number system for which
[ d
i
[ 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.
Amr Elmasry Number systems and Data Structures (6)
Full repertoire of operations
increment(d, i): Assert that i 0, 1, . . . , k. Perform ++d
i
resulting in
d
t
, i.e. val (d
t
) = val (d) + w
i
. Make d
t
valid without changing its
value. We refer to increment(d, 0) as ++.
decrement(d, i): Assert that i 0, 1, . . . , k 1. Perform --d
i
resulting
in d
t
, i.e. val (d
t
) = val (d) w
i
. Make d
t
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 d
0
, d
1
, . . . , d
i1
) and

d
i
, d
i+1
, . . . , d
k1

.
concatenate(d, d
t
): Concatenate rep(d) and rep(d
t
) into one valid se-
quence that has the same value as

d
0
, d
1
, . . . , d
k1
, d
t
0
, d
t
1
, . . . , d
t
k
t
1

.
add(d, d
t
): Construct a valid sequence d
tt
such that val (d
tt
) = val (d)+
val (d
t
).
Amr Elmasry Number systems and Data Structures (7)
Classic Number systems
Binary: d
i
0, 1; w
i
= 2
i
Canonical skew binary: d
i
0, 1; w
i
= 2
i+1
1 and the rst non-
zero digit may be a 2. Every sequence of digits is of the form
0

([2)(0[1)

. [Myers 83]
Redundant binary: d
i
0, 1, 2; w
i
= 2
i
Redundant regular (RR): d
i
0, 1, 2; w
i
= 2
i
; Every sequence of
digits is of the form

0 [ 1 [ 01

. [Clancy and Knuth 77]


Extended regular system: d
i
0, 1, 2, 3; Every 3 is preceded by at
least 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 or
running out of digits. [Clancy and Knuth 77; Kaplan, Shafrir and
Tarjan 02]]
Zeroless regular: d
i
1, 2, 3; w
i
= 2
i
; Every sequence of digits is
of the form

1 [ 2 [ 12

[Brodal 95]
Amr Elmasry Number systems and Data Structures (8)
Properties
Increments involve constant digit ips for the RR system.
Increments and decrements only at d
0
involve constant digit ips
in the skew-binary system.
Increments and decrements involve constant digit ips 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 2
k
1 (the exponentiality property).
Amr Elmasry Number systems and Data Structures (9)
From number systems to data structures
Dene 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)
x-carry resembles a join of b objects of rank j to one of rank j +1.
x-borrow resembles a split of an object of rank j to b objects of rank
j 1.
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]
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 ecient 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]
Amr Elmasry Number systems and Data Structures (12)
Application: Binomial queues
N = 11
10
= 1011
2
29 2
7
6
19
48
10 14
9 26
51
min
Primitives
join:
+
B
r
B
r
B
r+1
split:
+
B
r+1
B
r
B
r
Worst-case bounds
nd-min: O(1)
insert/borrow: (lgN) O(1)
meld: two queues of size M,N,
O(lgM +lgN)
delete-min:

meld
Amr Elmasry Number systems and Data Structures (13)
Outline
Some existing number systems:
Properties
Data-structural applications
The extended regular system:
Properties
Implementation
The strictly regular system:
Properties
Implementation
In-place counters:
Properties
Implementation
Open issues and conclusions
Amr Elmasry Number systems and Data Structures (14)
Extended regular system
Blocks: 12

3 02

3 21

0 31

0
0 and 3 are distinguishing digits for the blocks
x-carry : 3 x 1 (x +1) (no change in value)
Example: d = 1222232221031121101
increment(d, 2) : d = 1232232221031121101 ???
x-carry(d, 5) : d = 1232213221031121101
increment(d, 4) : d = 1232313221031121101 ???
x-carry(d, 4) : d = 1232123221031121101
Amr Elmasry Number systems and Data Structures (15)
Extended regular system
x-borrow : 0 x 2 (x 1) (no change in value)
decrement(d, 15) : d = 1232123221031120101 ???
x-borrow(d, 17) : d = 123212322103112012
decrement(d, 11), decrement(d, 11), decrement(d, 11) :
d = 123212322100112012
x-borrow(d, 11) : d = 123212322102012012
A x-carry corresponds to combining (joining) two structures of the
same size (rank) into a bigger one.
A x-borrow corresponds to dividing (splitting) a structure into two
smaller structures of equal size (rank).
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 x 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.
Amr Elmasry Number systems and Data Structures (17)
Increments [Elmasry and Katajainen 12]
Algorithm x-carry(d,j)
1: assert 0 j k 1 and d
j
= 3
2: d
j
1
3: ++d
j+1
4: if d
j+1
= 3
5: f
j
j +1 // a new block of two digits
6: else
7: f
j
f
j+1
// extending a possible block from the beginning
Algorithm increment(d, i)
1: if d
i
= 3
2: x-carry(d,i) // either this x is executed
3: j f
i
4: if j k 1 and d
j
= 3
5: x-carry(d,j)
6: ++d
i
7: if d
i
= 3
8: x-carry(d,i) // or this x
Amr Elmasry Number systems and Data Structures (18)
Decrements [Elmasry and Katajainen 12]
Algorithm x-borrow(d,j)
1: assert 0 j < k 1 and d
j
= 0
2: d
j
2
3: --d
j+1
4: if d
j+1
= 0
5: f
j
j +1 // a new block of two digits
6: else
7: f
j
f
j+1
// extending a possible block from the beginning
Algorithm decrement(d, i)
1: if d
i
= 0
2: x-borrow(d,i) // either this x is executed
3: j f
i
4: if j < k 1 and d
j
= 0
5: x-borrow(d,j)
6: --d
i
7: if i < k 1 and d
i
= 0
8: x-borrow(d,i) // or this x
Amr Elmasry Number systems and Data Structures (19)
Outline
Some existing number systems:
Properties
Data-structural applications
The extended regular system:
Properties
Implementation
The strictly regular system:
Properties
Implementation
In-place counters:
Properties
Implementation
Open issues and conclusions
Amr Elmasry Number systems and Data Structures (20)
Strictly regular system [Elmasry et al. 10]
Digits: d
i
0, 1, 2; w
i
= 2
i
Strict regularity: The sequence from the least-signicant to the most-
signicant digit is of the form

1
+
[ 01

[ 01
+

Extreme digits: 0 and 2


a) 1111111 yes
b) 11011211101 yes
c) 1201 no
d) 1110101 no
Amr Elmasry Number systems and Data Structures (21)
Increment example
Notation: Digit d
i
to be increased is displayed in red. d
a
is the rst
extreme digit after d
i
, k is a non-negative integer, denotes any
combination of 1
+
and 01

2 blocks, and any combination of


1
+
and 01

2 blocks followed by at most one 01


+
block.
Initial conguration: 01

11

211
k

Action: d
i
2; d
a
d
a
2; d
a+1
d
a+1
+1
Final conguration: 01

21

021
k

Remark: This is one of 19 cases considered in our correctness proof.


Amr Elmasry Number systems and Data Structures (22)
General algorithm
x-carry(d, i): Assert that d
i
2. Perform d
i
d
i
2 and d
i+1

d
i+1
+1.
Algorithm increment(d, i)
1: ++d
i
2: Let d
b
be the rst extreme digit before d
i
, d
b
0, 2, undened
3: Let d
a
be the rst extreme digit after d
i
, d
a
0, 2, undened
4: if d
i
= 3 or (d
i
= 2 and d
b
,= 0)
5: x-carry(d, i)
6: else if d
a
= 2
7: x-carry(d, a)
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)
Amr Elmasry Number systems and Data Structures (24)
Outline
Some existing number systems:
Properties
Data-structural applications
The extended regular system:
Properties
Implementation
The strictly regular system:
Properties
Implementation
In-place counters:
Properties
Implementation
Open issues and conclusions
Amr Elmasry Number systems and Data Structures (25)
In-place counters [Elmasry and Katajainen
13]
Digits: (a) d
0
is a basket of 1s,
call their number x; (b) there
may exist at most one such
that d

= 2; (c) d
i
0, 1
for all i ,= 0 and i ,=
Weights: w
i
= 2
i
Rules: (a) d is of the form
((1(0[1)

x[ ((1(0[1)

20

x
(b)

k1
i=0
d
i
lg(1 +val(d))|
Operations: ++, --, and add
Our system is as the regular sys-
tem, but we only allow at most
one 2, except that x can become
as high as lg(1 +val(d))|.
Amr Elmasry Number systems and Data Structures (26)
Current knowledge
Binary system
Representation: k +O(lgk) bits
++/--: (k/w) worst-case time
add: k
1
, k
2
bits, ((k
1
+k
2
)/w)
worst-case time
Our in-place system
Representation: k +O(lgk) bits
++/--: O(1) worst-case time
add: k
1
, k
2
bits, O((k
1
+k
2
)/w)
worst-case time
Extended regular system
Representation: O(k) words
increment/decrement: O(1) digit
changes in the worst case
add: k
1
, k
2
bits, O(mink
1
, k
2
)
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]
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 innite array a for storing data
a constant number of variables
N = 14
w = 4
0 1 2 3
1010 1110
workspace
0110 0011
14 a
if a counter uses N bits, these must be kept in the rst
N/w| locations of a
the word size w lg(1 +N)|, where N is the problem
size.
Amr Elmasry Number systems and Data Structures (28)
Actual representation
50
10
when only ++ is supported
1 0 1 1 0
b
5
b
4
b
3
b
2
b
1
b
0
= 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)
Amr Elmasry Number systems and Data Structures (29)
Key ideas
Basket x
not too big

k1
i=0
d
i

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
1
1 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 rst non-zero digit
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: x it
4: else if no 2 and x 2
5: x x 2
6: ++d
1
7: else (other modes)
8: +2
9: x x +1

1 0 2 0 0 2
+ 1
1 1 0 0 0 3
Amr Elmasry Number systems and Data Structures (31)
The idea for --
Implement -- as the reverse of ++. However, this approach
fails when there is a long borrow sequence.
Solution:
Keep a basket of 1s (x)
Search for the rst d
i
, d
i
,= 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 rst 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.
Amr Elmasry Number systems and Data Structures (32)
Outline
Some existing number systems:
Properties
Data-structural applications
The extended regular system:
Properties
Implementation
The strictly regular system:
Properties
Implementation
In-place counters:
Properties
Implementation
Open issues and conclusions
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 eciently support increment and decrement
with fewer digits? We can do it with 3 digits, but we cannot
implement it except if operations are done on d
0
or d
k1
.
Do you know how to eciently support increment and decrement
of d
0
with the minimum number of bits? To represent numbers
up to 2
n
1, we know how to do that with n +1 bits using Gray
codes, it is impossible with n bits. For a practical implementation
with binary weights, we know how to do it with n +O(lgn) bits.
Using other weights (for example, ternary weights)
Amr Elmasry Number systems and Data Structures (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
ecacious.
Applications in hardware design are to be investigated.

Das könnte Ihnen auch gefallen