Is the following language context free? $$L = \{ uxvy \mid u,v,x,y \in \{ 0,1 \}^+, |u| = |v|, u \neq v, |x| = |y|, x \neq y\} $$

As pointed out by sdcvvc, a word in this language can also be described as the concatenation of two words of the same length the hamming distance of which is 2 or greater.

I think it's not context free but I'm having a hard time proving it. I tried intersecting this language with a regular language (like $ \ 0^*1^*0^*1^* $ for example) then use the pumping lemma and \ or homomorphisms but I always get a language that is too complicated to characterize and write down.

  • $\begingroup$ Did you try pumping the string $0^u1^x1^u0^x$? $\endgroup$
    – Pål GD
    Commented Apr 27, 2013 at 14:19
  • $\begingroup$ Yes, but I've failed to pump this string out of the language (it doesn't mean that it's not possible, just that I've failed to do so). $\endgroup$
    – Robert777
    Commented Apr 27, 2013 at 15:45
  • 1
    $\begingroup$ @PålGD, you'd probably need a way to "mark" the pieces, like $1^u 0 1^x 0 1^u 0 1^x 0$ $\endgroup$
    – vonbrand
    Commented Apr 27, 2013 at 21:44
  • 9
    $\begingroup$ This language can be written as $\{uv:|u|=|v|,d(u,v) \geq 2\}$ where $d$ is the Hamming distance. Note that if we replace 2 by 1, it is contextfree (cs.stackexchange.com/questions/307) but the trick used there will not work. Personally I'm betting it is not contextfree. $\endgroup$
    – sdcvvc
    Commented Jun 10, 2013 at 22:19
  • 1
    $\begingroup$ @sdcvvc: You are right, one partitions the $u$ into $u'x$ so that one of the differing bits is in $u'$ and the other in $x$. I stand corrected. $\endgroup$ Commented Jun 12, 2013 at 21:20

3 Answers 3


Note [2019-07-30] The proof is wrong ... the question is more complicated than it sounds.

After a failed attempt here it is another idea.

If we intersect $L$ with the regular language $L_{reg} = 0^*10^*10^*10^*$ we get a CF language.

Perhaps we can have more luck if we use $L_{reg}' = 0^*10^*10^*10^*10^*$ (a string with exactly 4 1s).

Let $L_1 = L \cap L_{reg}'$, informally $w \in L_1$ if it can be split in two halves, such that one half contains exactly $\{0,1,3,4\}$ $1s$ or both halves contain two $1$s but their positions don't match.

Suppose that $L_1$ is CF and let $G$ be its grammar in Chomsky normal form, and let

$$w = uv = 0^a 1 0^b 1 0^c 1 0^d 1 0^e \in L_1$$

We have $|u|=|v|$ (even length) and $d(u,v) \geq 2$

If we restrict our attention to the ways in which the four 1s of $w$ can be generated we have the three cases shown at the top of the figure 1. The central part of the figure 1 shows the first case (but the others are similar).

enter image description here
Figure 1 (the full picture can be downloaded here)

If we pick $a=e, c=2a$ and $b,d \gg a$ we see that the zeros between the two pairs of 1s must be independently pumpable (red nodes in the figure): in particular, for large enough $b \gg a$, we get a duplicate nonterminal node on a internal subtree (node X in figure 2) or a repeated subsequence in the path towards the first or the second 1 (node Y in figure 2). Note that Figure 2 is a little bit simplified: there can be more nonterminal nodes between the two $X$s, and also between the two $Ys$ ($Y\to ... \to Z_i \to ... Y$ but with $Z_i$ that produces only 0s on the right of the first 1).

enter image description here
Figure 2

So we can fix an arbitrary $a = e = k, c = 2a$, then pick large enough $b$ to get an independently pumpable node on the sequence of zeros between first and second $1$. For the sequence of zeros between the third and fourth 1, we can choose $d = b! +b$.
But $0^b$ is independtly pumpable so there is a $p \leq b$ pumpable substring $y$, i.e. such that $b = xyz, |y|=p, |x|\geq 0, |z|\geq 0$ and $xy^iz = b!+b$. The string we get is:

$$w' = 0^k 1 0^{b!+b} 1 0^{2k} 1 0^{b!+b} 1 0^k$$

but $w' \notin L_1$. Thus $L_1$ is not CF and finally $L$ is not CF.

If the proof is correct (???) it can be extended to every language $L_k = \{ uv : |u|=|v|, d(u,v)\geq k\}, k\geq 2$

  • $\begingroup$ I'm afraid the bounty will expire before we can actually verify this proof, so unless any drastic information arises in the next 4 hours, this gets the points for being the best attempt so far. $\endgroup$ Commented Jun 20, 2013 at 16:09
  • $\begingroup$ @jmite: don't worry there are high chances that it is a wrong attempt like the previous one (which lasted for about 30 mins before discovering a trivial error) :-) :-) $\endgroup$
    – Vor
    Commented Jun 20, 2013 at 16:17
  • $\begingroup$ Why the case distinction? The branches in the grammar have no relation with the halves of the word. But I think it does not matter; if the proof works, this case distinction is not needed. Looking at an assumed grammar and using the proof of the Pumping lemma instead of the lemma itself is a nice trick (one should do this more often). I have one (real) concern: if you pump a substring of $0^b$, you get $0^{b+p(i-1)}$; I don't see how you get to $b+b!$. Don't think that should harm the proof, but better check. Also, you might want to straighten out some notation (and typos). $\endgroup$
    – Raphael
    Commented Jun 21, 2013 at 6:13
  • 1
    $\begingroup$ @Raphael: thanks for the comments. Perhaps I'm wrong, but if you pick as target length $b+b!$ then for every pumping length $p$, the string $0^b$ can be decomposed in $0^{xyz}, (|xyz|=b, |y|=p \leq b)$ and can be pumped to $xy^iz = b + b!$, indeed in your example p surely divides $b!$, so there is a $(i-1)$ for which $p(i-1)=b!$, but the original string length is $b$, so the total pumped length is $|xy^{(i-1)}z| = b+b!$. I remember it from a couple of exercises that use the Ogden's lemma ... now I'll double check them. $\endgroup$
    – Vor
    Commented Jun 21, 2013 at 6:40
  • $\begingroup$ @Raphael: ... I didn't find the proof anywhere but only a paper by Zach Tomaszewski that proves that the complement of $L_{dup} = \{ ww \}$ is CF (see question ), so perhaps it is a new result (though simple); and a pumping-lemma-style theorem can be derived for languages with strings that contain a finite number of a particular symbol and substrings of arbitary length between them. $\endgroup$
    – Vor
    Commented Jun 21, 2013 at 6:53

After 2 failed attempts, that were disproved by @Hendrik Jan (thank you), here is another one, that is not more successful. @Vor found an example of a deterministic CF language where the same construction would apply, if correct. This allowed identifying an error in the anchoring of the $y$ string in the application of the lemma. The lemma itself does not seem at fault. This is clearly too simplistic a construction. See more details in the comments.

The language $L = \{ uxvy \mid u,v,x,y \in \{ 0,1 \}^*\text \{ \epsilon \} \ ,\ \mid u \mid = \mid v \mid \ , \ u \not= v \ , \ \mid x \mid = \mid y \mid \ , \ x \not= y \ \} $ is not Context-Free.

It is helpful to keep in mind the characterization $L= \{uv:|u|=|v|,d(u,v) \geq 2\}$ where d is the Hamming distance, proposed by @sdcvvc. What one needs to think about are 2 selected positions in each half string such that the corresponding symbols differ.

Then you consider a string $10^i10^j$ such that $i \lt j$ and $i+j$ is even. It is clearly in the language L, by cutting $u$ and $x$ anywhere between the two 1's. We want to pump that string on the first part between the 1's, so that it will become $10^j10^j$ which is not supposed to be in the language.

We first try to use Ogden's lemma, which is like the pumping lemma, but applies to $p$ or more distinguished symbols that are marked on the string, $p$ being the pumping length for marked symbols (but the lemma can pump more because it can pump also unmarked symbols). The pumping marked-length $p$ depends only on the language. This attempt will fail, but the failure will be a hint.

We can then choose $i=p$ and we mark symbols on the first sequence of $i$ 0's. We know that none of the two 1's will be in the pump, because it can pump out once (exponent 0) instead of pumping in. And pumping out the 1's would get us out of the language.

However, we could be pumping on both sides of the second 1 as fast or even faster on the right side, so that the second 1 would never get across the middle of the string. Also Ogden's lemma does not fix an upper limit to the size of what is being pumped, so that it is not possible to organize the pumping to get the rightmost 1 exactly across the middle of the string.

We use a modified version of the lemma, here called Nash's Lemma, which can handle these difficulties.

We first need a definition (it probably has another name in the literature, but I do not know which - help is welcome). A string $u$ is said to be an erasure of a string $v$ iff it is obtained from $v$ by erasing symbols in $v$. We will note $u \prec v$.

Nash's Lemma : If $L$ is a context-free language, then there exists two numbers $p\gt0$ and $q\gt 0$ such that for any string $w$ of length at least $p$ in $L$, and every way of “marking” $p$ or more of the positions in $w$, $w$ can be written as $w=uxyzv$ with string $u$, $x$, $y$, $z$, $v$, such that

  1. $xz$ has at least one marked position,
  2. $xyz$ has at most $p$ marked positions, and
  3. there are 3 strings $\hat x$, $\hat y$, $\hat z$ such that
    1. $\hat x \prec x$, $\hat y \prec y$, $\hat z \prec z$,
    2. $1 \leq \mid \hat x \hat z \mid \leq q$, $1 \leq \mid \hat y \mid \leq q$, and
    3. $ux^j\hat x^i\hat y\hat z^iz^jv$ is in $L$ for every $i \geq 0$ and for every $j \geq 0$.

Proof: Similar to the proof of Ogden's lemma, but the subtrees corresponding to the strings $y$ and $xz$ are pruned so that they do not contain any path with twice the same non-terminal (except for the roots of these two subtrees). This necessarily limits the size of the generated strings $\hat x\hat z$ and $\hat y$ by a constant $q$. The strings $x^j$ and $z^j$, for $ j \geq 0$, corresponding to an unpruned version of the tree, are used mainly with $j=1$ to simplify the accounting when the lemma is applied.

We modify the above proof attempt by marking the $p$ leftmost symbols 0, but they are followed by $2q$ symbols 0 to make sure that we pump in the left part of the string, between the two 1's. That make a total of $i = p + 2q$ 0's between the 1's (actually $i = p + q$ would be sufficient, since the rightmost 1 cannot be in $\hat z$, which would allow to simply remove it).

What is left is to have chosen $j$ so that we can pump exactly the right number of 0's so that the two sequences are equal. But so far, the only constraint on $j$ is to be greater than $i$. And we also know that the number of 0's that are pumped at each pumping is between 1 and q. So let $h$ be product of the first $q$ integers. We choose $j=i+h$.

Hence, since the pumping increment $d$ - whatever it is - is in $[1,q]$, it divides $h$. Let $k$ be the quotient. If we pump exactly $k$ times, we get a string $10^j10^j$ which is not in the language. Hence L is not context-free.


I think that I shall never see
A string lovely as a tree.
For if it does not have a parse,
The string is naught but a farce

  • $\begingroup$ Note however that the pass over the second half reads the stack in reverse. That seems to mean that the two positions are in the same position in both halves, but in reverse? $\endgroup$ Commented Jun 11, 2013 at 13:36
  • $\begingroup$ you are correct ... I goofed ... now I know what was nagging me at the back of my head. $\endgroup$
    – babou
    Commented Jun 11, 2013 at 13:45
  • $\begingroup$ I recognized the argument (because I could not make it work when I tried myself). $\endgroup$ Commented Jun 11, 2013 at 14:00
  • $\begingroup$ Should I leave this wrong answer ? It is somehow helping, I think, as it make the problem suspiciously similar to ${a^ib^jc^ka^ib^jc^k}$. The problem is that rules of the site are not intended to encourage wrong results for discussion ( I mean I do not enjoy downvotes more than anyone else). $\endgroup$
    – babou
    Commented Jun 11, 2013 at 14:04
  • $\begingroup$ @HendrikJan Did I goof again ? (BTW, thanks for making it a discussion) $\endgroup$
    – babou
    Commented Jun 11, 2013 at 17:58

by this question I think $L$ is context-free and generated by the following grammar $\qquad\begin{align} S &\to AXBY \mid BYAX \\ A &\to 0 \mid 0A0 \mid 0A1 \mid 1A0 \mid 1A1 \\ B &\to 1 \mid 0B0 \mid 0B1 \mid 1B0 \mid 1B1 \\ X &\to 0 \mid 0X0 \mid 0X1 \mid 1X0 \mid 1X1 \\ Y &\to 1 \mid 0Y0 \mid 0Y1 \mid 1Y0 \mid 1Y1 \\ \end{align}$

  • 4
    $\begingroup$ This is incorrect; you cannot guard that length of AX is the same as BY. For example, your grammar generates S -> AXBY -> A011 -> 0A1011 -> 001011 which is not in the original language. Also, your symbols A and X generate the same language, same for B and Y; they can be merged. $\endgroup$
    – sdcvvc
    Commented Jun 15, 2013 at 20:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.