Friday 25 October 2013

Sipser 2.28 c. {w | the number of a's is at least the number of b's}

2.28 c. {w | the number of a's is at least the number of b's}

Solution:

S → aSbS | bSaS | aS | ε

aSbS | bSaS generates equal number of a and b. Then aS will help you to generate as many a you want. But it is ambiguous.

2.28 b. { w | the number of a's and b's in w are equal}

2.28 b. { w | the number of a's and b's in w are equal}

Solution:

S → aSbS | bSaS | ε

Don't ask me why? Work it out.

Sipser 2.28 a. { w | in every prefix of w the number of a's is at least the number of b's }

2.28 a. { w | in every prefix of w the number of a's is at least the number of b's }

Hint:
Replace a with open bracket ( and b with closing  bracket ). You will get the big picture that he is talking about set of prefixes of balanced paranthesis language.

Solution:
Finding ambiguity free context free grammer is not that trivial.
Very first step to find an unambiguous grammar is finding a grammar, unambiguous or ambiguous.
Secondly, If you see the ambiguity in it then explore it to know it deeper. And set a priority rules set which will tell you which of the parse tree (derivation) you have to pick. And now set production rules.

Initially what clicked me is:
S → SS | (S) | ε
but it is ambiguous. Think !!

After trying a day or a couple, I figured out the grammar should be
S → (S)S | ε

But this is not what we were looking for but grammar in which every string's prefix have more ( than number of  ).
S → (S)S | (S | ε

Please correct me if I am wrong.

Thursday 24 October 2013

Sipser 2.25: CFLs are closed under SUFFIX(A).

2.25 For any language (A), let SUFFIX(A) = { v | uv ∈ A for some string u }. Show that the class of CFLs is closed under the SUFFIX operation.

Hint: Add epsilon transitions to skip symbols.

Solution: For every state A, add a state A′. And new start  state will be S′. For every transition δ(A,0,a) → (B,b) add a transition δ(A′,ε,a) → (B′,b). And δ(A′,ε,ε) → (A,ε).

Wednesday 23 October 2013

2.24 Sipser: Let E = { aibj i ≠ j and 2i ≠ j } Show that D is context-free language.

2.24 Let E = { aibj  | i ≠ j and 2i ≠ j } Show that D is context-free language.

solution:

Split it as unions of  3 languages.

{ aibj | i > j } ∪ { aibj | i < j and 2i > j } ∪ { aibj | 2i < j }

G1 = { aibj | i > j }

G2 = { aibj | i < j and 2i > j }

G3 = { aibj | 2i < j }

G1 and G3 are quite easy to figure out.
for G1

S →   aSb | aS1
S→ aS1 |ε

for G3

S → aSbb | S1b
S1 → S1b | ε

Now tricky part is G2.

S → aSb | aS1b
S1 → aS1bb | aS2bb
S2 → ε

Very clearly, S will generate anamb2mbn that is equivalent to an+mbn+2m where m,n must be greater than 0.

Feel free to comment and correct me if I am wrong. :)

2.23 Let D = {xy | x,y ∈ {0,1}* |x| = |y| but x ≠ y} Show that D is context-free language.

2.23 Let D = {xy | x,y ∈ {0,1}* |x| = |y| but x ≠ y} Show that D is context-free language.

Solution:

It is a bit tricky. 
Strategy is, x and will be first and second half respectively of a string w ∈ D. If we are able to show that after skipping some n position from start in x and y, we get different symbols, consider the problem done. 

<n symbols>0<m symbols>    <n symbols>1<m symbols>

or 

<n symbols>1<m symbols>    <n symbols>0<m symbols>

where m,n ∈ non negative intezers. 


Grammar : 

S →  AB | BA
→ 0A0 | 0A1 | 1A0 | 1A1 | 0
→ 0B0 | 0B1 | 1B0 | 1B1 | 1 

this grammar will generate strings of type 

<n symbols>0<n symbols> <m symbols>1<m symbols>

which is actually equivalent to 

<n symbols>0<m symbols>  <n symbols>1<m symbols>


Feel free to comment to clear doubt. :)

Sunday 20 October 2013

Sipser Second Edition 2.19

2.19
Let CFG G be
S → aSb | bY | Ya
Y → bY | aY | ε
Give a simple description of L(G) in English. Use that description to give a CFG for L(G)c, the complement of L(G).

Solution.
I don't know the exact solution. Here I am discussing what I am thinking.
Clearly, Y generates all strings over a,b. So if we consider only last two rules for S those are bY| Ya, then they will generate all strings { w | w has length at least 1 and w either starts with a 'b' or ends with
a 'a' }. That means these two rule will not generate a string starting with a and ending with b. Now consider S → aSb. This rule generates string starting with a, ending with b and middle will not contain a string ba.
Hence L(G) have every string over a,b but not the strings of even length whose middle(center) have 'ab'. Like it will never generate <substring of length k>ab<substring of length k>.

So its complement will have all strings which look like <substring of length k>ab<substring of length k>.

S → aSb | bSa | aSa | bSb | ba



I expect it to be right, but in case I am not then please comment.

Thursday 17 October 2013

Sipser 2.2 b. CFLs are not closed under Intersection.

2.2 b. Use part (a) and DeMorgan's law to show that the class of CFLs is not closed under complementation.

Sol.


A = {ambncn | m,n ≥ 0}
B = {anbncm | m,n ≥ 0}
A ∩ B = {anbncn | n ≥ 0}
Clearly, A ∩ B is not CFL(pumping lemma). Demorgans' law :

(A ∩ B)c = Ac ∪ Bc

Ac  = {ambicj | m, i, j ≥ 0 & i ≠ j} = {ambicj | m, i, j ≥ 0 & i > j} ∪ {ambicj | m, i, j ≥ 0 & i < j}

now prove that {ambicj | m, i, j ≥ 0 & i > j} is CFL.
{ambicj | m, i, j ≥ 0 & i  <  j} :
S → S1S2
S1 → aS1
S2 → bS2c | S2c | c

Similarly {ambicj | m, i, j ≥ 0 & i < j} is CFL and so is Ac. Similar argument holds for Bc. So Ac ∪ Bc is also CFL(Union of CFL is CFL) . Hence proved complement of CFL is not closed.