Toc Class Test
Toc Class Test
1) Construct the CFG for the language having any number of a's over the
set ∑= {a}.
For the language containing any number of a's over the alphabet Σ={a}
the corresponding Context-Free Grammar (CFG) can be defined as
follows:
CFG:
Non-terminal: S (Start symbol)
Terminal: a
Production rules:
1. S→aS
2. S→ϵS
The first rule S→aS generates one a and then calls the production again, allowing for
any number of a's.
The second rule S→ϵS is the base case, which allows the string to terminate with no
a's, representing the empty string.
This CFG generates strings like ϵ\epsilonϵ, a, aa, aaa, etc., which cover all possible strings of
a over the alphabet Σ={a}.
This grammar is already left-linear since all the non-terminal symbols appear at the
leftmost side of the right-hand side of the production rules. A left-linear grammar is a
type of regular grammar where all the rules have the form A→xB or A→xA where B
is a non-terminal and x is a string of terminals.
Thus, there is no need for conversion as this grammar is already left-linear. Let's
rewrite it just for clarity:
Left-Linear Grammar:
1. S→Sa ∣ Abc
2. A→Sa ∣ Ab ∣ a
Handling multiple rules for the same non-terminal: Combine similar rules into
a single set where necessary.
Step 1: Reverse the production rules
We'll reverse each production and modify non-terminals as needed. For example:
1. Reversing the language: For every string generated by an RLG, the corresponding
LLG must generate the reverse of that string. Therefore, we will first reverse the
production rules by shifting non-terminals to the leftmost position.
2. Handling multiple rules for the same non-terminal: Combine similar rules into a
single set where necessary.
We'll reverse each production and modify non-terminals as needed. For example:
1. From S→aS
o S→Sa(reverse of S→aS)
o A→Sb (reverse of S→bA)
2. From S→aS ∣ bB:
o S→Sa (already done in previous step)
o B→Sb (reverse of S→bB)
3. From S→aB ∣ bB ∣ϵ:
o B→Sa (reverse of S→aB)
o B→Sb (already done)
o S→ϵ (remains unchanged since it represents the empty string)
4) Differenciate between Chomsky normal form CNF and Greibach normal form
GNF alongwith their examples.
5) Derive the string for leftmost derivation and rightmost derivation using a CFG
given by, w1=("aabbabba") and w2= "00101" 1) S → aB | bA 2)S → A1B S → a |
aS | bAA A → 0A | ε S → b | aS | aBB B → 0B | 1B | ε
Derivation of w1 = "aabbabba"
1. Leftmost Derivation
1. Start with S.
2. Rightmost Derivation
1. Start with S.
Derivation of w2 = "00101"
1. Leftmost Derivation
1. Start with S.
2. Rightmost Derivation
1. Start with S.
7)
Given a grammar G, its corresponding language L(G) represents the set of all strings
generated from G. Consider the following grammar,
G: S-> aSb|ε
8) Explain PDA
Pushdown Automata(PDA)
o Pushdown automata is a way to implement a CFG in the same way we design DFA
for a regular grammar. A DFA can remember a finite amount of information, but a
PDA can remember an infinite amount of information.
o Pushdown automata is simply an NFA augmented with an "external stack memory".
The addition of stack is used to provide a last-in-first-out memory management
capability to Pushdown automata. Pushdown automata can store an unbounded
amount of information on the stack. It can access a limited amount of information on
the stack. A PDA can push an element onto the top of the stack and pop off an
element from the top of the stack. To read an element into the stack, the top elements
must be popped off and are lost.
o A PDA is more powerful than FA. Any language which can be acceptable by FA can
also be acceptable by PDA. PDA also accepts a class of language which even cannot
be accepted by FA. Thus PDA is much more superior to FA.
Γ: a stack symbol which can be pushed and popped from the stack
9)
10)
Given CFG:
S → 0S1 | A
A → 1A0 | S | ε
For string: '11'
To construct a PDA from the given CFG, the approach is to simulate the derivations
of the CFG in the PDA by pushing and popping the symbols in correspondence to the
production rules.
PDA Transitions:
4. d(q0, 1, A) = (q1, ε)
5. d(q1, 1, A) = (q1, ε)
11) Convert a PDA to a CFG : d(q0,0,Z0) = (q0, XZo) d(q0,0,X) = (q0,XX) d(q0,1,X)
= (q1,e) d(q1,1,X) = (q1,e) d(q1,e,X) = (q1,e) d(q1,e,Zo) = (q1,e
PDA Transitions
Start Symbol
The start symbol of the CFG will be A_q0q1, which represents the PDA moving from
state q0 to state q1, starting with the initial stack symbol Z0.
The final CFG that corresponds to the given PDA transitions is:
1. A_q0q1 → 0A_q0q0 | 1A_q0q1
2. A_q0q0 → 0A_q0q0A_q0q0
3. A_q1q1 → 1A_q1q1 | ε
13) Construct push down automata (PDA) for a^nb^n where n>=1.
A PDA for the language L = { a^n b^n | n ≥ 1 } works by pushing each 'a' onto the
stack and then popping for each 'b'. When the stack is empty and the input is fully
consumed, the string is accepted.
Transitions:
3. d(q0, b, a) = (q1, ε)
4. d(q1, b, a) = (q1, ε)
For the language L = { a^n b^2n | n ≥ 1 }, the PDA pushes 'a' onto the stack and
expects two 'b's for each 'a'. The transitions handle two 'b's for each pop of 'a'.
Transitions:
3. d(q0, b, a) = (q1, ε)
4. d(q1, b, a) = (q1, ε)
Turing Machine was invented by Alan Turing in 1936 and it is used to accept
Recursive Enumerable Languages (generated by Type-0 Grammar).
In the context of automata theory and the theory of computation, Turing machines are
used to study the properties of algorithms and to determine what problems can and
cannot be solved by computers. They provide a way to model the behavior of
algorithms and to analyze their computational complexity, which is the amount of
time and memory they require to solve a problem.
A Turing machine is a finite automaton that can read, write, and erase symbols on an
infinitely long tape. The tape is divided into squares, and each square contains a
symbol. The Turing machine can only read one symbol at a time, and it uses a set of
rules (the transition function) to determine its next action based on the current state
and the symbol it is reading.
A turing machine consists of a tape of infinite length on which read and writes
operation can be performed. The tape consists of infinite cells on which each cell
either contains input symbol or a special symbol called blank. It also consists of a
head pointer which points to cell currently being read and it can move in both
directions.
B is blank symbol (every cell is filled with B except input alphabet initially)
F is the set of final states. If any state of F is reached, input string is accepted.
17) Show that if a language is accepted by a multitape turing machine ,it is accepted
by a single-tape TM.
The proof involves constructing a single-tape Turing machine that simulates the
behavior of the multitape Turing machine. Here are the main steps:
In the single-tape Turing machine, we can combine all the tapes into a single tape.
The single tape will use a special delimiter (e.g., a special symbol such as #) to
separate the contents of each tape. The configuration of the single tape will look like
this:
x1x2x3…#y1y2y3…#…#z1z2z3
The single-tape TM needs to simulate the transitions of the multitape TM. For this, we
will keep track of the head positions of all kkk tapes.
When the multitape TM moves one of its heads, the single-tape TM must adjust the
position of the head accordingly. Here’s how we can do that:
1. Initialization: The single-tape TM starts with its head at the leftmost symbol of the
first tape.
2. Reading Symbols: To read the symbols from each tape:
o The single-tape TM can first move to the delimiter #, reading the symbols
from the first tape until it reaches the first delimiter.
o Then, it moves to the next delimiter to read symbols from the second tape, and
so on.
3. Simulating Head Movements:
o The single-tape TM simulates the movements of the multitape TM’s heads. If
the multitape TM moves one of its heads left or right, the single-tape TM must
navigate through the entire tape to reach the relevant symbol.
o For example, if the first head of the multitape TM moves right, the single-tape
TM will:
Read the current configuration,
Move to the right (to the next symbol),
Write if necessary, and then
Go back to the position to continue processing.
4. Transition Function:
o The transition function of the multitape TM is defined as
δ(q,a1,a2,…,ak)→(p,b1,b2,…,bk,d1,d2,…,dk)
o The single-tape TM will mimic this by simulating each head movement and
performing the required actions according to the transition function, while
keeping track of the delimiters.
If the multitape TM enters an accepting state, the single-tape TM will also enter a
corresponding accepting state. The single-tape TM will halt and accept the input if the
multitape TM accepts.
19) Draw a Turing machine which subtract two numbers m and n, where m is greater
than n.
Steps:
Step-1. If 0 found convert 0 into X and go right then convert all 0’s into 0’s and go
right.
Step-2. Then convert C into C and go right then convert all X into X and go right.
Step-3. Then convert 0 into X and go left then convert all X into X and go left.
Step-4. Then convert C into C and go left then convert all 0’s into 0’s and go left then
convert all X into X and go right and repeat the whole process.
Step-5. Otherwise if C found convert C into C and go right then convert all X into B
and go right then convert 0 into 0 and go left and then stop the machine.
Here, q0 shows the initial state and q1, q2, q3, q4, q5are the transition states
and q6shows the final state. And X, 0, C are the variables used for subtraction and R,
L shows right and left. Problem-2: Draw a Turing machine which subtract two
numbers m and n, where m is greater than n.
Steps:
Step-1. If 0 found convert all 0’s into 0’s and go right then convert C into C and go
right
Step-2. If X found then convert all X into X and go right or if 0 found then convert 0
into X and go left and go to next step otherwise go to 5th step
Step-3. Then convert all X into X and go left then convert C into C and go left
Step-4. Then convert all 0’s into 0’s and go left then convert B into B and go right
then convert 0 into B and go right and repeat the whole process
Step-5. Otherwise if B found convert B into B and go left then convert all X into B
and go left then convert C into B and go left and then stop the machine.
Here, q0 shows the initial state and q1, q2, q3, q4, q5are the transition states
and q6shows the final state. And B, X, 0, C are the variables used for
subtraction(m>n) and R, L shows right and left and B variable is a input symbol.
To construct a Turing machine for the language L={0n1n2n∣n≥1}L = \{0^n 1^n 2^n
L={0n1n2n∣n≥1}, we need a machine that processes inputs with equal numbers of
000's, 111's, and 222's in that order. The idea is to successively match one 0 with one
1 and one 2 until the entire string is processed.
Steps:
1. Mark a 000:
o Move right and find the first 0, mark it by changing it to X to indicate that it
has been processed.
2. Find the corresponding 111:
o After marking the 0, move to the right until the first 1 is found and mark it by
changing it to Y
3. Find the corresponding 222:
o After marking the 1, continue moving right until the first 2 is found and mark
it by changing it to Z.
4. Return to the beginning:
o After marking a 0, 1, and 2, move left to the beginning of the string, and repeat
the process until all symbols are marked.
5. Check for completion:
o When all 0's, 1's, and 2's are marked, check if the tape only contains X's, Y's,
and Z's. If so, accept the input; otherwise, reject it.
The Turing machine operates in several states to handle marking, checking, and
returning to the start.
P Class
The P in the P class stands for Polynomial Time. It is the collection of decision
problems(problems with a “yes” or “no” answer) that can be solved by a deterministic
machine in polynomial time.
Features:
P is often a class of computational problems that are solvable and tractable. Tractable
means that the problems can be solved in theory as well as in practice. But the
problems that can be solved in theory but not in practice are known as intractable.
NP Class
Features:
The solutions of the NP class are hard to find since they are being solved by a non-
deterministic machine but the solutions are easy to verify.
Problems of NP can be verified by a Turing machine in polynomial time.
w ? A <=> f(w) ? B
Features:
It takes a long time to check them. This means if a solution for an NP-hard problem is
given then it takes a long time to check whether it is right or not.
A problem A is in NP-hard if, for every problem L in NP, there exists a polynomial-
time reduction from L to A.
Features:
The Node Cover Decision Problem is a decision problem associated with graph
theory. It asks whether a given undirected graph contains a set of nodes (vertices)
such that every edge in the graph is incident to at least one of the nodes in the set.
This set of nodes is called a node cover.
Problem Instance
Solution
Since we cannot find any subset of size 2 that covers all edges, we conclude that
there is no node cover of size 2 for this graph.
Complexity
The Node Cover Decision Problem is known to be NP-complete. This means that
while it is easy to verify a given solution (i.e., checking if a specific set of nodes
covers all edges), finding the optimal solution (the smallest node cover) is
computationally challenging.
26) Obtain the solution for the following system of posts correspondence problem, X
= {100, 0, 1}, Y = {1, 100, 00}.
To solve this, we can try to find combinations of the strings from X and Y to see if
any concatenation results in the same string.
Steps
1. Try Using Pair Indices: We'll generate all possible combinations of indices for the
pairs in X and Y.
Exploring Combinations
27) Find whether the lists 1)M = (abb, aa, aaa) and N = (bba, aaa, aa) 2) M = (ab, bab,
bbaaa) and N = (a, ba, bab) have a Post Correspondence Solution
We need to find if there exists a sequence of indices i1, i2, ... such that the
corresponding concatenations from both lists are equal.
We need to find if there is a sequence of indices such that the concatenations from M
and N are equal.