24-25 Fll Semester First Midterm Exmintion ) Give the stte digrm of DFA tht recognizes the lnguge A over lphet Σ = {, } where A = {w w contins or } 2) The following DFA recognizes the lnguge B over lphet Σ = {, }. Descrie B verlly., q q 2 q 3 q 4 q 5 q 6 3) Convert the following NFA to DFA:, q q 2 q 3 ε 4) For the regulr expression [ ( ) ], find n equivlent NFA. 5) Show tht the lnguge A = { 2n n+2 c n 2 n 2} is not regulr.
Answers ), 2) Contins t lest two s OR If it strts with zero, chnges symols t lest 3 times, if it strts with, chnges symols t lest 4 times. 3), q q 23
4) ε ε 5) Assume A is regulr. Let pumping length e p. Consider s = 2p p+2 c p 2 According to pumping lemm, we cn find x, y, z such tht s = xyz. We lso know tht xy p therefore y consists of s only. Suppose y = k. In this cse xy i z = 2p+ik p+2 c p 2 / A We hve contrdiction, so A is not regulr.
24-25 Fll Semester Second Midterm Exmintion ) Find context free grmmr tht recognizes the lnguge over Σ = {, } consisting of strings of odd length where first, middle nd lst symols re the sme. 2) Find PDA tht recognizes the lnguge { n m m > n}. 3) Consider the following lnguges over Σ = {,, c, d}: { n m c m d n } { n n c m d m } { n m c n d m } ) Which one is non-context free? ) Show tht it is not using pumping lemm. 4) Descrie Turing Mchine deciding the lnguge A = { n n2 n }. 5) Wht lnguge does the following Turing mchine recognize? Is it decider? (Input lphet is: Σ = {, }). Sweep from left to right. IF there is ny fter the first, REJECT. 2. Move hed to strt. Serch for. IF found, cross it. (Replce y ) ELSE, Go to 6. 3. Serch for. IF found, cross it. ELSE, REJECT. 4. Serch for. IF found, cross it. ELSE, REJECT. 5. Go to 2. 6. Move hed to strt. Serch for. IF found, ACCEPT. ELSE, REJECT.
Answers ) S A B A A A A A B B B B B OR S A B A CAC B CBC C 2), ε, ε ε, ε $ ε, ε ε ε, $ ε, ε ε, ε ε 3) ) { n m c n d m } ) Suppose the lnguge is context free. Let p e the pumping length. Choose s s s = n m c n d n where m, n > p. According to pumping lemm, we cn find v, y such tht n m c n d n = uvxyz If v or y contin more thn one type of symol, clerly uv 2 xy 2 z / A ecuse symols re out of order. Therefore v nd y cn consist of single symol only. In this cse, we hve two choices. v must consist of s nd y must consist of c s, or v must consist of s nd y must consist of d s. Both cses violte the rule vxy < p Therefore we cnnot pump this string so the lnguge is not context free.
4). Sweep from left to right. IF there is no, or no, or if they re out of order, REJECT. 2. Move hed to strt. Serch for. IF found, Mrk it. Move right until lnk. Write #. Go to 2. //Write s mny # s s there re s. 3. Serch for #. IF found Mrk it. Unmrk ll mrked s. Shuttle etween s nd s. Mrk one for ech one. IF is not found, Go to 3. IF is not found, REJECT. 4. ELSE (# not found) Move hed to strt. Serch for. IF found, REJECT. ELSE, ACCEPT. 5) It is decider, it cn not enter ny infinite loops. Its lnguge is: { n m m 2n + }.
24-25 Fll Semester Finl Exmintion ) Give the stte digrm of DFA tht recognizes the lnguge A over lphet Σ = {, } where w 2 nd the first two nd the lst two digits of w re identicl. For exmple: A, A ut / A, / A 2) Convert the following grmmr into Chomsky norml form: S AA A B B ε B A 3) Let A e the lnguge in {, } mde of strings where the numer of zeros is t lest 3 times the numer of ones. Descrie Turing Mchine recognizing A. 4) You re given set of n distinct positive integers. You wnt to determine if there re two integers p, q in the set such tht p = q 2. Write n lgorithm in pseudo-code for this prolem. Show tht it is in P. 5) Consider the following prolem: Given grph, is there wy to prtition the vertices into 4 susets such tht no two elements in suset re connected? Show tht this prolem is in NP.
Answers ) 2) First eliminte B A, then eliminte A ε nd then rek triples to otin: S Y A AY AC A XB Y D XA Y E Y Y B Y X C Y A D BY E AY X Y
3). Move hed to strt. Serch for. IF found, cross it. (Replce y ) ELSE, ACCEPT. 2. Repet 3 times: Move hed to strt. Serch for. IF found, cross it. (Replce y ) ELSE, REJECT. 3. Go to. 4) INPUT Integer A[], A[2],..., A[n] For i = to n For j = to n If A[i] A[i] == A[j] Return TRUE EndIf EndFor EndFor Return FALSE This lgorithm clerly does Θ(n 2 ) opertions, so it is in P. 5) Suppose there re n vertices. Then, there re t most n(n ) 2 = Θ(n 2 ) edges. We cn check given solution y checking ll edges one y one. (We will return FALSE if the two vertices connected y the edge elong to the sme suset) Therefore given solution cn e verified in Θ(n 2 ) opertions. The prolem is in NP.
Nme-Surnme:..24 ID Numer: CLASSWORK Give the stte digrm of DFA tht recognizes the lnguge A over lphet Σ = {, } where A = {w w contins ut does not contin }.,
Nme-Surnme: 2..24 ID Numer: CLASSWORK Let: A = {Strings of length t lest 3 whose second nd third items re the sme} over lphet Σ = {, }. Give the stte digrm of the DFA tht recognizes this lnguge.,,,
Nme-Surnme: 5..24 ID Numer: CLASSWORK 2 Find regulr expression equivlent to the lnguge recognized y the following NFA: q q 2 q 3 q 4 q 5 ( ) ( )
Nme-Surnme: 6..24 ID Numer: CLASSWORK 2 Find regulr expression equivlent to the lnguge recognized y the following NFA: q q 2 q 3 q 4 [ () ] ( ) ( )
Nme-Surnme: 5..24 ID Numer: CLASSWORK 3 ) Find PDA tht recognizes the lnguge { n 2 n+2 n } ) Find CFG generting the sme lnguge., ε, ε ε, ε $, ε, ε ε, $ ε S S 2 2
Nme-Surnme: 6..24 ID Numer: CLASSWORK 3 ) Find PDA tht recognizes the lnguge { n 2 c 3 d n n } ) Find CFG generting the sme lnguge., ε ε, ε $, ε ε, ε ε c, ε ε ε, $ ε c, ε ε c, ε ε d, ε S Sd 2 c 3
Nme-Surnme: 2..24 ID Numer: CLASSWORK 4 Use the pumping lemm to show tht the following lnguge is not context free: A = { n c 4n n n } Suppose A is context free. Let p e the pumping length. Choose s s s = p c 4p p. According to pumping lemm, we cn findv, y such tht p c 4p p = uvxyz If v or y contin or c, clerly uv 2 xy 2 z / A ecuse it is not in the given formt. Therefore v nd y cn consist of s only. But in this cse, we cn pump t most two of the prts including. The size of the third prt will remin the sme, so pumped string will not e in A. For exmple, v =, y = will result in uv 2 xy 2 z = p+ c 4p+4 p / A Therefore we cnnot pump this string nd A is not context free.
Nme-Surnme: 3..24 ID Numer: CLASSWORK 4 Use the pumping lemm to show tht the following lnguge is not context free: A = { n m c 2n d 2m m, n } Suppose A is context free. Let p e the pumping length. Choose s s s = n m c 2n d 2m nd n, m p. According to pumping lemm, we cn find v, y such tht n m c 2n d 2m = uvxyz If v or y contin more thn one type of symol, clerly uv 2 xy 2 z / A ecuse it is not in the given formt. Therefore v nd y cn consist of single symol only. If v consists of s, then y must consist of (twice s mny) c s. If v consists of s, then y must consist of (twice s mny) d s. In othe cses, vxy > p nd the third condition of pumping lemm is violted. Therefore we cnnot pump this string nd A is not context free.
Nme-Surnme: 9..24 ID Numer: CLASSWORK 5 Descrie Turing Mchine tht gives the output x m, x m,..., x 2, x for the input x, x 2,..., x m, x m.. Move to Strt. Find the first element tht is not crossed. Cll it. Cross it. If not found, Go To 4. 2. Move right until meeting lnk or cross. Move left. Red the element. Cll it. 3. Swp nd if they re different. Cross nd. Go To. 4. Sweep from left to right. Restore ll crossed elements. Return.
Nme-Surnme: 2..24 ID Numer: CLASSWORK 5 Descrie Turing Mchine tht gives the output x, x 2,..., x m, x, x 2,..., x m for the input x, x 2,..., x m.. Move to Strt. Find the first element tht is not crossed. Cross it. If not found, Go To 4. 2. Move right until meeting lnk. 3. Write the element found in. Cross it. Go To. 4. Sweep from left to right. Restore ll crossed elements. Return.
Nme-Surnme: 26..24 ID Numer: CLASSWORK 6 Let A e the lnguge in {, } mde of strings contining n equl numer of s nd s. Descrie Turing Mchine recognizing A. Is it decider?. Move hed to strt. Serch for. 2. IF there s, Cross it. Move hed to strt. Serch for. IF there is, Cross it. Go to. ELSE REJECT. 3. IF there s no, Move hed to strt. Serch for. IF there is, REJECT. ELSE ACCEPT. This mchine stops fter finitely mny steps, ecuse loops re repeted (t most) s mny times s the numer of symols in the input. It is decider.
Nme-Surnme: 27..24 ID Numer: CLASSWORK 6 Let B e the lnguge in {, } mde of strings contining more s thn s. Descrie Turing Mchine recognizing B. Is it decider?. Move hed to strt. Serch for. 2. IF there s, Cross it. Move hed to strt. Serch for. IF there is, Cross it. Go to. ELSE ACCEPT. 3. IF there s no, REJECT. This mchine stops fter finitely mny steps, ecuse loops re repeted (t most) s mny times s the numer of symols in the input. It is decider.
Nme-Surnme:.2.24 ID Numer: CLASSWORK 7 Let A e the set of ll 2 2 mtrices with entries from Q. Show tht A is countle. We know tht Q is countle. Suppose list of Q is {q, q 2,...}. We cn count Q Q y mking n infinite tle nd counting digonlly. Suppose we otin list S = {s, s 2,...}. Now mke tle for S S. The list will give elements of A = Q Q Q Q.
Nme-Surnme:.2.24 ID Numer: CLASSWORK 7 Let B e the set of ll finite strings sed on the lphet {,,..., 9}. Show tht B is countle. The following gives list of ll finite strings tht cn e written with this lphet: String. 9. 99.
Nme-Surnme: 24.2.24 ID Numer: CLASSWORK 8 STRING MATCHING: You re given string of length n. You re lso given word ( second string) of length k, where k n. You wnt to determine if the word occurs in the string. Show tht the string mtching prolem is in P. INPUT String A[n], Word B[k] For i = to n k + test = TRUE For j = to k If A[i + j ] B[j] test = FALSE Brek EndIf EndFor If test == TRUE Return TRUE EndFor Return FALSE This lgorithm uses nk steps therefore it is Θ(n 2 ). So it is in P.
Nme-Surnme: 25.2.24 ID Numer: CLASSWORK 8 You re given n distinct positive integers where n 3. You wnt to find the third lrgest integer. Show tht this prolem is in P. INPUT A[n] For i = to 3 mx = A[] For j = 2 to n If A[j] > mx mx = A[j] index = j EndIf EndFor A[index] = EndFor Return mx This lgorithm uses 3n steps therefore it is Θ(n). So it is in P.
Nme-Surnme: 24.2.24 ID Numer: CLASSWORK 9 The SHORTEST PATH prolem is defined s follows: You re given weighted undirected grph G, nd nodes s nd t in the grph nd numer W. Is there pth from s to t with weight W? Show tht this prolem is in NP. A given solution contins n edges. We cn check ech edge in n steps if ech vertex hs its list of edges. Then we hve to check the sum. Therefore we do (n ) 2 +n opertions, so verifying lgorithm is Θ(n 2 ). This is polynomil, so the prolem is in NP.
Nme-Surnme: 25.2.24 ID Numer: CLASSWORK 9 The MINIMUM SPANNING TREE prolem is defined s follows: You re given weighted undirected grph G nd numer W. Is there spnning tree with weight W? Show tht this prolem is in NP. A given solution contins Θ(n) edges. We cn check ech edge in Θ(n) steps if ech vertex hs its list of edges. (If we hve n unsorted list of edges, this will require n 2 opertions ut still, it is polynomil) Then we hve to check the sum nd lso we hve to check tht ll vertices re connected. Verifying lgorithm is Θ(n 2 ). This is polynomil, so the prolem is in NP. Prepred y: Dr. Emre Sermutlu (25)