Top Banner
Summer 2012 Master of Computer Application (MCA) – Semester 5 MC0082 –Theory of Computer Science – 4 Credits Assignment Set – 1
29

MC0082

Nov 03, 2014

Download

Documents

rahul_151190

MC0082 Semester 5 Assignment SMU
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: MC0082

Summer 2012

Master of Computer Application (MCA) – Semester 5

MC0082 –Theory of Computer Science – 4 Credits

Assignment Set – 1

Page 2: MC0082

Q1) Define the concept of equivalence relation. Give at least two examples of equivalencerelation.

Ans 1) Equivalence Relation

Definition A relation is said to be an equivalence relation if it is reflexive, symmetric and transitive.

If (a, b) is an element of the equivalence relation, then we write a ~ b and we say that a and b are equivalent.

Let R be an equivalence relation on S and a is an element of S. Then the set [a] = {s S / s ~ a}

is called the equivalence class of a (or equivalence class containing a).

Example 1:

If A = {a, b, c} and R = {(a, a), (b, b), (c, c), (a, b), (b, a)}, then R is an equivalence relation on A

and [a] = {a, b}, [b] = {a, b}, and [c] = {c}.

Example 2:

i) Let S be the set of all integers. Define for any a, b S, a ~ b a - b is an even number. Then relation ~ is an equivalence relation.

ii) Let R, the set of all real numbers, x ~ y x – y is an integer. Then ~ is an equivalence relation. The set of all equivalence classes are given by {[x] / x (0, 1]}.

Page 3: MC0082

Q2) Prove by Mathematical Induction that

13+23+33+...+n3=n2 ¿¿

Ans 2)

i) For n=1, left side = 13 = 1

right side = 12 ¿¿ = 1.14

= 1

Hence it is true for n = 1

ii) Assume the result to be true for n = m

Then 13+23+33+...+m3=m2 ¿¿ (induction hypothesis)

Adding the (m+1)th term viz.,(m+3)3 to both sides,

13+23+33+...+m3=m2 ¿¿ + (m+1)3

= ¿¿ + (m2+4m+4)

= ¿¿

= ¿¿

Therefore the result is true for n = m+1.

Hence by mathematical induction the given result is established for all positive integers.

Q3) Prove that a graph G is connected if and only if it has a spanning tree.

Ans 3)

Proof: Let G be a connected graph.

Page 4: MC0082

If G has no circuit, then G is a spanning tree.

If G has a circuit, then delete an edge from this circuit. If till leaves the graph connected.

If there are more circuits, repeat the operation till an edge from the last circuit is deleted, leaving the graph connected, circuitless, and contains all the vertices of G.

Thus the subgraph obtained is a spanning tree of G.

Hence every connected graph has at least one spanning tree.

Q4) Prove that a b(modm) is an equivalence relation.

Ans 4)

To proof: The relation “a b mod m” is an equivalence relation on Z.

Page 5: MC0082

Solution: Let a Z.

Reflexive: Since n divides a - a = 0, we have a a mod m.

Symmetric: Let a b mod m. m divides a - b m divides - (a - b) m divides b - a b a mod m.

Transitivity: Let a, b, c Z such that a b mod m, b c mod m. m divides a - b, and m divides b - c m divides ( a - b ) + ( b - c) m divides a - c a c mod m.

Hence the relation is an equivalence relation.

Q5) Explain the concept of a Transition graph.

Ans 5)

A finite directed labeled graph in which each node or vertex of the graph represents a state and the directed edges from one node to another represent transition of a state. All the edges of the

Page 6: MC0082

transition graph are labeled as input/output. For example, an edge labeled 1/0 specifies that for a certain initial state if the input is 1, then the output is 0.

Consider the following diagram:

In the transition graph as shown in the figure,

The initial state, q0, of the system is represented by a circle with an arrow pointing towards it.

The final state, q1, is represented by two concentric circles.

The directed edges from the initial state to the final state are labeled as input/output.

Example

The graph represents the DFA,

M = (Q = {q0, q1, q2}, = {0, 1}, , q0 = initial state, F = {q1}), where is given by

(q0, 0) = q0, (q0, 1) = q1, (q1, 0) = q0, (q1, 1) = q2, (q0, 0) = q2,

(q2, 1) = q1.

Representation of DFA using Transition table:

In this method, the DFA is represented in the tabular form. This table is called transitional table. There is one row for each state, and one column for each input. Since, in the transition diagram shown in the fig., there are three states, there are three rows for each state. The input symbols are only 0 and 1 so, there are two columns for the input symbols. The transitional table for the diagram is given below.

Page 7: MC0082

States 0 1 q0

q1

q0 q1

q1 q2

q2 q1

Q6) Explain the steps of conversion of Mealy machine into Moore machine

Ans 6) Conversion of Mealy machine into Moore Machine

Consider the following steps:

q2

Page 8: MC0082

Step 1: For a state qi determine the number of different outputs that are available in θ state table of the Mealy machine.

Step 2: If the outputs corresponding to state qi in the next state columns are same, then retain state qi as it is. Else, break qi into different states with the number of new states being equal to the number of different outputs of qi.

Step 3: Rearrange the states and outputs in the format of a Moore machine. The common output of the new state table can be determined by examining the outputs under the next state columns of the original Mealy machine.

Step 4: If the output in the constructed state table corresponding to the initial state is 1, then this specifies the acceptance of the null string by Mealy machine. Hence, to make both the Mealy and Moore machines equivalent, we either need to ignore the output corresponding to the null string or we need to insert a new initial state at the beginning whose output is 0; the other row elements in this case would remain the same.

Consider the following example, to convert a given mealy machine into a Moore machine.

Present StateNext State

Input a = 0 Input a = 1State Output State Output

q1 q1 1 q2 0q2 q4 1 q4 1q3 q2 1 q3 1q4 q3 0 q1 1

After assessing the state table given above, we observe that the outputs for the states q1 and q4 are same while the outputs for the states q2 and q3, have two different values. Hence, after splitting states q2 and q3, we obtain the state table shown below.

Present StateNext State

Input a = 0 Input a = 1State Output State Output

q1 q1 1 q20 0q20 q4 1 q4 1q21 q4 1 q4 1q30 q21 1 q31 1q31 q21 1 q31 1q4 q30 0 q1 1

Now, rearranging the state table as shown in table, into the Moore machine format, we obtain the state table as shown below.

Page 9: MC0082

Moore Machine

Present StateNext State

Input a = 0 Input a = 1 OutputState State

q1 q1 q20 1q20 q4 q4 0q21 q4 q4 1q30 q21 q31 0q31 q21 q31 1q4 q30 q1 1

Now, from the state table as shown above, we observe that the output corresponding to the initial state q1 is 1. Hence, to ensure that the Mealy and Moore machines are equivalent, we need to insert a row at the beginning of the state table with present initial state q0.

Moore Machine

Present StateNext State

Input a = 0 Input a = 1 OutputState State

q0 q1 q20 1q1 q1 q20 1q20 q4 q4 0q21 q4 q4 1q30 q21 q31 0q31 q21 q31 1q4 q30 q1 1

Page 10: MC0082

Summer 2012

Master of Computer Application (MCA) – Semester 5

MC0082 –Theory of Computer Science – 4 Credits

Assignment Set – 2

Page 11: MC0082

Q1) Construct a grammar for the language L= {aibj i, j 1, i j }.

Ans 1)

It is clear from the given language that ‘i' number of a’s are followed by ‘j’ number of b’s and

number of a’s and b’s are not equal.

As a first attempt, we can have the production

S aSb

using which j number of a’s are followed by one S followed by j number of b’s are generated.

Now, if we replace the non-terminal S by the production

S

we get j number of a’s followed by j number of b’s.

But, we should see that number of a’s and b’s are different. So, we should be in a position to

generate either one or more extra a’s or one or more extra b’s. Hence, instead of the production

S

we can have productions

S A B

From the non-terminal A, we can generate one or more a’s and from non-terminal B, we can

generate one or more b’s as shown below:

A aA a

B bB b

So for the language L= {aibj i, j 1, i j }

The context free grammar is as follows:

Page 12: MC0082

G = (VN, VT ,Φ, S) where

VN = {S,A,B} , VT = {a,b},

Φ:S aSb A B

A aA a

B bB b

S: starting symbol,

Q2) Write short notes on Deterministic Finite Automata.

Ans 2) Deterministic Finite Automata

Page 13: MC0082

The first type of automata, we study in detail the finite accepters that are deterministic in their

operation. We start with a precise formal definition of deterministic accepters.

Definition

A DFA is 5-tuple or quintuple M = (Q, , , q0, F) where

Q is non-empty, finite set of states.

is non-empty, finite set of input alphabet.

is transition function, which is a mapping from Q x to Q. For this transition function the parameters to be passed are state and input symbol. Based on the current state and input symbol, the machine may enter into another state.

q0 Q is the start state.

F Q is set of accepting or final states.

Note

For each input symbol a, from a given state there is exactly one transition (there can be no

transitions from a state also) and we are sure (or can determine) to which state the machine

enters. So, the machine is called Deterministic machine. Since it has finite number of states the

machine is called Deterministic finite machine (automaton).

Illustration: Let us take the pictorial representation of DFA shown in figure and understand the

various components of DFA.

0 0 0,1

Start state 1 1 Final state

It is clear from this diagram that, the DFA is represented using circles, arrows and arcs labeled with some digits, concentric circles etc. The circles are nothing but the states of DFA. In the DFA shown in figure, there are three states viz., q0, q1 and q2. An arrow enters into state q0 and is not originating from any state and so it is quite different from other states and is called the start state or initial state. The state q2 has two concentric circles and also a special state called the final

q0 q1 q2

Page 14: MC0082

state or accepting state. In this DFA, there is, only one final state. Based on the language accepted by DFA, there can be more than one final state also.

The states other than start state and final states are called intermediate states. Always the machine initially will be in the start state. It is clear from the figure that, the machine in state q0, after accepting the symbol 0, stays in state q0 and after accepting the symbol 1, the machine enters into state q1. Whenever the machine enters from one state to another state, we say that there is a transition from one state to another state. Here we can say that there is a transition from state q0 to q1 on input symbol 1.

In state q1, on input symbol 0, the machine will stay in q1 and on symbol 1, there is a transition to state q2. In state q2, on input symbol 0 or 1, the machine stays in state q2 only. This DFA has three states q0, q1 and q2 and can be represented as

Q = {q0, q1, q2}, the possible input symbols set = {0, 1}, which is set of input symbols

(alphabet) for the machine.

There will be a transition from one state to another based on the input alphabet. If there is a transition from vi, to vj on an input symbol a, it can be represented as (vi, a) = vj.

The transitions from each state of the machine shown in figure based on the input alphabet {0, 1}

are shown in table.

State Input Output Transition Representation

q0 0 q0 (q0 , 0) = q0

q0 1 q1 (q0 , 1) = q1

q1 0 q1 (q1 , 0) = q1

q1 1 q2 (q1 , 1) = q2

q2 0 q2 (q2 , 0) = q2

q2 1 q2 ( q2 , 1) = q2

Q3) Describe the technique of conversion from NDFA to DFA.

Page 15: MC0082

Ans 3)

Let MN = (QN, N, N, q0, FN) be an NFA and accepts the language L(MN). There should be an

equivalent DFA, MD = (QD, D, D, q0, FD) such that L(MD) = L(MN). The procedure to convert

an NFA to its equivalent DFA is shown below.

Step 1: The start state of MN is the start state of MD. So, add q0 (which is the start state of MN) to

Q and find the transitions from this state. The way to obtain different transitions is shown in the

next step.

Step 2: For each state {qi, qj, …, qk} QD, the transitions for each input symbol in can be obtained as shown below.

1. D({qi, qj, …, qk}, a) = N(qi, a) N(qj, a) … N(qk, a) = {ql, qm, …, qn}(say)

2. Add the state {ql, qm, …, qn} to Q, if it is not already in QD.

3. Add the transition from {qi, qj, …, qk} to {ql, qm, …, qn} on the input symbol a if and only if the state {ql, qm, …, qn} is not added to QD in the previous step.

Step 3: The state {qa, qb, …, qc} Q is the final state, if at least one of the states in qa, qb, …, qc

FN (that is, at least one component in {qa, qb, …, qc} should be the final state of NFA.

Q4) Prove by Mathematical Induction that

Page 16: MC0082

12+22+32+...+n2=n (n+1 )(2 n+1)

6

Ans 4)

i) Base Step: Let n = 0. Then the sum on the left is zero, since there is nothing to add. The expression on the right is also zero.

If n = 1, left side = 12 = 1

Right side = 1 (1+1 )(2∗1+1)

6 =

1∗2∗36

= 1

Hence, the result is true for n = 1.

ii) Induction Hypothesis: Assume that the result to be true for n = m

Then,

12+22+32+...+m2=m (m+1 )(2 m+1)

6

Adding the (m+1)th term i.e.(m+1)2 to both sides of the above equation, we get,

12+22+...+m2+¿ + (m+1)2

= (m+1)

6 {m (2m+1) + 6 (m+1)}

= (m+1)

6 (2m2 + 7m +6)

= (m+1 ) (m+2 )(2 m+3)

6

= (m+1 ) (m+1+1 )(2(m+1)+1)

6

Therefore, the result is true for n = m+1.

Hence by mathematical induction, the given result is true for all positive integers n.

Q5) Find a regular expression to accept a language consisting of strings of a’s and b’s of odd

Page 17: MC0082

length.

Ans 5) String of a’s and b’s of odd length can be obtained by the combination of the strings aa, ab, ba and bb followed by either a or b.

Therefore, the regular expression can be of the form

(aa + ab + ba + bb)* (a + b)

String of a’s and b’s of odd length can also be obtained by the combination of the strings aa, ab, ba and bb preceded by either a or b.

Therefore, the regular expression can also be represented as

(a + b) (aa + ab + ba + bb)*

Observation: Even though these two expressions seem to be different, the language corresponding to those two expressions is the same.

Q6) State and prove Pumping Lemma.

Page 18: MC0082

Ans 6) Pumping Lemma

We are facing problem whenever we come across non regular languages. The easiest way to prove that a language is regular is to construct a DFA. Then, what is the easiest way to prove that a language is not regular?

We discuss the way to prove that certain languages are infinite using Pumping Lemma. The principle used in Pumping Lemma is similar to the Pigeonhole principle.

Pigeonhole principle: The pigeonhole principle is based on a simple observation. Suppose there are n objects and m boxes where number of objects n, are greater than number of boxes m. In this case, if all n objects are placed into m boxes, then at least one box will have more than one object.

The pumping lemma is based on this principle.

The pumping lemma is stated as follows:

Lemma

Let M = (Q, , , q0, F) be an FA and has n number of states. Let L be the language accepted by M and assume the language is regular. Let x L and x n, that is, length of the string x is greater than the number of states of FA. If the string x can be decomposed into combinations of strings

x = uvw

such that uv ≤ n, v 1, then uvi w L for i = 0, 1, 2, ….

Proof: Let M = (Q, , , q0, F) be an FA and Let x = a1a2a3 … am is the input which is accepted by the machine. Assume the machine has n states and assume that m n.

Observe that, if exactly one symbol is accepted by an FA, then there are two distinct states in it. Similarly, to accept exactly two symbols, FA should have three distinct states. In general, to accept a string x where x = n, then the FA should have n + 1 states.

Let those states be q0, q1, …, qm.

Since we have m input symbols, naturally we should have m + 1 states in the sequence q0, q1, …, qm, where q0 will be the start state and qm will be the final state as shown below:

Page 19: MC0082

It is clear from the figure that (qi-1, ai) = qi for each 1 ≤ i ≤ m. In this chain of states from q0 to qm, suppose a state q appears more than once, say q = qi and q = qj, where i < j.

In this case, the chain should have a loop and we can split into three groups:

1. The first group is the string prefix from a1a2a3 … ai

2. The second group is the loop string from ai+1ai+2 … aj-1aj

3. The third group is the string suffix from aj+1aj+2 … am, shown below.

Note that the machine cannot remember the previous state or it does not know how it has reached the current state.

So, whenever we say that the machine has reached the state q, the machine does not know whether the state is reached after the end of the prefix or after it has processed the loop. Since the machine cannot distinguish these states, after the prefix (u), the machine may be in a loop for zero or more time (accepting the string v zero or more times) and then accept the suffix string (w). In general, if the string x is split into sub strings uvw, then for all i 0,

uvi w L.

If F = {qm}, a1a2a3 … am L(M), then

a1a2a3 … ai-1ai ai+1ai+2 … aj-2aj-1aj aj+1aj+2 … am L(M).

This can be expressed using the transition as shown below.

(q0, a1a2a3 … ai-1ai ai+1ai+2 … aj-2aj-1aj aj+1aj+2 … am) = ( (q0, a1a2a3 … ai-1ai ), ai+1ai+2 … aj-2aj-1aj aj+1aj+2 … am) = (q, aj+1aj+2 … am) = (qk, ak+1ak+2 … am) = qm.

Page 20: MC0082

Also, after the string a1a2a3 … ai-1ai

the machine will be in state qi. Since qi and qj are same, we can input the string

ai+1ai+2 … aj-2aj-1aj

any number of times and the machine will stay in qj only. Finally, if the input string is

aj+1aj+2 … am

the machine enters into final state qm.

Pumping Lemma (for Context Free Languages)

Statement: Let L be the context free language and is infinite. Let z be sufficiently long string and z L so that z n where n is some positive integer. If the string z can be decomposed into combinations of strings

z = uvwxy

such that vwx ≤ n, vx 1, then uvi wiy L for i = 0, 1, 2, ….

Observations:

n is the length of the longest string that can be generated by the parse tree where the same non terminal never occurs twice on the same path through the tree.

The string z is sufficiently long so that it can be decomposed into various sub strings u, v, w, x and y in that sequence.

The two sub strings v and x are present somewhere in z.

The sub string u appears before v, the sub string w is in between v and x and the sub string y appears after x.

The string w in between v and x cannot be too long since vwx ≤ n for some positive integer n.

Both the sub strings v and x cannot be empty since vx 1. (One of them can be empty).

If all the points mentioned (first five points) are satisfied and if we duplicate sub string v and x same number of times, the resulting string will definitely be in L and the string z L is context free. Otherwise, the string z L is not context free.

Proof of Pumping Lemma:

Page 21: MC0082

By Pumping Lemma, it is assumed that string z L is finite and is context free language. We know that z is string of terminal which is derived by applying series of productions.

Case 1: To generate a sufficiently long string z (it is assumed that the string is infinite), one or more variables (non-terminals) must be recursive. Let us assume that the language is finite, the grammar has a finite number of variables (assume that all are ‘useful variables) and each has finite length. The only way to derive sufficiently long string using such productions is that the grammar should have one or more recursive variables. Assume that no variable is recursive.

Since no non-terminal (variable) is recursive, each variable must be defined only ‘in terms of terminal(s) and / or other variables’. Since those variables are also non-recursive, they have to be defined in terms of terminals and other variables and so on. If we keep applying the productions, there are no variables at all in the final derivation and finally we get string of terminals and the generated string is finite.

From this we conclude that there is a limit on the length of the string that is generated from the start symbol S. This contradicts our assumption that the language is finite.

Therefore, the assumption that one or more variables are non recursive is incorrect.

This means that one or more variables are recursive and hence the proof.

Proof of Case 2:

The string z L implies that after applying some / all productions some number of times, we get finally string of terminals and the derivation stops.

Let z L is sufficiently long string and so the derivation must have involved recursive use of some non-terminal A and the derivations must have the form

*S uAy

Note that any derivation should start from the start symbol S. Since A is used recursively, the derivation can take the following form:

* *S uAy uvAxy

and the final derivation should be of the form

* * *

S uAy uvAxy uvwxy = z

It implies that the following derivations * *

A vAx and A w

Page 22: MC0082

are also possible. From this, we can easily conclude that the derivation *A vwx

must also be possible.

Next, we have to prove that the longest string vwx is generated without recursion since it is assumed that vwx ≤ n. This can be easily proved since CFG that generates CFL does not contain

-productions or unit productions.

It shows that every derivation step either increases the length of the sentential form (using recursive variable) or introduces a terminal. The derivation

* A vAx

used earlier clearly shows that

vx 1

Observe from the derivation

* * * S uAy uvAxy

that uvAxy occurs in the derivation, and

* * A vAx and A w

are also possible. It follows that, uvi wxi y L

Hence the proof.