13-Push Down Automata (PDA)

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 24

Push Down Automata (PDA)

1
NPDA - Definition

δ(q0, a, Z) = (q0, aZ)


δ(q0, a, a) = (q0, aa)
δ(q0, a, Z) = (q0, λ)
δ(q0, a, Z) = (q1, bZ)
δ(q0, a, Z) = (q1, λ)
δ(q0, a, Z) = (q0, Z)
δ(q0, a, a) = (q0, b)
δ(q1, λ, Z) = (q2, Z)
δ(q1, λ, Z) = (q2, λ)
2
Example 1 Example: aaaabbbb λ
Design a PDA to accept L={anbn:n>=0}

a
a
a
a
accept
Z

3
Example 1 Example: aaaabbbb λ
q0 q0 q0 q0q0 q1 q1 q1 q1 q2
Design a PDA to accept L={anbn:n>=0}
δ(q0, a, Z) = (q0, aZ) What if n>=1 ???
δ(q0, a, a) = (q0, aa) a

δ(q0, b, a) = (q1, λ) a
a
δ(q1, b, a) = (q1, λ)
Final state a
δ(q1, λ, Z) = (q2, Z) accept
Z
To accept empty string
δ(q0, λ, Z) = (q2, Z)
M=({q0, q1, q2}, {a, b}, {a}, δ, q0, Z, {q2}}
Same stack symbol and same state, but different
input symbols, so given automata is NPDA
4
Example 1

Accept by
final state

δ(q1, λ, Z) = (q2, λ) Accept by


Empty stack
δ(q0, λ, Z) = (q2, λ)

5
Example 1
Check if aaabbb is part of the
language generated by the
automata
Initial ID Check if aab is part of the language
(q0, aaabbb, Z) Ͱ (q0, aabbb, aZ) generated by the automata
Ͱ (q0, abbb, aaZ) Ͱ (q0, bbb, aaaZ) Initial ID
Ͱ (q1, bb, aaZ) Ͱ (q1, b, aZ) (q0, aab, Z) Ͱ (q0, ab, aZ)
Ͱ (q1, λ, Z) Ͱ (q2, λ, Z) Ͱ (q0, b, aaZ) Ͱ (q1, λ, aZ)
Accept Transition not defined
Reject the string
6
Language accepted by PDA

• This is called Instantaneous description (ID)


• (q0, w, z) is called Initial ID
• (q0, w, z) is called Initial ID
Acceptance by Empty Stack
L(M) = { W ϵ ∑* , (q0, w, z) ͱ* (n, λ, λ) where n ϵ Q} 7
Two Types of PDA
1.Non-Deterministic PDA (NPDA)
2. Deterministic PDA (DPDA)

A PDA is said to be Deterministic PDA if the following 2 conditions


are satisfied:
p ϵ Q, c ϵ Г , a ϵ ∑
1. δ (p, a, c) is at most one element
2. δ (p, λ, c) is not empty then δ (p, a, c) should be empty

13-06-2022 8
Example 2
Design a PDA to accept L={anbn+1:n>=0}
δ(q0, a, Z) = (q0, aZ)
δ(q0, a, a) = (q0, aa)
δ(q0, b, a) = (q1, λ)
δ(q1, b, a) = (q1, λ)
δ(q1, b, Z) = (q2, Z) (make sure that only one extra b is there)
δ(q2, λ, Z) = (q3, Z) (final state)
δ(q0, b, Z) = (q4, Z) (minimum string)
δ(q4, λ, Z) = (q3, Z) (minimum string, final state)
NPDA or DPDA?
DPDA
9
Example 3
Design a PDA to accept L={an+1bn:n>=0}
δ(q0, a, Z) = (q0, aZ)
δ(q0, a, a) = (q0, aa)
δ(q0, b, a) = (q1, λ)
δ(q1, b, a) = (q1, λ)
δ(q1, λ, a) = (q2, λ) (make sure that only one extra a is there)
δ(q2, λ, Z) = (q3, Z) (final state)
δ(q0, λ, a) = (q4, λ) (minimum string) How to do..
δ(q4, λ, Z) = (q3, Z) (minimum string, final state) L={ambn:m>n}
NPDA or DPDA? mbn:m<n}
NPDA bcoz of rule 2 and 7
L={a
10
Example 4
• PDA for L={wcwR : w ∈ (a+b)*}
δ(q0, a, Z) = (q0, aZ)
δ(q0, b, Z) = (q0, bZ)
δ(q0, a, a) = (q0, aa)
δ(q0, b, b) = (q0, bb) δ(q1, a, a) = (q1, λ)
δ(q0, a, b) = (q0, ab) δ(q1, b, b) = (q1, λ)
δ(q0, b, a) = (q0, ba) δ(q1, λ, Z) = (q2, Z) (final state)
δ(q0, c, a) = (q1, a) δ(q0, c, Z) = (q3, Z)
δ(q0, c, b) = (q1, b)
δ(q3, λ, Z) = (q2, Z) (Minimum string)
11
Example 4
• Check if abbcbba is accepted by the PDA
(q0, abbcbba, Z) Ͱ (q0, bbcbba, aZ)
Ͱ (q0, bcbba, baZ) Ͱ (q0, cbba, bbaZ)
Ͱ (q1, bba, bbaZ) Ͱ (q1, ba, baZ) • Check if abcbba is accepted by the PDA
Ͱ (q1, a, aZ) Ͱ (q1, λ, Z) Ͱ (q2, λ, Z) (q0, abcbba, Z) Ͱ (q0, bcbba, aZ)
q2 ∈ F hence, accept the string Ͱ (q0, cbba, baZ) Ͱ (q1, bba, baZ)
Ͱ (q1, ba, aZ)
no transition defined
Hence reject the string
12
Example 5
• PDA for L={wwR : w ∈ (a+b)*}
δ(q0, a, Z) = (q0, aZ)
δ(q1, a, a) = (q1, λ)
δ(q0, b, Z) = (q0, bZ)
δ(q0, a, a) = (q0, aa) δ(q1, b, b) = (q1, λ)
δ(q0, b, b) = (q0, bb) δ(q1, λ, Z) = (q2, Z) (final state)
δ(q0, a, b) = (q0, ab) δ(q0, λ, Z) = (q2, Z) (Minimum string)
δ(q0, b, a) = (q0, ba)
δ(q0, λ, a) = (q1, a)
δ(q0, λ, b) = (q1, b) Imaginary λ
NPDA or DPDA??
NPDA – rules 1&12 or 2&12 or 3&7 or 4 &8 13
• Check if abba is accepted by the PDA
Example 5 (q0, abba, Z) Ͱ (q0, bba, aZ)
Ͱ (q0, ba, baZ) Ͱ (q0, λa, bbaZ)
• Check if abba is accepted by the PDA
Ͱ (q1, a, bbaZ)
(q0, abba, Z) Ͱ (q0, bba, aZ)
No transition defined
Ͱ (q0, λba, baZ)
reject the string (as per this)
Ͱ (q1, ba, baZ) Ͱ (q1, a, aZ)
Ͱ (q1, λ, Z) Ͱ (q2, λ, Z)
• Check if abba is accepted by the PDA
q2 ∈ F hence, accept the string
(q0, abba, Z) Ͱ (q0, λbba, aZ)
But if any one derivation Ͱ (q1, bba, aZ)
accepts the string, then the No transition defined
string has to be accepted reject the string (as per this) 14
Example 6
• L={anb2n:n>=0}
• For such problems, push 2 a’s for a single a, and match the b’s
δ(q0, a, Z) = (q0, aaZ)
δ(q0, a, a) = (q0, aaa)
δ(q0, b, a) = (q1, λ)
δ(q1, b, a) = (q1, λ)
δ(q1, λ, Z) = (q2, Z) (final state)
δ(q0, λ, Z) = (q2, Z) (to accept minimum string)

15
Example 7
• L={a2nbn:n>=0}
• For such problems, push 1 a for every 2 a’s, and match the b’s (easier to
implement/understand)
δ(q0, a, Z) = (q3, Z)
δ(q3, a, Z) = (q0, aZ)
δ(q0, a, a) = (q3, a)
δ(q3, a, a) = (q0, aa)
δ(q0, b, a) = (q1, λ)
δ(q1, b, a) = (q1, λ)
δ(q1, λ, Z) = (q2, Z) (final state)
δ(q0, λ, Z) = (q2, Z) (to accept minimum string)

16
Example 8
• Design a PDA for L(ab*ab)
• Keep accepting inputs as per the requirement. Change the state if
necessary
δ(q0, a, Z) = (q0, aZ)
δ(q0, b, a) = (q0, ba)
δ(q0, b, b) = (q0, bb)
δ(q0, a, b) = (q1, ab)
δ(q0, a, a) = (q1, aa)
δ(q1, b, a) = (q2, ba) (final state)

17
Example 9
• L={w : na(w)=nb(w)}
δ(q0, a, Z) = (q0, aZ)
δ(q0, a, a) = (q0, aa)
δ(q0, b, a) = (q0, λ)

δ(q0, b, Z) = (q0, bZ)


δ(q0, b, b) = (q0, bb)
δ(q0, a, b) = (q0, λ)

δ(q0, λ, Z) = (q1, Z) (final state)

18
Example 10
• Find the language accepted by the NPDA
M=({q0, q1, q2, q3, q4, q5}, {a, b}, {1}, δ, q0, Z, {q4, q5}}

δ(q0, b, Z) = (q1, 1Z)


δ(q1, b, 1) = (q2, 11)
δ(q2, a, 1) = (q3, λ)
δ(q3, a, 1) = (q4, λ)
δ(q4, a, Z) = {(q4, Z), (q5, Z)}

L={bban : n>=2}
19
CFG to PDA – Example 1
• PDA can also be generated from CFG
• But the CFG should be in GNF
• Example 1: Equivalent PDA
S -> aAB δ(q0, λ, Z) = (q1, SZ) (always push start symbol from
A -> a the initial state)
B -> b δ(q1, a, S) = (q1, AB)
δ(q1, a, A) = (q1, λ)
δ(q1, b, B) = (q1, λ)
δ(q1, λ, Z) = (q2, Z) (final state)
20
Example 1
• Check if aab belongs to the language generated by PDA
Initial ID (q0, aab, Z) δ(q0, λ, Z) = (q1, SZ)
δ(q1, a, S) = (q1, AB)
δ(q1, a, A) = (q1, λ)
(q0, λaab, Z) Ͱ (q1, aab, SZ) δ(q1, b, B) = (q1, λ)
Ͱ (q1, ab, ABZ) Ͱ (q1, b, BZ) δ(q1, λ, Z) = (q2, Z)

Ͱ (q1, λ, Z) Ͱ (q2, λ, Z)
Accept the string

21
Example 2
• Obtain the PDA for
S -> aSBB | a Equivalent PDA
B -> b δ(q0, λ, Z) = (q1, SZ) (always push start symbol from
the initial state)
δ(q1, a, S) = {(q1, SBB), (q1, λ)}
δ(q1, b, B) = (q1, λ)
δ(q1, λ, Z) = (q2, Z) (final state)

22
Problems that PDA cannot solve
• L={anbncn : n>=0}
• L={ww : w ∈ (a+b)*}
• L={anbmcndm : n, m>=0}
Basically the problems that need extra memory other than stack
memory….

23
Exercises
1. L={ambncm+n: m, n>=0}
2. L={aab*aba*}
3. Matching parenthesis for the inputs [, (, ), ]
4. L={anbm: n=m+2, n,m>=0}
5. Find the language accepted by PDA. Assume 0 is the initial stack top symbol.
M=({q0, q1, q2, q3}, {a, b}, {0, 1}, δ, q0, 0, {q3}}
δ(q0, a, 0) = {(q1, 10), (q3, λ)}
δ(q0, λ, 0) = (q3, λ)
δ(q1, a, 1) = (q1, 11)
δ(q1, b, 1) = (q2, λ)
δ(q2, b, 1) = (q2, λ)
δ(q2, λ, 0) = (q3, λ)
6. Generate the PDA for the grammar represented by productions
S -> aSb | bSa | ab | ba
7. Check if the strings aaba and aabb belong to the automata in Example 2 represented by
S -> aSBB | a B -> b
24

You might also like