dva337 ht 17 lecture 6 equivalences for regular … nfa →dfa from m nfa = (q nfa, Σ nfa, δ nfa,...
TRANSCRIPT
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
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
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
15
0
1
a
3a,b
{0}start
{1,3}
a
{3}b
start
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
MDFA = ({{q0}} ⊆ P(QNFA), ΣNFA, { q0 }, ?, ? )
Is the start state qDFA always { qNFA }?
17
a
b
a
0q 1q 2q
0q
NFA
DFA
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
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
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
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
STEP 2
Final result after completion of step 2
22
a
b
a
0q 1q 2q
NFA
DFA
0q 21,qqa
b
ab
ba,
SUBSET CONSTRUCTION, STEP 3
Step 3, add accepting states
23
a
b
a
0q 1q 2q
0q 21,qqa
b
ab
ba,
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,
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
EXERCISE, SUBSET CONSTRUCTION
26
a
b
b
b
a
a
b
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
28
AUTOMATON SPACE TIME
NFA
DFA
O(|Q|)
O(2|Q|)
O(|Q||w|)
O(|w|)
REGULAR LANGUAGES SO FAR
29
DFA NFA
subset construction
trivially
(subset construction) nfa-to-dfa
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
Create NFA for L1 ∪ L2
34
1M
2M
CLOSURE UNDER CONCATENATION
Create NFA for L1L2
35
1M 2M
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
46
Example : (a+b)*abb step by step construction
2
a
4 5
b
6
1
3
(a+b)
EXAMPLE: FROM A RE TO AN NFA
47
Example : (a+b)*abb
2
a
4 5b
1
6
3
0 7
(a+b)*
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
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))
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
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.
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
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
EXAMPLE
58
b
a
a
b1
3 4a2
b
abab *
a
b1
3 4
baabab *)*( 1
4
NOTE, COMMON MISTAKE
59
*)( ba
ba,
a b means
i.e.
NOT *)*( ba
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
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
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:
63
;
ba ,
ab
b
1 2
3
a
s
f
ba( )
64
ba ,
ab
b
1 2
3
a
s
f
ba a; ,
ba( )a
ba( )
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
66
ba ,
ab
1 2
3
a
s
f
a ; ba( )
a
bba( )a
67
Union again..
ba ,
a
b
1 2
3
a
s
f
ba( )
a
bba( )a
68
ab
2
3
s
f
ba( )
a
bba( )a
Without state 1...
69
ab
2
3
s
f
ba( )
a
ba(a
Now we repeat the same procedure for the state 2...
b
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*
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*)
72
3
s
f
This is how the FA looks like without state 2:
a
bba( ) a*
( bba( )a ba*)
73
3
s
f
Finally we remove state 3…
a
bba( ) a*
( bba( )a ba*)
74
3
s
f
...so we concatenate strings s-3, loop in 3 and 3-f
** )))((( babbaa
babbaa * ))((
baba *)(
a
baba * )( )( a
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
76
So union the arrows...
s f
...and we are done!
** )))((( babbaababa * )( )( a
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
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))
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