24
Space-Efficient Static Trees and Graphs Guy Jacobson IEEE Symposium on Foundations of Computer Science, 1989 Speaker: 吳吳吳

Space-Efficient Static Trees and Graphs Guy Jacobson IEEE Symposium on Foundations of Computer Science, 1989 Speaker:

Embed Size (px)

Citation preview

Page 1: Space-Efficient Static Trees and Graphs Guy Jacobson IEEE Symposium on Foundations of Computer Science, 1989 Speaker:

Space-Efficient Static Trees and Graphs

Guy Jacobson

IEEE Symposium on Foundations of Computer Science, 1989

Speaker: 吳展碩

Page 2: Space-Efficient Static Trees and Graphs Guy Jacobson IEEE Symposium on Foundations of Computer Science, 1989 Speaker:

• A pointer needs lg n bits to address n different locations.

• Using pointers to represent the linking relation of a graph will therefore occupy (n lg n) bits.

1

2 3

4 5 6

7 8

1 2 3 4 5 6 7 8

2 3 4 5 nil 6 nil nil 7 nil 8 nil nil nil nil nil

Page 3: Space-Efficient Static Trees and Graphs Guy Jacobson IEEE Symposium on Foundations of Computer Science, 1989 Speaker:

Outline

• To store a binary tree in asymptotically optimal space– Represent a tree in O(n) bits– Efficient tree-traversal in space-efficient trees

• To store planar graphs in asymptotically optimal space

Page 4: Space-Efficient Static Trees and Graphs Guy Jacobson IEEE Symposium on Foundations of Computer Science, 1989 Speaker:

1. Mark all the nodes of the tree with 1.

2. Add external nodes to the tree, and mark them all with 0.

3. Read off the makes of nodes of the tree in level-order.

Binary Trees in 2n+1 bits

Page 5: Space-Efficient Static Trees and Graphs Guy Jacobson IEEE Symposium on Foundations of Computer Science, 1989 Speaker:

1. Mark all the nodes of the tree with 1.

2. Add external nodes to the tree, and mark them all with 0.

3. Read off the makes of nodes of the tree in level-order.

Binary Trees in 2n+1 bits

Page 6: Space-Efficient Static Trees and Graphs Guy Jacobson IEEE Symposium on Foundations of Computer Science, 1989 Speaker:

• How to compute the linking relations in a space-efficient tree?

Page 7: Space-Efficient Static Trees and Graphs Guy Jacobson IEEE Symposium on Foundations of Computer Science, 1989 Speaker:

Rank and Select

• Define two operations rank(m) and select(m) as follows:

rank(m): Counts the number of 1s from position 1 up to position m in a binary stringselect(m): Finds the m-th 1s in a binary string

• Example:

rank(10) = 7select(7) = 10

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17

1 1 1 1 1 0 1 0 0 1 0 1 0 0 0 0 0

1 2 3 4 5 6 7 8

Page 8: Space-Efficient Static Trees and Graphs Guy Jacobson IEEE Symposium on Foundations of Computer Science, 1989 Speaker:

Algorithm to Compute Linking Relations

1

2 3

4 5 6

7 8

1

2 3

4 5

9 12

6 7

8 10 11 13

14 15 16 17

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17

1 1 1 1 1 0 1 0 0 1 0 1 0 0 0 0 0

1 2 3 4 5 6 7 8

left-child(m) 2 rank(m)right-child(m) 2 rank(m) 1parent(m) select(m2)

Page 9: Space-Efficient Static Trees and Graphs Guy Jacobson IEEE Symposium on Foundations of Computer Science, 1989 Speaker:

• How to compute rank(m) and select(m) efficiently? (e.g. constant time)

Page 10: Space-Efficient Static Trees and Graphs Guy Jacobson IEEE Symposium on Foundations of Computer Science, 1989 Speaker:

The rank directory:• Conceptually break the bit-string into blocks of length

lg2n. Keep a table containing the number of 1s up to the last position in each block. This takes n / lg n bits.

• Break each block into sub-blocks of length ½lg n. Keep a table containing the number of 1s within the block up to the last position in each sub-block. This takes n lglg2n / lg n bits.

• Keep a pre-computed table giving the number of 1s up to every possible position in every possible distinct sub-block.

Compute Rank(m) and Select(m)

Page 11: Space-Efficient Static Trees and Graphs Guy Jacobson IEEE Symposium on Foundations of Computer Science, 1989 Speaker:

1 2 3 n/lg2n

101100 110011 001... ... ... ..11 000010 101000

0 3 7 ... ... ... ... 55 56

lg2n

lg n

0

½lg n

lglg2n

n / lg n bits

2n lglg2n / lg n bits

n bits

Page 12: Space-Efficient Static Trees and Graphs Guy Jacobson IEEE Symposium on Foundations of Computer Science, 1989 Speaker:

1 2 3 n/lg2n

101100 110011 001... ... ... ..11 000010 101000

0 3 7 ... ... ... ... 55 56

lg2n

lg n

0

½lg n

lglg2n

n / lg n bits

2n lglg2n / lg n bits

n bits

Precomputed Table

2½lg n ½lg n lg½lg n bits

Page 13: Space-Efficient Static Trees and Graphs Guy Jacobson IEEE Symposium on Foundations of Computer Science, 1989 Speaker:

Planar Graphs in O(n) Space• Represent a special case of planar graphs called one-p

age graphs in O(n) bits

• k-page graphs can be represented in O(kn) bits• Any planar graph can be embedded in a four-page gra

ph.Yannakakis, M. "Four pages are necessary and sufficient for planar graphs." Proceedings of the 18th ACM Symposium on Theory of Computing, pages 104-108, 1986.

Page 14: Space-Efficient Static Trees and Graphs Guy Jacobson IEEE Symposium on Foundations of Computer Science, 1989 Speaker:

One-page graph

• One-page Graph:All edges are lying to one side and can not cross.

Page 15: Space-Efficient Static Trees and Graphs Guy Jacobson IEEE Symposium on Foundations of Computer Science, 1989 Speaker:

One-page graph in O(n) Space

• One-page Graph:All edges are lying to one side and can not cross.

| ((( | )( | ( | )) | )( | ))

Page 16: Space-Efficient Static Trees and Graphs Guy Jacobson IEEE Symposium on Foundations of Computer Science, 1989 Speaker:

• How to compute the linking relations in the parenthesis string?

Page 17: Space-Efficient Static Trees and Graphs Guy Jacobson IEEE Symposium on Foundations of Computer Science, 1989 Speaker:

Finding the close parenthesis which match the open one• First, break the string of parentheses into bloc

ks of length lgn.

Page 18: Space-Efficient Static Trees and Graphs Guy Jacobson IEEE Symposium on Foundations of Computer Science, 1989 Speaker:

Definitions

Far parenthesis:

An open parenthesis p is called a far parenthesis if and only if p's matching parenthesis lies outside its own block.

Pioneer:

A far parenthesis is a pioneer if and only if its matching parenthesis lies in a different block that of the previous far parenthesis.

The number of pioneers is at most 2lgn.

Dotted lines denote the matches of far parenthesesRed ones denote the matches of pioneers

Page 19: Space-Efficient Static Trees and Graphs Guy Jacobson IEEE Symposium on Foundations of Computer Science, 1989 Speaker:

Use a Directory Structure of Size O(n) bits for Matching P

arentheses

Page 20: Space-Efficient Static Trees and Graphs Guy Jacobson IEEE Symposium on Foundations of Computer Science, 1989 Speaker:
Page 21: Space-Efficient Static Trees and Graphs Guy Jacobson IEEE Symposium on Foundations of Computer Science, 1989 Speaker:

Matching Parentheses

• For a parenthesis p, its matching parenthesis q can be found out as follows:

Case1: p and q are in the same block

Using precomputed table

lgn

Page 22: Space-Efficient Static Trees and Graphs Guy Jacobson IEEE Symposium on Foundations of Computer Science, 1989 Speaker:

Matching Parentheses• Case2: p and q are not in the same block

Find the pioneer of p and use it to locate the block containing q

Compute the position of q via nesting depths

Page 23: Space-Efficient Static Trees and Graphs Guy Jacobson IEEE Symposium on Foundations of Computer Science, 1989 Speaker:

O(n) bits

O(n) bits

O(n) bits + number of pioneers lg n bits o(n) bits

Page 24: Space-Efficient Static Trees and Graphs Guy Jacobson IEEE Symposium on Foundations of Computer Science, 1989 Speaker:

Conclusion

• A space-efficient data structureachieve almost optimal space while supporting the required operations almost as efficient as using pointers to represent it.

References• Dinesh P. Mehta and Sartaj Sahni. Handbook of Data Structur

es and Applications. Chapman & Hall/CRC, 2005.