Complexity and Computability Theory I

Download Report

Transcript Complexity and Computability Theory I

Complexity and Computability
Theory I
Lecture #6
Instructor: Rina Zviel-Girshin
Lea Epstein
Overview
Regular expressions
Examples
Properties of regular languages
Transforming regular expressions into FA
Transforming FA into regular expressions
Examples
Rina Zviel-Girshin @ASC
2
Algebraic expressions
• Regular languages are often described by
means of algebraic expressions called
regular expressions.
• In arithmetic we use the +, * operations to
construct expressions:
(2+3)*5
• The value of the arithmetic expression is the
number 25.
Rina Zviel-Girshin @ASC
3
Regular operations
• We can use regular operations to construct
expressions describing regular languages:
(0+1)* 0*
where :
+ means OR
* Kleene star
 concatenation
• The value of a regular expression is a regular
language.
Rina Zviel-Girshin @ASC
4
Formal definition
• A set of regular expressions over an alphabet  is
defined inductively as follows:
Basis:
, , and  (for all ) are regular expressions.
Induction:
If r and s are regular expressions then the following
expressions are also regular:
(r) , r+s, r  s and r*.
Rina Zviel-Girshin @ASC
5
Examples over ={a,b}
, a, a+b, b*, (a+b)b, ab*, a*+b*
• To avoid using to many parentheses, we assume
that the operations have the following priority
hierarchy:
* - highest (do it first)

+ - lowest (do it last)
So:
(b+(a (b*)) = b + ab*
Rina Zviel-Girshin @ASC
6
Examples over ={0,1}
L1 = { w | w has a single 1}
r1 = 0*10*
L2 = { w | w has at least one 1}
r2 = (0+1)*1 (0+1)*= *1*
L3 = { w | w contains the string 110}
r3 = (0+1)*110(0+1)* = *110*
Rina Zviel-Girshin @ASC
7
Examples over ={0,1}
L4 = { w | |w| mod 2 =0}
r4 = ((0+1) (0+1))* = ()*
L5 = { w | w starts with 1}
r5 = 1(0+1)* = 1*
L6 = { w | w ends with 00}
r6 = (0+1)*00 = *00
Rina Zviel-Girshin @ASC
8
Examples over ={0,1}
L7 = { w | w starts with 0 and ends with 10}
r7 = 0(0+1)*10 = 0*10
L8 = { w | w contains the string 010 or the string 101 }
r8 = (0+1)* 010 (0+1)* + (0+1)* 101 (0+1)*=
(0+1)*(010+101)(0+1)* = *(010+101)*
L9 = { w | w starts and ends with the same letter }
r9 = 0(0+1)*0 + 1(0+1)*1 + 0 +1 = 0*0 + 1*1 + 0 +1
Rina Zviel-Girshin @ASC
9
Regular languages and regular
expressions
We associate each regular expression r with a
regular language L(r) as follows:
• L()=,
• L()={},
• L()={} for each ,
• L(r+s)=L(r)L(s),
• L(r  s)=L(r)  L(s),
• L(r*)=(L(r))*.
Rina Zviel-Girshin @ASC
10
Properties of regular
expressions 1
Some useful properties of regular expressions
r+s=s+r
r+=+r
r+r=r
r=r=
rr*=r+
r=r=r
Rina Zviel-Girshin @ASC
11
Properties of regular
expressions 2
r(s+t)=rs+rt
r+(s+t)=(r+s)+t
r(st)=(rs)t
r+r*=(r*)*=r*r*=r*
r*+r+=r*
Rina Zviel-Girshin @ASC
12
Equivalence of regular
expressions
• To prove that two regular expressions r and s are
equivalent we have to prove that
L[r]L[s] and L[s]L[r].
• To show that two regular expressions are not
equivalent we have to find a word that belongs to
one expression and does not belong to the other.
Rina Zviel-Girshin @ASC
13
Example
Are r and s equivalent?
r=+(0+1)*1
s=(0*1)*
Rina Zviel-Girshin @ASC
14
L[s]L[r]
• Let wL[s] =(0*1)*.
• w= or w=x1x2..xn , n0 such that xiL[0*1],
i=0,..n.
• If w= then wL[r].
• If w=x1x2..xn than we can represent
w=w’1=x1x2..xn-100001 with zero or more 0.
 But w’= x1x2..xn-10000L[(0+1)*].
 So wL[r].
Rina Zviel-Girshin @ASC
15
L[r]L[s]
• Let wL[r]=+(0+1)*1.
• If w= then wL[s] (by definition of *).
• If w then can be represented as w=w’1 where
w’L[(0+1)*]. Assume that in w’ contains k
instances of the letter 1. That means that w’ can be
represented as w’= x11x21.. 1xk+1 where xi0*
 But then w=w’1= (x11)(x21).. 1)(xk+1 1)
 So wL[(0*1)*].
Rina Zviel-Girshin @ASC
16
Another example
Are r and s equivalent?
r=(0+1)*1+0*
s=(1+0)(0*1)*
No.
• We choose a word w = .
• wL[r]=(0+1)*1+0*, because w0*.
• But wL[s] =(1+0)(0*1)*, as all words in L[s]
have at least one letter.
Rina Zviel-Girshin @ASC
17
Equivalence with FA
• Regular expressions and finite automata are
equivalent in terms of the languages they describe.
Theorem:
A language is regular if”f some regular expression
describes it.
This theorem has two directions. We prove each
direction separately.
Rina Zviel-Girshin @ASC
18
Converting RE into FA
• If a language is described by a regular
expression, then it is regular.
Proof idea:
Transformation of some regular expression r
into a finite automaton N that accepts the
same language L(r).
Rina Zviel-Girshin @ASC
19
RE to FA Algorithm
• Given r we start the algorithm with N
having a start state, a single accepting state
and an edge labeled r:
s
r
Rina Zviel-Girshin @ASC
q0
f
20
RE to FA Algorithm (cont.)
Now transform this machine into an NFA N
by applying the following rules until all the
edges are labeled with either a letter  from
 or :
1.If an edge is labeled , then delete the edge.
Rina Zviel-Girshin @ASC
21
RE to FA Algorithm (cont.)
2.Transform any diagram of the type
i
r+s
j
into the diagram
r
i
j
s
Rina Zviel-Girshin @ASC
22
RE to FA Algorithm (cont.)
3.Transform any diagram of the type
i
rs
j
into the diagram
i
r
s
Rina Zviel-Girshin @ASC
j
23
RE to FA Algorithm (cont.)
4.Transform any diagram of the type
i
r*
r
into the diagram
i
j


Rina Zviel-Girshin @ASC
j
24
Example
Construct an NFA for the regular expression b*+ ab.
We start by drawing the diagram:
s
b*+ab
Rina Zviel-Girshin @ASC
f
q0
25
Example (cont.)
Next we apply rule 2 for b*+ab:
b*
s
q0
f
ab
Rina Zviel-Girshin @ASC
26
Example (cont.)
Next we apply rule 3 for ab:
b*
s
q0
f
a
Rina Zviel-Girshin @ASC
b
27
Example (cont.)
b
Next we apply
rule 4 for b*:


s
q0
f
a
Rina Zviel-Girshin @ASC
b
28
Transforming a FA into RE
• If a language is regular , then it is described
by a regular expression.
Proof idea:
transformation of some finite automaton N
into a regular expression r that represents
the regular language accepted by the finite
automaton L(N).
Rina Zviel-Girshin @ASC
29
Transforming a FA into RE
• The algorithm will perform a sequence of
transformations into new machines with
edges labeled with regular expressions.
• It stops when the machine has :
– two states:
• start
• finish
– one edge with regular expression on it.
Rina Zviel-Girshin @ASC
30
Algorithm FA to RE
Assume we have a DFA or an NFA N=(Q, , , q0, F).
Perform the following steps.
1. Create a new start state s and draw a new edge
labeled with  from s to the q0. (s,)= q0 .
N
s

q0
Rina Zviel-Girshin @ASC
F
31
Algorithm FA to RE (cont.)
2. Create a new accepting state f and draw new edges
labeled with  from all the original accepting
states to f. For each qF (q,)= f .
N

s

q0
Rina Zviel-Girshin @ASC

f
32
Algorithm FA to RE (cont.)
3. For each pair of states i and j that have more than
one edge from i to j, replace all the edges from i to
j by a single edge labeled with the regular
expression formed by the sum of the labels of all
edges from i to j.
r
i
j
s
i
r+s
Rina Zviel-Girshin @ASC
j
33
Algorithm FA to RE (cont.)
4. Construct a sequence of new machines by
eliminating one state at a time until the only two
states remaining are s and f.
As each state is eliminated a new machine is
constructed in the following way:
Rina Zviel-Girshin @ASC
34
Algorithm FA to RE (cont.)
Eliminating state k
• Let old(i,j) denote the label on edge <i,j> of the
current machine.
• If there is no an edge <i,j> then old(i,j)=. Now
for each pair of edges <i,k> and <k,j>, where ik
and jk, calculate a new edge labeled new(i,j) as
follows:
new(i,j)=old(i,j) + old(i,k)old(k,k)*old(k,j)
Rina Zviel-Girshin @ASC
35
Algorithm FA to RE (cont.)
new(i,j)=old(i,j) + old(i,k)old(k,k)*old(k,j)
qi
old(i,j)
old(i,k)
qj
old(k,j )
qk
old(k,k)
Rina Zviel-Girshin @ASC
36
Algorithm FA to RE (cont.)
• For all other edges <i,j> where ik and jk,
new(i,j)=old(i,j).
• The states of the new machine are those of the
current machine with state k eliminated. The edges
of the new machine are those edges <i,j> for
which new(i,j) has been calculated.
Rina Zviel-Girshin @ASC
37
Algorithm FA to RE (cont.)
• Now s and f are two remaining states.
Regular expression new(s,f) represents the
language of the original automaton.
Rina Zviel-Girshin @ASC
38
Example
• Transform into regular expression the following
DFA
a,b
q1
a
q0
b
q2
a,b
Rina Zviel-Girshin @ASC
39
Example
The result of first three steps of the algorithm:
• new start state
• new final state
• unify multiple edges
a+b
s

q0
a

q1
q1
f
b
q2
a+b
Rina Zviel-Girshin @ASC
40
Example
Eliminate state 2
• No paths pass through 2. There are no states that
connect through 2. So no need to change anything
after deletion of state 2.
new(i,j)=old(i,j) for all i,j.
a+b
s

q0
a
q1
Rina Zviel-Girshin @ASC

q1
f
41
Example
Eliminate state 0
• The only path through it is s  q1.
• We delete q0 and edges sq0 and q0q1.
• Instead we add an edge that is labeled by regular
expression associated with the deleted edges.
new(s,q1)=old(s,q1)+old(s,q0)old(q0,q0)*old(q0,q1)
=+*a=a
a+b
s
a
q1
Rina Zviel-Girshin @ASC

q1
f
42
Example
Eliminate state q1
• The only path through it is s  f.
new(s,f)=old(s,f) + old(s,q1)old(q1,q1)*old(q1,f)
=+a(a+b)* = a(a+b)*
s
a(a+b)*
Rina Zviel-Girshin @ASC
q1
f
43
Any Questions?
Rina Zviel-Girshin @ASC
44