SlideShare a Scribd company logo
1 of 4
Download to read offline
ITK 328 Theory of Computation                                                            Selected Exercise 5.1 Solution


Exercises 5.1

7: A general method used in designing a context-free grammar for a given language is to characterize the language
     into a few sub-languages that can easily find context-free grammars for them. Then, we analyze their relations
     and find a way to combine them into the required language.
     (a) L = {an bm n ≤ m + 3}.
         Here is an easier approach. We use A to generate L1 = {an bm m ≤ n ≤ m + 3} and B to generate
         L2 = {bm 0 ≤ m}. One can verify that L = L1 L2 because attaching L2 to L1 means we can add any
         numbers of b’s to the end of the string in L1 . Thus, m, the number of b’s, is no longer bounded by n, the
         number of a’s.
                                               S → AB
                                               A   → λ       a    aa     aaa      aAb
                                               B   → λ       bB

     (b) L = {an bm n = m − 1}.
         We use A to generate L1 = {an bm     n > m − 1} and B to generate L2 = {an bm          n < m − 1}. It is clear
         that L = L1 ∪ L2 .
                                                   S       → A     B
                                                   A       → λ     aA       aAb
                                                   B       → bb    Bb       aBb

     (c) L = {an bm n = 2m}.
         We use E to generate L1 = {an bm n = 2m}, A to generate non-empty strings of a’s, and B non-empty
         strings of b’s. Thus, AE and EB can generate {an bm n > 2m} and {an bm n < 2m}, respectively.
         respectively.
                                                S → AE EB
                                                       A    → a        aA
                                                       B    → b     bB
                                                       E    → λ        aaEb

     (d) L = {an bm 2n ≤ m ≤ 3n}.
         The idea is simple: Every a generated is companied with two or three b’s. That way, we can keep the
         numbers of a’s and b’s in the required range.

                                                   S   → λ        aSbb      aSbbb

     (e) L = {ω ∈ {a, b}∗ na (ω) = nb (ω)}.
         We partition L in to two sub-languages, {ω ∈ {a, b}∗ na (ω) > nb (ω)} and {ω ∈ {a, b}∗ na (ω) < nb (ω)},
         each can be generated by A and B, respectively. We also design E that generates strings with na (ω) =
         nb (ω) to help us construct A and B.

                                              S    → A       B
                                              A    → a      aA     Aa       AE      EA
                                              B    → b      bB     Bb       BE    EB
                                              E    → λ      aEb     bEa       EE


                                                                                                c Chung-Chih Li   P-1/4
ITK 328 Theory of Computation                                                                     Selected Exercise 5.1 Solution


     (f ) L = {ω ∈ {a, b}∗ na (µ) ≥ nb (µ) where µ is any possible prefix of ω}.
           This language is not as difficult as it appears. If we want to attach one b to the end of a string, we have to
           be sure that there is a corresponding a before it. That is, if µ is already in good standard, we can derive
           aµb, aaµbb aaaµbbb, and so on. Also, if µ and ν are in good standard, so are µν and µa. Thus,

                                                     S→λ          Sa     aSb        SS

     (g) L = {ω ∈ {a, b}∗ na (ω) = 2nb (ω) + 1}.
         We design A to generate strings in which the number of a’s is twice as many as the number of b’s. Then,
         we add an extra a into the strings.

                                 S   → AaA
                                 A → λ        aAab       aaAb     aAba        abAb       bAaa   baAa   AA

8: We use the same principle as the one used in the previous problem.

     (a) L = {an bm ck n = m or m ≤ k}.
         As before, we separate our language into two parts, one satisfies n = m and the other m ≤ k. We use X
         to generate strings with a’s followed by equally many b s. Then, the strings are followed by any number
         of c’s generated by C. For m ≤ k, we generate any number of a’s first, then use Y to generate b follows by
         c. To make sure that c’s will be out number b’s, we make sure each b generated will be companied with
         one c.

                                                         S       → XC         AY
                                                         X       → λ    aXb
                                                         Y       → λ    bY c        Yc
                                                         A       → λ    aA
                                                         C       → λ    cC

     (b) L = {an bm ck n = m or m = k}.
         XC is same as the XC in the previous problem. We create Y to generate strings in which there are more
         b’s than c’s, and Z generate strings in which there are more c’s than b’s,

                                                     S       → XC         AY        AZ
                                                     A       → λ       aA
                                                     C       → λ       cC
                                                     X       → λ       aXb
                                                     Y       → b       bY c    bY
                                                     Z       → c       bZc     Zc

     (c) L = {an bm ck k = n + m}.
         The idea is easy: each c will be companied with one a or one b.

                                                             S    → aSc        B
                                                             B    → λ         bBc



                                                                                                         c Chung-Chih Li   P-2/4
ITK 328 Theory of Computation                                                               Selected Exercise 5.1 Solution


     (d) L = {an bm ck k = n + 2m}.
         The idea is similar to the previous problem except that: each c will be companied with one a but we need
         two c’s for one b.

                                                     S    → aSc           B
                                                     B    → λ           bBcc

     (e) L = {an bm ck k = |n − m|}.
         The condition k = |n − m| is equivalent to (k = n − m or k = m − n). We move one step farther: the
         condition is equivalent to (n = k + m or m = k + n).

                                                    S     → X           YC
                                                    X     → aXc              B
                                                    B     → λ           aBb
                                                    Y     → λ           aY b
                                                    C     → λ           bCc

     (f ) L = {ω ∈ {a, b, c}∗ na (ω) + nb (ω) = nc (ω)}.
           We separate the language into two parts: na (ω) + nb (ω) > nc (ω) and na (ω) + nb (ω) < nc (ω). E is a
           helper that generate intermediate language: na (ω) + nb (ω) = nc (ω).

                                         S   → A     C
                                         A   → a     b    aE       bE    Ea      Eb    AA
                                         C   → c     cE    Ec       CC
                                         E   → λ     aEc       bEc      cEa      cEb   EE

     (g) L = {an bm ck k = n + m}.
         Use the same approach as in the previous question, we consider n + m > k and n + m < k separately.

                                               S   → X         Y
                                               X   → aX         aXc       aB     bB
                                               B   → λ     bB          bBc
                                               Y   → EC
                                               C   → c     cC
                                               E   → aEc           F
                                               F   → λ     bF c

     (h) L = {an bm ck   k ≥ 3}.

                                                     S    → ABC
                                                     A    → λ           aA
                                                     B    → λ           bB
                                                     C    → ccc          cC


                                                                                                   c Chung-Chih Li   P-3/4
ITK 328 Theory of Computation                                                                     Selected Exercise 5.1 Solution


12: Given any context-free grammar G = (V, T, S, P ), repeat the following process until every production rule in P
     is of the format A → BX or A → B, where A ∈ V and B, X ∈ V ∪ T ∪ {λ}, and for every A ∈ V we have
     A → λ.

        • Find a production rule A → ω from P , with |ω| ≥ 2. Let ω = xα, where x ∈ V ∪ T and α ∈ (V ∪ T )∗ .
            1.   Remove A → ω from P .
            2.   Create a new variable X and add X into V .
            3.   Add A → xX into P.
            4.   Add X → α into P.
            5.   Add X → λ into P.

17: The complement of {an bm ck      k = n + m} is as follows:

                         L = {an bm ck   k = n + m} ∪ L(((a + b + c)∗ (ba + ca + cb)(a + b + c)∗ )+ )
      Note that the regular expression above represents those strings in which there exists at least two consecutive
      alphabets that are out of order, because they are strings that are not in {an bm ck k = n + m}. X and Y will
      generate strings just as the X and Y do in problem 8.g.

                                     S   → X       Y       Z
                                     X   → aX          aXc      aB    bB
                                     B   → λ      bB       bBc
                                     Y   → EC
                                     C   → c     cC
                                     E   → aEc         F
                                     F   → λ      bF c
                                     Z   → ba      ca      cb    aZ       bZ   cZ      Za   Zb   Zc

18: L = {ω1 cω2     ω1 , ω2 ∈ {a, b}+ and ω1 = ω2 }.
                                                R

                                                                                       R
      It is clear that if the lengths of ω1 and ω2 are different, then ω1 cannot be ω2 . On the other hand, if ω1 and
      ω2 are of the same length and ω1 cω2 ∈ L, there must be at least one position at which the alphabets in ω1
             R
      and ω2 are different. We use X to generate strings in which ω1 is longer, Y to generate strings in which ω2 is
      longer, and Z for strings in which |ω1 | = |ω2 | but there exists at least one mismatch. We also design a helper
      E that generates strings in which |ω1 | = |ω2 | but there is no guarantee that there exists at least one mismatch
                          R
      between ω1 and ω2 . Before we pass control to E, we have to be sure we already have some necessary condition
                                                                                 R
      for the string to be in the language, i.e., a mismatch between ω1 and ω2 .

                                             S   → X            Y     Z
                                             X   → aX            bX       aE     bE
                                             Y   → Ya            Yb       Ea   Eb
                                             E   → c           aEb    aEa      bEa       bEb
                                             Z   → aEb              bEa    aZa        bZb

22:
                                                  S     → λ          (S)   [S]      SS



                                                                                                         c Chung-Chih Li   P-4/4

More Related Content

Similar to Itk328 ex5 1

Similar to Itk328 ex5 1 (20)

Chapter 3( 3 d space vectors)
Chapter 3( 3 d space vectors)Chapter 3( 3 d space vectors)
Chapter 3( 3 d space vectors)
 
Roots of polynomial equations
Roots of polynomial equationsRoots of polynomial equations
Roots of polynomial equations
 
Simplifiaction of grammar
Simplifiaction of grammarSimplifiaction of grammar
Simplifiaction of grammar
 
Problem Set 1
Problem Set 1Problem Set 1
Problem Set 1
 
Ijetr011923
Ijetr011923Ijetr011923
Ijetr011923
 
Fuvest 2015 - aberta
Fuvest 2015 - abertaFuvest 2015 - aberta
Fuvest 2015 - aberta
 
Sol63
Sol63Sol63
Sol63
 
Update 2
Update 2Update 2
Update 2
 
matrix theory and linear algebra.pptx
matrix theory and linear algebra.pptxmatrix theory and linear algebra.pptx
matrix theory and linear algebra.pptx
 
Calculus Homework Help
Calculus Homework HelpCalculus Homework Help
Calculus Homework Help
 
Differential Equations Assignment Help
Differential Equations Assignment HelpDifferential Equations Assignment Help
Differential Equations Assignment Help
 
Calculus Assignment Help
Calculus Assignment HelpCalculus Assignment Help
Calculus Assignment Help
 
Remodulization of Congruences
Remodulization of CongruencesRemodulization of Congruences
Remodulization of Congruences
 
A Novel Solution Of Linear Congruences
A Novel Solution Of Linear CongruencesA Novel Solution Of Linear Congruences
A Novel Solution Of Linear Congruences
 
Differential Equations Homework Help
Differential Equations Homework HelpDifferential Equations Homework Help
Differential Equations Homework Help
 
Linear Programming
Linear ProgrammingLinear Programming
Linear Programming
 
Maths Assignment Help
Maths Assignment HelpMaths Assignment Help
Maths Assignment Help
 
4.vectors Further Mathematics Zimbabwe Zimsec Cambridge
4.vectors   Further Mathematics Zimbabwe Zimsec Cambridge4.vectors   Further Mathematics Zimbabwe Zimsec Cambridge
4.vectors Further Mathematics Zimbabwe Zimsec Cambridge
 
Problem set2 | Theory of Computation | Akash Anand | MTH 401A | IIT Kanpur
Problem set2 | Theory of Computation | Akash Anand | MTH 401A | IIT KanpurProblem set2 | Theory of Computation | Akash Anand | MTH 401A | IIT Kanpur
Problem set2 | Theory of Computation | Akash Anand | MTH 401A | IIT Kanpur
 
Complex numbers
Complex numbersComplex numbers
Complex numbers
 

Itk328 ex5 1

  • 1. ITK 328 Theory of Computation Selected Exercise 5.1 Solution Exercises 5.1 7: A general method used in designing a context-free grammar for a given language is to characterize the language into a few sub-languages that can easily find context-free grammars for them. Then, we analyze their relations and find a way to combine them into the required language. (a) L = {an bm n ≤ m + 3}. Here is an easier approach. We use A to generate L1 = {an bm m ≤ n ≤ m + 3} and B to generate L2 = {bm 0 ≤ m}. One can verify that L = L1 L2 because attaching L2 to L1 means we can add any numbers of b’s to the end of the string in L1 . Thus, m, the number of b’s, is no longer bounded by n, the number of a’s. S → AB A → λ a aa aaa aAb B → λ bB (b) L = {an bm n = m − 1}. We use A to generate L1 = {an bm n > m − 1} and B to generate L2 = {an bm n < m − 1}. It is clear that L = L1 ∪ L2 . S → A B A → λ aA aAb B → bb Bb aBb (c) L = {an bm n = 2m}. We use E to generate L1 = {an bm n = 2m}, A to generate non-empty strings of a’s, and B non-empty strings of b’s. Thus, AE and EB can generate {an bm n > 2m} and {an bm n < 2m}, respectively. respectively. S → AE EB A → a aA B → b bB E → λ aaEb (d) L = {an bm 2n ≤ m ≤ 3n}. The idea is simple: Every a generated is companied with two or three b’s. That way, we can keep the numbers of a’s and b’s in the required range. S → λ aSbb aSbbb (e) L = {ω ∈ {a, b}∗ na (ω) = nb (ω)}. We partition L in to two sub-languages, {ω ∈ {a, b}∗ na (ω) > nb (ω)} and {ω ∈ {a, b}∗ na (ω) < nb (ω)}, each can be generated by A and B, respectively. We also design E that generates strings with na (ω) = nb (ω) to help us construct A and B. S → A B A → a aA Aa AE EA B → b bB Bb BE EB E → λ aEb bEa EE c Chung-Chih Li P-1/4
  • 2. ITK 328 Theory of Computation Selected Exercise 5.1 Solution (f ) L = {ω ∈ {a, b}∗ na (µ) ≥ nb (µ) where µ is any possible prefix of ω}. This language is not as difficult as it appears. If we want to attach one b to the end of a string, we have to be sure that there is a corresponding a before it. That is, if µ is already in good standard, we can derive aµb, aaµbb aaaµbbb, and so on. Also, if µ and ν are in good standard, so are µν and µa. Thus, S→λ Sa aSb SS (g) L = {ω ∈ {a, b}∗ na (ω) = 2nb (ω) + 1}. We design A to generate strings in which the number of a’s is twice as many as the number of b’s. Then, we add an extra a into the strings. S → AaA A → λ aAab aaAb aAba abAb bAaa baAa AA 8: We use the same principle as the one used in the previous problem. (a) L = {an bm ck n = m or m ≤ k}. As before, we separate our language into two parts, one satisfies n = m and the other m ≤ k. We use X to generate strings with a’s followed by equally many b s. Then, the strings are followed by any number of c’s generated by C. For m ≤ k, we generate any number of a’s first, then use Y to generate b follows by c. To make sure that c’s will be out number b’s, we make sure each b generated will be companied with one c. S → XC AY X → λ aXb Y → λ bY c Yc A → λ aA C → λ cC (b) L = {an bm ck n = m or m = k}. XC is same as the XC in the previous problem. We create Y to generate strings in which there are more b’s than c’s, and Z generate strings in which there are more c’s than b’s, S → XC AY AZ A → λ aA C → λ cC X → λ aXb Y → b bY c bY Z → c bZc Zc (c) L = {an bm ck k = n + m}. The idea is easy: each c will be companied with one a or one b. S → aSc B B → λ bBc c Chung-Chih Li P-2/4
  • 3. ITK 328 Theory of Computation Selected Exercise 5.1 Solution (d) L = {an bm ck k = n + 2m}. The idea is similar to the previous problem except that: each c will be companied with one a but we need two c’s for one b. S → aSc B B → λ bBcc (e) L = {an bm ck k = |n − m|}. The condition k = |n − m| is equivalent to (k = n − m or k = m − n). We move one step farther: the condition is equivalent to (n = k + m or m = k + n). S → X YC X → aXc B B → λ aBb Y → λ aY b C → λ bCc (f ) L = {ω ∈ {a, b, c}∗ na (ω) + nb (ω) = nc (ω)}. We separate the language into two parts: na (ω) + nb (ω) > nc (ω) and na (ω) + nb (ω) < nc (ω). E is a helper that generate intermediate language: na (ω) + nb (ω) = nc (ω). S → A C A → a b aE bE Ea Eb AA C → c cE Ec CC E → λ aEc bEc cEa cEb EE (g) L = {an bm ck k = n + m}. Use the same approach as in the previous question, we consider n + m > k and n + m < k separately. S → X Y X → aX aXc aB bB B → λ bB bBc Y → EC C → c cC E → aEc F F → λ bF c (h) L = {an bm ck k ≥ 3}. S → ABC A → λ aA B → λ bB C → ccc cC c Chung-Chih Li P-3/4
  • 4. ITK 328 Theory of Computation Selected Exercise 5.1 Solution 12: Given any context-free grammar G = (V, T, S, P ), repeat the following process until every production rule in P is of the format A → BX or A → B, where A ∈ V and B, X ∈ V ∪ T ∪ {λ}, and for every A ∈ V we have A → λ. • Find a production rule A → ω from P , with |ω| ≥ 2. Let ω = xα, where x ∈ V ∪ T and α ∈ (V ∪ T )∗ . 1. Remove A → ω from P . 2. Create a new variable X and add X into V . 3. Add A → xX into P. 4. Add X → α into P. 5. Add X → λ into P. 17: The complement of {an bm ck k = n + m} is as follows: L = {an bm ck k = n + m} ∪ L(((a + b + c)∗ (ba + ca + cb)(a + b + c)∗ )+ ) Note that the regular expression above represents those strings in which there exists at least two consecutive alphabets that are out of order, because they are strings that are not in {an bm ck k = n + m}. X and Y will generate strings just as the X and Y do in problem 8.g. S → X Y Z X → aX aXc aB bB B → λ bB bBc Y → EC C → c cC E → aEc F F → λ bF c Z → ba ca cb aZ bZ cZ Za Zb Zc 18: L = {ω1 cω2 ω1 , ω2 ∈ {a, b}+ and ω1 = ω2 }. R R It is clear that if the lengths of ω1 and ω2 are different, then ω1 cannot be ω2 . On the other hand, if ω1 and ω2 are of the same length and ω1 cω2 ∈ L, there must be at least one position at which the alphabets in ω1 R and ω2 are different. We use X to generate strings in which ω1 is longer, Y to generate strings in which ω2 is longer, and Z for strings in which |ω1 | = |ω2 | but there exists at least one mismatch. We also design a helper E that generates strings in which |ω1 | = |ω2 | but there is no guarantee that there exists at least one mismatch R between ω1 and ω2 . Before we pass control to E, we have to be sure we already have some necessary condition R for the string to be in the language, i.e., a mismatch between ω1 and ω2 . S → X Y Z X → aX bX aE bE Y → Ya Yb Ea Eb E → c aEb aEa bEa bEb Z → aEb bEa aZa bZb 22: S → λ (S) [S] SS c Chung-Chih Li P-4/4