MELJUN CORTES Automata manual handouts

download MELJUN CORTES Automata manual handouts

of 32

  • date post

  • Category


  • view

  • download


Embed Size (px)


MELJUN CORTES Automata manual handouts

Transcript of MELJUN CORTES Automata manual handouts

  • 1. June 23, 2012 MELJUN P. CORTES, MBA,MPA,BSCS,ACSFINITE AUTOMATA It is a mathematical model of a system with discrete inputs and outputs. It consist of finite set of a sets and a set of transitions from state to state the occur on input symbol chosen from alphabet ().CONVENTIONS: 1. states qs and ps 2. q0 initial state 3. 0, 1, a, b input symbols 4. w, x, y, z strings of input symbols.NOTE:Finite Automata are limited in strength but they are a thoroughly understood subclass of more powerful computational models. a further reason for studying FA is their applicability to the design of several common types of computer algorithms and programs.Example: Lexical analysis phase of Compilers is often based on the simulation of automation.Three (3) Ways Of Presenting Finite Automata 1. State Diagram (DIAGRAM)Vertices denotes the stateEdges transition functionInitial StateFinal State Example: State Diagram x,y x,y 2. State TablePage 1 / aav

2. June 23, 2012 MELJUN P. CORTES, MBA,MPA,BSCS,ACSState Tablex yq0 q1q1*q1 q0q0 3. Transition Function ( set of states, set of input alphabet )Transition Function (q0,x) = q1 (q0,y) = q1 (q1,x) = q0 (q1,y) = q0TWO TYPES OF FINITE AUTOMATA : 1. Deterministic Finite Automata (DFA) 0 11 00 Finite Control One-way, infinite tape, broken into cells One-way, read-only tape head. Finite control, I.e., a program, containing the position of the read head, current symbol being scanned, and the current state. A string is placed on the tape, read head is positioned at the left end, and the DFA will read the string one symbol at a time until all symbols have been read. The DFA will then either accept or reject. For each input symbol in , there is exactly one transition of each state (possibly back tothe state itself). It do not accept empty strings. It is a quintuple where M = (Q, , q0, , F)where:Q = finite set of all sets = finite set of input symbolsq0 = initial state = transition functionF = set of final statePage 2 / aav 3. June 23, 2012 MELJUN P. CORTES, MBA,MPA,BSCS,ACSTransition Diagram Examples: 1. Diagrams that ends with 10.State Diagram0 1 10 qoq1 q21 0Transition Table01 q0 q0q1 q1 q2 q1 *q2 q0q1 Strings that can be derived: 10 0110 010 01010Transition Function (q0,0) = q0 (q1,1) = q1 (q0,1) = q1 (q2,0) = q0 (q1,0) = q2 (q2,1) = q12. Draw a transition diagram that will end with 001.10 001 qoq1 q2 q3 1 013.1 0q0q11 01001 1 q0 q0q1 q0 q0 q0 Page 3/ aav 4. June 23, 2012 MELJUN P. CORTES, MBA,MPA,BSCS,ACS One state is final/accepting, all others are rejecting. The above DFA accepts those strings that contain an even number of 0s 4. Accepts those strings that contain at least two csaaa/b/cccq0 q1 q2bbacc cb accepted q0 q0 q1 q2q2 q2aacrejected q0 q0 q0 q1Extension of to Strings^ : (Q x *) > Q^(q,w) The state entered after reading string w having started in state q.Formally:1) ^(q, ) = q, and2) For all w in * and a in ^(q,wa) = (^(q,w), a)Recall Example #3:1 0 q0 q11 0What is ^(q0, 011)? Informally, it is the state entered by M after processing 011 having started instate q0.Formally: Page 4 / aav 5. June 23, 2012 MELJUN P. CORTES, MBA,MPA,BSCS,ACS^(q0, 011) = (^(q0,01), 1) by rule #2= ( ( ^(q0,0), 1), 1)by rule #2= ( ( (^(q0, ), 0), 1), 1) by rule #2= ( ((q0,0), 1), 1)by rule #1= ( (q1, 1), 1) by definition of = (q1, 1)by definition of = q1 by definition of Is 011 accepted? No, since ^(q0, 011) = q1 is not a final state. Note that:^ (q,a)= (^(q, ), a)by definition of ^, rule #2= (q, a) by definition of ^, rule #1 Therefore: (q, a1a2an) = ((((q, a1), a2)), an)^ Hence, we can use in place of ^:^(q, a1a2an) = (q, a1a2an)Definitions for DFAs Let M = (Q, , ,q0,F) be a DFA and let w be in *. Then w is accepted by M iff (q0,w) = p for some state p in F. Let M = (Q, , ,q0,F) be a DFA. Then the language accepted by M is the set: L(M) = {w | w is in * and (q0,w) is in F} Another equivalent definition: L(M) = {w | w is in * and w is accepted by M} Let L be a language. Then L is a regular language iff there exists a DFA M such that L = L(M). Let M1 = (Q1, 1, 1, q0, F1) and M2 = (Q2, 2, 2, p0, F2) be DFAs. Then M1 and M2 are equivalent iff L(M1) = L(M2).Notes:A DFA M = (Q, , ,q0,F) partitions the set * into two sets: L(M) and * - L(M).If L = L(M) then L is a subset of L(M) and L(M) is a subset of L.Similarly, if L(M1) = L(M2) then L(M1) is a subset of L(M2) and L(M2) is a subset of L(M1). Some languages are regular, others are not. For example, ifL1 = {x | x is a string of 0s and 1s containing an even number of 1s} andL2 = {x | x = 0n1n for some n >= 0} then L1 is regular but L2 is not. Questions: How do we determine whether or not a given language is regular? How could a program simulate a DFA? Page 5/ aav 6. June 23, 2012 MELJUN P. CORTES, MBA,MPA,BSCS,ACSSeatwork: 1. Give the following DFA M such that: a. L(M) = {x | x is a string of 0s and 1s and |x| >= 2} no. states 3 b. L(M) = {x | x is a string of (zero or more) as, bs and cs suchthat x does not contain the substring aa} # states 3 c. L(M) = {x | x is a string of as, bs and cs such that x contains the substring aba} # states 4 d. L(M) = {x | x is a string of as and bs such that x contains both aa and bb} no of states 8 Nondeterministic Finite State Automata (NFA)*RECALL: In FA or DFA, for each input there is exactly one transition output of each state.Lets try to modify the finite automaton by alowing zero, one or more transitions from a state onthe same input symbol. The modified model is called Non-Deterministic Finite Automaton.If for some of Q, a, (q,a) does either to a unique state or several states or not states at all,then the FA is a NFA. A Non-Deterministic Finite Automaton is a quintuple M=(Q,,q0,,F)where:Q = set of states = set of input alphabets = transition function: (Q x ) > 2Q-2Q is the power set of Q, the set of all subsets of Q (q,s)-The set of all states p such that there is a transition labeled s from q to pq0 = initial stateF = set of final state It allows zero, one or more transitions for every inputs symbol. Empty string accepted A sequence of symbols, say a1, a2, is accepted by NFA if there exists a sequence of transition corresponding to the input sequence that leads from the initial states to the final state. Example: Construct a NFA that accepts string ending with 011.1011qoq1q2q30 (q0,011)--> q3 Page 6 / aav 7. June 23, 2012 MELJUN P. CORTES, MBA,MPA,BSCS,ACS To check if 1011 is a valid stringq0q0q1q2q2 validq0q0q0 not valid * The reason for the word nondeterministic is that we are in a state where there aremultiple outgoing edges all having the same input symbol we have a choice of next state. *The only difference between a NFA & DFA is that in DFA the next state function takes usto a uniquely defined state whereas in NFA the next state takes us to a set of states. Example : NFA Diagram1 0000q0qoq3 q411q111,0q2State Table01q0{q0,q3}{q0,q1}q10{q2}q2{q2} {q2}q3{q4} 0q4{q4} {q4}Transition Function Let the input be 01001 (q0, ) = q0 (q0, 0) = ((q0, )0) = (q0,0)={q0,q3} (q1, 01) = ((q0,0)1) =(q0,1) U (q3,1)={q0,q1}U{ }={q0,q1} (q1, 010) = ((q0,01)0)=(q0,0) U (q1,0)={q0,q3}U{ }={q0,q3} (q2, 0100) = ((q0,010)0)=(q0,1) U (q3,1) U (q3,0)={q0,q3} U {q4}={q0,q3,q4} (q2, 01001) = ((q0,1) U (q3,1) U (q4,1) = {q0,q1} U {} U {q4}= {q0,q1,q4}Example: pair os 0s or pair of 1s Page 7/ aav 8. June 23, 2012 MELJUN P. CORTES, MBA,MPA,BSCS,ACS Q = {q0, q1, q2 , q3 , q4} = {0, 1} Start state is q0F = {q2, q4}Determining if a given NFA accepts a given string (001) can be done algorithmically:Each level will have at most n statesAnother example (010): All paths have been explored, and none lead to an accepting stateLet = {a, b, c}. Give an NFA M that accepts: L = {x | x is in * and x contains ab}Is L a subset of L(M)?Is L(M) a subset of L? Is an NFA necessary? Could a DFA accept L? Try and give an equivalent DFA as an exercise. Designing NFAs is not a typical task.Let = {a, b}. Give an NFA M that accepts:L = {x | x is in * and the third to the lastsymbol in x is b}Extension of to Strings and Sets of States What we currently have: : (Q x ) > 2Q What we want (why?): : (2Q x *) > 2QPage 8 / aav 9. June 23, 2012 MELJUN P. CORTES, MBA,MPA,BSCS,ACSWe will do this in two steps, which will be slightly different from the book, and we will make use ofthe following NFA.Step #1:Given : (Q x ) > 2Q define #: (2Q x ) > 2Q as follows:1) #(R, a) =(q, a) for all subsets R of Q, and symbols a in Note that: ({p},a) =# (q, a) by definition of #, rule #1 above = (p, a) Hence, we can use for #({q0, q2}, 0)These now make sense, but previously({q0, q1, q2}, 0)they did not.Example:({q0, q2}, 0) = (q0, 0) U (q2, 0) ({q0, q1, q2}, 1) = (q0, 1) U (q1, 1) U (q2, 1) = {q1, q3} U {q3, q4}= {} U {q2, q3} U {} = {q1, q3, q4} = {q2, q3}Step #2:Given : (2Q x ) > 2Q define ^: (2Q x *) > 2Q as follows:^(R,w) The set of states M could be in after processing string w, having startingfrom any state in R. Formally:2) ^(R, ) = R for any subset R of Q3) ^(R,wa) = (^(R,w), a)for any w in *, a in , and subset R of Q Note that:^(R, a) = (^(R, ), a) by definition of ^, rule #3 above = (R, a)by definition of ^, rule #2 above Hence, we can use for ^({q0, q2}, 0110)These now make sense, but previously({q0, q1, q2}, 101101)they did not.Example: Page 9 / aav 10. June 23, 2012 MELJUN P. CORTES, MBA,MPA,BSCS,ACSWhat is ({q0}, 10)?Informally: The set of states the NFA could be in after processing 10,having started in state q 0,i.e., {q1, q2, q3}.Formally:({q0}, 10) = (({q0}, 1), 0) = ({q0}, 0) = {q1, q2, q3}Is 10 accepted? Yes!What is ({q0, q1}, 1)?({q0 , q1}, 1)= ({q0}, 1) U ({q1}, 1)= {q0} U {q2, q3}= {q0, q2, q3}What is ({q0, q2}, 10)?({q0 , q2}, 10)= (({q0 , q2}, 1), 0)= (({q0}, 1) U ({q2}, 1), 0)= ({q0} U {q3}, 0)= ({q0,q3}, 0)= ({q0}, 0) U ({q3}, 0)= {q1, q2, q3} U {}= {q1, q2, q3}({q0}, 101)= (({q0}, 10), 1)= ((({q0}, 1), 0), 1)= (({q0}