Download presentation
Presentation is loading. Please wait.
Published byKent Plaskett Modified over 9 years ago
1
Succinct Representation of Balanced Parentheses, Static Trees and Planar Graphs J. Ian Munro & Venkatesh Raman
2
Outline Introduction Balanced Parentheses Static Trees –Rooted Ordered Tree –Binary Trees Planar Graphs Balanced Parentheses Implementation
3
Problem We would like to represent tree with minimal number of bits, permitting the regular operations in constant time.
4
Information limit Number of trees on n nodes: or about. So, at least 2n bits needed ( ignoring logarithmic terms ) to encode an arbitrary tree.
5
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.
6
Example 1 2 33 44 2 55 66 1 ( ( ( ) ( ) ) ( ) ( ) ) 0 0 01 01 1 01 01 1 1 43 256
7
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)
8
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.
9
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 256 1 2 33 44 2 55 66 1 ( ( ( ) ( ) ) ( ) ( ) ) parent (3): subtree (2):
10
Rooted Ordered Tree(Cont) The i-th child of node x: 1 43 256 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 ( ( ( ) ( ) ) ( ) ( ) )
11
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
12
Static Trees – Binary trees Q: For a node that has only one child, how we can distinct is it right or left son ? 11 2 2 A: Use isomorphism between binary trees and rooted ordered trees. ( ( ) )
13
Isomorphism example 1 2 7 106 5 8 9 3496 8 7 1 0 5 3 24 Binary Tree Equivalent Rooted Ordered Tree
14
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.
15
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 34 parent 2parent 9 96 8 10 7 1 0 5 3 24 Binary Tree
16
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 24 subtree 2: right subtreeleft subtree Binary Tree enclose 2
17
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.
18
Binary tree - conclusions We can find: parent, size of subtree, left and right child in O(1) using 2n+o(n) bits
19
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.
20
One-Page Graphs - example 1234567 () (( () )( () (( () ) () ))( () ))( () ) Parenthesis representation uses 2n+2m bits
21
adjacent(i,j) - example 1234567 () (( () )( () (( () ) () ))( () ))( () )
22
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
23
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 ).
24
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.
25
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.
26
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.
27
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.
28
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.
29
Proof: Divide sequence to blocks: big block small block... big block... small block
30
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
31
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.
32
With each pioneer parenthesis we keep address of its matching parenthesis. Space requirement:
33
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 with the same right excess.
34
Intuition – step 1 :... big block... small block #big blocks = #small blocks =
35
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).
36
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...
37
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.
38
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...
39
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.
40
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 with the same right excess.
41
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. excess place i j (i<j) k (j<k)... #big blocks #table entries size of entry Table complexity:
42
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 number such portions Space requirement:
43
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. #of possible strings for each excess value Space requirement:
44
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.
45
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
46
Q:What about not far parenthesis inside small block ? A:Table of answers For 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 binary next one or previous zero or none of them. Space: o(n)
47
What we have done ? findclose(i) findopen(i) excess(i) enclose(i)
48
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.
49
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.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.