NPDA for the language L ={wϵ{a,b}*| w contains equal no. of a’s and b’s}



Prerequisite – Pushdown automata, Pushdown automata acceptance by final state
Problem – Design a non deterministic PDA for accepting the language L ={wϵ{a,b}* | w contains equal no. of a’s and b’s}, i.e.,

L = {ab, aabb, abba, aababb, bbabaa, baaababb, .......}

The number of a’s and b’s are same in all the strings.

Explanation –
Here, we need not to maintain any order of a’s and b’s. Thus our state diagram will contain only an initial state and a final state. The count of a’s and b’s is maintained by stack. We will take 3 stack alphabets:

\Gamma = {a, b, z}

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

Approach used in the construction of PDA –
If ‘a’ comes first then push it in stack and if again ‘a’ comes then also push it. Similarly, if ‘b’ comes first (‘a’ did not comes yet) then push it into the stack and if again ‘b’ comes then also push it.



Now, if ‘a’ is present in the top of the stack and ‘b’ comes then pop the ‘a’ from the stack. And if ‘b’ present in the top of the stack and ‘a’ comes then pop the ‘b’ from the stack.

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, z)  \vdash  (q0, bz)
\delta(q0, b, b)  \vdash  (q0, bb)
\delta(q0, a, b)  \vdash  (q0, \epsilon)
\delta(q0, b, a)  \vdash  (q0, \epsilon)
\delta(q0, \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 strings which contain equal no. of a’s and b’s.



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 contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. 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 contribute@geeksforgeeks.org to report any issue with the above content.