Professional Documents
Culture Documents
All TOC E-Lecture Notes
All TOC E-Lecture Notes
All TOC E-Lecture Notes
Type-01 Problems-
In Type-01 problems, we will discuss the construction of DFA for languages consisting of strings ending with a
particular substring.
Step-01:
Determine the minimum number of states required in the DFA.
RULE
All strings ending with ‘n’ length substring will always require minimum (n+1) states in the DFA.
Step-02:
Decide the strings for which DFA will be constructed.
Step-03:
Construct a DFA for the strings decided in Step-02.
Remember the following rule while constructing the DFA-
RULE
While constructing a DFA,
Example-01:
Draw a DFA for the language accepting strings ending with ’01’ over input alphabets ∑ = {0, 1}
Solution-
Regular expression for the given language = (0 + 1)*01
Step-01:
All strings of the language ends with substring “01”.
Step-02:
We will construct DFA for the following strings-
01
001
0101
Step-03:
The required Final DFA is-
Example-02:
Draw a DFA for the language accepting strings ending with ‘abb’ over input alphabets ∑ = {a, b}
Solution-
Regular expression for the given language = (a + b)*abb
Step-01:
All strings of the language ends with substring “abb”.
So, length of substring = 3.
Thus, Minimum number of states required in the DFA = 3 + 1 = 4.
Step-02:
We will construct DFA for the following strings-
abb
aabb
ababb
abbabb
Step-03:
The required Final DFA is-
Type-02 Problems-
In Type-02 problems, we will discuss the construction of DFA for languages consisting of strings starting with a
particular substring.
Step-01:
RULE
Calculate the length of substring.
All strings starting with ‘n’ length substring will always require minimum (n+2) states in the DFA.
Step-02:
RULE
While constructing a DFA,
Step-04:
Send all the left possible combinations to the dead state.
Do not send the left possible combinations over the starting state.
Example-01:
Draw a DFA for the language accepting strings starting with ‘ab’ over input alphabets ∑ = {a, b}
Solution-
Step-01:
Step-02:
ab
aba
abab
Step-03:
Example-02:
Draw a DFA for the language accepting strings starting with ‘a’ over input alphabets ∑ = {a, b}
Solution-
Step-01:
Step-02:
a
aa
Step-03:
The required DFA is-
Example-03(Type02)
Draw a DFA for the language accepting strings starting with ‘101’ over input alphabets ∑ = {0, 1}
Solution-
Step-01:
Step-02:
101
1011
10110
101101
Step-03:
Symbols are an entity or individual objects.A symbol (often also called a character) is the smallest building block,
which can be any alphabet, letter, or picture.
a,b,c,………0,1,2…………….
Alphabets (Σ):
String:
A string is a finite sequence of symbols from some alphabet. A string is generally denoted as w and the length of
a string is denoted as |w|.
Empty string is the string with zero occurrence of symbols, represented as ε.
Number of Strings (of length 2)
that can be generated over the alphabet {a, b}:
- -
a a
a b
b a
b b
Conclusion:
For alphabet {a, b} with length n, number of
strings can be generated = 2n.
Note: If the number of symbols in the alphabet Σ is represented by |Σ|, then a number of strings of length n,
possible over Σ is |Σ|n.
Regular operations.
There are three operations which are performed on languages namely: union, concatenation and kleen closure.
For example,
Concatenation.
Substring.
Kleen star operation.
Reversal.
Concatenation
Concatenation is nothing but combining the two strings one after another.
Example
Example
Let’s consider w=’Theory’ is defined with prefix, x=’The’ and suffix , y=’ry’.
The substring is v=’o’, because, w=xvy and Theory= Thevry
Therefore, v=o
Kleen star operation
Let ‘w’ be a string. w’ is a set of strings obtained by applying any number of concatenations of w with itself,
including empty string.
Example
a*= { ε,a,aa,aaa,………}
Reversal operation
If ‘w’ is a string, then wR is the reversal of the string in backwards.
Rules
The rules for the reversal operation are as follows −
x=(xR)R
(xz)R= zR.xR
Example
X= tutorial
(x)R= lairotut
(xR)R= tutorial
Moore and Mealy machines
Moore and Mealy machines are called Finite Automata with output. This is different from
a DFA or NFA (Finite Automata without output) where there is no output (input is either accepted or rejected).
Moore Machine
Moore machine is a finite state machine in which the next state is decided by the current state and current input
symbol. The output symbol at a given time depends only on the present state of the machine.
Example 1:
Input: 010
Output: 1110(1 for q0, 1 for q1, again 1 for q1, 0 for q2)
Example 2:
Solution: To generate 1's complement of a given binary number the simple logic is that if the input is 0 then the
output will be 1 and if the input is 1 then the output will be 0. That means there are three states. One state is start
state. The second state is for taking 0's as input and produces output as 1. The third state is for taking 1's as input and
producing output as 0.
Input 1 0 1 1
State q0 q2 q1 q2 q2
Output 0 0 1 0 0
Thus we get 00100 as 1's complement of 1011, we can neglect the initial 0 and the output which we get is 0100
which is 1's complement of 1011. The transaction table is as follows:
Thus Moore machine M = (Q, q0, ∑, O, δ, λ); where Q = {q0, q1, q2}, ∑ = {0, 1}, O = {0, 1}. the transition table
shows the δ and λ functions.
Example 3:
Design a Moore machine for a binary input sequence such that if it has a substring 101, the machine output A, if the
input has substring 110, it outputs B otherwise it outputs C.
Solution: For designing such a machine, we will check two conditions, and those are 101 and 110. If we get 101, the
output will be A, and if we recognize 110, the output will be B. For other strings, the output will be C.
Now we will insert the possibilities of 0's and 1's for each state. Thus the Moore machine becomes:
Example 4:
Construct a Moore machine that determines whether an input string contains an even or odd number of 1's. The
machine should give 1 as output if an even number of 1's are in the string and 0 otherwise.
Solution:
This is the required Moore machine. In this machine, state q1 accepts an odd number of 1's and state q0 accepts even
number of 1's. There is no restriction on a number of zeros. Hence for 0 input, self-loop can be applied on both the
states.
Mealy Machine
In a Mealy machine the output symbol depends upon the present input symbol and present state of the machine.
In the Mealy machine, the output is represented with each input symbol and each state is separated by /.
The Mealy machine can be described by six tuples (Q, q0, Σ, O, δ, λ')
Where,
In the Mealy machine, the output is represented with each input symbol and each state is separated by /.
The length of output for a mealy machine is equal to the length of input.
Example 1:
Design a Mealy machine for a binary input sequence such that if it has a substring 101, the machine output A, if the
input has substring 110, it outputs B otherwise it outputs C.
Solution: For designing such a machine, we will check two conditions, and those are 101 and 110. If we get 101, the
output will be A. If we recognize 110, the output will be B. For other strings the output will be C.
Now we will insert the possibilities of 0's and 1's for each state. Thus the Mealy machine becomes:
Less hardware requirement for circuit The hardware requirement is comparatively more for circuit
implementation. implementation.
If input changes, output does not change If input changes, output also changes.
https://www.codingninjas.com/studio/library/moore-and-mealy-machine
Minimization of DFA
The process of reducing a given DFA to its minimal form is called as minimization of DFA.
DFA minimization stands for converting a given DFA to its equivalent DFA with minimum number of
states. DFA minimization is also called as Optimization of DFA and uses partitioning algorithm.
Minimization of DFA means reducing the number of states from given FA. Thus, we get the FSM(finite state
machine) with redundant states after minimizing the FSM.
Minimization of DFA(Methods)
Step-01:
Eliminate all the dead states and inaccessible states from the given DFA (if any).
Dead State (All those non-final states which transit to itself for all input symbols in ∑ are called as dead
states.)
Inaccessible State (All those states which can never be reached from the initial state are called as
inaccessible states.)
Step-02:
Draw a state transition table for the given DFA.
Transition table shows the transition of all states on all input symbols in Σ.
Step-03:
Now, start applying equivalence theorem.
Example-01:
Minimize the given DFA-
Solution-
Step-01:
The given DFA contains no dead states and inaccessible states.
Step-02:
a b
→q0 q1 q2
q1 q1 q3
q2 q1 q2
q3 q1 *q4
*q4 q1 q2
Step-03:
P0 = { q0 , q1 , q2 , q3 } { q4 }
P1 = { q0 , q1 , q2 } { q3 } { q4 }
P2 = { q0 , q2 } { q1 } { q3 } { q4 }
P3 = { q0 , q2 } { q1 } { q3 } { q4 }
Since P3 = P2, so we stop.
From P3, we infer that states q0 and q2 are equivalent and can be merged together.
https://www.gatevidyalay.com/minimization-of-dfa-minimize-dfa-example/
Example-02:
Solution-
Step-01:
Step-02:
a b
→q0 *q1 q0
Step-03:
P0 = { q0 } { q1 , q2 }
P1 = { q0 } { q1 , q2 }
From P1, we infer that states q1 and q2 are equivalent and can be merged together.
We have to follow the various steps to minimize the DFA. These are as follows:
Step 1: Remove all the states that are unreachable from the initial state via any set of the transition of DFA.
Step 3: Now split the transition table into two tables T1 and T2. T1 contains all final states, and T2 contains non-
final states.
1. δ (q, a) = p
2. δ (r, a) = p
That means, find the two states which have the same value of a and b and remove one of them.
Step 5: Repeat step 3 until we find no similar rows available in the transition table T1.
Step 7: Now combine the reduced T1 and T2 tables. The combined transition table is the transition table of
minimized DFA.
Example:
Solution:
Step 1: In the given DFA, q2 and q4 are the unreachable states so remove them.
State 0 1
→q0 q1 q3
q1 q0 q3
*q3 q5 q5
*q5 q5 q5
Step 2: Draw the transition table for the rest of the states.
Step 3: Now divide rows of transition table into two sets as:
1. One set contains those rows, which start from non-final states:
State 0 1
q0 q1 q3
q1 q0 q3
2. Another set contains those rows, which starts from final states.
State 0 1
q3 q5 q5
q5 q5 q5
State 0 1
q3 q3 q3
State 0 1
→q0 q1 q3
q1 q0 q3
*q3 q3 q3
Myhill Nerode Theorem gives sufficient and necessary conditions for a language to be
regular.
Step-2: Mark all the pairs (Qa,Qb) such a that Qa is Final state and Qb is Non-
Final State.
Step-3: If there is any unmarked pair (Qa,Qb) such a that δ(Qa,x) and δ(Qb,x)
is marked, then mark (Qa,Qb). Here x is a input symbol. Repeat this step until
no more marking can be made.
Check for the unmarked pair Q2,Q1
Check when x=0 : δ(Q2,0) = Q4 and δ(Q1,0) = Q3, check if the pair
Q4,Q3 is marked and no it is not marked.
Check when x=1 : δ(Q2,1) = Q3 and δ(Q1,1) = Q4, check if the pair
Q4,Q3 is marked and no it is not marked.
Hence we cannot mark the pair Q2,Q1.
Check for the unmarked pair Q3,Q0
Check when x=0 : δ(Q3,0) = Q5 and δ(Q0,0) = Q1, check if the pair
Q5,Q1 is marked and no it is not marked.
Check when x=1 : δ(Q3,1) = Q5 and δ(Q0,1) = Q2, check if the pair
Q5,Q2 is marked and no it is not marked.
Hence we cannot mark the pair Q3,Q0.
Check for the unmarked pair Q4,Q0
Check when x=0 : δ(Q4,0) = Q5 and δ(Q0,0) = Q1, check if the pair
Q5,Q1 is marked and no it is not marked.
Check when x=1 : δ(Q4,1) = Q5 and δ(Q0,1) = Q2, check if the pair
Q5,Q2 is marked and no it is not marked.
Hence we cannot mark the pair Q4,Q0.
Check for the unmarked pair Q4,Q3
Check when x=0 : δ(Q4,0) = Q5 and δ(Q3,0) = Q5, Such pair of
state Q5,Q5 don’t exists.
Check when x=1 : δ(Q4,1) = Q5 and δ(Q3,1) = Q5, Such pair of
state Q5,Q5 don’t exists.
Hence we cannot mark the pair Q4,Q3.
Check for the unmarked pair Q5,Q1
Check when x=0 : δ(Q5,0) = Q5 and δ(Q1,0) = Q3, check if the pair
Q5,Q3 is marked and yes it is marked.
Hence we can mark the pair Q5,Q1.
We have checked for all the unmarked pairs but don’t need to stop here we
need to continue this process until no more markings can be made.
Check for the unmarked pair Q2,Q1
Check when x=0 : δ(Q2,0) = Q4 and δ(Q1,0) = Q3, check if the pair
Q4,Q3 is marked and no it is not marked.
Check when x=1 : δ(Q2,1) = Q3 and δ(Q1,1) = Q4, check if the pair
Q4,Q3 is marked and no it is not marked.
Hence we cannot mark the pair Q2,Q1.
Check for the unmarked pair Q3,Q0
Check when x=0 : δ(Q3,0) = Q5 and δ(Q0,0) = Q1, check if the pair
Q5,Q1 is marked and yes it is marked.
Hence we can mark the pair Q3,Q0.
If the language consists of finite number of strings, then it is called as a Finite language.
If the language consists of infinite number of strings, then it is called as an Infinite language.
Formal Definition-
A Grammar is a 4-tuple such that-
G = (V , T , P , S)
where-
1. Terminal symbols
2. Non-terminal symbols
1. Terminal Symbols-
Terminal symbols are those which are the constituents of the sentence generated using a grammar.
Terminal symbols are denoted by using small case letters such as a, b, c etc.
2. Non-Terminal Symbols-
Non-Terminal symbols are those which take part in the generation of the sentence but are not part of it.
Example-02:
Consider a grammar G = (V , T , P , S) where-
Important Concept-
For any given grammar, the language generated by it is always unique.
For any given language, we may have more than one grammar generating that language.
Example-
Consider the following two grammars-
Grammar G1-
S → AB
A→a
B→b
L(G1) = { ab }
Grammar G2-
S → AB
A→∈
B → ab
L(G2) = { ab }
Here,
Types of Grammars-
Grammars are classified on different basis as-
The two popular methods for converting a given DFA to its regular expression are-
Arden’s Theorem-
Arden’s Theorem is popularly used to convert a given DFA to its regular expression.
It states that-
Conditions-
To use Arden’s Theorem, following conditions must be satisfied-
To convert a given DFA to its regular expression using Arden’s Theorem, following steps are followed-
Step-01:
Form a equation for each state considering the transitions which comes towards that state.
Bring final state in the form R = Q + RP to get the required regular expression.
Note-02:
A = ∈ + B.1 ……(1)
B = A.0 ……(2)
Step-02:
B = (∈ + B.1).0
B = ∈.0 + B.1.0
B = 0 + B.(1.0) ……(3)
B = 0.(1.0)*
Regular Expression
(0 + ε)(1
L = {ε, 0, 1, 01}
+ ε)
Set of strings of a’s and b’s of any length including the null string. So L = { ε, a, b,
(a+b)*
aa , ab , bb , ba, aaa…….}
(a+b)*ab Set of strings of a’s and b’s ending with the string abb. So L = {abb, aabb, babb,
b aaabb, ababb, …………..}
Set consisting of even number of 1’s including empty string, So L= {ε, 11, 1111,
(11)*
111111, ……….}
(aa)*(bb Set of strings consisting of even number of a’s followed by odd number of b’s , so L
)*b = {b, aab, aabbb, aabbbbb, aaaab, aaaabbb, …………..}
(aa + ab String of a’s and b’s of even length can be obtained by concatenating any
+ ba + combination of the strings aa, ab, ba and bb including null, so L = {aa, ab, ba, bb,
bb)* aaab, aaba, …………..}
Regular Sets
Any set that represents the value of the Regular
Expression is called a Regular Set.
Properties of Regular Sets
Property 1. The union of two regular set is regular.
Property 2. The intersection of two regular set is
regular.
Property 3. The complement of a regular set is
regular.
Property 4. The difference of two regular set is
regular.
Property 5. The reversal of a regular set is regular.
Property 6. The closure of a regular set is regular.
Property 7. The concatenation of two regular sets is
regular.
9. Identities for regular expression –
There are many identities for the regular expression. Let p, q
and r are regular expressions.
∅+r=r
∅.r= r.∅ = ∅
∈.r = r.∈ =r
∈* = ∈ and ∅* = ∈
r+r=r
r*.r* = r*
r.r* = r*.r = r+.
(r*)* = r*
∈ +r.r* = r* = ∈ + r.r*
(p.q)*.p = p.(q.p)*
(p + q)* = (p*.q*)* = (p* + q*)*
(p+ q).r= p.r+ q.r and r.(p+q) = r.p + r.q
Pumping Lemma
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.
Context Free Grammar provides no mechanism to restrict the usage of the production
rule A → α within some specific context unlike other types of grammars.
That is why it is called as “Context Free” Grammar.
Example-01:
Example-02:
The language generated using Context Free Grammar is called as Context Free Language
Properties-
Normal Forms-
By reducing the grammar, the grammar gets minimized but does not gets
standardized.
This is because the RHS of productions have no specific format.
In order to standardize the grammar, normalization is performed using
normal forms.
A context free grammar is said to be in chomsky normal form (CNF) if all its productions are o
the form-
A → BC or A → a
where A, B, C are non-terminals and a is a terminal.
Example-
S → AB
A→a
B→b
Steps-
The following steps are followed to standardize the grammar using CNF-
Rule-01:
Reduce the grammar completely by-
Eliminating ∈ productions
Eliminating unit productions
Eliminating useless productions
Rule-02:
Rule-03:
Problem-01:
Solution-
Step-01:
Step-02:
Step-03:
Step-04:
Step-05:
From (1), (2), (5), (6), (9), (10), (11) and (12), the resultant grammar is-
S → CaCAD
A → CaB / CbCAB
B→b
D→d
Ca → a
Cb → b
CAD → AD
CAB → AB
Solution-
Step-01:
Step-02:
Step-03:
Step-04:
Out of (8), (9) and (10), the productions already in Chomsky Normal
Form are-
S → DA / CB ………..(11)
A → CS ………..(12)
B → DS ………..(13)
These productions will remain as they are.
Step-05:
Step-06:
From (1), (2), (6), (7), (11), (12), (13), (16), (17), (18) and (19), the
resultant grammar is-
S → DA / CB
A → CS / DE / 0
B → DS / CF / 1
C→0
D→1
E → AA
F → BB
Example:
1. S → XB | AA
2. A → a | SA
3. B → b
4. X → a
ORRRRRRRRRR
The production rules of Grammar G1 satisfy the above rules specified for GNF.
Therefore, G1 is in GNF.
However, the production rule of G2 does not satisfy the rules specified for GNF as
A → ε and B → ε contains ε, but only the start symbol can generate ε. So the
grammar G2 is not in GNF.
Notes:
1. There can be more than one GNF for a given grammar.
2. GNF produces the same language as CFG produces.
Solution:
We can skip steps 1 and 2 and move directly to step 3 because the given
grammar G is already in CNF, and there is no left recursion.
1. The production rule A → SA is not in GNF, so we substitute S -> AA | BC in the
production rule A → SA as
S → AA | BC
A → a | AAA | BCA
B→a
C→c
PDA Components:
Input tape: The input tape is divided in many cells or symbols. The input
head is read-only and may only move from left to right, one symbol at a
time.
There are many cells or symbols on this tape. A single symbol may only be
entered at a time using the input head, which is read-only.
Finite control: The finite control has some pointer which points the current
symbol which is to be read.
Stack: The stack is a structure in which we can push and remove the items
from one end only. It has an infinite size. Items are temporarily stored on the
stack in a PDA.