- GATE CSE
Transcription
- GATE CSE
WWW.GATECSE.COM Finite Automaton The finite automaton has complete absence of memory outside its fixed central processor. It delivers no output at all, except on indication of whether or not the input is considered acceptable. A deterministic finite state transducer is a device much like a DFA except that its purpose is not to accept strings or languages but to transform input strings into output strings. For every NDFA, there exists a DFA which stimulates the behavior of NDFA. NDFA is a more genera machine without being powerful. If an NDFA had n states, the corresponding finite automaton has 2” states. However, we need not construct for all these 2” states, but only for those states reachable from [q0] Two automata are considers to be equivalent if they accept the same language; even though they may use the different methods to do so. The class of languages accepted by finite automate is the same as the class of regular languages- these that can be described by regular expressions. A language is regular if and only if it is accepted by a finite state automaton. For only language L:L=L = The class of languages accepted by finite automata is closed under union, concentration, Kleen Star, complementation, intersection and transpose operation. Identities for regular expressions: 1) 2) 3) +R=R R=R = R= R = R WWW.GATECSE.COM 4) ʹ = and ʹ = 5) R+R = R 6) Rʹ Rʹ= Rʹ 7) RRʹ= Rʹ R 8) (Rʹ)ʹ = Rʹ 9) + RRʹ= Rʹ= + RʹR 10) (PQ)ʹ P = P(QP)ʹ 11) (P+Q) ʹ = (PʹQʹ) = (Pʹ+ Qʹ)ʹ 12) (P+Q) R = PR+QR And R(P+Q) = RP+RQ Arden’s theorem : Let P and Q be two regular expressions over . If P does not contain , then the following equation in R :R= Q+ RP Has a unique solution given by :R= QPʹ Transition systems containing - moves:Suppose we want to replace a - moves from vertex 1 to vertex 2. Then we proceed as follows:Step 1- find all edges starting from 2 Step 2- duplicates all these edges starting from 1, without changing the edge labels. Step 3- if 1 is an initial state, make 2 also an initial state Step 4- if 2 is a final state; make 1 as the final state. Example: WWW.GATECSE.COM Finding the regular expression recognized by a transition system: The following assumptions are made regarding the transition system: i) The transition graph does not have - moves. ii) It has only one initial state, say 1 Example: q1= q1 a + q2 b + it will come only with initial state (i) q2=q1 a + q2b + q3 a (ii) q3= q2 a (iii) Placing value of q3 in eq- (ii):q2 = q1 a +q2 b + q2 a q2 = q1 a + q2 (b+aa) q2 = q1 a (b+ aa)ʹ (iv) substantially q2 from equation (iv) in eq (i) : WWW.GATECSE.COM q1= q1 a + q1a (b+aa)* b + q1= + q1 ( a+a (b+aa)* b) q1= (a+a (b+aa)* b)* Now: q2 = q2 a =q1a( b+aa)* a = (a+a(b+aa)* b)*a (b+ aa)* a --Equivalency of two finite automata: two finite automata over they accept the same set of strings over are equivalent of Example: consider the following to DFAs M and Mʹ over {0, 1}. Find out whether M and Mʹ are equivalent. Solution: WWW.GATECSE.COM The first element of the first column must be (q1, q2), i.e initial state of M and Mʹ As we do not get a pair (q, qʹ), where q is a final state and qʹ is a non final state (or vice versa) at every raw, we proceed until all the elements in the second and third column are also in first column. Therefore M and Mʹ are equivalent. Minimization of finite automata: Definition1- two stated q1 andq2 are equivalent if both (q1, x) and final states, or both of them are non final states for all x ʹ. (q2, x) are Definition 2- two states q1 and q2 are k.equivalent (k20) if both (q2, x) are final states or both non final states for all strings x of length k or less. In particular, any to final states are o-equivalent Example : construct the minimum state automating equivalent to the transition diagram given below: Solution WWW.GATECSE.COM WWW.GATECSE.COM Two- way finite automation: It is like a deterministic finite automation except that the reading head can go backwards as well as forwards on the input tape. It is a quintuple ( , , ,q0, F), where is a function from or indicators the direction of head movement. to { , }; A configuration (p, u, v) indicates that the machine is in state p with u to the left of the head. If v=A, configuration (p,u, A) means that it has completed its operation on u and ended up in state p. Formal languages Language : A language generated by a grammer G ( denoted by L(G)) is defined as: x {w ʹ | S w}. G If w contains non-terminals, it is called a sentential form of G. If w does not contain non-terminals, it is called as a sentence of G. OR L(G) is the set of all terminal strings donated from the start symbol S. # two grammers are equivalent if they produce the same language. WWW.GATECSE.COM Chomsky classification of languages:Type 0: A type 0 grammar is only phrase structured grammar without any restriction. A production without any restriction is called a type o production. Type1: a production of the form A is called a type -1 production if In type-1 productions erasing of A is not permitted. Example: i) AB A bcd is a type-1 production. The left context is A , right context is . ii) A abA is a type-1 production. Here both the left and right context are . A grammer is called type -1 or context sensitive If all its productions are type-1 procudtions. The production S is also allowed in a type-1 grammer, but in this case S does not appear on the right hand side of any production. The language generated by a type-1 grammer is called a type-1 or context sensitive language. In context sensitive grammer G , we allowed S for including in L(G). apart from S , all the other productions do not decrease the length of the working string (if , then | | | |). # BC CB is not context sensitive. Type -2 A type-2production is a production of the form A , where A VN (VN U . in other words, the L.H.S has no left context and right context. Example: - S Aa, Aa, Babc, A A grammer is called a type-2 grammer if it contains only type-2 procutions. It is also called a context free grammer. A language generated by a context- free grammer is called a type-2 language or context free language. WWW.GATECSE.COM Type-3 A production of the form A a or A aB, where A, B VN and a , is called a type-3 production. A grammer is called a type -3 pr regular grammer if all the productions are type-3 production. A production S is allowed in type-3 grammer, but in this case S close not appear on the right hand side of any production. A language is regular if it can be described by a regular expression. Relation between languages: # LRL LC.F.L LC.SL L0 # two grammer of different types may generate the same language. # LC.F.L is not closed under intersection, but the intersection of a context – free language and regular language is context-free. Construction of a regular grammer from a given DFA: Example :- construct a regular grammer G generating the regular set represented by P= a*b (a+b)* Solution : DFA corr esponding to P is :- WWW.GATECSE.COM Regular grammer corresponding to P is :- Construction of a transition accepting a regular grammer G:Example : - Solution Example : S a S| a Solution Property of languages that are regulars: As a string is scanned left to right, the amount of memory that is required in order to determine at the end of whether or not the string is in the language must be bounded, fixed in advance and dependent on the language, not the particular input string. Regular languages with an infinite number of strings are represented by automata with cycles and regular expressions involving the kleen star. It means they must have some respective structure. WWW.GATECSE.COM Example: L(G)= { aⁿ : n≥ 1 is a prime} is not regules. L={ w equal no of aʹs and bʹs} is not regular. {a, b}ʹ: wʹ has an L= aʹbʹ is regular. # every finite subset of a non-regular set is regular. Ambiuty in C.F.G: A terminal string w for w. L(G) is ambigous if there exists two or more derivation treas A context free grammer G is ambiguous if there exists some w ambiguous. L(G), which is Construction of reduced grammers: # for every CFG G there exists a reduced grammer Gʹ which is equivalent to G. We construct the reduced grammer in two steps: Step-1 we construct a grammer G, equivalent to the given grammer G so that every variable in G1 derived some terminal sring. Step -2 We construct a grammer Gʹ equivalent to G1 so that every symbol in Gʹ appears in some senstential form of Gʹ. Gʹ is the reduced grammer. Example: find a reduced grammer equivalent to the grammer G, whose production are : SAB |CA, B BC|AB, Aa, CaB|b Solution Step -1 W= { A, C} is Aa and Cb are procuction with a terminal string on R.H.S. W2= {A, C}U {A1|A1 with (Σ U {A,C})*} WWW.GATECSE.COM = {A, C} U{S} = {A, C, S} W3 = {A, C, S} U {A1 | A1 with (Σ U {A, C,S})*} = {A, C, S} U = {A, C, S} = W2 VNʹ = W2= {S, A, C} Pʹ = {A1 (VNʹ U Σ)*} | A1, = {SCA, A a1 C b} Thus, G1 =({S, A, C}, {a, b} { SCA, Aa, c b}S) Step -2 : W1 = {S} W2 = {S} U {A, C} = {S,A, C} W3 = {S, A, C } U {a, b} = {S, A, C, a, b} VNʹʹ= VNʹ Ո W3 = {S, A, C} Σʹʹ= Σʹ Ո W3 = {a, b} Pʹʹ= {A1 | A1 W3} = Pʹ Therefore:Gʹ= ({S, A, C}, {a, b}, {SCA, Aa, Cb}, S) Elimination of Null productions: A production of the form A˄, where A is a variable is called a null production. A variable A is a context- free grammer is nullable if A ˄ WWW.GATECSE.COM If G= (VN, Σ, P, S) is a context- free grammer, then we can find a context-free grammer G, having no null producions such that : L(G) =L(G) – {˄} Example : consider the grammer G whose productions are S aS | AB, A ˄, B˄, D b, construct a grammer G, without null productions generating L(G){˄}. Solution : Step 1: construction of the set W of all nullable variables: W1 = {A1 VN | A1 ˄ is a production in G} = {A,…} W2 = {A, B} U {S} as S AB is a production with AB W1* = {S, A, B} W3= W2 U = W2 Step-2 construction of Pʹ i) ii) iii) D b is included in Pʹ as b W2 S aS gives rise to SaS and S a SAB gives rise to S AB, SA and SB (Note: we can not erase both the nullable variables A and B in S AB as we will get S˄ in that case) The required grammer without null productions is : G1 =({ S, A, B, D}, {a, b}, Pʹ, S) where Pʹ consists of : D b, S aS, S AB, S a, S A, SB Elimination of unit productions: A unit production in a context-free grammer G is a production of the form A B, where A and B are variables in G. If G is a context-free grammer, we can find a context-free grammer G1 which has no null or unit productions such that- L(G1)-L(G). Let A be any variable in VN , then : WWW.GATECSE.COM Step-1: construction of the set of variables derivable from A. Define Wi (A) recursively as follows: W0 (A)= {A} Wi+1(A) = Wi(A) U {B VN | CB is in P with C Wi(A)} Step-2 construction of A-productions in G1. The A- productions in G1 are either: a) The non-unit productions in Gʹ or b) A whenever B is in G with B W(A) and VN. Now we define G1= (VN, Σ, P1, S), where P1 is construted using step-2 for every A VN Example : let G be S AB, Aa, B C |b, CD, D E and Ea. eliminate unit productions and get an equivalent grammer. Solution : Step-1 W0(S)-{S}, W1(S)= W0 (S) U = W0 (S). Hence W(S) = {S}. Similarly , W(A)= {A}, W(E)= {E} W0(B) ={B}, W1(B)= {B}U{C}= {B,C}. W2(B)= {B, C}U{D}= {B,C, D} W3= (B)= {B,C,D}U{E}= {B,C,D,E} Therefour W(B) = {B,C, D, E} Similarly W(C)= {C,D,E} W(D)= {D,E} Step-2 the productions in G1 are: SAB, Aa, Ea, B a|b, Ca, Da Corrolary : if G is a context-free grammers, we can construct an equivalent grammers Gʹ which is reduced and has no null productuons or unit productions. Proof: we construct Gʹ in the following way : Step-1 : eliminate null productions to get G1 WWW.GATECSE.COM Step-2: eliminate unit productions in G1 to get G2 Step -3 construct a reduced grammer Gʹ equivalent to G2. Chomsky normal form for context- free grammers: A context-free grammer G is in chomsky normal form if every production is of the form:Aa, or ABC, and S˄ is in G if ˄ L(G). When ˄ is in L(G), we assume that S does nt appear on the R.H.S of any production. Reduction to chomsky normal form: Step-1 elimination of null productions and unit productions. Step-2 elimination of terminals on R.H.S : we define G1=( VNʹ, Σ,P,S), where P1 and VNʹ are constructed as follows: i) All the productions in P of the form Aa or ABC are included in P1. All the variables in VN are included in VNʹ. ii) Consider AX1X2,…,Xn with some terminal on R.H.S if Xi is a terminal, say ai, add a new variable ai to VNʹ and a ai to P1, in production AX1X2,..,Xn, every terminal on the R.H.S is replaced by the corresponding new variable and the variables on the R.H.S are retained. The resulting on the R.H.S to P1. Thus we get : G1=( VNʹ, Σ,P1,S). Step-3 restricting the number of variables on R.H.S We define G2=( VNʹ,Σ,P2,S) as follows : i) ii) All prouctions in P1 are added to P2 if they are in required form. All the variables in VNʹ are added to VNʹʹ. Consider AA1,A2,..,Am, where m23. We introduce, new productions AA1C1, CiA2C2, …, m-2 Am-1, Am, and new variables C1,C2,..Cm-2. These are added to Pʹʹ and VNʹʹ, respectivly. Example: reduce the following grammer G to CNr. G is SaAD, Aab| bAB, , Bb, Dd. WWW.GATECSE.COM Solution: Step-1: there are no null or unit productions. Step-2 : let G1=( VNʹ,{a,b,d}, P1,S), where P1 and VNʹ are constructed as follows. i) ii) Bb, Dd are included in P1. SaAD gives rise to S aAD and a AaB gives rise to A AbAB gives rise to A AB and b VNʹ ={S,A,B, D, a, } Step-3 P1={S a AD, A b AB, Bb, Dd, a, bb} A a B, B b, D d, a, bb are added to P2. S a AD is replaced by S and AD. A AB is replaced by A and AB. Now , G2 = , where P2= {S | AD, AB, B d, D d} a, b. Greibach Normal Form: A context free grammer generating the set accepted by a pushdown automaton is in Greibach Normal Form. A context free grammer is in Greibach Normal Form if every production is of the form Aa , where VNʹ and a Σ ( may be ˄), and S ˄ is in G if ˄ L(G). when ˄ L(G), we assume that S doen not appear on the R.H.S of any production. Lemma -1: deleting a variable B appearing as the first symbol on the R.H.S of some A- production provided no B- production has B as the first symbol on the R.H.S. If P={ABr, B }, then P1= { A } Lemma -2: eliminationg left recursion. Let P= {AA 1 |A A A Z| 1Z| 2Z…| }, then P1= { Z, A WWW.GATECSE.COM Example: remove left- recursion from the following grammer : A aBD | bDB|c, AAB|AD Solution : A aBD |bDB|c, AaBDZ | bDBZ| cZ, Z BZ| DZ| B| D. Example: remove ˄- productions from the following grammer : SASB | ˄, A aAS | AB| a|˄, BSBS| Aa| b Solution :Step-1 construction of set W of all nullable variables:W0= {S, A}, W1= {S, A}= W0 S0 W= {S, A} Step-2: construction of P:S ASB| SB|AB|B AaAS| aS| aA|AB| B|a BSbS| Sb|bS| b| Aa|a # L= {aⁿbⁿcⁿ |n≥1} is not context-free but context sensetive. # L= { aᵖ | is a prime} is not context-free Pushdown Automata:Each context-free language is accepted by some pushdown automaton. If a langiage is accepted by a pushdown automaton, it is a context-free language. Pushdown Automaton for {aⁿbⁿ | n≥1}:Let A = ( Q, Σ, T, , q0, Z0, F) Set of pushdown symbols. Q (Σ U {˄}) T Q T* Where : Q= {q0, q1,qf}, Σ= {a1,b}, T={a,Z0}, F={qf}, and (q0, a, Z0)= (q0,aZ0) ( q0, a1a) = (q0,aa) is given by:- WWW.GATECSE.COM ( q0, b,a) = (q1, ˄) (q1, ˄, Z0)= (qf, ˄) # infinite context-free languages display periodicity of a somewhat subteler form than do regular languages. If A is a pda accepting L by empty store, we can find a pda B, which accpets L by final state. If A isa pda accepting L by final state, we can find a pda B, which accepts L by empty store. Determinism and porsing:A pushdown automaton is not of immodiate use in porsing, because it’s a nondeterministic device. A pushdown automaton M is derterministic if for each configuration, there is at most one configuration that can succed it in a computation by M. Deterministic context-free languages are essentially those that are accepted by deterministic pushdown automaton. There are some context-free languages that can not accepted by deterministic pushdown automata. The machine to accept the language { W c Wᴿ : W {a b}*} is deterministic, while the machine to accept {WWᴿ : W c{ab}* } is non deterministic. The class of deterministic context-free languages is closed under complement. Non-deterministic is more powerful than deterministic in the context of pushdown automata. Turing machines and recursive & recursive enumerable languages:Turing machine:A turing machine consists of a finite control, a type and a head that can be used for reading or writing. The turing machines form a stable and maximal class of computational devices, in terms of the computations they can perform. A turing machine M does not accept W if the machine eihter halts in a nonaccepting state or doen not halt. WWW.GATECSE.COM A general purpose turing machine is usually called unieversal turing machine which is powereful enough to stimulate the behaviour of any computer including any turing machine itself. If set of languges accepted by nondeteministic turing machine is same as the set of languaues accepted by deterministic turing machine. Liner Bounded Automaton: A liner bounded automaton is a non deterministic turing machine which has a single tape whose length is not infinite but bounded by a linear function of the length of the input string. In linear bounded automaton, the length of the tape is restricted. If L is a contextsensetive language, then L is accepted by a Linear Bounded Automaton (LAB). The converse is also true. Recursive and recursive enumerable languages:A turing machine M decides a language L if when started with input W, it always halts and does so in a halt state. It is accepts W, it halts in an accepting state and it does not accept W, it halts in non accepting state. A turing machine M semi-decides a language L, if when started with input W, it halts if and only if it accepts W, otherwise it does not halt. A procedure for solving a problem is a finite sequence of instructions which can be mechanically carried out given any input. An algorithm is a procedure that terminates after a finite number of steps any input. Recursive language:A language L is recursive if there is a turing machine that decides it. OR A set X is recursive if we have an algorithm to determine whether a given element belongs to X or not. Recursive enumerable language:- WWW.GATECSE.COM A language L is recursive enumerable if and only if there is a turing machine M that semi decides it. OR A recursive enumerable set is a set X for which we have a procedure to determine whether a given element belongs to X or not. Every recursive language is recursive enumerable but the two classes are not same. Recursive language Recursive enumerable lang The class of recursive languages is a proper subset of the class of recursively enumerable languages. If L is a recursive language, then its complement L is also recursive. A language is recursive if and only if both, it and its complement are recursively enumerable. The class of recursively enumerable languages is not closed under component. A language is generated by a grammer if and only if it is recursively enumerable. Recursive enumerable language= Type-0 language. If L1 is not recursive and there is a reduction from L1 to L2, then L2 is also not recursive. A context-sensitive language is recursive. There exists a recursive set which is not a context-sensitive language. Closure properties of recursive and recursive enumerable languages:Recursive languages are closed under union, concentration, closure, intersection, complementation and set difference. If L, L1 and L2 are recursive languages then so are L1 UL2 L1L2 L. L1Ո L2, L1L2 and Σ-L. Recursive enumerable languages are closed under union concentration, closure and intersection. If L, L1 and L2 are recursive numerable, then so are L1 UL2 L1L2 Lʹ and L1Ո L2. WWW.GATECSE.COM If LR is a recursive language and LRE is a recursively enumerable language then:i) ii) iii) LR U LRE, LR Ո LRE , LR LRE, LRE LR, are recursive enumerable. LRE – LR is recursive enumerable. LR - LRE is not recursive enumerable. Halting problem: Halting machine takes as input an encoding of a turing machine e(M) and an encoding of an input string e(W), and returns “yes” if M halts on W, and “no” if M does not halt on W. The halting problem is undecideble: There exists no turing machine that decides it. There is no turing machine that halts on all input, and always say “yes” if M halts on W, and always say “no” if M does not halt on W. The following problem about Turing machines are undecidable: i) Given a turing machine M, is the language that M semi decides regular? is it content free? Is it recursive? Reduction :To solve a problem B is undecidable:i) ii) iii) Start with an instance of a known undecidable problem. Create an instance of problem B, such that the answer to the instance of problem B gives the answer to the undecidable problem. If we would solve problem B, we would solve halting problem. Thus, problem B must be un decidable. If we can recduce problem A to problem B, and problem A is un decidable, then:Problem B must also be undecidable. If we can reduce problem B to problem A and problem A is undecidable then :- it tells nothing about problem B. WWW.GATECSE.COM Unsolvable problems about grammars:Each of the following problems is un decidable:1) 2) 3) 4) Given a grammer G and a string W, is W L(G)? Given grammers G, is ˄ L(G)? Given grammers G1 and G2, is L(G) = L(G1)? Given grammer G, is L(G)= The following questions about context- free grammers are decidable:1) Given a C.F.G G and string w, is w L(G) 2) Given a CFG G, is ˄ L(G) Some important results 1) Regular expression for all strings over {0,1} that:i) Have an even no. of is = 0 * (1 0* 10*)* ii) Have no occurrences of 00 = 1* (011*)* (0+1*) iii) Have exactly one occurrences of 00 = 1*(011*)* 00 (11*0)*1* iv) Do not contain 01 = 1*0* 2) 3) 4) 5) 6) 7) 8) 9) L= {aⁿbⁿcⁿ | n≥1} is not context-free, but context invisible. L= { aᵖ | p is a prime} is not a context- free language. L= {0ⁿ1ⁿ | n≥ 1} is not regular, but context-free. L= { ww | w {a,b}*} is not context-free. L= { aⁿ ᶻ | n ≥ 0} is not context-free. L= {a ᶻⁿ | n ≥1} is regular L= {babaabaaa…baⁿ ‾ ˡ baⁿ b | n ≥1} is not context-free L= { w x wᵀ | x1 w c { a,b}*} is regular. 10)L= {wwᵀ | w {a, b}* } is context-free. 11) DFA that accepts all binary strings whose decimal value is divisible by 3. WWW.GATECSE.COM NP Completencess Tractable problems:The set of all the problems that can be solves within polynomial amount of time using deterministic machine. It is of type O (nᵏ). Intractable problems:The set of all problems that cant be solved within polynominal amount of time using deterministic machine. It is of type O(k ⁿ). P- Class problems:The set of problems that can be solved in polynomial amount of time by deterministic machines is called the P-class or polynomial problems. NP-class problems:The set of problems that can be solved in polyniminal time suing non-deterministic machines. NP = Non- deterministic Polynomial Time. NP ≠ Non- polynomial. P NP The class NP consists of those problems that are variable in polynomial time. WWW.GATECSE.COM NP-Hard problems: These problems are at least as- hard as the hardest problems in NP, but not necessarily in NP. The problems, to which all NP-Class problems are reducible in polynomial time, are known as NP-hard class of problems. NP- complete problems:NP- Complete problems are those NP-hard problems which are within NP. These are set of the hardest problems in NP, i.e in real sense these are the ones that most likely not to be in P. WWW.GATECSE.COM NP- complete problems are solvable using heuristic. It is not- known whether NP problems are tractable or interactable. If a problem in a class of NP-Complete can be solved in polynomial time, then all the problems in that class can be solved in a polynomial time using the some algorithm. If a deterministic polynomial time algorithm is found for an NP-Complete problem, then every problem inNP-Class can be solved in deterministic polynomial time. # the class of all polynomial decidable languages, denoted by P is closed under union, intersection, concatenation, and complement and kleene star. Example of NP- complete problems:1) 2) 3) 4) 5) 6) 7) 8) 9) Bounded tiling 3-satisfiabilty / satisfiablity (SAT) MAX-SAT Hamiltonian Cycle Traveling salesman problem Exact Cover and Node Cover Independent Set 0-1 knapsack Bin packing 10) Printed Circuit Board (PCB) 11) CLIQUF