Skip to content
Official PYQ · 2020

GATE CS 2020 Q32 — Regular and Context-free Languages

Explain with AI

Consider the following languages.

L1={wxyx∣w,x,y∈(0+1)+}L2={xy∣x,y∈(a+b)∗,∣x∣=∣y∣,x≠y}\begin{array}{ll} L_1= \{ wxyx \mid w,x,y \in (0+1)^{+} \} \\ L_2= \{xy \mid x,y \in (a+b)^{*}, \mid x \mid=\mid y \mid, x \neq y \} \end{array}

Which one of the following is TRUE?

Choose one option
View worked solution

Answer: A

To determine the classes of the given languages, we analyze L1L_1 and L2L_2 individually:

Analysis of L1L_1:

The language is defined as L1={wxyx∣w,x,y∈(0+1)+}L_1 = \{ wxyx \mid w,x,y \in (0+1)^{+} \}. Let xx end with a single symbol a∈{0,1}a \in \{0, 1\}, so we can write x=x′ax = x'a where x′∈(0+1)∗x' \in (0+1)^*. Substituting this back:

wxyx=w(x′a)y(x′a)=(wx′)a(yx′)awxyx = w(x'a)y(x'a) = (wx')a(yx')a

Let w1=wx′w_1 = wx' and y1=yx′y_1 = yx'. Since w,y∈(0+1)+w, y \in (0+1)^+, we must have w1,y1∈(0+1)+w_1, y_1 \in (0+1)^+. Thus, any string in L1L_1 can be simplified to have a single-symbol matching marker at the end of each segment. This allows us to write L1L_1 as the union of two regular languages:

L1={w10y10∣w1,y1∈(0+1)+}∪{w11y11∣w1,y1∈(0+1)+}L_1 = \{ w_1 0 y_1 0 \mid w_1, y_1 \in (0+1)^+ \} \cup \{ w_1 1 y_1 1 \mid w_1, y_1 \in (0+1)^+ \}

This corresponds to the regular expression (0+1)+0(0+1)+0+(0+1)+1(0+1)+1(0+1)^+ 0 (0+1)^+ 0 + (0+1)^+ 1 (0+1)^+ 1. Hence, L1L_1 is regular.

Analysis of L2L_2:

L2={xy∣x,y∈(a+b)∗,∣x∣=∣y∣,x≠y}L_2 = \{xy \mid x,y \in (a+b)^{*}, |x|=|y|, x \neq y \} represents the set of even-length strings where the first half is not equal to the second half. This means there is at least one position ii where the symbol in the first half differs from the symbol in the second half.

We can generate L2L_2 using the following Context-Free Grammar (CFG):

S→AB∣BAA→aAa∣aAb∣bAa∣bAb∣aB→aBa∣aBb∣bBa∣bBb∣b\begin{aligned} S &\to AB \mid BA \\ A &\to aAa \mid aAb \mid bAa \mid bAb \mid a \\ B &\to aBa \mid aBb \mid bBa \mid bBb \mid b \end{aligned}

Here, AA generates odd-length strings with aa in the middle, and BB generates odd-length strings with bb in the middle. The concatenation ABAB ensures that the mismatch occurs at the exact corresponding index in both halves. Thus, L2L_2 is context-free (but not regular).

Free practice · Your answers on this page are not saved.

Published
Report a correction
Keep exploringMore Regular and Context-free Languages questions