ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
BalancedParentheses.h File Reference

A static sequence \( B \) of balanced parentheses, 1 for an open one and 0 for a closed one, with the matching of Munro and Raman by the range min-max tree of Navarro and Sadakane: the base of the trees by parentheses [see BPTree.h], which chapter 15, Compressed Data Structures, of Ferragina's book counts among the codes of \( 2 n \) bits of a tree, and which, unlike the codes by levels, also give the size of a subtree and the depth of a node. More...

#include "ADS/Contract.h"
#include "ADS/succinct/BitVector.h"
#include "ADS/succinct/CompactRankSelect.h"
#include <algorithm>
#include <array>
#include <cstddef>
#include <cstdint>
#include <string_view>
#include <vector>
#include "ADS/succinct/impl/BalancedParentheses.tpp"

Go to the source code of this file.

Classes

class  ADS::BalancedParentheses
 a static sequence of balanced parentheses with a range min-max tree More...

Namespaces

namespace  ADS

Detailed description

A static sequence \( B \) of balanced parentheses, 1 for an open one and 0 for a closed one, with the matching of Munro and Raman by the range min-max tree of Navarro and Sadakane: the base of the trees by parentheses [see BPTree.h], which chapter 15, Compressed Data Structures, of Ferragina's book counts among the codes of \( 2 n \) bits of a tree, and which, unlike the codes by levels, also give the size of a subtree and the depth of a node.

With the excess \( E( i ) \), the number of open parentheses minus the number of closed ones in \( B[ 0 .. i ] \) (and \( E( -1 ) = 0 \)), every operation is a search of the first place, forward or backward from \( i \), whose excess is at most a target \( t \):

  • findClose( i ), the closed parenthesis of the open one at \( i \), is the first \( j > i \) with \( E( j ) = E( i ) - 1 \);
  • findOpen( j ), the open parenthesis of the closed one at \( j \), is \( k + 1 \) for the last \( k < j \) with \( E( k ) = E( j ) \);
  • enclose( i ), the open parenthesis of the pair that encloses the one at \( i \), is \( k + 1 \) for the last \( k < i \) with \( E( k ) = E( i ) - 2 \);

and since the excess moves by \( \pm 1 \), the first place with an excess at most \( t \) has exactly \( t \). The range min-max tree keeps, for each leaf of 512 bits, for each group of 16 leaves and for each node of a complete binary tree over the groups, the total excess of its range and its minimum prefix excess. Every search runs on the excess relative to its start, so that it needs no rank: it scans the leaf of 512 bits of \( i \) (a byte at a time, with the excess, the minimum and the place of each target of each of the 256 bytes in tables), then the leaves of its group of 16 by their excess and minimum in 16 bits, then climbs the tree, whose leaves are the groups, to the first range on its side whose minimum reaches the target, and goes down to its group, its leaf and its byte, in \( O( \log ( n / 8192 ) + 16 + 512 / 8 ) \) time; most searches of a tree end in the leaf of \( i \), at the cost of a miss of the cache. The tree takes \( 32 / 512 \) bits per parenthesis for the leaves and at most \( 4 \cdot 128 / 8192 \) for the groups, besides the rank directory [see CompactRankSelect.h], 3.125% of the bits besides its samples of select, since no search needs a rank. The excess of any place is \( E( i ) = 2 \, \mathrm{rank}_1( i + 1 ) - ( i + 1 ) \), in \( O( 1 ) \) time.

Jacobson gives findClose() in \( O( \log n ) \) time with the pioneers, and Munro and Raman in \( O( 1 ) \) time with two levels of pioneers and tables, in \( 2 n + o( n ) \) bits; the range min-max tree of Navarro and Sadakane reaches \( O( 1 ) \) time with a tree of degree \( \Theta( \log n ) \) and tables, and its binary form with blocks of a few hundred bits, the one here, is the fastest in practice (Arroyuelo, Cánovas, Navarro and Sadakane; Gog and Petri; Grossi and Ottaviano).

The contracts [see Contract.h]: the sequence is balanced (an audit), findClose() and enclose() get an open parenthesis and findOpen() a closed one; the postconditions are the excesses of the definitions. The functions are defined in BalancedParentheses.tpp.

See also
P. Ferragina, Pearls of Algorithm Engineering, Cambridge University Press, 2023, chapter 15, Compressed Data Structures, doi:10.1017/9781009128933
G. Jacobson, Space-efficient static trees and graphs, Proc. 30th FOCS, 549-554, 1989, doi:10.1109/SFCS.1989.63533
J. I. Munro, V. Raman, Succinct representation of balanced parentheses and static trees, SIAM Journal on Computing 31(3), 762-776, 2001, doi:10.1137/S0097539799364092
G. Navarro, K. Sadakane, Fully functional static and dynamic succinct trees, ACM Transactions on Algorithms 10(3), 16, 2014, doi:10.1145/2601073
D. Arroyuelo, R. Cánovas, G. Navarro, K. Sadakane, Succinct trees in practice, Proc. 12th ALENEX, 84-97, 2010, doi:10.1137/1.9781611972900.9
S. Gog, M. Petri, Optimized succinct data structures for massive data, Software: Practice and Experience 44(11), 1287-1314, 2014, doi:10.1002/spe.2198
R. Grossi, G. Ottaviano, Design of practical succinct data structures for large data collections, Proc. 12th SEA, LNCS 7933, 5-17, 2013, doi:10.1007/978-3-642-38527-8_3
Author
Donato Meoli
Dipartimento di Informatica
Università di Pisa