Title: CS 317 Automata
1CS 317 - Automata Formal Languages Notes 4
2Chapter 9 Regular Expressions and Finite
Automata
- Exam 1 Tuesday 9/29
-
- Exam review Thursday 9/24
3Chapter 9 Regular Expressions and Finite
Automata
- How to simplify regular expressions?
- For regular expressions a, ß
- If L(a) L(ß) then a and ß are equivalent.
Written a ß - Reflexive a a for all a
- Symmetric if a ß then ß a
- Transitive if a ß and ß ? then a ?
4Chapter 9 Regular Expressions and Finite
Automata
- Laws to simplify regular expressions
- a (ß ?) (a ß) ?
- a(ß?) (aß)?
- a ß ß a
- a ? a
- a? ?a ?
- a a a
- ea ae a
- a(ß ?) aß a?
- (a ß)? a? ß?
- e aa a
- e aa a
5Chapter 9 Regular Expressions and Finite
Automata
- Definition subset order
- a ß ? L(a) ? L(ß)
- ? L(a ß) L(ß)
- ? a ß ß
- Two more rules for regular expressions a, ß where
is subset order - ß a? ? ? aß ?
- ß ?a ? ? ßa ?
L(ß)
L(a)
or L(a) L(ß)
- Assume L(?) is infinite.
- Since ß ? and a? ? then we get aß a?.
- Since a? ? then a(a?) ? and repeating we get
a? ?
6Chapter 9 Regular Expressions and Finite
Automata
- Useful equations
- (aß)a a(ßa)
- (aß)a (ß a)
- a(ßa) (ß a)
- (e a) a
- aa aa
Example reg. expressions for 1.
b
a
q2
q1
a
a
b
q3
q2
q1
7Chapter 9 Regular Expressions and Finite
Automata
- Examples regular expressions
- (11 0)(00 1)
- Easy to make a NFA from this expression
- (1 01 001)(e 0 00)
- Rewrite as ((e 0 00)1) (e 0 00)
- ((e 0)(e 0)1)(e 0)(e 0)
- (110(00 000))
- Rewrite as (11000(e 0))
- (11000 110000)
8Chapter 9 Regular Expressions and Finite
Automata
- Converting FAs into reg. expressions
- Step 1 Make a final state for the new FA by
adding e-transition from every final state. If
the start state of FA is also final then make a
new start state using a e-transition to the
original start state. - Step 2 If FA has 1 state with 2 or more loops
with symbols a1,,ak then combine them into 1
loop labeled by a1 ak - Step 3 If FA has 1 state p with 2 or more exit
edges to a state q with symbols a1,,ak then
combine them into 1 exit edge going from p to q
and labeled by a1 ak
9- Converting FAs reg. into expressions
- Step 4 (state elimination and bypass operations)
If FA has state p with an exit edges to state q
on symbols a1 and state q has an exit edge to
state r labeled a2 then eliminate state q. Make
an exit edge from p to r labeled by a1a2
(concatenation). In this case q has no other
edges but the one to r. - If q has a simple self loop with symbol a3 then
label the new edge a1 a3a2 - If q has more than 1 exit edge then make a new
edge from p to each of these states before
eliminating q - Step 5 (special cases) If FA has state p with an
exit edges to state q on symbols a1 and state q
has an exit edge to state p labeled a2 and q has
a simple self loop with symbol a3 and r has a
exit edge that goes to q labeled a4 - Solution first make one self loop to p labeled
a1 a3a2 this eliminates the edge from p to q.
Now the other rules apply
10Example
- Make the regular expression
Eliminate q3
aba
a
a
q2
q1
e
a
a
q3
q2
b
b
q1
q4
b
b
b
abab
Combine loops
a
q2
q1
Step 1 new final state
a
q4
e
b
a
a
q3
q2
q1
Eliminate q2
e
a(abab)
b
b
q4
q1
b
q4
b
Eliminate q1
ba(abab)
11a
b
- Make the regular expression
b
a
b
q2
q4
q1
q3
a
b
Step 1 new final state
b
a
b
q2
q4
q1
q3
q5
e
e
a
Eliminate q4
bb
b
a
q2
q1
q3
q5
e
a
e
Eliminate q3
b(bb)e
a
q2
q5
q1
e
aa
Eliminate q2
ab(bb)
(aa)(a ab(bb))
q5
q1
ae
12- Make the regular expression
Step 1 new final state new start
state
Eliminate q3
13- Make the regular expression
Remove loops
Eliminate q2 and combine loops
(ba(bab)a)(ea(bab)ba)
q4
q0
14a
Eliminate q2
a(bb)a
e
q0
q1
ba
e
b(aa)b
ab
b
b
q5
a
q4
q3
a
Step 1 new final state new start
state
Eliminate q3
a(bb)a
e
q0
q1
baab
e
b(aa)b
q5
baab
q4
b(aa)b
a(bb)a
Next eliminate q4
15- Converting reg. expressions into FAs
- Rule 1 There is an FA that accepts any symbol of
S and there is an FA that accepts e. - If x is in S then give an FA that accepts x
- Give an FA that accepts e.
- Rule 2 Given FA1 that accepts reg. exp. r1 and
FA2 that accepts reg. exp. r2 then we can make an
FA3 that accepts r1 r2. (Product construction
and accept if either r1 or r2 accept. Second
method Add a new start state s and make a
e-transition from this state to each of the start
states of FA1 and F2. ) - Rule 3 Given FA1 that accepts reg. exp. r1 and
FA2 that accepts reg. exp. r2 then we can make an
FA3 that accepts r1r2. (e-transition from every
final state of r1 to the start state of r2. Then
make a DFA from this NFA if needed.) - Rule 4 Given FA1 that accepts reg. exp. r then
we can make an FA2 that accepts r. (Add a new
start state s and make a e-transition from this
state to the start state of FA1. Make a
e-transition from every final state of F1 to the
new start state s. s is also a final state. Make
a DFA from this NFA if needed.)
16- Reg. expressions to FAs
- Rule 1 There is an FA that accepts any symbol of
S and FAs that accept e and Ø. - Rule 2 Given FA1 that accepts reg. exp. r1 and
FA2 that accepts reg. exp. r2 then we can make an
FA3 that accepts r1 r2. - Rule 3 Given FA1 that accepts reg. exp. r1 and
FA2 that accepts reg. exp. r2 then we can make an
FA3 that accepts r1r2. - Rule 4 Given FA1 that accepts reg. exp. r then
we can make an FA2 that accepts r.
17Example
Make the DFA for abb(ab) (bab)
DFA for abb(ab)
b
b
q3
q2
q1
a
e
b
a
q4
q00
e
e
NFA for (bab)
e
e
b
q3
q2
q0
q1
a
b
Now turn this NFA into a DFA
18Example
b
b
q3
q2
q1
a
e
b
a
q4
q00
e
e
b
e
e
q0
q3
q2
q1
Can simplify and remove some of the
nondeterminism but be careful! abb(ab)
(bab)
b
a
b
b
q3
q2
q1
a
e
b
a
q4
q00
b
e
b
b
q3
q2
q1
a
b
19Chapter 9 Regular Expressions and Finite
Automata
- Now for the rest of the proof that
- Theorem Let A ? S. The following three
statements are equivalent - A is regular so A L(M) for some finite
automata M - A L(a) for some pattern a
- A L(a) for some regular expression a
- Need to prove
- 1 ? 3
- (1) If A L(M) for some finite automata M then
(3) A L(a) for some regular expression a - We will use structural induction to show how to
construct a regular expression from an FA
We already showed how to construct a FA from a
regular expression
20Chapter 9 Regular Expressions and Finite
Automata
- Regular Expressions are all patterns made with
just - Atoms a ? S, e, ?
- operators , ,
- Structural Induction http//www-cs-students.stanf
ord.edu/csilvers/proof/node5.html - useful when objects are built up from more
primitive objects (atoms or in FAs two states u
and v) - if we can show the primitive objects have the
desired property, and that the act of building
(operators or in FAs any subset of states X in
the FA) preserves that property, then we have
shown that all objects must have the property. - The inductive hypothesis (the assumption) is to
assume that something is true for simpler''
forms of an object and then prove that it holds
for more complex'' forms.
21- Theorem Every FA can be converted into a regular
expressions - Proof
- Given an NFA M Q, S, ?, S, F and X ? Q and
states u,v in Q then make a regular expression
aXuv - aXuv is the set of all strings w such that there
is a path from u to v in M labeled w and all
states on that path (except maybe u and v) are in
X - Induction done on the size of X
- Base Case Let X ? and a1, a2, ak are symbols
in S where v ? ?(u, ai) - If u ? v then a ?uv a1 ak if k 1
- ? if k 0
- If u v then a ?uv a1 ak e if k 1
- e if k 0
22Chapter 9 Regular Expressions and Finite
Automata
- Base Case Let X ? and a1, a2, ak are symbols
in S where v ? ?(u, ai) - In English the paths from u to v has no states
in between u and v and if u and v are not the
same state then it might have symbols a1 ak
each on different paths. If uv then include e
since the paths will be loops back to u - If u ? v then a ?uv a1 ak if k 1
- ? if k 0
- If u v then a ?uv a1 ak e if k 1
- e if k 0
23Chapter 9 Regular Expressions and Finite
Automata
- Inductive Case Let X ? ? pick any q in X
- aXuv aX-quv aX-quq (aX-qqq )aX-qqv
- aX-quv This is the case where q is never
visited - aX-quq If q is visited then there is a point
where it is visited for the first time. - (aX-qqq ) These are all loops that exit q
and then return to q where the states in
the loop are in X. - aX-qqv Followed by a path out of q to v
24Chapter 9 Regular Expressions and Finite
Automata
- Now consider aXsf
- This signifies the regular expression constructed
using all states Q in the FA and beginning in the
start state s and ending in the final state f. - aXsf is built inductively from the base cases and
all regular expressions built between any states
u, v in Q.
This theorem gives a way to make a FA into a
regular expression. You can use this or you can
use the 5 rules given earlier in these slides.
25Define aXuv aX-quv aX-quq (aX-qqq
)aX-qqv
0
1
q
p
0
1
0
r
- Example
- ap,q,rpp ap,rpp ap,rpq (ap,rqq
)ap,rqp - ap,rpp 0
- ap,rpq 01
- ap,rqq e 01 0001 e 001
- ap,rqp 000
- So ap,q,rpp 0 e 01(001)000
X p, q, r and to eliminate q X- q p, r
26Define aXuv aX-quv aX-quq (aX-qqq
)aX-qqv
1
0
Modified FA
1
q
p
0
1
0
r
- Example
- ap,q,rpp ap,rpp ap,rpq (ap,rqq
)ap,rqp - ap,rpp 0
- ap,rpq 01
- ap,rqq e 01 0001 101 e 001
101 - ap,rqp 000 10
- So ap,q,rpp 0 e 01(001 101)(000
10)
27State Elimination
0
11
e
q0
1
p
11
e
0
10
1
q
p
0
s
r
0
10
1
0
110
r
e
q0
p
110
e
10
1
s
0
r
10
e
1
q
q0
p
110
e
0
e
1
q0
p
0
s
10(10)(110)
e
r
s
(11010(10)(110))
28- COMPARE THE TWO EXPRESSIONS
- A 0 e 01(001 101)(000 10)
- 0 01(101 001)000 01(101
001)10 - 0 (01000 01(101)000
01(001)000) (0110 01(101)10
01(001)10) - B (11 0 10(10)(110))
- 0 (11 0 10(10)(110))
- 0 (11 0 10(10)11 10(10)0)
- 0 (0110 010(10)110 010(10)00)
- From A 01000 010(10)00 where is subset
- More to do
(aß)a a(ßa) (aß)a (ß a) a(ßa) (ß
a) (e a) a aa aa