Title: Pushdown Automata PDAs
1Lecture 34
- Pushdown Automata (PDAs)
- Another example
2Palindromes
- Let PAL be the set of palindromes over a,b
- Let PAL1 be the following related language
- wcwr w consists only of as and bs
- we add c to the input alphabet as a special
marker character - Strings in PAL1
- aca, bcb, abcba, aabcbaa, c
- strings not in PAL1
- aaca, aaccaa, abccba, abcb, abba
- Let PAL2 be the set of even length palindromes
- wwr w consists only of as and bs
3PAL1
- Lets first construct a PDA for PAL1
- Basic ideas
- Have one state remember first half of string
- Have one state match second half of string to
first half - Transition between these two states when the
first c is encountered
4PDA for PAL1
- M (Q, S, G, q0, Z, A, d)
- Q q0, qm, qf
- S a, b, c
- G Z, a, b
- q0 q0
- Z Z
- A qf
5Transition Function
Trans Current Input Top of Next Stack
State Char. Stack State
Update -------------------------------------------
------------ 1 q0 a
Z q0 aZ 2 q0
a a q0 aa 3 q0
a b q0 ab 4
q0 b Z q0
bZ 5 q0 b a
q0 ba 6 q0 b
b q0 bb 7 q0
c Z qm Z 8
q0 c a qm
a 9 q0 c b
qm b 10 qm a
a qm l 11 qm b
b qm l 12 qm
l Z qf Z
First three transitions push a on top of the
stack Second three transitions push b on the
stack Third three transitions switch state q0 to
qm No change to stack Transitions 10 and 11
match characters from first and last half of
input string
6Notation comment
Trans Current Input Top of Next Stack
State Char. Stack State
Update -------------------------------------------
------------ 1 q0 a
Z q0 aZ 2 q0
a a q0 aa 3 q0
a b q0 ab 4
q0 b Z q0
bZ 5 q0 b a
q0 ba 6 q0 b
b q0 bb 7 q0
c Z qm Z 8
q0 c a qm
a 9 q0 c b
qm b 10 qm a
a qm l 11 qm b
b qm l 12 qm
l Z qf Z
- We might represent transition 1 in two other ways
- d(q0,a,Z) (q0, aZ)
- (q0, a, Z, q0, aZ)
- Question
- Is this PDA deterministic?
7Computation Graph 1
Trans Current Input Top of Next Stack
State Char. Stack State
Update -------------------------------------------
------------ 1 q0 a
Z q0 aZ 2 q0
a a q0 aa 3 q0
a b q0 ab 4
q0 b Z q0
bZ 5 q0 b a
q0 ba 6 q0 b
b q0 bb 7 q0
c Z qm Z 8
q0 c a qm
a 9 q0 c b
qm b 10 qm a
a qm l 11 qm b
b qm l 12 qm
l Z qf Z
(q0, abcba, Z)
8Computation Graph 2
Trans Current Input Top of Next Stack
State Char. Stack State
Update -------------------------------------------
------------ 1 q0 a
Z q0 aZ 2 q0
a a q0 aa 3 q0
a b q0 ab 4
q0 b Z q0
bZ 5 q0 b a
q0 ba 6 q0 b
b q0 bb 7 q0
c Z qm Z 8
q0 c a qm
a 9 q0 c b
qm b 10 qm a
a qm l 11 qm b
b qm l 12 qm
l Z qf Z
(q0, abcab, Z)
9Computation Graph 3
Trans Current Input Top of Next Stack
State Char. Stack State
Update -------------------------------------------
------------ 1 q0 a
Z q0 aZ 2 q0
a a q0 aa 3 q0
a b q0 ab 4
q0 b Z q0
bZ 5 q0 b a
q0 ba 6 q0 b
b q0 bb 7 q0
c Z qm Z 8
q0 c a qm
a 9 q0 c b
qm b 10 qm a
a qm l 11 qm b
b qm l 12 qm
l Z qf Z
(q0, acab, Z)
10PAL2
- Lets now construct a PDA for PAL
- What is harder this time?
- When do we switch from putting strings on the
stack to matching? - Example
- After seeing aab, should we switch to match mode
or stay in stack mode? - Solution
- Do both using nondeterminism
11PDA for PAL2
- M (Q, S, G, q0, Z, A, d)
- Q q0, qm, qf
- S a, b
- G Z, a, b
- q0 q0
- Z Z
- A qf
12Transition Function
Trans Current Input Top of Next Stack
State Char. Stack State
Update -------------------------------------------
------------ 1 q0 a
Z q0 aZ 2 q0
a a q0 aa 3 q0
a b q0 ab 4
q0 b Z q0
bZ 5 q0 b a
q0 ba 6 q0 b
b q0 bb 7 q0
l Z qm Z 8
q0 l a qm
a 9 q0 l b
qm b 10 qm a
a qm l 11 qm b
b qm l 12 qm
l Z qf Z
First three transitions push a on top of the
stack Second three transitions push b on the
stack Third three transitions switch state q0 to
qm Note now that one of transitions 7, 8, and 9
can be used whenever transitions 1-6 are used.
13Computation Graph 1
Trans Current Input Top of Next Stack
State Char. Stack State
Update -------------------------------------------
------------ 1 q0 a
Z q0 aZ 2 q0
a a q0 aa 3 q0
a b q0 ab 4
q0 b Z q0
bZ 5 q0 b a
q0 ba 6 q0 b
b q0 bb 7 q0
l Z qm Z 8
q0 l a qm
a 9 q0 l b
qm b 10 qm a
a qm l 11 qm b
b qm l 12 qm
l Z qf Z
(q0, abba, Z)
14Computation Graph 2
Trans Current Input Top of Next Stack
State Char. Stack State
Update -------------------------------------------
------------ 1 q0 a
Z q0 aZ 2 q0
a a q0 aa 3 q0
a b q0 ab 4
q0 b Z q0
bZ 5 q0 b a
q0 ba 6 q0 b
b q0 bb 7 q0
l Z qm Z 8
q0 l a qm
a 9 q0 l b
qm b 10 qm a
a qm l 11 qm b
b qm l 12 qm
l Z qf Z
(q0, aba, Z)
15PAL
- Challenge
- Construct a PDA for PAL
- First step
- Construct a PDA for odd length palindromes
- Then
- Combine PDAs for odd length and even length
palindromes