You are on page 1of 81

DVA337 HT 17 – LECTURE 6 Equivalences for regular

languages

1
1
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
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
≡ x x*2 x*2+1
Correct? Assume x = d1...dn 0 k*5+0 k*10+0 ...+1
 x02 = x10 * 2
1 k*5+1 ...+2 ...+3
 x12 = x10 * 2 + 1
2 k*5+2 4 5≡0
3 k*5+3 6≡1 7≡2
4 k*5+4 8≡3 9≡4

3
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
EXTENDED TRANSITION FUNCTION of NFAs

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

6
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
NFA  DFA

8
QUESTION?
Are NFAs and DFAs equivalent?
 what does this statement mean?

9
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
STEP 1
Theorem: ∀MDFA ∈ DFA ∃MNFA ∈ NFA . L(MDFA) = L(MNFA)
Proof:

11
STEP 2
Theorem: ∀MNFA ∈ NFA ∃MDFA ∈ DFA . L(MDFA) = L(MNFA)
Proof:

12
NFA → DFA Subset construction

13
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
EXAMPLE, NFA → DFA
Define the states in the DFA to collect the states in the NFA denoting the same string

{1,3}
1
a
a
start
start {0}
0

b
a,b {3}
3

15
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
SUBSET CONSTRUCTION, STEP 1
Step 1: create the start state a NFA
q0 a q1  q2
b
MDFA = ({{q0}} ⊆ P(QNFA), ΣNFA, { q0 }, ?, ? )
DFA
Is the start state qDFA always { qNFA }?
q0 

17
SUBSET CONSTRUCTION, STEP 2
Step 2: build the transition function δDFA a NFA
iteratively
 for each node still missing transitions, add
transitions induced by δNFA q0 a q1  q2
 add new node to QDFA if needed
b
DFA

q0 
18
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
a
SUBSET CONSTRUCTION, STEP 2 NFA
q0 a q1  q2
Step 2: build the transition function δDFA
iteratively
b
 for each node still missing transitions, add
transitions induced by δNFA
DFA

δDFA({q0},b)?
q0  a
q1, q2 

20
a NFA
SUBSET CONSTRUCTION, STEP 2
q0 a q1  q2
Step 2: build the transition function δDFA b
iteratively
 for each node still missing transitions, add
transitions induced by δNFA DFA

δDFA({q1,q2},a)? q0  a
q1, q2 
b
δDFA({q1,q2},b)?


21
NFA a
STEP 2 q0 a 
q1 q2
Final result after completion of step 2 b

DFA b a

q0  a
q1, q2 
b

 a, b
22
SUBSET CONSTRUCTION, STEP 3 a
q0 a q1  q2
Step 3, add accepting states
b
b a

q0  a
q1, q2 
b

 a, b
23
a
SUBSET CONSTRUCTION, STEP 3 NFA

q0 a q1  q2
Step 3, add accepting states
b
Have
 MNFA = (QNFA, ΣNFA, δNFA, q0, FNFA) construct DFA
a
 MDFA = (QDFA ⊆ P(QNFA), ΣNFA, δDFA, q0, FDFA ) DFA b

q0  a
q1, q2 
FDFA = { Q : Q ∈ QDFA, Q ∩ FNFA ≠ ∅ }
b

 a, b
24
EXERCISE, SUBSET CONSTRUCTION
a a
Convert this NFA to a DFA

Step 1: convert start state
Step 2: add induced transitions
b b
 add new nodes as needed
b
 until no new transitions can be added a
Step 3: compute accepting states

b
25
EXERCISE, SUBSET CONSTRUCTION
a a

b b
b
a

b
26
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
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

AUTOMATON SPACE TIME


NFA O(|Q|) O(|Q||w|)
DFA O(2|Q|) O(|w|)

28
REGULAR LANGUAGES SO FAR

trivially

subset construction

DFA NFA

(subset construction) nfa-to-dfa


29
CLOSURE PROPERTIES OF REGULAR
LANGUAGES
30
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
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
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
CLOSURE UNDER UNION
M1
Create NFA for L1 ∪ L2

M2

34
CLOSURE UNDER CONCATENATION
Create NFA for L1L2

M1 M2

35
CLOSURE UNDER REPETITION
Create NFA for L*

36
DONE?
Did we prove closure under union, concatenation and repetition?

37
EXERCISE, CLOSURE UNDER COMPLEMENT
Are regular languages closed under complement, i.e., is L∁ a regular language given
that L is regular?

38
EXERCISE, CLOSURE UNDER INTERSECTION
If L1 and L2 are regular languages, is L1 ∩ L2 a regular language?

39
CLOSURE UNDER SET OPERATIONS
Regular languages are closed under

Union, L1 ∪ L2
Intersection, L1 ∩ L2
Concatenation, L1L2
Repetition, L*
Complement, L∁

40
REGULAR EXPRESSION DEFINED
REGULAR LANGUAGES
41
QUESTION
Theorem: regular expressions (RE) describe exactly regular languages.

42
RE → NFA Thompson’s construction (1968)

43
RE → NFA
Create NFA from RE

Primitive regular expressions



λ
a∈Σ

44
RE → NFA
Create NFA from RE

Compound regular expressions


r1 + r2
r1r2
r*

45
EXAMPLE: FROM A RE TO AN NFA
Example : (a+b)*abb step by step construction

(a+b)

a
 2 3

1 6

 
4 5
b

46
EXAMPLE: FROM A RE TO AN NFA
Example : (a+b)*abb (a+b)*

a
2 3 



0 1 6 7

4 5 
b

47
EXAMPLE: FROM A RE TO AN NFA
 (a+b)*abb
Example : (a+b)*abb

a
2 3 


0 1 6  a
7 8
 
4 b 5 b

 b

10
48
REGULAR LANGUAGES SO FAR
trivially

subset construction

DFA NFA

RE Thompson’s construction

(subset construction) nfa-to-dfa implied:


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

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

50
NFA → RE
51
GENERALIZED NOTION OF NFA
Allow transitions to be regular expressions

52
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
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
ELIMINATE
B
Repeat the following steps, which
eliminate a state: A C
p q r
1. Pick a non-start, non-accepting
state q to eliminate.
1. The state q will have i transitions in and with
j transitions out. Each will be labeled
with a regular expression. AB * C
p r
2. For each of the ij combinations of
transitions into and out of q, replace:

and delete state q.

55
SIMPLIFY
A
2. If several transitions go between a
pair of states, replace them with a single p r
transition that is the union of the
individual transitions.
B
with
E.g. replace:

p A B r

56
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
b a
EXAMPLE 1
a 2 a 3 b 4

b
a
1
b  ab*a 3 b 4

1
(b  a b * a)a * b 4

58
NOTE, COMMON MISTAKE

a b means ( a  b) *
i.e. a, b

NOT (a *  b*)

59
EXAMPLE
a
a,b
1 2

b
a
b
3

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

60
a
 a,b
s 1 2

b
a
The original: b
 3
a
a,b
1 2
b
a
3
b
f 
Let’s remove state 1!
Each combination of input/output to 1 will generate
a new path once state 1 is gone

 ; a,b  ; a ; a, b a;
61
When state 1 is gone we must be able to make all those transitions!

 a b ; ,  ( a b )

 a,b
s 1 2 a
b
a
Previous: b
 a, b
a  3


a
 b
f


62
 ;

 ( a  b)

 a,b
s 1 2 a
b
a
b
  3

f 
63
a ; a, b

 ( a  b)

 a,b
s 1 2 a
b
a
 b
 3
a ( a  b)
f 
64
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.

 ( a  b)

 a,b
s 1 2 a
b
a b
  3 a ( a  b)  b

f 
65
a;
 ( a  b)

 a,b
s 1 2 a
b
a
  3
a ( a  b)  b

f 
a
66
Union again..

 ( a  b)

 a,b
s 1 2 a
b
a a ( a  b)  b
  3

f a

67
Without state 1...

 ( a  b)

s 2 a
b
a ( a  b)  b
 3

f a

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

 ( a  b)

s 2 a
b
a(a  b  b
 3

f a

69
Following the path s-2-3 we concatenate all strings on our way…don’t forget
a* in 2!

 ( a  b)

s 2 a
*
 a bab
(  )
b
a ( a  b)  b
 3

f a
70
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!

 ( a  b)

s 2 a
* b
 ( a b) a b

a( a b  b
 3
(
a ( a  b)  b ) a*b
f
a
71
This is how the FA looks like without state 2:

s
*
 a bab
(  )

 3
(
a ( a  b)  b ) a*b
f a

72
Finally we remove state 3…

s
*
 a bab
(  )


a
3

f
(
a ( a  b)  b ) a*b

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

s
 ( a  b)a * b

 3
*
( a ( a  b)  b) a b
f  a

 (a  b)a *b (( a (a  b)  b)a *b)* (  a )

74
Now we can omit state 3!

 (a  b)a*b ((a(a  b)  b)a*b)* (  a )


From s we have two choices, empty string or the long expression
OR is represented by union , as usually

75
So union the arrows...

(a  b)a b ((a (a  b)  b)a b) (  a )  


* * *

s f

...and we are done!

76
EXERCISE
Construct a regular expression for the following automaton

77
EXERCISE
Construct a regular expression for the following automaton

78
EXERCISE
Construct a regular expression for the following automaton

79
SUMMARY trivially

subset construction

DFA NFA

state elimination

RE Thompson’s construction

(subset construction) nfa-to-dfa implied:


(state elimination) nfa-to-re dfa-to-re(x) = nfa-to-re(dfa)
(Thompson) re-to-nfa re-to-dfa(x) = nfa-to-dfa(re-to-nfa(x))

80
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

You might also like