Presentation on theme: "Succinct Representation of Balanced Parentheses, Static Trees and Planar Graphs J. Ian Munro & Venkatesh Raman."— Presentation transcript:
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
Problem We would like to represent tree with minimal number of bits, permitting the regular operations in constant time.
Information limit Number of trees on n nodes: or about. So, at least 2n bits needed ( ignoring logarithmic terms ) to encode an arbitrary tree.
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.
Example ( ( ( ) ( ) ) ( ) ( ) )
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)
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.
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 ( ( ( ) ( ) ) ( ) ( ) ) parent (3): subtree (2):
Rooted Ordered Tree(Cont) The i-th child of node x: ( ( ( ) ( ) ) ( ) ( ) ) 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... ( ( ( ) ( ) ) ( ) ( ) ) ( ( ( ) ( ) ) ( ) ( ) ) ( ( ( ) ( ) ) ( ) ( ) )
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
Static Trees – Binary trees Q: For a node that has only one child, how we can distinct is it right or left son ? A: Use isomorphism between binary trees and rooted ordered trees. ( ( ) )
Isomorphism example Binary Tree Equivalent Rooted Ordered Tree
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.
Example – subtree(i) ( ( ( ( ) ) ( ) ( ) ) ( ) ) subtree 2: right subtreeleft subtree Binary Tree enclose 2
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.
Binary tree - conclusions We can find: parent, size of subtree, left and right child in O(1) using 2n+o(n) bits
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.
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
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 ).
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.
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.
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.
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.
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.
Proof: Divide sequence to blocks: big block small block... big block... small block
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
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.
With each pioneer parenthesis we keep address of its matching parenthesis. Space requirement:
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.
Intuition – step 1 :... big block... small block #big blocks = #small blocks =
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).
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...
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.
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...
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.
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.
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
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:
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:
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.
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
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)
What we have done ? findclose(i) findopen(i) excess(i) enclose(i)
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.
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.