Formal Language
Formal Language
Introduction :- Alphabet
Related Terminologies
Alphabet
• Definition − An alphabet is any finite set of symbols.
• Example − ∑ = {a, b, c, d} is an alphabet set where ‘a’, ‘b’, ‘c’, and ‘d’
are symbols.
String
• Definition − A string is a finite sequence of symbols taken from ∑.
• Example − ‘cabcad’ is a valid string on the alphabet set ∑ = {a, b, c, d}
Length of a String
• Definition − It is the number of symbols present in a string. (Denoted
by |S|).
• Examples −
o If S = ‘cabcad’, |S|= 6
o If |S|= 0, it is called an empty string (Denoted by λ or ε)
Kleene Star
• Definition − The Kleene star, ∑*, is a unary operator on a set of symbols
or strings, ∑, that gives the infinite set of all possible strings of all
possible lengths over ∑ including λ.
• Representation − ∑* = ∑0 ∪ ∑1 ∪ ∑2 ∪……. where ∑p is the set of all
possible strings of length p.
• Example − If ∑ = {a, b}, ∑* = {λ, a, b, aa, ab, ba, bb,………..}
Kleene Closure / Plus
• Definition − The set ∑+ is the infinite set of all possible strings of all
possible lengths over ∑ excluding λ.
• Representation − ∑+ = ∑1 ∪ ∑2 ∪ ∑3 ∪…….
∑+ = ∑* − { λ }
• Example − If ∑ = { a, b } , ∑+ = { a, b, aa, ab, ba, bb,………..}
Language
• Definition − A language is a subset of ∑* for some alphabet ∑. It can be
finite or infinite.
• Example − If the language takes all possible strings of length 2 over ∑
= {a, b}, then L = { ab, aa, ba, bb }
Theory of Automata
Theory of automata is a theoretical branch of computer science and mathematical. It
is the study of abstract machines and the computation problems that can be solved
using these machines. The abstract machine is called the automata. The main
motivation behind developing the automata theory was to develop methods to
describe and analyse the dynamic behaviour of discrete systems.
This automaton consists of states and transitions. The State is represented by circles,
and the Transitions is represented by arrows.
Automata is the kind of machine which takes some string as input and this input goes
through a finite number of states and may enter in the final state.
There are the basic terminologies that are important and frequently used in automata
Symbols:
Symbols are an entity or individual objects, which can be any letter, alphabet or any
picture.
Example:
1, a, b, #
Alphabets:
Alphabets are a finite set of symbols. It is denoted by ∑.
Examples:
1. ∑ = {a, b}
2.
3. ∑ = {A, B, C, D}
4.
5. ∑ = {0, 1, 2}
6.
7. ∑ = {0, 1, ....., 5]
8.
9. ∑ = {#, β, Δ}
String:
Example 1:
If ∑ = {a, b}, various string that can be generated from ∑ are {ab, aa, aaa, bb, bbb, ba,
aba.....}.
Example 2:
1. w = 010
2.
3. Number of Sting |w| = 3
Language:
Example: 1
L1 = {Set of string of length 2}
= {aa, bb, ba, bb} Finite Language
Example: 2
L2 = {Set of all strings starts with 'a'}
Grammar
A grammar G can be formally written as a 4-tuple (N, T, S, P) where −
• N or VN is a set of variables or non-terminal symbols.
• T or ∑ is a set of Terminal symbols.
• S is a special variable called the Start symbol, S ∈ N
• P is Production rules for Terminals and Non-terminals. A production
rule has the form α → β, where α and β are strings on VN ∪ ∑ and least
one symbol of α belongs to VN.
Example
Grammar G1 −
({S, A, B}, {a, b}, S, {S → AB, A → a, B → b})
Here,
• S, A, and B are Non-terminal symbols;
• a and b are Terminal symbols
• S is the Start symbol, S ∈ N
• Productions, P : S → AB, A → a, B → b
Example
Grammar G2 −
(({S, A}, {a, b}, S,{S → aAb, aA → aaAb, A → ε } )
Here,
• S and A are Non-terminal symbols.
• a and b are Terminal symbols.
• ε is an empty string.
• S is the Start symbol, S ∈ N
• Production P : S → aAb, aA → aaAb, A → ε
Example
Let us consider the grammar −
G2 = ({S, A}, {a, b}, S, {S → aAb, aA → aaAb, A → ε } )
Some of the strings that can be derived are −
S ⇒ aAb using production S → aAb
⇒ aaAbb using production aA → aAb
⇒ aaaAbbb using production aA → aaAb
⇒ aaabbb using production A → ε
Grammar in theory of computation is a finite set of formal rules that are generating
syntactically correct sentences.
The formal definition of grammar is that it is defined as four tuples −
G=(V,T,P,S)
• G is a grammar, which consists of a set of production rules. It is used to
generate the strings of a language.
• T is the final set of terminal symbols. It is denoted by lower case letters.
• V is the final set of non-terminal symbols. It is denoted by capital letters.
• P is a set of production rules, which is used for replacing non-terminal
symbols (on the left side of production) in a string with other terminals
(on the right side of production).
• S is the start symbol used to derive the string.
Grammar is composed of two basic elements
Terminal Symbols - Terminal symbols are the components of the sentences that are
generated using grammar and are denoted using small case letters like a, b, c etc.
Non-Terminal Symbols - Non-Terminal Symbols take part in the generation of the
sentence but are not the component of the sentence. These types of symbols are
also called Auxiliary Symbols and Variables. They are represented using a capital
letter like A, B, C, etc.
Example 1
Consider a grammar
G = (V , T , P , S)
Where,
V = { S , A , B } ⇒ Non-Terminal symbols
T={a,b} ⇒ Terminal symbols
Production rules P = { S → ABa , A → BB , B → ab , AA → b }
S={S} ⇒ Start symbol
Example 2
Consider a grammar
G=(V,T,P,S)
Where,
V= {S, A, B} ⇒ non terminal symbols
T = { 0,1} ⇒ terminal symbols
Production rules P = { S→A1B
A→0A| ε
B→0B| 1B| ε }
S= {S} ⇒ start symbol.
Types of grammar
The different types of grammar −
The diagram representing the types of grammar in the theory of computation (TOC)
is as follows −
is ( V + T )*.
In type 0 there must be at least one variable on the Left side of production.
For example:
Sab --> ba
A --> S
Here, Variables are S, A, and Terminals a, b.
Type 1: Context-Sensitive Grammar
Type-1 grammars generate context-sensitive languages. The language generated
by the grammar is recognized by the Linear Bound Automata
In Type 1
• First of all Type 1 grammar should be Type 0.
• Grammar Production in the form of
|\alpha |<=|\beta |
For Example:
S --> AB
AB --> abc
B --> b
Type 2: Context-Free Grammar: Type-2 grammars generate context-free
languages. The language generated by the grammar is recognized by a Pushdown
automata. In Type 2:
• First of all, it should be Type 1.
• The left-hand side of production can have only one variable and there is
no restriction on
|\alpha | = 1.
For example:
S --> AB
A --> a
B --> b
Type 3: Regular Grammar: Type-3 grammars generate regular languages.
These languages are exactly all languages that can be accepted by a finite-state
automaton. Type 3 is the most restricted form of grammar.
Type 3 should be in the given form only :
V --> VT / T (left-regular grammar)
(or)
V --> TV /T (right-regular grammar)
For example:
S --> a
The above form is called strictly regular grammar.
There is another form of regular grammar called extended regular grammar. In
this form:
V --> VT* / T*. (extended left-regular grammar)
(or)
V --> T*V /T* (extended right-regular grammar)
For example :
S --> ab.
Introduction
According to the given hierarchy, Grammar is divided into four types
Type 0 Unrestricted Grammar
Module 2
Regular language and finite automata : (Regular expression and
language)
A Regular Expression can be recursively defined as follows −
• ε is a Regular Expression indicates the language containing an empty
string. (L (ε) = {ε})
• φ is a Regular Expression denoting an empty language. (L (φ) = { })
• x is a Regular Expression where L = {x}
• If X is a Regular Expression denoting the language L(X) and Y is a
Regular Expression denoting the language L(Y), then
o X + Y is a Regular Expression corresponding to the
language L(X) ∪ L(Y) where L(X+Y) = L(X) ∪ L(Y).
o X . Y is a Regular Expression corresponding to the
language L(X) . L(Y) where L(X.Y) = L(X) . L(Y)
o R* is a Regular Expression corresponding to the
language L(R*)where L(R*) = (L(R))*
• If we apply any of the rules several times from 1 to 5, they are Regular
Expressions.
Some RE Examples
(a+b)* Set of strings of a’s and b’s of any length including the null
string. So L = { ε, a, b, aa , ab , bb , ba, aaa…….}
(a+b)*abb Set of strings of a’s and b’s ending with the string abb. So L =
{abb, aabb, babb, aaabb, ababb, …………..}
(aa + ab + ba + bb)* String of a’s and b’s of even length can be obtained by
concatenating any combination of the strings aa, ab, ba and bb
including null, so L = {aa, ab, ba, bb, aaab, aaba, …………..}
Regular Expressions
Regular Expressions are used to denote regular languages. An expression is
regular if:
• ɸ is a regular expression for regular language ɸ.
• ɛ is a regular expression for regular language {ɛ}.
• If a ∈ Σ (Σ represents the input alphabet), a is regular expression
with language {a}.
• If a and b are regular expression, a + b is also a regular expression
with language {a,b}.
• If a and b are regular expression, ab (concatenation of a and b) is
also regular.
• If a is regular expression, a* (0 or more times a) is also regular.
Regular
• Expression Regular Languages
a followed by 0 or
(a.b*) {a, ab, abb, abbb, abbbb,….}
more b
• Q = {a, b, c},
• ∑ = {0, 1},
• q0 = {a},
• F = {c}, and
Transition function δ as shown by the following table −
Present State Next State for Input 0 Next State for Input 1
a a b
b c a
c b c
Utility – To construct DFA from a given regular expression, we can first construct
an NFA for the given expression and then convert this NFA to DFA by a subset
construction method. But to avoid this two-step procedure, the other way round
is to directly construct a DFA for the given expression.
DFA refers to Deterministic Finite Automata. In DFA, for each state, and for each
input symbol there is one and only one state to which the automaton can have a
transition from its current state. DFA does not accept any ∈-transition.
In order to construct a DFA directly from a regular expression, we need to follow
the steps listed below:
Example: Suppose given regular expression r = (a|b)*abb
1. Firstly, we construct the augmented regular expression for the given
expression. By concatenating a unique right-end marker ‘#’ to a regular expression
r, we give the accepting state for r a transition on ‘#’ making it an important state
of the NFA for r#.
So, r' = (a|b)*abb#
2. Then we construct the syntax tree for r#.
n is a leaf node
true ∅ ∅
labeled €
n is a leaf node
labelled with false {i} {i}
position i
Node n nullable(n) firstpos(n) lastpos(n)
n is an or node
with left child c1 nullable(c1) or firstpos(c1) ∪ lastpos(c1) ∪
and right child nullable(c2) firstpos(c2) lastpos(c2)
c2
n is a star node
with child node true firstpos(c1) lastpos(c1)
c1
Let us now compute the followpos bottom up for each node in the syntax tree.
NODE followpos
1 {1, 2, 3}
2 {1, 2, 3}
3 {4}
4 {5}
5 {6}
NODE followpos
6 ∅
4. Now we construct Dstates, the set of states of DFA D and Dtran, the transition
table for D. The start state of DFA D is firstpos(root) and the accepting states are
all those containing the position associated with the endmarker symbol #.
According to our example, the firstpos of the root is {1, 2, 3}. Let this state be A and
consider the input symbol a. Positions 1 and 3 are for a, so let B = followpos(1) ∪
followpos(3) = {1, 2, 3, 4}. Since this set has not yet been seen, we set Dtran[A, a]
:= B.
When we consider input b, we find that out of the positions in A, only 2 is
associated with b, thus we consider the set followpos(2) = {1, 2, 3}. Since this set
has already been seen before, we do not add it to Dstates but we add the transition
Dtran[A, b]:= A.
Continuing like this with the rest of the states, we arrive at the below transition
table.
Input
State a b
⇢A B A
B B C
C B D
D B A
• Q = {a, b, c}
• ∑ = {0, 1}
• q0 = {a}
• F = {c}
The transition function δ as shown below −
Present State Next State for Input 0 Next State for Input 1
a a, b b
b c a, c
c b, c c
DFA vs NDFA
The following table lists the differences between DFA and NDFA.
DFA NDFA
The transition from a state is to a single The transition from a state can be to
particular next state for each input symbol. multiple next states for each input
Hence it is called deterministic. symbol. Hence it is called non-
deterministic.
Empty string transitions are not seen in NDFA permits empty string
DFA. transitions.
Acceptor (Recognizer)
An automaton that computes a Boolean function is called an acceptor. All the states
of an acceptor is either accepting or rejecting the inputs given to it.
Classifier
A classifier has more than two final states and it gives a single output when it
terminates.
Transducer
An automaton that produces outputs based on current input and/or previous state is
called a transducer. Transducers can be of two types −
• Mealy Machine − The output depends both on the current state and the
current input.
• Moore Machine − The output depends only on the current state.
Strings accepted by the above DFA: {0, 00, 11, 010, 101, ...........}
Strings not accepted by the above DFA: {1, 011, 111, ........}
Let, M = (Q, ∑, δ, q0, F) is an NFA which accepts the language L(M). There should be
equivalent DFA denoted by M' = (Q', ∑', q0', δ', F') such that L(M) = L(M').
Step 2: Add q0 of NFA to Q'. Then find the transitions from this start state.
Step 3: In Q', find the possible set of states for each input symbol. If this set of states
is not in Q', then add it to Q'.
Step 4: In DFA, the final state will be all the states which contain F(final states of NFA)
Example 1:
Convert the given NFA to DFA.
Solution: For the given transition diagram we will first construct the transition table.
State 0 1
→q0 q0 q1
q1 {q1, q2} q1
1. δ'([q0], 0) = [q0]
2. δ'([q0], 1) = [q1]
1. δ'([q2], 0) = [q2]
2. δ'([q2], 1) = [q1, q2]
The state [q1, q2] is the final state as well because it contains a final state q2. The
transition table for the constructed DFA will be:
State 0 1
Example 2:
Convert the given NFA to DFA.
Solution: For the given transition diagram we will first construct the transition table.
State 0 1
1. δ'([q1], 0) = ϕ
2. δ'([q1], 1) = [q0, q1]
Similarly,
As in the given NFA, q1 is a final state, then in DFA wherever, q1 exists that state
becomes a final state. Hence in the DFA, final states are [q1] and [q0, q1]. Therefore
set of final states F = {[q1], [q0, q1]}.
State 0 1
Suppose
1. A = [q0]
2. B = [q1]
3. C = [q0, q1]
∑ = {a,b}
Initial state(q0) = A
Final state(F) = B
The RLG corresponding to FA is
A ⇢ bB
B ⇢ ∈/aB/bB
The above grammar is RLG, which can be written directly through FA.
The above RLG can derive strings that start with b and after that any input
symbol(i.e. ∑ ={a, b} can be accepted).
The regular language corresponding to RLG is
L= {b, ba, bb, baa, bab ,bba,bbb ..... }
If we reverse the above production of the above RLG, then we get
A ⇢ Bb
B ⇢ ∈/Ba/Bb
It derives the language that contains all the strings which end with
b.
i.e. L' = {b, bb, ab, aab, bab, abb, bbb .....}
So we can conclude that if we have FA that represents language L and if we
convert it, into RLG, which again represents
language L, but after reversing RLG we get LLG which represents language L'(i.e.
reverse of L).
For converting the RLG into LLG for language L, the following procedure
needs to be followed:
Step 1: Reverse the FA for language L
Step 2: Write the RLG for it.
Step 3: Reverse the right linear grammar.
after this we get the grammar that generates the language that
represents the LLG for the same language L.
This represents the same procedure as above for converting RLG to LLG
Explanation: First convert the LLG which represents the Language(L) to RLG,
which represents, the reversal of language L(i.e.LR) then design FA corresponding
to it(i.e. FA for Language LR ). Then reverse the FA. Then the final FA is FA for
language L).
Conversion of LLG to RLG: For example, the above grammar is taken which
represents language L(i.e. set of all strings that start with b)
The LLG for this grammar is
B ⇢ Ba/Bb/Ab
A ⇢ ∈
Step 1: Convert the LLG into FA(i.e. the conversion procedure is the same as
above)
Step 2: Reverse the FA(i.e. initial state is converted into final state and convert
final state to initial state and reverse all edges)
Theorem:
If G is a regular grammar then L (G) is a regular language.
Proof:
The regular languages are recognized by finite automata. So, first of all we will
construct a NFA equivalent to given right linear grammar which accepts the
language defined by the given regular grammar G.
Let G = (V, T, P, S) be a right linear grammar. Let V = {A0A1…..An}, where A0
is the start symbol S. We define an NFA N = ({q0q1….qnqf}, ?, ?, q0,
qf}) where ? is defined as:
• For each production Ai -> bAj. In this N has transition from qi to qj with
label b.
• For each production Ak -> b. In this N has transition from qk to qf with
label b.
From the construction, it is clear that A0 => b1A1 => b1b2A2 => b1b2b3A3 =>
…..=>b1bn-1 => b1….bn, if and only if there is a path in N starting from initial
state q0 and ends on final state qf with path value b1b2….bn.
Therefore L (G) = T (N).
Example:
Let G = (V, T, P, S) be a regular grammar, where
V = {A0, A1, A2}
T = {0, 1}
S is the start symbol of the grammar.
P is the set of production rules defined as:
A0 -> 0A1
A0 -> 1A2
A1 -> 0A2
A2 -> 0
Construct a finite-automata that accepts the language generated by a given
grammar G.
Solution:
Let M = (Q, ?, q0, F, ?) be a finite-automata that accepts L (G), where
Q = {q0, q1, q2, qf}
? = {0, 1}
q0 is the initial state
F = {qf}
The states q0, q1, q2 corresponds to A0, A1, A2, and qf is the new final state of
M.
Initially we have 4 states of finite automata M.
The production rule A0 -> 0A1 includes a transition from q0 to q1 with label 0.
After this production rule, we have following partial diagram of finite automata.
The production rule A0 -> 1A2 includes a transition from q0 to q2 with label 1.
After this production rule we have following partial diagram of finite automata.
The production rule A1 -> 0A2 includes a transition from q1 to q2 with label 0.
After this production rule we have following partial diagram of finite automata.
Similarly, for the production rule A2 -> 0includes a transition from q2 to qf with
label 0. After this production rule we have following final diagram of finite
automata accepting L (G).
• Union
• Intersection
• concatenation
• Kleene closure
• Complement
Let see one by one with an example
Union
If L1 and If L2 are two regular languages, their union L1 U L2 will also be regular.
Example
L1 = {an | n > O} and L2 = {bn | n > O}
L3 = L1 U L2 = {an U bn | n > O} is also regular.
Intersection
If L1 and If L2 are two regular languages, their intersection L1 ∩ L2 will also be
regular.
Example
L1= {am bn | n > 0 and m > O} and
L2= {am bn U bn am | n > 0 and m > O}
L3 = L1 ∩ L2 = {am bn | n > 0 and m > O} are also regular.
Concatenation
If L1 and If L2 are two regular languages, their concatenation L1.L2 will also be
regular.
Example
L1 = {an | n > 0} and L2 = {bn | n > O}
L3 = L1.L2 = {am . bn | m > 0 and n > O} is also regular.
Kleene Closure
If L1 is a regular language, its Kleene closure L1* will also be regular.
Example
L1 = (a U b )
L1* = (a U b)*
Complement
If L(G) is a regular language, its complement L'(G) will also be regular. Complement
of a language can be found by subtracting strings which are in L(G) from all possible
strings.
Example
L(G) = {an | n > 3} L'(G) = {an | n <= 3}
Note − Two regular expressions are equivalent, if languages generated by them are
the same. For example, (a+b*)* and (a+b)* generate the same language. Every string
which is generated by (a+b*)* is also generated by (a+b)* and vice versa.
Note: q3 denotes
the dead state.
Once you enter
q3, you remain in it
forever.
L' denotes the language that does not contain strings that begin and end with a. This implies L'
contains strings that
The DFA for L' is obtained by flipping the final states of DFA(L) to non-final states and vice-
versa. The DFA for L' is given below.
• q0 ensures ε is
accepted
Important Note: While specifying the DFA for L, we have also included the dead state q3. It is
important to include the dead state(s) if we are going to derive the complement DFA since, the
dead state(s) too would become final in the complementation. If we didn't add the dead state(s)
originally, the complement will not accept all strings supposed to be accepted.
In the above example, if we didn't include q3 originally, the complement will not accept strings
starting with b. It will only accept strings that begin with a and end with b which is only a subset
of the complement.
CONCLUSION: REGULAR LANGUAGES ARE CLOSED UNDER COMPLEMENTATION.
2. Union
If L1 and L2 are regular, then L1 ∪ L2 is regular.
This is easier proved using regular expressions. If L1 is regular, there exists a regular
expression R1 to describe it. Similarly, if L2 is regular, there exists a regular expression R2 to
describe it. R1 + R2 denotes the regular expression that describe L1 ∪ L2. Therefore, L1 ∪ L2 is
regular.
This again can be shown using an example. If L1 is a language that contains strings that begin
with a and L2 is a language that contain strings that end with a, then L1 ∪ L2 denotes the
language the contain strings that either begin with a or end with a.
- a(a+b)* is the regular expression that denotes L1.
- (a+b)*a is the regular expression that denotes L2.
- L1 ∪ L2 is denoted by the regular expression a(a+b)* + (a+b)*a. Therefore, L1 ∪ L2 is
regular.
In terms of DFA, we can say that a DFA(L1 ∪ L2) accepts those strings that are accepted by
either DFA(L1) or DFA(L2) or both.
• DFA(L1 ∪ L2) can be constructed by adding a new start state and new final state.
• The new start state connects to the two start states of DFA(L1) and DFA(L2) by
εtransitions.
• Similarly, two ε transitions are added from the final states of DFA(L1) and DFA(L2) to the
new final state.
• Convert this resulting NFA to its equivalent DFA.
As an exercise you can try this approach of DFA construction for union for the given example.
CONCLUSION: REGULAR LANGUAGES ARE CLOSED UNDER UNION.
3. Intersection
If L1 and L2 are regular, then L1 ∩ L2 is regular.
Since a language denotes a set of (possibly infinite) strings and we have shown above that
regular languages are closed under union and complementation, by De Morgan's law can be
applied to show that regular languages are closed under intersection too.
L1 and L2 are regular ⇒ L1' and L2' are regular (by Complementation property)
L1' ∪ L2' is regular (by Union property)
L1 ∩ L2 is regular (by De Morgan's law)
In terms of DFA, we can say that a DFA(L1 ∩ L2) accepts those strings that are accepted by
both DFA(L1) and DFA(L2).
CONCLUSION: REGULAR LANGUAGES ARE CLOSED UNDER INTERSECTION.
4. Concatenation
If L1 and L2 are regular, then L1 . L2 is regular.
This can be easily proved by regular expressions. If R1 is a regular expression denoting L1 and
R2 is a regular expression denoting L2, then we R1 . R2 denotes the regular expression
denoting L1 . L2. Therefore, L1 . L2 is regular.
In terms of DFA, we can say that a DFA(L1 . L2) can be constructed by adding an ε-trainstion
from the final state of DFA(L1) - which now ceases to be the final state - to the start state of
DFA(L2). You can try showing this using an example.
CONCLUSION: REGULAR LANGUAGES ARE CLOSED UNDER CONCATENATION.
5. Kleene star
If L is regular, then L* is regular.
This can be easily proved by regular expression. If L is regular, then there exists a regular
expression R. We know that if R is a regular expression, R* is a regular expression too. R*
denotes the language L*. Therefore L* is regular.
In terms of DFA, in the DFA(L) we add two ε transitions, one from start state to final state and
another from final state to start state. This denotes DFA(L*). You can try showing this for an
example.
CONCLUSION: REGULAR LANGUAGES ARE CLOSED UNDER KLEENE STAR.
6. Difference
If L1 and L2 are regular, then L1 - L2 is regular.
We know that L1 - L2 = L1 ∩ L2'
L1 and L2 are regular ⇒ L1 and L2' are regular (by Complementation property)
L1 ∩ L2' is regular (by Intersection property)
L1 - L2 is regular (by De Morgan's law)
In terms of DFA, we can say that a DFA(L1 - L2) accepts those strings that are accepted by both
DFA(L1) and not accepted by DFA(L2). You can try showing this for an example.
CONCLUSION: REGULAR LANGUAGES ARE CLOSED UNDER DIFFERENCE.
7. Reverse
If L is regular, then LR is regular.
Let DFA(L) denote the DFA of L. Make the following modifications to construct DFA(LR).
In case there are more than one final state in DFA(L), first add a new final state and
add ε- transitions from the final states (which now cease to be final states any more)
and perform this step.
• Construct DFA(L).
• Run w on DFA(L).
• If DFA(L) accepts w, then w ∈ L. Else w ∉ L.
2. Emptiness question
Is L = φ?
This can be validated as follows.
• Construct DFA(L).
• If there exists no path from start state to final state, L = φ. Else L ≠ φ.
3. Equivalence question
Is L1 = L2?
This can be validated as follows.
• Construct DFA(L1) and DFA(L2).
• Reduce them to their minimal DFAs: MinDFA(L1) and MinDFA(L2).
• If MinDFA(L1) = MinDFA(L2) then L1 = L2. Else L1 ≠ L2.
4. Subset question
Is L1 ⊂ L2?
This can be validated as follows.
5. Infinite question
Is L infinite?
This can be validated as follows.
• Construct DFA(L).
• If DFA(L) has at least one loop, then L is infinite. Else L finite.
There are two Pumping Lemmas (PL), which are defined for Regular Languages and
Context - Free Languages.
Theorem
For any regular language L, there exists an integer P, such that for all w in L
|w|>=P
We can break w into three strings, w=xyz such that.
(1)lxyl < P
(2)lyl > 1
(3)for all k>= 0: the string xykz is also in L
Introduction
The language accepted by Finite Automata is known as Regular Language.
Pumping Lemma is used to prove that a Language is not Regular. It cannot be used
to prove that a language is Regular.
The term Pumping Lemma is made up of two words:-
Pumping: The word pumping refers to generating many input strings by pushing a
symbol in an input string repeatedly.
Lemma: The word Lemma refers to the intermediate theorem in a proof.
There are two Pumping Lemmas, that are defined for
• Regular Languages
• Context-Free Languages
Let’s now learn about Pumping Lemma for Regular Languages in-depth.
Pumping Lemma For Regular Languages
Theorem: If A is a Regular Language, then A has a Pumping Length ‘P’ such that
any string ‘S’ where |S ≥ P may be divided into three parts S = xyz such that the
following conditions must be true:
1.) xyiz ∈ A for every i ≥ 0
2.) |y| > 0
3.) |xy| ≤ P
In simple words, if a string y is ‘pumped’ or insert any number of times, the resultant
string still remains in A.
Pumping Lemma is used as proof of the irregularity of a language. It means, that if
a language is regular, it always satisfies the pumping lemma. If at least one string is
made from pumping, not in language A, then A is not regular.
We use the CONTRADICTION method to prove that a language is not Regular.
To prove that a language is not Regular using Pumping Lemma, follow the below
steps:
Step 1: Assume that Language A is Regular.
Step 2: It has to have a Pumping Length (say P).
Step 3: All strings longer than P can be pumped |S| ≥ P.
Step 4: Now, find a string ‘S’ in A such that |S| ≥ P.
Step 5: Divide S into x y z strings.
Step 6: Show that xyiz ∉ A for some i.
Step 7: Then consider how S can be divided into x y z.
Step 8: Show that none of the above strings satisfies all three pumping conditions
simultaneously.
Step 9: S cannot be pumped == CONTRADICTION.
Let’s apply these above steps to check whether a Language is not Regular with the
help of Pumping Lemma.
Implementation of Pumping lemma for regular languages
Example: Using Pumping Lemma, prove that the language A = {anbn | n≥0} is Not
Regular.
Solution: We will follow the steps we have learned above to prove this.
Assume that A is Regular and has a Pumping length = P.
Let a string S = apbp.
Now divide the S into the parts, x y z.
To divide the S, let’s take the value of P = 7.
Therefore, S = aaaaaaabbbbbbb (by putting P=7 in S = apbp).
Case 1: Y consists of a string having the letter only ‘a’.
For all the above cases, we need to show xyiz ∉ A for some i.
Let the value of i = 2. xyiz => xy2z
In Case 1. xy2z = aa aaaa aaaa abbbbbbb
No of ‘a’ = 11, No. of ‘b’ = 7.
Since the No of ‘a’ != No. of ‘b’, but the original language has an equal number of
‘a’ and ‘b’; therefore, this string will not lie in our language.
In Case 2. xy2z = aaaaaaabb bbbb bbbb b
No of ‘a’ = 7, No. of ‘b’ = 11.
Since the No of ‘a’ != No. of ‘b’, but the original language has an equal number of
‘a’ and ‘b’; therefore, this string will not lie in our language.
In Case 3. xy2z = aaaa aabb aabb bbbbb
No of ‘a’ = 8, No. of ‘b’ = 9.
Since the No of ‘a’ != No. of ‘b’, but the original language has an equal number of
‘a’ and ‘b’, and also, this string did not follow the anbn pattern; therefore, this string will
not lie in our language.
We can see at i = 2 all the above three strings do not lie in the language A = {anbn |
n≥0}.
Therefore, the language A = {anbn | n≥0} is not Regular.
Also see, Turing Machine in TOC.
Pumping Lemma for Context-free Languages
The Bar-Hillel lemma, also referred to as the pumping lemma for context-free
languages, is a generalisation of the pumping lemma for regular languages in formal
language theory and computer science. It creates a characteristic that all context-
free languages share.
The pumping lemma can be used to show by contradiction that a particular language
is not context-free. In contrast, there are other conditions that must be met in order to
guarantee that a language is context-free, such as Ogden's lemma or the
Interchange lemma.
Implementation of Pumping lemma for Context-free Languages
Suppose a language L is context free, then there will exist any integer p≥1, also
called pumping length such that every string S in the context free language L which
has a length of p or more can be written as :
s = uvwxy
with the substrings following the following property:
1.) uvnwxny∈ L for every n ≥ 0
2.) |vx| ≥ 1
3.) |vwx| ≤ P
Applications of Pumping Lemma
Applying the Pumping Lemma will demonstrate that some languages are irregular.
Never utilise a language to demonstrate its regularity.
• Pumping Lemma is satisfied if L is regular.
• L is not regular if it does not satisfy the Pumping Lemma.
Frequently Asked Questions
What do you mean by pumping lemma?
The term Pumping Lemma is made up of two words:-
Pumping: The word pumping refers to generating many input strings by pushing a
symbol in an input string repeatedly.
Lemma: The word Lemma refers to the intermediate theorem in a proof.
What is pumping lemma and its application?
Applying the Pumping Lemma will demonstrate that some languages are irregular.
Never utilise a language to demonstrate its regularity.
• Pumping Lemma is satisfied if L is a regular language.
• L is not regular if it does not satisfy the Pumping Lemma.
What do you mean by pumping Lemma for regular language?
If A is a Regular Language, then A has a Pumping Length ‘P’ such that any string ‘S’
where |S ≥ P may be divided into three parts S = xyz such that the following
conditions must be true:
1.) xyiz ∈ A for every i ≥ 0
2.) |y| > 0
3.) |xy| ≤ P
What do you mean by pumping Lemma for context free
languages?
Suppose a language L is context free, then there will exist any integer p≥1, also
called pumping length such that every string S in the context free language L which
has a length of p or more can be written as :
s = uvwxy
The properties are:
1.) uvnwxny∈ L for every n ≥ 0
2.) |vx| ≥ 1
3.) |vwx| ≤ P
What is pumping lemma in finite automata?
Applying the Pumping Lemma will demonstrate that some languages are irregular.
Never utilise a language to demonstrate its regularity. Pumping Lemma is satisfied if
L is a regular language. L is not regular if it does not satisfy the Pumping Lemma.
What are the conditions of the pumping lemma?
Suppose a language L is context free, then there will exist any integer p≥1, also
called pumping length such that every string S in the context free language L which
has a length of p or more can be written as :
s = uvwxy
The conditions for pumping lemma are as follows:
1.) uvnwxny∈ L for every n ≥ 0
2.) |vx| ≥ 1
3.) |vwx| ≤ P
Algorithm
Input − DFA
Output − Minimized DFA
Step 1 − Draw a table for all pairs of states (Qi, Qj) not necessarily connected directly
[All are unmarked initially]
Step 2 − Consider every state pair (Qi, Qj) in the DFA where Qi ∈ F and Qj ∉ F or vice
versa and mark them. [Here F is the set of final states]
Step 3 − Repeat this step until we cannot mark anymore states −
If there is an unmarked pair (Qi, Qj), mark it if the pair {δ (Qi, A), δ (Qi, A)} is marked for
some input alphabet.
Step 4 − Combine all the unmarked pair (Qi, Qj) and make them a single state in the
reduced DFA.
Example
Let us use Algorithm 2 to minimize the DFA shown below.
a b c d e f
c ✔ ✔
d ✔ ✔
e ✔ ✔
f ✔ ✔ ✔
Step 3 − We will try to mark the state pairs, with green colored check mark,
transitively. If we input 1 to state ‘a’ and ‘f’, it will go to state ‘c’ and ‘f’ respectively.
(c, f) is already marked, hence we will mark pair (a, f). Now, we input 1 to state ‘b’
and ‘f’; it will go to state ‘d’ and ‘f’ respectively. (d, f) is already marked, hence we will
mark pair (b, f).
a b c d e f
c ✔ ✔
d ✔ ✔
e ✔ ✔
f ✔ ✔ ✔ ✔ ✔
After step 3, we have got state combinations {a, b} {c, d} {c, e} {d, e} that are
unmarked.
We can recombine {c, d} {c, e} {d, e} into {c, d, e}
Hence we got two combined states as − {a, b} and {c, d, e}
So the final minimized DFA will contain three states {f}, {a, b} and {c, d, e}
Algorithm 3
Step 1 − All the states Q are divided in two partitions − final states and non-final
states and are denoted by P0. All the states in a partition are 0th equivalent. Take a
counter k and initialize it with 0.
Step 2 − Increment k by 1. For each partition in Pk, divide the states in Pk into two
partitions if they are k-distinguishable. Two states within this partition X and Y are k-
distinguishable if there is an input S such that δ(X, S) and δ(Y, S) are (k-1)-
distinguishable.
Step 3 − If Pk ≠ Pk-1, repeat Step 2, otherwise go to Step 4.
Step 4 − Combine kth equivalent sets and make them the new states of the reduced
DFA.
Example
Let us consider the following DFA −
q δ(q,0) δ(q,1)
a b c
b a d
c e f
d e f
e e f
f f f
• P0 = {(c,d,e), (a,b,f)}
• P1 = {(c,d,e), (a,b),(f)}
• P2 = {(c,d,e), (a,b),(f)}
Hence, P1 = P2.
There are three states in the reduced DFA. The reduced DFA is as follows −
Q δ(q,0) δ(q,1)
• Unreachable state
• Dead State
In the above DFA, we have unreachable state E, because on any input from
the initial state, we are unable to reach to that state. This state is useless in
finite automata. So, the best solution is to eliminate these types of states to
minimize the finite automata.
Dead State: It is a non-accepting state, which goes itself for every possible
input symbol.
In the above DFA, we have q5, and q6 are dead states because every
possible input symbol goes to itself.
Minimization of Deterministic Finite Automata
The following steps are used to minimize a Deterministic Finite Automata.
Step1: First of all, we detect the unreachable state.
Step2: After detecting the unreachable state, in the second step, we
eliminate the unreachable state (if found).
Step3: Identify the equivalent states and merge them.
This step is repeated for every group. Find group the input lead to if there are
differences the partition the group into sets containing states which go to the
same groups under the inputs.
• The resulting final partition contains equivalent states now merge them
into a single state.
T = {q0}
• Now, for all states in temporary state set T, find transition from each
state on each input symbol in ?. If resulting state is not in T add that
state in T.
? (q0, a) = q1
? ( q0, b) = q2
Again
? ( q1, a) = q3
? ( q1, b) = q4
Again
? ( q2, a) = q3
? ( q2, b) = q5
Again
? ( q3, a) = q3
? ( q3, b) = q1
Again
? ( q4, a) = q4
? ( q4, b) = q5
Again
? ( q5, a) = q5
? ( q5, b) = q4
U = Q – T
U = {q0, q1, q2, q3, q4, q5, q6} – {q0, q1, q2, q3, q4, q5}
? (q5, a) = q5
Check Group A2 for input b
? (q4, b) = q5
? (q5, b) = q4
As both belong to the same group, the further division is not possible.
Now, we check Group B2 for input a and b
?(q1, a) = q3
?(q2, a) = q3
?(q1, b) = q4
?(q2, b) = q5
q4 and q5 belong to group A2 for input b. So no further partitioning is possible.
Finally, the following groups are formed:
Group A1 – q3
Group A2 – q4, q5
Group B1 – q0
Group B2 – q1, q2
The resulting automata is given below:
Step4: In this step, we detected dead states. There are no dead states in the
above DFA; hence it is minimized.
-: Module 3:-
-:Context-sensitive languages :-(context-sensitive grammars and
context-sensitive languages.)
The Context sensitive grammar (CSG) is defined as G=(V,Σ,P,S)
Where,
Example
• aS→SAa|aA
• aA→abc
In context sensitive grammar, there is either left context or right context (αAβ i.e. α
is left context and β is right) with variables.
But in context free grammar (CFG) there will be no context.
For example in production rule
S →0 B S 2 ,
B0→0B
We cannot replace B until we get B0.
Therefore, CSG is harder to understand than the CFG.
The CFG, CSG and the unrestricted grammar are depicted below −
Context-sensitive Grammar (CSG) and Language (CSL)
Context-Sensitive Grammar –
A Context-sensitive grammar is an Unrestricted grammar in which all the
productions are of form –
Previous Page
Next Page
where,
Q -> A finite set of transition states
T -> Tape alphabet
E -> Input alphabet
q0 -> Initial state
ML -> Left bound of tape
MR -> Right bound of tape
S -> Transition Function
F -> A finite set of final states
Diagrammatic Representation of LBA :
Examples:
Languages that form LBA with tape as shown above,
• L = {an! | n >= 0}
• L = {wn | w from {a, b}+, n >= 1}
• L = {wwwR | w from {a, b}+}
Facts :
Suppose that a given LBA M has
--> q states,
--> m characters within the tape alphabet, and
--> the input length is n
1. Then M can be in at most f(n) = q * n * mn configurations i.e. a tape of n
cells and m symbols, we are able to have solely mn totally different
tapes.
2. The tape head is typically on any of the n cells which we have a
tendency to are typically death penalty in any of the q states.
-: Module 5:-
Undecidability :-
Proof
A proof by the Church-Turing thesis is a shortcut often taken in establishing the
existence of a decision algorithm.
For any decision problem, rather than constructing a Turing machine solution, let us
describe an effective procedure which solves the problem.
The Church-Turing thesis explains that a decision problem Q has a solution if and
only if there is a Turing machine that determines the answer for every q ϵ Q. If no
such Turing machine exists, the problem is said to be undecidable.
Church-Turing Thesis
The Church-Turing thesis (formerly commonly known simply as Church's thesis) says that
any real-world computation can be translated into an equivalent computation involving
a Turing machine. In Church's original formulation (Church 1935, 1936), the thesis says
that real-world calculation can be done using the lambda calculus, which is equivalent to
using general recursive functions.
The Church-Turing thesis encompasses more kinds of computations than those originally
envisioned, such as those involving cellular automata, combinators, register machines,
and substitution systems. It also applies to other kinds of computations found in
theoretical computer science such as quantum computing and probabilistic computing.
There are conflicting points of view about the Church-Turing thesis. One says that it can be
proven, and the other says that it serves as a definition for computation. There has never
been a proof, but the evidence for its validity comes from the fact that every realistic
model of computation, yet discovered, has been shown to be equivalent. If there were a
device which could answer questions beyond those that a Turing machine can answer,
then it would be called an oracle.
Some computational models are more efficient, in terms of computation time and
memory, for different tasks. For example, it is suspected that quantum computers can
perform many common tasks with lower time complexity, compared to modern
computers, in the sense that for large enough versions of these problems, a quantum
computer would solve the problem faster than an ordinary computer. In contrast, there
exist questions, such as the halting problem, which an ordinary computer cannot answer,
and according to the Church-Turing thesis, no other computational device can answer
such a question.
The Church-Turing thesis has been extended to a proposition about the processes in the
natural world by Stephen Wolfram in his principle of computational equivalence (Wolfram
2002), which also claims that there are only a small number of intermediate levels of
computing power before a system is universal and that most natural systems are
universal.
The Turing Machine (TM) is the machine level equivalent to a digital computer.
It was suggested by the mathematician Turing in the year 1930 and has become the
most widely used model of computation in computability and complexity theory.
The model consists of an input and output. The input is given in binary format form
on to the machine’s tape and the output consists of the contents of the tape when
the machine halts
The problem with the Turing machine is that a different machine must be constructed
for every new computation to be performed for every input output relation.
This is the reason the Universal Turing machine was introduced which along with
input on the tape takes the description of a machine M.
The Universal Turing machine can go on then to simulate M on the rest of the content
of the input tape.
A Universal Turing machine can thus simulate any other machine.
The idea of connecting multiple Turing machine gave an idea to Turing −
• Can a Universal machine be created that can ‘simulate’ other machines?
• This machine is called as Universal Turing Machine
This machine would have three bits of information for the machine it is simulating
The UTM can then simulate M on the rest of the input tape's content. As a result, a
Universal Turing Machine can simulate any other machine.
Creating a general-purpose Turing Machine(UTM) is a more difficult task. Once the
Turing machine's transition is defined, the machine is restricted to performing a
specific type of computation.
We can create a universal Turing machine by modifying our fundamental Turing
machine model. For even simple behavior to be stimulated, the modified Turing
computer must have a huge number of states. We modify our basic model by doing
the following:
• Increase the number of read/write heads.
• Increase the number of input tape dimensions.
• Increasing memory space.
The UTM would include three pieces of data for the machine it is simulating:
• A basic description of the machine.
• The contents of the machine tape.
• The internal state of the machine.
The Universal machine would simulate the machine by checking the tape input and
the machine's state.
It would command the machine by modifying its state in response to the input. This
will be like a computer running another computer.
The schematic diagram of a Universal Turing Machine is as follows:
Formal Definition
If P is a non-trivial property, and the language holding the property, Lp , is recognized
by Turing machine M, then Lp = {<M> | L(M) ∈ P} is undecidable.
Proof
Suppose, a property P is non-trivial and φ ∈ P.
Since, P is non-trivial, at least one language satisfies P, i.e., L(M0) ∈ P , ∋ Turing
Machine M0.
Let, w be an input in a particular instant and N is a Turing Machine which follows −
On input x
• Run M on w
• If M does not accept (or doesn't halt), then do not accept x (or do not
halt)
• If M accepts w then run M0 on x. If M0 accepts x, then accept x.
A function that maps an instance ATM = {<M,w>| M accepts input w} to a N such that
Example
The problems for which we can’t construct an algorithm that can answer the problem
correctly in the infinite time are termed as Undecidable Problems in the theory of
computation (TOC).
A problem is undecidable if there is no Turing machine that will always halt an
infinite amount of time to answer as ‘yes’ or ‘no’.
Examples
The examples of undecidable problems are explained below. Here, CFG refers to
Context Free Grammar.
• Whether two CFG L and M equal − Since, we cannot determine all the
strings of any CFG, we can predict that two CFG are equal or not.
• Given a context-free language, there is no Turing machine (TM) that will
always halt an infinite amount of time and give an answer to whether
language is ambiguous or not.
• Given two context-free languages, there is no Turing machine that will
always halt an infinite amount of time and give an answer whether two
context-free languages are equal or not.
• Whether CFG will generate all possible strings of the input alphabet
(∑*) is undecidable.
Halting Problem
The Halting problem is the most famous of the undecidable problems.
Consider the code
num=1;
while(num=0)
{
num=num+1;
}
It counts up forever since it will never equal 0. This is an example of the halting
problem.
Note: Every context-free language is decidable.
Some of the other undecidable problems are:
Totality problem − It decide whether an arbitrary TM halts on all inputs. This is
equivalent to the problem of whether a program can ever enter an infinite loop, for
any input. It differs from the halting problem, which asks whether it enters an infinite
loop for a particular input.
Equivalence problem − It decide whether two TMs accept the same language. This
is equivalent to the problem of whether two programs compute the same output for
every input.
-: MODULE 4 :-
Turning Machines :-( The basic model for turning machines )
Definition
A Turing Machine (TM) is a mathematical model which consists of an infinite length
tape divided into cells on which input is given. It consists of a head which reads the
input tape. A state register stores the state of the Turing machine. After reading an
input symbol, it is replaced with another symbol, its internal state is changed, and it
moves from one cell to the right or left. If the TM reaches the final state, the input
string is accepted, otherwise rejected.
A TM can be formally described as a 7-tuple (Q, X, ∑, δ, q0, B, F) where −
• Q is a finite set of states
• X is the tape alphabet
• ∑ is the input alphabet
• δ is a transition function; δ : Q × X → Q × X × {Left_shift, Right_shift}.
• q0 is the initial state
• B is the blank symbol
• F is the set of final states
Comparison with the previous automaton
The following table shows a comparison of how a Turing machine differs from Finite
Automaton and Pushdown Automaton.
Tape alphabet symbol Present State ‘q0’ Present State ‘q1’ Present State ‘q2’
Here the transition 1Rq1 implies that the write symbol is 1, the tape moves right, and
the next state is q1. Similarly, the transition 1Lq2 implies that the write symbol is 1,
the tape moves left, and the next state is q2.
Turing Machine was invented by Alan Turing in 1936 and it is used to accept
Recursive Enumerable Languages (generated by Type-0 Grammar).
Turing machines are a fundamental concept in the theory of computation and
play an important role in the field of computer science. They were first
described by the mathematician and computer scientist Alan Turing in 1936
and provide a mathematical model of a simple abstract computer.
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.
The Turing machine’s behavior is determined by a finite state machine,
which consists of a finite set of states, a transition function that defines the
actions to be taken based on the current state and the symbol being read,
and a set of start and accept states. The Turing machine begins in the start
state and performs the actions specified by the transition function until it
reaches an accept or reject state. If it reaches an accept state, the
computation is considered successful; if it reaches a reject state, the
computation is considered unsuccessful.
Turing machines are an important tool for studying the limits of computation
and for understanding the foundations of computer science. They provide a
simple yet powerful model of computation that has been widely used in
research and has had a profound impact on our understanding of algorithms
and computation.
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.
Illustration
Let us see how this turing machine works for 0011. Initially head points to 0
which is underlined and state is q0 as:
The move will be δ(q0, 0) = (q1, X, R). It means, it will go to state q1, replace
0 by X and head will move to right as:
The move will be δ(q1, 0) = (q1, 0, R) which means it will remain in same
state and without changing any symbol, it will move to right as:
The move will be δ(q1, 1) = (q2, Y, L) which means it will move to q2 state
and changing 1 to Y, it will move to left as:
Working on it in the same way, the machine will reach state q3 and head will
point to B as shown:
Using δ(q0, 1) = (q1, 1, R), it will move to state q1 and head will move to
right as:
Using δ(q1, B) = (q0, B, L), it will move to state q0 and head will move to left
as:
It will run in the same way again and again and not halt.
Option D says M halts on all string ending with 1, but it is not halting for 1.
So, option D is incorrect.
Let us see whether machine halts on string ‘0’. Initially state will be q0, head
will point to 1 as:
Using δ(q0, 0) = (q1, 1, R), it will move to state q1 and head will move to
right as:
Using δ(q1,B)=(q0,B,L), it will move to state q0 and head will move to left
as:
It will run in the same way again and again and not halt.
Option C says M halts on all string ending with 0, but it is not halting for 0.
So, option C is incorrect.
Option B says that TM does not halt for any string (00 + 1)*. But NULL string
is a part of (00 + 1)* and TM will halt for NULL string. For NULL string, tape
will be,
Using δ(q0, B) = halt, TM will halt. As TM is halting for NULL, this option is
also incorrect.
So, option (A) is correct.
This article is contributed by Sonal Tuteja. Please write comments if you find
anything incorrect, or you want to share more information about the topic
discussed above
L1 says n no. of a’s followed by n no. of b’s followed by n no. of c’s and then any no.
of d’s. L2 says any no. of a’s followed by n no. of b’s followed by n no. of c’s followed
by n no. of d’s. Their intersection says n no. of a’s followed by n no. of b’s followed
by n no. of c’s followed by n no. of d’s. So it can be decided by turing machine, hence
recursive.
Similarly, complement of recursive language L1 which is ∑*-L1, will also be
recursive.
Note: As opposed to REC languages, RE languages are not closed under
complementation which means complement of RE language need not be RE.
GATE Questions
Question 1: Which of the following statements is/are FALSE?
1.For every non-deterministic TM, there exists an equivalent deterministic TM.
2.Turing recognizable languages are closed under union and complementation.
3.Turing decidable languages are closed under intersection and complementation.
4.Turing recognizable languages are closed under union and intersection.
A.1 and 4
B.1 and 3
C.2
D.3
Solution:
Statement 1 is true as we can convert every non-deterministic TM to deterministic
TM.
Statement 2 is false as Turing recognizable languages (RE languages) are not
closed under complementation.
Statement 3 is true as Turing decidable languages (REC languages) are closed
under intersection and complementation.
Statement 4 is true as Turing recognizable languages (RE languages) are closed
under union and intersection.
Question 2 : Let L be a language and L’ be its complement. Which one of the
following is NOT a viable possibility?
A.Neither L nor L’ is RE.
B.One of L and L’ is RE but not recursive; the other is not RE.
C.Both L and L’ are RE but not recursive.
D.Both L and L’ are recursive.
Solution:
Option A is correct because if L is not RE, its complementation will not be RE.
Option B is correct because if L is RE, L’ need not be RE or vice versa because RE
languages are not closed under complementation.
Option C is false because if L is RE, L’ will not be RE. But if L is recursive, L’ will also
be recursive and both will be RE as well because REC languages are subset of RE.
As they have mentioned not to be REC, so option is false.
Option D is correct because if L is recursive L’ will also be recursive.
Question 3: Let L1 be a recursive language, and let L2 be a recursively enumerable
but not a recursive language. Which one of the following is TRUE?
A.L1′ is recursive and L2′ is recursively enumerable
B.L1′ is recursive and L2′ is not recursively enumerable
C.L1′ and L2′ are recursively enumerable
D.L1′ is recursively enumerable and L2′ is recursive
Solution:
Option A is False as L2’ can’t be recursive enumerable (L2 is RE and RE are not
closed under complementation).
Option B is correct as L1’ is REC (REC languages are closed under
complementation) and L2’ is not recursive enumerable (RE languages are not
closed under complementation).
Option C is False as L2’ can’t be recursive enumerable (L2 is RE and RE are not
closed under complementation).
Option D is False as L2’ can’t be recursive enumerable (L2 is RE and RE languages
are not closed under complementation). As REC languages are subset of RE, L2’
can’t be REC as well.
This article is contributed by Sonal Tuteja. Please write comments if you find
anything incorrect, or you want to share more information about the topic
discussed above
Let us understand the concept of recursive language before learning about the
recursively enumerable language in the theory of computation (TOC).
Recursive Language
A language L is recursive (decidable) if L is the set of strings accepted by some Turing
Machine (TM) that halts on every input.
Example
When a Turing machine reaches a final state, it halts. We can also say that a Turing
machine M halts when M reaches a state q and a current symbol ‘a’ to be scanned
so that δ(q, a) is undefined.
There are TMs that never halt on some inputs in any one of these ways, So we make
a distinction between the languages accepted by a TM that halts on all input strings
and a TM that never halts on some input strings.
Recursive Enumerable Language
A language L is recursively enumerable if L is the set of strings accepted by some
TM.
If L is a recursive enumerable language then −
If w ∈ L then a TM halts in a final state,
If w ∉ L then a TM halts in a non-final state or loops forever.
If L is a recursive language then −
If w ∈ L then a TM halts in a final state,
If w ∉ L then TM halts in a non-final state.
Recursive Languages are also recursive enumerable
Proof − If L is a recursive then there is TM which decides a member in language then
−
• M accepts x if x is in language L.
• M rejects on x if x is not in language L.
According to the definition, M can recognize the strings in language that are accepted
on those strings.
Halting Problem
If turing machine answers YES for string belonging to language.
But for string not belonging to language Turing Machine can either answer NO or it could go
into infinite loop.
Then this is called Halting problem and such a language is called Recursie Enumerable
Language.
Below picture will put more light on the explanation:
In above pic the outer rectangle is Σ*(all possible strings over given input) and langauge is a
subset of it.
Union Yes
Intersection Yes
Set Difference No
Compliment No
Concatination Yes
Reversal Yes
Homomorphism Yes
Substitution Yes
• A k-track Turing machine(for some k>0) has k-tracks and one R/W
head that reads and writes all of them one by one.
• A k-track Turing Machine can be simulated by a single track Turing
machine
2. Two-way infinite Tape Turing Machine:
Turing machines (TM) can also be deterministic or non-deterministic, but this does
not make them any more or less powerful.
However, if the tape is restricted so that you can only see use of the part of the tape
with the input, the TM becomes less powerful (linear bounded automata) and can
only recognise context sensitive languages.
Many other TM variations are equivalent to the original TM. This includes the
following −
• Multi-track
• Multi-tape
• Multi-head
• Multi-dimensional tape
• The off-line Turing machine
Multi-tape Turing Machine
A Turing machine with several tapes we call it a multi tape Turing machine.
Every tape’s have their own Read/Write head
For N-tape Turing Machine
M={( Q,X, ∑,δ,q0,B,F)}
We define
δ=QxXN ->Q x XN x {L,R}N
Example
If n=2 with current configuration δ(q0,a,e)=(q1,X,Y,L,R)
δ=QxXN ->Q x XN x {L,R}N
Non Deterministic Turing Machine
It is similar to DTM except that for any input and current state it has a number of
choices.
A string is accepted by a NDTM if there is a sequence of moves that leads to a final
state
The Transition function −
=Q x X ->2QxXx(L,R)
A NDTM is allowed to have more than one transition for a given tape symbol.
Previous Page
Next Page
In a Non-Deterministic Turing Machine, for every state and symbol, there are a group
of actions the TM can have. So, here the transitions are not deterministic. The
computation of a non-deterministic Turing Machine is a tree of configurations that
can be reached from the start configuration.
An input is accepted if there is at least one node of the tree which is an accept
configuration, otherwise it is not accepted. If all branches of the computational tree
halt on all inputs, the non-deterministic Turing Machine is called a Decider and if for
some input, all branches are rejected, the input is also rejected.
A non-deterministic Turing machine can be formally defined as a 6-tuple (Q, X, ∑, δ,
q0, B, F) where −
• Q is a finite set of states
• X is the tape alphabet
• ∑ is the input alphabet
• δ is a transition function;
δ : Q × X → P(Q × X × {Left_shift, Right_shift}).
• q0 is the initial state
• B is the blank symbol
• F is the set of final states
Previous Page
Next Page
To understand class P and NP, first we should know the computational model.
Hence, in this chapter we will discuss two important computational models.
https://www.youtube.com/watch?v=GitLr3MwsFE
https://www.youtube.com/watch?v=CQNBBz_e4ss
Unrestricted grammar
In automata theory, the class of unrestricted grammars (also called semi-
Thue, type-0 or phrase structure grammars) is the most general class of
grammars in the Chomsky hierarchy. No restrictions are made on the productions of
an unrestricted grammar, other than each of their left-hand sides being non-
empty.[1]: 220 This grammar class can generate arbitrary recursively enumerable
languages.
Formal definition[edit]
An unrestricted grammar is a formal grammar , where
is the same as saying that for every unrestricted grammar there exists
generate sentential forms from . The Turing machine then does the following:
1. Start at the left of the second tape and repeatedly choose to move right or
select the current position on the tape.
forms of on its second tape after the last step is executed an arbitrary number
1.
• — • Linear context-free rewriting • Linear context-free rewriting • restricted Tree stack autom
h category of languages, except those marked by a *, is a proper subset of the category directly above it. Any language in each category is generated by a grammar a
is ( V + T )*.
In type 0 there must be at least one variable on the Left side of production.
For example:
Sab --> ba
A --> S
Here, Variables are S, A, and Terminals a, b.
Type 1: Context-Sensitive Grammar
Type-1 grammars generate context-sensitive languages. The language generated
by the grammar is recognized by the Linear Bound Automata
In Type 1
• First of all Type 1 grammar should be Type 0.
• Grammar Production in the form of
|\alpha |<=|\beta |
For Example:
S --> AB
AB --> abc
B --> b
Type 2: Context-Free Grammar: Type-2 grammars generate context-free
languages. The language generated by the grammar is recognized by a Pushdown
automata. In Type 2:
• First of all, it should be Type 1.
• The left-hand side of production can have only one variable and there is
no restriction on
|\alpha | = 1.
For example:
S --> AB
A --> a
B --> b
Type 3: Regular Grammar: Type-3 grammars generate regular languages.
These languages are exactly all languages that can be accepted by a finite-state
automaton. Type 3 is the most restricted form of grammar.
Type 3 should be in the given form only :
V --> VT / T (left-regular grammar)
(or)
V --> TV /T (right-regular grammar)
For example:
S --> a
The above form is called strictly regular grammar.
There is another form of regular grammar called extended regular grammar. In
this form:
V --> VT* / T*. (extended left-regular grammar)
(or)
V --> T*V /T* (extended right-regular grammar)
For example :
S --> ab.
Please write comments if you find anything incorrect, or if you want to share
more information about the topic discussed above.
Formal definition[edit]
An enumerator can be defined as a 2-tape Turing machine (Multitape Turing
all the tapes are blank (i.e., filled with blank symbols). Newly defined symbol is
the delimiter that marks end of an element of . The second tape can be
set of all possible strings over the input alphabet i.e. the Kleene Closure is
a countable set, we can enumerate the strings in it as etc. Then the Enumerator
1 for i = 1,2,3,...
Now the question comes whether every string in the language will be printed by
the -th step of the Enumerator will be printed. Thus the Enumerator will
print every string recognizes but a single string may be printed several times.
An Enumerable Language is Turing Recognizable
It's very easy to construct a Turing Machine that recognizes the enumerable
language . We can have two tapes. On one tape we take the input string and on
the other tape, we run the enumerator to enumerate the strings in the language one
after another. Once a string is printed in the second tape we compare it with the
input in the first tape. If its a match, then we accept the input, else reject.
MODULE 2
Context-free language and pushdown automata :-
Context free grammar and context free language
Previous Page
Next Page
Definition − A context-free grammar (CFG) consisting of a finite set of grammar rules
is a quadruple (N, T, P, S) where
• N is a set of non-terminal symbols.
• T is a set of terminals where N ∩ T = NULL.
• P is a set of rules, P: N → (N ∪ T)*, i.e., the left-hand side of the
production rule P does have any right context or left context.
• S is the start symbol.
Example
Representation Technique
• Root vertex − Must be labeled by the start symbol.
• Vertex − Labeled by a non-terminal symbol.
• Leaves − Labeled by a terminal symbol or ε.
If S → x1x2 …… xn is a production rule in a CFG, then the parse tree / derivation tree
will be as follows −
If a partial derivation tree contains the root S, it is called a sentential form. The above
sub-tree is also in sentential form.
Previous Page
Next Page
If a context free grammar G has more than one derivation tree for some string w ∈
L(G), it is called an ambiguous grammar. There exist multiple right-most or left-most
derivations for some string generated from that grammar.
Problem
Check whether the grammar G with production rules −
X → X+X | X*X |X| a
is ambiguous or not.
Solution
Let’s find out the derivation tree for the string "a+a*a". It has two leftmost derivations.
Derivation 1 − X → X+X → a +X → a+ X*X → a+a*X → a+a*a
Parse tree 1 −
Previous Page
Next Page
• Union
• Concatenation
• Kleene Star operation
Union
Let L1 and L2 be two context free languages. Then L1 ∪ L2 is also context free.
Example
Let L1 = { anbn , n > 0}. Corresponding grammar G1 will have P: S1 → aAb|ab
Let L2 = { cmdm , m ≥ 0}. Corresponding grammar G2 will have P: S2 → cBb| ε
Union of L1 and L2, L = L1 ∪ L2 = { anbn } ∪ { cmdm }
The corresponding grammar G will have the additional production S → S1 | S2
Concatenation
If L1 and L2 are context free languages, then L1L2 is also context free.
Example
Union of the languages L1 and L2, L = L1L2 = { anbncmdm }
The corresponding grammar G will have the additional production S → S1 S2
Kleene Star
If L is a context free language, then L* is also context free.
Example
Let L = { anbn , n ≥ 0}. Corresponding grammar G will have P: S → aAb| ε
Kleene Star L1 = { anbn }*
The corresponding grammar G1 will have additional productions S1 → SS1 | ε
Context-free languages are not closed under −
• Intersection − If L1 and L2 are context free languages, then L1 ∩ L2 is
not necessarily context free.
• Intersection with Regular Language − If L1 is a regular language and
L2 is a context free language, then L1 ∩ L2 is a context free language.
• Complement − If L1 is a context free language, then L1’ may not be
context free.
CFG Simplification
Previous Page
Next Page
In a CFG, it may happen that all the production rules and symbols are not needed for
the derivation of strings. Besides, there may be some null productions and unit
productions. Elimination of these productions and symbols is called simplification of
CFGs. Simplification essentially comprises of the following steps −
• Reduction of CFG
• Removal of Unit Productions
• Removal of Null Productions
Reduction of CFG
CFGs are reduced in two phases −
Phase 1 − Derivation of an equivalent grammar, G’, from the CFG, G, such that each
variable derives some terminal string.
Derivation Procedure −
Step 1 − Include all symbols, W1, that derive some terminal and initialize i=1.
Step 2 − Include all symbols, Wi+1, that derive Wi.
Step 3 − Increment i and repeat Step 2, until Wi+1 = Wi.
Step 4 − Include all production rules that have Wi in it.
Phase 2 − Derivation of an equivalent grammar, G”, from the CFG, G’, such that each
symbol appears in a sentential form.
Derivation Procedure −
Step 1 − Include the start symbol in Y1 and initialize i = 1.
Step 2 − Include all symbols, Yi+1, that can be derived from Yi and include all
production rules that have been applied.
Step 3 − Increment i and repeat Step 2, until Yi+1 = Yi.
Problem
Find a reduced grammar equivalent to the grammar G, having production rules, P: S
→ AC | B, A → a, C → c | BC, E → aA | e
Solution
Phase 1 −
T = { a, c, e }
W1 = { A, C, E } from rules A → a, C → c and E → aA
W2 = { A, C, E } U { S } from rule S → AC
W3 = { A, C, E, S } U ∅
Since W2 = W3, we can derive G’ as −
G’ = { { A, C, E, S }, { a, c, e }, P, {S}}
where P: S → AC, A → a, C → c , E → aA | e
Phase 2 −
Y1 = { S }
Y2 = { S, A, C } from rule S → AC
Y3 = { S, A, C, a, c } from rules A → a and C → c
Y4 = { S, A, C, a, c }
Since Y3 = Y4, we can derive G” as −
G” = { { A, C, S }, { a, c }, P, {S}}
where P: S → AC, A → a, C → c
Removal Procedure −
Step 1 − To remove A → B, add production A → x to the grammar rule whenever B
→ x occurs in the grammar. [x ∈ Terminal, x can be Null]
Step 2 − Delete A → B from the grammar.
Step 3 − Repeat from step 1 until all unit productions are removed.
Problem
Remove unit production from the following −
S → XY, X → a, Y → Z | b, Z → M, M → N, N → a
Solution −
There are 3 unit productions in the grammar −
Y → Z, Z → M, and M → N
At first, we will remove M → N.
As N → a, we add M → a, and M → N is removed.
The production set becomes
S → XY, X → a, Y → Z | b, Z → M, M → a, N → a
Now we will remove Z → M.
As M → a, we add Z→ a, and Z → M is removed.
The production set becomes
S → XY, X → a, Y → Z | b, Z → a, M → a, N → a
Now we will remove Y → Z.
As Z → a, we add Y→ a, and Y → Z is removed.
The production set becomes
S → XY, X → a, Y → a | b, Z → a, M → a, N → a
Now Z, M, and N are unreachable, hence we can remove those.
The final CFG is unit production free −
S → XY, X → a, Y → a | b
Removal Procedure
Step 1 − Find out nullable non-terminal variables which derive ε.
Step 2 − For each production A → a, construct all productions A → x where x is
obtained from ‘a’ by removing one or multiple non-terminals from Step 1.
Step 3 − Combine the original productions with the result of step 2 and remove ε -
productions.
Problem
Remove null production from the following −
S → ASA | aB | b, A → B, B → b | ∈
Solution −
There are two nullable variables − A and B
At first, we will remove B → ε.
After removing B → ε, the production set becomes −
S→ASA | aB | b | a, A ε B| b | &epsilon, B → b
Now we will remove A → ε.
After removing A → ε, the production set becomes −
S→ASA | aB | b | a | SA | AS | S, A → B| b, B → b
This is the final production set without null transition.
Next Page
A CFG is in Chomsky Normal Form if the Productions are in the following forms −
• A→a
• A → BC
• S→ε
where A, B, and C are non-terminals and a is terminal.
Problem
Convert the following CFG into CNF
S → ASA | aB, A → B | S, B → b | ε
Solution
(1) Since S appears in R.H.S, we add a new state S0 and S0→S is added to the
production set and it becomes −
S0→S, S→ ASA | aB, A → B | S, B → b | ∈
(2) Now we will remove the null productions −
B → ∈ and A → ∈
After removing B → ε, the production set becomes −
S0→S, S→ ASA | aB | a, A → B | S | ∈, B → b
After removing A → ∈, the production set becomes −
S0→S, S→ ASA | aB | a | AS | SA | S, A → B | S, B → b
(3) Now we will remove the unit productions.
After removing S → S, the production set becomes −
S0→S, S→ ASA | aB | a | AS | SA, A → B | S, B → b
After removing S0→ S, the production set becomes −
S0→ ASA | aB | a | AS | SA, S→ ASA | aB | a | AS | SA
A → B | S, B → b
After removing A→ B, the production set becomes −
S0 → ASA | aB | a | AS | SA, S→ ASA | aB | a | AS | SA
A→S|b
B→b
After removing A→ S, the production set becomes −
S0 → ASA | aB | a | AS | SA, S→ ASA | aB | a | AS | SA
A → b |ASA | aB | a | AS | SA, B → b
(4) Now we will find out more than two variables in the R.H.S
Here, S0→ ASA, S → ASA, A→ ASA violates two Non-terminals in R.H.S.
Hence we will apply step 4 and step 5 to get the following final production set which
is in CNF −
S0→ AX | aB | a | AS | SA
S→ AX | aB | a | AS | SA
A → b |AX | aB | a | AS | SA
B→b
X → SA
(5) We have to change the productions S0→ aB, S→ aB, A→ aB
And the final production set becomes −
S0→ AX | YB | a | AS | SA
S→ AX | YB | a | AS | SA
A → b A → b |AX | YB | a | AS | SA
B→b
X → SA
Y→a
Previous Page
Next Page
A CFG is in Greibach Normal Form if the Productions are in the following forms −
A→b
A → bD1…Dn
S→ε
where A, D1,....,Dn are non-terminals and b is a terminal.
Problem
Convert the following CFG into CNF
S → XY | Xn | p
X → mX | m
Y → Xn | o
Solution
Here, S does not appear on the right side of any production and there are no unit or
null productions in the production rule set. So, we can skip Step 1 to Step 3.
Step 4
Now after replacing
X in S → XY | Xo | p
with
mX | m
we obtain
S → mXY | mY | mXo | mo | p.
And after replacing
X in Y → Xn | o
with the right side of
X → mX | m
we obtain
Y → mXn | mn | o.
Two new productions O → o and P → p are added to the production set and then we
came to the final GNF as the following −
S → mXY | mY | mXC | mC | p
X → mX | m
Y → mXD | mD | o
O→o
P→p
Previous Page
Next Page
Lemma
If L is a context-free language, there is a pumping length p such that any string w ∈
L of length ≥ p can be written as w = uvxyz, where vy ≠ ε, |vxy| ≤ p, and for all i ≥ 0,
uvixyiz ∈ L.
Problem
Find out whether the language L = {xnynzn | n ≥ 1} is context free or not.
Solution
Let L is context free. Then, L must satisfy pumping lemma.
At first, choose a number n of the pumping lemma. Then, take z as 0n1n2n.
Break z into uvwxy, where
|vwx| ≤ n and vx ≠ ε.
Hence vwx cannot involve both 0s and 2s, since the last 0 and the first 2 are at least
(n+1) positions apart. There are two cases −
Case 1 − vwx has no 2s. Then vx has only 0s and 1s. Then uwy, which would have
to be in L, has n 2s, but fewer than n 0s or 1s.
Case 2 − vwx has no 0s.
Here contradiction occurs.
Hence, L is not a context-free language.
Previous Page
Next Page
• an input tape,
• a control unit, and
• a stack with infinite size.
The stack head scans the top symbol of the stack.
A stack does two operations −
• Push − a new symbol is added at the top.
• Pop − the top symbol is read and removed.
A PDA may or may not read an input symbol, but it has to read the top of the stack
in every transition.
Instantaneous Description
The instantaneous description (ID) of a PDA is represented by a triplet (q, w, s) where
• q is the state
• w is unconsumed input
• s is the stack contents
Turnstile Notation
The "turnstile" notation is used for connecting pairs of ID's that represent one or many
moves of a PDA. The process of transition is denoted by the turnstile symbol "⊢".
Consider a PDA (Q, ∑, S, δ, q0, I, F). A transition can be mathematically represented
by the following turnstile notation −
(p, aw, Tβ) ⊢ (q, w, αb)
This implies that while taking a transition from state p to state q, the input
symbol ‘a’ is consumed, and the top of the stack ‘T’ is replaced by a new string ‘α’.
Note − If we want zero or more moves of a PDA, we have to use the symbol (⊢*) for
it.
Previous Page
Next Page
Example
Construct a PDA that accepts L = {0n 1n | n ≥ 0}
Solution
Initially we put a special symbol ‘$’ into the empty stack. At state q2, the w is being
read. In state q3, each 0 or 1 is popped when it matches the input. If any other input
is given, the PDA will go to a dead state. When we reach that special symbol ‘$’, we
go to the accepting state q4.
Previous Page
Next Page
Problem
Construct a PDA from the following CFG.
G = ({S, X}, {a, b}, P, S)
where the productions are −
S → XS | ε , A → aXb | Ab | ab
Solution
Let the equivalent PDA,
P = ({q}, {a, b}, {a, b, X, S}, δ, q, S)
where δ −
δ(q, ε , S) = {(q, XS), (q, ε )}
δ(q, ε , X) = {(q, aXb), (q, Xb), (q, ab)}
δ(q, a, a) = {(q, ε )}
δ(q, 1, 1) = {(q, ε )}
Previous Page
Next Page
Parsing is used to derive a string using the production rules of a grammar. It is used
to check the acceptability of a string. Compiler is used to check whether or not a string
is syntactically correct. A parser takes the inputs and builds a parse tree.
A parser can be of two types −
• Top-Down Parser − Top-down parsing starts from the top with the
start-symbol and derives a string using a parse tree.
• Bottom-Up Parser − Bottom-up parsing starts from the bottom with
the string and comes to the start symbol using a parse tree.
The CFG which accepts deterministic PDA accepts non-deterministic PDAs as well.
Similarly, there are some CFGs which can be accepted only by NPDA and not by DPDA.
Thus NPDA is more powerful than DPDA.
Example:
Design PDA for Palindrome strips.
Solution:
Play Video
Suppose the language consists of string L = {aba, aa, bb, bab, bbabb, aabaa, ......]. The
string can be odd palindrome or even palindrome. The logic for constructing PDA is
that we will push a symbol onto the stack till half of the string then we will read each
symbol and then perform the pop operation. We will compare to see whether the
symbol which is popped is similar to the symbol which is read. Whether we reach to
end of the input, we expect the stack to be empty.
This PDA is a non-deterministic PDA because finding the mid for the given string and
reading the string from left and matching it with from right (reverse) direction leads to
non-deterministic moves. Here is the ID.
Simulation of abaaba
Example:
Production rules:
1. T= T + T | T * T
2. T = a|b|c
Input:
a * b + c
Step 1:
Step 2:
Step 3:
Step 4:
Step 5:
Deterministic Pushdown Automata
The Deterministic Pushdown Automata is a variation of pushdown automata that
accepts the deterministic context-free languages.
A language L(A) is accepted by a deterministic pushdown automata if and only if
there is a single computation from the initial configuration until an accepting one for
all strings belonging to L(A). It is not as powerful as non-deterministic finite
automata. That's why it is less in use and used only where determinism is much
easier to implement.
A PDA is said to be deterministic if its transition function δ(q,a,X) has at most one
member for -
a ∈ Σ U {ε}
So, for a deterministic PDA, there is at most one transition possible in any
combination of state, input symbol and stack top.
Formal Definition of Deterministic PDA
A Deterministic PDA is 5 tuple -
M = (Σ,Γ,Q,δ,q)