Push Down Automata - Examples
Example-1
1. The Language
-
Example strings:
aa,abba,baab,ababba, … -
Not in the language:
ab,aabb,abb, … -
Basically, the string must be a palindrome of even length.
2. The Core Idea
-
When reading the first half (), we push symbols onto the stack.
-
For the second half (), we pop and compare: each input symbol must match the stack top.
-
At the end, if everything matches and stack returns to , accept.
3. The Difficulty
We don’t know where the middle of the string is.
-
This is exactly where nondeterminism helps.
-
The NPDA can guess the middle (i.e., when to stop pushing and start popping).
-
Only the “correct guess” leads to acceptance.
4. The NPDA Definition
-
-
(input symbols)
-
(stack symbols)
-
Start state = , start stack symbol =
-
Final state set =
5. Transition Function (informal)
-
In : Pushing phase
-
Read
a→ pusha. -
Read
b→ pushb. -
ε-transition: nondeterministically switch from to (guess the middle).
-
-
In : Popping & Matching phase
-
On input
awith stack topa→ pop. -
On input
bwith stack topb→ pop.
-
-
In : End check
-
If stack has just
zand input finished → ε-move to .
-
-
In : Accepting state
-
Input consumed, stack back to
z.
-
6. Example Run: Input = abba
Let’s trace:
Initial: (q0, abba, z)
-
Read
a, push:(q0, bba, az) -
Read
b, push:(q0, ba, baz) -
Nondeterministic choice:
-
Option A: keep pushing (
bon stack). -
Option B: ε-move to (switch to popping).
We choose B:(q1, ba, baz)
-
-
In :
-
Read
b, popb:(q1, a, az) -
Read
a, popa:(q1, ε, z)
-
-
Input finished, stack =
z. ε-move →(q2, ε, z)
✅ Accepted.
7. Why Nondeterminism Is Needed
-
If the PDA always kept pushing in , it wouldn’t know the middle.
-
If it switched too early, stack/input wouldn’t match.
-
Only one of the nondeterministic branches leads to success.
Thus, palindromes (except odd-length) require nondeterminism, showing DPDA < NPDA.
✅ Summary:
-
Finite automata cannot handle palindromes.
-
Even DPDAs cannot — we really need nondeterminism here.
-
This example beautifully illustrates the power of NPDA over DPDA
Example-2
Construct a PDA for
Informal idea
-
Read one or more
0’s and push a0for each onto the stack. (enforces ). -
When the first
1appears, switch to pushing1’s (one or more, enforces ). -
On the first
2, begin popping a1for every2. If a2appears while the top of stack is0(i.e., more 2’s than 1’s so far), there is no transition → reject. After all1’s are popped, the top will be0. -
When
2’s are finished, the next symbol must be3. For each3pop one0. -
If input is finished and the stack has only the bottom marker, accept.
We will accept by reaching a final state (after an ε-move when stack is back to bottom marker).
Formal PDA
Let
where
-
-
: pushing 0’s (start)
-
: pushing 1’s
-
: popping 1’s while reading 2’s
-
: popping 0’s while reading 3’s
-
: accepting final state
-
-
(input alphabet)
-
(stack alphabet;
zis bottom marker) -
Start state , start stack symbol
-
We accept by final state; after popping all 0’s and returning to bottom marker z we take an ε-move to qf.
Transition function
I list only moves that exist (any configuration not covered has no move → rejected). Notation:
means: in state reading input symbol with stack top , go to state and replace by string (where ). ε for input means an ε-move; ε as stack replacement means pop.
(1) Push 0’s in
-
-
— push more 0’s.
(2) First 1 — move to and start pushing 1’s (this enforces at least one 1)
-
— reading first 1 when top is 0; push 1 above that 0.
(We do not allow reading1with topz— that would mean no 0’s before the 1 so such input is rejected, enforcing .)
(3) Continue pushing 1’s in
(4) First 2 — begin popping 1’s, move to (enforces at least one 2)
-
— on first 2 pop one 1 and enter pop-1 phase.
(There is no — if a 2 appears while top is0at this stage, the machine has no move and the string is rejected. This prevents more 2’s than 1’s.)
(5) Continue popping 1’s in
-
— for each subsequent 2 pop one 1.
(6) After all 1’s popped, when top is 0, a 3 starts the popping-0 phase (move to )
-
— first 3 pops a 0 and moves to q3 (enforces at least one 3).
(There is no — so extra 2’s when there are no 1’s left is rejected.)
(7) Continue popping 0’s in
-
— each 3 pops a 0.
(8) Accept when input finished and stack is back to bottom marker z
-
— ε-move to accepting state (stack unchanged except bottom marker).
(There is no transition on0,1,2in except the ones above; any unexpected symbol causes rejection.)
Example trace
Take string: 00112233 (which is ).
Start:
-
read
0: — used -
read
0: — used -
read
1: (first 1) -
read
1: — used -
read
2: — used (first 2, popped a 1) -
read
2: — used (pop the final 1) -
read
3: — used (first 3, pop a 0) -
read
3: — used (pop final 0) -
input finished and stack top is
z, take ε-move: — accepted.
Input : 0 0 1 1 1 2 2 2 3 3
Example-3
let’s construct a clear, lecture-ready PDA for
Formal PDA
Let
where
-
-
-
the bottom marker
-
start state start stack symbol
-
Acceptance by final state. (We leave z on the stack and move to by an ε-move when appropriate.)
Transition function (only defined moves)
Notation:
(Phase 1) push zeros in :
(first 1 — ignore & go to )
(second 1 — ignore & go to )
(start popping zeros on next 1 — go to )
-
— pop one
0and enter popping mode
(while popping zeros — stay in )
-
— pop one
0per1
(after all zeros are popped: the next 1 is the 3rd extra — go to )
-
— when top is
zand a1arrives, move to q4 (leavez)
(in accepting-phase allow more 1s)
(final ε-move to accepting state when input finished and stack has only z)
All other combinations (input symbol & stack top) have no transition (that branch dies)
Example traces
-
Accepting example: — input
00 11111(m-n=3→ should accept).
-
Start:
-
Read
0: Read
0:-
Read 1 (1st ignored):
-
Read 1 (2nd ignored):
-
Read 1 (pop a 0):
-
Read 1 (pop a 0):
-
Read 1 (top z → move to q4):
-
Input finished, ε-move to qf → accepted.
-
Rejected example (not enough extras): —
00 1111(m-n=2): following the same trace stops inq3with topzafter consuming last1but never reachedq4; input finishes in non-accepting state → rejected. -
Rejected example (no zeros): input
1111—q0has no1-transition when top isz, so no move from start → rejected (ensures ).
Example-4
Build a PDA for
This PDA is deterministic: it pushes A for every a, then when the first b appears it switches to popping A for every b. We accept by final state when the input is exhausted and only the bottom marker remains.
Formal PDA
Let
where
-
-
: push phase (reading
a's) — start state -
: pop phase (reading
b's) -
: accepting final state
-
-
-
where
zis the bottom-of-stack marker andAis the stack symbol pushed for eacha. -
start state and start stack symbol
-
We accept by reaching after consuming the whole input (stack may contain only z).
Transition function
Notation: meaning in state reading input symbol (or ) with stack top , go to state and replace by string (ε means pop).
Push A for each a (stay in ):
On first b, switch to pop-phase and pop one A:
(There is intentionally no — that disallows strings that start with b or have no a — enforcing
While in pop-phase , pop one A for each b:
When input finished and stack is just bottom marker, accept:
(We leave z unchanged and move to final state. Alternatively you could accept by empty-stack by popping z with an ε-move; both are equivalent up to standard constructions.)
Example trace
Take input a a b b:
Start:
-
read
a: -
read
a: via -
read
b: via (first b, switch to pop-phase) -
read
b: via (pop last A) -
input finished and stack top is
z: via → accepted.
If the numbers of a and b differ, either a pop will be attempted when no A is present (no transition) or after finishing input some A remain (no ε-move to ), so the string is rejected.
Example-5
Build a PDA for
(i.e. strings with a single center symbol c and a right half that is the reverse of the left half). The PDA will follow your steps:
-
push every input symbol (0 or 1) until the
cis seen; -
on reading
cswitch to the matching phase; -
thereafter, for each input symbol (0 or 1) pop the same symbol from the stack;
-
accept iff the input is exhausted and only the bottom marker remains.
Informal description (phases)
-
q0 (push phase) — start here. For each
0or1read, push that symbol on the stack. -
On reading the central
c, do not change the stack; move to q1. -
q1 (match/pop phase) — for each symbol
0or1read, compare it with the stack top: if equal, pop and continue; otherwise there is no move (branch dies → reject). -
When input is finished and only the bottom marker remains, take an ε-move to the final state qf and accept.
This PDA accepts by final state (you can also accept by empty stack; both are standard).
Formal definition
where
-
-
-
with the bottom-of-stack marker
-
start state , start stack symbol
-
Transition function
Notation: means in state reading input symbol (or go to and replace by . Replacing by the same symbol (e.g. 0→0) means effectively leave stack unchanged;
Only listed moves exist (all others are absent).
(Push phase: q0)
-
-
— push 0 on 0.
-
— push 0 on 1 (stack may have mixed symbols).
-
-
— push 1 on 1.
-
— push 1 on 0.
(These cover pushing 0 or 1 regardless of top symbol.)
(On the center symbol c: move to match phase without changing stack)
(Match/pop phase: q1) — each input symbol must match and pop top:
-
— read
0and pop a0. -
— read
1and pop a1.
(If the input symbol does not equal the stack top, no transition exists → reject.)
(When stack is back to bottom and input finished: accept)
(That ε-move moves to the accepting state when input is consumed and the stack contains only z.)
Worked trace (example)
Take . Then input string is 101 c 101. Start stack is z.
-
-
read
1: push → -
read
0: push → -
read
1: push → (stack top is 1) -
read
c: move to match phase without changing stack → -
read
1: top is1→ pop → -
read
0: top is0→ pop → -
read
1: top is1→ pop → -
input finished and stack top is
z→ ε-move to accept: . Accepted.
If at any point a right-half symbol does not match the stack-top, e.g. stack top=0 but input symbol is 1, there is no transition and the branch dies — string rejected.
Input : 1 0 1 0 1 0 1 0 1Output :ACCEPTED
Input : 1 0 1 0 1 1 1 1 0
Output :NOT ACCEPTED
- Construct Pushdown Automata for given languages
- Construct Pushdown Automata for all length palindrome
- NPDA for accepting the language L = {an bm cn| m,n>=1}
- NPDA for accepting the language L = {an bn cm | m,n>=1}
- NPDA for accepting the language L = {anbn | n>=1}
- NPDA for accepting the language L = {am b(2m) | m>=1}
- NPDA for accepting the language L = {am bn cp dq| m+n=p+q ; m,n,p,q>=1}
- Construct Pushdown automata for L = {0n1m2m3n | m,n ? 0}
- Construct Pushdown automata for L = {0n1m2(n+m) | m,n ? 0}
- NPDA for accepting the language L = {ambnc(n+m) | m,n ? 1}
- NPDA for accepting the language L = {amb(n+m)cn| m,n ? 1}
- NPDA for accepting the language L = {a2mb3m | m ? 1}
- NPDA for accepting the language L = {amb(2m+1) | m ? 1}
- NPDA for accepting the language L = {aibjckdl | i==k or j==l,i>=1,j>=1}
- Construct Pushdown automata for L = {a(2*m)c(4*n)dnbm | m,n ? 0}
- Construct Pushdown automata for L = {0n1m2(n+m) | m,n ? 0}
- NPDA for L = {0i1j2k | i==j or j==k ; i , j , k >= 1}
- NPDA for accepting the language L = {anb(2n) | n>=1} U {anbn | n>=1}
- NPDA for the language L ={w?{a,b}*| w contains equal no. of a’s and b’s}




Comments
Post a Comment