460 likes | 604 Views
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. Algebraic expressions.
E N D
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
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
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
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
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
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
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
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
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
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
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
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
Example Are r and s equivalent? r=+(0+1)*1 s=(0*1)* Rina Zviel-Girshin @ASC
L[s]L[r] • Let wL[s] =(0*1)*. • w= or w=x1x2..xn , n0 such that xiL[0*1], i=0,..n. • If w= then wL[r]. • If w=x1x2..xn than we can represent w=w’1=x1x2..xn-100001 with zero or more 0. But w’= x1x2..xn-10000L[(0+1)*]. So wL[r]. Rina Zviel-Girshin @ASC
L[r]L[s] • Let wL[r]=+(0+1)*1. • If w= then wL[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 xi0* But then w=w’1= (x11)(x21).. 1)(xk+1 1) So wL[(0*1)*]. Rina Zviel-Girshin @ASC
Another example Are r and s equivalent? r=(0+1)*1+0* s=(1+0)(0*1)* No. • We choose a word w = . • wL[r]=(0+1)*1+0*, because w0*. • But wL[s] =(1+0)(0*1)*, as all words in L[s] have at least one letter. Rina Zviel-Girshin @ASC
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
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
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: Rina Zviel-Girshin @ASC
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
RE to FA Algorithm (cont.) 2.Transform any diagram of the type into the diagram Rina Zviel-Girshin @ASC
RE to FA Algorithm (cont.) 3.Transform any diagram of the type into the diagram Rina Zviel-Girshin @ASC
RE to FA Algorithm (cont.) 4.Transform any diagram of the type into the diagram Rina Zviel-Girshin @ASC
Example Construct an NFA for the regular expression b*+ ab. We start by drawing the diagram: Rina Zviel-Girshin @ASC
Example (cont.) Next we apply rule 2 for b*+ab: Rina Zviel-Girshin @ASC
Example (cont.) Next we apply rule 3 for ab: Rina Zviel-Girshin @ASC
Example (cont.) Next we apply rule 4 for b*: Rina Zviel-Girshin @ASC
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
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
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 . Rina Zviel-Girshin @ASC
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 qF (q,)= f . Rina Zviel-Girshin @ASC
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. Rina Zviel-Girshin @ASC
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
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 ik and jk, 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
Algorithm FA to RE (cont.) new(i,j)=old(i,j) + old(i,k)old(k,k)*old(k,j) Rina Zviel-Girshin @ASC
Algorithm FA to RE (cont.) • For all other edges <i,j> where ik and jk, 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
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
Example • Transform into regular expression the following DFA Rina Zviel-Girshin @ASC
Example The result of first three steps of the algorithm: • new start state • new final state • unify multiple edges Rina Zviel-Girshin @ASC
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. Rina Zviel-Girshin @ASC
Example Eliminate state 0 • The only path through it is s q1. • We delete q0 and edges sq0 and q0q1. • 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 Rina Zviel-Girshin @ASC
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)* Rina Zviel-Girshin @ASC
Any Questions? Rina Zviel-Girshin @ASC