1

Lets consider a range of "K" binary digit numbers. In that range, we want to take a subset of those values which have (<="n" consecutive 0s) AND (<="n" consecutive 1s).

Then, we want to find the order of values in the resulting subset incrementally.

Also, we want to do the "un-ordering" of the values. i.e. find the value of the subset from the given order.

Note: The order must be found only by checking the subset value, and not by trying to iterate all values upto that value. This will be efficient for a large "K" range of digits.

Example:

K = 6

n <= 2 consecutive 0s AND 1s

000000 -> (>2 consecutive 0s OR 1s) -> remove from subset
000001 -> (>2 consecutive 0s OR 1s) -> remove from subset
000010 -> (>2 consecutive 0s OR 1s) -> remove from subset
000011 -> (>2 consecutive 0s OR 1s) -> remove from subset
000100 -> (>2 consecutive 0s OR 1s) -> remove from subset
000101 -> (>2 consecutive 0s OR 1s) -> remove from subset
000110 -> (>2 consecutive 0s OR 1s) -> remove from subset
000111 -> (>2 consecutive 0s OR 1s) -> remove from subset
001000 -> (>2 consecutive 0s OR 1s) -> remove from subset
001001 -> (<= 2 consecutive 0s AND 1s) -> order [1]
001010 -> (<= 2 consecutive 0s AND 1s) -> order [2]
001011 -> (<= 2 consecutive 0s AND 1s) -> order [3]
001100 -> (<= 2 consecutive 0s AND 1s) -> order [4]
001101 -> (<= 2 consecutive 0s AND 1s) -> order [5]
001110 -> (>2 consecutive 0s OR 1s) -> remove from subset
001111 -> (>2 consecutive 0s OR 1s) -> remove from subset

010000 -> (>2 consecutive 0s OR 1s) -> remove from subset 010001 -> (>2 consecutive 0s OR 1s) -> remove from subset 010010 -> (<= 2 consecutive 0s AND 1s) -> order [6] 010011 -> (<= 2 consecutive 0s AND 1s) -> order [7] 010100 -> (<= 2 consecutive 0s AND 1s) -> order [8] 010101 -> (<= 2 consecutive 0s AND 1s) -> order [9] 010110 -> (<= 2 consecutive 0s AND 1s) -> order [10] 010111 -> (>2 consecutive 0s OR 1s) -> remove from subset 011000 -> (>2 consecutive 0s OR 1s) -> remove from subset 011001 -> (<= 2 consecutive 0s AND 1s) -> order [11] 011010 -> (<= 2 consecutive 0s AND 1s) -> order [12] 011011 -> (<= 2 consecutive 0s AND 1s) -> order [13] 011100 -> (>2 consecutive 0s OR 1s) -> remove from subset 011101 -> (>2 consecutive 0s OR 1s) -> remove from subset 011110 -> (>2 consecutive 0s OR 1s) -> remove from subset 011111 -> (>2 consecutive 0s OR 1s) -> remove from subset

100000 -> (>2 consecutive 0s OR 1s) -> remove from subset 100001 -> (>2 consecutive 0s OR 1s) -> remove from subset 100010 -> (>2 consecutive 0s OR 1s) -> remove from subset 100011 -> (>2 consecutive 0s OR 1s) -> remove from subset 100100 -> (<= 2 consecutive 0s AND 1s) -> order [14] 100101 -> (<= 2 consecutive 0s AND 1s) -> order [15] 100110 -> (<= 2 consecutive 0s AND 1s) -> order [16] 100111 -> (>2 consecutive 0s OR 1s) -> remove from subset 101000 -> (>2 consecutive 0s OR 1s) -> remove from subset 101001 -> (<= 2 consecutive 0s AND 1s) -> order [17] 101010 -> (<= 2 consecutive 0s AND 1s) -> order [18] 101011 -> (<= 2 consecutive 0s AND 1s) -> order [19] 101100 -> (<= 2 consecutive 0s AND 1s) -> order [20] 101101 -> (<= 2 consecutive 0s AND 1s) -> order [21] 101110 -> (>2 consecutive 0s OR 1s) -> remove from subset 101111 -> (>2 consecutive 0s OR 1s) -> remove from subset

110000 -> (>2 consecutive 0s OR 1s) -> remove from subset 110001 -> (>2 consecutive 0s OR 1s) -> remove from subset 110010 -> (<= 2 consecutive 0s AND 1s) -> order [22] 110011 -> (<= 2 consecutive 0s AND 1s) -> order [23] 110100 -> (<= 2 consecutive 0s AND 1s) -> order [24] 110101 -> (<= 2 consecutive 0s AND 1s) -> order [25] 110110 -> (<= 2 consecutive 0s AND 1s) -> order [26] 110111 -> (>2 consecutive 0s OR 1s) -> remove from subset 111000 -> (>2 consecutive 0s OR 1s) -> remove from subset 111001 -> (>2 consecutive 0s OR 1s) -> remove from subset 111010 -> (>2 consecutive 0s OR 1s) -> remove from subset 111011 -> (>2 consecutive 0s OR 1s) -> remove from subset 111100 -> (>2 consecutive 0s OR 1s) -> remove from subset 111101 -> (>2 consecutive 0s OR 1s) -> remove from subset 111110 -> (>2 consecutive 0s OR 1s) -> remove from subset 111111 -> (>2 consecutive 0s OR 1s) -> remove from subset

Dave
  • 25
  • 4

1 Answers1

3

These are known as ranking and unranking algorithms. See, e.g., https://computationalcombinatorics.wordpress.com/2012/09/10/ranking-and-unranking-of-combinations-and-permutations/

Let $\text{rank}(x)$ denote the rank of $x \in \{0,1\}^K$, i.e., the number of $K$-bit strings that lexicographically precede $x$ and satisfy your requirements (no more than $n$ consecutive 0's or 1's).

Define $f_{K,n}(w)$ as the number of $K$-bit strings that start with $w$ and that satisfy your requirements (no more than $n$ consecutive 0's or 1's). $f_{K,n}(w)$ can be computed in polynomial time by forming a DFA for the regular language of $K$-bit strings that start with $w$ and satisfy your requirements, and then counting the number of strings accepted by this DFA. (See https://cstheory.stackexchange.com/q/8200/5038 and https://cs.stackexchange.com/a/71379/755.)

$\text{rank}(x)$ can be expressed as a sum of at most $K$ terms of the form $f_{K,n}(w)$. Specifically,

$$\text{rank}(x) = \sum_{i=1}^K x_i f_{K,n}(x_{1 \cdots i-1} 0).$$

By the above remarks, this gives a polynomial-time algorithm for computing $\text{rank}(x)$.

Let $\text{unrank}(n)$ denote the unranking of $n \in \mathbb{N}$, i.e., the string $x$ such that $\text{rank}(x)=n$. You can obtain an algorithm for computing $\text{unrank}(n)$ in polynomial time, using filling in one bit of $x$ at a time and calling $\text{rank}(\cdot)$ at each step. For instance, if $\text{rank}(100\cdots 0) > n$, then $x$ starts with 0, otherwise $x$ starts with $1$; and so on.

D.W.
  • 167,959
  • 22
  • 232
  • 500