dva337 ht 17 lecture 6 equivalences for regular … nfa →dfa from m nfa = (q nfa, Σ nfa, δ nfa,...

81
DVA337 HT 17 – LECTURE 6 Equivalences for regular languages 1 1

Upload: buiminh

Post on 31-Mar-2018

229 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: DVA337 HT 17 LECTURE 6 Equivalences for regular … NFA →DFA From M NFA = (Q NFA, Σ NFA, δ NFA, q NFA, F NFA) construct DFA the alphabet of the DFA is the alphabet of the NFA the

DVA337 HT 17 – LECTURE 6 Equivalences for regular

languages

1

1

Page 2: DVA337 HT 17 LECTURE 6 Equivalences for regular … NFA →DFA From M NFA = (Q NFA, Σ NFA, δ NFA, q NFA, F NFA) construct DFA the alphabet of the DFA is the alphabet of the NFA the

RECAP OF LECTURE 5

DFA – MDFA = (Q,Σ,δ,q0,F) Q set of states

Σ alphabet

δ : Q ⨉ Σ → Q (total) transition function

q0 start state

F ⊆ Q set of final (accepting) states

L(MDFA) = { w ∈ Σ* | δ*(q0,w) ∈ F }

NFA – MNFA = (Q,Σ,δ,q0,F) δ : Q ⨉ Σ ∪ { λ } → P(Q) - ads non-determinism (λ transitions and sets successor states)

L(MNFA) = { w ∈ Σ* | δ*(q0,w) ∩ F ≠ ∅ }

Parallel processes/Back tracking

2

Page 3: DVA337 HT 17 LECTURE 6 Equivalences for regular … NFA →DFA From M NFA = (Q NFA, Σ NFA, δ NFA, q NFA, F NFA) construct DFA the alphabet of the DFA is the alphabet of the NFA the

EXERCISE, REVISITED

Create a DFA, M, over {0,1} such that all w ∈ L(M) are a multiple of 5 when interpreted as a binary number

1010 ∈ L(M), since 10102 = 1010 is a multiple of 5

1110 ∉ L(M), since 11102 = 1410

Correct? Assume x = d1...dn

x02 = x10 * 2

x12 = x10 * 2 + 1

3

≡ x x*2 x*2+1

0 k*5+0 k*10+0 ...+1

1 k*5+1 ...+2 ...+3

2 k*5+2 4 5 ≡ 0

3 k*5+3 6 ≡ 1 7 ≡ 2

4 k*5+4 8 ≡ 3 9 ≡ 4

Page 4: DVA337 HT 17 LECTURE 6 Equivalences for regular … NFA →DFA From M NFA = (Q NFA, Σ NFA, δ NFA, q NFA, F NFA) construct DFA the alphabet of the DFA is the alphabet of the NFA the

TODAY

DFA NFA RE

We will look at

DFA → NFA (trivial)

NFA → DFA (subset construction)

RE → NFA (Thompson’s construction)

NFA → RE (state elimination)

Those imply

DFA → RE (transitivity, DFA → NFA, NFA → RE)

RE → DFA (transitivity, RE → NFA, NFA → DFA)

4

Page 5: DVA337 HT 17 LECTURE 6 Equivalences for regular … NFA →DFA From M NFA = (Q NFA, Σ NFA, δ NFA, q NFA, F NFA) construct DFA the alphabet of the DFA is the alphabet of the NFA the

EXTENDED TRANSITION FUNCTION of NFAs

5

Page 6: DVA337 HT 17 LECTURE 6 Equivalences for regular … NFA →DFA From M NFA = (Q NFA, Σ NFA, δ NFA, q NFA, F NFA) construct DFA the alphabet of the DFA is the alphabet of the NFA the

Λ-CLOSURE

The λ-closure of a state q in and NFA is the set of states reachable with λ transitions from q

6

Page 7: DVA337 HT 17 LECTURE 6 Equivalences for regular … NFA →DFA From M NFA = (Q NFA, Σ NFA, δ NFA, q NFA, F NFA) construct DFA the alphabet of the DFA is the alphabet of the NFA the

EXTENDED TRANSITION FUNCTION

The extended transition function lifts the transition function to strings

δ : Q ⨉ Σ ∪ { λ } → P(Q)

δ* : Q ⨉ Σ* → P(Q)

There are two challenges

we have to work with sets

we have to work with λ transitions

7

Page 8: DVA337 HT 17 LECTURE 6 Equivalences for regular … NFA →DFA From M NFA = (Q NFA, Σ NFA, δ NFA, q NFA, F NFA) construct DFA the alphabet of the DFA is the alphabet of the NFA the

NFA DFA

8

Page 9: DVA337 HT 17 LECTURE 6 Equivalences for regular … NFA →DFA From M NFA = (Q NFA, Σ NFA, δ NFA, q NFA, F NFA) construct DFA the alphabet of the DFA is the alphabet of the NFA the

QUESTION?

Are NFAs and DFAs equivalent?

what does this statement mean?

9

Page 10: DVA337 HT 17 LECTURE 6 Equivalences for regular … NFA →DFA From M NFA = (Q NFA, Σ NFA, δ NFA, q NFA, F NFA) construct DFA the alphabet of the DFA is the alphabet of the NFA the

OBSERVATION

If

1) ∀MDFA ∈ DFA ∃MNFA ∈ NFA . L(MDFA) = L(MNFA) and

2) ∀MNFA ∈ NFA ∃MDFA ∈ DFA . L(MDFA) = L(MNFA) hold

We have that the set of all DFAs and the set of all NFAs define the same set of languages, i.e., { L(MDFA) : MDFA ∈ DFA } = { L(MNFA) : MNFA ∈ NFA }

10

Page 11: DVA337 HT 17 LECTURE 6 Equivalences for regular … NFA →DFA From M NFA = (Q NFA, Σ NFA, δ NFA, q NFA, F NFA) construct DFA the alphabet of the DFA is the alphabet of the NFA the

STEP 1

Theorem: ∀MDFA ∈ DFA ∃MNFA ∈ NFA . L(MDFA) = L(MNFA)

Proof:

11

Page 12: DVA337 HT 17 LECTURE 6 Equivalences for regular … NFA →DFA From M NFA = (Q NFA, Σ NFA, δ NFA, q NFA, F NFA) construct DFA the alphabet of the DFA is the alphabet of the NFA the

STEP 2

Theorem: ∀MNFA ∈ NFA ∃MDFA ∈ DFA . L(MDFA) = L(MNFA)

Proof:

12

Page 13: DVA337 HT 17 LECTURE 6 Equivalences for regular … NFA →DFA From M NFA = (Q NFA, Σ NFA, δ NFA, q NFA, F NFA) construct DFA the alphabet of the DFA is the alphabet of the NFA the

NFA → DFA Subset construction

13

Page 14: DVA337 HT 17 LECTURE 6 Equivalences for regular … NFA →DFA From M NFA = (Q NFA, Σ NFA, δ NFA, q NFA, F NFA) construct DFA the alphabet of the DFA is the alphabet of the NFA the

NFA → DFA

How can we construct a DFA, MDFA, from an MNFA such that L(MDFA) = L(MNFA)?

a string w corresponds to one state in a DFA when starting from the starting state

a string w corresponds to a set of states in a NFA when starting from the starting state

Idea; identify states in the NFA that are reachable from the same string

let those states form a state in the DFA

the states of the DFA are sets of states of the NFA

14

Page 15: DVA337 HT 17 LECTURE 6 Equivalences for regular … NFA →DFA From M NFA = (Q NFA, Σ NFA, δ NFA, q NFA, F NFA) construct DFA the alphabet of the DFA is the alphabet of the NFA the

EXAMPLE, NFA → DFA

Define the states in the DFA to collect the states in the NFA denoting the same string

15

0

1

a

3a,b

{0}start

{1,3}

a

{3}b

start

Page 16: DVA337 HT 17 LECTURE 6 Equivalences for regular … NFA →DFA From M NFA = (Q NFA, Σ NFA, δ NFA, q NFA, F NFA) construct DFA the alphabet of the DFA is the alphabet of the NFA the

FORMALLY, NFA → DFA

From MNFA = (QNFA, ΣNFA, δNFA, qNFA, FNFA) construct DFA

the alphabet of the DFA is the alphabet of the NFA

the states of the DFA are sets of states of the NFA (remember a node can be anything – also a set of nodes from another automaton)

MDFA = (QDFA ⊆ P(QNFA), ΣNFA, δDFA, qDFA, FDFA)

What should the start state qDFA be?

16

Page 17: DVA337 HT 17 LECTURE 6 Equivalences for regular … NFA →DFA From M NFA = (Q NFA, Σ NFA, δ NFA, q NFA, F NFA) construct DFA the alphabet of the DFA is the alphabet of the NFA the

SUBSET CONSTRUCTION, STEP 1

Step 1: create the start state

MDFA = ({{q0}} ⊆ P(QNFA), ΣNFA, { q0 }, ?, ? )

Is the start state qDFA always { qNFA }?

17

a

b

a

0q 1q 2q

0q

NFA

DFA

Page 18: DVA337 HT 17 LECTURE 6 Equivalences for regular … NFA →DFA From M NFA = (Q NFA, Σ NFA, δ NFA, q NFA, F NFA) construct DFA the alphabet of the DFA is the alphabet of the NFA the

SUBSET CONSTRUCTION, STEP 2

Step 2: build the transition function δDFA

iteratively

for each node still missing transitions, add transitions induced by δNFA

add new node to QDFA if needed

18

a

b

a

0q 1q 2q

NFA

DFA

0q

Page 19: DVA337 HT 17 LECTURE 6 Equivalences for regular … NFA →DFA From M NFA = (Q NFA, Σ NFA, δ NFA, q NFA, F NFA) construct DFA the alphabet of the DFA is the alphabet of the NFA the

SUBSET CONSTRUCTION. STEP 2

In general, for each node { qi, … ,qj } in the DFA

and each a ∈ ΣNFA

Add an edge for a to the node corresponding all nodes reachable in the NFA over a from any qk ∈ { qi, … ,qj }

19

Page 20: DVA337 HT 17 LECTURE 6 Equivalences for regular … NFA →DFA From M NFA = (Q NFA, Σ NFA, δ NFA, q NFA, F NFA) construct DFA the alphabet of the DFA is the alphabet of the NFA the

SUBSET CONSTRUCTION, STEP 2

Step 2: build the transition function δDFA

iteratively

for each node still missing transitions, add transitions induced by δNFA

δDFA({q0},b)?

20

a

b

a

0q 1q 2q

NFA

DFA

0q 21,qqa

Page 21: DVA337 HT 17 LECTURE 6 Equivalences for regular … NFA →DFA From M NFA = (Q NFA, Σ NFA, δ NFA, q NFA, F NFA) construct DFA the alphabet of the DFA is the alphabet of the NFA the

SUBSET CONSTRUCTION, STEP 2

Step 2: build the transition function δDFA

iteratively

for each node still missing transitions, add transitions induced by δNFA

δDFA({q1,q2},a)?

δDFA({q1,q2},b)?

21

a

b

a

0q 1q 2q

NFA

DFA

0q 21,qqa

b

Page 22: DVA337 HT 17 LECTURE 6 Equivalences for regular … NFA →DFA From M NFA = (Q NFA, Σ NFA, δ NFA, q NFA, F NFA) construct DFA the alphabet of the DFA is the alphabet of the NFA the

STEP 2

Final result after completion of step 2

22

a

b

a

0q 1q 2q

NFA

DFA

0q 21,qqa

b

ab

ba,

Page 23: DVA337 HT 17 LECTURE 6 Equivalences for regular … NFA →DFA From M NFA = (Q NFA, Σ NFA, δ NFA, q NFA, F NFA) construct DFA the alphabet of the DFA is the alphabet of the NFA the

SUBSET CONSTRUCTION, STEP 3

Step 3, add accepting states

23

a

b

a

0q 1q 2q

0q 21,qqa

b

ab

ba,

Page 24: DVA337 HT 17 LECTURE 6 Equivalences for regular … NFA →DFA From M NFA = (Q NFA, Σ NFA, δ NFA, q NFA, F NFA) construct DFA the alphabet of the DFA is the alphabet of the NFA the

SUBSET CONSTRUCTION, STEP 3

Step 3, add accepting states

Have

MNFA = (QNFA, ΣNFA, δNFA, q0, FNFA) construct DFA

MDFA = (QDFA ⊆ P(QNFA), ΣNFA, δDFA, q0, FDFA )

FDFA = { Q : Q ∈ QDFA, Q ∩ FNFA ≠ ∅ }

24

a

b

a

0q 1q 2q

NFA

DFA

0q 21,qqa

b

ab

ba,

Page 25: DVA337 HT 17 LECTURE 6 Equivalences for regular … NFA →DFA From M NFA = (Q NFA, Σ NFA, δ NFA, q NFA, F NFA) construct DFA the alphabet of the DFA is the alphabet of the NFA the

EXERCISE, SUBSET CONSTRUCTION

Convert this NFA to a DFA

Step 1: convert start state

Step 2: add induced transitions

add new nodes as needed

until no new transitions can be added

Step 3: compute accepting states

25

a

b

b

b

a

a

b

Page 26: DVA337 HT 17 LECTURE 6 Equivalences for regular … NFA →DFA From M NFA = (Q NFA, Σ NFA, δ NFA, q NFA, F NFA) construct DFA the alphabet of the DFA is the alphabet of the NFA the

EXERCISE, SUBSET CONSTRUCTION

26

a

b

b

b

a

a

b

Page 27: DVA337 HT 17 LECTURE 6 Equivalences for regular … NFA →DFA From M NFA = (Q NFA, Σ NFA, δ NFA, q NFA, F NFA) construct DFA the alphabet of the DFA is the alphabet of the NFA the

CORRECTNESS

We have described a way to translate an NFA (MNFA) to a DFA (MDFA)

subset consdtruction

How can we know that the construction is correct?

show that L(MNFA) = L(MDFA)

da

27

Page 28: DVA337 HT 17 LECTURE 6 Equivalences for regular … NFA →DFA From M NFA = (Q NFA, Σ NFA, δ NFA, q NFA, F NFA) construct DFA the alphabet of the DFA is the alphabet of the NFA the

DFA VS. NFA

Space and time taken to recognize regular expressions:

NFAs are more compact but take time to backtrack all choices

DFAs take more space, but save time

28

AUTOMATON SPACE TIME

NFA

DFA

O(|Q|)

O(2|Q|)

O(|Q||w|)

O(|w|)

Page 29: DVA337 HT 17 LECTURE 6 Equivalences for regular … NFA →DFA From M NFA = (Q NFA, Σ NFA, δ NFA, q NFA, F NFA) construct DFA the alphabet of the DFA is the alphabet of the NFA the

REGULAR LANGUAGES SO FAR

29

DFA NFA

subset construction

trivially

(subset construction) nfa-to-dfa

Page 30: DVA337 HT 17 LECTURE 6 Equivalences for regular … NFA →DFA From M NFA = (Q NFA, Σ NFA, δ NFA, q NFA, F NFA) construct DFA the alphabet of the DFA is the alphabet of the NFA the

CLOSURE PROPERTIES OF REGULAR LANGUAGES

30

Page 31: DVA337 HT 17 LECTURE 6 Equivalences for regular … NFA →DFA From M NFA = (Q NFA, Σ NFA, δ NFA, q NFA, F NFA) construct DFA the alphabet of the DFA is the alphabet of the NFA the

CLOSURE UNDER SET OPERATIONS

For regular languages L, L1 and L2 we will show that

L1 ∪ L2 is regular

L1L2 is regular

L* is regular

We say that regular languages are closed under union, concatenation, and repetition.

How do we prove this?

31

Page 32: DVA337 HT 17 LECTURE 6 Equivalences for regular … NFA →DFA From M NFA = (Q NFA, Σ NFA, δ NFA, q NFA, F NFA) construct DFA the alphabet of the DFA is the alphabet of the NFA the

SIMPLIFICATION

L, L1 and L2 are regular – there are NFAs M, M1, and M2 that accept the languages

Assume that those NFAs have unique isolated entry and exit points

why is this ok?

32

Page 33: DVA337 HT 17 LECTURE 6 Equivalences for regular … NFA →DFA From M NFA = (Q NFA, Σ NFA, δ NFA, q NFA, F NFA) construct DFA the alphabet of the DFA is the alphabet of the NFA the

SIMPLIFICATION

Not a unique isolated entry point

add one and connect with λ transition

Not a unique isolated accept point

add one and connect all previous ones with λtransitions

33

Page 34: DVA337 HT 17 LECTURE 6 Equivalences for regular … NFA →DFA From M NFA = (Q NFA, Σ NFA, δ NFA, q NFA, F NFA) construct DFA the alphabet of the DFA is the alphabet of the NFA the

CLOSURE UNDER UNION

Create NFA for L1 ∪ L2

34

1M

2M

Page 35: DVA337 HT 17 LECTURE 6 Equivalences for regular … NFA →DFA From M NFA = (Q NFA, Σ NFA, δ NFA, q NFA, F NFA) construct DFA the alphabet of the DFA is the alphabet of the NFA the

CLOSURE UNDER CONCATENATION

Create NFA for L1L2

35

1M 2M

Page 36: DVA337 HT 17 LECTURE 6 Equivalences for regular … NFA →DFA From M NFA = (Q NFA, Σ NFA, δ NFA, q NFA, F NFA) construct DFA the alphabet of the DFA is the alphabet of the NFA the

CLOSURE UNDER REPETITION

Create NFA for L*

36

Page 37: DVA337 HT 17 LECTURE 6 Equivalences for regular … NFA →DFA From M NFA = (Q NFA, Σ NFA, δ NFA, q NFA, F NFA) construct DFA the alphabet of the DFA is the alphabet of the NFA the

DONE?

Did we prove closure under union, concatenation and repetition?

37

Page 38: DVA337 HT 17 LECTURE 6 Equivalences for regular … NFA →DFA From M NFA = (Q NFA, Σ NFA, δ NFA, q NFA, F NFA) construct DFA the alphabet of the DFA is the alphabet of the NFA the

EXERCISE, CLOSURE UNDER COMPLEMENT

Are regular languages closed under complement, i.e., is L∁ a regular language given that L is regular?

38

Page 39: DVA337 HT 17 LECTURE 6 Equivalences for regular … NFA →DFA From M NFA = (Q NFA, Σ NFA, δ NFA, q NFA, F NFA) construct DFA the alphabet of the DFA is the alphabet of the NFA the

EXERCISE, CLOSURE UNDER INTERSECTION

If L1 and L2 are regular languages, is L1 ∩ L2 a regular language?

39

Page 40: DVA337 HT 17 LECTURE 6 Equivalences for regular … NFA →DFA From M NFA = (Q NFA, Σ NFA, δ NFA, q NFA, F NFA) construct DFA the alphabet of the DFA is the alphabet of the NFA the

CLOSURE UNDER SET OPERATIONS

Regular languages are closed under

Union, L1 ∪ L2

Intersection, L1 ∩ L2

Concatenation, L1L2

Repetition, L*

Complement, L∁

40

Page 41: DVA337 HT 17 LECTURE 6 Equivalences for regular … NFA →DFA From M NFA = (Q NFA, Σ NFA, δ NFA, q NFA, F NFA) construct DFA the alphabet of the DFA is the alphabet of the NFA the

REGULAR EXPRESSION DEFINED REGULAR LANGUAGES

41

Page 42: DVA337 HT 17 LECTURE 6 Equivalences for regular … NFA →DFA From M NFA = (Q NFA, Σ NFA, δ NFA, q NFA, F NFA) construct DFA the alphabet of the DFA is the alphabet of the NFA the

QUESTION

Theorem: regular expressions (RE) describe exactly regular languages.

42

Page 43: DVA337 HT 17 LECTURE 6 Equivalences for regular … NFA →DFA From M NFA = (Q NFA, Σ NFA, δ NFA, q NFA, F NFA) construct DFA the alphabet of the DFA is the alphabet of the NFA the

RE → NFA Thompson’s construction (1968)

43

Page 44: DVA337 HT 17 LECTURE 6 Equivalences for regular … NFA →DFA From M NFA = (Q NFA, Σ NFA, δ NFA, q NFA, F NFA) construct DFA the alphabet of the DFA is the alphabet of the NFA the

RE → NFA

Create NFA from RE

Primitive regular expressions

λ

a ∈ Σ

44

Page 45: DVA337 HT 17 LECTURE 6 Equivalences for regular … NFA →DFA From M NFA = (Q NFA, Σ NFA, δ NFA, q NFA, F NFA) construct DFA the alphabet of the DFA is the alphabet of the NFA the

RE → NFA

Create NFA from RE

Compound regular expressions

r1 + r2

r1r2

r*

45

Page 46: DVA337 HT 17 LECTURE 6 Equivalences for regular … NFA →DFA From M NFA = (Q NFA, Σ NFA, δ NFA, q NFA, F NFA) construct DFA the alphabet of the DFA is the alphabet of the NFA the

EXAMPLE: FROM A RE TO AN NFA

46

Example : (a+b)*abb step by step construction

2

a

4 5

b

6

1

3

(a+b)

Page 47: DVA337 HT 17 LECTURE 6 Equivalences for regular … NFA →DFA From M NFA = (Q NFA, Σ NFA, δ NFA, q NFA, F NFA) construct DFA the alphabet of the DFA is the alphabet of the NFA the

EXAMPLE: FROM A RE TO AN NFA

47

Example : (a+b)*abb

2

a

4 5b

1

6

3

0 7

(a+b)*

Page 48: DVA337 HT 17 LECTURE 6 Equivalences for regular … NFA →DFA From M NFA = (Q NFA, Σ NFA, δ NFA, q NFA, F NFA) construct DFA the alphabet of the DFA is the alphabet of the NFA the

EXAMPLE: FROM A RE TO AN NFA

48

Example : (a+b)*abb

2a

4 5b

10

10

a

b

b

76

3

8

9

(a+b)*abb

Page 49: DVA337 HT 17 LECTURE 6 Equivalences for regular … NFA →DFA From M NFA = (Q NFA, Σ NFA, δ NFA, q NFA, F NFA) construct DFA the alphabet of the DFA is the alphabet of the NFA the

REGULAR LANGUAGES SO FAR

49

DFA NFA

RE

subset construction

trivially

Thompson’s construction

(subset construction) nfa-to-dfa

(Thompson) re-to-nfa

implied:

re-to-dfa(x) = nfa-to-dfa(re-to-nfa(x))

Page 50: DVA337 HT 17 LECTURE 6 Equivalences for regular … NFA →DFA From M NFA = (Q NFA, Σ NFA, δ NFA, q NFA, F NFA) construct DFA the alphabet of the DFA is the alphabet of the NFA the

EXERCISE

Construct an NFA which is equivalent to (00 + (1 + 01)(11+0)*10)*

50

Page 51: DVA337 HT 17 LECTURE 6 Equivalences for regular … NFA →DFA From M NFA = (Q NFA, Σ NFA, δ NFA, q NFA, F NFA) construct DFA the alphabet of the DFA is the alphabet of the NFA the

NFA → RE

51

Page 52: DVA337 HT 17 LECTURE 6 Equivalences for regular … NFA →DFA From M NFA = (Q NFA, Σ NFA, δ NFA, q NFA, F NFA) construct DFA the alphabet of the DFA is the alphabet of the NFA the

GENERALIZED NOTION OF NFA

Allow transitions to be regular expressions

52

Page 53: DVA337 HT 17 LECTURE 6 Equivalences for regular … NFA →DFA From M NFA = (Q NFA, Σ NFA, δ NFA, q NFA, F NFA) construct DFA the alphabet of the DFA is the alphabet of the NFA the

LANGUAGE ACCEPTED BY A GENERALIZED NFA

A GNFA (Q,Σ,δ,q0,F) accepts a string w if

w = w1w2…wk and

exists a path of states q0,q1,q2, …, qk from Q such that

wi∈ L(ri) for ri = δ(qi-1,qi)

53

Page 54: DVA337 HT 17 LECTURE 6 Equivalences for regular … NFA →DFA From M NFA = (Q NFA, Σ NFA, δ NFA, q NFA, F NFA) construct DFA the alphabet of the DFA is the alphabet of the NFA the

SIMPLIFICATION

To begin with, the automaton should have a start state that has no transitions into it (including self-loops), and which is not accepting.

The automaton should also have a single accepting final state with no transitions leaving it, and no self-loops.

Same assumptions as for Thompson’s construction

54

Page 55: DVA337 HT 17 LECTURE 6 Equivalences for regular … NFA →DFA From M NFA = (Q NFA, Σ NFA, δ NFA, q NFA, F NFA) construct DFA the alphabet of the DFA is the alphabet of the NFA the

ELIMINATE

Repeat the following steps, which eliminate a state:

1. Pick a non-start, non-accepting state q to eliminate.

1. The state q will have i transitions in and j transitions out. Each will be labeled with a regular expression.

2. For each of the ij combinations of transitions into and out of q, replace:

55

A

B

p q

Cr

with

CBA *p r

and delete state q.

Page 56: DVA337 HT 17 LECTURE 6 Equivalences for regular … NFA →DFA From M NFA = (Q NFA, Σ NFA, δ NFA, q NFA, F NFA) construct DFA the alphabet of the DFA is the alphabet of the NFA the

SIMPLIFY

2. If several transitions go between a pair of states, replace them with a single transition that is the union of the individual transitions.

E.g. replace:

56

A

B

p r

with

p rBA

Page 57: DVA337 HT 17 LECTURE 6 Equivalences for regular … NFA →DFA From M NFA = (Q NFA, Σ NFA, δ NFA, q NFA, F NFA) construct DFA the alphabet of the DFA is the alphabet of the NFA the

IN GENERAL

Repeat the following steps, which eliminate a state:

1. Pick a non-start, non-accepting state q to eliminate.

1. The state q will have i transitions in and j transitions out. Each will be labeled with a regular expression.

2. For each of the ij combinations of transitions into and out of q, replace:

57

Page 58: DVA337 HT 17 LECTURE 6 Equivalences for regular … NFA →DFA From M NFA = (Q NFA, Σ NFA, δ NFA, q NFA, F NFA) construct DFA the alphabet of the DFA is the alphabet of the NFA the

EXAMPLE

58

b

a

a

b1

3 4a2

b

abab *

a

b1

3 4

baabab *)*( 1

4

Page 59: DVA337 HT 17 LECTURE 6 Equivalences for regular … NFA →DFA From M NFA = (Q NFA, Σ NFA, δ NFA, q NFA, F NFA) construct DFA the alphabet of the DFA is the alphabet of the NFA the

NOTE, COMMON MISTAKE

59

*)( ba

ba,

a b means

i.e.

NOT *)*( ba

Page 60: DVA337 HT 17 LECTURE 6 Equivalences for regular … NFA →DFA From M NFA = (Q NFA, Σ NFA, δ NFA, q NFA, F NFA) construct DFA the alphabet of the DFA is the alphabet of the NFA the

EXAMPLE

60

Add a new start (s) and a new final (f) state:

• From s to the ex-starting state (1)

• From each ex-final state (1,3) to f

ba ,

a

b

b

1 2

3

a

Page 61: DVA337 HT 17 LECTURE 6 Equivalences for regular … NFA →DFA From M NFA = (Q NFA, Σ NFA, δ NFA, q NFA, F NFA) construct DFA the alphabet of the DFA is the alphabet of the NFA the

61

ba ,

a

b

b

1 2

3

a

s

f

Let’s remove state 1!

Each combination of input/output to 1 will generate

a new path once state 1 is gone

; ba , ; ba a; , a ;

The original:

ba ,

a

b

b

1 2

3

a

ba ,

a

b

b

1 2

3

a

Page 62: DVA337 HT 17 LECTURE 6 Equivalences for regular … NFA →DFA From M NFA = (Q NFA, Σ NFA, δ NFA, q NFA, F NFA) construct DFA the alphabet of the DFA is the alphabet of the NFA the

62

When state 1 is gone we must be able to make all those transitions!

ba ,

ab

b

1 2

3

a

s

f

ba( ); ba ,

ba,a

b

ba

Previous:

Page 63: DVA337 HT 17 LECTURE 6 Equivalences for regular … NFA →DFA From M NFA = (Q NFA, Σ NFA, δ NFA, q NFA, F NFA) construct DFA the alphabet of the DFA is the alphabet of the NFA the

63

;

ba ,

ab

b

1 2

3

a

s

f

ba( )

Page 64: DVA337 HT 17 LECTURE 6 Equivalences for regular … NFA →DFA From M NFA = (Q NFA, Σ NFA, δ NFA, q NFA, F NFA) construct DFA the alphabet of the DFA is the alphabet of the NFA the

64

ba ,

ab

b

1 2

3

a

s

f

ba a; ,

ba( )a

ba( )

Page 65: DVA337 HT 17 LECTURE 6 Equivalences for regular … NFA →DFA From M NFA = (Q NFA, Σ NFA, δ NFA, q NFA, F NFA) construct DFA the alphabet of the DFA is the alphabet of the NFA the

65

A common mistake: having several arrows between the same pair of states. Join the

two arrows (by union of their regular expressions) before going on to the next step.

ba ,

ab

1 2

3

a

s

f

ba( )

bba( )a

b

Page 66: DVA337 HT 17 LECTURE 6 Equivalences for regular … NFA →DFA From M NFA = (Q NFA, Σ NFA, δ NFA, q NFA, F NFA) construct DFA the alphabet of the DFA is the alphabet of the NFA the

66

ba ,

ab

1 2

3

a

s

f

a ; ba( )

a

bba( )a

Page 67: DVA337 HT 17 LECTURE 6 Equivalences for regular … NFA →DFA From M NFA = (Q NFA, Σ NFA, δ NFA, q NFA, F NFA) construct DFA the alphabet of the DFA is the alphabet of the NFA the

67

Union again..

ba ,

a

b

1 2

3

a

s

f

ba( )

a

bba( )a

Page 68: DVA337 HT 17 LECTURE 6 Equivalences for regular … NFA →DFA From M NFA = (Q NFA, Σ NFA, δ NFA, q NFA, F NFA) construct DFA the alphabet of the DFA is the alphabet of the NFA the

68

ab

2

3

s

f

ba( )

a

bba( )a

Without state 1...

Page 69: DVA337 HT 17 LECTURE 6 Equivalences for regular … NFA →DFA From M NFA = (Q NFA, Σ NFA, δ NFA, q NFA, F NFA) construct DFA the alphabet of the DFA is the alphabet of the NFA the

69

ab

2

3

s

f

ba( )

a

ba(a

Now we repeat the same procedure for the state 2...

b

Page 70: DVA337 HT 17 LECTURE 6 Equivalences for regular … NFA →DFA From M NFA = (Q NFA, Σ NFA, δ NFA, q NFA, F NFA) construct DFA the alphabet of the DFA is the alphabet of the NFA the

70

Following the path s-2-3 we concatenate all strings on our way…don’t forget

a* in 2!

a

b

2

3

s

f

ba( )

a

bba( )a

bba( ) a*

Page 71: DVA337 HT 17 LECTURE 6 Equivalences for regular … NFA →DFA From M NFA = (Q NFA, Σ NFA, δ NFA, q NFA, F NFA) construct DFA the alphabet of the DFA is the alphabet of the NFA the

71

When 2 is removed, the path 3 - 2 –3 has also to be preserved, so we concatenate 3-

2 string, take a* loop and go back 2-3 with string b!

ab

2

3

s

f

ba( )

bba(a

a

bba( ) a*

( bba( )a ba*)

Page 72: DVA337 HT 17 LECTURE 6 Equivalences for regular … NFA →DFA From M NFA = (Q NFA, Σ NFA, δ NFA, q NFA, F NFA) construct DFA the alphabet of the DFA is the alphabet of the NFA the

72

3

s

f

This is how the FA looks like without state 2:

a

bba( ) a*

( bba( )a ba*)

Page 73: DVA337 HT 17 LECTURE 6 Equivalences for regular … NFA →DFA From M NFA = (Q NFA, Σ NFA, δ NFA, q NFA, F NFA) construct DFA the alphabet of the DFA is the alphabet of the NFA the

73

3

s

f

Finally we remove state 3…

a

bba( ) a*

( bba( )a ba*)

Page 74: DVA337 HT 17 LECTURE 6 Equivalences for regular … NFA →DFA From M NFA = (Q NFA, Σ NFA, δ NFA, q NFA, F NFA) construct DFA the alphabet of the DFA is the alphabet of the NFA the

74

3

s

f

...so we concatenate strings s-3, loop in 3 and 3-f

** )))((( babbaa

babbaa * ))((

baba *)(

a

baba * )( )( a

Page 75: DVA337 HT 17 LECTURE 6 Equivalences for regular … NFA →DFA From M NFA = (Q NFA, Σ NFA, δ NFA, q NFA, F NFA) construct DFA the alphabet of the DFA is the alphabet of the NFA the

75

Now we can omit state 3!

s

f

From s we have two choices, empty string or the long expression

OR is represented by union , as usually

** )))((( babbaababa * )( )( a

Page 76: DVA337 HT 17 LECTURE 6 Equivalences for regular … NFA →DFA From M NFA = (Q NFA, Σ NFA, δ NFA, q NFA, F NFA) construct DFA the alphabet of the DFA is the alphabet of the NFA the

76

So union the arrows...

s f

...and we are done!

** )))((( babbaababa * )( )( a

Page 77: DVA337 HT 17 LECTURE 6 Equivalences for regular … NFA →DFA From M NFA = (Q NFA, Σ NFA, δ NFA, q NFA, F NFA) construct DFA the alphabet of the DFA is the alphabet of the NFA the

EXERCISE

Construct a regular expression for the following automaton

77

Page 78: DVA337 HT 17 LECTURE 6 Equivalences for regular … NFA →DFA From M NFA = (Q NFA, Σ NFA, δ NFA, q NFA, F NFA) construct DFA the alphabet of the DFA is the alphabet of the NFA the

EXERCISE

Construct a regular expression for the following automaton

78

Page 79: DVA337 HT 17 LECTURE 6 Equivalences for regular … NFA →DFA From M NFA = (Q NFA, Σ NFA, δ NFA, q NFA, F NFA) construct DFA the alphabet of the DFA is the alphabet of the NFA the

EXERCISE

Construct a regular expression for the following automaton

79

Page 80: DVA337 HT 17 LECTURE 6 Equivalences for regular … NFA →DFA From M NFA = (Q NFA, Σ NFA, δ NFA, q NFA, F NFA) construct DFA the alphabet of the DFA is the alphabet of the NFA the

SUMMARY

80

DFA NFA

RE

subset construction

trivially

state elimination

Thompson’s construction

(subset construction) nfa-to-dfa

(state elimination) nfa-to-re

(Thompson) re-to-nfa

implied:

dfa-to-re(x) = nfa-to-re(dfa)

re-to-dfa(x) = nfa-to-dfa(re-to-nfa(x))

Page 81: DVA337 HT 17 LECTURE 6 Equivalences for regular … NFA →DFA From M NFA = (Q NFA, Σ NFA, δ NFA, q NFA, F NFA) construct DFA the alphabet of the DFA is the alphabet of the NFA the

HOW TO TELL IF A LANGUAGE L IS REGULAR?

It can be described by a regular expression.

It can be described by a NFA.

It can be described by a DFA.

81