Download presentation
Presentation is loading. Please wait.
1
1 Module 33 Pushdown Automata (PDA’s) –Another example
2
2 Palindromes Let PAL be the set of palindromes over {a,b} –Let PAL1 be the following related language: {wcw r | w consists only of a’s and b’s} –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 {ww r | w consists only of a’s and b’s}
3
3 PAL1 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
4
4 PDA for PAL1 M = (Q, , , q 0, Z, A, ) –Q = {q 0, q m, q f } – = {a, b, c} – = {Z, a, b} –q 0 = q 0 –Z = Z –A = {q f }
5
5 Transition Function Trans Current Input Top of Next Stack # State Char. Stack State Update ------------------------------------------------------- 1 q 0 a Z q 0 aZ 2 q 0 a a q 0 aa 3 q 0 a b q 0 ab 4 q 0 b Z q 0 bZ 5 q 0 b a q 0 ba 6 q 0 b b q 0 bb 7 q 0 c Z q m Z 8 q 0 c a q m a 9 q 0 c b q m b 10 q m a a q m 11 q m b b q m 12 q m Z q f Z First three transitions push a on top of the stack Second three transitions push b on the stack Third three transitions switch state q 0 to q m No change to stack Transitions 10 and 11 “match” characters from first and last half of input string
6
6 Notation comment We might represent transition 1 in two other ways (q 0,a,Z) = (q 0, aZ) (q 0, a, Z, q 0, aZ) Question Is this PDA deterministic? Trans Current Input Top of Next Stack # State Char. Stack State Update ------------------------------------------------------- 1 q 0 a Z q 0 aZ 2 q 0 a a q 0 aa 3 q 0 a b q 0 ab 4 q 0 b Z q 0 bZ 5 q 0 b a q 0 ba 6 q 0 b b q 0 bb 7 q 0 c Z q m Z 8 q 0 c a q m a 9 q 0 c b q m b 10 q m a a q m 11 q m b b q m 12 q m Z q f Z
7
7 Computation Graph 1 (q 0, abcba, Z) (q 0, bcba, aZ)(q 0, cba, baZ) (q m, ba, baZ) (q m, a, aZ) (q m,, Z)(q f,, Z) Trans Current Input Top of Next Stack # State Char. Stack State Update ------------------------------------------------------- 1 q 0 a Z q 0 aZ 2 q 0 a a q 0 aa 3 q 0 a b q 0 ab 4 q 0 b Z q 0 bZ 5 q 0 b a q 0 ba 6 q 0 b b q 0 bb 7 q 0 c Z q m Z 8 q 0 c a q m a 9 q 0 c b q m b 10 q m a a q m 11 q m b b q m 12 q m Z q f Z
8
8 Computation Graph 2 (q 0, abcab, Z) (q 0, bcab, aZ)(q 0, cab, baZ)(q m, ab, baZ) Trans Current Input Top of Next Stack # State Char. Stack State Update ------------------------------------------------------- 1 q 0 a Z q 0 aZ 2 q 0 a a q 0 aa 3 q 0 a b q 0 ab 4 q 0 b Z q 0 bZ 5 q 0 b a q 0 ba 6 q 0 b b q 0 bb 7 q 0 c Z q m Z 8 q 0 c a q m a 9 q 0 c b q m b 10 q m a a q m 11 q m b b q m 12 q m Z q f Z
9
9 Computation Graph 3 (q 0, acab, Z) (q 0, cab, aZ)(q m, ab, aZ)(q m, b, Z)(q f, b, Z) Trans Current Input Top of Next Stack # State Char. Stack State Update ------------------------------------------------------- 1 q 0 a Z q 0 aZ 2 q 0 a a q 0 aa 3 q 0 a b q 0 ab 4 q 0 b Z q 0 bZ 5 q 0 b a q 0 ba 6 q 0 b b q 0 bb 7 q 0 c Z q m Z 8 q 0 c a q m a 9 q 0 c b q m b 10 q m a a q m 11 q m b b q m 12 q m Z q f Z
10
10 PAL2 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
11
11 PDA for PAL2 M = (Q, , , q 0, Z, A, ) –Q = {q 0, q m, q f } – = {a, b} – = {Z, a, b} –q 0 = q 0 –Z = Z –A = {q f }
12
12 Transition Relation Trans Current Input Top of Next Stack # State Char. Stack State Update ------------------------------------------------------- 1 q 0 a Z q 0 aZ 2 q 0 a a q 0 aa 3 q 0 a b q 0 ab 4 q 0 b Z q 0 bZ 5 q 0 b a q 0 ba 6 q 0 b b q 0 bb 7 q 0 Z q m Z 8 q 0 a q m a 9 q 0 b q m b 10 q m a a q m 11 q m b b q m 12 q m Z q f Z First three transitions push a on top of the stack Second three transitions push b on the stack Third three transitions switch state q 0 to q m Is the PDA deterministic or nondeterministic?
13
13 Computation Graph 1 (q 0, abba, Z) (q 0, bba, aZ) (q m, abba, Z) (q 0, ba, baZ) (q m, bba, aZ)(q f, abba, Z) (q 0, a, bbaZ) (q m, ba, baZ) (q 0,, abbaZ) (q m, a, bbaZ) (q m, a, aZ) (q m,, abbaZ) (q m,, Z) (q f,, Z) Trans Current Input Top of Next Stack # State Char. Stack State Update ------------------------------------------------------- 1 q 0 a Z q 0 aZ 2 q 0 a a q 0 aa 3 q 0 a b q 0 ab 4 q 0 b Z q 0 bZ 5 q 0 b a q 0 ba 6 q 0 b b q 0 bb 7 q 0 Z q m Z 8 q 0 a q m a 9 q 0 b q m b 10 q m a a q m 11 q m b b q m 12 q m Z q f Z
14
14 Computation Graph 2 (q 0, aba, Z) (q 0, ba, aZ) (q m, aba, Z) (q 0, a, baZ) (q m, ba, aZ)(q f, aba, Z) (q 0,, abaZ) (q m, a, baZ) (q m,, abaZ) Trans Current Input Top of Next Stack # State Char. Stack State Update ------------------------------------------------------- 1 q 0 a Z q 0 aZ 2 q 0 a a q 0 aa 3 q 0 a b q 0 ab 4 q 0 b Z q 0 bZ 5 q 0 b a q 0 ba 6 q 0 b b q 0 bb 7 q 0 Z q m Z 8 q 0 a q m a 9 q 0 b q m b 10 q m a a q m 11 q m b b q m 12 q m Z q f Z
15
15 PAL Challenge –Construct a PDA for PAL –First step Construct a PDA for odd length palindromes –Then Combine PDA’s for odd length and even length palindromes
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.