Download ppt - Pushdown Automata PDAs

Transcript
Page 1: Pushdown Automata PDAs

Fall 2004 COMP 335 1

Pushdown AutomataPDAs

Page 2: Pushdown Automata PDAs

Fall 2004 COMP 335 2

Pushdown Automaton -- PDAInput String

Stack

States

Page 3: Pushdown Automata PDAs

Fall 2004 COMP 335 3

Initial Stack Symbol

Stack

$

Stack

z

bottomspecial symbol

stackhead top

Page 4: Pushdown Automata PDAs

Fall 2004 COMP 335 4

The States

q1 q2a, b c

Inputsymbol

Popsymbol

Pushsymbol

Page 5: Pushdown Automata PDAs

Fall 2004 COMP 335 5

q1 q2a, b c

a

b top

input

stack

a

Replaceeh

$eh

$

c

Page 6: Pushdown Automata PDAs

Fall 2004 COMP 335 6

q1 q2a, c

a a

Pushb

eh

$eh

$

bc

top

input

stack

Page 7: Pushdown Automata PDAs

Fall 2004 COMP 335 7

q1 q2a, b

a a

Popb

eh

$eh

$

top

input

stack

Page 8: Pushdown Automata PDAs

Fall 2004 COMP 335 8

q1 q2a,

a a

No Changeb

eh

$eh

$

btop

input

stack

Page 9: Pushdown Automata PDAs

Fall 2004 COMP 335 9

q1 q2$,a

a a

Pop$ top

input

stack

A Possible Transition

empty

Page 10: Pushdown Automata PDAs

Fall 2004 COMP 335 10

q1 q2cba ,

a input

A Bad Transition

The automaton Halts in state and Rejects the input string

Empty stack

q1

HALT

Page 11: Pushdown Automata PDAs

Fall 2004 COMP 335 11

q1 q2ca ,

a input

A Bad Transition

The automaton Halts in state and Rejects the input string

Empty stack

q1

HALT

Page 12: Pushdown Automata PDAs

Fall 2004 COMP 335 12

q1 q2zyx ,

No transition is allowed to be followedwhen the stack is empty

Empty stack

Page 13: Pushdown Automata PDAs

Fall 2004 COMP 335 13

q1 q2ba $,

a a

Pop$ top

input

stack

A Good Transition

b

Page 14: Pushdown Automata PDAs

Fall 2004 COMP 335 14

Non-Determinism

q1

q2

q3dba ,

q1 q2, b c

transition

These are allowed transitions in a Non-deterministic PDA (NPDA)

cba ,

Page 15: Pushdown Automata PDAs

Fall 2004 COMP 335 15

NPDA: Non-Deterministic PDA

Example:

,

a, a

b, a q0 q1 q2 q3

b, a

, $ $

Page 16: Pushdown Automata PDAs

Fall 2004 COMP 335 16

a, a

b, a 0q q1 q2 q3

Execution Example:Inputa a a b b b

currentstate

b, a

Time 0

, , $ $

Stack$

Page 17: Pushdown Automata PDAs

Fall 2004 COMP 335 17

a, a

b, a q0 q1 q2 q3

Inputa a a b b b

b, a

Time 1

, , $ $

Stack$

Page 18: Pushdown Automata PDAs

Fall 2004 COMP 335 18

a, a

b, a q0 q1 q2 q3

Input

Stack

a a a b b b$a

b, a

Time 2

, , $ $

Page 19: Pushdown Automata PDAs

Fall 2004 COMP 335 19

a, a

b, a q0 q1 q2 q3

Input

Stack

a a a b b b$aa

b, a

Time 3

, , $ $

Page 20: Pushdown Automata PDAs

Fall 2004 COMP 335 20

a, a

b, a q0 q1 q2 q3

Input

Stack

a a a b b b

$aaa

b, a

Time 4

, , $ $

Page 21: Pushdown Automata PDAs

Fall 2004 COMP 335 21

a, a

b, a q0 q1 q2 q3

Inputa a a b b b

Stack$aaa

b, a

Time 5

, , $ $

Page 22: Pushdown Automata PDAs

Fall 2004 COMP 335 22

a, a

b, a q0 q1 q2 q3

Inputa a a b b b

$a

Stack

b, a

Time 6

, , $ $

a

Page 23: Pushdown Automata PDAs

Fall 2004 COMP 335 23

a, a

b, a q0 q1 q2 q3

Inputa a a b b b

$Stack

b, a

Time 7

, , $ $

a

Page 24: Pushdown Automata PDAs

Fall 2004 COMP 335 24

a, a

b, a q0 q1 q2 q3

Inputa a a b b b

b, a

Time 8

accept, , $ $

$Stack

Page 25: Pushdown Automata PDAs

Fall 2004 COMP 335 25

A string is accepted if there is a computation such that:

All the input is consumed AND The last state is a final state

At the end of the computation,we do not care about the stack contents

Page 26: Pushdown Automata PDAs

Fall 2004 COMP 335 26

The input stringis accepted by the NPDA:

aaabbb

a, a

b, a q0 q1 q2 q3

b, a

, , $ $

Page 27: Pushdown Automata PDAs

Fall 2004 COMP 335 27

}0:{ nbaL nn

is the language accepted by the NPDA:

a, a

b, a q0 q1 q2 q3

b, a

In general,

, , $ $

Page 28: Pushdown Automata PDAs

Fall 2004 COMP 335 28

NPDA -- Another example

, $ $q1 q2

bbaa

,,

, q0

bbaa

,,

NPDA M

}{)( RwwML

Page 29: Pushdown Automata PDAs

Fall 2004 COMP 335 29

Execution Example:Input

Time 0

Stack$

, $ $q1 q2

bbaa

,,

, q0

bbaa

,,

a ab b

Page 30: Pushdown Automata PDAs

Fall 2004 COMP 335 30

Inputa ab

Time 1

Stack$

, $ $q1 q2

bbaa

,,

, q0

bbaa

,,

ab

Page 31: Pushdown Automata PDAs

Fall 2004 COMP 335 31

InputTime 2

Stack$

, $ $q1 q2

bbaa

,,

, q0

bbaa

,,

aa ab bb

Page 32: Pushdown Automata PDAs

Fall 2004 COMP 335 32

InputTime 3

Stack$

, $ $q1 q2

bbaa

,,

, q0

bbaa

,,

aa ab bb

Guess the middle of string

Page 33: Pushdown Automata PDAs

Fall 2004 COMP 335 33

InputTime 4

Stack$

, $ $q1 q2

bbaa

,,

, q0

bbaa

,,

aa ab bb

Page 34: Pushdown Automata PDAs

Fall 2004 COMP 335 34

InputTime 5

Stack$

, $ $1q q2

bbaa

,,

, q0

bbaa

,,

a ab b a

Page 35: Pushdown Automata PDAs

Fall 2004 COMP 335 35

InputTime 6

Stack$

, $ $q1

bbaa

,,

, q0

bbaa

,,

a ab b

acceptq2

Page 36: Pushdown Automata PDAs

Fall 2004 COMP 335 36

Rejection Example:Input

Time 0

Stack$

, $ $q1 q2

bbaa

,,

, q0

bbaa

,,

a b b b

Page 37: Pushdown Automata PDAs

Fall 2004 COMP 335 37

InputTime 1

Stack$

, $ $q1 q2

bbaa

,,

, q0

bbaa

,,

aa b b b

Page 38: Pushdown Automata PDAs

Fall 2004 COMP 335 38

InputTime 2

Stack$

, $ $q1 q2

bbaa

,,

, q0

bbaa

,,

ab

a b b b

Page 39: Pushdown Automata PDAs

Fall 2004 COMP 335 39

InputTime 3

Stack$

, $ $q1 q2

bbaa

,,

, q0

bbaa

,,

ab

Guess the middle of string

a b b b

Page 40: Pushdown Automata PDAs

Fall 2004 COMP 335 40

InputTime 4

Stack$

, $ $q1 q2

bbaa

,,

, q0

bbaa

,,

ab

a b b b

Page 41: Pushdown Automata PDAs

Fall 2004 COMP 335 41

InputTime 5

Stack$

, $ $1q q2

bbaa

,,

, q0

bbaa

,,

aa b b b

There is no possible transition.

Input is not consumed

Page 42: Pushdown Automata PDAs

Fall 2004 COMP 335 42

Another computation on the same string:Input Time 0

Stack$

, $ $q1 q2

bbaa

,,

, q0

bbaa

,,

a b b b

Page 43: Pushdown Automata PDAs

Fall 2004 COMP 335 43

InputTime 1

Stack$

, $ $q1 q2

bbaa

,,

, q0

bbaa

,,

aa b b b

Page 44: Pushdown Automata PDAs

Fall 2004 COMP 335 44

InputTime 2

Stack$

, $ $q1 q2

bbaa

,,

, q0

bbaa

,,

ab

a b b b

Page 45: Pushdown Automata PDAs

Fall 2004 COMP 335 45

InputTime 3

Stack$ab

a b b b

, $ $q1 q2

bbaa

,,

, q0

bbaa

,,

b

Page 46: Pushdown Automata PDAs

Fall 2004 COMP 335 46

InputTime 4

Stack

a b b b

, $ $q1 q2

bbaa

,,

, q0

bbaa

,,

$abbb

Page 47: Pushdown Automata PDAs

Fall 2004 COMP 335 47

InputTime 5

Stack

a b b b

$abbb

, $ $q1 q2

bbaa

,,

, q0

bbaa

,,

No final state is reached

Page 48: Pushdown Automata PDAs

Fall 2004 COMP 335 48

, $ $q1 q2

bbaa

,,

, q0

bbaa

,,

There is no computation that accepts string abbb

)(MLabbb

Page 49: Pushdown Automata PDAs

Fall 2004 COMP 335 49

A string is rejected if there is no computation such that:

All the input is consumed AND The last state is a final state

At the end of the computation,we do not care about the stack contents

Page 50: Pushdown Automata PDAs

Fall 2004 COMP 335 50

In other words, a string is rejected if in every computation with this string:

The input cannot be consumed OR

The input is consumed and the last state is not a final state

ORThe stack head moves below the bottom of the stack

Page 51: Pushdown Automata PDAs

Fall 2004 COMP 335 51

q0

aa ,

1q

ab,

$,bab,

NPDA – Another Example

$,b

}1:{)( mnbaML mn

}0,:{}1:{ 1 kmbamba mkmmm

Page 52: Pushdown Automata PDAs

Fall 2004 COMP 335 52

Pushing Strings

q1 q2a, b w

Inputsymbol

Popsymbol

Pushstring

Page 53: Pushdown Automata PDAs

Fall 2004 COMP 335 53

q1 q2a, b cdf

a

b top

input

stack

a

Pusheh h

e

cdf

pushedstring

Example:

$ $

Page 54: Pushdown Automata PDAs

Fall 2004 COMP 335 54

Another NPDA example

$$, q1 q2

a, $ 0$a, 0 00a,1

b, $ 1$b, 1 11b, 0

NPDA M})()(:{)( wnwnwML ba

Page 55: Pushdown Automata PDAs

Fall 2004 COMP 335 55

Time 0Inputa ab b b

currentstate

a

$Stack

$$, q1 q2

a, $ 0$a, 0 00a,1

b, $ 1$b, 1 11b, 0

Execution Example:

Page 56: Pushdown Automata PDAs

Fall 2004 COMP 335 56

Time 1Inputa ab b ba

$Stack

0

$$, q1 q2

a, $ 0$a, 0 00a,1

b, $ 1$b, 1 11b, 0

Page 57: Pushdown Automata PDAs

Fall 2004 COMP 335 57

Time 3Inputa bb b a

$Stack

a

$

$$, q1 q2

a, $ 0$a, 0 00a,1

b, $ 1$b, 1 11b, 0

0

Page 58: Pushdown Automata PDAs

Fall 2004 COMP 335 58

Time 4Inputa bb b a

$Stack

a1

$$, q1 q2

a, $ 0$a, 0 00a,1

b, $ 1$b, 1 11b, 0

Page 59: Pushdown Automata PDAs

Fall 2004 COMP 335 59

Time 5Inputa bb b a

$Stack

a11

$$, q1 q2

a, $ 0$a, 0 00a,1

b, $ 1$b, 1 11b, 0

Page 60: Pushdown Automata PDAs

Fall 2004 COMP 335 60

Time 6Inputa bb b a

$Stack

a1

$$, q1 q2

a, $ 0$a, 0 00a,1

b, $ 1$b, 1 11b, 0

1

Page 61: Pushdown Automata PDAs

Fall 2004 COMP 335 61

Time 7Inputa bb b a

$Stack

a1

$$, q1 q2

a, $ 0$a, 0 00a,1

b, $ 1$b, 1 11b, 0

Page 62: Pushdown Automata PDAs

Fall 2004 COMP 335 62

Time 8Inputa bb b a a

$Stack

$$, q1 q2

a, $ 0$a, 0 00a,1

b, $ 1$b, 1 11b, 0

accept

Page 63: Pushdown Automata PDAs

Fall 2004 COMP 335 63

Formalities for NPDAs

Page 64: Pushdown Automata PDAs

Fall 2004 COMP 335 64

q1 q2a, b w

)},{(),,( 21 wqbaq

Transition function:

Page 65: Pushdown Automata PDAs

Fall 2004 COMP 335 65

q1

q2wba ,

q3uba ,

)},(),,{(),,( 321 uqwqbaq

Transition function:

Page 66: Pushdown Automata PDAs

Fall 2004 COMP 335 66

Formal DefinitionNon-Deterministic Pushdown Automaton NPDA

),,,δ,Γ,Σ,( 0 FzqQM

States

Inputalphabet

Stackalphabet

Transitionfunction

Finalstates

Stackendsymbol

Initialstate

Page 67: Pushdown Automata PDAs

Fall 2004 COMP 335 67

Instantaneous Description (ID)

),,( uvq

Currentstate Remaining

unread input

Currentstackcontents

Page 68: Pushdown Automata PDAs

Fall 2004 COMP 335 68

aa /,

/,abq0 q1 q2 q3

Input

Stack

a a a b b b$aaTime 4:

/, $/$,

Example: Instantaneous Description (ID)$),,( 1 aaabbbq

a

/,ab

Page 69: Pushdown Automata PDAs

Fall 2004 COMP 335 69

a, a

b, a q0 q1 q2 q3

Input

Stack

a a a b b b$aa

b, a

Time 5:

, , $ $

Example: Instantaneous Description (ID)$),,( 2 aabbq

a

Page 70: Pushdown Automata PDAs

Fall 2004 COMP 335 70

We write:

Time 4 Time 5$),,($),,( 21 aabbqaaabbbq

Page 71: Pushdown Automata PDAs

Fall 2004 COMP 335 71

a, a

b, a q0 q1 q2 q3

b, a

, , $ $

,$),(,$),($),,($),,($),,($),,($),,(

,$),(,$),(

3222

111

10

qqabqaabbqaaabbbqaaabbbqaaabbbq

aaabbbqaaabbbq

A computation:

Page 72: Pushdown Automata PDAs

Fall 2004 COMP 335 72

,$),(,$),($),,($),,($),,($),,($),,(

,$),(,$),(

3222

111

10

qqabqaabbqaaabbbqaaabbbqaaabbbq

aaabbbqaaabbbq

For convenience we write:

,$),(,$),( 30 qaaabbbq

Page 73: Pushdown Automata PDAs

Fall 2004 COMP 335 73

Formal Definition

Language of NPDA :M

}),,,(),,(,:{)( **

0* uuqzwqwwML fM

Initial ID Final ID

)(ML

Page 74: Pushdown Automata PDAs

Fall 2004 COMP 335 74

Example:,$),(,$),( 30 qaaabbbq

a, a

b, a q0 q1 q2 q3

b, a

, , $ $

NPDA :M

)(MLaaabbb

Page 75: Pushdown Automata PDAs

Fall 2004 COMP 335 75

,$),(,$),( 30 qbaq nn

a, a

b, a q0 q1 q2 q3

b, a

, , $ $

NPDA :M

)(MLba nn

Page 76: Pushdown Automata PDAs

Fall 2004 COMP 335 76

a, a

b, a q0 q1 q2 q3

b, a

, , $ $

NPDA :M

}0:{)( nbaML nnTherefore:


Recommended