theoryofcomp science

15
qwertyuiopasdfghjklzxcvbnmq wertyuiopasdfghjklzxcvbnmqw ertyuiopasdfghjklzxcvbnmqwe rtyuiopasdfghjklzxcvbnmqwer tyuiopasdfghjklzxcvbnmqwert yuiopasdfghjklzxcvbnmqwerty uiopasdfghjklzxcvbnmqwertyu iopasdfghjklzxcvbnmqwertyui opasdfghjklzxcvbnmqwertyuio pasdfghjklzxcvbnmqwertyuiop asdfghjklzxcvbnmqwertyuiopa sdfghjklzxcvbnmqwertyuiopas dfghjklzxcvbnmqwertyuiopasd fghjklzxcvbnmqwertyuiopasdf

Upload: raghu-nath

Post on 20-May-2015

99 views

Category:

Technology


1 download

DESCRIPTION

Theory of Computer Scince

TRANSCRIPT

Page 1: Theoryofcomp science

qwertyuiopasdfghjklzxcvbnmqwertyuiopasdfghjklzxcvbnmqwertyuiopasdfghjklzxcvbnmqwertyuiopasdfghjklzxcvbnmqwertyuiopasdfghjklzxcvbnmqwertyuiopasdfghjklzxcvbnmqwertyuiopasdfghjklzxcvbnmqwertyuiopasdfghjklzxcvbnmqwertyuiopasdfghjklzxcvbnmqwertyuiopasdfghjklzxcvbnmqwertyuiopasdfghjklzxcvbnmqwertyuiopasdfghjklzxcvbnmqwertyuiopasdfghjklzxcvbnmqwertyuiopasdfghjklzxcvbnmqwertyuiopasdfghjklzxcvbnmqwertyuiopasdfghjklzxcvbnmqwertyuiopasdfghjklzxcvbnmqwertyuiopasdfghjklzxcvbnmrtyuiopasdfghjklzxcvbnmqwertyuiopasdfghjklzxcvbnmqwertyuiopasdfghjklzxcvbnmqwertyuiopasdfghjklzxcvbnmqw

Page 2: Theoryofcomp science

Theory of Computer Science

1.Differentiate between Recursive Functions and growth functions

Recursion is the technique of defining a function, a set or an algorithm in terms of itself. That is, the definition will be in terms of previous values.

Definition A function f: N → N, where N is the set of non-negative integers is defined recursively if the value of f at 0 is given and for each positive integer n, the value of f at n is defined in terms of the values of f at k, where 0 ≤ k < n.

Observation: f defined (above) may not be a function. Hence, when a function is defined recursively it is necessary to verify that the function is well defined.

Example The sequence 1, 4, 16, 64, ... , can be defined explicitly by the formula f(n) = 4n for all integers n ≥ 0.

The same function can also be defined recursively as follows:

f(0) = 1, f(n + 1) = 4f(n), for n > 0

To prove that the function is well defined we have to prove existence and uniqueness of such function. In this case, existence is clear as f(n) = 4n.

Theorem: (Recursion Theorem): Let F is a given function from a set S into S. Let s0 be fixed element of S. Then there exists a unique function f: N → N where N is the set of non-negative integers satisfying

i) f(0) = s0

ii) f(n + 1) = F(f(n)) for all integers n € N.

(Here the condition (i) is called initial condition and (ii) is called the recurrence relation).

Example

Define n! recursively and compute 5! recursively. Solution: We have f: N → N. Then

i) f(0) = 1

ii) f(n + 1) = (n + 1)f(n) for all n 0.

Clearly f(n) = n!. Now we compute 5! recursively as follows: 5! = 5. 4! = 5. 4. 3!

M.C.A vth semester Page 2

Page 3: Theoryofcomp science

Theory of Computer Science

= 5. 4. 3. 2! = 5. 4. 3. 2. 1! = 5. 4. 3. 2. 1. 0! = 5. 4. 3. 2. 1. 1 = 120. Note Any sequence in arithmetic progression or geometric progression can be defined recursively. Consider the sequence a, a + d, a + 2d, …. Then

A(0) = a, A(n + 1) = A(n) + d.

Consider another sequence a, ar, ar2, … . Then

G(0) = a, G(n +1) = r G(n).

Definition The Fibonacci sequence can be defined recursively as

i) F0 = 1 = F1

ii) Fn+1 = Fn + Fn-1 for n > 1.

Then F2 = F1 + F0 = 2 F3 = F2 + F1 = 3 F4 = F3 + F2 = 5 ….. Here, there are two initial conditions.

Example: Define F(x) = {x/2 when x is even { x-1/2 when x is odd

Solution: Define f: N → N such that f(0) = 0 and f(x + 1) = x – f(x).

Then f(6) = 5 – f(5) = 5 – [4 – f(4)] = 5 – 4 + [3 – (3)] = 5 – 4 + 3 – 2 + [1 – f(1)] = 5 – 4 + 3 – 2 +1 – [0 – f(0)] = 3.

and f(5) = 4 – f(4) = 4 – [3 - f(3)] = 4 – 3 + 2 - [1-f(1)] = f – 3 + 2 -1 + [0 – f(0)] = 2.

Growth Functions The growth of a function is often described using a special notation, O-notation (read as “big-oh notation”). It provides a special way to compare relative sizes of functions that is very useful in

M.C.A vth semester Page 3

Page 4: Theoryofcomp science

Theory of Computer Science

the analysis of computer algorithms. It often happens that the time or memory space requirements for the algorithms available to do a certain job differ from each other on such a grand scale that differences of just a constant factor are completely overshadowed. The O-notation makes use of approximations that highlight these large-scale differences while ignoring differences of a constant factor and differences that only occur for small sets of input data.

Definition Let f and g be functions from the set of integers or the set of real numbers to the set of real numbers. Then f of order g written as f(x) is O(g(x)), if there are constants C and k such that |f(x)| ≤ C |g(x)| whenever x > k (this is read as ‘f(x) is big-oh of g(x)’).

Remark To show f(x) is O(g(x)), we need only find one pair of constants C and k such that f(x) < C(g(x)) if x > k. However, a pair C, k that satisfies the definition is not unique. Moreover, if one such pair exists, there are infinitely many such pairs. A simple way to see this is to note that if C, k is one such pair, any pair C1, k1 with C < C1 and k < k1 also satisfies the definition, since f (x) < C1g(x) whenever x > k1 > k.

Example Use O-notation to express |3x3+ 2x + 7| ≤ 12 |x3, for all real numbers x > 1.

Solution: Take C = 12 and k = 1, the given statement translates to 2x3+ 2x + 7 is O(x3).

Note: Order of Polynomial functions

i) If 1 < x then x < x2and so x2< x3. Thus, if 1 < x, then 1 < x < x0< x3.

ii) For any rational numbers r and s, if x > 1 and r < s, then x0< xs. Therefore xsis O(xs).

Example Show that for any real number x > 1, 2x4+ 4x3+ 5 ≤ 11x4. Solution: Since x is a real number and x > 1, we have x3< x4and 1 < x4. So 4x3< 4x4and 5 < 5x4. Adding we obtain, 2x4+ 4x3+ 5 ≤ 2x4+ 4x4+ 5x4= 11x4.

2. Describe direct and indirect proof techniques

A significant requirement for reading this subject is the ability to follow proofs. In mathematical arguments, we employ the accepted rules of deductive reasoning, and many proofs are simply a sequence of such steps.

Direct Proof: Consider a set of hypothesis H1, H2, …, Hn from which we want to infer a conclusion C.

M.C.A vth semester Page 4

Page 5: Theoryofcomp science

Theory of Computer Science

Consider the example: Prove that if x and y are rational numbers then x + y is rational.

Solution: Since x and y are rational numbers, we can find integers p, q, m, n such that x = p/q and y = m/n. Then x + y = p/q + m/n = (pn + mq)/qn. Since pn + mq and qn are both integers, we conclude that x + y is a rational number.

Indirect Proof: Proofs that are not direct are called indirect. Two main types of indirect proof uses both the negation and conclusion, so they are often suitable when that negation is easy to state. The first type of proof is contra-positive proof.

Consider the example: Prove that if m + n = 73, then m = 37 or n = 37, m and n being positive integers.

Solution: We prove this by taking contra-positive: not “m = 37 or n = 37” implies not “m + n = 73”. By De morgan law, the negation of “m = 37 or n = 37” is “not m = 37 and n = 37”. That is, “m ≤ 36 and n ≤ 36” so that the contrapositive proposition is if m ≤ 36 and n ≤ 36 then m + n ≤ 72. This follows that from the inequalities: a ≤ c and b ≤ d imply that a + b ≤ c + d for all real numbers a, b, c, d.

A few special proof techniques are used so frequently that it is appropriate to review them briefly.

1. Proof by induction

2. Proof by contradiction

3. The pigeonhole principle, and

4. The Diagonalization Principle

5. Proof by Contradiction

3.Discuss about Walks and Paths in Trees

Definition

A finite alternating sequence of vertices and edges (no repetition of edge allowed) beginning and ending with vertices such that each edge is incident with the vertices preceding and following it, is called a walk.

M.C.A vth semester Page 5

Page 6: Theoryofcomp science

Theory of Computer Science

Vertices with which a walk begins and ends are called the terminal vertices of the given walk.

The remaining vertices in the walk are called intermediate vertices of the walk.

A walk is said to be a closed walk if the terminal points are same.

Example

i) From the above definition, it is clear that no edge appears more than once in a walk.

ii) Consider the graph given. We can observe that “v1av2bv3cv3d v4e v2f v5” is a walk

The set of vertices and edges constituting a given walk in a graph G forms a subgraph of G. A walk which is not closed, is called an open walk. [In other words, a walk is said to be an open walk if the terminal points are different]. In the above example, the walk “v1av2bv3cv1” is a closed walk and the walk “v1av2bv3dv4ev2fv5” is an open walk.

4.What is DFA? Discuss about Transition System.

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 × ∑ 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.

M.C.A vth semester Page 6

Page 7: Theoryofcomp science

Theory of Computer Science

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 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.

Transition System (Transition graph)

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 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.C.A vth semester Page 7

Page 8: Theoryofcomp science

Theory of Computer Science

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.

5. Differentiate between Moore machine and Mealy machine.

The automata systems we have discussed so far are limited to binary output. That is, the systems can either accept or reject a string. In thosesystems, this acceptability is decided based on the reachability of the initial state to the final state. This property of the system produces restrictions in 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 automata system is dependent only on the present state, then, Output = (q(t)), where q(t) is the present state. The above automata system is called a Moore machine.

Alternatively, when the output of the automata 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 automata 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

M.C.A vth semester Page 8

Page 9: Theoryofcomp science

Theory of Computer Science

machine 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, , , , , q0) where: Q is non-empty, finite set of states. is non-empty, finite set of input alphabet. is the output alphabet is transition function, which is a mapping from Q into Q.

is the output function mapping Q into q0 Q is the start state.

Conversion of Moore Machine into Mealy machineThe procedure of converting a Moore machine into a Mealy Machine is very simple. From the given Moore machine state table, you need to transform each column of inputs into the pairs of the next state and the output. The output for a particular state in a pair can be determined by observing the outputs in first table (Moore machine). If, after converting table to this format, it is observed that for any two of the present state the other values in the row are identical, then we can delete one of the states. Let us now consider a few examples to convert a given Moore machine into a Mealy machine.Example Convert the Moore machine M1 whose state table is given in table into and equivalent mealy machine.

Moore machine Present State Next state Output

Input a = 0 Input a = 1 q0 q1 q2 1 q1 q3 q2 0 q2 q2 q1 1 q3 q0 q3 1

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 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.

6.

M.C.A vth semester Page 9

Page 10: Theoryofcomp science

Theory of Computer Science

Define context-free grammar. What is an ambiguous grammar? Explain with an example.

Definition A grammar G is a quadruple G = (VN, VT, , S), where VN is set of variables or non-terminals VT is set of terminals symbols @ is set of productions S is the start symbol, is said to be type 2 grammar or context free grammar (CFG) if all the productions are of the form A →α, where α € (VN u VT)* and A € VN. The symbol ᴧ (indicating NULL string) can appear on the right hand side of any production).

The language generated from this grammar is called type-2 language or context free language (CFL).

Observations:

1. There is only one symbol A on the left hand side of the production and that symbol must be a non-terminal.

2. α € (VN u VT)* implies that right hand side of the string may contain any number of terminals and non-terminals including ᴧ(NULL string).

3. Every regular grammar is a CFG and hence a regular language is also context free language but the reverse is not true always.

4. Notation: nx(w) = number of x’s in the string w.

Example Let G = (VN, VT, , S) be a CFG, where VN = {S} VT = {a, b} : S → aSa | bSb | ᴧS: Starting symbol. Find the language generated by this grammar

Solution: The null string ᴧ can be obtained by applying the production S → ᴧ so that S =>ᴧ. S =>ᴧ aSa (applying S → aSa) => abSba (applying S → bSb) => abbSbba (applying S → bSb) => abbbSbbba (applying S → bSb) => abbbbbba (applying S →ᴧ). Therefore, by applying the productions S → aSa and S → bSb, and any number of times and in any order, finally applying the production S → aSa, we get a string w followed by reverse of it (say wR).

M.C.A vth semester Page 10

Page 11: Theoryofcomp science

Theory of Computer Science

Hence, the language generated by this grammar is L = {wwR | w € {a + b}*}. Since this language is generated from the context free grammar, it is a context free language.

Example Show that the language L = {ambn|m ≠ n} is context free. Solution: It is clear from the given language that a number of a’s are followed by n 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 n number of a’s are followed by one S followed by n number of b’s are generated. Now, if we replace the non-terminal S by the production S -> ABwe get n number of a’s followed by n 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, the context free grammar 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, which generates the language L = {ambn m n}. Since a CFG exists for the language, the language is context free.

Example Obtain a Context free grammar on {a, b} to generate a language L = {anwwRbn|w € ∑*, n 1}. Solution: Here, the string wwRmust be enclosed between an and bn where n ≥ 1. The final grammar is VN = (S} VT = {a, b} @: S -> aSb |aAb A -> aAa |bAb|ᴧ, and S is the start symbol.

We will verify, by taking n = 2, so that we generate a string aabaabbb (here w = ba, and that wR

= ab). Consider the following sequence of productions to get the string aabaabbb. S => aSb (applying S -> aSb) aaAbb (applying S -> aAb) aabAbbb (applying A -> bAb) aabaAabbb (applying A -> aAa)

M.C.A vth semester Page 11

Page 12: Theoryofcomp science

Theory of Computer Science

aabaᴧ abbb (applying A ->ᴧ) aabaabbb. (since is an empty string)

Problem Obtain the context free grammar for the regular expression (011 + 1)*(01)*. Solution: The expression (011 + 1)*(01)* is of the form A*B* where A = 001 or 1 and B = 01. The regular expression A*B* means that any number of A’s (possibly none) are followed by any number of B’s (possibly none). Any number of A’s (that is, 011’s or 1’s) can be generated using the productions A -> 011A | 1A|ᴧAny number of B’s (that is, 01’s) can be generated using the productions B -> 01B|ᴧNow, the language generated from the regular expression (011 + 1)*(01)* can be obtained by concatenating A and B using the production S -> AB Therefore, the final grammar G = (VN, VT, @, S), where VN: {S, A, B} VT: {0, 1} VT: S -> AB, A -> 011A |1A|ᴧB -> 01B |ᴧS: start symbol.

M.C.A vth semester Page 12