Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Challenges

Comments on Fast sampling of special binary strings

Post

Fast sampling of special binary strings

+2
−1

We are going to define a simple little language. A word in this language is a binary string where the longest run of consecutive $0$s, is shorter than every run of $1$s which is not contained within a larger run (a "maximal" run). So for example: \begin{align} 001110111110011 \end{align} which we will write as \begin{align} 0^21^301^50^21^2 \end{align} is not legal, because the following red segment is just as long as the blue segment: \begin{align} {\color{red}0^2}1^301^50^2{\color{blue}1^2} \end{align} The string: \begin{align} 0^31^401^80^31^5 \end{align} is valid, because the longest run of $0$s has length three and the shortest maximal run of $1$s has length four.

Strings of the form $0^n$ are somewhat ambiguous in the above description. Technically they should be allowed, since there are no runs of $1$s at all, but they seem to in some sense violate the spirit of the description. Whether they are included is up to you, it will not make a huge difference for the challenge.

Challenge

Given a non-negative integer $n$, uniformly sample from the words of length $n$ in the language. Your algorithm must have sub-exponential average-case time complexity in terms of the input value.

You may choose a word using a built-in source of randomness, or you can accept as an additional input a source of randomness (e.g. a infinite list of 1s and 0s, or a stateful blackbox function).

This is code-golf so the goal is to minimize the size of your source code as measured in bytes.

Non-solutions

An easy would-be solution is to sample randomly on binary strings of the correct length until you get something in the language.

We will show that this does not work. This gives a uniform distribution but fails because the average-case time complexity is exponential. On average you will fail exponentially many times before you succeed.

To see this notice that the string $010$ is never a legal substring in our language, regardless of context. So there are at most 7 possible length 3 segments that can extend a valid word at any point.

So the rate at which words in our language grow is $O\left(\sqrt[3]{7}^n\right)$ (we use the $O$-notation here precisely, meaning it is bounded above) while all binary strings grow at a rate of $2^n$. $2 = \sqrt[3]{8}>\sqrt[3]{7}\approx 1.913$.

You might try to generate random strings which don't contain $010$ instead (this can be done uniformly and efficiently), and then reject the ones that don't work. However a general version of this argument can show that any finite set of forbidden substrings will either rule out some words you want to sample (i.e. make the sampling non-uniform) or it will require exponentially many samples (i.e. the base of the exponent is too large).

History

1 comment thread

How about explaining in plain English? (6 comments)
How about explaining in plain English?
Olin Lathrop‭ wrote 10 months ago

It seems you want strings of 0s and 1s where the longest run of 0s is shorter than the shortest run of 1s. But, then you confuse things with "maximal". However, the real problem is the challenge definition. You're given a number, but you don't say what to do with that number. Then "sample" really confuses things. I thought you wanted the code to generate the strings, but they have to be previously existing to sample them.

The whole "Non-solutions" makes things more confusing. It seems you want solutions that create (sample?) the strings without guess-and-check methods.

WheatWizard‭ wrote 10 months ago

It is unclear what you are asking for. If you are confused it is more helpful to state what your confusion is rather than the words you are confused by, because I am obviously not confused by those words.

trichoplax‭ wrote 10 months ago

My understanding of "maximal" in this context is that a string of 1s only counts as a maximal run of 1s if it does not have a 1 before or after it. So 01110 contains a maximal run of 3 1s, but does not contain a maximal run of 2 1s, even though the run of 3 1s does contain a (non-maximal) run of 2 1s as a substring.

I could reach this understanding by working backwards from the examples, but the word "maximal" did not immediately explain this to me. I can't think of a better word for this that would be clear to people not familiar with this topic, so it might be worth sticking with the word "maximal" but defining it before its first usage.

My first attempt at a definition:

A maximal run of 1s is a contiguous string of 1s that is not part of a longer contiguous string of 1s.

This or a better wording could either be included near the beginning of the challenge wording, or as a footnote on the first usage of "maximal".

WheatWizard‭ wrote 10 months ago

trichoplax‭ Ah thanks. I've gone ahead and given a description first and then in a parenthetical given "maximal" as our shorthand. Maybe this clarifies that. I'm not sure if OP is confused about the definitions of these terms or something else.

trichoplax‭ wrote 10 months ago

That introduction of "maximal" makes it easier to understand for me personally - thank you.

In rereading to confirm, I noticed a potential typo:

You might try to generate random strings which don't contain $101$ instead

Was this $101$ intended to be $010$?

WheatWizard‭ wrote 10 months ago

trichoplax‭ Yes. Thanks. I corrected that in the SE version of the post but forgot to change it here when I did.