0% found this document useful (0 votes)
282 views

Pushdown Automata

DSA

Uploaded by

sapnarrrjodha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
282 views

Pushdown Automata

DSA

Uploaded by

sapnarrrjodha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 29

CS 19.

206
Theory of Computation
Unit-III
Pushdown Automata

Dr. Ajay Kumar, Asst. Prof.,


Dept. of CSE, SET-MUST, 9772850088
Pushdown Automata

 Pushdown automata is a way to implement/represent a CFG in the same way we


design DFA for a regular grammar. A DFA can not information, but a PDA can
remember an infinite amount of information.
 Pushdown automata is simply an NFA augmented with an "external stack memory".
The addition of stack is used to provide a last-in-first-out memory management
capability to Pushdown automata.
 A PDA is more powerful than FA. Any language which can be acceptable by FA can
also be acceptable by PDA. PDA also accepts a class of language which even cannot
be accepted by FA. Thus PDA is much more superior to FA.
Model of Pushdown Automata

 Input tape: The input tape is divided in many cells or symbols. The input head is read-only
and may only move from left to right, one symbol at a time.
 Finite control: The finite control has some pointer which points the current symbol which is
to be read, with finite number of states.
 Stack: The stack is a structure in which we can push and remove the items from one end
only. It has an infinite size. In PDA, the stack is used to store the items temporarily.
Definition - Pushdown Automata

A Pushdown Automata M is defined as 7- tuples:

M=(Q, ∑, Γ, q0, Z0, F, δ), where

 Q: the finite non-empty set of states

 ∑: the finite non-empty set of input symbols

 Γ: a finite non-empty set of stack / pushdown symbol which can be pushed and
popped from the stack

 q0: the initial state

 Z0: a start symbol which is in Γ.

 F: a set of final states, F is a subset of Q

 δ: the transition function which maps from QX{ΣU^}X Γ into the set of finite subset
of QXΓ*.
Graphical Notations for PDA

Instantaneous Description (ID)


At any given time the state of a PDA M is given by: (q, x, α)
 The state qϵQ the PDA is currently in
 The input string xϵΣ*, which still has to process
 The contents of the stack ( pushdown store) αϵΓ*

The transition from an ID to another ID is defined by the term move relation and
denoted by Turnstile notations:
 ⊢ sign describes the turnstile notation and represents one move.

 ⊢* sign describes a sequence of moves.

Example: (q 1,x 1,α1) ⊢ (q 2,x 2,α2)


Instantaneous Description (ID)

…. a b a b …. …. a b a b ….

q b
a
 q’ a
a a
Z0 Z0

(q,abab,baaZ0)
⊢ (q’,bab,aaZ0)

(q,abab,baaZ0) ⊢* (q’,^,Z0)
Representation of PDA by Transition Diagram

A transition pushing input symbol on PDS δ(qi,0,Z0)={(qj,0Z0)} can be represented as


(0,Z0,0Z0)
qi qj

Similarly, the transition δ(qi,0,0)={(qi,^)}, popping an element from the stack can be
represented as (0,0,^)

qi
String / Language Acceptance

Acceptance by Final State


 The acceptance conditions include that the string (input) must be
exhausted as the PDA reaches to final state.
 Let P= (Q, ∑, Γ, δ , q0, Z0, F) be a pushdown automaton. The set accepted by
PDA in terms of final state denoted by T(P) is defined as:
T(P)={wϵΣ*|(q0,w,Z0) ⊢ (qf,^,α) for some qfϵF and α ϵΓ*}
String / Language Acceptance

Acceptance by Null or Empty Store


 The acceptance conditions include that the stack must be empty as the
input string is exhausted.
 Let P= (Q, ∑, Γ, δ , q0, Z0, F) be a pushdown automaton accepting language L
by final state. For this we can define a pushdown automaton
P’= (Q’, ∑, Γ’, δ’ , q0’, Z0’, {φ})
Which accepts language L by empty store, i.e. L=T(P)=T(P’)
T(P)=the set accepted by PDA, P in terms of final state
T(P’)=the set accepted by PDA, P in terms of empty store
The set accepted by PDA in terms of empty store, denoted by T(P’) is defined
as:
T(P)={wϵΣ*|(q0,w,Z0) ⊢ (q’,^,^) or (q’,^, Z0) for some q’ϵQ}
Example

1. Design a PDA for accepting a language {anb2n | n>=1}.

Solution: In this language, n number of a's should be followed by 2n number of b's.


Hence, we will apply a very simple logic, and that is if we read single 'a', we will push
two a's onto the stack. As soon as we read 'b' then for every single 'b' only one 'a'
should get popped from the stack.

The ID can be constructed as follows:


δ(q0, a, Z) = (q0, aaZ)
δ(q0, a, a) = (q0, aaa)

Now when we read b, we will change the state from q0 to q1 and start popping
corresponding 'a'. Hence,
δ(q0, b, a) = (q1, ε)
Thus this process of popping 'b' will be repeated unless all the symbols are read. Note
that popping action occurs in state q1 only.
δ(q1, b, a) = (q1, ε)
Example

Thus this process of popping 'b' will be repeated unless all the symbols are read. Note that popping action
occurs in state q1 only.
δ(q1, b, a) = (q1, ε)

After reading all b's, all the corresponding a's should get popped. Hence when we read ε as input symbol
then there should be nothing in the stack. Hence the move will be:
δ(q1, ε, Z) = (q2, ε)
Where
PDA = ({q0, q1, q2}, {a, b}, {a, Z}, δ, q0, Z, {q2})
We can summarize the ID as:
δ(q0, a, Z) = (q0, aaZ)
δ(q0, a, a) = (q0, aaa)
δ(q0, b, a) = (q1, ε)
δ(q1, b, a) = (q1, ε)
δ(q1, ε, Z) = (q2, ε)
Example

Now we will simulate this PDA for the input string "aaabbbbbb".

δ(q0, aaabbbbbb, Z) ⊢ δ(q0, aabbbbbb, aaZ)

⊢ δ(q0, abbbbbb, aaaaZ)

⊢ δ(q0, bbbbbb, aaaaaaZ)

⊢ δ(q1, bbbbb, aaaaaZ)


⊢ δ(q1, bbbb, aaaaZ)

⊢ δ(q1, bbb, aaaZ)

⊢ δ(q1, bb, aaZ)

⊢ δ(q1, b, aZ)

⊢ δ(q1, ε, Z)
⊢ δ(q2, ε)

ACCEPT
Example
2. Design a PDA for accepting a language {0n1m0n | m, n>=1}.

Solution: Hence the logic for design of such PDA will be as follows:

Push all 0's onto the stack on encountering first 0's. Then if we read 1, just do nothing.
Then read 0, and on each read of 0, pop one 0 from the stack.

The ID can be constructed as follows:


δ(q0, 0, Z) = δ(q0, 0Z)

δ(q0, 0, 0) = δ(q0, 00)

δ(q0, 1, 0) = δ(q1, 0)

δ(q1, 1, 0) = δ(q1, 0)

δ(q1, 0, 0) = δ(q1, ε)
δ(q1, ε, Z) = δ(qf, Z) (ACCEPT state)
Exercise
1. Construct Pushdown automata for L = {0n1m2m3n | m,n ≥ 0}
2. Construct Pushdown automata for L = {a(2*m)c(4*n)dnbm | m,n ≥ 0}
3. Construct Pushdown automata for L = {0n1m2(n+m) | m,n ≥ 0}
4. Construct Pushdown automata for L = {0m1(n+m)2n | m,n ≥ 0}
5. Construct Pushdown automata for L = {0(n+m)1m2n | m, n ≥ 0}
Deterministic Push Down Automata

A deterministic pushdown automaton is one for which every input string has a unique
path through initial state to final state in the machine.

A push down automata M= (Q, ∑, Γ, δ , q0, Z0, F), is said to be deterministic if for all qϵQ,
aϵΣ and Zϵ Γ

(i) δ(q, a, Z) is either empty or single move, and

(ii) δ(q, ^, Z) = q, it means the state is not changed on input ^.

If any of the above rules is not satisfied even once, the PDA is called nondeterministic.
Nondeterministic Push Down Automata

A nondeterministic pushdown automaton is one for which, at a certain time it has to


select a particular path among possible paths. Input string is accepted by such a
machine if some set of choices leads to an ACCEPT state.

A push down automata M= (Q, ∑, Γ, δ , q0, Z0, F), is said to be nondeterministic with the
transition function defined as the mapping:

QX{ΣU^}X Γ ---> 2QXΓ*

Relative strengths of NPDA, DPDA and FA


Equivalence of PDA and Context Free Language

Conversion of CFG to PDA


Theorem: For a language L, the following is equivalent:

a) L is derived by the context free grammar G, i.e., L=L(G)

b) L is a language of the PDA M, i.e., L=L(M).

Context Free Language (CFL’s) can be described by Context Free Grammars (CFG’s) and
can be processed by Pushdown Automata.

Given a CFG G=(VN,Σ,P,S) generating language L(G), we define a PDA M(G) for CFG G

M(G)= ({q0}, ∑, VNUΣ , δ , q0, S,{φ}),

where δ is defined as:

δ(q0,^,A)={(q0,α)|AαϵP} for all AϵVN

δ(q0,a,a)={(q0,^)} for all aϵ Σ


Steps for CFG to PDA Conversion
The following steps are used to obtain PDA from CFG is:

Step 1: Convert the given productions of CFG into GNF.

Step 2: The PDA will only have one state {q}.

Step 3: The initial symbol of CFG will be the initial symbol in the PDA.

Step 4: For non-terminal symbol, add the following rule:

δ(q, ε, A) = (q, α) , Where the production rule is A → α


Step 5: For each terminal symbols, add the following rule:

δ(q, a, a) = (q, ε) for every terminal symbol


Example
1. Convert the following grammar to a PDA that accepts the same language.
S → 0S1 | A
A → 1A0 | S | ε
Solution:
The CFG can be first simplified by eliminating unit productions:
S → 0S1 | 1S0 | ε
Now we will convert this CFG to GNF:
S → 0SX | 1SY | ε
X→1
Y→0
The PDA can be:
R1: δ(q, ε, S) = {(q, 0SX) | (q, 1SY) | (q, ε)}
R2: δ(q, ε, X) = {(q, 1)}
R3: δ(q, ε, Y) = {(q, 0)}
R4: δ(q, 0, 0) = {(q, ε)}
R5: δ(q, 1, 1) = {(q, ε)}
Example
1. Draw a PDA for the CFG given below:
S → aSb
S→a|b|ε
Solution:
The PDA can be given as: M = {(q), (a, b), (S, a, b, z0), δ, q, z0, q}
The mapping function δ will be:
R1: δ(q, ε, S) = {(q, aSb)}
R2: δ(q, ε, S) = {(q, a) | (q, b) | (q, ε)}
R3: δ(q, a, a) = {(q, ε)}
R4: δ(q, b, b) = {(q, ε)}
R5: δ(q, ε, z0) = {(q, ε)}

Simulation: Consider the string aaabb


δ(q, εaaabb, S) ⊢ δ(q, aaabb, aSb) R3
⊢ δ(q, εaabb, Sb) R1
⊢ δ(q, aabb, aSbb) R3
⊢ δ(q, εabb, Sbb) R2
⊢ δ(q, abb, abb) R3
⊢ δ(q, bb, bb) R4
⊢ δ(q, b, b) R4
⊢ δ(q, ε, z0) R5
⊢ δ(q, ε)
Exercise
1. Construct PDA for the given CFG, and test whether 0104 is acceptable by this PDA.
S → 0BB
B → 0S | 1S | 0
Test 0104 i.e. 010000 against PDA.

2. Construct PDA for the CFG G=({E,T,F},{(,),a,+,*},P,E), productions are defined as:
P={E->T|E+T, T->F|T*F, F->a|(E)}
Test string “a+(a*a)” against PDA.
Equivalence of PDA and Context Free Language

Conversion of PDA to CFG


Theorem: If there is a PDA M= (Q, ∑, Γ, δ , q0, Z0, F), then there exists a context free grammar G,
such that the language generated by grammar G (say L(G)) is equivalent to the set accepted by
PDA M(G) by null store, i.e. L(G)=M(G).

Step 1: We define CFG G=(VN, Σ , P , S), where

VN = {S} U {[q, Z, q’]|q, q’ ϵ Q, and Z ϵ Γ }

The productions P in grammar G are induced by moves of PDA. P is defined by the following rules:

R1: S-productions are given by S->[q0, Z0, q] for all states q ϵ Q

R2: Each move removing a symbol from push down store is given by (q’,^) ϵ δ(q, a, Z) which

induces the production [q, Z, q’]->a.

R3: A move that does not remove a symbol from stack is given by

(q1, Z1Z2Z3…Zm) ϵ δ(q, a, Z), induces several production of the form


Example
1. Construct a CFG G that accepts the set accepted by PDA M by null store, where,

M=({q0,q1},{a,b},{Z0,Z},δ,q0,Z0,{φ}),

δ is given by
δ(q0, b, Z0)={(q0, ZZ0)}
δ(q0, ^, Z0)={(q0, ^)}
δ(q0, a, Z)={(q1, Z)}
δ(q0, b, Z0)={(q0, ZZ)}
δ(q1, a, Z0)={(q0, Z0)}
δ(q1, b, Z)={(q1, ^)}

Solution:

Suppose the context free grammar G is defined as G=(VN, Σ , P , S) where, Σ ={a,b}

VN = { S, [q0,Z0,q0], [q0,Z0,q1], [q0,Z,q0], [q0,Z,q1], [q1,Z0,q0], [q1,Z0,q1], [q1,Z,q0], [q1,Z,q1]}

Note that the elements in VN, except S, denoted by a triple are constructed by using all

combinations of q0, and q1, with Z0 or Z1 in the middle. Thus, there are 2m*n+1 variable in VN
(including S), if there are m unique states and n unique push down symbols.
Example
The set of production P includes

P1:S->[q0,Z0,q0]

P2:S->[q0,Z0,q1] (by Rule R1)

The transition δ(q0, b, Z0)={(q0, ZZ0)} gives productions

P3:[q0,Z0,q0]->b[q0,Z,q0] [q0,Z0,q0]

P4:[q0,Z0,q0] ->b[q0,Z,q1] [q1,Z0,q0] (by Rule R3)

P5:[q0,Z0,q1] ->b[q0,Z,q0] [q0,Z0,q1]

P6:[q0,Z0,q1] ->b[q0,Z,q1] [q1,Z0,q1]

The transition δ(q0, ^, Z0)={(q0, ^)}


P7:[q0,Z0,q0] -> ^ (by Rule R2)

The transition δ(q0, b, Z)={(q0, ZZ)} gives productions

P8:[q0,Z,q0]->b[q0,Z,q0] [q0,Z0,q0]

P9:[q0,Z,q0] ->b[q0,Z,q1] [q1,Z0,q0] (by Rule R3)

P10:[q0,Z,q1] ->b[q0,Z,q0] [q0,Z0,q1]


Example
The transition δ(q0, a, Z)={(q1, Z)} gives productions

P12:[q0,Z,q0] ->a[q1,Z,q0]

P13:[q0,Z,q1] ->a[q1,Z,q1] (by Rule R3)

The transition δ(q1, b, Z)={(q1, ^)} gives productions

P14:[q1,Z,q1]->b (by Rule R2)

The transition δ(q1, a, Z0)={(q0, Z0)} gives productions

P15:[q1,Z0,q0] ->a[q0,Z,q0] (by Rule R3)

P16:[q1,Z0,q1] ->a[q0,Z,q1]

The productions P1 to P16 are in P.


Two Stack Push Down Automata
It can be seen that neither finite automaton nor push down automata are the general
models of a computer. Since they can not recognize simple language like {anbncn|
n>=0}.
A two stack push down automaton ( also called two push down stack machine ot two
push down store machine) is like a push down automaton except that it has two push
down stores.
The transition function of 2-PDA is as:

Q*{ΣU^}* Γ * Γ finite subset of QxΓ*xΓ*.


This transition function states that the move in a two stack PDA depends on the top
of two stacks and results in new value being pushed on two stacks.
The class of two stack automaton is equivalent to the class of a Turin Machine.
Minsky’s Theorem

“ A language accepted by a two stack PDA can also be accepted by some


Turing Machine and any language accepted by a Turing Machine can also be
accepted by some two stack PDA (i.e., 2-PDA=TM)”.

… a a a . . a a a bn c c c c . . c c .
. . . . . Input Tape

a b
a Finite State Control
b
. .
. .
Z1 Z2
Stack S1 Stack S2

The Minsky’s Model ( Two Stack PDA)


Example
1. Construct a 2 stack PDA to recognize the language L={ anbncn|n>=0}

Solution: The transitions for the required 2-stack PDA are:


T0: δ(q0, ^, Z1, Z2)={qf, Z1, Z2 } // acceptance for n=0
T1: δ(q0, a, Z1, Z2)={q0, aZ1, Z2 } // push first ‘a’ symbol in stack 1
T2: δ(q0, a, a, Z2)={q0, aa, Z2 } // push all ‘a’ symbol in stack 1
T3: δ(q0, b, a, Z2)={q0, a, bZ2 } // push first ‘b’ symbol in stack 2
T4: δ(q0, b, a, b)={q0, a, bb } // push all ‘b’ symbol in stack 2
T5: δ(q0, c, a, b)={q1, ^, ^ } // on reading first ‘c’ pop topmost
symbols of both the stacks
T6: δ(q1, c, a, b)={q1, ^, ^ } // remove top most symbols of both
stack on reading every ‘c’ symbol
T7: δ(q1, ^, Z1, Z2 )={qf, Z1, Z2 }

Input string is exhausted, stacks are left with Z1 and Z2 , and machine is in final state.
Hence, the language L={ anbncn|n>=0} can be processed by the machine.
Exercise
Construct a 2 stack PDA to recognize the language

a) L={ anbncndn|n>=0}
b) L={ anbncmdn|n,m>=0}
c) L={ anbnc2n|n>=0}
d) L={ anb2ncndn|n>=0}
e) L={ anbmcn+m|n>=0}

You might also like