This action might not be possible to undo. Are you sure you want to continue?

Welcome to Scribd! Start your free trial and access books, documents and more.Find out more

)

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, 349–360.

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, 130–142.

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, 26–37.

© 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

k−1

(d

0

is the least signiﬁcant digit)

val (d):

k−1

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

i−1

) and

d

i

, d

i+1

, . . . , d

k−1

.

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

k−1

, 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

∗

2

∗

. [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

∗

3

∗

[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

Deﬁne 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 eﬃcient priority queue

with the working-set property. [Elmasry ’06]

Using the strictly regular system: improving Brodal’s priority queue

with constant meld. [Elmasry et al. ’10]

Using the extended RR system: improving Brodal’s 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-signiﬁcant to the most-

signiﬁcant digit is of the form

1

+

[ 01

∗

2

∗

ε [ 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 conﬁguration: α01

∗

11

∗

211

k

ω

Action: d

i

← 2; d

a

← d

a

−2; d

a+1

← d

a+1

+1

Final conﬁguration: α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, undeﬁned¦

3: Let d

a

be the ﬁrst extreme digit after d

i

, d

a

∈ ¡0, 2, undeﬁned¦

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 1’s,

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)

k−1

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(min¡k

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 inﬁnite 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 1’s)

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

k−1

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 1’s (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 ≥#0’s 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 eﬃciently 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

k−1

.

• Do you know how to eﬃciently 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

eﬃcacious.

• Applications in hardware design are to be investigated.

- Alexandria ACM Student Chapter | Formal Specifications and VData-Centric Web Service
- Alexnadria ACM SC | TECHnews Magazine IV
- Alexandria ACM Student Chapter | Systems Analysis
- Alexandria ACM SC | Introduction to Genetic Algorithms
- Alexandria ACM SC | Web Engineering Series | Episode #1
- Alexandria ACM SC | Rigorous Computational Simulation of Dynamical Systems
- Alexandria ACM SC | Quantum Computing Lecture.
- Alexandria ACM SC | Introduction to Natural Language Processing
- Alexandria ACM SC | Challenges in Computer Architecture
- Alexandria ACM Mentorship Program | Guidelines for Successful Mentorship
- Alexandria ACM Student Chapter | Research in Egypt | Changing Culture & Building Hope
- Alexandria ACM SC | Graduation Projects
- Alexandria ACM SC | W8 Phone Applications Training
- Alexandria ACM SC | Android Applications Training Program
- Alexandria ACM SC | Cloud Computing Trainign
- Alexandria ACM SC | TECHnews (III)
- Alex ACM SC Machine Learning Day [Materials] | Machine Learning& Artificial Intelligence ... Our Ultimate Dream. By Eng. Mohamad Abdelaziz Gowayed
- Alex ACM SC Machine Learning Day [Materials] | Introduction to Machine Learning By Eng. Ibrahim Sabek
- Alexandria ACM SC | TECHnew Magazine | April Special Edition
- Webalo Alex Internship Opportunity
- Cloud Computing ... The Art of Storage
- Alexandria ACM SC | TechNews | April
- Scientific Debates ... Dignified History & Lustrous Future | Article By Ahmed Ayoub

Alexandria ACM Student Chapter introduced a talk by Dr. Amr Elmasry about the correlation between the regular numeral systems we know i.e. binary, decimal and hexadecimal and the data structures we...

Alexandria ACM Student Chapter introduced a talk by Dr. Amr Elmasry about the correlation between the regular numeral systems we know i.e. binary, decimal and hexadecimal and the data structures we daily use in the development of our known algorithms and how this simple relation is so vital and efficacious in our daily CS application and theories.

More about the seminar and the topic:

• FB Event and Description: http://goo.gl/sVQFnk

• Instructed by: Dr. Amr Emasry: http://goo.gl/pZCXCd

More about the seminar and the topic:

• FB Event and Description: http://goo.gl/sVQFnk

• Instructed by: Dr. Amr Emasry: http://goo.gl/pZCXCd

- Alexandria ACM SC | Introduction to Genetic Algorithms
- Alexandria ACM Student Chapter | Formal Specifications and VData-Centric Web Service
- Alexandria ACM SC | Rigorous Computational Simulation of Dynamical Systems
- Alexnadria ACM SC | TECHnews Magazine IV
- Alexandria ACM Student Chapter | Systems Analysis
- Scientific Debates ... Dignified History & Lustrous Future | Article By Ahmed Ayoub
- A Primer on Cryptarithmetic
- Math Shortcuts for Competitive Examinations
- COL106 Assignment 5
- esc101-lec29
- Vedic Mathematics - Methods
- Message Queues, Mailboxes, And Pipes - RTOS
- Technical - Tricky C Progra..
- j Query Latest
- 400 Puzzles
- fundas
- Data Structures and Algorithms 2009-4-3 0
- Algorithms
- Alexandria ACM SC | Web Engineering Series | Episode #1
- Cloud Computing ... The Art of Storage
- m3-search
- unit II
- Bbm
- Aptitude Daily
- Stacks Nd Linked List
- Competition 2008 Spring
- Data Structures
- AudioQueueProgrammingGuide
- sutra1
- PSDM Solutions

Are you sure?

This action might not be possible to undo. Are you sure you want to continue?

We've moved you to where you read on your other device.

Get the full title to continue

Get the full title to continue reading from where you left off, or restart the preview.

scribd