Title: CS 3240: Languages and Computation
1CS 3240 Languages and Computation
- Nondeterministic Finite Automata andEquivalence
with Regular Expressions
2Review Regular Expressions
- Symbols and alphabet
- A symbol is a valid character in a language
- Alphabet is set of legal symbols
- Typically denoted as ?
- Metacharacters/metasymbols
- Defining reg-ex operations
- Escape character (\)
- Empty string ? and empty set ?
- Basic regular expressions
- Basic operations union, concatenation, repetition
3Review DFA
- A DFA is a five-tuple consisting of
- Alphabet ??
- A set of states Q
- A transition function d Q??? ? Q
- One start state q0
- One or more accepting states F ? Q
- A language accepted by a DFA is the set of
strings such that DFA ends at an accepting state
after processing the string
4Outline
- Nondeterministic Finite Automata (NFA)
- Equivalence between DFA and NFA
- Closure properties of regular languages
- Equivalence between regular expressions DFAs, and
NFAs
5NFA
- Nondeterministic Finite Automaton
- Same input may produce multiple paths
- Allows transition with an empty string or
transition from one state to different states
given a character
q2
1
?
q1
q1
q2
q3
1
empty string transition
nondeterministic transition
6Example
- Accept strings containing either 101 or 11 as a
substring
7Another Example
- Accept strings containing a 1 in the third
position from the back
8How does NFA work?
- Start in start state
- If any ? transitions, clone a machine for each
- Read a symbol, clone a machine for each matching
transition - If a symbol is read and there is no way to exit
from a state, then that machine dies - At end of input if any machine accepts then accept
9Example Read 010110
10Read 010110
11Definition NFA
- A nondeterministic finite automaton (NFA) is
defined by a 5-tuple, with - Alphabet ??
- A set of states Q
- A transition function d Q ?Se? P(Q)
- One start state q0
- One or more accepting states F ? Q
- Notation Se S?e P(Q) is power set of Q
- What is the difference from DFA?
12Nondeterministic Transition
- The function d Q ?Se? P(Q) is the key
difference! - When reading symbol a while in state q, it may
go to one of the states in d(q,a)?Q. - e in Se allows empty-string transitions
13Languages
- Language accepted by a NFA is the set of strings
such that NFA ends at an accepting state - Each string of language is c1c2cn with ci?Se
(possibly with e) - States are qi ? d(qi-1,ci) for i1n
- qn is an accepting state
14DFA NFA
- Theorem For every language L accepted by an NFA,
there is a DFA that accepts L. - In other words, DFA and NFA are equivalent
computational models. - Proof idea When keeping track of a
nondeterministic computation of an NFA N, use
many fingers to point at the subset ? Q of
states of N that can be reached on a given input
string. We can simulate this computation with a
DFA M with state space P(Q).
15Proof of DNANFA
- More formal proof Let A be the language
recognized by the NFA N (Q,S,d,q0,F). Define
the DFA M (Q,S,d,q0,F) by - Q P(Q)
- d(R,a) q?Q q?d(r,a) for an r?R
- q0 q0
- F R?Q R contains an accept state of N
- Example
16Proof of DNANFA Contd
- This construction almost works, except for the
e-transitions - Solution Define- E(R) q q reachable from
R using e steps - d(R,a) q?Q
q?E(d(r,a)) for an r?R - q0 E(q0) - DFA M accepts the same language as N.
- Therefore, a language is regular if and only if
it is accepted by an NFA.
17NFA and Regular Expressions
18Regular Operations
- For languages L1 and L2, we define the following
regular operations - Union L1?L2 x x?L1 or x?L2
- Concatenation L1L2 xy x?L1 and y?L2
- Star L1 x1xk xj?L1 for all 1jk, k0
- Example L100,11, L20,1L1?L2
0,1,00,11L1L2 000,001,110,111L1
e,00,11,0000,0011,1100,1111,000000, - Recall definition of regular expressions
19Closure under Union
- The regular languages are closed under the
union operation. In other words, if L1 and L2 are
regular languages, then so is L1 ? L2. - Proof idea Construct a third automaton M3 such
that L(M3) L1 ? L2.
20Union N N1 ? N2
N1
N2
21Closure under Concatenation
- The regular languages are closed under
concatenation. In other words, if L1 and L2 are
regular, then so is L1L2. - Proof idea Construct a third automaton M3 such
that L(M3) L1 L2.
22Concatenation N N1N2
23Closure under Star N N1
N1
24Regular Language Regular Expression
- As the names suggest, the following result holds
- Theorem A language is regular if and only if
some regular expression describes it. - Lemma If a language is described by a regular
expression, then it is regular. (This follows
from the closure properties of regular
languages). - Lemma If a language is regular, then it can be
described by a regular expression. - (This is harder and requires the notion of
Generalized Nondeterministic Finite Automata
(GNFA)).
25Regular Language ?Regular Expression
- If a language is regular, then it can be
described by a regular expression. - Proof Idea Use generalized nondeterministic
finite automata where the labels on the
transition arrows are regular expressions. For
an R?R this GNFA recognizes L(R) - Given a NFA/GNFA, repeatedly reduce it into a
GNFA with a smaller number of states until only
two states left.
26Example GNFA
27Example GNFA
28Definition Generalized NFA
- A Generalized nondeterministic finite automaton
(GNFA) is defined by M(Q, S, d, qstart,
qaccept) with - Q finite set of states
- S the input alphabet
- qstart the start state
- qaccept the accept state
- d(Q\qaccept)?(Q\qstart) ? R the transition
function - R is the set of regular expressions over S
29DFA M ? GNFA M
Let M have k states Qq1,,qk
- Add two states qaccept and qstart
30Rip Internal state of GNFA
- If the GNFA M has more than 2 states, rip
internal qrip to get equivalent GNFA M - - Remove state qrip QQ\qrip
- - Change the transition function d to d(qi,qj)
d(qi,qj) ? (d(qi,qrip)(d(qrip,qrip))d(qrip,qj))
for every qi?Q\qaccept and qj?Q\qstart