Professional Documents
Culture Documents
Slides Regular
Slides Regular
● All languages
● Decidable
Turing machines
● NP
● P
● Context-free
Context-free grammars, push-down automata
● Regular
Automata, non-deterministic automata,
regular expressions
DFA (Deterministic Finite Automata)
0 1
0 1
q0 qa
1 0 0
1
DFA (Deterministic Finite Automata)
0 1
0 1
q0 qa
1 0 0
1
● States , this DFA has 4 states
● Transitions
labelled with elements of the alphabet S = {0,1}
DFA (Deterministic Finite Automata)
0 1
0 1
q0 qa
1 0 0
1
Computation on input w:
● Begin in start state q0
● Read input string in a one-way fashion
● Follow the arrows matching input symbols
● When input ends: ACCEPT if in accept state
REJECT if not
DFA (Deterministic Finite Automata)
0 1
0 1
q0 qa
1 0
0
1
0 1
0 1
q0 qa
1 0
0
1
0 1
0 1
q0 qa
1 0
0
1
0 1
0 1
q0 qa
1 0
0
1
0 1
0 1
q0 qa
1 0
0
1
0 1
0 1
q0 qa
1 0
0
1
0 1
0 1
q0 qa
1 0
0
1
0 1
0 1
q0 qa
1 0
0
1
0 1
0 1
q0 qa
1 0
0
1
0 1
0 1
q0 qa
1 0
0
1
0 1
0 1
q0 qa
1 0
0
1
0 1
0 1
q0 qa
1 0
0
1
0 1
M :=
0 1
q0 qa
1 0
0
1
M recognizes language
L(M) = { w : w starts with 0 and ends with 1 }
L(M) is the language of strings causing M to accept
●
00 causes M to accept, so 00 is in L(M) 00 L(M)
● 01 does not cause M to accept, so 01 not in L(M),
01 L(M)
●
0101 L(M)
●
01101100 L(M)
●
011010 L(M)
Example 1
M := q0 q1
S = {0,1} 1 0
0
● L(M) = ?
Example
M := 1
S = {0,1}
= {0,1}*
Example 1
S = {0,1}
0 0
M :=
q0
1
1
0
● L(M) = ?
Example 1
S = {0,1}
0 0
M :=
q0
1
1
0
●
L(M) = all strings over {0,1} except empty string e
= {0,1}* - { e }
Example
S = {0,1} 0 1
1
M :=
0 0
q0
0
1
1 0
1
● L(M) = ?
Example
S = {0,1} 0 1
1
M :=
0 0
q0
0
1
1 0
1
0 1
M :=
0 1
q0 qa
1 0
0
We already saw that 1
0 1
M :=
0 1
q0 qa
0
Don't need to write such arrows:
If, from some state, read symbol with no
corresponding arrow, imagine M goes into “sink state”
that is not shown, and REJECT.
0,1,2
Means 0
1
2
M=
0,1 0,1
L(M) = ?
Example ∑ = {0,1}
M=
0,1 0,1
L(M) = ∑2 = {00,01,10,11}
Example from programming languages:
Recognize strings representing numbers:
S = {0,1,2,3,4,5,6,7,8,9, +, -, . }
+ 0,...,9
0,...,9
-
.
0,...,9
0,...,9
● r0 = q0
● r1 := ?
1
Example q0 q1
1 0
0
● r0 = q0
● r1 = d(r0 ,w1 )=d(q0 ,0 ) = q0
●r
2 := ?
1
Example q0 q1
1 0
0
● r0 = q0
● r1 = d(r0 ,w1 )=d(q0 ,0 ) = q0
● r2 = d(r1 ,w2 )=d(q0 ,1 ) = q1
● r3 := ?
1
Example q0 q1
1 0
0
● r0 = q0
● r1 = d(r0 ,w1 )=d(q0 ,0 ) = q0
● r2 = d(r1 ,w2 )=d(q0 ,1 ) = q1
● r3 = d(r2 ,w3 )=d(q1 ,1 ) = q0
● r3 = q0 in F OK DONE!
● Definition: For a DFA M, we denote by L(M) the
set of strings accepted by M:
L(M) := { w : M accepts w}
Trace of M on w.
M=
0,1 0,1
L(M) = ∑2 = {00,01,10,11}
M' =
0,1 0,1 0,1 0,1
0 1 1
0
L(MB) = B = ?
L(MA) = A = ?
Example 1 0
MB :=
MA := a b c d
0 1 1
0
L(MB) = B =
L(MA) = A =
{ w : w has odd number of 0}
{ w : w has even number of 1}
0 1 1
0
L(MB) = B =
L(MA) = A =
{ w : w has odd number of 0}
{ w : w has even number of 1}
MAUB := 1
a,c b,c
0
L(MAUB) = AUB = 0
1
{ w : w has even number of 1,
a,d b,d
or odd number of 0}
● Theorem: If A, B are regular, then so is A U B
● Proof:
Given DFA MA = (QA,S, δA,qA, FA) such that L(M) = A,
DFA MB = (QB,S, δB,qB, FB) such that L(M) = B.
Define DFA M = (Q, S, d, q0, F), where
Q := ?
● Theorem: If A, B are regular, then so is A U B
● Proof:
Given DFA MA = (QA,S, δA,qA, FA) such that L(M) = A,
DFA MB = (QB,S, δB,qB, FB) such that L(M) = B.
Define DFA M = (Q, S, d, q0, F), where
Q := QA X QB
q0 := ?
● Theorem: If A, B are regular, then so is A U B
● Proof:
Given DFA MA = (QA,S, δA,qA, FA) such that L(M) = A,
DFA MB = (QB,S, δB,qB, FB) such that L(M) = B.
Define DFA M = (Q, S, d, q0, F), where
Q := QA X QB
q0 := (qA , qB )
F := ?
● Theorem: If A, B are regular, then so is A U B
● Proof:
Given DFA MA = (QA,S, δA,qA, FA) such that L(M) = A,
DFA MB = (QB,S, δB,qB, FB) such that L(M) = B.
Define DFA M = (Q, S, d, q0, F), where
Q := QA X QB
q0 := (qA , qB )
F := {(q,q') ∈ Q : q ∈ FA or q' ∈ FB }
δ( (q,q'), v) := (?, ? )
● Theorem: If A, B are regular, then so is A U B
● Proof:
Given DFA MA = (QA,S, δA,qA, FA) such that L(M) = A,
DFA MB = (QB,S, δB,qB, FB) such that L(M) = B.
Define DFA M = (Q, S, d, q0, F), where
Q := QA X QB
q0 := (qA , qB )
F := {(q,q') ∈ Q : q ∈ FA or q' ∈ FB }
δ( (q,q'), v) := (δA (q,v), δB (q',v) )
● We need to show L(M) = A U B that is, for any w:
M accepts w MA accepts w or MB accepts w
● Proof M accepts wMA accepts w or MB accepts w
● Suppose that M accepts w of length k.
● From the definitions of accept and M,
the trace (s0 , t0 ) , …, (sk , tk ) of M on w
has (sk,tk)∈?
● Proof M accepts wMA accepts w or MB accepts w
● Suppose that M accepts w of length k.
● From the definitions of accept and M,
the trace (s0 , t0 ) , …, (sk , tk ) of M on w
has (sk,tk)∈ F.
1
● Next we allow multiple choices,
non-deterministic 1
●
We also allow e-transitions: e
can follow without reading anything
Example of NFA
q0
b a
e
a q2
q1 a,b
Intuition of how it computes:
● Accept string w if there is a way to follow transitions
that ends in accept state
●
Transitions labelled with symbol in S = {a,b}
must be matched with input
●
e transitions can be followed without matching
Example of NFA
q0
b a
e
a q2
q1 a,b
Example:
●
Accept a (first follow e-transition )
● Accept baaa
ANOTHER Example of NFA
q1 b
b a
e
q0 q3
a,b q2 b
Example:
● Accept bab (two accepting paths, one
uses the e-transition)
● Reject ba (two possible paths, but neither
has final state = q1)
● Definition: A non-deterministic finite automaton (NFA)
is a 5-tuple (Q, S, d, q0, F) where
● Theorem:
For every NFA N there is DFA M : L(M) = L(N)
● Theorem:
For every NFA N there is DFA M : L(M) = L(N)
●
Construction without e transitions
●
Given NFA N (Q, S, d, q, F)
●
Construct DFA M (Q', S, d', q', F') where:
● Q' := Powerset(Q)
● q' = {q}
●
F' = { S : S Q' and S contains an element of F}
● d'(S, a) := U
s S d(s,a)
●
We think of following e transitions at beginning, or
right after reading an input symbol in S
● Theorem:
For every NFA N there is DFA M : L(M) = L(N)
●
Construction including e transitions
●
Given NFA N (Q, S, d, q, F)
●
Construct DFA M (Q', S, d', q', F') where:
● Q' := Powerset(Q)
● q' = E({q})
●
F' = { S : S Q' and S contains an element of F}
● d'(S, a) := E( U
s S d(s,a) )
NFA DFA
1
{1,3} {3}
b a
a e
2 3
a,b {2} {2,3} {1,2,3}
QDFA = Powerset(QNFA)
= Powerset({1,2,3}) {1} {1,2}
= {,{1},{2},{3},{1,2}...}
Example: NFA → DFA conversion
NFA DFA
1
{1,3} {3}
b a
a e
2 3
a,b {2} {2,3} {1,2,3}
qDFA = E({qNFA})
= E({1}) {1} {1,2}
= {1,3}
Example: NFA → DFA conversion
NFA DFA
1
{1,3} {3}
b a
a e
2 3
a,b {2} {2,3} {1,2,3}
FDFA = {S : S contains
an element of FNFA} {1} {1,2}
Example: NFA → DFA conversion
NFA DFA
1
{1,3} {3}
b a
a e
2 3
a,b {2} {2,3} {1,2,3}
dDFA({1}, a)
= E(dNFA(1, a)) a
{1} {1,2}
= E() =
Example: NFA → DFA conversion
NFA DFA
1
{1,3} {3}
b a
a e
2 3
a,b {2} {2,3} {1,2,3}
dDFA({1}, b) b
= E(dNFA(1, b)) a
{1} {1,2}
= E({2}) = {2}
Example: NFA → DFA conversion
NFA DFA
1
{1,3} {3}
b a
a e
2 3 a
a,b {2} {2,3} {1,2,3}
dDFA({2}, a) b
= E(dNFA(2, a)) a
{1} {1,2}
= E({2,3}) = {2,3}
Example: NFA → DFA conversion
NFA DFA
1
{1,3} {3}
b a b
a e
2 3 a
a,b {2} {2,3} {1,2,3}
dDFA({2}, b) b
= E(dNFA(2, b)) a
{1} {1,2}
= E({3}) = {3}
Example: NFA → DFA conversion
NFA DFA
a
1
{1,3} {3}
b a b
a e
2 3 a
a,b {2} {2,3} {1,2,3}
dDFA({3}, a) b
= E(dNFA(3, a)) a
{1} {1,2}
= E({1}) = {1,3}
Example: NFA → DFA conversion
NFA DFA
a b
1
{1,3} {3}
b a b
a e
2 3 a
a,b {2} {2,3} {1,2,3}
dDFA({3}, b) b
= E(dNFA(3, b)) a
{1} {1,2}
= E() =
Example: NFA → DFA conversion
NFA DFA
a b
1
{1,3} {3}
b a b
a e
2 3 a
a,b {2} {2,3} {1,2,3}
a
dDFA({2,3}, a) b
= E(dNFA(2,a) U dNFA(3,a)) a
{1} {1,2}
= E({2,3} U {1}) = {1,2,3}
Example: NFA → DFA conversion
NFA DFA
a b
1
{1,3} {3}
b a b
a e b
2 3 a
a,b {2} {2,3} {1,2,3}
a
dDFA({2,3}, b) b
= E(dNFA(2,b) U dNFA(3,b)) a
{1} {1,2}
= E({3} U ) = {3}
Example: NFA → DFA conversion
NFA DFA
a
a b
1
{1,3} {3}
b a b
a e b
2 3 a
a,b {2} {2,3} {1,2,3}
a
dDFA({1,3}, a) b
= E(dNFA(1,a) U dNFA(3,a)) a
{1} {1,2}
= E( U {1}) = {1,3}
Example: NFA → DFA conversion
NFA DFA
a
a b
1
{1,3} {3}
b a b b
a e b
2 3 a
a,b {2} {2,3} {1,2,3}
a
dDFA({1,3}, b) b
= E(dNFA(1,b) U dNFA(3,b)) a
{1} {1,2}
= E({2} U ) = {2}
Example: NFA → DFA conversion
NFA DFA
a
a b
1
{1,3} {3}
b a b b
a e b
2 3 a
a,b {2} {2,3} {1,2,3}
a
dDFA({1,2}, a) b a
= E(dNFA(1,a) U dNFA(2,a)) a
{1} {1,2}
= E( U {2,3}) = {2,3}
Example: NFA → DFA conversion
NFA DFA
a
a b
1
{1,3} {3}
b a b b
a e b
2 3 a
a,b {2} {2,3} {1,2,3}
a
dDFA({1,2}, b) b a,b
= E(dNFA(1,b) U dNFA(2,b)) a
{1} {1,2}
= E({2} U {3}) = {2,3}
Example: NFA → DFA conversion
NFA DFA
a
a b
1
{1,3} {3}
b a b b a
a e b
2 3 a
a,b {2} {2,3} {1,2,3}
a
dDFA({1,2,3}, a) b a,b
=E(d (1,a) U dNFA(2,a) U dNFA(3,a))
{1} a
NFA
{1,2}
=E( U {2,3} U {1}) = {1,2,3}
Example: NFA → DFA conversion
NFA DFA
a
a b
1
{1,3} {3}
b a b b a
a e b
2 3 a b
a,b {2} {2,3} {1,2,3}
a
dDFA({1,2,3}, b) b a,b
=E(d (1,b) U dNFA(2,b) U dNFA(3,b))
{1} a
NFA
{1,2}
=E({2} U {3} U ) = {2,3}
Example: NFA → DFA conversion
NFA DFA
a a,b
a b
1
{1,3} {3}
b a b b a
a e b
2 3 a b
a,b {2} {2,3} {1,2,3}
a
b a,b
dDFA(, a) =
{1} {1,2} a
dDFA(, b) =
Example: NFA → DFA conversion
NFA DFA
a a,b
a b
1
{1,3} {3}
b a b b a
a e b
2 3 a b
a,b {2} {2,3} {1,2,3}
a
NFA DFA
b {3} {1}
1 2
b
a
e {1,2,3}
3
{2}
QDFA = Powerset(QNFA) {1,2}
{1,3}
= Powerset({1,2,3}) {2,3}
= {,{1},{2},{3},{1,2}...}
ANOTHER Example: NFA → DFA conversion
NFA DFA
b {3} {1}
1 2
b
a
e {1,2,3}
3
{2}
qDFA = E({qNFA}) {1,2}
{1,3}
= E({1}) {2,3}
= {1}
ANOTHER Example: NFA → DFA conversion
NFA DFA
b {3} {1}
1 2
b
a
e {1,2,3}
3
{2}
FDFA = {S : S contains {1,2}
{1,3}
an element of FNFA} {2,3}
ANOTHER Example: NFA → DFA conversion
NFA DFA
b {3} {1}
1 2
a
b
a
e {1,2,3}
3
{2}
dDFA({1}, a) {1,2}
{1,3}
= E(dNFA(1, a)) {2,3}
= E() =
ANOTHER Example: NFA → DFA conversion
NFA DFA
b {3} {1}
1 2
a b
b
a
e {1,2,3}
3
{2}
dDFA({1}, b) {1,2}
{1,3}
= E(dNFA(1, b)) {2,3}
= E({2,3}) = {1,2,3}
ANOTHER Example: NFA → DFA conversion
NFA DFA
b {3} {1}
1 2
a b
b
a
e {1,2,3}
3
{2} a
dDFA({2}, a) {1,2}
{1,3}
= E(dNFA(2, a)) {2,3}
= E({3}) = {1,3}
ANOTHER Example: NFA → DFA conversion
NFA DFA
b {3} {1}
1 2
a b
b
a
e {1,2,3}
3
b
{2} a
dDFA({2}, b) {1,2}
{1,3}
= E(dNFA(2, b)) {2,3}
= E() =
ANOTHER Example: NFA → DFA conversion
NFA DFA
b {3} {1}
1 2
a a b
b
a
e {1,2,3}
3
b
{2} a
dDFA({3}, a) {1,2}
{1,3}
= E(dNFA(3, a)) {2,3}
= E() =
ANOTHER Example: NFA → DFA conversion
NFA DFA
b {3} {1}
1 2
a,b a b
b
a
e {1,2,3}
3
b
{2} a
dDFA({3}, b) {1,2}
{1,3}
= E(dNFA(3, b)) {2,3}
= E() =
ANOTHER Example: NFA → DFA conversion
NFA DFA
b {3} {1}
1 2
a,b a b
b
a
e {1,2,3}
3
b
{2} a
dDFA({1,2}, a) {1,2}
{1,3} a
= E(dNFA(1,a) U dNFA(2,a)) {2,3}
= E( U {3}) = {1,3}
ANOTHER Example: NFA → DFA conversion
NFA DFA
b {3} {1}
1 2
a,b a b
b
a
e {1,2,3}
3
b b
{2} a
dDFA({1,2}, b) {1,2}
{1,3} a
= E(dNFA(1,b) U dNFA(2,b)) {2,3}
= E({2,3} U ) = {1,2,3}
ANOTHER Example: NFA → DFA conversion
NFA DFA
b {3} {1}
1 2
a,b a b
b
a
e {1,2,3}
3
b a b
{2} a
dDFA({1,3}, a) {1,2}
{1,3} a
= E(dNFA(1,a) U dNFA(3,a)) {2,3}
= E( U ) =
ANOTHER Example: NFA → DFA conversion
NFA DFA
b {3} {1}
1 2
a,b a b
b
a
e {1,2,3}
3
b a b b
{2} a
dDFA({1,3}, b) {1,2}
{1,3} a
= E(dNFA(1,b) U dNFA(3,b)) {2,3}
= E({2,3} U ) = {1,2,3}
ANOTHER Example: NFA → DFA conversion
NFA DFA
b {3} {1}
1 2
a,b a b
b
a
e {1,2,3}
3
b a b b
{2} a
dDFA({2,3}, a) {1,2}
{1,3} a
= E(dNFA(2,a) U dNFA(3,a)) {2,3}
a
= E({3} U ) = {1,3}
ANOTHER Example: NFA → DFA conversion
NFA DFA
b {3} {1}
1 2
a,b a b
b
a
e {1,2,3}
3
b b a b b
{2} a
dDFA({2,3}, b) {1,2}
{1,3} a
= E(dNFA(2,b) U dNFA(3,b)) {2,3}
a
= E( U ) =
ANOTHER Example: NFA → DFA conversion
NFA DFA
b {3} {1}
1 2
a,b a b
b
a
e {1,2,3}
3
b b a b b
a
{2} a
dDFA({1,2,3}, a) {1,2}
{1,3} a
=E(dNFA
(1,a) U dNFA(2,a) U dNFA(3,a)) {2,3}
a
=E( U {3} U ) = {1,3}
ANOTHER Example: NFA → DFA conversion
NFA DFA
b {3} {1}
1 2
a,b a b b
b
a
e {1,2,3}
3
b b a b b
a
{2} a
dDFA({1,2,3}, b) {1,2}
{1,3} a
=E(dNFA
(1,b) U dNFA(2,b) U dNFA(3,b)) {2,3}
a
=E({2,3} U U ) = {1,2,3}
ANOTHER Example: NFA → DFA conversion
NFA DFA
b {3} {1}
1 2
a,b a b b
b
a
e a,b {1,2,3}
3
b b a b b
a
{2} a
{1,2}
dDFA(, a) = {1,3} a
{2,3} a
dDFA(, b) =
ANOTHER Example: NFA → DFA conversion
NFA DFA
b {1}
1 2
a b b
b
a
e a,b {1,2,3}
3
a b
a
M M e
A B
U = e
MA N
M e
U B = e
Construction:
A = (QA, S, dA, qA, FA) : L(MA) = A,
● Given DFA M
M1 = 0,1
0,1 0,1
L(M1) = L1
Example
Is L = {w in {0,1}* : |w| is divisible by 3 OR
w starts with a 1} regular?
M1 = 0,1 M2 = 0,1
1
0,1 0,1
L(M1) = L1 L(M2) = L2
Example
Is L = {w in {0,1}* : |w| is divisible by 3 OR
w starts with a 1} regular?
N e
e
= e
N
M MB
e
e
A o = e
Construction:
A = (QA, S, dA, qA, FA) : L(MA) = A,
● Given DFA M
Construction:
A = (QA, S, dA, qA, FA) : L(MA) = A,
● Given DFA M
Construction:
A = (QA, S, dA, qA, FA) : L(MA) = A,
● Given DFA M
Construction:
A = (QA, S, dA, qA, FA) : L(MA) = A,
● Given DFA M
Construction:
A = (QA, S, dA, qA, FA) : L(MA) = A,
● Given DFA M
Construction:
A = (QA, S, dA, qA, FA) : L(MA) = A,
● Given DFA M
Construction:
A = (QA, S, dA, qA, FA) : L(MA) = A,
● Given DFA M
Let L0 = {w : w contains a 0}
L1 = {w : w contains a 1}. Then L = L0 o L1.
Example
Is L = {w in {0,1}* : w contains a 1 after a 0}
regular?
Let L0 = {w : w contains a 0}
L1 = {w : w contains a 1}. Then L = L0 o L1.
M0 = 1 0,1
0
L(M0) = L0
Example
Is L = {w in {0,1}* : w contains a 1 after a 0}
regular?
Let L0 = {w : w contains a 0}
L1 = {w : w contains a 1}. Then L = L0 o L1.
M0 = 1 0,1 M1 = 0 0,1
0 1
L(M0) = L0 L(M1) = L1
Example
Is L = {w in {0,1}* : w contains a 1 after a 0}
regular?
Let L0 = {w : w contains a 0}
L1 = {w : w contains a 1}. Then L = L0 o L1.
M = 1 0,1 0 0,1
0 e 1
L is regular.
We now return to the question:
● Suppose A, B are regular languages, then
● not A := { w : w is not in A } REGULAR
● A U B := { w : w in A or w in B } REGULAR
● A o B := { w1 w2 : w1 ∈A and w2 ∈ B } REGULAR
● A* := { w1 w2 … wk : k 0 , wi in A for every i }
Theorem: If A is a regular language, then so is
A* := { w : w = w1...wk, wi in A for i=1,...,k }
Construction:
A = (QA, S, dA, qA, FA) : L(MA) = A,
● Given DFA M
Construction:
A = (QA, S, dA, qA, FA) : L(MA) = A,
● Given DFA M
Construction:
A = (QA, S, dA, qA, FA) : L(MA) = A,
● Given DFA M
Construction:
A = (QA, S, dA, qA, FA) : L(MA) = A,
● Given DFA M
Construction:
A = (QA, S, dA, qA, FA) : L(MA) = A,
● Given DFA M
M0 =
0,1 0,1
L(M0) = L0
Example
Is L = {w in {0,1}* : w has even length}
regular?
M = e
e 0,1 0,1
Conventions:
● Write a instead of {a}
● Write AB for A o B
● Write ∑ for U
a∈∑ a So if ∑ = {a,b} then ∑ = a U b
● Operation * has precedence over o, and o over U
so 1 U 01* means 1U(0(1)*)
●
R=e M := ?
Theorem: For every RE R there is NFA M: L(M) = L(R)
Construction:
●
R= M :=
●
R=e M :=
● R=a M := ?
Theorem: For every RE R there is NFA M: L(M) = L(R)
Construction:
●
R= M :=
●
R=e M :=
● R=a M :=
a
● R = R U R' ?
Theorem: For every RE R there is NFA M: L(M) = L(R)
Construction:
●
R= M :=
●
R=e M :=
● R=a M :=
a
●
R=e M :=
● R=a M :=
a
●
R=e M :=
● R=a M :=
a
RE = (ab U a)*
Example: RE → NFA
RE = (ab U a)*
a
Ma =
L(Ma)=L(a)
Example: RE → NFA
RE = (ab U a)*
b
Ma = a Mb =
L(Ma)=L(a) L(Mb)=L(b)
Example: RE → NFA
RE = (ab U a)*
Mab =
a e b
L(Mab)=L(ab)
Example: RE → NFA
RE = (ab U a)*
a
Mab = Ma =
a e b
L(Mab)=L(ab) L(Ma)=L(a)
Example: RE → NFA
RE = (ab U a)*
Mab U a =
a e b
e
e a
L(Mab U a)=L(ab U a)
Example: RE → NFA
RE = (ab U a)*
M(ab U a)* =
e
a e b
e e
e a
e
L(M(ab U a)*)=L((ab U a)*)=L(RE)
ANOTHER Example: RE → NFA
RE =(e U a)ba*
ANOTHER Example: RE → NFA
RE =(e U a)ba*
Me =
L(Me)=L(e)
ANOTHER Example: RE → NFA
RE =(e U a)ba*
a
Me = Ma =
L(Me)=L(e) L(Ma)=L(a)
ANOTHER Example: RE → NFA
RE =(e U a)ba*
Me U a =
e
e
a
L(Me U a)=L(e U a)
ANOTHER Example: RE → NFA
RE =(e U a)ba*
b
Me U a = Mb =
e
L(Mb)=L(b)
e
a
L(Me U a)=L(e U a)
ANOTHER Example: RE → NFA
RE =(e U a)ba*
M(e U a)b =
e
e b
e e
a
RE =(e U a)ba*
M(e U a)b = Ma = a
e
e b
e e L(Ma)=L(a)
a
RE =(e U a)ba*
M(e U a)b = e
Ma* = e a
e
e b
e e L(Ma*)=L(a*)
a
RE =(e U a)ba*
M(e U a)ba* =
e
e b e e
e e a
e
a
a*b* ab
q0 qa
Nondeterministic
Transitions labelled by RE
a*b* ab
q0 qa
Convention:
Unique final state
Exactly one transition between each pair of states
except nothing going into start state
nothing going out of final state
If arrow not shown in picture, label =
●Definition: A generalized finite automaton (GNFA)
● is a 5-tuple (Q, S, d, q , q ) where
0 a
● Q is a finite set of states
●
S is the input alphabet
● d : (Q - {q }) X (Q – {q }) → Regular Expressions
a 0
● q0 in Q is the start state
● qa in Q is the accept state
● Definition: GNFA (Q, S, d, q0, qa) accepts a string w if
● ∃integer k, ∃ k strings w1 , w2 , …, wk S*
such that w = w1 w2 … wk
(divide w in k strings)
a* ab
q0 q1 qa
Accepts w = aaabbab
w1=?
Example b*
a* ab
q0 q1 qa
Accepts w = aaabbab
w1=aaa w2=?
Example b*
a* ab
q0 q1 qa
Accepts w = aaabbab
w1=aaa w2=bb w3=ab
r0=q0 r1=?
Example b*
a* ab
q0 q1 qa
Accepts w = aaabbab
w1=aaa w2=bb w3=ab
r0=q0 r1=q1 r2=?
a* ab
q0 q1 qa
Accepts w = aaabbab
w1=aaa w2=bb w3=ab
r0=q0 r1=q1 r2=q1 r3 = ?
a* ab
q0 q1 qa
Accepts w = aaabbab
w1=aaa w2=bb w3=ab
r0=q0 r1=q1 r2=q1 r3 = qa
1 1U0
DFA a b
b,c q2
q1
Example: DFA → GNFA → RE
GNFA a b
e bUc q2 e qa
q0 q1
Example: DFA → GNFA → RE
a b
e bUc q2 e qa
q0 q1
q0 q2 e qa
Example: DFA → GNFA → RE
a b
e bUc q2 e qa
q0 q1
q0 q2 e qa
Example: DFA → GNFA → RE
Ø
a b
e bUc q2 e qa
q0 q1
e a* (b U c) U Ø q2 e qa
q0
Example: DFA → GNFA → RE
a b
e bUc q2 e qa
q0 q1
a* (b U c) q2 e qa
q0
Example: DFA → GNFA → RE
a b
e bUc q2 e qa
q0 q1
a* (b U c) q2 e qa
q0
Example: DFA → GNFA → RE
a* (b U c) q2 e qa
q0
q0 qa
Example: DFA → GNFA → RE
a* (b U c) q2 e qa
q0
q0 qa
Example: DFA → GNFA → RE
a* (b U c) q2 e qa
q0
a* (b U c) b* e U Ø qa
q0
Example: DFA → GNFA → RE
a* (b U c) q2 e qa
q0
a* (b U c) b* qa
q0
Example: DFA → GNFA → RE
a* (b U c) q2 e qa
q0
a* (b U c) b* qa
q0
Example: DFA → GNFA → RE
a* (b U c) b* qa
q0
RE = a* (b U c) b*
ANOTHER Example: DFA → GNFA → RE
a
DFA
q1 b q3
c
a
c q2
b
ANOTHER Example: DFA → GNFA → RE
a
GNFA
q0 e q1 b q3 e qa
c
a
c q2
b
ANOTHER Example: DFA → GNFA → RE
a
q0 e q1 b q3 e qa
c
a
Eliminate q1: c q2
b
re-draw GNFA with
all other states
q3 e qa
q0
a
q2
b
ANOTHER Example: DFA → GNFA → RE
a
q0 e q1 b q3 e qa
c
a
Eliminate q1: c q2
b
find a path
through q1
q3 e qa
q0
a
q2
b
ANOTHER Example: DFA → GNFA → RE
a
q0 e q1 b q3 e qa
c
a
Eliminate q1: c q2
b
add edge to
new GNFA
e a*b U Ø q3 e qa
q0
a
q2
b
ANOTHER Example: DFA → GNFA → RE
a
q0 e q1 b q3 e qa
c
a
Eliminate q1: c q2
b
find another
path through q1
e a*b U Ø q3 e qa
q0
a
q2
b
ANOTHER Example: DFA → GNFA → RE
a
q0 e q1 b q3 e qa
c
a
Eliminate q1: c q2
b
add edge to
new GNFA
e a*b U Ø q3 e qa
q0
a
e a*c U Ø q2
b
ANOTHER Example: DFA → GNFA → RE
a
q0 e q1 b q3 e qa
c
a
Eliminate q1: c q2
b
find another
path through q1
e a*b U Ø q3 e qa
q0
a
e a*c U Ø q2
b
ANOTHER Example: DFA → GNFA → RE
a
q0 e q1 b q3 e qa
c
a
Eliminate q1: c q2 don't forget current
b q2 → q3 edge!
add edge to
This time is not Ø !
new GNFA
e a*b U Ø q3 e qa
q0
ca*b U a
e a*c U Ø q2
b
ANOTHER Example: DFA → GNFA → RE
a
q0 e q1 b q3 e qa
c
a
Eliminate q1: c q2
b
find another
path through q1
e a*b U Ø q3 e qa
q0
ca*b U a
e a*c U Ø q2
b
ANOTHER Example: DFA → GNFA → RE
a
q0 e q1 b q3 e qa
c
a
Eliminate q1: c q2 don't forget current
b q2 → q2 edge!
add edge to
new GNFA
e a*b U Ø q3 e qa
q0
ca*b U a
e a*c U Ø q2
ca*c U b
ANOTHER Example: DFA → GNFA → RE
a
q0 e q1 b q3 e qa
c
a
Eliminate q1: c q2
b
when no more paths
through q1, start over
(and simplify a*b q3 e qa
q0
REs)
a*c ca*b U a
q2
ca*c U b
ANOTHER Example: DFA → GNFA → RE
a*b q3 e qa
q0
a*c ca*b U a
q2
q0 a*b q3 e qa
ANOTHER Example: DFA → GNFA → RE
a*b q3 e qa
q0
a*c ca*b U a
q2
q0 a*b q3 e qa
ANOTHER Example: DFA → GNFA → RE
a*b q3 e qa
q0
a*c ca*b U a
q2
a*b q3 e qa
q0
a*c ca*b U a
q2
Eliminate q2:
ca*c U b
when no more paths
through q2, start over
Eliminate q3:
re-draw GNFA with
all other states
q0 qa
ANOTHER Example: DFA → GNFA → RE
Eliminate q3: Ø
find a path through q3 Ø
q0 qa
ANOTHER Example: DFA → GNFA → RE
Eliminate q3: Ø
Eliminate q3:
when no more paths through q3, start over
(and simplify REs)
don't forget: Ø*= ε
q0
a*c(ca*c U b)*(ca*b U a) U a*b
qa
ANOTHER Example: DFA → GNFA → RE
q0
a*c(ca*c U b)*(ca*b U a) U a*b
qa
RE DFA NFA
Any of the three recognize exactly
the regular languages (initially defined using DFA)
These conversions are used every time you enter
an RE, for example for pattern matching using grep
● Two players:
You, Adversary.
● Rules:
First Adversary says a number.
Then You say a number.
You win if your number is bigger.
● Two players:
You, Adversary.
● Rules:
First Adversary says a number.
Then You say a number.
You win if your number is bigger.
● Two players:
You, Adversary. ,
● Rules:
First Adversary says a number. xy:y>x
Then You say a number.
You win if your number is bigger.