2

I have six characters: (,),[,],{,}. They are stored lexicographically: '(' < ')' < '[' < ']' < '{' < '}'. So I can store all balanced parenthesis sequences of length $n$ lexicographically and each sequence will have unique number $1 \dots catalan(n)*3^n$. Than I want to get sequence by its number.

Problem: If I have another order, for example '}' < '[' < ')' < '(' < '{' < ']', then all balanced parenthesis sequences again are stored in lexicographical order, but the same sequences will have different numbers, according to these two orders. If I'm given a number of a sequence in second order, can I efficiently recalculate it to the number of the same sequence but in the first order? The main problem is that I don't know the sequence which corresponds to given number in second order. (I wish to find it)

Example: First order: '(' < ')' < '[' < ']' < '{' < '}'

Second order: '}' < '[' < ')' < '(' < '{' < ']'

I get number 1 for sequences with 1 pair of parthenses, the corresponding sequence is [] in the second order. I want to calculate number of [] in first order --- it is 2.

But I don't know how sequence for number 1 in seconds order looks like. I want just by number get the number of the same sequence in first "canonical" order.

Grigori
  • 125
  • 6

1 Answers1

2

Aside from trivial cases, transforming lexicographic order indices from one ordering to another ordering can can rarely be done more efficiently than unranking the index using the original alphabet ordering, and then ranking the resulting sequence using the new alphabet ordering.

That works fine in this case, because after precomputation of a table of size $O(N^2)$ (which doesn't depend on the chosen ordering), you can rank and unrank an element of the lexicographic sequence with an arbitrary lexical ordering in $O(N)$ time. (The naïve proof-of-concept code I wrote does the ranking and unranking in $O(Nk)$ time, where $k$ is the size of the bracketing algorithm. But it should be possible to get rid of the $k$.)

The algorithm can be found in Ranking and unranking of a generalized Dyck language and the application to the generation of random trees, Jens Liebehenschel (1999). Liebehenschel's algorithm uses a fixed ordering but extending it to arbitrary orderings is straight-forward.

Liebehenschel's generalisation of Dyck languages allows the use of an arbitrary many-to-many relation between opening and closing symbols (although it requires that the set of opening symbols and the set of closing symbols not have common elements). Restricting the relationship to an isomorphism doesn't significantly simplify the algorithm.

rici
  • 12,150
  • 22
  • 40