Number Systems and Data structures Amr Elmasry [email protected] A. Elmasry, C. Jensen and J....

14
Number Systems and Data structures Amr Elmasry elmasry@mpi- inf.mpg.de 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.

Transcript of Number Systems and Data structures Amr Elmasry [email protected] A. Elmasry, C. Jensen and J....

Page 1: Number Systems and Data structures Amr Elmasry elmasry@mpi-inf.mpg.de A. Elmasry, C. Jensen and J. Katajainen, The strictly-regular number system and worst-case.

Number Systems and Data structures

Amr Elmasry [email protected]

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.

Page 2: Number Systems and Data structures Amr Elmasry elmasry@mpi-inf.mpg.de A. Elmasry, C. Jensen and J. Katajainen, The strictly-regular number system and worst-case.

Elmasry, Amr 2Number 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

Page 3: Number Systems and Data structures Amr Elmasry elmasry@mpi-inf.mpg.de A. Elmasry, C. Jensen and J. Katajainen, The strictly-regular number system and worst-case.

Elmasry, Amr 3Number Systems and Data Structures

What is a Number System?

x = <d0,d1,d2,… dk-1>

b-ary wj = bj

wj = bj+1 – 1 (skew)

Examples:

Binary: dj є {0,1}, wj = 2j

Redundant binary: dj є {0,1,2}, wj = 2j

(Canonical) skew binary: The first non-zero digit may be a 2.0* (ε | 2) (0|1)*, wj = 2j+1 – 1 [Myers 83]

Redundant-regular (RR) binary: Every 2 is preceded by at least one 0. (0 | 1 | 01*2)* [Clancy & Knuth 77]

j

k

jjwdxval

1

0

)(

Page 4: Number Systems and Data structures Amr Elmasry elmasry@mpi-inf.mpg.de A. Elmasry, C. Jensen and J. Katajainen, The strictly-regular number system and worst-case.

Elmasry, Amr 4Number Systems and Data Structures

Operations

Extended-RR system: dj є {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: dj ≠ 0

Others: dk-1 = 2

Operations:increment(x,j): s val(x) + wj

decrement(x,j): s val(x) – wj

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.

Page 5: Number Systems and Data structures Amr Elmasry elmasry@mpi-inf.mpg.de A. Elmasry, C. Jensen and J. Katajainen, The strictly-regular number system and worst-case.

Elmasry, Amr 5Number Systems and Data Structures

Properties

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 d0 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 2k-1 (the exponentiality property).

Page 6: Number Systems and Data structures Amr Elmasry elmasry@mpi-inf.mpg.de A. Elmasry, C. Jensen and J. Katajainen, The strictly-regular number system and worst-case.

Elmasry, Amr 6Number 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)

01200001112012020011 11010001112012020011 02010001112012020011 10110001112012020011 01110001112012020011 Keep track of a list of 2’s from least to most significant.For general increments use Brodal’s guides.

Page 7: Number Systems and Data structures Amr Elmasry elmasry@mpi-inf.mpg.de A. Elmasry, C. Jensen and J. Katajainen, The strictly-regular number system and worst-case.

Elmasry, Amr 7Number Systems and Data Structures

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 ≤ 2j+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.

Page 8: Number Systems and Data structures Amr Elmasry elmasry@mpi-inf.mpg.de A. Elmasry, C. Jensen and J. Katajainen, The strictly-regular number system and worst-case.

Elmasry, Amr 8Number 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 dj ≥ 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]

Page 9: Number Systems and Data structures Amr Elmasry elmasry@mpi-inf.mpg.de A. Elmasry, C. Jensen and J. Katajainen, The strictly-regular number system and worst-case.

Elmasry, Amr 9Number 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).

Page 10: Number Systems and Data structures Amr Elmasry elmasry@mpi-inf.mpg.de A. Elmasry, C. Jensen and J. Katajainen, The strictly-regular number system and worst-case.

Elmasry, Amr 10Number 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 2r 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

datastructures. For meldable heaps (no decrease) delete-min requires at most 2 lg n +O(1) comparisons instead of 7 lg n + O(1).

Page 11: Number Systems and Data structures Amr Elmasry elmasry@mpi-inf.mpg.de A. Elmasry, C. Jensen and J. Katajainen, The strictly-regular number system and worst-case.

Elmasry, Amr 11Number Systems and Data Structures

The Skew Five-Symbol Number System

dj є {0,1,2,3,4}, wj = 2j+1 – 1

increment by 1:1. Increase d0 by 1.

2. Find the smallest j where dj є {3,4}.

3. If j exists, perform a fix for dj .

fix dj :

1. Decrease dj by 3.

2. Increase dj+1 by 1.

3. If j≠0, Increase dj -1 by 2.

A fix does not change the value of a number.

Page 12: Number Systems and Data structures Amr Elmasry elmasry@mpi-inf.mpg.de A. Elmasry, C. Jensen and J. Katajainen, The strictly-regular number system and worst-case.

Elmasry, Amr 12Number 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 usingan 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.

Page 13: Number Systems and Data structures Amr Elmasry elmasry@mpi-inf.mpg.de A. Elmasry, C. Jensen and J. Katajainen, The strictly-regular number system and worst-case.

Elmasry, Amr 13Number 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.

Page 14: Number Systems and Data structures Amr Elmasry elmasry@mpi-inf.mpg.de A. Elmasry, C. Jensen and J. Katajainen, The strictly-regular number system and worst-case.

Elmasry, Amr 14Number 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.