pushdown automata and informatics... · 2018-12-12 · a pda for {anbn: n ≥ 0} •we usually use...

46
PushDown Automata By Dr. Fathy

Upload: others

Post on 16-Jul-2020

0 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: PushDown Automata and Informatics... · 2018-12-12 · A PDA for {anbn: n ≥ 0} •We usually use the stack for counting. •For this language for example, you first insert all the

PushDown Automata

By Dr. Fathy

Page 2: PushDown Automata and Informatics... · 2018-12-12 · A PDA for {anbn: n ≥ 0} •We usually use the stack for counting. •For this language for example, you first insert all the

What is a stack?

• A stack is a Last In First Out data structure where I only have access to the last element inserted in the stack.

• In order to access other elements I have to remove those that are on top one by one.

Page 3: PushDown Automata and Informatics... · 2018-12-12 · A PDA for {anbn: n ≥ 0} •We usually use the stack for counting. •For this language for example, you first insert all the

Stack

Page 4: PushDown Automata and Informatics... · 2018-12-12 · A PDA for {anbn: n ≥ 0} •We usually use the stack for counting. •For this language for example, you first insert all the

Stack

Page 5: PushDown Automata and Informatics... · 2018-12-12 · A PDA for {anbn: n ≥ 0} •We usually use the stack for counting. •For this language for example, you first insert all the

Stack

Page 6: PushDown Automata and Informatics... · 2018-12-12 · A PDA for {anbn: n ≥ 0} •We usually use the stack for counting. •For this language for example, you first insert all the

Stack

Page 7: PushDown Automata and Informatics... · 2018-12-12 · A PDA for {anbn: n ≥ 0} •We usually use the stack for counting. •For this language for example, you first insert all the

Stack

Page 8: PushDown Automata and Informatics... · 2018-12-12 · A PDA for {anbn: n ≥ 0} •We usually use the stack for counting. •For this language for example, you first insert all the

Stack

Page 9: PushDown Automata and Informatics... · 2018-12-12 · A PDA for {anbn: n ≥ 0} •We usually use the stack for counting. •For this language for example, you first insert all the

Stack

Page 10: PushDown Automata and Informatics... · 2018-12-12 · A PDA for {anbn: n ≥ 0} •We usually use the stack for counting. •For this language for example, you first insert all the

Stack

Page 11: PushDown Automata and Informatics... · 2018-12-12 · A PDA for {anbn: n ≥ 0} •We usually use the stack for counting. •For this language for example, you first insert all the

What is a PDA?

• A PDA is an enhanced finite automaton that also contains an infinite stack.

• The transitions in a PDA are of the form

a, x ⟶ y

meaning that if you see an a in the input string and the stack contains the symbol x on top then you remove the x and add a y.

• The stack gives us extra power to recognize non-regular languages.

Page 12: PushDown Automata and Informatics... · 2018-12-12 · A PDA for {anbn: n ≥ 0} •We usually use the stack for counting. •For this language for example, you first insert all the

Transitions

• Transitions of the form a, x ⟶ y require that the next input symbol should be a and the top stack symbol should be x.

q q’a, x ⟶ y

xw

...abb...

Stack Input

q q’a, x ⟶ y

yw

...abb...

Stack Input

Page 13: PushDown Automata and Informatics... · 2018-12-12 · A PDA for {anbn: n ≥ 0} •We usually use the stack for counting. •For this language for example, you first insert all the

Transitions

• Transitions of the form ε, x ⟶ y require that the top stack symbol is x.

q q’ε, x ⟶ y

xw

Stack Input

q q’ε, x ⟶ y

yw

Stack Input

...abb... ...abb...

Page 14: PushDown Automata and Informatics... · 2018-12-12 · A PDA for {anbn: n ≥ 0} •We usually use the stack for counting. •For this language for example, you first insert all the

Transitions

• Transitions of the form a, ε ⟶ y require that the next input symbol is a.

q q’a, ε ⟶ y

xw

Stack Input

q q’a, ε ⟶ y

yxw

Stack Input

...abb... ...abb...

Page 15: PushDown Automata and Informatics... · 2018-12-12 · A PDA for {anbn: n ≥ 0} •We usually use the stack for counting. •For this language for example, you first insert all the

Transitions

• Transitions of the form ε, ε ⟶ y can be followed without restrictions.

q q’ε, ε ⟶ y

xw

Stack Input

q q’ε, ε ⟶ y

yxw

Stack Input

...abb... ...abb...

Page 16: PushDown Automata and Informatics... · 2018-12-12 · A PDA for {anbn: n ≥ 0} •We usually use the stack for counting. •For this language for example, you first insert all the

PDA Accept – Reject Status

• The PDA accepts when there exists a computation path such that:

– The computation path ends in an accept state

– All the input is consumed

– (no requirement for the stack)

• The PDA rejects when all the paths:

– Either end in a non-accepting state

– Or are incomplete (meaning that at some point there is no possible transition under the current input and stack symbols)

Page 17: PushDown Automata and Informatics... · 2018-12-12 · A PDA for {anbn: n ≥ 0} •We usually use the stack for counting. •For this language for example, you first insert all the

A PDA for {anbn : n ≥ 0}

• We usually use the stack for counting.

• For this language for example, you first insert all the as in the stack until you start seeing bs .

• When you see the first b start removing as from the stack.

• When you have consumed the whole string you check the stack: if it’s empty then this means that the number of as equals the number of bs.

Page 18: PushDown Automata and Informatics... · 2018-12-12 · A PDA for {anbn: n ≥ 0} •We usually use the stack for counting. •For this language for example, you first insert all the

Is the stack empty?

How can you check if the stack is empty?

• What we usually do is to place a special symbol (for example a $) at the bottom of the stack.

• Whenever we find the $ again we know that we reached the end of the stack.

• In order to accept a string there is no need for the stack to be empty.

Page 19: PushDown Automata and Informatics... · 2018-12-12 · A PDA for {anbn: n ≥ 0} •We usually use the stack for counting. •For this language for example, you first insert all the

Stack push and pop in PDA

• a, ε ⟶ t

when you see an a in the input push t on the stack

• a, b ⟶ ε

when you see an a in the input and b is on the top of the stack, pop b out.

Page 20: PushDown Automata and Informatics... · 2018-12-12 · A PDA for {anbn: n ≥ 0} •We usually use the stack for counting. •For this language for example, you first insert all the

A PDA for {anbn : n ≥ 0}

q0 q1

q3 q2

ε, ε ⟶ $

ε, $ ⟶ ε

b, x ⟶ ε

Page 21: PushDown Automata and Informatics... · 2018-12-12 · A PDA for {anbn: n ≥ 0} •We usually use the stack for counting. •For this language for example, you first insert all the

Visualization of {anbn:n ≥ 0}

aaabbb

q0 q1

q3 q2

ε, ε ⟶ $

ε, $ ⟶ ε

b, x ⟶ ε

Page 22: PushDown Automata and Informatics... · 2018-12-12 · A PDA for {anbn: n ≥ 0} •We usually use the stack for counting. •For this language for example, you first insert all the

Visualization of {anbn:n ≥ 0}

aaabbb

$

q0 q1

q3 q2

ε, ε ⟶ $

ε, $ ⟶ ε

b, x ⟶ ε

Page 23: PushDown Automata and Informatics... · 2018-12-12 · A PDA for {anbn: n ≥ 0} •We usually use the stack for counting. •For this language for example, you first insert all the

Visualization of {anbn:n ≥ 0}

aaabbb

x$

q0 q1

q3 q2

ε, ε ⟶ $

ε, $ ⟶ ε

b, x ⟶ ε

Page 24: PushDown Automata and Informatics... · 2018-12-12 · A PDA for {anbn: n ≥ 0} •We usually use the stack for counting. •For this language for example, you first insert all the

Visualization of {anbn:n ≥ 0}

aaabbb

xx$

q0 q1

q3 q2

ε, ε ⟶ $

ε, $ ⟶ ε

b, x ⟶ ε

Page 25: PushDown Automata and Informatics... · 2018-12-12 · A PDA for {anbn: n ≥ 0} •We usually use the stack for counting. •For this language for example, you first insert all the

Visualization of {anbn:n ≥ 0}

aaabbb

xxx$

q0 q1

q3 q2

ε, ε ⟶ $

ε, $ ⟶ ε

b, x ⟶ ε

Page 26: PushDown Automata and Informatics... · 2018-12-12 · A PDA for {anbn: n ≥ 0} •We usually use the stack for counting. •For this language for example, you first insert all the

Visualization of {anbn:n ≥ 0}

aaabbb

xx$

q0 q1

q3 q2

ε, ε ⟶ $

ε, $ ⟶ ε

b, x ⟶ ε

Page 27: PushDown Automata and Informatics... · 2018-12-12 · A PDA for {anbn: n ≥ 0} •We usually use the stack for counting. •For this language for example, you first insert all the

Visualization of {anbn:n ≥ 0}

aaabbb

x$

q0 q1

q3 q2

ε, ε ⟶ $

ε, $ ⟶ ε

b, x ⟶ ε

Page 28: PushDown Automata and Informatics... · 2018-12-12 · A PDA for {anbn: n ≥ 0} •We usually use the stack for counting. •For this language for example, you first insert all the

Visualization of {anbn:n ≥ 0}

aaabbb

$

q0 q1

q3 q2

ε, ε ⟶ $

ε, $ ⟶ ε

b, x ⟶ ε

Page 29: PushDown Automata and Informatics... · 2018-12-12 · A PDA for {anbn: n ≥ 0} •We usually use the stack for counting. •For this language for example, you first insert all the

Visualization of {anbn:n ≥ 0}

aaabbb

q0 q1

q3 q2

ε, ε ⟶ $

ε, $ ⟶ ε

b, x ⟶ ε

Page 30: PushDown Automata and Informatics... · 2018-12-12 · A PDA for {anbn: n ≥ 0} •We usually use the stack for counting. •For this language for example, you first insert all the

Visualization of {anbn:n ≥ 0}

aab

q0 q1

q3 q2

ε, ε ⟶ $

ε, $ ⟶ ε

b, x ⟶ ε

Page 31: PushDown Automata and Informatics... · 2018-12-12 · A PDA for {anbn: n ≥ 0} •We usually use the stack for counting. •For this language for example, you first insert all the

Visualization of {anbn:n ≥ 0}

aab

$

q0 q1

q3 q2

ε, ε ⟶ $

ε, $ ⟶ ε

b, x ⟶ ε

Page 32: PushDown Automata and Informatics... · 2018-12-12 · A PDA for {anbn: n ≥ 0} •We usually use the stack for counting. •For this language for example, you first insert all the

Visualization of {anbn:n ≥ 0}

aab

x$

q0 q1

q3 q2

ε, ε ⟶ $

ε, $ ⟶ ε

b, x ⟶ ε

Page 33: PushDown Automata and Informatics... · 2018-12-12 · A PDA for {anbn: n ≥ 0} •We usually use the stack for counting. •For this language for example, you first insert all the

Visualization of {anbn:n ≥ 0}

aab

xx$

q0 q1

q3 q2

ε, ε ⟶ $

ε, $ ⟶ ε

b, x ⟶ ε

Page 34: PushDown Automata and Informatics... · 2018-12-12 · A PDA for {anbn: n ≥ 0} •We usually use the stack for counting. •For this language for example, you first insert all the

Visualization of {anbn:n ≥ 0}

aab

x$

q0 q1

q3 q2

ε, ε ⟶ $

ε, $ ⟶ ε

b, x ⟶ ε

Page 35: PushDown Automata and Informatics... · 2018-12-12 · A PDA for {anbn: n ≥ 0} •We usually use the stack for counting. •For this language for example, you first insert all the

Visualization of {anbn:n ≥ 0}

aab

x$

q0 q1

q3 q2

ε, ε ⟶ $

ε, $ ⟶ ε

b, x ⟶ ε

Page 36: PushDown Automata and Informatics... · 2018-12-12 · A PDA for {anbn: n ≥ 0} •We usually use the stack for counting. •For this language for example, you first insert all the

Visualization of {anbn:n ≥ 0}

abb

q0 q1

q3 q2

ε, ε ⟶ $

ε, $ ⟶ ε

b, x ⟶ ε

Page 37: PushDown Automata and Informatics... · 2018-12-12 · A PDA for {anbn: n ≥ 0} •We usually use the stack for counting. •For this language for example, you first insert all the

Visualization of {anbn:n ≥ 0}

abb

$

q0 q1

q3 q2

ε, ε ⟶ $

ε, $ ⟶ ε

b, x ⟶ ε

Page 38: PushDown Automata and Informatics... · 2018-12-12 · A PDA for {anbn: n ≥ 0} •We usually use the stack for counting. •For this language for example, you first insert all the

Visualization of {anbn:n ≥ 0}

abb

x$

q0 q1

q3 q2

ε, ε ⟶ $

ε, $ ⟶ ε

b, x ⟶ ε

Page 39: PushDown Automata and Informatics... · 2018-12-12 · A PDA for {anbn: n ≥ 0} •We usually use the stack for counting. •For this language for example, you first insert all the

Visualization of {anbn:n ≥ 0}

abb

$

q0 q1

q3 q2

ε, ε ⟶ $

ε, $ ⟶ ε

b, x ⟶ ε

Page 40: PushDown Automata and Informatics... · 2018-12-12 · A PDA for {anbn: n ≥ 0} •We usually use the stack for counting. •For this language for example, you first insert all the

Visualization of {anbn:n ≥ 0}

abb

$

q0 q1

q3 q2

ε, ε ⟶ $

ε, $ ⟶ ε

b, x ⟶ ε

Page 41: PushDown Automata and Informatics... · 2018-12-12 · A PDA for {anbn: n ≥ 0} •We usually use the stack for counting. •For this language for example, you first insert all the

PDA formally

• A PDA is a sextuple (Q, Σ, Γ, δ, q0, F), where:

– Q is the set of states

– Σ is the input alphabet

– Γ is the alphabet for the stack

– δ is the transition function

– q0 is the start state

– F is the set of accepting states

About Γ: The stack alphabet can contain any symbol you want. It can be completely disjoint from Σ.

Page 42: PushDown Automata and Informatics... · 2018-12-12 · A PDA for {anbn: n ≥ 0} •We usually use the stack for counting. •For this language for example, you first insert all the

L() : proper opening and closing parenthesis

Page 43: PushDown Automata and Informatics... · 2018-12-12 · A PDA for {anbn: n ≥ 0} •We usually use the stack for counting. •For this language for example, you first insert all the

L() : proper opening and closing parenthesis

(, ε ⟶ *

), * ⟶ ε

q0 q1

ε, ε ⟶ $

ε, $ ⟶ ε

Page 44: PushDown Automata and Informatics... · 2018-12-12 · A PDA for {anbn: n ≥ 0} •We usually use the stack for counting. •For this language for example, you first insert all the

Try it yourself

• Create a PDA for the language:

L= = {w : w contains an equal number of 0s and 1s}

Page 45: PushDown Automata and Informatics... · 2018-12-12 · A PDA for {anbn: n ≥ 0} •We usually use the stack for counting. •For this language for example, you first insert all the

L= : equal number of 0s and 1s

q0 q1

q2

0, ε ⟶ *1, * ⟶ ε

q3

ε, ε ⟶ $

1, ε ⟶ *0, * ⟶ ε

Page 46: PushDown Automata and Informatics... · 2018-12-12 · A PDA for {anbn: n ≥ 0} •We usually use the stack for counting. •For this language for example, you first insert all the

L= : equal number of 0s and 1s

NPDA for this language

0, ε ⟶ y0, x⟶ ε1, ε ⟶ x1, y⟶ ε

q0 q1

ε, ε ⟶ $

ε, $ ⟶ ε