Top Banner
Succinct Representation of Balanced Parentheses , Static Trees and Planar Graphs J . Ian Munro & Venkatesh Raman
49

Succinct Representation of Balanced Parentheses, Static Trees and Planar Graphs J. Ian Munro & Venkatesh Raman.

Dec 14, 2015

Download

Documents

Kent Plaskett
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Succinct Representation of Balanced Parentheses, Static Trees and Planar Graphs J. Ian Munro & Venkatesh Raman.

Succinct Representation of Balanced Parentheses , Static Trees and Planar Graphs

J . Ian Munro

&

Venkatesh Raman

Page 2: Succinct Representation of Balanced Parentheses, Static Trees and Planar Graphs J. Ian Munro & Venkatesh Raman.

Outline

• Introduction

• Balanced Parentheses

• Static Trees– Rooted Ordered Tree

– Binary Trees

• Planar Graphs

• Balanced Parentheses Implementation

Page 3: Succinct Representation of Balanced Parentheses, Static Trees and Planar Graphs J. Ian Munro & Venkatesh Raman.

Problem

We would like to represent tree with

minimal number of bits , permitting the

regular operations in constant time .

Page 4: Succinct Representation of Balanced Parentheses, Static Trees and Planar Graphs J. Ian Munro & Venkatesh Raman.

Information limit

Number of trees on n nodes:

or about .

So , at least 2n bits needed ( ignoring

logarithmic terms ) to encode an arbitrary

tree .

)1(2

nn

ncn n

n 2/32

2

Page 5: Succinct Representation of Balanced Parentheses, Static Trees and Planar Graphs J. Ian Munro & Venkatesh Raman.

2n bit representation

• Do a preorder traversal of the tree– if visit a node for a first time – output “(“– if visit a node for a last time – output “)”

We got a balanced nested parenthesis

sequence .

Page 6: Succinct Representation of Balanced Parentheses, Static Trees and Planar Graphs J. Ian Munro & Venkatesh Raman.

Example

1 2 33 44 2 55 66 1( ( ( ) ( ) ) ( ) ( ) )

0 0 01 01 1 01 01 1

1

43

2 5 6

Page 7: Succinct Representation of Balanced Parentheses, Static Trees and Planar Graphs J. Ian Munro & Venkatesh Raman.

Balanced Parentheses Operations

• findclose(i)• findopen(i)• excess(i) - find the difference of the number of open

and close parentheses at position i.

• enclose(i) - given a parenthesis pair , opened at position i , find its closest enclosing matching parenthesis pair.

We will see there implementation in constant time using 2n+o(n) bits …

O(1)

Page 8: Succinct Representation of Balanced Parentheses, Static Trees and Planar Graphs J. Ian Munro & Venkatesh Raman.

Static Trees – Rooted Ordered Tree

• We use isomorphism between general ordered trees and balanced parenthesis expressions as was described earlier.

• Node represented by its corresponding left parenthesis .

Page 9: Succinct Representation of Balanced Parentheses, Static Trees and Planar Graphs J. Ian Munro & Venkatesh Raman.

Rooted Ordered Tree ( Cont.)

• parent(i) - is a left parenthesis of enclose(i)

• size of subtree(i) – all nodes between i and findclose(i) , number of nodes is half a difference between them. 1

43

2 5 6

1 2 33 44 2 55 66 1( ( ( ) ( ) ) ( ) ( ) )

( ( ( ) ( ) ) ( ) ( ) )

parent (3):

subtree (2):

Page 10: Succinct Representation of Balanced Parentheses, Static Trees and Planar Graphs J. Ian Munro & Venkatesh Raman.

Rooted Ordered Tree(Cont)

The i-th child of node x:

1

43

2 5 6

1 2 33 44 2 55 66 1( ( ( ) ( ) ) ( ) ( ) )

• if the parenthesis next to x’s “(“ is “)”, than x has no children.• otherwise “(“ and its matching “)” form the first child.• if the next parenthesis is “(“ together with matching parenthesis match the second child of x and so on ...( ( ( ) ( ) ) ( ) ( ) )1 2 33 44 2 55 66 1

( ( ( ) ( ) ) ( ) ( ) )1 2 33 44 2 55 66 1

( ( ( ) ( ) ) ( ) ( ) )

Page 11: Succinct Representation of Balanced Parentheses, Static Trees and Planar Graphs J. Ian Munro & Venkatesh Raman.

General ordered tree - conclusion

We can find:

parent , size of subtree in O(1) right (first) or left(last) son of node x in O(1). i-th child of x in O(i)

using 2n+o(n) bits

Page 12: Succinct Representation of Balanced Parentheses, Static Trees and Planar Graphs J. Ian Munro & Venkatesh Raman.

Static Trees – Binary treesQ: For a node that has only one child ,

how we can distinct is it right or left

son ?

1 1

2 2

A: Use isomorphism between binary

trees and rooted ordered trees.

( ( ) )

Page 13: Succinct Representation of Balanced Parentheses, Static Trees and Planar Graphs J. Ian Munro & Venkatesh Raman.

Isomorphism example

1

2

7

106

5

8

9

3 4 96 8

10

7

1

0

5

3

2 4

Binary TreeEquivalent Rooted

Ordered Tree

Page 14: Succinct Representation of Balanced Parentheses, Static Trees and Planar Graphs J. Ian Munro & Venkatesh Raman.

Binary trees ( cont. )

• Ordered root has a root which doesn’t correspond to any node in the binary tree .

• The left child in the binary is the leftmost child in the ordered tree.

• The right child in the binary is the next sibling to the right in the ordered tree.

• The root of the binary is the leftmost child of the root of the ordered tree.

Page 15: Succinct Representation of Balanced Parentheses, Static Trees and Planar Graphs J. Ian Munro & Venkatesh Raman.

Example – parent(i)

0 1 2 3 3 2 4 4 1 5 6 7 7 6 8 8 9 9 5 10 10 0

( ( ( ( ) ) ( ) ) ) ( ( ) ) ( ) ( ) ) ( ) )

1

2

7

106

5

8

9

3 4

parent 2 parent 9

96 8

10

7

1

0

5

3

2 4

Binary Tree

Page 16: Succinct Representation of Balanced Parentheses, Static Trees and Planar Graphs J. Ian Munro & Venkatesh Raman.

Example – subtree(i)

0 1 2 3 3 2 4 4 5 5 1 6 6 0

( ( ( ( ) ) ( ) ( ) ) ( ) )

4

1

2 6

5

3

5

1

0

6

3

2 4

subtree 2:

right subtree left subtree

Binary Treeenclose 2

Page 17: Succinct Representation of Balanced Parentheses, Static Trees and Planar Graphs J. Ian Munro & Venkatesh Raman.

Binary trees ( cont. )

For a given left parenthesis of node x at position i: parent : if i = 1 no parent ( root) if symbol at position i-1 is “(“ than it its parent else findopen(i-1) is its parent. left child : if symbol at position i+1 is “(“. right child : if symbol at position findclose(i)+1 is

“(“. subtree : all nodes in the subtree and its right

siblings.

Page 18: Succinct Representation of Balanced Parentheses, Static Trees and Planar Graphs J. Ian Munro & Venkatesh Raman.

Binary tree - conclusions

We can find:

parent , size of subtree , left and right child

in O(1) using 2n+o(n) bits

Page 19: Succinct Representation of Balanced Parentheses, Static Trees and Planar Graphs J. Ian Munro & Venkatesh Raman.

Planar graphs

Definition :

A k-page book embedding of a graph

G=(V,E) is a printing order of V , plus a partition of E into k pages . The edges on a given page must not intersect .

• Planar graphs can be embedded

in four pages .

Page 20: Succinct Representation of Balanced Parentheses, Static Trees and Planar Graphs J. Ian Munro & Venkatesh Raman.

One-Page Graphs - example

1 2 3 4 5 6 7

() (( () )( () (( () ) () ))( () ))( () )

Parenthesis representation uses 2n+2m bits

Page 21: Succinct Representation of Balanced Parentheses, Static Trees and Planar Graphs J. Ian Munro & Venkatesh Raman.

adjacent(i,j) - example

1 2 3 4 5 6 7

() (( () )( () (( () ) () ))( () ))( () )

Page 22: Succinct Representation of Balanced Parentheses, Static Trees and Planar Graphs J. Ian Munro & Venkatesh Raman.

adjacent(i,j) – cont.

() )))) ((( () ))) .... ()

excess i excess i+1#closed #opened

#closed + #opened = degree(i)

#closed - #opened = excess i – excess i+1

First open parenthesis of i:

j

Page 23: Succinct Representation of Balanced Parentheses, Static Trees and Planar Graphs J. Ian Munro & Venkatesh Raman.

One-Page Graphs ( cont. )

• Given node i , we can find node i+1 in O(1) ; given edge , we can find node it belongs to in O(1) using o(n) extra space

( using rank , select operations - won’t be shown here ).

Page 24: Succinct Representation of Balanced Parentheses, Static Trees and Planar Graphs J. Ian Munro & Venkatesh Raman.

One-Page Graphs ( cont. )

• degree(i) – difference indices between nodes i and i+1.

• adjacent( i , j) – find the matching parenthesis of the first open parenthesis after the pair corresponding to vertex i. if its index less than j , adjacent(i,j)=false , otherwise find the matching parenthesis of the last close parenthesis after the pair corresponding to vertex j .If it corresponds to i, adjacent(i ,j ) = true.

Page 25: Succinct Representation of Balanced Parentheses, Static Trees and Planar Graphs J. Ian Munro & Venkatesh Raman.

One-Page Graphs - conclusion

Can be represented using 2n+2m+o(n+m) bits.

Adjacency of a pair of vertices , the degree of any given vertex can be found in O(1) , the neighbors of a vertex i can be produced in O( degree(i) ) time.

Page 26: Succinct Representation of Balanced Parentheses, Static Trees and Planar Graphs J. Ian Munro & Venkatesh Raman.

Graph with more than one page

• Represent each page separately , share the same order of vertices .

• If the graph has k pages , a vertex requires 2k bits whereas an edge 2 bits.

• The parenthesis representation requires 2nk + 2m bits.

• Adjacency of a pair of vertices , the degree of any given vertex can be found in O(k) , the neighbors of a vertex i can be produced in O( degree(i) + k ) time.

Page 27: Succinct Representation of Balanced Parentheses, Static Trees and Planar Graphs J. Ian Munro & Venkatesh Raman.

Planar graphs - conclusions

• Space requirement is at most: 8n+m+o(m+n) = 14n+o(n) , since m is at most 3n.

• Time performance is like one–page graphs.

Page 28: Succinct Representation of Balanced Parentheses, Static Trees and Planar Graphs J. Ian Munro & Venkatesh Raman.

Balanced Parentheses – implementation in 2n+o(n)

Theorem:Given a nested string of 2n balanced

parentheses , using o(n) auxiliary bits we can

perform the operations: findclose(i) , findopen(i),

excess(i) and enclose(i) in constant time.

Page 29: Succinct Representation of Balanced Parentheses, Static Trees and Planar Graphs J. Ian Munro & Venkatesh Raman.

Proof:

Divide sequence to blocks:big blocksmall block

nb lg2

)lg(lglg22

4 nb

...big block big block...

small block

Page 30: Succinct Representation of Balanced Parentheses, Static Trees and Planar Graphs J. Ian Munro & Venkatesh Raman.

Definition

• far parenthesis – if its matching parenthesis lies outside its block

• far parenthesis is a pioneer if its matching parenthesis lies in a different block than that of the previous far parenthesis in the sequence.

( ( ( ) ) ( ) ) ( )

far

pioneer

Page 31: Succinct Representation of Balanced Parentheses, Static Trees and Planar Graphs J. Ian Munro & Venkatesh Raman.

Proof:Imagine a graph with a node for each block.

Create an edge between blocks if there is pioneer

between them .

None of the edges can cross.

No double edges.

We got outer-planar graph , one of its properties

that it has at most 2b – 3 edges.

Theorem:The number of pioneers in b blocks is at most

2b – 3.

Page 32: Succinct Representation of Balanced Parentheses, Static Trees and Planar Graphs J. Ian Munro & Venkatesh Raman.

With each pioneer parenthesis we keep

address of its matching parenthesis.

Space requirement:

nnnnn lglg*lg2

Page 33: Succinct Representation of Balanced Parentheses, Static Trees and Planar Graphs J. Ian Munro & Venkatesh Raman.

findclose(i) – for far parenthesis

Given left far parenthesis , find its excess and its preceding pioneer

Given pioneer , find far parenthesis target block.

Given target block , find first close parenthesis withthe same right excess.

Page 34: Succinct Representation of Balanced Parentheses, Static Trees and Planar Graphs J. Ian Munro & Venkatesh Raman.

Intuition – step1:

...big block ...small block

nn lglg2

nn lg2

#big blocks =

#small blocks =

nlg

nlglg

Page 35: Succinct Representation of Balanced Parentheses, Static Trees and Planar Graphs J. Ian Munro & Venkatesh Raman.

Storing with each big block head:

• the position of the immediately preceding pioneer (to its left) in big block.

• excess in binary ( (#0 - #1) up to that position) – value between (0 -n) .

Page 36: Succinct Representation of Balanced Parentheses, Static Trees and Planar Graphs J. Ian Munro & Venkatesh Raman.

Storing with each small block head:

• the position of the immediately preceding pioneer ( and 0 if there is no pioneer ).

• the excess in binary ( starting from the big block head ) at that position.

• a bit indicating whether there is a pioneer in the small block.

• and ...

Page 37: Succinct Representation of Balanced Parentheses, Static Trees and Planar Graphs J. Ian Munro & Venkatesh Raman.

Storing all possible small blocks:

• a bitmap of pioneers in the small block if the small block contains a pioneer.

number of possible small blocks:

for each entries of size

bits storing previous pioneer index .

)(lg2lglg4)lg(lg4

2

nnn

)lg(lg2

4 n nlglglg

nTotal nnn

lglglg*4*: )lg(lg)(lg2lglg4

Page 38: Succinct Representation of Balanced Parentheses, Static Trees and Planar Graphs J. Ian Munro & Venkatesh Raman.

Find the preceding pioneer address for a far parenthesis.

• Given i , check if its small block has pioneer.

• if yes , consult bitmap if exist pioneer before i.

• if not , the small block header or the big block header will give the address.

• Analogous strategy is to find excess of far parenthesis at the given position ...

Page 39: Succinct Representation of Balanced Parentheses, Static Trees and Planar Graphs J. Ian Munro & Venkatesh Raman.

• Once we find the preceding pioneer parenthesis of a given far, we know the target big block of the given parenthesis from pioneer information.

Given left far parenthesis , find its excess and its preceding pioneer

Given pioneer , find far parenthesis target block.

Page 40: Succinct Representation of Balanced Parentheses, Static Trees and Planar Graphs J. Ian Munro & Venkatesh Raman.

Given left far parenthesis , find its excess and its preceding pioneer

Given pioneer , find far parenthesis target block.

Given target block , find first close parenthesis withthe same right excess.

Page 41: Succinct Representation of Balanced Parentheses, Static Trees and Planar Graphs J. Ian Munro & Venkatesh Raman.

Looking for a first parenthesis with excess e in a given block

• excess within a big block is at most .

• store a table for every excesses .

nlg2

nlg

excess place

i

j (i<j)

k (j<k)

... ...

nlg

nlg2

nlg3

nnnnn

n

n

nlglglglglg

lg

lglg

2

2

#big blocks

#table entries

size of entry

Table complexity:

Page 42: Succinct Representation of Balanced Parentheses, Static Trees and Planar Graphs J. Ian Munro & Venkatesh Raman.

Looking for a first parenthesis with excess e in a given block ( cont.)

for two consecutive entries in table:

• if ( j – i > lg n / 2 ) keep all entries nlg

nnnnnnn lglglglglg*lg*lg2

number such portions

Space requirement:

Page 43: Succinct Representation of Balanced Parentheses, Static Trees and Planar Graphs J. Ian Munro & Venkatesh Raman.

Looking for a first parenthesis with excess e in a given block ( cont.)

for two consecutive entries in table:

• if ( j – i <= log n / 2 ) keep table of answers.

nnn

lglg*lg*2 2

lg

#of possible strings

for each excess value

Space requirement:

Page 44: Succinct Representation of Balanced Parentheses, Static Trees and Planar Graphs J. Ian Munro & Venkatesh Raman.

We can find the matching closing

parenthesis for a far open parenthesis

using o(n) bits of extra storage in O(1).

We also can find the excess value at every

far position at constant time.

Page 45: Succinct Representation of Balanced Parentheses, Static Trees and Planar Graphs J. Ian Munro & Venkatesh Raman.

What about not far parenthesis ?

• Use exactly the same strategy for far parenthesis with respect to the small blocks within the big block .

• small block

• tiny block

)lg(lglg22

4 nb

nlglg2

Page 46: Succinct Representation of Balanced Parentheses, Static Trees and Planar Graphs J. Ian Munro & Venkatesh Raman.

Q:What about not far parenthesis inside small block ? A:Table of answersFor every possible string of small block ( of

length ) and for every position in that string create a table entry.

In each entry two values:excess in binarynext one or previous zero or none of them .

Space: o(n)

)lg(lg2

4 n

Page 47: Succinct Representation of Balanced Parentheses, Static Trees and Planar Graphs J. Ian Munro & Venkatesh Raman.

What we have done ?

findclose(i)findopen(i)excess(i) enclose(i)

Page 48: Succinct Representation of Balanced Parentheses, Static Trees and Planar Graphs J. Ian Munro & Venkatesh Raman.

enclose(i)

• e = excess(i)

• Look for right parenthesis with e-1 in a target block of closing parenthesis .

• What if its not in that block ?

• With every block header we keep a pointer to the parenthesis that has right excess one less that the minimum at that block .

Page 49: Succinct Representation of Balanced Parentheses, Static Trees and Planar Graphs J. Ian Munro & Venkatesh Raman.

Conclusions

• Parentheses representation in 2n+o(n) with findclose(i) , findopen(i) , excess(i) , enclose(i) in constant time.

• Static trees representation in 2n+o(n) that can find parent , left child , right child , size of the subtree in constant time.

• Planar graph representation in 8n+ 2m + o(m + n) that can answer adjacent , degree in constant time , give all neighbors in O(degree ) time.