Theory of Computer Science Prelim Question Paper Solution...

12
1113/Engg/TE/Pre Pap/2013/CMPN/Soln/TCS 1 Vidyalankar T.E. Sem. V [CMPN] Theory of Computer Science Prelim Question Paper Solution (i) String ending with 110 or 111 Consider 110100110 q 0 110100110 1q 1 10100110 11q 2 0100110 110q 3 100110 1101q 1 00110 11010q 0 0110 110100q 0 110 1101001q 1 10 11010011q 2 0 11010011q 3 accepted (ii) String containing 010 as a substring 0 1 q 0 q 1 q 0 q 1 q 1 q 2 q 2 q 3 q 0 q 3 q 3 q 3 consider 111001011 q 0 111001011 1q 0 11001011 11q 0 1001011 1. (a) Fig. (a) : String ending with 110 or 111 Fig. (b) : Transition table q 0 q 1 q 2 q 3 1 0 0 1 0 0, 1 1 Vidyalankar

Transcript of Theory of Computer Science Prelim Question Paper Solution...

1113/Engg/TE/Pre Pap/2013/CMPN/Soln/TCS 1

Vidyalankar T.E. Sem. V [CMPN]

Theory of Computer Science Prelim Question Paper Solution

(i) String ending with 110 or 111 Consider 110100110 q0110100110 1q110100110 11q20100110 110q3100110 1101q100110 11010q00110 110100q0110 1101001q110 11010011q20 11010011q3 accepted (ii) String containing 010 as a substring

0 1

q0 q1 q0

q1 q1 q2

q2 q3 q0

q3 q3 q3

consider 111001011 q0111001011 1q011001011 11q01001011

1. (a)

Fig. (a) : String ending with 110 or 111 Fig. (b) : Transition table

q0 q1 q2 q3

1 0

0

1 00, 1

1

Vidyala

nkar

Vidyalankar : T.E. − TCS

1113/Engg/TE/Pre Pap/2013/CMPN/Soln/TCS 2

111q0001011 1110q101011 11100q11011 111001q2011 1110010q311 11100101q31 111001011q3 Accepted

L = k| {0, has atleast three consecutive zeros.

RE = 000 (0 + 1)K 000 (0 + 1)K 000 zero’s can be at any place viz at start, end or in between. There is a difference between recursively enumerable (Turing Acceptable) and recursive (Turing Decidable) language. Following statements are equivalent : 1. The language L is Turing acceptable. 2. The language L is recursively enumerable. Following statements are equivalent 1. The language L is Turing decidable. 2. The language L is recursive. 3. There is an algorithm for recognizing L. Every Turing decidable language is Turing acceptable. Every Turing acceptable language need not be Turing decidable. Turing Acceptable Language:

A language L is said to be Turing Acceptable language if there is a Turing machine M which halts on every L with an answer ‘YES’. However, if L, then M may not halt. Turing Decidable Language :

A language L is said to be turing being decidable if there is a turing machine M which

always halts on every . If L then M halts, with answer ‘YES’ and if L then M halts, with answer ‘NO’. A set of solutions for any problem defines a language.

A problem P is said to be decidable / solvable if the language L representing the problem (set of solutions) is turing decidable. If P is solvable / decidable then there is an algorithm for recognizing L, representing the problem. It may be noted that an algorithm terminates on all inputs. Following statements are equivalent : 1. The language L is Turing decidable. 2. The language L is recursive. 3. There is an algorithm for recognizing L. Remarks : 1. Every turing decidable language is turing acceptable. 2. Every turing acceptable language need not be turing decidable.

3. A language L many not be turing acceptable and hence not turing decidable. Thus we cannot design a turing machine / algorithm which halts for every L.

1. (b)

1. (c)

Vidyala

nkar

Prelim Question Paper Solution

1113/Engg/TE/Pre Pap/2013/CMPN/Soln/TCS 3

abb by aba Mealy m/c Transition Table

a b q0 q1 a q0b q1 q1a q2b q2 q1a q0a

Moore m/c Step 1 : Simplification of grammar Symbol S is nullable. After removing productions, the set of productions is given by S ASB | AB A AaS | Aa | a B SbS | Sb | bS | b | A | bb Unit production B A is removed, the resulting set of productions is given by S ASB | AB A AaS | Aa | a B SbS | Sb | bS | b | AaS | Aa | a | bb Step 2 : Every symbol in , in productions of the form A where || 2 should be a variable. This can be done by adding two productions. Ca a and Cb b

The set of productions after the above changes is S ASB | AB A ACaS | ACa | a B SCbS | SCb | CbS | b | ACaS | ACa | a | CbCb Ca a Cb b

2. (a)

q0 q1 q2a/a b/b

a/a

b/a

a/a b/b

q0 q2 q3 q4

q1

E a b

bb

a

a

a

a

b

b

b

2. (b)

Vidyala

nkar

Vidyalankar : T.E. − TCS

1113/Engg/TE/Pre Pap/2013/CMPN/Soln/TCS 4

Step 3 : Finding an equivalent CNF

Original production Equivalent production in CNF

S ASB S AC1 C1 SB

S AB S AB A ACaS A AC2

C2 CaS A ACa A ACa A a A a B SCbS B SC3

C3 CbS B SCb| CbS|b S SCb | CbS | b B ACaS B AC2 B ACa | a | CbCb B ACa| a | CbCb

Ca a Ca a Cb b Cb b

(i) Step 1: Let us assume that L is regular and L is accepted by a FA with n states. Step 2: Let us choose a string

= an bn | | = 2n n Let us write w as xyz, with | y | > 0 and |xy| n Since, |xy| n, y must be of the form ar | r > 0 Since, |xy| n, x must be the form as. Now, anbn can be written as

Step 3: Let us check wheterh xyiz for i = 2 belongs to L.

xy2z = as 2r n s r n s 2r n s r na a b a a a b

= s 2r n s r n n r na b a b

Since r > 0, number of a’s in n r na b is greater than number of b’s. Therefore, xy2z L. Hence by contradiction we can say that the given language is not regular.

(ii) Step 1: Let us assume that L is regular and L is accepted by a FA with n states.

Step 2: Let us choose a string

| | = 2n + 2 n Let us write w as xyz with | y | > 0 and | xy | n Since | xy | n, x must be of the form as. Since | xy | n, y must be of the form ar | r > 0. Now,

3. (a)

as

x

ar

y

ansrbn

z

anb ban

R From R

=

= anbban = as

x

ar

y

ansrbban

z

Vidyala

nkar

Prelim Question Paper Solution

1113/Engg/TE/Pre Pap/2013/CMPN/Soln/TCS 5

Step 3: Let us check whether xyi z for i = 2 belongs to L.

xy2z = s 2r n s r n n r na a a bba a bba

Since r > 0, n r na bba is not of the form R as the strings starts with (n + r)a’s but ends in (n) a’s. Therefore, xy2z L. Hence by contradiction, we can say that the given language is not regular.

Step 1 : First, we can design an FA to recognize a string staring with 0 and ending in 11. Step 2 : From FA to TM Complements of Languages in NP It is not known whether NP is closed under complementation. The class of languages P is closed under complementation. It is believed that whenever language L is NPcomplete, its complement is not in NP. There is a set of languages whose complements are in NP. Such languages are called CoNP. 1. We believe that complement of NPcomplete problem is not in NP. 2. No NPcomplete problem is in CoNP 3. We believe that the complements of NPcomplete problems, which are in CoNP are not in

NP. NP Problems A problem is NPcomplete if it is in NP and for which no polynomial time deterministic TM solution is known so far. An interesting aspect of NPcomplete problem is that for each of these problems 1. It has not been possible to design a deterministic TM, so far. 2. It has not been possible to establish that a deterministic TM does not exist. There is a large number of problems, for which it is not known whether it is in P or not in P. However, for each of these problems, it is known that it is in NP. Each of these problems can be solved by at least one Nondeterministic TM, the time complexity of which is a polynomial function of the size of the problem. A problem from the class NP, can be defined as one for which a potential solution, if given, can be verified in polynomial time whether the potential solution is actually a solution or not.

3. (b)

q0 q1 q2 q30

00

11 1

0

q0 q1 q2 q3

..

.

q4

0|0, R

0|0, R0|0, R

1|1, R

B|B, R

Halt

0|0, R

1|1, R1|1, R

4. (a)

Vidyala

nkar

Vidyalankar : T.E. − TCS

1113/Engg/TE/Pre Pap/2013/CMPN/Soln/TCS 6

Some of the NPcomplete problems include 1. Satisfiability Problem (SAT) 2. Travelling Salesman Problem (TSP) 3. Hamiltonian Circuit Problem (HCP) 4. The Vertex Cover Problem (VCP) 5. KColourability Problem. 6. The Complete Subgraph Problem The Classes P and NP There are two groups in which a problem can be classified. The first group consists of the problems that can be solved in polynomial time. For example: searching of an element from the list O(logn), sorting of element O(logn). The second group of problems that can be solved in nondeterministic polynomial time. For example: Knapsack problem O(2n’2) and Travelling Salesperson problem (0(n22’’)). Intractable problems : Any problem for which answer is either yes or no is called decision problem. al The algorithm for decision problem is called decision algorithm. Any problem that involves the identification of optimal cost (minimum or maximum) is called optimization problem. The algorithm for optimization problem is called optimization algorithm. Definition of PProblems that can be solved in polynomial time. (“P” stands for polynomial). ExamplesSearching of key element, Sorting of elements, All pair shortest path. Definition of NPIt stands for “nondeterministic polynomial time”. Note that NP does not stand for “nonpolynomial”. Examples Travelling Salesperson problem, Graph coloring problem. Knapsack problem, Hamiltonian circuit problems. The NP class problems can be further categorized into NPcomplete and NP hard problems. A problem D is called NPcomplete if i) It belongs to class NP al ii) Every problem in NI’ can also be solved in polynomial time. If an NPhard problem can be solved in polynomial time then all NPcomplete problems can also be solved in polynomial time. All NPcomplete problems are NPhard but all NPhard problems can not be NPcomplete. The NP dass problems are the decision problems that can be solved by nondeterministic polynomial algorithms. Variants of a Turing Machine In a standard Turing machine, the tape is semiinfinite. It is bounded on the left and unbounded on the right side. Some of the extensions of turing machine are given below : 1. Tape is infinite length in both the directions. 2. Multiple heads and a single tape. 3. Multiple tapes with each tape having its own independent head. 4. Kdimension tape. 5. Nondeterministic turing machine. Two way Infinite Turing Machine : In a standard turing machine number of positions for leftmost blanks is fixed and they are included in instantaneous description, where the righthand blanks are not included. In the two way infinite Turing machine, there is an infinite sequence of blanks on each side of the input string. In an instantaneous description, these blanks are never shown. Turing Machine with Multiple Heads : A turing machine with single tape can have multiple heads. Let us consider a turing machine with two heads H1 and H2. Each head is capable of performing read/write / move operation independently.

4. (b)

4. (c)

Vidyala

nkar

Prelim Question Paper Solution

1113/Engg/TE/Pre Pap/2013/CMPN/Soln/TCS 7

The transition behavior of 2head one tape Turing machine can be defined as given below : (State, Symbol under H1, Symbol under H2) = (New state, (S1, M1), (S2, M2)) Where, S1 is the symbol to be written in the cell under H1. M1 is the movement (L, R, N) of H1.

S2 is the symbol to be written in the cell under H2. M2 is the movement (L, R, N) of H2. MultiTape Turing Machine : MultiTape turing machine has multiple tuples with each tape having its own independent head. Let us consider the case of a two tape turing machine. It is shown in Fig. The transition behavior of a twotape Turing machine can be defined as : (q1, a1, a2) = (q2, (S1, M1), (S2,M2)) Where, q1

is the current state, q2 is the next state, a1 is the symbol under the head on tape 1, a2 is the symbol under the head on tape 2, S1 is the symbol written in the current cell on tape 1, S2 is the symbol written in the current cell on tape 2, M1 is the movement (L, R, N) of head on tape 1, M2 is the movement (L, R, N) of head on tape 2. NonDeterministic Turing Machine : Nondeterministic is a powerful feature. A non-deterministic TM machine might have, on certain combinations of state and symbol under the head, more than one possible choice of behaviour. Nondeterministic does not make a TM more powerful. For every nondeterministic TM, there is an equivalent deterministic TM. It is easy to design a nondeterministic TM for certain class of problems. A string is said to be accepted by a NDTM, if there is at least one sequence of moves that takes the machine to final state. An example of nondeterministic move for a TM is shown in Fig. The transition behaviour for state q0 for TM of Fig. can be written as (q0, a) = {(q0, a, R) (q1, x, R)}

BabaabbaBBB

H1 H2

Fig. A Turing machine with two heads

B a b a a b b a B B B Tape 1:

B a a b b a b a B B Tape 2:

Fig. A twotape turing machine

q0 q1

a/a,R

a/x,R

Fig. A sample move for NDTM

Vidyala

nkar

Vidyalankar : T.E. − TCS

1113/Engg/TE/Pre Pap/2013/CMPN/Soln/TCS 8

The equivalent PDA, M is given by M = ({q}, {0, 1}, {0, 1, S, B}, , q, S, ), where is given by Acceptance of 0104 by M : Thus the string 0104 is accepted by M using an empty stack. 0104 L Simplifying Context-Free Grammars The productions of context-free grammars can be coerced into a variety of forms without affecting the expressive power of the grammar. Empty production removal If the empty string does not belong to a language, then there is a way to eliminate productions of the form A from the grammar. If the empty string does belong to a language, then we can eliminate from all productions save for the single production S . In this case we can also eliminate any occurrences of S from the right-hand-side of productions. Unit production removal We can eliminate productions of the form A B from a context-free grammar. Left Recursion Removal A variable A is left-recursive if it occurs in a production of the form

A Ax for any x (V T)*. A grammar is left-recursive if it contains at least one left-recursive variable. Every context-free language can be represented by a grammar that is not left-recursive.

5. (a)

For each production in the given grammar

For each terminal

(q, є, S) {(q, 0BB)} (q, є, B) {q, 0S), (q, 1S), (q, 0)}(q, 0, 0) {(q, є)} (q, 1, 1) {(q, є)}

(q, є, S) = (q, 0BB)

(q, 0, 0) = (q, є)

(q, є, B) = (q, 1S)

(q, 1, 1) = (q, є)

(q, 010000, 0BB)

(q, 10000, BB)

(q, 10000, 1SB)

(q, 0000, SB)

(q, 010000, S)

(q, є, S) = (q, 0BB)(q, 0000, 0BBB)

(q, 0, 0) = (q, є) (q, 000, BBB)

(q, є, B) = (q, 0) (q, 00, 0BB)

(q, 0, 0) = (q, є)

(q, є, B) = (q, 0) (q, 00, BB)

(q, 00, 0B) (q, 0, 0) = (q, є)

(q, 0, B) (q, є, B) = (q, 0)

(q, 0, 0) (q, 0, 0) = (q, є)

(q, є, є)

5. (b)

Vidyala

nkar

Prelim Question Paper Solution

1113/Engg/TE/Pre Pap/2013/CMPN/Soln/TCS 9

Some languages are regular. There are other languages which are not regular. One can neither express a nonregular language using regular expression nor design finite automata for it. Pumping lemma gives a necessary condition for an input string to belong to a regular set. Pumping lemma does not give sufficient condition for a language to be regular. Pumping lemma should not be used to establish that a given language is regular. Pumping lemma should be used to establish that a given language is not regular. The pumping lemma uses the pigeonhole principle which states that if n pigeons are placed into less than n holes, some holes have to have more than one pigeon in it. Similarly, a string of length n when recognized by a FA with n states will see some states repeating. Definition of pumping lemma : Let L be a regular language and M = (Q, , , q0, F) be a finite automata with nstates. Language L is accepted by m. Let L and | | n, then can be written as xyz, where (i) | y| > 0 (ii) | xy | n (iii) xyi z L for all i 0 here yi denotes that y is repeated or pumped i times.

L = n 2n{a b | n 0} is not a context free language Proof that L is not a CFL : It can be proved using a contradiction. Let us assume that L is a context free language. 1. Let n be the constant as per the pumping lemma. 2. Let us choose a word w = anb2n 3. W is rewritten as uvxyz.

Where | vxy | n and v . y є, both v and y are not null.

| uvxyz | = n 2n 2a b n

2 2 2uv xy z uvxyz vy n

And 2 2 2uv xy z n n[as vy n]

n2 + 2n + 1 (n +1)2

2 2uv ny z is a square for every i, uvixyiz L. But there is no square between n2 and (n+1)2. This

is a contradiction. Therefore, L is not a CFL. (i) closure of states

a b 1 2 1 2 2 3 3* 1 1 3

state closure comment 1 {1} There is no move from 1 2 {2} There is no move from 2 3 {1, 3} There is move from 3 to 1

(ii) Conversion of NFA to DFA Step 1: closure of the initial state 1 is taken as first subset closure (1) = {1} asuccessor of {1} = closure ( (1, a)) = closure (2) = {2} bsuccessor of {1} = closure ( (1, b)) = closure (1) = {1}

6. (a)

6. (b)

Vidyala

nkar

Vidyalankar : T.E. − TCS

1113/Engg/TE/Pre Pap/2013/CMPN/Soln/TCS 10

closure of final state 3 is closure (3) = {1, 3} asuccessor of (3) = closure ( (3, a)) = closure (1) = {1} bsuccessor of {3} = closure ( (3, b)) = closure (3) = {1, 3} closure of 2 is asuccessor of (2) = closure ( (2, a)) = closure (2) = {2} bsuccessor of {2} = closure ( (2, b)) = closure (3) = {1, 3} Step 2: The new subset {1, 3} is generated. Their successors are calculated. asuccessor of (1, 3) = closure ( (1, a) (3, a)) = closure ({2} {1}) = closure of ({2, 1}) = closure (2) closure (1) = {2} {1} = {2, 1} bsuccessor of (1, 3) = closure ( (1, b) (3, b)) = closure ({1} (3}) = {1} {1, 3} = {1, 3} asuccessor of (2, 1) = closure ( (2, a) (1, a)) = closure ({2} {2}) = {2} bsuccessor of (2, 1) = closure ( (2, b) (1, b)) = closure ({3} {1} = {1, 3} {1} = {1, 3}

No new subsets are generated. Each subset containing 3 (final state) in given (NFA) is marked as final state

Halting problem The halting problem of a Turing machine states : Given a Turing machine M and an input to the machine M, determine if the machine M will eventually halt when it is given input . Halting problem of a Turing machine is unsolvable. Proof : Moves of a turing machine can be represented using a binary number. Thus, a Turing machine

can be represented using a string over (0, 1). This concept has already been explained in the chapter. Insolvability of halting problem of a Turing machine can be proved through the method of contradiction.

7. (a) Vidyala

nkar

Prelim Question Paper Solution

1113/Engg/TE/Pre Pap/2013/CMPN/Soln/TCS 11

Step 1 : Let us assume that the halting problem of a Turing machine is solvable. There exists a machine H1(say). H1 takes two inputs : 1. a string describing M. 2. An input for machine M. H1 generates an output “halt” if H1 determines that M stops on input ; otherwise H outputs “loop”. Working of the machine H1 is shown below. Step 2 : Let us revise the machine H1 as H2 to take M as both inputs and H2 should be able to determine if M will halt on M as its input. Please note that a machine can be described as a string over 0 and 1. Step 3 : Let us construct a new Turing machine H3 that takes output of H2 as input and does the following : 1. If the output of H2 is “loop” than H3 halts. 2. If the output of H2 is “halt” than H3 will loop forever. H3 will do the opposite of the output of H2.

Step 4 : Let us give H3 itself as inputs to H3. If H3 halts on H3 as input then H3 would loop (that is how we constructed it). If H3 loops forever on H3 as input H3 halts (that is how we constructed it). In either case, the result is wrong. Hence, H3 does not exist. If H3 does not exist than H2 does not exist. If H2 does not exist than H1 does not exist.

Post Correspondence Problem

Definition of post correspondence problem (PCP) : Let A and B be two nonempty lists of strings over . A and B are given as below : A = {x1, x2, x3 …xk} B = {y1, y2, y3 …yk} We say, there is a post correspondence between A and B if there is a sequence of one or more integers i, j, k …m such that : The string xi xj …xm is equal to yi yj …ym.

Example : Does the PCP with two lists : A = {a, aba3, ab} and B = {a3, ab, b} have a solution ?

We will have to find a sequence using which when the elements of A and B are listed, will produce identical strings. The required sequence is (2, 1, 1, 3) A2 A1 A1 A3 = aba3 a aab = ab a6 b B2 B1 B1 B3 = aba3 a3 b = aba6 b Thus, the PCP has solution. We are accepting the undecidability of post correspondence problem without proof. Chomsky Hierarchy A grammar can be classified on the basis of production rules. Chomsky classified grammars into the following types : 1) Type 3 : Regular grammar 2) Type 2 : Context free grammar 3) Type 1 : Context sensitive grammar 4) Type 0 : Unrestricted grammar

H1

M

W halt

loop

H2

M halt

loop

H2

Mhalt

loop

Machine H3 loops forever

Machine H3 halts

H3H3

7. (b)

7. (c)

Vidyala

nkar

Vidyalankar : T.E. − TCS

1113/Engg/TE/Pre Pap/2013/CMPN/Soln/TCS 12

Type 3 or Regular Grammar : A grammar is called type 3 or regular grammar if all its productions are of the following forms : A є A a A aB A Ba Where, a and A, B V. A language generated by type 3 grammar is known as regular language.

Type 2 or Context Free Grammar : A grammar is called type 2 or context free grammar if all its productions are of the following form A where A V and (V T) *. V is a set of variables and T is a set of terminals. The language generated by a type 2 grammar is called a context free language, a regular language but not the reverse.

Type 1 or Context Sensitive Grammar : A grammar is called a type 1 or context sensitive grammar if all its productions are of the following form. , Where, is atleast as long as .

Type 0 or unrestricted grammar : Productions can be written without any restriction in a unrestricted grammar. If there is production of the , then length of could be more than length of . Every grammar also is a type 0 grammar. A type 2 grammar is also a type 1 grammar. A type 3 grammar is also a type 2 grammar. Greibach Theorem The theorem states that : “Let be a class of languages that is effectively closed under concatenation with regular sets and

union, and for which L = is undecidable for any sufficiently large fixed . Let P be any nontrivial property that is true for all regular sets and that is preserved under a, where a is single symbol in . Then P is undecidable for ”. Greibach theorem can be used to prove that many problems related to CFG are undecidable.

Greibach normal form In computer science and formal language theory, a contextfree grammar is in Greibach normal form if the righthand sides of all production start with a terminal symbol, optionally followed by some variables. A nonstrict form allows one exception to this format restriction for allowing the empty word (epsilon, є) to be a member of the described language. The normal form bears the name of Sheila Greibach. More precisely, a contextfree grammar is in Greibach normal form, if all production rules are of the form : A A1, A2, …, An or S є where A is a nonterminal symbol, is a terminal symbol, A1, A2, …, An is a (possibly empty) sequence of nonterminal symbols not including the start symbol, S is the start symbol, and є is the empty word. Observe that the grammar must be without left recursions. Every contextfree grammar can be transformed into an equivalent grammar in Greibach normal form. (Some definitions do not consider the second form of rule to be permitted, in which case a contextfree grammar that can generate the empty word cannot be so transformed.) In particular, there is a construction ensuring that the resulting normal form grammar is of size at most O(n4), where n is the size of the original grammar. This conversion can be used to prove that every contextfree language can be accepted by a nondeterministic pushdown automaton. Given a grammar in GNF and a derivable string in the grammar with length n, any topdown parser will halt at depth n.

7. (d)

Vidyala

nkar