CS3012 Formal Languages Exercises CS3012 Formal Languages Exercises 8: Proof by induction (note: I won’t ask proof by induction in the exam) 1. Consider the language L defined by the following grammar: S -> aSb | bSa | abS | baS | Sab | Sba | (i) (ii) Prove by induction that wL only if w has equal numbers of a’s and b’s. Prove by counterexample that the following statement is false: “wL if w has equal numbers of a’s and b’s” Answer: 2. Consider the language L defined by the following grammar: S -> aSb | bSa | aSa | bSb | (i) (ii) Prove by induction that wL if w has equal numbers of a’s and b’s. Prove by counterexample that the following statement is false: “wL only if w has equal numbers of a’s and b’s” Answer: 3. Consider the language L defined by the following grammar: S -> (S) | SS | Prove by induction that wL if and only if w is a string of properly paired round brackets. Remember: this is a two way implication; you need to prove both "if" and "only if". Answer: P=language of properly paired round brackets "if" induction on length of string true for length 0 : S -> assume any element of P of length <=N is in L say we are given any string w of length N+2 from P w must start with (, and there must be a ) to match this this ) can be easily found by scanning through the string, starting after the first (: set a counter to 0 every time we meet ( increase the counter, every time we meet ) decrease the counter, if the counter goes negative then we have just found it now the bit of string between these (…) is shorter than N so it must be in L, call it x likewise, whatever’s left after the ) must be a (possibly empty) string of P which is shorter than N so it must also be in L, call it y CS3012 Formal Languages Exercises so w=(x)y and we know S->*x and S->*y all we need to do is S->SS->(S)S->*(x)S->*(x)y=w and this is true for any w we pick from P we have shown that if it’s true that any string from P of length <=N is in L, then it’s true that any string from P of length N+2 is also in L we have shown it’s true for length 0, therefore it is true for length 2,4,6,8,10…. it is obvious that all strings in P have even length, therefore everything in P is also in L "only if" induction on the length of derivation true for length 1: S -> assume all derivations of length <=N generate strings which are in P say we have some derivation of length N+1 it can start in two possible ways: 1. S->SS->… where what follows must be a derivation of length <=N for each of the S’s, meaning that each of those S’s are producing strings from P. Clearly if each of those S’s produces something from P, then if we concatenate them we still have a string of P. 2. S->(S)->… where what follows must be a derivation of length N for the inner S, meaning that the inner S is producing a string from P. Clearly if the inner S produces something from P, then if we put brackets surrounding it we still have a string of P. we have shown that if it’s true that any derivation of length <=N produces a string of P, then it’s true that any derivation of length N+1 also produces a string of P we have shown it’s true for length 0, therefore it is true for length 1,2,3,4,5…. 4. Consider the language L defined by the following grammar: S -> aS | aSbS | Prove by induction that wL if and only if every prefix of w is a string with at least as many a’s as b’s. Remember: this is a two way implication; you need to prove both "if" and "only if". Answer: