BC0052 - Theory of Computer Science Set-2

Click here to load reader

Transcript of BC0052 - Theory of Computer Science Set-2

Vinod H Bachelor of Computer Application Semester - 5 BC0052 Theory of Computer Science Fall 2012 Assignment Set 2 Roll No: 521110833 Center Code: 03011 2 BC0052 Theory of Computer Science Set -2 1. Prove that A tree G with n vertices has (n-1) edges Problem: If T is a tree (with two or more vertices), then there exists at least two pendant (a vertex of degree 1) vertices. Solution: Let n = the number of vertices in G. Then G has n-1 edges. Now . Now if there is only one vertex, say v1 of degree 1, then deg(vi) > 2 for i = 2, 3, , n and But 2n-2 > 2n-1 or -2 > -1, a contradiction. Therefore there are at least two vertices of degree 1. Note: If two nonadjacent vertices of a tree T are connected by adding an edge, then the resulting graph will contain a cycle. 2. If G =({S}, {0,1}, (S 0S1, S }, S) then find L(G), the language generated by G Solution: Since S . is a production, S .. This implies that . e L(G). Now, for all n > 1, we can write the following: S 0S1 00S11 0nS1n 0n1n. Therefore, 0n1n e L(G). In the above derivation, at every step, S 0S1 is applied, except in the last step where S . is applied. Therefore, {0n1n n > 0} _ L(G). Now suppose w e L(G). So we should start the derivation of w with S. If we are applying S . first, then we will get w = .. Otherwise, the first production that we need to apply is S 0S1. However, at any stage we can apply S . to obtain the terminating string. Therefore w can be derived in the following form. S 0nS1n 0n1n, for some n > 1, That is L(G) _ {0n1n n > 0}. 3 BC0052 Theory of Computer Science Set -2 Hence L(G) = {0n1n n > 0}. 3. Obtain a DFA to accept strings of as and bs starting with the string ab. It is clear that the string should start with ab and so, the minimum string that can be accepted by the machine is ab To accept the string ab, we need three states and the machine can be written as Where q2 is the final or accepting state. In state q0, if the input symbol is b, the machine should reject b (note that the string should start with a). So, in state q0, on input b, we enter into the rejecting state q3. The machine for this can be of the form The machine will be in state q1, if the first input symbol is a. If this a is followed by another a, the string aa should be rejected by the machine. So, in state q1, if the input symbol is a, we reject it and enter into q3 which is the rejecting state. The machine for this can be of the form Whenever the string is not starting with ab, the machine will be in state q3 which is the rejecting state. So, in state q3, if the input string consists of as and bs of any length, the entire string can be rejected and can stay in state q3 only. The resulting machine can be of the form 4 BC0052 Theory of Computer Science Set -2 The machine will be in state q2, if the input string starts with ab. After the string ab, the string containing any combination of as and bs, can be accepted and so remain in state q2 only. The complete machine to accept the strings of as and bs starting with the string ab is shown in figure. The state q3 is called trap state or rejecting state. In the set notation, the language accepted by DFA can be represented as L = {ab(a + b)n n 0} Or L = {ab(a + b)*} Therefore the DFA which accepts strings of as and bs starting with the string ab is given by M = (Q, S, d, q0, F), where Q = {q0, q1, q2, q3}, S = {a, b}, q0: initial state, F = {q2}, and the transition function d is defined as 5 BC0052 Theory of Computer Science Set -2 To accept the string abab: The string is accepted by the machine. (q0, abab) = d ( (q0, aba), b) = d (d ( (q0, ab), a), b) = d (d (d (d (q0, a), b), a), b) = d (d (d (q1, b), a), b) = d (d (q2, a), b) = d (q2, b) = q2 F. To reject the string aabb: (q0, aabb) = d ( (q0, aab), b) = d (d ( (q0, aa), b), b) = d (d (d (d (q0, a), a, b), b) = d (d (d (q1, a), b), b) = d (d (q3, b), b) = d (q3, b) = q3 F. Therefore the string aabb is not accepted by the machine. 4. Briefly describe Moore and Mealy machines The automation systems we have discussed so far are limited to binary output. That is, the systems can either accept or do not accept a string. In those systems, this acceptability is decided based on the reachability from the initial state to the final state. This property of the system produces restrictions in 6 BC0052 Theory of Computer Science Set -2 choosing outputs from some other alphabet, then output. You can remove this constraint using both the Moore and Mealy machine, which help in generating an output from a certain output alphabet. Let us denote the output function with the symbol. Thus, when the output of an automation system is dependent only on the present state, then, Output = (q(t)), where q(t) is the present state. The above automaton system is called a Moore machine. Alternatively, when the output of the automaton system is dependent on both the present input and the present state, then, Output = (q(t), x(t)), where q(t) is the present state and x(t) is the present input. The above automaton system is called a Mealy machine. Since the output of a Mealy machine is dependent on both the input and the present state, no output is generated when the input is a null string. This implies that when the input is ., the output is also .. However, in case of the Moore machine, there is some output of the Moore machine which is only dependent on the present state and not on the present input. Hence, when the input to a Moore machine is ., the output is (q0). Definition: A Moore machine can be with a 6-tuple (Q, E, A, o, , q0) where: Q is non-empty, finite set of states. E is non-empty, finite set of input alphabets. A is the output alphabet o is transition function, which is a mapping from E Q into Q. is the output function mapping Q into A q0 e Q is the start state. For the state table of Moore machine as shown in the table, it is clear that the output is common for both the inputs, which means the output is only dependent on the present state and not on the present input. Definition: A Moore machine can be with a 6-tuple (Q, E, A, o, , q0) where: 7 BC0052 Theory of Computer Science Set -2 Q is non-empty, finite set of states. E is non-empty, finite set of input alphabets. A is the output alphabet o is transition function, which is a mapping from E Q into Q. is the output function mapping E Q into A q0 e Q is the start state. From the state table of Mealy machine as shown in the table, it is clear that the output is dependent on both the present state and present input in a Mealy machine. Conversion of Mealy machine into Moore Machine: Consider the following steps Step 1: For a state qi determine the number of different outputs that are available in u 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. Example: Consider the Mealy machine: 8 BC0052 Theory of Computer Science Set -2 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. Now, rearranging the state table as shown in table, into the Moore machine format, we obtain the state table as shown below. 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. 9 BC0052 Theory of Computer Science Set -2 5. Draw the state diagram for the finite automation where E = {a, b, F = {q0, q1}, defined by Verify whether or not the strings aaab and bbaa are acceptable to M. Solution: i) Consider the string: aaab Now = = = . Therefore the string aaab is not accepted by M. ii) Consider the string: bbaa Now = = = Therefore bbaa is not accepted by M. iii) State diagram: 10 BC0052 Theory of Computer Science Set -2 Example: Construct deterministic finite state automata that recognize each of this language. a) The set of bit strings that begin with two 0s. b) The set of bit strings that contain two consecutive 0s. c) The set of bit strings that do not contain two consecutive 0s. d) The set of bit strings that end with two 0s. Solution: (a) our aim is to construct a deterministic finite state automaton that recognizes the set of bit strings with two 0s. Besides that start state s0, we include a nonfinal state s1; we move to s1 from s0 if the first bit is a 0. Next, we add a final state s2, which we move to from s1 if the second bit is a 0. When we have reached s1 we know that the first two input bits are both 0s, so we stay in the state s2 (no matter what the succeeding bits, if any are). We move to a nonfinal state s3 from s0 if the first bit is a 1 and from s1 if the second bit is a 1. We can easily verify that the finite state automaton recognizes the set of bit strings that begin with two 0s. To construct a deterministic finite-state automaton that recognizes that contain two consecutive 0s. Besides the start state s0, we include a nonfinal state s1, which tells us that the last input bit seen is a 0, but either the bit before it was a 1, or this bit was the initial bit of the string. We include a final state s2 11 BC0052 Theory of Computer Science Set -2 that we move to from s1 when the next input bit after a 0 is also a 0. If a 1 follows a 0 in the string (before we encounter two consecutive 0s), we return to s0 and begin looking for consecutive 0s all over again. In this, our goal is to construct a deterministic finite-state automaton that recognizes the set of bit strings that end with two 0s. Besides the start state s0, we include a nonfinal state s1, which we move to the first bit is 0. We include a final state s2, which we move to from s1 if the next input bit after a 0 is also 0. If an input of 0 follows a previous 0, we stay in state s2 because the last two input bits are still 0s. Once we are in state s2, an input bit of 1 sends us back to s0 and we begin looking for consecutive 0s all over again. 6. Obtain a regular expression such that L(R) = {w w e {0, 1}* with at least three consecutive 0s. Solution: A string consisting of 0s and ls can be represented by the regular expression (0 + 1)* This arbitrary string can precede three consecutive zeros and can follow three consecutive zeros. Therefore the regular expression can be written as (0 +1)* 000(0+1)*. The language corresponding to the regular expression can be written as L(R) = { (0 + 1)m000(0+1)n m > 0 and n > 0}.