Beruflich Dokumente
Kultur Dokumente
1. Regular Languages
1.1 Finite Automata
Deterministic FA Non-Deterministic FA -NFA
Finite Automata
The simplest computational model, with very limited memory, but highly useful Example: a finite automaton modeling an on/off switch
push Start
off
push
on
State Diagram of FA
M1:
transitions
0 1
states
1
q1 q2
0
q3
0, 1
start state
accept state
M1 Contd
0
1
1
0
q1
q2
0, 1
q3
M1 Contd
0
1
1
0
q1
q2
0, 1
q3
M1 Contd
0
1
1
0
q1
q2
Formal Definition of FA
A finite automaton is defined by a 5tuple (Q, , , q0, F)
Q: finite set of states : finite alphabet : transition function, : Q x Q, takes a state and input symbol as arguments, and returns a state q0Q: start state FQ: set of accept states
0 1 q1 0
1 0 q2 q3
0, 1
1
q1 q1 q2 q2 q3 q2 q3 q2 q2
Extension of to Strings
Intuitively, an FA accepts a string w = a1a2an if there is a path in the state diagram that:
1. Begins at the start state, 2. Ends at an accept state, and 3. Has sequence of labels a1, a2, , an.
Formally, the transition function can be extended to *(q, w), where w is any string of input symbols.
Basis: *(q, ) = q Induction: *(q, wa) = (*(q, w), a)
Language of an FA
An FA M = (Q, , , q0, F) accepts a string w if *(q0, w) F. The language recognized by an FA M= (Q, , , q0, F) is
L(M) = {w | *(q0, w) F}.
Example
={0,1}, L = {w | w has odd number of 1s}, design a FA to recognize L.
What is the necessary information to remember? --- Is the number of 1s seen so far even or odd? Two possibilities.
Example
={0,1}, L = {w | w has odd number of 1s}, design a FA to recognize L.
What is the necessary information to remember? --- Is the number of 1s seen so far even or odd? Two possibilities. Assign a state to each possibility.
qeven
qodd
Example
={0,1}, L = {w | w has odd number of 1s}, design a FA to recognize L.
What is the necessary information to remember? --- Is the number of 1s seen so far even or odd? Two possibilities. Assign a state to each possibility. Assign the transitions from one possibility to another upon reading a symbol.
0 1 qeven 1
qodd
Example
={0,1}, L = {w | w has odd number of 1s}, design a FA to recognize L.
What is the necessary information to remember? --- Is the number of 1s seen so far even or odd? Two possibilities. Assign a state to each possibility. Assign the transitions from one possibility to another upon reading a symbol. Set the start and accept states.
0 1 qeven 1
qodd
Exercise
={0,1}, L = {w | w has even number of 0s and even number of 1s}, design a FA to recognize L.
What to remember? How many possibilities?
Exercise
1 q00 0 q11 1 q01 0
1
1
q10
Example 1.9
={0,1}, L = {w | w contains 001 as a substring}, design a FA to recognize L.
four possibilities: 1. havent just seen any symbols of 001 --q 2. have just seen a 0 --- q0 3. have just seen a 00 --- q00 4. have seen the entire pattern 001 --- q001
Example 1.9
1 0 0, 1
q
1
q0
0 q00
0 1
q001
M2:
q0
q1
q2
q0
q0
q0
q0
q0
q1
q0
q1 q1 (die)
q2
Input:
(die) 0
q2
1
q0
0 1 {q0,q1} {q0}
q1 q2
{q2}
q0
q1
q2
Language of an NFA
Extension of to *(q, w):
Basis: *(q, ) = {q} Induction: *(q, wa) = p *(q, w)(p, a)
Subset Construction
Given an NFA N=(QN, , N, q0, FN), we will construct an DFA D=(QD, , D, {q0}, FD), such that L(D) = L(N). Subset construction:
QD = {S | S QN}, i.e. power set of QN D(S, a) = p S N(p, a) FD = {S | S F , S QD}
0
{q0} {q1} {q2} {q0,q1} {q0,q2} {q1,q2} {q0,q1 ,q2}
{q0}
{q0,q1}
0 1
{q0,q2}
Example NFA
Design an NFA to accept strings over alphabet {1, 2, 3} such that the last symbol appears previously, without any intervening higher symbol, e.g., 11, 21112, 312123.
1, 2, 3
1 p 2
q
r
1 2 t
3
1, 2 s
1 3
Equivalent DFA
32 possible subsets, 15 accessible: DFA is much larger than NFA
p pq pqt pr prt ps pst prs prst pqs
Proof: L(D)=L(N)
Induction on |w| to show that
*D({q0}, w) = *N(q0, w) Basis: w=, the claim follows from the def. Induction: *D({q0}, wa) = D(*D({q0}, w), a) = D(*N(q0, w), a) = p* (q0, w)N(p, a) = *N(q0, wa)
N
Example of an -NFA
0 1 q 0 1 r s
Elimination of -Transitions
-transitions are a convenience, but do not increase the power of FA's. To eliminate transitions:
1. Compute the transitive closure of the -arcs only. 2. If a state p can reach state q by -arcs, and there is a transition from q to r on input a (not ), then add a transition from p to r on input a. 3. Make state p an accept state if p can reach some accept state q by -arcs. 4. Remove all -transitions.
-CLOSURE
-CLOSURE(q): all states reachable from q by a sequence
q-CLOSURE(q); p-CLOSURE(q) and r(p, ) r-CLOSURE(q)
Example
1. -CLOSURE(q)={q}, -CLOSURE(r)={r,s}, -CLOSURE(s)={r,s}
1
0
Example
1. 2. -CLOSURE(q)={q}, -CLOSURE(r)={r,s}, -CLOSURE(s)={r,s} Add (s, 0)={q}, (r, 1)={q}
1
0, 1
0, 1
Example
1. 2. 3. -CLOSURE(q)={q}, -CLOSURE(r)={r,s}, -CLOSURE(s)={r,s} Add (s, 0)={q}, (r, 1)={q} Add r into F as an accept state
1
0, 1
0, 1
Example
1. 2. 3. 4. -CLOSURE(q)={q}, -CLOSURE(r)={r,s}, -CLOSURE(s)={r,s} Add (s, 0)={q}, (r, 1)={q} Add r into F as an accept state Remove (s, )={r}, (r, )={s}
0
1
0, 1
0, 1
Example 1.21
a b a,b
Assignment 1
Exercise 1.4: (a)(b)(g)(i)(j) Exercise 1.5: (b)(c) Exercise 1.12