NPDA for accepting the language L = {an bn | n>=1}

Prerequisite – Pushdown automata, Pushdown automata acceptance by final state

Problem – Design a non deterministic PDA for accepting the language L = {a^n b^n | n>=1}, i.e.,

L = {ab, aabb, aaabbb, aaaabbbb, ......} 

In each of the string, the number of a’s are followed by equal number of b’s.

Explanation –
Here, we need to maintain the order of a’s and b’s. That is, all the a’s are are coming first and then all the b’s are coming. Thus, we need a stack along with the state diagram. The count of a’s and b’s is maintained by the stack. We will take 2 stack alphabets:

\Gamma = { a, z }

\Gamma = set of all the stack alphabet
z = stack start symbol

Approach used in the construction of PDA –
As we want to design a NPDA, thus every time ‘a’ comes before ‘b’. When ‘a’ comes then push it in stack and if again ‘a’ comes then also push it. After that, when ‘b’ comes then pop one ‘a’ from the stack each time. So, at the end if the stack becomes empty then we can say that the string is accepted by the PDA.

Stack transition functions –

\delta(q0, a, z) \vdash (q0, az)
\delta(q0, a, a) \vdash (q0, aa)
\delta(q0, b, a) \vdash (q1, \epsilon )
\delta(q1, b, a) \vdash (q1, \epsilon )
\delta(q1, \epsilon, z) \vdash (qf, z)     

Where, q0 = Initial state
qf = Final state
\epsilon = indicates pop operation

So, this is our required non deterministic PDA for accepting the language L = {a^n b^n : n>=1}.

Attention reader! Don’t stop learning now. Get hold of all the important CS Theory concepts for SDE interviews with the CS Theory Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using or mail your article to See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :

Be the First to upvote.

Please write to us at to report any issue with the above content.