1 non-deterministic automata regular expressions

37
1 Non-Deterministic Automata Regular Expressions

Post on 21-Dec-2015

258 views

Category:

Documents


2 download

TRANSCRIPT

1

Non-Deterministic Automata

Regular Expressions

2

NFA Example

0q 1q 2q

0

11,0

*10

...,101010,1010,10,

L

3

NFA Example

0q 1q 2qa b

3q

ab

abababababL ...,,,

4

Formal Definition of NFAs FqQM ,,,, 0

:Q

:

:0q

:F

Set of states, i.e. 210 ,, qqq

: Input aplhabet, i.e. ba,Transition function

Initial state

Final states

5

Transition Function

0q 1q 2q

0

11,0

200

0

10

,,

0,

1,

qqq

q

qq

1,

,0,

2

201

q

qqq

6

Extended Transition Function

*

0q

5q4q

3q2q1qa

aa

b

0320

540

10

,,,*

,,*

,*

qqqabq

qqaaq

qaq

7

Formally

wqq ij ,*

It holds

if and only if

there is a walk from towith label

iq jqw

8

The Language of an NFA

0q

5q4q

3q2q1qa

aa

b

0320

540

,,,*

,,*

qqqabq

qqaaq

M

MLab

MLaa

)(

50 ,qqF

9

0q

5q4q

3q2q1qa

aa

b

50 ,qqF

10

540

,*

,,*

qabaq

qqabaaq

MLaba

MLaaba

)(

10

0q

5q4q

3q2q1qa

aa

b

aaababaaML *

11

FormallyThe languageaccepted by NFA is:

where

and there is some

)(MLM

,...,, 321 wwwML

,...,),(* 0 ji qqwq

Fqk (final state)

12

0q kq

w

w

w

),(* 0 wq MLw

Fqk

iq

jq

13

Equivalence of NFAs and DFAs

For DFAs or NFAs:

Machine is equivalent to

if

1M 2M

21 MLML

14

Example

0q 1q 2q

0

11,0

0q 1q 2q

0

11

0

1,0

NFA

DFA

*}10{1 ML

*}10{2 ML

15

Since

machines and are equivalent

*1021 MLML

1M 2M

0q 1q 2q

0

11,0

0q 1q 2q

0

11

0

1,0

DFA

NFA 1M

2M

16

Equivalence of NFAs and DFAs

Every DFA is also an NFA

A language accepted by a DFA will be accepted by an NFA

An NFA is as least as powerful as a DFA

Is an DFA as powerful as an NFA?

17

Is a DFA as Powerful as an NFA?

Answer: YES!

A language accepted by an NFA will be accepted by some DFA

For every NFA there is an equivalent DFAthat accepts the same language

18

NFAs Accept the Regular Languages

For every NFA there is an equivalent DFA

The language accepted by a DFA is regular

The language accepted by an NFA is regular

19

NFA to DFA

a

b

a

0q 1q 2q

NFA

DFA

0q

20

NFA to DFA

a

b

a

0q 1q 2q

NFA

DFA

0q 21,qqa

21

NFA to DFA

a

b

a

0q 1q 2q

NFA

DFA

0q 21,qqa

b

22

NFA to DFA

a

b

a

0q 1q 2q

NFA

DFA

0q 21,qqa

b

a

23

NFA to DFA

a

b

a

0q 1q 2q

NFA

DFA

0q 21,qqa

b

ab

24

NFA to DFA

a

b

a

0q 1q 2q

NFA

DFA

0q 21,qqa

b

ab

25

NFA to DFA

a

b

a

0q 1q 2q

NFA

DFA

0q 21,qqa

b

ab

ba,

26

NFA to DFA

a

b

a

0q 1q 2q

NFA

DFA

0q 21,qqa

b

ab

ba,

27

NFA to DFA Observations

We are given an NFA

We want to convert it to an equivalent DFA

With

M

M

)(MLML

28

If the NFA has states

The DFA has states in the powerset

,...,, 210 qqq

,....,,,,,,, 7432110 qqqqqqq

29

Procedure NFA to DFA

1. Initial state of NFA:

Initial state of DFA:

0q

0q

30

Example

a

b

a

0q 1q 2q

NFA

DFA

0q

31

Procedure NFA to DFA 2. For every DFA’s state

Compute in the NFA

Giving union

Add a transition

mji qqq ,...,,

...,,*

,,*

aq

aq

j

i

mji qqq ,...,,

mjimji qqqaqqq ,...,,,,...,,

32

Exampe

a

b

a

0q 1q 2q

NFA

0q 21,qqa

DFA

},{),(* 210 qqaq

210 ,, qqaq

33

Procedure NFA to DFA

Repeat Step 2 for all letters, untilno more transitions can be added.

34

Example

a

b

a

0q 1q 2q

NFA

DFA

0q 21,qqa

b

ab

ba,

35

Procedure NFA to DFA 3. For any DFA state

If some is a final state for the NFA

Then, is a final state for the DFA

mji qqq ,...,,

jq

mji qqq ,...,,

36

Example

a

b

a

0q 1q 2q

NFA

DFA

0q 21,qqa

b

ab

ba,

Fq 1

Fqq 21,

37

Theorem Take NFA M

Apply procedure to obtain DFA M

Then and are equivalent :M M

MLML