Push-Down Memory and Push-Down Automata

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

PUSH-DOWN MEMORY AND

PUSH-DOWN AUTOMATA

Push-down (stack) memory:


top (most recent) item in memory;
only c k is accessible at this point
c 1 c 2 c 2 ⋅⋅⋅ c k−1 c k
stack = c 1 c 2 ⋅⋅⋅c k−1 c k .

c k−1 is accessible
only after the removal of c k
c 1 c 2 c 2 ⋅⋅⋅ c k−1
stack = c 1 c 2 ⋅⋅⋅c k−1

addition of c k+1
makes c k inaccessible
c 1 c 2 c 2 ⋅⋅⋅ c k−1 c k c k+1
stack = c 1 c 2 ⋅⋅⋅c k−1 c k c k+1

Push-down automata = FSA + push-down memory.

• The stack-alphabet may be different from input alphabet.


Each transition does four things:
(1) Reads an input-symbol (and moves the read-head right).
(2) Looks at the top stack-symbol.
(3) Replaces the top stack-symbol by zero or more symbols.
(4) Changes state (perhaps back to the same state).
copyright@1995 8.2

EXAMPLE OF A PDA

Example. A PDA for L = {a n b n : n ≥ 1}.


• Save the a’s on the stack, then match them off with the b’s.

λ/a/a, a/a/aa a/b/λ


P an bn : q0 a/b/λ q Deterministic
1
PDA

• Transition-label: (stack-symbol, input-symbol, replacement-string).


• Initially stack is empty; in this case, stack-symbol = λ.
Processing of input x = aaabbb:
Stack (rightmost Remaining Transition
State
symbol on top) input string used
λ q0 aaabbb λ /a/a
a q0 aabbb a/a/aa
aa q0 abbb a/a/aa
(end of saving a’s) aaa q0 bbb a/b/ λ
aa q1 bb a/b/ λ
a q1 b a/b/ λ
(end of matching b’s) λ q1 λ

Language Defined by PDA: {x: x is accepted}.


x is accepted: The PDA reaches a final-state on reading all of x and
the stack is empty at that point.
x is rejected: x cannot be read completely or after reading x, PDA
does not reach a final-state or the stack is not empty.
copyright@1995 8.3

PDA FOR BALANCED


PARENTHETICAL STRINGS

We write a for ’(’ and b for ’)’for readability.


L λ +bal- par = {λ, ab, abab, aabb,
ababab, abaabb, aabbab, aababb, aaabbb, ⋅⋅⋅}

• Each of these strings, except for λ, contains an initial string of the


form a n b n (n > 0) or has such a string following an initial part a m .
• The removal of that a n b n makes the smaller string again balanced.

Idea: Merge q 0 and q 1 in P a n b n into a single state to allow the saving


of a’s and matching off with b’s can occur repeatedly.
a/b/λ ,
a/a/aa,
λ /a/a

P λ +bal- par : Deterministic


q0 PDA

Processing of x = a⋅ab⋅ab b:
Remaining Transition
Stack memory State
input string used
λ q0 aababb λ /a/a
a q0 ababb a/a/aa
aa q0 babb a/b/ λ
a q0 abb a/a/aa
aa q0 bb a/b/ λ
a q0 b a/b/ λ
λ q0 λ
copyright@1995 8.4

EXERCISE.
1. Construct a PDA to accept L bal- par ; λ ∉ L bal- par .
2. For each PDA below, describe the language accepted by it. Which
of the PDAs are deterministic?
λ/a/a, a/a/aa a/b/λ λ/a/a, a/a/aa a/b/λ a/b/λ, a/a/aa
q 0 a/b/λ q 1 q 0 a/b/λ q 1 q λ/a/a q
0 1
λ/a/a, a/a/aa λ/a/a

3. Consider the language L m≥n = {a m b n : m ≥ n ≥ 1} and the PDAs


below. For each PDA, find a smallest string x ∈ L m≥n not accepted
or x ∉ L m≥n but accepted. For each PDA, eliminate any unneces-
sary non-determinism if possible (without changing the language of
the PDA).
λ/a/λ a/a/aa, a/b/λ λ/a/λ a/a/aa a/b/λ
q0 λ/a/a q q 0 λ/a/a q 1 a/b/λ q 2
1

a/a/aa, a/a/a,
λ/a/λ a/b/λ λ/a/λ a/a/aa a/b/λ
q0 λ/a/a q q 0 λ/a/λ q 1 λ/a/a q 2 a/b/λ q 3
1

4. Show a PDA which accepts all non-empty strings over the alphabet
{0, 1, ×, =} which represent correct multiplications of the form
2m 2n = 2m+n . For m = 2 and n = 3, the input string is
100×1000=100000. (Hint: the symbols ’×’ and ’=’ in the input
string should drive the PDA to different states.)
5. Show that the language described in Problem 4 is not regular.
6. Give the state-diagram for a deterministic PDA which accepts the
language L m=2n = {a m b n : m = 2n, n ≥ 1}. Do the same for the lan-
guage L m=2n+1 , defined in a similar way.
copyright@1995 8.5

CONTEXT-FREE LANGUAGES AND PDAs

CFL: A language is context-free if it accepted by a PDA.

Language point of view:


• CFLs describe more general patterns than those described by regu-
lar languages. (Each regular language is also a CFL.)

CFLs •• {a n b n : n ≥ 1}
•• symmetric strings in {a, b}
RLs
•• balanced parenthetical strings
{a m b n : m ≥ 1, n ≥ 1} • •• bin. mult. of the form 2m 2n = 2m+n
L 3n •

•• {a m b n : m ≥ n ≥ 1}

••• a non-CFL: {a n b n c n : n ≥ 1}
••• a non-CFL: symmetric balanced parenthetical strings

Machine point of view:


• PDAs characterize computations that can be performed by a limited
use of external memory, called the stack memory, where the amount
of memory used is no more than a constant times the size of the
input.

A PDA uses at most a linear


(in the size of the input) amount of stack memory.
copyright@1995 8.6

PDA FOR SYMMETRIC STRINGS


L sym = all non-empty symmetric strings over {a, b}.
= {a, b, aa, bb, aaa, aba, bab, bbb, ⋅⋅⋅}
Idea:
(1) Non-deterministically decide a mid-point of the string (and
changes state from q 0 to q 1 ).
(2) Non-deterministically decide whether the string is of even length
or odd length. In the second case, ignore the unique middle sym-
bol (not matched with the next symbol).
(3) Save the first half of the string on the stack, and then match it off
with the second half.
λ/a/a, λ/b/b,
a/a/aa, a/b/ab, λ/a/λ, λ/b/λ,
a/a/a, a/b/a, a/a/λ,
b/a/ba, b/b/bb b/b/λ 3 transitions at q 0
b/a/b, b/b/b,
a/a/ λ , b/b/ λ for (stack-symbol,
P sym : q0 q1 input-symbol) = (a, a).

Processing of x = aabbaa (deterministic accepting behavior).


State Remaining Transition
Stack memory
tate input string used
λ q0 aabbaa λ /a/a
a q0 abbaa a/a/aa
aa q0 bbaa a/b/ab
aab q0 baa b/b/ λ
aa q1 aa a/a/ λ
a q1 a a/a/ λ
λ q1 λ
Any other processing lead to rejection of aabbaa.
copyright@1995 8.7

ALL MOVE-SEQUENCES IN P sym FOR x = ab ∉ L sym

Configuration: (stack, state, remaining-input).


• For input x and start-state = q 0 , initial configuration is (λ, q 0 , x).

(λ, q 0 , ab)

(a, q 0 , b) (λ, q 1 , b)
non-accepting

(ab, q 0 , λ) (a, q 1 , λ)
non-accepting non-accepting

x = ab is not accepted by P sym .


EXERCISE.
1. Give the numbers of symmetric strings and non-symmetric strings
for length m = 2k and length m = 2k + 1.
2. Give the state-diagram of a deterministic PDA for accepting the
language L #a=#b = {x ∈ (a + b)+ : #(a, x) = #(b, x)}.
3. Give a PDA for L a n+3 b n .
4. Give the state diagram of deterministic-behavior PDA for L non−sym
= {ab, ba, aab, abb, baa, bba, ⋅⋅⋅} with deterministic-behavior.
Show the tree of all move sequences of the PDA for x = ab.
5. Show a PDA for the language L m≠n = {a m b n : m ≠ n, m, n ≥ 1} with
deterministic behavior.
Show the configurations in all possible move sequences for your
PDA for the input x = a2 b2 to show that it is not accepted.
copyright@1995 8.8

COMPLEMENT OF {a n b n : n ≥ 1}

Five kinds of strings in the complement:


(1) x = λ. (4) x = a n b m and m < n (> 0).
(2) x begins with b, i.e., x = by. (5) x = a n b m ay and m ≤ n (> 0).
(3) x = a n b n+1 y (n > 0).
λ/a/λ,
λ/a/λ a/a/aa a/b/λ λ/b/λ
q4 λ/a/a q a/b/λ q λ/a/λ q
5 6 7

λ/a/λ
a/a/aa a/b/λ
λ/a/a q a/b/λ q q 0 handles (1)
q0 2 3 q 1 handles (2)-(3) and (5) for m = n
q 4 handles (4) for m = 0
λ/b/λ λ/b/λ, q 6 handles (4) for m > 0
λ/a/λ q 7 handles (5) for m < n
q1

λ/a/λ, λ/b/λ

Non-deterministic PDA, with a deterministic accepting behavior.

EXERCISE.
1. Construct a PDA to accept the complement of L bal- par ; the com-
plement includes λ.
copyright@1995 8.9

INTERSECTION OF CFL AND RL IS CFL

AND-construction of a PDA and an FSA:


• The states of the new PDA have the form q i q′j , where q i = a state
of the PDA and q′j = a state of the FSA. The parts q i and q′j
change according to the PDA and the FSA, respectively.
• The stack operations remain as in the old PDA.
λ/a/a, λ/b/b,
a/a/aa, a/b/ab, λ/a/λ, λ/b/λ,
b/a/ba, b/b/bb a/a/a, a/b/a, a/a/λ,
b/a/b, b/b/b, b/b/λ a b
a/a/ λ , b/b/ λ b
L sym : q 0 q1 L end-b : A a B

PDA for L sym ∩L end-b :


• Non-deterministic transitions but deterministic behavior.
• The transition λ/a/λ from q 0 A to q 1 A never takes part in accepting
a string (and can be removed). Are there other such transitions?

λ /a/a, a/a/aa b/b/bb


q0 A a/b/ab, λ /b/b q B
b/a/ba 0

λ/a/λ, λa/b/a
/b/ λ , b/a/b b/b/ λ ,
a/a/ λ , b/b/b
a/a/a
q1 A b/b/ λ q1 B
a/a/ λ
a/a/ λ b/b/ λ

Question:
•? Show the tree of all configurations in processing aba.
copyright@1995 8.10

EXERCISE
1. Argue that the union of a CFL and an RL is a CFL.
2. Assume that L a n b n c n = {a n b n c n : n ≥ 1} is not CFL, i.e., it is not
accepted by any PDA. Then, show that {x ∈ (a + b + c)∗ : #(x, a) =
#(x, b) = #(x, c)} is not CFL.
3. Give a PDA for the language L #a=#b = {x ∈ (a + b)∗ : #(x, a) =
#(x, b)}.
4. If L is a CFL and L′ is regular, then argue that L L′ and L′L are
CFL
5. Since L a n b n c n = L a n b n c∗ ∩ L a∗ b n c n and both L a n b n c∗ and L a∗ b n c n are
CFL, it follows that intersection of CFLs need not be a CFL. Thus,
complement of a CFL need not be a CFL either.
copyright@1995 8.11

FINDING NORTHMOST POSITION


IN A LINEAR MAZE

Linear Maze: Each square has ≤ 2 neighbors; it is a strip laid in a


straight line or a curved non-crossing fashion.

20 23
24 45
2 1
3
28 34
33
32

Some observations:
• Starting at 1, when the robot reaches 20 (or 23) the stack must tell
us that the robot is one step further north of position 1.
• At 28 (also at 34), the stack should tell us that the maximum north
reached so far was one step higher than the start position, and that
currently the robot is 4 steps south from that max-north position.
• At 45, the stack must tell us we are still south of the max-north
position and it must allow us to get back to position 23, say.
EXERCISE.
1. Design a PDA-control of a robot so that for any initital placement
of it in a linear maze, it will stop at a northmost position. Hints: (1)
First make the robot go to a dead-end. (2) Use the stack to keep
track of north-south motion as it now moves to the other dead-end.
Show the stack for each position of the robot till it stops.
copyright@1995 8.12

TRAVERSING AN ACYCLIC MAZE ONCE

1. Design a PDA to control the motion of a robot so that if it is put in


any acyclic maze as shown below, then it will start traversing the
maze using "keep to the right" strategy and come to a stop when it
arrives at its initial position for the first time; in the process it may
have traversed the whole maze or only a part of the maze. (Hint:
Use the stack to keep track of information related to the path taken
so that when you return to the initial position, the stack is empty.)
Show the stack during the operation of robot starting at 3 till it gets
back to it for the first time.

1 3

2. Is it possible to make the robot stop at a northmost position in


any acyclic maze?

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy