Lecture 10 Theory of Automata 2014 Kleenes Theorem

  • Slides: 23
Download presentation
Lecture 10: Theory of Automata: 2014 Kleene’s Theorem and NFA Asif Nawaz UIIT, PMAS-Arid

Lecture 10: Theory of Automata: 2014 Kleene’s Theorem and NFA Asif Nawaz UIIT, PMAS-Arid Agriculture University Rawalpindi.

Lecture 10: Theory of Automata: 2014 Proof of Part 3: Converting Regular Expressions into

Lecture 10: Theory of Automata: 2014 Proof of Part 3: Converting Regular Expressions into FAs • We prove this part by recursive definition and constructive algorithm at the same time. – We know that every regular expression can be built up from the letters of the alphabet ∑ and Λ by repeated application of certain rules: (i) addition, (ii) concatenation, and (iii) closure. – We will show that as we are building up a regular expression, we could at the same time building up an FA that accepts the same language. • Slides 3 - 30 below show the proof of part 3. Asif Nawaz UIIT, PMAS-Arid Agriculture University Rawalpindi. 2

Lecture 10: Theory of Automata: 2014 • Before we proceed, let’s have a quick

Lecture 10: Theory of Automata: 2014 • Before we proceed, let’s have a quick review of the formal definition of regular expressions. • The set of regular expressions is defined by the following rules: • Rule 1: Every letter of the alphabet ∑ can be made into a regular expression by writing it in boldface: Λ itself is a regular expression. • Rule 2: If r 1 and r 2 are regular expressions, then so are: (i) (r 1) (ii) r 1 r 2 (iii) r 1 + r 2 (iv) r 1* • Rule 3: Nothing else is a regular expression. We now present proof of part 3 recursively. Asif Nawaz UIIT, PMAS-Arid Agriculture University Rawalpindi. 3

Lecture 10: Theory of Automata: 2014 Rule 1 • There is an FA that

Lecture 10: Theory of Automata: 2014 Rule 1 • There is an FA that accepts any particular letter of the alphabet. • There is an FA that accepts only the word Λ. Asif Nawaz UIIT, PMAS-Arid Agriculture University Rawalpindi. 4

Lecture 10: Theory of Automata: 2014 Proof of rule 1 • If letter x

Lecture 10: Theory of Automata: 2014 Proof of rule 1 • If letter x is in ∑, then the following FA accepts only the word x. • The following FA accepts only λ: Asif Nawaz UIIT, PMAS-Arid Agriculture University Rawalpindi. 5

Lecture 10: Theory of Automata: 2014 Rule 2 • If there is an FA

Lecture 10: Theory of Automata: 2014 Rule 2 • If there is an FA called FA 1 that accepts the language defined by the regular expression r 1, and there is an FA called FA 2 that accepts the language defined by the regular expression r 2, then there is an FA that we shall call FA 3 that accepts the language defined by the regular expression (r 1 + r 2). Asif Nawaz UIIT, PMAS-Arid Agriculture University Rawalpindi. 6

Lecture 10: Theory of Automata: 2014 Proof of Rule 2 • We shall show

Lecture 10: Theory of Automata: 2014 Proof of Rule 2 • We shall show that FA 3 exists by presenting an algorithm showing how to construct FA 3. • Algorithm: – Starting with two machines, FA 1 with states x 1; x 2; x 3; …, and FA 2 with states y 1; y 2; y 3; …, we construct a new machine FA 3 with states z 1; z 2; z 3; … where each zi is of the form xsomething or ysomething. – The combination state xstart or ystart is the start state of the new machine FA 3. – If either the x part or the y part is a final state, then the corresponding z is a final state. Asif Nawaz UIIT, PMAS-Arid Agriculture University Rawalpindi. 7

Lecture 10: Theory of Automata: 2014 Algorithm (cont. ) – To go from one

Lecture 10: Theory of Automata: 2014 Algorithm (cont. ) – To go from one state z to another by reading a letter from the input string, we observe what happens to the x part and what happens to the y part and go to the new state z accordingly. We could write this as a formula: znew after reading letter p = (xnew after reading letter p on FA 1) or (ynew after reading letter p on FA 2) Asif Nawaz UIIT, PMAS-Arid Agriculture University Rawalpindi. 8

Lecture 10: Theory of Automata: 2014 Remarks • The new machine FA 3 constructed

Lecture 10: Theory of Automata: 2014 Remarks • The new machine FA 3 constructed by the above algorithm will simultaneously keep track of where the input would be if it were running on FA 1 alone, and where the input would be if it were running on FA 2 alone. • If a string traces through the new machine FA 3 and ends up at a final state, it means that it would also end at a final state either on machine FA 1 or on machine FA 2. Also, any string accepted by either FA 1 or FA 2 will be accepted by this FA 3. So, the language FA 3 accepts is the union of the languages accepted by FA 1 and FA 2, respectively. • Note that since there are only finitely many states x’s and finitely many states y’s, there can be only finitely many possible states z’s. • Let us look at an example illustrating how the algorithm works. Asif Nawaz UIIT, PMAS-Arid Agriculture University Rawalpindi. 9

Lecture 10: Theory of Automata: 2014 Example • Consider the following two FAs: •

Lecture 10: Theory of Automata: 2014 Example • Consider the following two FAs: • FA 1 accepts all words with a double a in them. • FA 2 accepts all words ending with b. • Let’s follow the algorithm to build FA 3 that accepts the union of the two languages. Asif Nawaz UIIT, PMAS-Arid Agriculture University Rawalpindi. 10

Lecture 10: Theory of Automata: 2014 Combining the FAs • The start (-) state

Lecture 10: Theory of Automata: 2014 Combining the FAs • The start (-) state of FA 3 is z 1 = x 1 or y 1. • In z 1, if we read an a, we go to x 2 (observing FA 1), or we go to y 1 (observing FA 2). Let z 2 = x 2 or y 1. • In z 1, if we read a b, we go to x 1 (observing FA 1), or to y 2 (observing FA 2). Let z 3 = x 1 or y 2. Note that z 3 must be a final state since y 2 is a final state. Asif Nawaz UIIT, PMAS-Arid Agriculture University Rawalpindi. 11

Lecture 10: Theory of Automata: 2014 • In z 2, if we read an

Lecture 10: Theory of Automata: 2014 • In z 2, if we read an a, we go to x 3 or y 1. Let z 4 = x 3 or y 1. z 4 is a final state because x 3 is. • In z 2, if we read a b, we go to x 1 or y 2, which is z 3. Asif Nawaz UIIT, PMAS-Arid Agriculture University Rawalpindi. 12

Lecture 10: Theory of Automata: 2014 • In z 3, if we read an

Lecture 10: Theory of Automata: 2014 • In z 3, if we read an a, we go to x 2 or y 1, which is z 2. • In z 3, if we read a b, we go to x 1 or y 2, which is z 3. • In z 4, if we read an a, we go to x 3 or y 1, which is z 4. Hence, we have an a-loop at z 4. • In z 4, if we read a b, we go to x 3 or y 2. Let z 5 = x 3 or y 2. Note that z 5 is a final state because x 3 (and y 2) are. • In z 5, if we read an a, we go to x 3 or y 1, which is z 4. • In z 5, if we read a b, we go to x 3 or y 2, which is z 5. Hence, there is a b-loop at z 5. • The whole machine looks like the following: Asif Nawaz UIIT, PMAS-Arid Agriculture University Rawalpindi. 13

Lecture 10: Theory of Automata: 2014 • This machine accepts all words that have

Lecture 10: Theory of Automata: 2014 • This machine accepts all words that have a double a or that end with b. • The labels z 1 = x 1 or y 1, z 2 = x 2 or y 1, etc. can be removed if you want. Asif Nawaz UIIT, PMAS-Arid Agriculture University Rawalpindi. 14

Lecture 10: Theory of Automata: 2014 Example • Consider the following two FAs: •

Lecture 10: Theory of Automata: 2014 Example • Consider the following two FAs: • FA 1 accepts all words that end in a. • FA 2 accepts all words with an odd number of letters (odd length). • Can you use the algorithm to build a machine FA 3 that accepts all words that either have an odd number of letters or end in a? Asif Nawaz UIIT, PMAS-Arid Agriculture University Rawalpindi. 15

Lecture 10: Theory of Automata: 2014 • Using the algorithm, we can produce FA

Lecture 10: Theory of Automata: 2014 • Using the algorithm, we can produce FA 3 that accepts all words that either have an odd number of letters or end in a, as follows: • The only state that is not a + state is the - state. To get back to that start state, a word must have an even number of letters and end in b. Asif Nawaz UIIT, PMAS-Arid Agriculture University Rawalpindi. 16

Lecture 10: Theory of Automata: 2014 Rule 3 If there is an FA 1

Lecture 10: Theory of Automata: 2014 Rule 3 If there is an FA 1 that accepts the language defined by the regular expression r 1, and there is an FA 2 that accepts the language defined by the regular expression r 2, then there is an FA 3 that accepts the language defined by the (concatenation) regular expression (r 1 r 2), i. e. the product language. Asif Nawaz UIIT, PMAS-Arid Agriculture University Rawalpindi. 17

Lecture 10: Theory of Automata: 2014 • We shall show that such an FA

Lecture 10: Theory of Automata: 2014 • We shall show that such an FA 3 exists by presenting an algorithm showing how to construct it from FA 1 and FA 2. • The idea is to construct a machine that starts out like FA 1 and follows along it until it enters a final state at which time an option is reached. Either we continue along FA 1, waiting to reach another +, or else we switch over to the start state of FA 2 and begin circulating there. Asif Nawaz UIIT, PMAS-Arid Agriculture University Rawalpindi. 18

Lecture 10: Theory of Automata: 2014 Algorithm • First, create a state z for

Lecture 10: Theory of Automata: 2014 Algorithm • First, create a state z for every state of FA 1 that we may go through before arriving at a final state. 2. For each final state xfinal of FA 1, add a state z = xfinal or y 1, where y 1 is the start state of FA 2. 3. From the states added in step 2, add states 4. Label every state z that contains a final state from FA 2 as a final state. Asif Nawaz UIIT, PMAS-Arid Agriculture University Rawalpindi. 19

Lecture 10: Theory of Automata: 2014 Example • FA 1 accepts all words that

Lecture 10: Theory of Automata: 2014 Example • FA 1 accepts all words that start with a b. • FA 2 accepts all words that end with a b. • We will use the above algorithm to construct FA 3 that accepts the product of the languages of FA 1 and FA 2, respectively. That is, FA 3 will accept all words that both start and end with the letter b. Asif Nawaz UIIT, PMAS-Arid Agriculture University Rawalpindi. 20

Lecture 10: Theory of Automata: 2014 • Initially, we must begin with x 1

Lecture 10: Theory of Automata: 2014 • Initially, we must begin with x 1 = z 1. • In z 1, if we read an a, we go to x 2 = z 2. • In z 1, if we read a b, we go to x 3, a final state, which gives us the option to jump to y 1. Hence, we label z 3 = x 3 or y 1. • From z 2 just like x 2, both an a or a b take us back to z 2, i. e. , we have a loop here. Asif Nawaz UIIT, PMAS-Arid Agriculture University Rawalpindi. 21

Lecture 10: Theory of Automata: 2014 • In z 3, if we read an

Lecture 10: Theory of Automata: 2014 • In z 3, if we read an a then the following happens. If z 3 is x 3, we can stay in x 3 or jump to y 1 (because x 3 is a final state). If z 3 is y 1, we would loop back to y 1. In any of the events, we end up at either x 3 or y 1, which is still z 3. Hence, we have an a-loop at z 3. • In z 3, if we read a b, then a different event takes place. If z 3 is x 3 we either stay in x 3 or jump to y 1. If z 3 is y 1, then we go to y 2, a final state. Hence, we need a new final state z 4 = x 3 or y 1 or y 2. • In z 4, if we read an a, what happens? If z 4 is x 3 then we go back to x 3 or jump to y 1. If z 4 is y 1 then we loop back to y 1. If z 4 is y 2, we go to y 1. Thus, from z 4, an a takes us to x 3 or y 1, which is z 3. • In z 4, if we read a b, what happens? If z 4 is x 3, we go back to x 3 or jump to y 1. If z 4 is y 1, we go to y 2, a final state. If z 4 is y 2, we loop back to y 2, a final state. Hence, from z 4 a b takes us to x 3 or y 1 or y 2, which is still z 4 (i. e. , we have a b-loop here). Asif Nawaz UIIT, PMAS-Arid Agriculture University Rawalpindi. 22

Lecture 10: Theory of Automata: 2014 x • This machine accepts all words that

Lecture 10: Theory of Automata: 2014 x • This machine accepts all words that both begin and end with the letter b, which is what the product of the two languages (defined by FA 1 and FA 2 respectively) would be. • If you multiply the two languages in opposite order (i. e. first FA 2 then FA 1), then the product language will be different. What is that language? Can you build a machine for that product language Asif Nawaz UIIT, PMAS-Arid Agriculture University Rawalpindi. 23