1. Gyanastra
  2. Theory of Computation
  3. Properties of CFLs
  4. Proving Languages Non-Context-Free (a^n b^{n^2}, a^n b^{2^n}, ww, a^n b^n c^m)
Free lesson, no account needed 14 min read 8 practice questions after it
Asked in past papers23 times
  • GATE CSE20222021202020192017201620152014201120102006200520011999
  • ISRO2017
  • UGC NET20232018201720162015201420062005

Theory of Computation · Properties of CFLs

Proving Languages Non-Context-Free (a^n b^{n^2}, a^n b^{2^n}, ww, a^n b^n c^m)

Overview

This section classifies a list of languages as regular, deterministic context-free (DCFL), context-free (CFL), or none of these, and justifies each classification from the limits of a pushdown automaton (PDA).
The central tool is the observation that a PDA loop can only add or remove a constant amount per iteration, so it can match counts that form an arithmetic progression (AP) in nn but never counts such as n2n^2, 2n2^n, or 3n3^n.

Key concepts

  • PDA capabilities: a PDA can only push symbols on the stack, pop symbols from the top of the stack, and run a finite number of loop states.
  • Fixed-multiple comparison: a PDA can compare two counts only when the second count is a fixed multiple of the first (e.g. nn vs 2n2n): put the second block in a loop and pop a fixed number of stack symbols per loop iteration.
  • Non-AP counts: a PDA cannot handle a second count that is not an arithmetic progression in nn — e.g. n2n^2, 2n2^n, 3n3^n — because a loop can only add a constant amount per iteration.
  • Top-only access: a PDA cannot compare an input symbol with the bottom of the stack; it sees only the top.
  • No re-reading, no empty-stack comparison: a PDA cannot go back and re-read input already consumed, and once the stack is emptied by an earlier comparison there is nothing left to compare a later block against.
  • Non-determinism: non-determinism can guess a centre (so wwRww^R is a CFL) but cannot fix the bottom-of-stack problem (so wwww is not a CFL).
  • Finite languages: every finite language is regular; every regular language is deterministic context-free; every DCFL is context-free.
    Hierarchy: regular ⊂\subset DCFL ⊂\subset CFL; if a language is not context-free it is neither DCFL nor regular.

How it works

  1. L={anbn2∣n≥0}L = \{a^n b^{n^2} \mid n \ge 0\} — not context-free. Not regular: an unbounded number of aa's must be saved and compared against a count derived from nn.
    Not context-free: push nn aa's; accepting would require a loop whose iteration count is n2n^2, but a loop can pop only a fixed number of aa's per iteration (e.g. 1 aa per iteration, as in anb2na^n b^{2n}).
    Since n2n^2 is not in AP, no loop can realise the count n2n^2. Hence no PDA — deterministic or non-deterministic — exists.
  2. L={anb2n∣n≥0}L = \{a^n b^{2^n} \mid n \ge 0\} — not context-free. Not regular (unbounded comparison). Not context-free: a loop would need to contain 2n2^n bb's, and 2n2^n is not in AP, so no loop can have 2n2^n as its count.
    Contrast: anb2na^n b^{2n} is context-free because 2n2n is in AP — one loop iteration pops exactly 1 aa.
  3. L={wwR∣w∈{a,b}∗}L = \{w w^R \mid w \in \{a,b\}^*\} — CFL but not DCFL, not regular. This is exactly the set of all even-length palindromes. Not regular: an unbounded ww cannot be saved in a finite automaton and compared with wRw^R.
    Context-free: push ww, then pop while matching wRw^R — a non-deterministic PDA guesses the centre. Not deterministic context-free: the centre is unknown, so a DPDA cannot know up to which point to push and from which point to pop.
    It is a non-deterministic CFL.
  4. L={ww∣w∈{a,b}∗}L = \{w w \mid w \in \{a,b\}^*\} — not context-free. Not regular (unbounded ww must be stored). Example string: abababab must be accepted; we must save w=abw = ab and then compare with the second abab.
    Non-determinism can guess the centre (at every point guess "centre has come / not come") — that part is fine.
    The fatal problem: after guessing the centre, the first input symbol of the second copy must be compared with the bottom of the stack (the first symbol of ww pushed earliest).
    A PDA can compare the input only with the top of the stack. Since the stack cannot examine its bottom, no PDA accepts wwww. Hence not context-free.
  5. L={anbncm∣L = \{a^n b^n c^m \mid with a comparison between nn and m}m\} — not context-free. Not regular, since anbna^n b^n is already inside it. Attempt with a PDA: push one aa for every aa; pop one aa for every bb.
    Then anbna^n b^n is accepted and the stack is empty when the cc's start.
    Now the cc's must be compared with nn (i.e. with the aa's/bb's), but (i) nothing is left on the stack, and (ii) the input already read cannot be re-read. So there is nothing to compare the cc's against.
    Attempted fix: push 2 aa's per aa, pop 1 aa per bb. Then you can compare aa's with bb's, but you cannot compare how many bb's are left with the cc's. Nothing works.
    Counter-example showing the "push 2, pop 1" trick is wrong: with that scheme, aabbcaabbc is accepted (4 aa's pushed, 2 popped, cc compared against the 2 remaining) — but aabcaabc is also accepted (4 aa's pushed, 1 popped, 2 remaining, cc compared) even though it should not be.
    So the logic fails. Conclusion: not context-free.
  6. L={anbn∣n<1010}L = \{a^n b^n \mid n < 10^{10}\} — regular (hence DCFL, hence CFL). The bound makes the language finite; every finite language is regular; every regular language is deterministic context-free; every DCFL is context-free.
  7. L={anb2nc3n∣n≥0}L = \{a^n b^{2n} c^{3n} \mid n \ge 0\} — not context-free. Push 2 aa's per aa, pop 1 aa per bb: the aa-vs-bb comparison succeeds, but by then the stack top is empty, so the cc's cannot be compared with the aa's. Not context-free.
    Attempted fix: push 5 aa's per aa and pop 1 aa per bb and per cc. Flaw: this accepts any string with 5i=j+k5i = j + k (ii aa's, jj bb's, kk cc's) — e.g. it accepts ab4a b^4 (one aa, four bb's) and other wrong strings.
    So the language recognised would be {aibjck∣5i=j+k}\{a^i b^j c^k \mid 5i = j + k\}, not the required one. Hence not context-free.
  8. L={xcy∣x,y∈{0,1}∗}L = \{x c y \mid x, y \in \{0,1\}^*\} — regular (hence DCFL, hence CFL). This is just the set of all strings containing cc as a substring — a regular language, so deterministic context-free and context-free.
  9. L={xxR∣x∈{a,b}∗, ∣x∣=l}L = \{x x^R \mid x \in \{a,b\}^*,\ |x| = l\} — regular (hence DCFL and CFL). With the length fixed to exactly ll, the number of possible strings is 2l2^l — a finite language. Finite ⇒\Rightarrow regular ⇒\Rightarrow (deterministic) context-free.
  10. L={wwwR∣w∈{a,b}∗}L = \{w w w^R \mid w \in \{a,b\}^*\} — not context-free. It contains the wwww pattern; since wwww cannot be recognised by any PDA, adding one more part wRw^R does not make it context-free. Hence not context-free, not DCFL, not regular.
  11. L={anb3n∣n≥0}L = \{a^n b^{3^n} \mid n \ge 0\} — not context-free. Push all aa's; the bb's would need a loop containing 3n3^n bb's. 3n3^n is not in AP, so — for the same reason as n2n^2 and 2n2^n — no PDA exists. Hence not context-free, not DCFL, not regular.
  12. L={ambn∣m≠n}L = \{a^m b^n \mid m \ne n\} — context-free and deterministic context-free. Take the ordinary PDA for equal counts (push aa's, pop for bb's) and accept in two ways:
    • if after all bb's the stack still contains aa's (input finished, stack not empty) ⇒m>n\Rightarrow m > n;
    • if the stack reaches z0z_0 (bottom marker) while bb's are still remaining ⇒n>m\Rightarrow n > m. It is deterministic: we know exactly when to push (on aa's) and when to pop (on bb's). So it is a DCFL.
  13. L={ambn∣m=2n+1}L = \{a^m b^n \mid m = 2n + 1\} — context-free (deterministic). Construction: in a loop over the aa's, push one aa for every two aa's, and leave one aa unrecorded (the "+1"). Then for every bb, pop one aa.
    Equivalently: for the first aa push nothing (leave it), and for every second aa push one aa; then pop one aa per bb.

Formulas

  • anbn2a^n b^{n^2}: second block count =n2= n^2, not in AP ⇒\Rightarrow no PDA loop can generate it.
  • anb2na^n b^{2^n}: second block count =2n= 2^n, not in AP ⇒\Rightarrow no PDA.
  • anb3na^n b^{3^n}: second block count =3n= 3^n, not in AP ⇒\Rightarrow no PDA.
  • anb2na^n b^{2n}: second block count =2n= 2n, in AP ⇒\Rightarrow CFL (pop 1 aa per loop iteration).
  • ambna^m b^n, m=2n+1m = 2n + 1: push 1 aa per 2 aa's, keep 1 aa aside; pop 1 aa per bb.
  • "Push 5 aa's per aa, pop 1 per bb and per cc" recognises {aibjck∣5i=j+k}\{a^i b^j c^k \mid 5i = j + k\} — the wrong language.
  • ∣{xxR∣x∈{a,b}∗, ∣x∣=l}∣=2l|\{x x^R \mid x \in \{a,b\}^*,\ |x| = l\}| = 2^l (finite ⇒\Rightarrow regular).

Worked examples

Example A – aabbcaabbc under the "push 2 aa's per aa, pop 1 per bb" scheme.

  • Read aa: push 2 aa's; read aa: push 2 aa's ⇒\Rightarrow stack has 4 aa's.
  • Read bb: pop 1 aa ⇒\Rightarrow 3 aa's; read bb: pop 1 aa ⇒\Rightarrow 2 aa's.
  • Read cc: compare with the 2 aa's on the stack ⇒\Rightarrow accepted (correct here).

Example B – aabcaabc under the same scheme (the flaw).

  • Read aa: push 2; read aa: push 2 ⇒\Rightarrow 4 aa's.
  • Read bb: pop 1 ⇒\Rightarrow 3 aa's.
  • Read cc: compare with the remaining aa's ⇒\Rightarrow accepted, although aabcaabc should be rejected. Hence the scheme is wrong; the language is not context-free.

Example C – "push 5 aa's per aa, pop 1 per bb and per cc".

  • String ab4a b^4: read aa ⇒\Rightarrow push 5 aa's; read 4 bb's ⇒\Rightarrow pop 4 aa's ⇒\Rightarrow 1 aa left; input ends with stack non-empty ⇒\Rightarrow accepted, but ab4∉{anb2nc3n}a b^4 \notin \{a^n b^{2n} c^{3n}\}. The scheme in fact recognises {aibjck∣5i=j+k}\{a^i b^j c^k \mid 5i = j + k\}.

Example D – wwww with w=abw = ab, string abababab.

  • Push aa, push bb (ww saved). Guess centre. Next input is aa, but the stack top is bb; the aa needed for comparison is at the bottom of the stack, which a PDA cannot see ⇒\Rightarrow no PDA.

Example E – ambna^m b^n, m≠nm \ne n.

  • Input aabaab (m=2m = 2, n=1n = 1): push aa, push aa; read bb, pop aa ⇒\Rightarrow stack has one aa left, input finished, stack not empty ⇒m>n\Rightarrow m > n ⇒\Rightarrow accept.
  • Input abbabb (m=1m = 1, n=2n = 2): push aa; read bb, pop aa ⇒\Rightarrow stack =z0= z_0; another bb arrives while stack is z0z_0 ⇒n>m\Rightarrow n > m ⇒\Rightarrow accept.
  • Input abab (m=nm = n): stack =z0= z_0 exactly when input ends ⇒\Rightarrow reject.

Comparison

LanguageRegular?DCFL?CFL?Reason
anbn2a^n b^{n^2}NoNoNon2n^2 not in AP; no PDA loop can count n2n^2
anb2na^n b^{2^n}NoNoNo2n2^n not in AP
anb3na^n b^{3^n}NoNoNo3n3^n not in AP
wwRw w^RNoNoYescentre unknown ⇒\Rightarrow non-deterministic PDA only
www wNoNoNoneeds bottom-of-stack comparison
wwwRw w w^RNoNoNocontains wwww
anbncma^n b^n c^m (nn vs mm compared)NoNoNostack empty after anbna^n b^n; no re-reading
anb2nc3na^n b^{2n} c^{3n}NoNoNostack empty after aa-vs-bb comparison
anbna^n b^n, n<1010n < 10^{10}YesYesYesfinite
xcyx c y, x,y∈{0,1}∗x,y \in \{0,1\}^*YesYesYesstrings containing cc as substring
xxRx x^R, ∣x∣=l\lvert x \rvert = lYesYesYesfinite (2l2^l strings)
ambna^m b^n, m≠nm \ne nNoYesYespush on aa, pop on bb; accept on leftover aa's or on z0z_0 with bb's remaining
ambna^m b^n, m=2n+1m = 2n + 1NoYesYespush 1 aa per 2 aa's, keep 1 aside, pop 1 per bb

Revision summary

  • A PDA loop can add or pop only a constant amount per iteration, so it can match counts in AP (nn, 2n2n, 3n3n, 2n+12n+1) but never n2n^2, 2n2^n, 3n3^n.
  • A PDA sees only the top of the stack, never the bottom, so wwww is impossible.
  • A PDA cannot re-read input and cannot compare a new block after the stack has been emptied, so anbncma^n b^n c^m (with nn vs mm compared) and anb2nc3na^n b^{2n} c^{3n} are not CFLs.
  • Non-determinism can guess a centre (so wwRww^R is a CFL) but cannot fix the bottom-of-stack problem (so wwww is not a CFL).
  • "Push kk aa's per aa, pop 1 per bb" changes the language to {aibj∣ki=j}\{a^i b^j \mid ki = j\} — always check with a small counter-example string (aabcaabc, ab4ab^4).
  • Finite languages are regular; regular ⇒\Rightarrow DCFL ⇒\Rightarrow CFL. Use this for bounded languages (n<1010n < 10^{10}, ∣x∣=l\lvert x \rvert = l).
  • "Contains cc as a substring" is regular.
  • m≠nm \ne n is a DCFL: accept either on leftover aa's (m>nm > n) or on reaching z0z_0 with bb's remaining (n>mn > m).

Previous-year exam insights

  • The CFL pumping lemma (uvwxy form): if L is context-free, there is a pumping length p such that every string s in L with |s| ≥ p can be written s = uvwxy with |vwx| ≤ p, |vx| ≥ 1, and uviuv^{i} wxiwx^{i} y in L for all i ≥ 0.
    Use it (or closure properties) to prove non-context-freeness; note that satisfying the pumping lemma does NOT prove a language is context-free.
    Asked inGATE 2005
  • CFLs are closed under union, concatenation, Kleene star, reversal, homomorphism and intersection with a regular language, but NOT under intersection or complementation.
    DCFLs are closed under complement but not under union or intersection.
    Asked inGATE 2022GATE 2021GATE 2020
  • A language of the form {ana^{n} bnb^{n} cnc^{n}} or {ana^{n} bnb^{n} cmc^{m} with n compared to m} is not context-free, but {ana^{n} bnb^{n} cmc^{m} | m,n ≥ 0} (no comparison between n and m) IS context-free, and {ama^{m} bnb^{n} cnc^{n}} is context-free (indeed DCFL).
    Asked inGATE 2022GATE 2016
  • A language whose strings have a fixed marker separating two parts, e.g. {w c wRw^{R}}, is deterministic context-free (the marker tells the DPDA where to switch from pushing to popping), whereas {wwRww^{R}} without a marker is still context-free but needs the centre guessed.
    Asked inGATE 2014GATE 2019
  • If L is not context-free, then neither is L intersected with a regular set when the intersection still contains the non-CFL core; more usefully, to show a language is not CFL, intersect it with a regular language to obtain a known non-CFL (closure under intersection with regular languages).
    Asked inGATE 2020GATE 2006
  • Every regular language is a DCFL and every DCFL is a CFL; the inclusions are strict (e.g. {ana^{n} bnb^{n}} is DCFL but not regular, and {wwRww^{R}} is CFL but not regular).
    A finite language is always regular.
    Asked inGATE 2020GATE 2015
Pages 1–2 of 2