Professional Documents
Culture Documents
, ,
Slides modied by Benny Chor, based on original slides by Maurice Herlihy, Brown University. p.1
Computational Models - Lecture 2
Non-Deterministic Finite Automata (NFA)
Closure of Regular Languages Under
, ,
Regular expressions
Slides modied by Benny Chor, based on original slides by Maurice Herlihy, Brown University. p.1
Computational Models - Lecture 2
Non-Deterministic Finite Automata (NFA)
Closure of Regular Languages Under
, ,
Regular expressions
Equivalence with nite automata
Slides modied by Benny Chor, based on original slides by Maurice Herlihy, Brown University. p.1
Computational Models - Lecture 2
Non-Deterministic Finite Automata (NFA)
Closure of Regular Languages Under
, ,
Regular expressions
Equivalence with nite automata
Slides modied by Benny Chor, based on original slides by Maurice Herlihy, Brown University. p.1
Computational Models - Lecture 2
Non-Deterministic Finite Automata (NFA)
Closure of Regular Languages Under
, ,
Regular expressions
Equivalence with nite automata
Sipsers book, 1.1-1.3
Slides modied by Benny Chor, based on original slides by Maurice Herlihy, Brown University. p.1
DFA Formal Denition (reminder)
A deterministic nite automaton (DFA) is a 5-tuple
(Q, , , q
0
, F), where
Q is a nite set called the states,
is a nite set called the alphabet,
: Q Q is the transition function,
q
0
Q is the start state, and
F Q is the set of accept states.
Slides modied by Benny Chor, based on original slides by Maurice Herlihy, Brown University. p.2
Languages and DFA (reminder)
Denition: Let L ( L
= {x
1
x
2
. . . x
k
|k 0 and each x
i
A}
Slides modied by Benny Chor, based on original slides by Maurice Herlihy, Brown University. p.4
Claim: Closure Under Union (reminder)
If A
1
and A
2
are regular languages, so is A
1
A
2
.
Slides modied by Benny Chor, based on original slides by Maurice Herlihy, Brown University. p.5
Claim: Closure Under Union (reminder)
If A
1
and A
2
are regular languages, so is A
1
A
2
.
Approach to Proof:
some M
1
accepts A
1
some M
2
accepts A
2
construct M that accepts A
1
A
2
.
Slides modied by Benny Chor, based on original slides by Maurice Herlihy, Brown University. p.5
What About Concatenation?
Thm: If L
1
, L
2
are regular languages, so is L
1
L
2
.
Example: L
1
= {good, bad} and L
2
= {boy, girl}.
L
1
L
2
= {goodboy, goodgirl, badboy, badgirl}
Slides modied by Benny Chor, based on original slides by Maurice Herlihy, Brown University. p.6
What About Concatenation?
Thm: If L
1
, L
2
are regular languages, so is L
1
L
2
.
Example: L
1
= {good, bad} and L
2
= {boy, girl}.
L
1
L
2
= {goodboy, goodgirl, badboy, badgirl}
This is much harder to prove.
Idea: Simulate M
1
for a while, then switch to M
2
.
Slides modied by Benny Chor, based on original slides by Maurice Herlihy, Brown University. p.6
What About Concatenation?
Thm: If L
1
, L
2
are regular languages, so is L
1
L
2
.
Example: L
1
= {good, bad} and L
2
= {boy, girl}.
L
1
L
2
= {goodboy, goodgirl, badboy, badgirl}
This is much harder to prove.
Idea: Simulate M
1
for a while, then switch to M
2
.
Problem: But when do you switch?
This leads us into non-determinism.
Slides modied by Benny Chor, based on original slides by Maurice Herlihy, Brown University. p.6
Non-Deterministic Finite Automata
q
4
q
1
q
2
q
3
0
0,1 0,1
1 0,
an NFA may have more than one transition
labeled with the same symbol,
Slides modied by Benny Chor, based on original slides by Maurice Herlihy, Brown University. p.7
Non-Deterministic Finite Automata
q
4
q
1
q
2
q
3
0
0,1 0,1
1 0,
an NFA may have more than one transition
labeled with the same symbol,
an NFA may have no transitions labeled with a
certain symbol, and
Slides modied by Benny Chor, based on original slides by Maurice Herlihy, Brown University. p.7
Non-Deterministic Finite Automata
q
4
q
1
q
2
q
3
0
0,1 0,1
1 0,
an NFA may have more than one transition
labeled with the same symbol,
an NFA may have no transitions labeled with a
certain symbol, and
an NFA may have transitions labeled with , the
empty string.
Slides modied by Benny Chor, based on original slides by Maurice Herlihy, Brown University. p.7
Non-Deterministic Finite Automata
q
4
q
1
q
2
q
3
0
0,1 0,1
1 0,
an NFA may have more than one transition
labeled with the same symbol,
an NFA may have no transitions labeled with a
certain symbol, and
an NFA may have transitions labeled with , the
empty string.
Comment: Every DFA is also a non-deterministic
nite automata (NFA).
Slides modied by Benny Chor, based on original slides by Maurice Herlihy, Brown University. p.7
Non-Deterministic Computation
q
4
q
1
q
2
q
3
0
0,1 0,1
1 0,
What happens when more than one transition is
possible?
Slides modied by Benny Chor, based on original slides by Maurice Herlihy, Brown University. p.8
Non-Deterministic Computation
q
4
q
1
q
2
q
3
0
0,1 0,1
1 0,
What happens when more than one transition is
possible?
the machine splits into multiple copies
each branch follows one possibility
together, branches follow all possibilities.
If the input doesnt appear, that branch dies.
Automaton accepts if some branch accepts.
Slides modied by Benny Chor, based on original slides by Maurice Herlihy, Brown University. p.8
Non-Deterministic Computation
q
4
q
1
q
2
q
3
0
0,1 0,1
1 0,
What happens when more than one transition is
possible?
the machine splits into multiple copies
each branch follows one possibility
together, branches follow all possibilities.
If the input doesnt appear, that branch dies.
Automaton accepts if some branch accepts.
What does an transition do?
Slides modied by Benny Chor, based on original slides by Maurice Herlihy, Brown University. p.8
Non-Deterministic Computation
q
4
q
1
q
2
q
3
0
0,1 0,1
1 0,
What happens on string 1001?
Slides modied by Benny Chor, based on original slides by Maurice Herlihy, Brown University. p.9
The String 1001
q
4
q
1
q
2
q
3
0
0,1 0,1
1 0,
Slides modied by Benny Chor, based on original slides by Maurice Herlihy, Brown University. p.10
The String 1001
q
4
q
1
q
2
q
3
0
0,1 0,1
1 0,
q
4
q
1
q
3
q
1
q
2
q
1
q
2
q
3
q
1
q
3
q
1
q
4
1
0
0
1
symbol
Slides modied by Benny Chor, based on original slides by Maurice Herlihy, Brown University. p.10
Why Non-Determinism?
Theorem (to be proved soon): Deterministic and
non-deterministic nite automata accept exactly the
same set of languages.
Slides modied by Benny Chor, based on original slides by Maurice Herlihy, Brown University. p.11
Why Non-Determinism?
Theorem (to be proved soon): Deterministic and
non-deterministic nite automata accept exactly the
same set of languages.
Q.: So why do we need them?
Slides modied by Benny Chor, based on original slides by Maurice Herlihy, Brown University. p.11
Why Non-Determinism?
Theorem (to be proved soon): Deterministic and
non-deterministic nite automata accept exactly the
same set of languages.
Q.: So why do we need them?
A.: NFAs are usually easier to design than equivalent
DFAs.
Slides modied by Benny Chor, based on original slides by Maurice Herlihy, Brown University. p.11
Why Non-Determinism?
Theorem (to be proved soon): Deterministic and
non-deterministic nite automata accept exactly the
same set of languages.
Q.: So why do we need them?
A.: NFAs are usually easier to design than equivalent
DFAs.
Example: Design a nite automaton that accepts all
strings with a 1 in their third-to-the-last position?
Slides modied by Benny Chor, based on original slides by Maurice Herlihy, Brown University. p.11
Solving with DFA
1
q
000
q
111
q
001
q
010
q
011
q
100
q
101
q
110
0
0 0
0
0 0 0
1 1
1
1 1 1
Slides modied by Benny Chor, based on original slides by Maurice Herlihy, Brown University. p.12
Solving with DFA
1
q
000
q
111
q
001
q
010
q
011
q
100
q
101
q
110
0
0 0
0
0 0 0
1 1
1
1 1 1
(oops, there are two errors:
Slides modied by Benny Chor, based on original slides by Maurice Herlihy, Brown University. p.12
Solving with DFA
1
q
000
q
111
q
001
q
010
q
011
q
100
q
101
q
110
0
0 0
0
0 0 0
1 1
1
1 1 1
(oops, there are two errors: q
101
should be an accept
state, and there should be a 0" labeled arrow from
q
011
to q
110
, but overall it is OK.)
Slides modied by Benny Chor, based on original slides by Maurice Herlihy, Brown University. p.12
Solving with NFA
q
4
q
1
q
2
q
3
1
0,1
0,1 0,1
Guesses which symbol is third from the last,
and
checks that indeed it is a 1.
Slides modied by Benny Chor, based on original slides by Maurice Herlihy, Brown University. p.13
NFA Formal Denition
Transition function is going to be different.
P(Q) is the powerset of Q.
= {}.
Slides modied by Benny Chor, based on original slides by Maurice Herlihy, Brown University. p.14
NFA Formal Denition
Transition function is going to be different.
P(Q) is the powerset of Q.
= {}.
A non-deterministic nite automaton is a 5-tuple
(Q, , , q
0
, F), where
Q is a nite set called the states,
is a nite set called the alphabet,
: Q
.
u be the string over obtained from w by
omitting all occurances of .
Slides modied by Benny Chor, based on original slides by Maurice Herlihy, Brown University. p.16
Formal Model of Computation
Let M = (Q, , , q
0
, F) be an NFA, and
w be a string over
.
u be the string over obtained from w by
omitting all occurances of .
Suppose there is a sequence of states (in Q),
r
0
, . . . , r
n
, such that
r
0
= q
0
r
i+1
(r
i
, y
i+1
), 0 i < n
r
n
F
Slides modied by Benny Chor, based on original slides by Maurice Herlihy, Brown University. p.16
Formal Model of Computation
Let M = (Q, , , q
0
, F) be an NFA, and
w be a string over
.
u be the string over obtained from w by
omitting all occurances of .
Suppose there is a sequence of states (in Q),
r
0
, . . . , r
n
, such that
r
0
= q
0
r
i+1
(r
i
, y
i+1
), 0 i < n
r
n
F
Then we say that M accepts u.
Slides modied by Benny Chor, based on original slides by Maurice Herlihy, Brown University. p.16
Equivalence of NFAs and DFAs
Given an an NFA, N, we construct a DFA, M,
that accepts the same language.
Slides modied by Benny Chor, based on original slides by Maurice Herlihy, Brown University. p.17
Equivalence of NFAs and DFAs
Given an an NFA, N, we construct a DFA, M,
that accepts the same language.
To begin with, we make things easier by ignoring
transitions.
Slides modied by Benny Chor, based on original slides by Maurice Herlihy, Brown University. p.17
Equivalence of NFAs and DFAs
Given an an NFA, N, we construct a DFA, M,
that accepts the same language.
To begin with, we make things easier by ignoring
transitions.
Make DFA simulate all possible NFA states.
Slides modied by Benny Chor, based on original slides by Maurice Herlihy, Brown University. p.17
Equivalence of NFAs and DFAs
Given an an NFA, N, we construct a DFA, M,
that accepts the same language.
To begin with, we make things easier by ignoring
transitions.
Make DFA simulate all possible NFA states.
As consequence of the construction, if the NFA
has k states, the DFA has 2
k
states.
Slides modied by Benny Chor, based on original slides by Maurice Herlihy, Brown University. p.17
Equivalence of NFAs and DFAs
Let N = (Q, , , q
0
, F) be the NFA accepting A.
Construct a DFA M = (Q
, ,
, q
0
, F
).
Q
= P(Q).
Slides modied by Benny Chor, based on original slides by Maurice Herlihy, Brown University. p.18
Equivalence of NFAs and DFAs
Let N = (Q, , , q
0
, F) be the NFA accepting A.
Construct a DFA M = (Q
, ,
, q
0
, F
).
Q
= P(Q).
For R Q
and a , let
, ,
, q
0
, F
).
Q
= P(Q).
For R Q
and a , let
0
= {q
0
}
Slides modied by Benny Chor, based on original slides by Maurice Herlihy, Brown University. p.18
Equivalence of NFAs and DFAs
Let N = (Q, , , q
0
, F) be the NFA accepting A.
Construct a DFA M = (Q
, ,
, q
0
, F
).
Q
= P(Q).
For R Q
and a , let
0
= {q
0
}
F
= {R Q
Slides modied by Benny Chor, based on original slides by Maurice Herlihy, Brown University. p.18
Dealing with -Transitions
For any state R of M, dene E(R) to be the collection
of states reachable from R by transitions only.
E(R) = {q Q|q can be reached from some r R
by 0 or more transitions}
Slides modied by Benny Chor, based on original slides by Maurice Herlihy, Brown University. p.19
Dealing with -Transitions
For any state R of M, dene E(R) to be the collection
of states reachable from R by transitions only.
E(R) = {q Q|q can be reached from some r R
by 0 or more transitions}
Dene transition function:
0
= E({q
0
})
Slides modied by Benny Chor, based on original slides by Maurice Herlihy, Brown University. p.19
Regular Languages, Revisited
By denition, a language is regular if it is accepted by
some DFA.
Slides modied by Benny Chor, based on original slides by Maurice Herlihy, Brown University. p.20
Regular Languages, Revisited
By denition, a language is regular if it is accepted by
some DFA.
Corollary: A language is regular if and only if it is
accepted by some NFA.
Slides modied by Benny Chor, based on original slides by Maurice Herlihy, Brown University. p.20
Regular Languages, Revisited
By denition, a language is regular if it is accepted by
some DFA.
Corollary: A language is regular if and only if it is
accepted by some NFA.
This is an alternative way of characterizing regular
languages.
Slides modied by Benny Chor, based on original slides by Maurice Herlihy, Brown University. p.20
Regular Languages, Revisited
By denition, a language is regular if it is accepted by
some DFA.
Corollary: A language is regular if and only if it is
accepted by some NFA.
This is an alternative way of characterizing regular
languages.
We will now use the equivalence to show that regu-
lar languages are closed under the regular operations
(union, concatenation, star).
Slides modied by Benny Chor, based on original slides by Maurice Herlihy, Brown University. p.20
Closure Under Union (alternative proof)
N
1
N
2
Slides modied by Benny Chor, based on original slides by Maurice Herlihy, Brown University. p.21
Regular Languages Closed Under Union
N
1
N
2
Slides modied by Benny Chor, based on original slides by Maurice Herlihy, Brown University. p.22
Regular Languages Closed Under Union
Suppose
N
1
= (Q
1
, ,
1
, q
1
, F
1
) accept L
1
, and
N
2
= (Q
2
, ,
2
, q
2
, F
2
) accept L
2
.
Dene N = (Q, , , q
0
, F):
Q = q
0
Q
1
Q
2
is the same, q
0
is the start state
F = F
1
F
2
(q, a) =
1
(q, a) q Q
1
2
(q, a) q Q
2
{q
1
, q
2
} q = q
0
and a =
q = q
0
and a =
Slides modied by Benny Chor, based on original slides by Maurice Herlihy, Brown University. p.23
Regular Languages Closed Under
Concatenation
N
2
N
1
Slides modied by Benny Chor, based on original slides by Maurice Herlihy, Brown University. p.24
Regular Languages
Closed Under Concatenation
N
2
N
1
Slides modied by Benny Chor, based on original slides by Maurice Herlihy, Brown University. p.25
Regular Languages
Closed Under Concatenation
Suppose
N
1
= (Q
1
, ,
1
, q
1
, F
1
) accept L
1
, and
N
2
= (Q
2
, ,
2
, q
2
, F
2
) accept L
2
.
Dene N = (Q, , , q
1
, F
2
):
Q = Q
1
Q
2
Slides modied by Benny Chor, based on original slides by Maurice Herlihy, Brown University. p.26
Regular Languages
Closed Under Concatenation
Suppose
N
1
= (Q
1
, ,
1
, q
1
, F
1
) accept L
1
, and
N
2
= (Q
2
, ,
2
, q
2
, F
2
) accept L
2
.
Dene N = (Q, , , q
1
, F
2
):
Q = Q
1
Q
2
q
1
is the start state of N
Slides modied by Benny Chor, based on original slides by Maurice Herlihy, Brown University. p.26
Regular Languages
Closed Under Concatenation
Suppose
N
1
= (Q
1
, ,
1
, q
1
, F
1
) accept L
1
, and
N
2
= (Q
2
, ,
2
, q
2
, F
2
) accept L
2
.
Dene N = (Q, , , q
1
, F
2
):
Q = Q
1
Q
2
q
1
is the start state of N
F
2
is the set of accept states of N
(q, a) =
1
(q, a) q Q
1
and q / F
1
1
(q, a) q Q
1
and a =
1
(q, a) {q
2
} q F
1
and a =
2
(q, a) q Q
2
Slides modied by Benny Chor, based on original slides by Maurice Herlihy, Brown University. p.26
Regular Languages Closed Under Star
N
1
Slides modied by Benny Chor, based on original slides by Maurice Herlihy, Brown University. p.27
Regular Languages Closed Under Star
N
1
N
1
Slides modied by Benny Chor, based on original slides by Maurice Herlihy, Brown University. p.27
Regular Languages Closed Under Star
N
1
N
1
Slides modied by Benny Chor, based on original slides by Maurice Herlihy, Brown University. p.28
Regular Languages Closed Under Star
N
1
accepts R
1
. Wanna build NFA for R = (R
1
)
GG
gg
GG
`
GG _
_
?
?
?
Slides modied by Benny Chor, based on original slides by Maurice Herlihy, Brown University. p.28
Regular Languages Closed Under Star
N
1
accepts R
1
. Wanna build NFA for R = (R
1
)
GG
gg
GG
`
GG _
_
?
?
?
GG ` _ \
q
0
GG
gg
GG
`
GG _
_
?
?
?
`
Slides modied by Benny Chor, based on original slides by Maurice Herlihy, Brown University. p.28
Regular Languages Closed Under Star
N
1
accepts R
1
. Wanna build NFA for R = (R
1
)
GG
gg
GG
`
GG _
_
?
?
?
GG ` _ \
q
0
GG
gg
GG
`
GG _
_
?
?
?
`
(q, a) =
1
(q, a) q Q
1
and q / F
1
1
(q, a) q F
1
and a =
1
(q, ) {q
1
} q F
1
and a =
{q
1
} q = q
0
and a =
q = q
0
and a =
Slides modied by Benny Chor, based on original slides by Maurice Herlihy, Brown University. p.29
Summary
Regular languages are closed under
Slides modied by Benny Chor, based on original slides by Maurice Herlihy, Brown University. p.30
Summary
Regular languages are closed under
union
Slides modied by Benny Chor, based on original slides by Maurice Herlihy, Brown University. p.30
Summary
Regular languages are closed under
union
concatenation
Slides modied by Benny Chor, based on original slides by Maurice Herlihy, Brown University. p.30
Summary
Regular languages are closed under
union
concatenation
star
Slides modied by Benny Chor, based on original slides by Maurice Herlihy, Brown University. p.30
Summary
Regular languages are closed under
union
concatenation
star
Non-deterministic nite automata
Slides modied by Benny Chor, based on original slides by Maurice Herlihy, Brown University. p.30
Summary
Regular languages are closed under
union
concatenation
star
Non-deterministic nite automata
are equivalent to deterministic nite automata
Slides modied by Benny Chor, based on original slides by Maurice Herlihy, Brown University. p.30
Summary
Regular languages are closed under
union
concatenation
star
Non-deterministic nite automata
are equivalent to deterministic nite automata
but much easier to use in some proofs and
constructions.
Slides modied by Benny Chor, based on original slides by Maurice Herlihy, Brown University. p.30
Regular Expressions
A notation for building up languages by describing
them as expressions, e.g. (0 1)0
.
0 and 1 are shorthand for {0} and {1}
Slides modied by Benny Chor, based on original slides by Maurice Herlihy, Brown University. p.31
Regular Expressions
A notation for building up languages by describing
them as expressions, e.g. (0 1)0
.
0 and 1 are shorthand for {0} and {1}
so (0 1) = {0, 1}.
Slides modied by Benny Chor, based on original slides by Maurice Herlihy, Brown University. p.31
Regular Expressions
A notation for building up languages by describing
them as expressions, e.g. (0 1)0
.
0 and 1 are shorthand for {0} and {1}
so (0 1) = {0, 1}.
0
.
Slides modied by Benny Chor, based on original slides by Maurice Herlihy, Brown University. p.31
Regular Expressions
A notation for building up languages by describing
them as expressions, e.g. (0 1)0
.
0 and 1 are shorthand for {0} and {1}
so (0 1) = {0, 1}.
0
.
concatenation, like multiplication, is implicit, so
0
10
.
0 and 1 are shorthand for {0} and {1}
so (0 1) = {0, 1}.
0
.
concatenation, like multiplication, is implicit, so
0
10
stand for?
Slides modied by Benny Chor, based on original slides by Maurice Herlihy, Brown University. p.31
Regular Expressions
A notation for building up languages by describing
them as expressions, e.g. (0 1)0
.
0 and 1 are shorthand for {0} and {1}
so (0 1) = {0, 1}.
0
.
concatenation, like multiplication, is implicit, so
0
10
stand for?
Remark: Regular expressions are often used in text
editors or shell scripts.
Slides modied by Benny Chor, based on original slides by Maurice Herlihy, Brown University. p.31
More Examples
Let be an alphabet.
The regular expression is the language of
one-symbol strings.
is all strings.
is all strings.
Slides modied by Benny Chor, based on original slides by Maurice Herlihy, Brown University. p.33
Regular Expressions Formal Denition
Syntax: R is a regular expression if R is of form
a for some a
Slides modied by Benny Chor, based on original slides by Maurice Herlihy, Brown University. p.33
Regular Expressions Formal Denition
Syntax: R is a regular expression if R is of form
a for some a
(R
1
R
2
) for regular expressions R
1
and R
2
Slides modied by Benny Chor, based on original slides by Maurice Herlihy, Brown University. p.33
Regular Expressions Formal Denition
Syntax: R is a regular expression if R is of form
a for some a
(R
1
R
2
) for regular expressions R
1
and R
2
(R
1
R
2
) for regular expressions R
1
and R
2
Slides modied by Benny Chor, based on original slides by Maurice Herlihy, Brown University. p.33
Regular Expressions Formal Denition
Syntax: R is a regular expression if R is of form
a for some a
(R
1
R
2
) for regular expressions R
1
and R
2
(R
1
R
2
) for regular expressions R
1
and R
2
(R
1
) for regular expression R
1
Slides modied by Benny Chor, based on original slides by Maurice Herlihy, Brown University. p.33
Regular Expressions Formal Denition
Let L(R) be the language denoted by regular
expression R.
R L(R)
a {a}
{}
(R
1
R
2
) L(R
1
) L(R
2
)
(R
1
R
2
) L(R
1
) L(R
2
)
(R
1
)
L(R
1
)
Slides modied by Benny Chor, based on original slides by Maurice Herlihy, Brown University. p.34
Regular Expressions Formal Denition
Let L(R) be the language denoted by regular
expression R.
R L(R)
a {a}
{}
(R
1
R
2
) L(R
1
) L(R
2
)
(R
1
R
2
) L(R
1
) L(R
2
)
(R
1
)
L(R
1
)
L(R
1
)
N
2
N
1
R = (R
1
R
2
) R = (R
1
R
2
)
R = (R
1
)
GG ` _ \
q
0
GG
gg
GG
`
GG _
_
?
?
?
`
Slides modied by Benny Chor, based on original slides by Maurice Herlihy, Brown University. p.37
Approximately (?) Correct Example
a
a
b
b
a b
ab
a b
a
ab
U
a
Slides modied by Benny Chor, based on original slides by Maurice Herlihy, Brown University. p.38
Regular Expression from an NFA (=)
We now dene generalized non-deterministic nite
automata (GNFA).
Slides modied by Benny Chor, based on original slides by Maurice Herlihy, Brown University. p.39
Regular Expression from an NFA (=)
We now dene generalized non-deterministic nite
automata (GNFA).
An NFA:
Each transition labeled with a symbol or ,
reads zero or one symbols,
takes matching transition, if any.
Slides modied by Benny Chor, based on original slides by Maurice Herlihy, Brown University. p.39
Regular Expression from an NFA (=)
We now dene generalized non-deterministic nite
automata (GNFA).
An NFA:
Each transition labeled with a symbol or ,
reads zero or one symbols,
takes matching transition, if any.
A GNFA:
Each transition labeled with a regular expression,
reads zero or more symbols,
takes transition whose regular expression matches
string, if any.
Slides modied by Benny Chor, based on original slides by Maurice Herlihy, Brown University. p.39
Regular Expression from an NFA (=)
We now dene generalized non-deterministic nite
automata (GNFA).
An NFA:
Each transition labeled with a symbol or ,
reads zero or one symbols,
takes matching transition, if any.
A GNFA:
Each transition labeled with a regular expression,
reads zero or more symbols,
takes transition whose regular expression matches
string, if any.
GNFAs are natural generalization of NFAs.
Slides modied by Benny Chor, based on original slides by Maurice Herlihy, Brown University. p.39
GNFA Special Form
Start state has outgoing arrows to every other
state, but no incoming arrows.
Slides modied by Benny Chor, based on original slides by Maurice Herlihy, Brown University. p.40
GNFA Special Form
Start state has outgoing arrows to every other
state, but no incoming arrows.
Unique accept state has incoming arrows from
every other state, but no outgoing arrows
Slides modied by Benny Chor, based on original slides by Maurice Herlihy, Brown University. p.40
GNFA Special Form
Start state has outgoing arrows to every other
state, but no incoming arrows.
Unique accept state has incoming arrows from
every other state, but no outgoing arrows
Except for start and accept states, arrows goes
from every state to every other state, including
itself.
Slides modied by Benny Chor, based on original slides by Maurice Herlihy, Brown University. p.40
GNFA Special Form
Start state has outgoing arrows to every other
state, but no incoming arrows.
Unique accept state has incoming arrows from
every other state, but no outgoing arrows
Except for start and accept states, arrows goes
from every state to every other state, including
itself.
Easy to transform any GNFA into special form.
Slides modied by Benny Chor, based on original slides by Maurice Herlihy, Brown University. p.40
GNFA Special Form
Start state has outgoing arrows to every other
state, but no incoming arrows.
Unique accept state has incoming arrows from
every other state, but no outgoing arrows
Except for start and accept states, arrows goes
from every state to every other state, including
itself.
Easy to transform any GNFA into special form.
Really? How? . . .
Slides modied by Benny Chor, based on original slides by Maurice Herlihy, Brown University. p.40
Converting DFA to Regular Expression
(=)
Strategy sequence of equivalent transformations
given a k-state DFA
Slides modied by Benny Chor, based on original slides by Maurice Herlihy, Brown University. p.41
Converting DFA to Regular Expression
(=)
Strategy sequence of equivalent transformations
given a k-state DFA
transform into (k + 2)-state GNFA
Slides modied by Benny Chor, based on original slides by Maurice Herlihy, Brown University. p.41
Converting DFA to Regular Expression
(=)
Strategy sequence of equivalent transformations
given a k-state DFA
transform into (k + 2)-state GNFA
while GNFA has more than 2 states, transform it
into equivalent GNFA with one fewer state
Slides modied by Benny Chor, based on original slides by Maurice Herlihy, Brown University. p.41
Converting DFA to Regular Expression
(=)
Strategy sequence of equivalent transformations
given a k-state DFA
transform into (k + 2)-state GNFA
while GNFA has more than 2 states, transform it
into equivalent GNFA with one fewer state
eventually reach 2-state GNFA (states are just
start and accept).
Slides modied by Benny Chor, based on original slides by Maurice Herlihy, Brown University. p.41
Converting DFA to Regular Expression
(=)
Strategy sequence of equivalent transformations
given a k-state DFA
transform into (k + 2)-state GNFA
while GNFA has more than 2 states, transform it
into equivalent GNFA with one fewer state
eventually reach 2-state GNFA (states are just
start and accept).
label on single transition is the desired regular
expression.
Slides modied by Benny Chor, based on original slides by Maurice Herlihy, Brown University. p.41
Converting Strategy (=)
3-state
DFA
5-state
GNFA
4-state
GNFA
3-state
GNFA
2-state
GNFA
regular
expression
Slides modied by Benny Chor, based on original slides by Maurice Herlihy, Brown University. p.42
Removing One State
We remove one state q
r
, and then repair the machine
by altering regular expression of other transitions.
q
i j
q
q
r
R
1
3
R
4
R
q
i
j
q
2
R
2
R* R
1 3
R U
4
R
Slides modied by Benny Chor, based on original slides by Maurice Herlihy, Brown University. p.43
Formal Treatment GNDA Denition
q
s
is start state.
q
a
is accept state.
Ris collection of regular expressions over .
Slides modied by Benny Chor, based on original slides by Maurice Herlihy, Brown University. p.44
Formal Treatment GNDA Denition
q
s
is start state.
q
a
is accept state.
Ris collection of regular expressions over .
The transition function is
: (Q{q
a
}) (Q{q
s
}) R
Slides modied by Benny Chor, based on original slides by Maurice Herlihy, Brown University. p.44
Formal Treatment GNDA Denition
q
s
is start state.
q
a
is accept state.
Ris collection of regular expressions over .
The transition function is
: (Q{q
a
}) (Q{q
s
}) R
If (q
i
, q
j
) = R, then arrow from q
i
to q
j
has label R.
Slides modied by Benny Chor, based on original slides by Maurice Herlihy, Brown University. p.44
Formal Treatment GNDA Denition
q
s
is start state.
q
a
is accept state.
Ris collection of regular expressions over .
The transition function is
: (Q{q
a
}) (Q{q
s
}) R
If (q
i
, q
j
) = R, then arrow from q
i
to q
j
has label R.
Arrows connect every state to every other state except:
no arrow from q
a
no arrow to q
s
Slides modied by Benny Chor, based on original slides by Maurice Herlihy, Brown University. p.44
Formal Denition
A generalized deterministic nite automaton (GDFA)
is (Q, , , q
s
, q
a
), where
Q is a nite set of states,
is the alphabet,
: (Q{q
a
}) (Q{q
s
}) Ris the
transition function.
q
s
Q is the start state, and
q
a
Q is the unique accept state.
Slides modied by Benny Chor, based on original slides by Maurice Herlihy, Brown University. p.45
A Formal Model of GNFA Computation
A GNFA accepts a string w
if there exists a
parsing of w, w = w
1
w
2
w
k
, where each w
i
,
and there exists a sequence of states q
0
, . . . , q
k
such
that
q
0
= q
s
, the start state,
q
k
= q
a
, the accept state, and
for each i, w
i
L(R
i
), where R
i
= (q
i1
, q
i
).
(namely w
i
is an element of the language
described by the regular expression R
i
.)
Slides modied by Benny Chor, based on original slides by Maurice Herlihy, Brown University. p.46
The CONVERT Algorithm
Given GDFA G, convert it to equivalent GNFA G
.
let k be the number of states of G.
Slides modied by Benny Chor, based on original slides by Maurice Herlihy, Brown University. p.47
The CONVERT Algorithm
Given GDFA G, convert it to equivalent GNFA G
.
let k be the number of states of G.
If k = 2, return the regular expression labeling
the only arrow.
Slides modied by Benny Chor, based on original slides by Maurice Herlihy, Brown University. p.47
The CONVERT Algorithm
Given GDFA G, convert it to equivalent GNFA G
.
let k be the number of states of G.
If k = 2, return the regular expression labeling
the only arrow.
If k > 2, select any q
r
distinct from q
s
and q
a
.
Slides modied by Benny Chor, based on original slides by Maurice Herlihy, Brown University. p.47
The CONVERT Algorithm
Given GDFA G, convert it to equivalent GNFA G
.
let k be the number of states of G.
If k = 2, return the regular expression labeling
the only arrow.
If k > 2, select any q
r
distinct from q
s
and q
a
.
Let Q
= Q{q
r
} .
Slides modied by Benny Chor, based on original slides by Maurice Herlihy, Brown University. p.47
The CONVERT Algorithm
Given GDFA G, convert it to equivalent GNFA G
.
let k be the number of states of G.
If k = 2, return the regular expression labeling
the only arrow.
If k > 2, select any q
r
distinct from q
s
and q
a
.
Let Q
= Q{q
r
} .
For any q
i
Q
{q
a
} and q
j
Q
{q
s
}, let
Slides modied by Benny Chor, based on original slides by Maurice Herlihy, Brown University. p.47
The CONVERT Algorithm
Given GDFA G, convert it to equivalent GNFA G
.
let k be the number of states of G.
If k = 2, return the regular expression labeling
the only arrow.
If k > 2, select any q
r
distinct from q
s
and q
a
.
Let Q
= Q{q
r
} .
For any q
i
Q
{q
a
} and q
j
Q
{q
s
}, let
R
1
= (q
i
, q
r
), R
2
= (q
r
, q
r
),
Slides modied by Benny Chor, based on original slides by Maurice Herlihy, Brown University. p.47
The CONVERT Algorithm
Given GDFA G, convert it to equivalent GNFA G
.
let k be the number of states of G.
If k = 2, return the regular expression labeling
the only arrow.
If k > 2, select any q
r
distinct from q
s
and q
a
.
Let Q
= Q{q
r
} .
For any q
i
Q
{q
a
} and q
j
Q
{q
s
}, let
R
1
= (q
i
, q
r
), R
2
= (q
r
, q
r
),
R
3
= (q
r
, q
j
), and R
4
= (q
i
, q
j
).
Slides modied by Benny Chor, based on original slides by Maurice Herlihy, Brown University. p.47
The CONVERT Algorithm
Given GDFA G, convert it to equivalent GNFA G
.
let k be the number of states of G.
If k = 2, return the regular expression labeling
the only arrow.
If k > 2, select any q
r
distinct from q
s
and q
a
.
Let Q
= Q{q
r
} .
For any q
i
Q
{q
a
} and q
j
Q
{q
s
}, let
R
1
= (q
i
, q
r
), R
2
= (q
r
, q
r
),
R
3
= (q
r
, q
j
), and R
4
= (q
i
, q
j
).
Dene
(q
i
, q
j
) = (R
1
)(R
2
)
(R
3
) (R
4
).
Slides modied by Benny Chor, based on original slides by Maurice Herlihy, Brown University. p.47
The CONVERT Algorithm
Given GDFA G, convert it to equivalent GNFA G
.
let k be the number of states of G.
If k = 2, return the regular expression labeling
the only arrow.
If k > 2, select any q
r
distinct from q
s
and q
a
.
Let Q
= Q{q
r
} .
For any q
i
Q
{q
a
} and q
j
Q
{q
s
}, let
R
1
= (q
i
, q
r
), R
2
= (q
r
, q
r
),
R
3
= (q
r
, q
j
), and R
4
= (q
i
, q
j
).
Dene
(q
i
, q
j
) = (R
1
)(R
2
)
(R
3
) (R
4
).
Denote the resulting k 1 states GNFA by G
.
Slides modied by Benny Chor, based on original slides by Maurice Herlihy, Brown University. p.47
The CONVERT Procedure
We dene the recursive procedure CONVERT():
Given GDFA G.
Let k be the number of states of G.
Slides modied by Benny Chor, based on original slides by Maurice Herlihy, Brown University. p.48
The CONVERT Procedure
We dene the recursive procedure CONVERT():
Given GDFA G.
Let k be the number of states of G.
If k = 2, return the regular expression labeling
the only arrow of G.
Slides modied by Benny Chor, based on original slides by Maurice Herlihy, Brown University. p.48
The CONVERT Procedure
We dene the recursive procedure CONVERT():
Given GDFA G.
Let k be the number of states of G.
If k = 2, return the regular expression labeling
the only arrow of G.
If k > 2, let G
).
Slides modied by Benny Chor, based on original slides by Maurice Herlihy, Brown University. p.48
Correctness Proof of Construction
Theorem: G and CONVERT(G) accept the same
language.
Proof: By induction on number of states of G
Slides modied by Benny Chor, based on original slides by Maurice Herlihy, Brown University. p.49
Correctness Proof of Construction
Theorem: G and CONVERT(G) accept the same
language.
Proof: By induction on number of states of G
Basis: When there are only 2 states, there is a single
label, which characterizes the strings accepted by G.
Slides modied by Benny Chor, based on original slides by Maurice Herlihy, Brown University. p.49
Correctness Proof of Construction
Theorem: G and CONVERT(G) accept the same
language.
Proof: By induction on number of states of G
Basis: When there are only 2 states, there is a single
label, which characterizes the strings accepted by G.
Induction Step: Assume claim for k 1 states, prove
for k.
Slides modied by Benny Chor, based on original slides by Maurice Herlihy, Brown University. p.49
Correctness Proof of Construction
Theorem: G and CONVERT(G) accept the same
language.
Proof: By induction on number of states of G
Basis: When there are only 2 states, there is a single
label, which characterizes the strings accepted by G.
Induction Step: Assume claim for k 1 states, prove
for k.
Let G
and CONVERT(G
)
accept the same language.
Slides modied by Benny Chor, based on original slides by Maurice Herlihy, Brown University. p.50
G and G
and CONVERT(G
)
accept the same language.
On input G, the procedure returns CONVERT(G
).
Slides modied by Benny Chor, based on original slides by Maurice Herlihy, Brown University. p.50
G and G
and CONVERT(G
)
accept the same language.
On input G, the procedure returns CONVERT(G
).
So to complete the proof, it sufces to show that G
and G
and CONVERT(G
)
accept the same language.
On input G, the procedure returns CONVERT(G
).
So to complete the proof, it sufces to show that G
and G
.
2. If G
are equivalent.
Slides modied by Benny Chor, based on original slides by Maurice Herlihy, Brown University. p.50
Step One
Claim: If G accepts w, then so does G
:
If G accepts w, then there exists a path of states
q
s
, q
1
, q
2
, . . . , q
a
traversed by G on w, leading to
the accept state q
a
.
Slides modied by Benny Chor, based on original slides by Maurice Herlihy, Brown University. p.51
Step One
Claim: If G accepts w, then so does G
:
If G accepts w, then there exists a path of states
q
s
, q
1
, q
2
, . . . , q
a
traversed by G on w, leading to
the accept state q
a
.
If q
r
does not appear on path, then G
accepts w
because the the new regular expression on each
edge of G
:
If G accepts w, then there exists a path of states
q
s
, q
1
, q
2
, . . . , q
a
traversed by G on w, leading to
the accept state q
a
.
If q
r
does not appear on path, then G
accepts w
because the the new regular expression on each
edge of G
(R
r,j
)
linking q
i
and q
j
encompasses any such string.
Slides modied by Benny Chor, based on original slides by Maurice Herlihy, Brown University. p.51
Step One
Claim: If G accepts w, then so does G
:
If G accepts w, then there exists a path of states
q
s
, q
1
, q
2
, . . . , q
a
traversed by G on w, leading to
the accept state q
a
.
If q
r
does not appear on path, then G
accepts w
because the the new regular expression on each
edge of G
(R
r,j
)
linking q
i
and q
j
encompasses any such string.
Either way, the claim holds.
Slides modied by Benny Chor, based on original slides by Maurice Herlihy, Brown University. p.51
Steps Two and Three
Claim: If G
corresponds
to a transition in G, either directly or through q
r
. Thus
if G
).
Slides modied by Benny Chor, based on original slides by Maurice Herlihy, Brown University. p.52
Steps Two and Three
Claim: If G
corresponds
to a transition in G, either directly or through q
r
. Thus
if G
).
Combined with the induction hypothesis, this
shows that G and the regular expression
CONVERT(G) accept the same language.
Slides modied by Benny Chor, based on original slides by Maurice Herlihy, Brown University. p.52
Steps Two and Three
Claim: If G
corresponds
to a transition in G, either directly or through q
r
. Thus
if G
).
Combined with the induction hypothesis, this
shows that G and the regular expression
CONVERT(G) accept the same language.
This, in turn, proves our remarkable claim:
A language, L, is described by a regular
expression, R, if and only if L is regular.
Slides modied by Benny Chor, based on original slides by Maurice Herlihy, Brown University. p.52
Conversion Example
We now construct a simple, 2 state DFA that
accepts the language over {0, 1} of all strings
with an even number of 1s.
Slides modied by Benny Chor, based on original slides by Maurice Herlihy, Brown University. p.53
Conversion Example
We now construct a simple, 2 state DFA that
accepts the language over {0, 1} of all strings
with an even number of 1s.
We followed the conversion through GNFAs to
translate this DFA (on the blackboard) into the
regular expression (0 10
1)
.
Slides modied by Benny Chor, based on original slides by Maurice Herlihy, Brown University. p.53