Lecture 5-FSMs-NFA-2-DFA
Lecture 5-FSMs-NFA-2-DFA
Lecture 5-FSMs-NFA-2-DFA
Lecture 5
NFA to DFA
2 methods
1
Syllabus and Terminologies
Regular Languages .. Regular Sets
REs (Regular Expressions)
FSMs (or FSA/FA) … Finite State Machines/Automata
DFA vs. NFA … Deterministic vs. Non-deterministic FSA
Comparison and conversion
Examples & Closure Operations
Pumping Lemma
Context Free Languages
CFGs … Context Free Grammars
PDA … Push Down Automata
Parsing: CFG generating strings vs. PDA recognizing strings
Turing Machine
05/11/2024 FCI-CU-EG 2
How to convert NFA to DFA??
High-level sketch
NFA
Regular
expressions DFA
Lexical Table-driven
Specification Implementation of DFA
3
How to convert from NFA to DFA
Create a Graph GD with vertex {q0}
Repeat until no more edges are missing
edge
Compute *(qi, a), *(qj, a).. *(qk, a)
Form a union of all these * {ql, qm, .. qn}
Create a vertex for GD labeled {ql, qm, .. qn}, if does not
exist
Add an edge a from {qi, qj, .. qk} to {ql, qm, .. qn}
Any vertex has q F is identified as a final state
f N
If M accepts , the vertex {q } in G is a final state
N 0 D
4
NFA to DFA conversion intuition
0, 1
1 0
NFA: q0 q1 q2
0 0
1 0
DFA: q0 q0 or q1 q0 or q2
1 1
5
NFA to DFA conversion intuition
0, 1
1 0
NFA: q0 q1 q2
0 0
1 0
DFA: q0 {q0, q1} {q0, q2}
1 1
6
NFA to DFA
7
Procedure NFA to DFA
8
Example
a
NFA
q0 a q1 q2
b
DFA
q0
9
Procedure NFA to DFA
10
Example
a
NFA
q0 a q1 q2
b
* (q0 , a ) {q1, q2 }
q0 a
DFA q1, q2
q0 , a q1, q2
11
Procedure NFA to DFA
12
NFA to DFA
a
NFA
q0 a q1 q2
b
DFA
q0 a
q1, q2
b
13
NFA to DFA
a
NFA
q0 a q1 q2
b a
DFA
q0 a
q1, q2
b
14
NFA to DFA
a
NFA
q0 a q1 q2
b
b a
DFA
q0 a
q1, q2
b
15
NFA to DFA
a
NFA
q0 a q1 q2
b
b a
DFA
q0 a
q1, q2
b
a, b
16
NFA to DFA … where is the final state?
a
NFA
q0 a q1 q2
b
b a
DFA
q0 a
q1, q2
b
a, b
17
NFA to DFA
a
NFA
q0 a q1 q2
b a
DFA b
q0 a
q1, q2
b
a, b
18
Procedure NFA to DFA
{qi , q j ,..., qm }
3. For any DFA state
19
Example
a
q0 a q1 q2
q1 F
NFA b
a
b
DFA a
q0 q1, q2
b q1, q2 F
a, b
20
Theorem
Take NFA M
Apply procedure to obtain DFA M
LM LM
21
Finally
We have
Languages Languages
accepted accepted
by NFAs by DFAs
22
NFA to DFA
Formal Graphical Method
23
NFA to DFA
24
ε–closure
ε
In this NFA: 1
ε
2 a 3 ε 4
1 = {1,2,4}
ε
2 = {2}
3 = {2,3,4}
4 = {4}
25
NFA to DFA Method
Input: An NFA S = States = { s0, s1, ..., sN} = SNFA
δ = Transition function = TransitionNFA
δ: (Q x Σ) –> 2Q -2Q is the power set of Q, the set of all subsets of Q
δ(q,s) is a function from Q x S to 2Q (but not to Q)
Output: A DFA S = States = {?, ?, ..., ?} = S
DFA
We’ll construct...
S the set of states in the DFA
DFA
Start state:
ε-Closure (0) = {0, 1, 2, 4, 7}
Call it A
A
A is the first node in the equivalent DFA
28
Example CONT.
Start state:
ε-Closure (0) = {0, 1, 2, 4, 7} = A
Call it A
a ?
A
b ?
29
Example CONT.
Start state:
ε-Closure (0) = {0, 1, 2, 4, 7} = A
Transition
DFA(A,a)
=
a ?
A
TransitionDFA(A,b)
b ?
=
30
Example CONT.
Start state:
ε-Closure (0) = {0, 1, 2, 4, 7} = A
Transition
DFA(A,a)
= ε-Closure (TransitionNFA(A,a))
= a ?
A
TransitionDFA(A,b)
b ?
=
31
Example CONT.
Start state:
ε-Closure (0) = {0, 1, 2, 4, 7} = A
Transition
DFA(A,a)
= ε-Closure (TransitionNFA(A,a))
= ε-Closure ({3,8}) a ?
= A
TransitionDFA(A,b)
b ?
=
32
Example CONT.
Start state:
ε-Closure (0) = {0, 1, 2, 4, 7} = A
Transition
DFA(A,a)
= ε-Closure (TransitionNFA(A,a))
= ε-Closure ({3,8}) a B
= {1,2,3,4,6,7,8} = B A
TransitionDFA(A,b)
b ?
=
33
Example CONT.
Start state:
ε-Closure (0) = {0, 1, 2, 4, 7} = A
Transition
DFA(A,a)
= ε-Closure (TransitionNFA(A,a))
= ε-Closure ({3,8}) a B
= {1,2,3,4,6,7,8} = B A
TransitionDFA(A,b)
b ?
= ε-Closure (TransitionNFA(A,b))
=
34
Example CONT.
Start state:
ε-Closure (0) = {0, 1, 2, 4, 7} = A
Transition
DFA(A,a)
= ε-Closure (TransitionNFA(A,a))
= ε-Closure ({3,8}) a B
= {1,2,3,4,6,7,8} = B A
TransitionDFA(A,b)
b ?
= ε-Closure (TransitionNFA(A,b))
= ε-Closure ({5})
=
35
Example CONT.
Start state:
ε-Closure (0) = {0, 1, 2, 4, 7} = A
Transition
DFA(A,a)
= ε-Closure (TransitionNFA(A,a))
= ε-Closure ({3,8}) a B
= {1,2,3,4,6,7,8} = B A
TransitionDFA(A,b)
b C
= ε-Closure (TransitionNFA(A,b))
= ε-Closure ({5})
= {1,2,4,5,6,7} = C
36
Example CONT.
Start state:
ε-Closure (0) = {0, 1, 2, 4, 7} = A
Transition
DFA(A,a)
= ε-Closure (TransitionNFA(A,a))
= ε-Closure ({3,8}) a B
= {1,2,3,4,6,7,8} = B A
TransitionDFA(A,b)
b C
= ε-Closure (TransitionNFA(A,b))
= ε-Closure ({5})
= {1,2,4,5,6,7} = C
37
Example CONT.
Start state:
ε-Closure (0) = {0, 1, 2, 4, 7} = A
Transition
DFA(A,a)
= ε-Closure (TransitionNFA(A,a))
= ε-Closure ({3,8}) a B
= {1,2,3,4,6,7,8} = B A
TransitionDFA(A,b) b C
= ε-Closure (TransitionNFA(A,b))
= ε-Closure ({5})
= {1,2,4,5,6,7} = C A is now done; mark it!
B and C are unmarked.
So far: Let’s do B next...
38
Example CONT.
Process B = {1,2,3,4,6,7,8}
a B
A
b C
39
Example CONT.
Process B = {1,2,3,4,6,7,8}
TransitionDFA(B,a)
=
a B
A
b C
40
Example CONT.
Process B = {1,2,3,4,6,7,8}
TransitionDFA(B,a)
= ε-Closure (TransitionNFA(B,a))
=
a B
A
b C
41
Example CONT.
Process B = {1,2,3,4,6,7,8}
TransitionDFA(B,a)
= ε-Closure (TransitionNFA(B,a))
= ε-Closure ({3,8})
= a B
A
b C
42
Example CONT.
Process B = {1,2,3,4,6,7,8}
TransitionDFA(B,a)
= ε-Closure (TransitionNFA(B,a))
= ε-Closure ({3,8})
= {1,2,3,4,6,7,8} = B
TransitionDFA(B,b)
=
43
Example CONT.
Process B = {1,2,3,4,6,7,8}
TransitionDFA(B,a)
= ε-Closure (TransitionNFA(B,a))
= ε-Closure ({3,8})
= {1,2,3,4,6,7,8} = B
TransitionDFA(B,b)
= ε-Closure (TransitionNFA(B,b))
=
44
Example CONT.
Process B = {1,2,3,4,6,7,8}
TransitionDFA(B,a)
= ε-Closure (TransitionNFA(B,a))
= ε-Closure ({3,8})
= {1,2,3,4,6,7,8} = B
TransitionDFA(B,b)
= ε-Closure (TransitionNFA(B,b))
= ε-Closure ({5,9})
=
45
Example CONT.
Process B = {1,2,3,4,6,7,8}
TransitionDFA(B,a)
= ε-Closure (TransitionNFA(B,a))
= ε-Closure ({3,8})
= {1,2,3,4,6,7,8} = B
TransitionDFA(B,b)
= ε-Closure (TransitionNFA(B,b))
= ε-Closure ({5,9})
= {1,2,4,5,6,7,9} = D
46
Example CONT.
Process B = {1,2,3,4,6,7,8}
TransitionDFA(B,a)
= ε-Closure (TransitionNFA(B,a))
= ε-Closure ({3,8})
= {1,2,3,4,6,7,8} = B
TransitionDFA(B,b)
= ε-Closure (TransitionNFA(B,b))
= ε-Closure ({5,9})
= {1,2,4,5,6,7,9} = D
47
Example CONT.
Process C = {1,2,4,5,6,7}
48
Example CONT.
Process C = {1,2,4,5,6,7}
TransitionDFA(C,a) =
TransitionDFA(C,b) =
49
Example CONT.
Process C = {1,2,4,5,6,7}
TransitionDFA(C,a) = {1,2,3,4,6,7,8} = B
TransitionDFA(C,b) =
50
Example CONT.
Process C = {1,2,4,5,6,7}
TransitionDFA(C,a) = {1,2,3,4,6,7,8} = B
TransitionDFA(C,b) = {1,2,4,5,6,7} = C
51
Example CONT.
Process C = {1,2,4,5,6,7}
TransitionDFA(C,a) = {1,2,3,4,6,7,8} = B
TransitionDFA(C,b) = {1,2,4,5,6,7} = C
52
Example CONT.
Process C = {1,2,4,5,6,7}
TransitionDFA(C,a) = {1,2,3,4,6,7,8} = B
TransitionDFA(C,b) = {1,2,4,5,6,7} = C
Process D = {1,2,4,5,6,7,9}
TransitionDFA(D,a) =
TransitionDFA(D,b) =
53
Example CONT.
Process C = {1,2,4,5,6,7}
TransitionDFA(C,a) = {1,2,3,4,6,7,8} = B
TransitionDFA(C,b) = {1,2,4,5,6,7} = C
Process D = {1,2,4,5,6,7,9}
TransitionDFA(D,a) = {1,2,3,4,6,7,8} = B
TransitionDFA(D,b) =
54
Example CONT.
Process C = {1,2,4,5,6,7}
TransitionDFA(C,a) = {1,2,3,4,6,7,8} = B
TransitionDFA(C,b) = {1,2,4,5,6,7} = C
Process D = {1,2,4,5,6,7,9}
TransitionDFA(D,a) = {1,2,3,4,6,7,8} = B
TransitionDFA(D,b) = {1,2,4,5,6,7,10} = E
55
Example CONT.
Process C = {1,2,4,5,6,7}
TransitionDFA(C,a) = {1,2,3,4,6,7,8} = B
TransitionDFA(C,b) = {1,2,4,5,6,7} = C
Process D = {1,2,4,5,6,7,9}
TransitionDFA(D,a) = {1,2,3,4,6,7,8} = B
TransitionDFA(D,b) = {1,2,4,5,6,7,10} = E
56
Example CONT.
Process E = {1,2,4,5,6,7,10}
TransitionDFA(E,a) =
TransitionDFA(E,b) =
57
Example CONT.
Process E = {1,2,4,5,6,7,10}
TransitionDFA(E,a) = {1,2,3,4,6,7,8} = B
TransitionDFA(E,b) =
58
Example CONT.
Process E = {1,2,4,5,6,7,10}
TransitionDFA(E,a) = {1,2,3,4,6,7,8} = B
TransitionDFA(E,b) = {1,2,4,5,6,7} = C
59
Example CONT.
Process E = {1,2,4,5,6,7,10}
TransitionDFA(E,a) = {1,2,3,4,6,7,8} = B
TransitionDFA(E,b) = {1,2,4,5,6,7} = C
60
Final DFA
61
NFA vs. DFA !
62