TCS Solved Question Paper
TCS Solved Question Paper
Answer :
● |A| = 1. The cardinality (size) of a set containing only the empty string is 1.
● Proper suffixes are substrings that start from a position other than the beginning of the
string.
○ 123
○ 23
○ 3
A Turing Machine (TM) is a mathematical model of computation that consists of the following
components:
● Tape: An infinite tape divided into cells, each capable of holding a single symbol from a
finite alphabet.
● Head: A read/write head that can move left or right along the tape, one cell at a time.
● States: A finite set of states that the TM can be in.
● Transition Function: A function that determines the next state, the symbol to write on
the tape, and the direction of the head movement based on the current state and the
symbol read from the tape.
● Start State: The initial state of the TM.
● Accept State(s): A set of states that, if the TM enters one of them, it halts and accepts
the input.
● Reject State(s): A set of states that, if the TM enters one of them, it halts and rejects the
input.
A left-linear grammar is a formal grammar where all production rules are of the form:
● A → aB
● A→a
where A and B are variables, and a is a terminal symbol or the empty string (ε).
● False. A DFA can have multiple final states. Each final state represents a possible
accepting configuration of the automaton.
● Context-free languages.
i) State true or false. Pumping lemma is used to show that language is not context-free.
● True. The pumping lemma is a technique used to prove that a language is not
context-free by demonstrating that it violates a certain property inherent to context-free
languages.
*j) Write smallest possible string accepted by the following regular expression. a(a+b)ab
● The smallest string that matches the regular expression is "aab".
Answer :
Chomsky Hierarchy categorizes grammars into four types based on the complexity of their
production rules:
○ Production rules are of the form: αAβ → αγβ, where α, β, γ are strings of
symbols, and A is a non-terminal.
○ Recognizable by Linear Bounded Automata.
○ Generates context-sensitive languages.
3. Type 2 (Context-Free Grammar):
○ For a given input symbol and current state, there is exactly one possible
transition.
2. Non-deterministic Turing Machine (NTM):
○ For a given input symbol and current state, there may be multiple possible
transitions.
3. Multi-tape Turing Machine:
○ A theoretical machine that has access to an oracle, which can solve any
decidable problem in a single step
a) Construct a DFA for a language L1L2 L1={All strings starting with 'a'} L2={All strings not
having 'ab' as substring}
b) Construct the following CFG intNormal Form (CNF) S–>aSa | bSb S–>a |b| aa | bb
c) Design TM for language, L={WCWR|W is in (0+1)*}
Answer :
a) DFA for L1 ∩ L2
To construct the DFA for the intersection of L1 and L2, we can combine the DFAs for each
language.
The DFA for the intersection will have the same states as the product of the two individual
DFAs. A state in the product DFA is a pair of states, one from each individual DFA. The
transition function of the product DFA will be defined such that it simulates the transitions of both
individual DFAs simultaneously. The final states of the product DFA will be the pairs of states
where both individual states are final.
The given CFG is already in CNF. All production rules are of the form A -> BC or A -> a, where
A, B, C are non-terminals and a is a terminal.
1. Initial State:
○ Scan the tape from left to right until the first '1' is encountered.
○ Mark the position of the first '1' with a special symbol (e.g., 'X').
2. Matching Phase:
○Once the end of the input is reached, move the tape head back to the marked
position.
○ Move the tape head one position to the right.
○ Compare the symbol under the head with the symbol to the left of the marked
position.
○ If they match, continue to the next symbol.
○ If they don't match, reject the input.
4. Acceptance:
○ If the tape head reaches the end of the input without any mismatches, accept the
input.
M = (Q, Σ, Γ, δ, q0, B, F)
The detailed transition function can be defined based on the steps described above. The Turing
Machine will systematically scan the tape, mark the middle, and compare the symbols on either
side to ensure the palindrome property
Answer :
A PDA can recognize this language by pushing 'a' symbols onto the stack for each 'a' input, then
popping one 'a' for each 'b' input, and finally popping one 'c' for each 'c' input. If the stack is
empty at the end of the input, the string is accepted.
Formal Definition:
M = (Q, Σ, Γ, δ, q0, Z, F)
A Moore machine can recognize this language by keeping track of the last two input symbols. If
the last two symbols were both '1', the machine outputs '*'. Otherwise, it outputs '#'.
Formal Definition:
M = (Q, Σ, O, δ, q0, λ)
Each state has exactly one transition A state can have multiple transitions for a
Determinism for each input symbol. single input symbol.
Commonly used in lexical analysis and Often used as an intermediate step in compiler
Practical Use text processing. design and theoretical computer science.
a) Construct a Mealy machine over alphabet {0, 1} which toggles its input.
b) Show that L={0n 1n | n>=1} is not regular.
Answer :
Formal Definition:
M = (Q, Σ, O, δ, q0)
● Q = {q0, q1}
● Σ = {0, 1}
● O = {0, 1}
● δ = { (q0, 0) -> (q1, 1), (q0, 1) -> (q1, 0), (q1, 0) -> (q0, 1), (q1, 1) -> (q0, 0) }
● q0 = initial state
Explanation:
● State q0: If the input is 0, the machine transitions to state q1 and outputs 1. If the input
is 1, the machine transitions to state q1 and outputs 0.
● State q1: If the input is 0, the machine transitions to state q0 and outputs 1. If the input
is 1, the machine transitions to state q0 and outputs 0.
This Mealy machine effectively toggles the input bit at each step.
We can use the Pumping Lemma to prove that L is not a regular language.
Pumping Lemma:
If L is a regular language, there exists a pumping length p such that any string s ∈ L with |s| ≥ p
can be written as s = xyz, where:
1. |xy| ≤ p
2. |y| ≥ 1
3. For all i ≥ 0, xy^iz ∈ L
Proof by Contradiction:
Assume L is regular. Let p be the pumping length. Consider the string s = 0^p 1^p ∈ L.
According to the Pumping Lemma, we can write s = xyz, where |xy| ≤ p and |y| ≥ 1.
Now, consider the string xy^2z = 0^(p+k) 1^p. This string is not in L because the number of 0s
and 1s is not equal. This contradicts the Pumping Lemma, so our assumption that L is regular
must be false. Therefore, L is not a regular language
TCS Solved Question paper 2022 October
Answer :
a) A unit production in a grammar is a production rule of the form A -> B, where A and B are
both non-terminal symbols.
b) A Mealy machine can be constructed with two states: one for the initial state and one for the
toggled state. The machine outputs the opposite of the input at each state transition.
c) A proper suffix of a string is a substring that starts from a position other than the beginning of
the string. For example, "xyz" is a proper suffix of "abcxyz". A proper prefix is a substring that
ends before the end of the string. For example, "ab" is a proper prefix of "abc".
d) A Pushdown Automaton (PDA) is a tuple M = (Q, Σ, Γ, δ, q0, Z, F), where Q is a finite set of
states, Σ is a finite input alphabet, Γ is a finite stack alphabet, δ is the transition function, q0 is
the initial state, Z is the initial stack symbol, and F is the set of final states.
e) A left linear grammar has production rules of the form A -> aB or A -> a. A right linear
grammar has production rules of the form A -> Ba or A -> a.
g) Chomsky Normal Form (CNF) and Greibach Normal Form (GNF) are the two main types of
normal forms.
h) A Linear Bounded Automaton (LBA) is a tuple M = (Q, Σ, Γ, δ, q0, B, F), where Q is a finite
set of states, Σ is a finite input alphabet, Γ is a finite tape alphabet, δ is the transition function,
q0 is the start state, B is the blank symbol, and F is the set of final states.
i) True. The Pumping Lemma is used to prove that a language is not context-free.
j) The smallest possible string accepted by the regular expression 10 + (0 + 11)0*1 is "10".
Q2) attempt any 4 for each 2 marks
Answer :
a) Types of Grammar
Chomsky Hierarchy categorizes grammars into four types based on the complexity of their
production rules:
○ Production rules are of the form: αAβ → αγβ, where α, β, γ are strings of
symbols, and A is a non-terminal.
○ Recognizable by Linear Bounded Automata.
○ Generates context-sensitive languages.
3. Type 2 (Context-Free Grammar):
Language
Recognition Recognizes regular languages Recognizes context-free languages
Transition Depends only on the current state Depends on the current state, input
Function and input symbol symbol, and top stack symbol
a) Construct a DFA for a language L1L2 L1= { All strings starting with ‘a’} L2= { All strings not
having ‘ab’ as substring}
b) Construct the following CFG into Normal Form (CNF) S-> ABA A-> aA | B-> bB |
c) Design TM for language, L {WCWR|W is in (0+1)*}
Answer :
a) DFA for L1 ∩ L2
To construct the DFA for the intersection of L1 and L2, we can combine the DFAs for each
language.
1. Initial State:
○ Scan the tape from left to right until the first '1' is encountered.
○ Mark the position of the first '1' with a special symbol (e.g., 'X').
2. Matching Phase:
○ Once the end of the input is reached, move the tape head back to the marked
position.
○ Move the tape head one position to the right.
○ Compare the symbol under the head with the symbol to the left of the marked
position.
○ If they match, continue to the next symbol.
○ If they don't match, reject the input.
4. Acceptance:
○ If the tape head reaches the end of the input without any mismatches, accept the
input.
M = (Q, Σ, Γ, δ, q0, B, F)
The detailed transition function can be defined based on the steps described above. The Turing
Machine will systematically scan the tape, mark the middle, and compare the symbols on either
side to ensure the palindrome property.
Answer :
A PDA can recognize this language by pushing '0' symbols onto the stack for each '0' input, then
pushing '1' symbols for each '1' input. After reading all '1's, it pops a '0' and a '1' for each '2'
input. If the stack is empty at the end of the input, the string is accepted.
Formal Definition:
M = (Q, Σ, Γ, δ, q0, Z, F)
A Moore machine can recognize this language by keeping track of the last two input symbols. If
the last two symbols were both '1', the machine outputs '*'. Otherwise, it outputs '#'.
Formal Definition:
M = (Q, Σ, O, δ, q0, λ)
a) Construct a Mealy machine to convert each occurrence of substring 101 by 100 over
alphabet {0,1}.
b) Show that L = {0n 1n | n>=1} is not regular.
Answer :
a) Mealy Machine to Convert 101 to 100
To convert 101 to 100, we can design a Mealy machine with four states:
Formal Definition:
M = (Q, Σ, O, δ, q0)
Explanation:
● State q0: If the input is 0, the machine stays in q0 and outputs 0. If the input is 1, it
transitions to q1 and outputs 1.
● State q1: If the input is 0, it transitions to q2 and outputs 0. If the input is 1, it goes back
to q0 and outputs 1.
● State q2: If the input is 0, it transitions to q3 and outputs 0 (converting 101 to 100). If the
input is 1, it goes back to q1 and outputs 1.
● State q3: If the input is 0 or 1, it goes back to q0 and outputs the same input.
We can use the Pumping Lemma to prove that L is not a regular language.
Pumping Lemma:
If L is a regular language, there exists a pumping length p such that any string s ∈ L with |s| ≥ p
can be written as s = xyz, where:
1. |xy| ≤ p
2. |y| ≥ 1
3. For all i ≥ 0, xy^iz ∈ L
Proof by Contradiction:
Assume L is regular. Let p be the pumping length. Consider the string s = 0^p 1^p ∈ L.
According to the Pumping Lemma, we can write s = xyz, where |xy| ≤ p and |y| ≥ 1.
Now, consider the string xy^2z = 0^(p+k) 1^p. This string is not in L because the number of 0s
and 1s is not equal.
This contradicts the Pumping Lemma, so our assumption that L is regular must be false.
Therefore, L is not a regular language.
TCS Solved Question paper 2023 April
Answer :
a) Output Function
Moore Machine:
Mealy Machine:
● The output function, λ, maps the current state and the input symbol to an output symbol.
● λ : Q × Σᵢ → Σₒ Where:
○ Q is the set of states
○ Σᵢ is the input alphabet
○ Σₒ is the output alphabet
c) Nullable Symbol
A nullable symbol in a grammar is a non-terminal symbol that can derive the empty string (ε).
A right-linear grammar is a formal grammar where all production rules are of the form:
● A → aB or A → a where:
○ A and B are non-terminal symbols
○ a is a terminal symbol or ε (the empty string)
f) True. DFAs do not have multiple final states. A DFA accepts a string if and only if it reaches a
single final state after processing the entire string.
h) Tuples of LBA
A Linear Bounded Automaton (LBA) is a 7-tuple (Q, Σ, Γ, δ, q₀, Z₀, F), where:
i) False. The pumping lemma is used to show that a language is not regular, not context-free.
The smallest possible string accepted by the regular expression l0+(0+l 1)0*1 is "l01".
Answer :
a) Reduction
Example:
S -> AB | a
A -> Aa | b
B -> Bb | c
We can reduce this grammar by eliminating the redundant production A -> Aa:
S -> AB | a
A -> b
B -> Bb | c
1. Create states:
DFA:
c) DFA vs NFA
Transition For each state and input symbol, there is For each state and input symbol, there can
Function exactly one transition. be zero, one, or multiple transitions.
A string is accepted if and only if there is a A string is accepted if there is at least one
unique path from the initial state to a final path from the initial state to a final state that
Acceptance state that corresponds to the string. corresponds to the string.
d) Epsilon Closure
Epsilon closure of a state is the set of states that can be reached from that state using only
epsilon transitions.
Step-by-Step Calculation
1. Epsilon Closure(q0):
● Start at q0.
● Follow epsilon transitions from q0: We can reach q1 and q2.
● So, Epsilon Closure(q0) = {q0, q1, q2}
2. Epsilon Closure(q1):
● Start at q1.
● Follow epsilon transitions from q1: We can reach q2.
● So, Epsilon Closure(q1) = {q1, q2}
3. Epsilon Closure(q2):
● Start at q2.
● There are no epsilon transitions from q2.
● So, Epsilon Closure(q2) = {q2}
Summary:
● It accepts an input string if it reaches a final state after processing the entire input.
● The machine halts in an accepting state.
● It rejects an input string if it halts in a non-final state after processing the entire input.
● The machine halts in a non-accepting state.
It's important to note that while multi-tape Turing Machines can be more powerful in terms of
computational efficiency, they are equivalent in terms of computational power to single-tape
Turing Machines. Any computation that can be performed by a multi-tape Turing Machine can
also be performed by a single-tape Turing Machine, albeit potentially less efficiently.
a) Construct a DFA for a language over Σ = {0,1 } L1∩L2 L1 = { All strings starting with ‘0’} L2 = {
All strings not having ‘01’ as substring}
b) Construct the following CFG int Normal Form (CNF) S-> aSa | bSb | a | b| aa | bb
c) Construct TM which accepts the language that start with 0 and ends with 1.
Answer :
a) DFA for L1 ∩ L2
To construct the DFA for L1 ∩ L2, we need to combine the conditions of both languages:
1. Initial state: The initial state must be reachable only by reading a '0'.
2. Accepting states: The DFA should accept any string that starts with '0' and doesn't
have the substring '01'.
0
-->q0----->q1
^ | |
| | |
| v v
| q2-----q3
1
Explanation:
Given CFG:
CNF Conversion:
S -> AB | CD | E | F | G | H
A -> aS
B -> a
C -> bS
D -> b
E -> aa
F -> bb
G -> a
H -> b
3.
Transition Function δ:
Explanation:
1. Initial state (q0): If the first symbol is '0', move to state q1. If the first symbol is '1', reject.
2. State q1: Keep moving right while reading '0'. If '1' is encountered, move to state q2.
3. State q2: Keep moving right while reading '1'. If the end of the tape is reached, accept.
4. State q3: Reject state.
Transition Function δ:
Explanation:
1. Initial state (q0): Push 'A' onto the stack for each 'a' read.
2. State q1: Pop 'A' from the stack for each 'b' read.
3. Final state (q2): Reached after all 'a's and 'b's are matched.
Transition Function δ:
δ(q0, 0) = (q0, C)
δ(q0, 1) = (q1, C)
δ(q1, 0) = (q2, A)
δ(q1, 1) = (q3, C)
δ(q2, 0) = (q0, C)
δ(q2, 1) = (q1, B)
δ(q3, 0) = (q0, C)
δ(q3, 1) = (q3, C)
Explanation:
c) Chomsky's Hierarchy
This hierarchy provides a framework for understanding the complexity of formal languages and
the computational power required to recognize and generate them.
Q5) attempt any 1 for 3 marks
a) Construct a Moore machine over alphabet {0,1} to get l’s compliment of a given binary string.
b) Show that L = {0n 1n | n>=1} is not regular
Answer :
● We need a Moore machine that outputs the 1's complement of the input binary string.
● In a Moore machine, the output is solely determined by the current state.
Transition Function:
Explanation:
● Initial State (q0): If the input is '0', the output is '1' and the machine stays in state q0. If
the input is '1', the output is '0' and the machine transitions to state q1.
● State q1: If the input is '0', the output is '1' and the machine stays in state q1. If the input
is '1', the output is '0' and the machine transitions back to state q0.
This Moore machine will output the 1's complement of the input binary string.
1. |xy| ≤ p
2. |y| ≥ 1
3. For all i ≥ 0, xy^iz ∈ L
Proof by Contradiction:
Assume L is regular. Let p be the pumping length. Consider the string s = 0^p 1^p.
According to the Pumping Lemma, we can write s = xyz, where |xy| ≤ p and |y| ≥ 1.
Now, let's pump y twice: xy^2z. This string will have more 0s than 1s, and thus, it does not
belong to L.
This contradicts the Pumping Lemma, and therefore, our assumption that L is regular must be
false.
Answer :
False. While PDAs can accept some non-regular languages, they can also accept regular
languages.
c) ambiguous grammar.
An ambiguous grammar is a context-free grammar that generates a string in multiple ways, i.e.,
there are two or more distinct parse trees for the same string.
e) What is Reduction?
Reduction is a technique used in formal language theory to simplify a grammar by eliminating
redundant or unnecessary productions.
A non-deterministic Turing machine is a Turing machine that has multiple possible transitions for
a given state and input symbol. This allows it to explore multiple computation paths
simultaneously.
i)
The epsilon closure of a state is the set of all states that can be reached from that state by
following zero or more epsilon transitions.
Answer :
1. Direct Definition:
○ Explicitly define the language by specifying the set of strings that the PDA
accepts. For example, the language L = {a^n b^n | n >= 0} can be directly defined
as the set of all strings with an equal number of 'a's followed by an equal number
of 'b's.
2. Grammar Definition:
○ Use a context-free grammar to generate the language. The PDA can then be
constructed to recognize the strings generated by the grammar. For example, the
grammar S -> aSb | ε generates the language L = {a^n b^n | n >= 0}.
1. Right-Linear Grammar:
○ Production rules are of the form A -> aB or A -> a, where A and B are
non-terminal symbols and a is a terminal symbol.
○ Examples:
■ S -> 0A | 1B
■ A -> 0A | 1B | ε
■ B -> 0B | 1B | ε
2. Left-Linear Grammar:
○ Production rules are of the form A -> Ba or A -> a, where A and B are
non-terminal symbols and a is a terminal symbol.
○ Examples:
■ S -> A0 | B1
■ A -> A0 | A1 | ε
■ B -> B0 | B1 | ε
1. Create states:
○ q0 -> q1 on input 0 or 1
○ q1 -> q1 on input 0 or 1
○ q1 -> q2 on input 0
○ q2 -> q0 on input 0
Step 2: Convert NFA to DFA (using subset construction)
DFA:
Moore Machine:
Mealy Machine:
● Output is determined by both the current state and the input symbol.
● Output function: λ : Q × Σᵢ -> Σₒ (Q is the set of states, Σᵢ is the input alphabet, Σₒ is the
output alphabet)
1. Tape Length:
a) Construct DFA for language which contains all string with exactly 2 consecutive 1's any
where.
b) Convert the following CFG into CNF S XYX X aX/ Y bY/
c) Design TM for language L = {a m b n / n m and m 1}
Answer :
0 1
q0 ----> q0
^| |
|| |
|v v
q1 ----> q2 ----> q3
1 0/1
Explanation:
Given CFG:
S -> XYX
X -> aX | ε
Y -> bY | ε
CNF Conversion:
S -> XYX | XY | YX | X | Y
X -> aX | a
Y -> bY | b
Step 2: Eliminate unit productions:
S -> AX | AY | BX | BY | a | b
A -> a
B -> b
X -> aX | a
Y -> bY | b
Turing Machine:
Transition Function δ:
Explanation:
1. q0: Initial state. Replaces the first 'a' with a blank symbol and moves right.
2. q1: Replaces subsequent 'a's with blanks and moves right.
3. q2: Moves right over 'b's, replacing them with blanks.
4. q3: Accepts the input if all 'b's are processed.
5.
Q4) attempt any 2 for each 4 marks
a) Construct a PDA for the language {w / na (w) = nb (w)} number of a's is equal to number of
b's.
b) Construct Moore machine which outputs even or odd according to number of a's encounted is
even or odd.
c) Construct equivalent DFA for the following NFA.
Answer :
Idea: Push an 'A' onto the stack for each 'a' encountered in the input. Pop an 'A' for each 'b'. If
the stack is empty at the end of the input, the string is accepted.
PDA:
Transition Function δ:
Idea: Use two states to track whether the number of 'a's seen so far is even or odd.
Moore Machine:
Transition Function δ:
δ(q0, a) = q1
δ(q0, b) = q0
δ(q1, a) = q0
δ(q1, b) = q1
Output Function λ:
λ(q0) = even
λ(q1) = odd
c)
To construct an equivalent DFA from the given NFA, we will use the subset construction method.
This method involves creating DFA states that represent subsets of NFA states.
1. Initial State:
● The initial state of the DFA will be the epsilon closure of the initial state of the NFA.
● In this case, the epsilon closure of q0 is {q0, q1, q2, q3, q4, q5, q6, q7}.
2. Transition Function:
● For each DFA state (a subset of NFA states) and each input symbol, we determine the
set of NFA states that can be reached from the current DFA state by following the
corresponding transitions in the NFA.
3. Final States:
● A DFA state is a final state if it contains at least one final state of the NFA. In this case,
states containing q7 will be final.
Transitions:
{q0, q1, q2, q3, q4, q5, q6, q7} --a--> {q1, q2, q3, q4, q5, q6, q7}
{q0, q1, q2, q3, q4, q5, q6, q7} --b--> {q6, q7}
{q1, q2, q3, q4, q5, q6, q7} --a--> {q1, q2, q3, q4, q5, q6, q7}
{q1, q2, q3, q4, q5, q6, q7} --b--> {q6, q7}
... (similarly for other states)
Final States:
By following these steps and constructing the DFA, we can now process input strings
deterministically and accept the same language as the original NFA.
Answer :
Pumping Lemma: If L is regular, there exists a pumping length p such that any string s in L with
|s| ≥ p can be written as s = xyz, where:
1. |xy| ≤ p
2. |y| ≥ 1
3. For all i ≥ 0, xy^iz ∈ L
Proof by Contradiction:
Assume L is regular. Let p be the pumping length. Consider the string s = 0^p 1^p 0^p.
According to the Pumping Lemma, we can write s = xyz, where |xy| ≤ p and |y| ≥ 1.
Now, let's pump y twice: xy^2z. This string will have more 0s in the first part than in the last part,
and thus, it does not belong to L.
This contradicts the Pumping Lemma, and therefore, our assumption that L is regular must be
false.
Eliminating ε-productions:
New Grammar:
S -> AB | A | B
A -> SA | BB | bB | aA | a
B -> b | aA