You are on page 1of 121

Module 5

PUSH DOWN AUTOMATA (PDA)

1
Push Down Automata
Overview
Advantage of PDA
Machine Definition
Representation of PDA
Operations on PDA
Instantaneous Description (ID)
Moves on PDA
Acceptance of String in PDA

2
• Basic Structure of PDA
• A pushdown automaton is a way to implement a context-
free grammar in a similar way we design DFA for a regular
grammar.
• A DFA can remember a finite amount of information, but
a PDA can remember an infinite amount of information.
• Basically a pushdown automaton is −
"Finite state machine" + "a stack"
• A pushdown automaton has three components −
• an input tape,
• a control unit, and
• a stack with infinite size.
• The stack head scans the top symbol of the stack.

3
A stack does two operations −
• Push − a new symbol is added at the top.
• Pop − the top symbol is read and removed.
• A PDA may or may not read an input
symbol, but it has to read the top of the
stack in every transition.

4
A Pushdown Automata (PDA) can be defined as :
• Q is the set of states
• ∑is the set of input symbols
• Γ is the set of pushdown symbols (which can be
pushed and popped from stack)
• q0 is the initial state
• Z is the initial pushdown symbol (which is initially
present in stack)
• F is the set of final states
• δ is a transition function which maps Q x {Σ ∪ ∈}
x Γ into Q x Γ*. In a given state, PDA will read
input symbol and stack symbol (top of the stack)
and move to a new state and change the symbol of
stack.
5
6
7
8
9
DPDA for anb2n n ≥ 1
For every a's push two a's into STACK because there are two b's for one 'a'
So by pushing two 'a' we can have 'a' for every 'b'.
Then we will achieve by pushing two a's and poping a's for every b
So in the end of the strings if nothing is left in the STACK then we can say that
language is accepted in the PDA.

10
Instantaneous Description (ID)
• ID is an informal notation of how a PDA
computes an input string and make a
decision that string is accepted or rejected.
• An instantaneous description is a triple
(q, w, α) where:
q describes the current state.
w describes the remaining input.
α describes the stack contents, top at
the left.

11
Turnstile Notation:
• ⊢ sign describes the turnstile notation and
represents one move.
• ⊢* sign describes a sequence of moves.
• For example,
• (p, b, T) ⊢ (q, w, α)
• In the above example, while taking a
transition from state p to q, the input
symbol 'b' is consumed, and the top of the
stack 'T' is represented by a new string α.

12
13
14
15
16
Pushdown Automaton -- PDA
Input String

Stack

States

17
Initial Stack Symbol

Stack Stack

stack
$ z top
head

bottom special symbol


Appears at time 0
18
The States

Input Pop Push


symbol symbol symbol

q1 a, b  c q2

19
20
21
22
23
24
25
a, b  c
q1 q2

input
 a   a 

stack
b top Replac
c
h e h
e e
$ $
26
a,   c
q1 q2

input
 a   a 

stack c
b top Pus
b
h h h
e e
$ $
27
a, b  
q1 q2

input
 a   a 

stack
b top
h Pop h
e e
$ $
28
a,   
q1 q2

input
 a   a 

stack
b top No b
h Change h
e e
$ $
29
Empty Stack

a, $  
q1 q2
input
 a   a 

stack empty
$ top Pop

The automaton HALTS


No possible transition after q2
30
A Possible Transition

a, $  b
q1 q2
input
 a   a 

stack
$ top Pop b

31
32
33
34
35
36
37
38
39
40
41
Non-Determinism
PDAs are non-deterministic
Allowed non-deterministic transitions

q2
a, b  c
, b  c
q1 q1 q2

a, b  c
  transition
q3
42
Example PDA

PDA M
L( M )  {a b : n  0}
n n

a,   a b, a  

q0  ,    q b, a   q  , $  $ q
1 2 3
43
L( M )  {a b : n  0}
n n

Basic Idea:

Push the a’s 2. Match the b’s on input


on the stack with a’s on stack

3. Match
a,   a b, a   found

q0  ,    q b, a   q  , $  $ q
1 2 3
44
Execution Example: Time 0
Input
a a a b b b
$
Stack

current a,   a b, a  
state

q0  ,    q b, a   q  , $  $ q
1 2 3
45
Time 1
Input
a a a b b b
$
Stack

a,   a b, a  

q0  ,    q1 b, a   q2  , $  $ q3
46
Time 2
Input
a a a b b b a
$
Stack

a,   a b, a  

q0  ,    q1 b, a   q2  , $  $ q3
47
Time 3
Input a
a a a b b b a
$
Stack

a,   a b, a  

q0  ,    q1 b, a   q2  , $  $ q3
48
Time 4
a
Input
a
a a a b b b a
$
Stack

a,   a b, a  

q0  ,    q1 b, a   q2  , $  $ q3
49
Time 5
a
Input
a
a a a b b b a
$
Stack

a,   a b, a  

q0  ,    q1 b, a   q2  , $  $ q3
50
Time 6
Input a
a a a b b b a
$
Stack

a,   a b, a  

q0  ,    q1 b, a   q2  , $  $ q3
51
Time 7
Input
a a a b b b a
$
Stack

a,   a b, a  

q0  ,    q1 b, a   q2  , $  $ q3
52
Time 8
Input
a a a b b b
$
Stack

a,   a b, a  
accept
q0  ,    q1 b, a   q2  , $  $ q3
53
A string is accepted if there is
a computation such that:

All the input is consumed


AND
The last state is an accepting state

At the end of the computation,


we do not care about the stack contents
(the stack can be empty at the last state)
54
The input string aaabbb
is accepted by the PDA:

a,   a b, a  

q0  ,    q1 b, a   q2  , $  $ q3
55
In general,
n n
L  {a b : n  0}

is the language accepted by the PDA:

a,   a b, a  

q0  ,    q1 b, a   q2  , $  $ q3
56
Rejection Example: Time 0
Input
a a b
$
Stack

current a,   a b, a  
state

q0  ,    q b, a   q  , $  $ q
1 2 3
57
Rejection Example: Time 1
Input
a a b
$
Stack

current a,   a b, a  
state

q0  ,    q1 b, a   q2  , $  $ q3
58
Rejection Example: Time 2
Input
a
a a b
$
Stack

current a,   a b, a  
state

q0  ,    q1 b, a   q2  , $  $ q3
59
Rejection Example: Time 3
Input a
a
a a b
$
Stack

current a,   a b, a  
state

q0  ,    q1 b, a   q2  , $  $ q3
60
Rejection Example: Time 4
Input a
a
a a b
$
Stack

current a,   a b, a  
state

q0  ,    q1 b, a   q2  , $  $ q3
61
Rejection Example: Time 4
Input a
a
a a b
$
Stack
reject
current a,   a b, a  
state

q0  ,    q1 b, a   q2  , $  $ q3
62
The input string aab
is rejected by the PDA:

a,   a b, a  

q0  ,    q1 b, a   q2  , $  $ q3
63
A string is rejected if there is
no computation such that:

All the input is consumed


AND
The last state is an accept state

At the end of the computation,


we do not care about the stack contents
64
Another PDA example


L( M )  {vv : v  {a, b} }
R

PDA M

a,   a a, a  
b,   b b, b  

q0 ,    q1 , $  $ q2
65

Basic Idea: L( M )  {vv : v  {a, b} }
R

Push v 3. Match v R on input


2. Guess
on stack with v on stack
middle
of input
a,   a a, a   4. Match
b,   b b, b   found

q0 ,    q1 , $  $ q2
66
Execution Example: Time 0
Input
a b b a
$
Stack
a,   a a, a  
b,   b b, b  

q0 ,    q1 , $  $ q2
67
Time 1
Input
a b b a
a
$
Stack
a,   a a, a  
b,   b b, b  

q0 ,    q1 , $  $ q2
68
Time 2
Input
b
a b b a
a
$
Stack
a,   a a, a  
b,   b b, b  

q0 ,    q1 , $  $ q2
69
Time 3
Input
b
a b b a
Guess the middle a
of string $
Stack
a,   a a, a  
b,   b b, b  

q0 ,    q1 , $  $ q2
70
Time 4
Input
b
a b b a
a
$
Stack
a,   a a, a  
b,   b b, b  

q0 ,    q1 , $  $ q2
71
Time 5
Input
a b b a
a
$
Stack
a,   a a, a  
b,   b b, b  

q0 ,    q1 , $  $ q2
72
Time 6
Input
a b b a
$
Stack
a,   a a, a  
b,   b b, b  
accept
q0 ,    q1 , $  $ q2
73
Rejection Example: Time 0
Input
a b b b
$
Stack
a,   a a, a  
b,   b b, b  

q0 ,    q1 , $  $ q2
74
Time 1
Input
a b b b
a
$
Stack
a,   a a, a  
b,   b b, b  

q0 ,    q1 , $  $ q2
75
Time 2
Input
b
a b b b
a
$
Stack
a,   a a, a  
b,   b b, b  

q0 ,    q1 , $  $ q2
76
Time 3
Input
b
a b b b
Guess the middle a
of string $
Stack
a,   a a, a  
b,   b b, b  

q0 ,    q1 , $  $ q2
77
Time 4
Input
b
a b b b
a
$
Stack
a,   a a, a  
b,   b b, b  

q0 ,    q1 , $  $ q2
78
Time 5
Input There is no possible transition.

a b b b Input is not a
consumed
$
Stack
a,   a a, a  
b,   b b, b  

q0 ,    q1 , $  $ q2
79
Another computation on same string:
Input Time 0
a b b b
$
Stack
a,   a a, a  
b,   b b, b  

q0 ,    q1 , $  $ q2
80
Time 1
Input
a b b b
a
$
Stack
a,   a a, a  
b,   b b, b  

q0 ,    q1 , $  $ q2
81
Time 2
Input
b
a b b b
a
$
Stack
a,   a a, a  
b,   b b, b  

q0 ,    q1 , $  $ q2
82
Time 3
Input b
b
a b b b
a
$
Stack
a,   a a, a  
b,   b b, b  

q0 ,    q1 , $  $ q2
83
Time 4 b
Input b
b
a b b b
a
$
Stack
a,   a a, a  
b,   b b, b  

q0 ,    q1 , $  $ q2
84
Time 5 b
Input b
No final state b
a b b b is reached a
$
Stack
a,   a a, a  
b,   b b, b  

q0 ,    q1 , $  $ q2
85
There is no computation
that accepts string abbb

abbb  L(M )

a,   a a, a  
b,   b b, b  

q0 ,    q1 , $  $ q2
86
Another PDA example

L( M )  {w  {a, b} :
*

in every prefix v, na (v)  nb (v)}

a,   a
b, a  

PDA M
q0
87
Execution Example: Time 0
Input
a a b

a,   a $
b, a   Stack
b, $  

q0
88
Time 1
Input
a a b a
a,   a $
b, a   Stack
b, $  

q0
89
Time 2
Input
a
a a b a
a,   a $
b, a   Stack
b, $  

q0
90
Time 3
Input
a a b a
a,   a $
b, a   Stack
b, $  

accept
q0
91
Rejection example: Time 0
Input
a b b b
$
Stack

q0
92
Time 1
Input
a b b b a
a,   a $
b, a   Stack
b, $  

q0
93
Time 2
Input
a b b b

a,   a $
b, a   Stack
b, $  

q0
94
Time 3
Input
a b b b

a,   a
b, a   Stack
b, $  

q0
95
Time 4
Input
a b b b

a,   a
b, a   Stack
b, $  

Halt and Reject


q0
96
Pushing Strings

Input Pop Push


symbol symbol string

q1 a, b  w q2

97
Example:
a, b  cdf
q1 q2

input
  
 a 

 a 

c pushed
stack d
top string
b f
h Push h
e e
$ $
98
Another PDA example
L( M )  {w  {a, b} : na ( w)  nb ( w)}
*

PDA M

a, $  0$ b, $  1$
a, 0  00 b, 1  11
a, 1   b, 0  

q1 , $  $ q2
99
Execution Example: Time 0
Input
a b b b a a

$
a, $  0$ b, $  1$
Stack
a, 0  00 b, 1  11
a, 1   b, 0  
current
state
q1 , $  $ q2
100
Time 1
Input
a b b b a a
0
$
a, $  0$ b, $  1$
Stack
a, 0  00 b, 1  11
a, 1   b, 0  

q1 , $  $ q2
101
Time 3
Input
a b b b a a
0
$
a, $  0$ b, $  1$
Stack
a, 0  00 b, 1  11
a, 1   b, 0  

q1 , $  $ q2
102
Time 4
Input
a b b b a a
1
$
a, $  0$ b, $  1$
Stack
a, 0  00 b, 1  11
a, 1   b, 0  

q1 , $  $ q2
103
Time 5
Input
a b b b a a 1
1
$
a, $  0$ b, $  1$
Stack
a, 0  00 b, 1  11
a, 1   b, 0  

q1 , $  $ q2
104
Time 6
Input
a b b b a a 1
1
$
a, $  0$ b, $  1$
Stack
a, 0  00 b, 1  11
a, 1   b, 0  

q1 , $  $ q2
105
Time 7
Input
a b b b a a
1
$
a, $  0$ b, $  1$
Stack
a, 0  00 b, 1  11
a, 1   b, 0  

q1 , $  $ q2
106
Time 8
Input
a b b b a a

$
a, $  0$ b, $  1$
Stack
a, 0  00 b, 1  11
a, 1   b, 0  
accept
q1 , $  $ q2
107
Formalities for PDAs

108
q1 a, b  w q2

Transition function:

 (q1, a, b)  {( q2 , w)}

109
q2
a, b  w

q1

a, b  w q3

Transition function:

 (q1, a, b)  {( q2 , w), (q3 , w)}


110
Formal Definition
Pushdown Automaton (PDA)

M  (Q, Σ, Γ, δ, q0 , z , F )
Final
States states

Input Stack
alphabet Transition Initial start
Stack
function state symbol
alphabet
111
Instantaneous Description

( q, u , s )

Current Current
state Remaining stack
input contents

112
Example: Instantaneous Description
(q1, bbb, aaa$)
a
Time 4: Input a
a a a b b b a
$
Stack
a,   a b, a  

q0  ,    q1 b, a   q2  , $  $ q3
113
Example: Instantaneous Description
(q2 , bb, aa$)
a
Time 5: Input a
a a a b b b a
$
Stack
a,   a b, a  

q0  ,    q1 b, a   q2  , $  $ q3
114
We write:

(q1, bbb, aaa$)  (q2 , bb, aa$)


Time 4 Time 5

115
A computation:

(q0 , aaabbb,$) (q1, aaabbb,$) 


(q1, aabbb, a$) (q1, abbb, aa$) (q1, bbb, aaa$) 
(q2 , bb, aa$) (q2 , b, a$) (q2 ,  ,$) (q3 ,  ,$)

a,   a b, a  

q0  ,    q1 b, a   q2  , $  $ q3
116
(q0 , aaabbb,$) (q1, aaabbb,$) 
(q1, aabbb, a$) (q1, abbb, aa$) (q1, bbb, aaa$) 
(q2 , bb, aa$) (q2 , b, a$) (q2 ,  ,$) (q3 ,  ,$)

For convenience we write:


(q0 , aaabbb,$)  (q3 ,  ,$)

117
Formal Definition

Language L(M ) of PDA M :


L( M )  {w : (q0 , w, s )  (q f ,  , s ' )}

Initial state Final state

118
Example: 
(q0 , aaabbb,$)  (q3 ,  ,$)

aaabbb L(M )

PDA M :

a,   a b, a  

q0  ,    q1 b, a   q2  , $  $ q3
119

n n
(q0 , a b ,$)  (q3 ,  ,$)

n n
a b  L(M )

PDA M :

a,   a b, a  

q0  ,    q1 b, a   q2  , $  $ q3
120
n n
Therefore: L( M )  {a b : n  0}

PDA M :

a,   a b, a  

q0  ,    q1 b, a   q2  , $  $ q3
121

You might also like