TOC Syllabus
TOC Syllabus
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:
∑ = {a, b}
∑ = {A, B, C, D}
∑ = {0, 1, 2}
∑ = {0, 1, ....., 5]
∑ = {#, β, Δ}
String:
It is a finite collection of symbols from the alphabet. The string is
denoted by w.
Example 1:
If ∑ = {a, b}, various string that can be generated from ∑ are {ab,
aa, aaa, bb, bbb, ba, aba.....}.
Example 2:
w = 010
Number of Sting |w| = 3
Language:
A language is a collection of appropriate string. A language which is formed over
Σ can be Finite or Infinite.
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'} = {a, aa, aaa, abb, abbb, ababb} Infinite
Language
Finite Automata
where:
F: final state
δ: Transition function
Finite Automata Model:
2. NFA
NFA stands for non-deterministic finite automata. It is used to transmit any
number of states for a particular input. It can accept the null move.
1. In DFA, the input to the automata can be any string. Now, put a pointer to
the start state q and read the input string w from left to right and move the
pointer according to the transition function, δ. We can read one symbol at
a time. If the next symbol of string w is a and the pointer is on state p,
move the pointer to δ(p, a). When the end of the input string w is
encountered, then the pointer is on some state F.
2. The string w is said to be accepted by the DFA if r ∈ F that means the input
string w is processed successfully and the automata reached its final state. The
string is said to be rejected by DFA if r ∉ F.
Example 1:
DFA with ∑ = {0, 1} accepts all strings starting with 1.
Solution:
The finite automata can be represented using a transition graph. In the above
diagram, the machine initially is in start state q0 then on receiving input 1 the
machine changes its state to q1. From q0 on receiving 0, the machine changes
its state to q2, which is the dead state. From q1 on receiving input 0, 1 the
machine changes its state to q1, which is the final state. The possible input
strings that can be generated are 10, 11, 110, 101, 111......., that means all
string starts with 1.
Example 2:
Solution:
Transition Diagram
The NFA can be represented using a transition graph. In the above diagram,
the machine initially is in start state q0 then on receiving input 1 the
machine changes its state to q1. From q1 on receiving input 0, 1 the
machine changes its state to q1. The possible input string that can be
generated is 10, 11, 110, 101, 111......, that means all string starts with 1.
Transition Table
Solution:
Transition table of given DFA is as follows:
→q0 q1 q2
q1 q0 q2
*q2 q2 q2
Explanation:
•In the above table, the first column indicates all the current states.
Under column 0 and 1, the next states are shown.
•The first row of the transition table can be read as, when the current
state is q0, on input 0 the next state will be q1 and on input 1 the next
state will be q2.
•In the second row, when the current state is q1, on input 0, the next
state will be q0, and on 1 input the next state will be q2.
•In the third row, when the current state is q2 on input 0, the next state
will be q2, and on 1 input the next state will be q2.
Solution:
Transition table of given NFA is as follows:
δ: Q x ∑→Q
Example 1:
Transition
Q = {q0, q1, q2} Table:
Present State Next state for Next State of
∑ = {0, 1} Input 0 Input 1
q0 = {q0} →q0 q0 q1
F = {q2} q1 q2 q1
*q2 q2 q2
Solution:
Transition Diagram:
Example 2:
In the above diagram, we can see that on given 0 as input to DFA in state q0
the DFA changes state to q1 and always go to final state q1 on starting input
0. It can accept 00, 01, 000, 001....etc. It can't accept any string which starts
with 1, because it will never go to final state on a string starting with 1.
Example 3:
Explanation:
In the above diagram, we can see that on given 0 as input to DFA in state q0,
the DFA changes state to q1. It can accept any string which ends with 0 like
00, 10, 110, 100....etc. It can't accept any string which ends with 1, because
it will never go to the final state q1 on 1 input, so the string ending with 1,
will not be accepted or will be rejected.
Question :
Design a FA with ∑ = {0, 1} accepts those string which starts with
1 and ends with 0.
Question :
Design a FA with ∑ = {0, 1} accepts those string which starts with
1 and ends with 0.
Solution:
The FA will have a start state q0 from which only the edge with input 1 will go to
the next state.
In state q1, if we read 1, we will be in state q1, but if we read 0 at state q1, we
will reach to state q2 which is the final state. In state q2, if we read either 0 or 1,
we will go to q2 state or q1 state respectively. Note that if the input ends with
0, it will be in the final state.
Example 2:
Design a FA with ∑ = {0, 1} accepts the only input 101.
Example 2:
Design a FA with ∑ = {0, 1} accepts the only input 101.
Solution:
In the given solution, we can see that only input 101 will be accepted. Hence, for
input 101, there is no other path shown for other input.
Example 3:
Design FA with ∑ = {0, 1} accepts even number of 0's and even
number of 1's.
Example 3:
Design FA with ∑ = {0, 1} accepts even number of 0's and even
number of 1's.
Solution:
This FA will consider four different stages for input 0 and input
1. The stages could be:
Here q0 is a start state and the final state also. Note carefully that
a symmetry of 0's and 1's is maintained. We can associate
meanings to each state as:
q0: state of even number of 0's and even number of 1's.
q1: state of odd number of 0's and even number of 1's.
q2: state of odd number of 0's and odd number of 1's.
q3: state of even number of 0's and odd number of 1's.
Example 4:
Design FA with ∑ = {0, 1} accepts the set of all strings with three
consecutive 0's.
Solution:
The strings that will be generated for this particular languages are 000,
0001, 1000, 10001, .... in which 0 always appears in a clump of 3. The
transition graph is as follows:
Example 5:
Design a DFA L(M) = {w | w ε {0, 1}*} and W is a string that does
not contain consecutive 1's.
Solution:
The stages q0, q1, q2 are the final states. The DFA will generate
the strings that do not contain consecutive 1's like 10, 110,
101,..... etc.
Example 6:
Design a FA with ∑ = {0, 1} accepts the strings with an even number of 0's
followed by single 1.
Solution:
The DFA can be shown by a transition diagram as:
NFA (Non-Deterministic finite automata)
•The finite automata are called NFA when there exist many paths for
•Every NFA is not DFA, but each NFA can be translated into DFA.
•NFA is defined in the same way as DFA but with the following two
NFA also has five states same as DFA, but with different
transition function, as shown follows:
δ: Q x ∑ →2Q
where,
Solution:
Transition diagram:
q1 ε q2
*q2 q2 q2
Example 3:
Present State 0 1
→q0 q0, q1 q0, q2
q1 q3 ε
q2 q2, q3 q3
→q3 q3 q3
Solution:
The transition diagram can be drawn by using the mapping function as given in the
table.
Transition Function :
δ(q0, 0) = {q0, q1}
δ(q0, 1) = {q0, q2}
Then, δ(q1, 0) = {q3}
Then, δ(q2, 0) = {q2, q3}
δ(q2, 1) = {q3}
Then, δ(q3, 0) = {q3}
δ(q3, 1) = {q3}
Example 2:
Design an NFA with ∑ = {0, 1} accepts all string ending with 01.
Solution:
Solutions:
Example 4:
Design an NFA in which all the string contain a substring 1110.
Solution:
Transition table for the above transition diagram can be given below:
Present State 0 1
→q1 q1 q1, q2
q2 q3
q3 q4
q4 q5
*q5 q5 q5
Example 5:
Design an NFA with ∑ = {0, 1} accepts all string in which the third symbol
from the right end is always 0.
Solution:
Eliminating ε Transitions
NFA with ε can be converted to NFA without ε, and this NFA without ε can
be converted to DFA. To do this, we will use a method, which can remove all
the ε transition from given NFA. The method will be:
1. Find out all the ε transitions from each state from Q. That
will be called as ε-closure{q1} .
2. Then δ' transitions can be obtained. The δ' transitions mean
a ε-closure on δ moves.
3. Repeat Step-2 for each input symbol and each state of
given NFA.
4. Using the result states, the transition table for equivalent
NFA without ε can be built.
Rules for Conversion
ε-closure(q0) = {q0}
ε-closure(q1) = {q1, q2}
ε-closure(q2) = {q2}
Now the δ' transition on each input symbol is obtained as:
Now the δ' transition on each input symbol is obtained as:
States a b
→q0 {q1, q2} Ф
*q1 Ф {q2}
*q2 Ф {q2}
State q1 and q2 become the final state as ε-closure of q1 and
q2 contain the final state q2. The NFA can be shown by the
following transition diagram:
Example:
Convert the following NFA with ε to NFA without ε
0 1 2
Ɛ Ɛ
qo q1 q2
Output :
State/ Input 0 1 2
*q2 Ǿ Ǿ {q2}
Conversion from NFA with ε to DFA
Where
Step 1: We will take the ε-closure for the starting state of NFA as a starting
state of DFA.
Step 2: Find the states for each input symbol that can be traversed from the
present. That means the union of transition value and their closures for
each state of NFA present in the current state of DFA.
Step 3: If we found a new state, take it as current state and repeat step 2.
Step 4: Repeat Step 2 and Step 3 until there is no new state present in the
transition table of DFA.
Step 5: Mark the states of DFA as a final state which contains the final state
of NFA.
Example 1:
Convert the NFA with ε into its equivalent DFA.
Finite automata may have outputs corresponding to
each transition. There are two types of finite state
machines that generate output −
1. Mealy Machine
2. Moore machine
Mealy Machine
A Mealy Machine is an FSM whose output depends on the present state as well
as the present input. It can be described by a 6 tuple (Q, ∑, O, δ, X, q0) where −
Next state
Present
input = 0 input = 1
state
State Output State Output
→a b x1 c x1
b b x2 d x3
c d x3 c x1
d d x3 d x2
The state diagram of the above Mealy Machine is −
/ x2
/ x2
/ x1 / x3
a d
/ x3
/ x1
c
/ x1
Moore Machine
Generally, it has fewer states than Generally, it has more states than
Moore Machine. Mealy Machine.
The value of the output function is a The value of the output function is a
function of the transitions and the function of the current state and the
changes, when the input logic on the changes at the clock edges, whenever
present state is done. state changes occur.
Next State
Present
a=0 a=1
State
State Output State Output
→a d b
b a d
c c c
d b a
Step 3 −
Next State
Present
a=0 a=1
State
State Output State Output
=> a d 1 b 0
b a 1 d 1
c c 0 c 0
d b 0 a 1
Mealy Machine to Moore Machine
Step 2 − If all the outputs of Qi are same, copy state Qi. If it has n
distinct outputs, break Qi into n states as Qin where n = 0, 1,
2.......
Next State
Present a=0 a=1
State Next Output Next Output
State State
→a d 0 b 1
b a 1 d 0
c c 1 c 0
d b 0 a 1
Here, states ‘a’ and ‘d’ give only 1 and 0 outputs respectively, so we retain
states ‘a’ and ‘d’. But states ‘b’ and ‘c’ produce different outputs (1 and 0). So,
we divide b into b0, b1 and c into c0, c1.
Present Next State
Output
State a=0 a=1
→a d b1 1
b0 a d 0
b1 a d 1
c0 c1 C0 0
c1 c1 C0 1
d b0 a 1
Grammar
A grammar G can be formally written as a 4-tuple ( N, T, S, P) where −
Here,
If there is a grammar
G:
V = {S, A, B}
T = {a, b}
P = {S → AB, A → a, B → b}
G= (V, T, P, S)
Where,
into 4 types:
ᵦ is ( V + T )*.
For example,
Sab –> ba
A –> S.
Here, Variables are S, A and Terminals a, b.
Type 1: Context-Sensitive Grammar
Type-1 grammars generate the context-sensitive languages. The
language generated by the grammar are recognized by the Linear Bound
Automata.
In Type 1 :
For Example,
S –> AB ᵦ is ( V + T )+
AB –> abc
B –> b
Type 2: Context Free Grammar
Type-2 grammars generate the context-free languages. The language generated
by the grammar is recognized by a Pushdown automata.
In Type 2,
1. First of all it should be Type 1.
2. Left hand side of production can have only one variable.
| α | = 1.
Their is no restriction on ᵦ . |α |=1
For example,
S –> AB
A –> a ᵦ is ( V + T )*
B –> b
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 most restricted form of grammar.
Type 3 should be in the given form only : |α |=1
T0 REL TM
T1 CSL LBA
T2 CFL PDA
T3 RL FA
Closure Properties
In an automata theory, there are different closure properties for
1. Union
2. Intersection
3. concatenation
4. Kleene closure
5. Complement
Union
Example
L1 = {an | n > O} and L2 = {bn | n > O}
L3 = L1 U L2 = {an U bn | n > O} is also regular.
Intersection
Example
Example
Example
L1 = (a U b )
L1* = (a U b)*
Complement
Example
L(G) = {an | n > 3} L'(G) = {an | n <= 3}
Note − Two regular expressions are equivalent, if
Example : L = {ε,00,0000,000000……}
automata.
Properties of Regular Sets
Property 1. The union of two regular set is regular.
Proof −
Let us take two regular expressions
RE1 = a(aa)* and RE2 = (aa)*
So, L1 = {a, aaa, aaaaa,.....} (Strings of odd length excluding Null)
and L2 ={ ε, aa, aaaa, aaaaaa,.......} (Strings of even length
including Null)
L1 ∪ L2 = { ε, a, aa, aaa, aaaa, aaaaa, aaaaaa,.......}
(Strings of all possible lengths including Null)
RE (L1 ∪ L2) = a* (which is a regular expression itself)
Hence, proved.
Property 2. The intersection of two regular set is regular.
Proof −
Let us take two regular expressions
RE1 = a(a*) and RE2 = (aa)*
So, L1 = { a,aa, aaa, aaaa, ....} (Strings of all possible lengths excluding
Null)
L2 = { ε, aa, aaaa, aaaaaa,.......} (Strings of even length including Null)
L1 ∩ L2 = { aa, aaaa, aaaaaa,.......} (Strings of even length excluding Null)
RE (L1 ∩ L2) = aa(aa)* which is a regular expression itself.
Hence, proved.
Property 3. The complement of a regular set is regular.
Proof −
Let us take a regular expression −
RE = (aa)*
So, L = {ε, aa, aaaa, aaaaaa, .......} (Strings of even length
including Null)
Complement of L is all the strings that is not in L.
So, L’ = {a, aaa, aaaaa, .....} (Strings of odd length excluding Null)
RE (L’) = a(aa)* which is a regular expression itself.
Hence, proved.
Property 4. The difference of two regular set is regular.
Proof −
Let us take two regular expressions −
RE1 = a (a*) and RE2 = (aa)*
So, L1 = {a, aa, aaa, aaaa, ....} (Strings of all possible lengths excluding
Null)
L2 = { ε, aa, aaaa, aaaaaa,.......} (Strings of even length including Null)
L1 – L2 = {a, aaa, aaaaa, aaaaaaa, ....}
(Strings of all odd lengths excluding Null)
RE (L1 – L2) = a (aa)* which is a regular expression.
Hence, proved.
Property 5. The reversal of a regular set is regular.
Proof −
We have to prove LR is also regular if L is a regular set.
Let, L = {01, 10, 11, 10}
RE (L) = 01 + 10 + 11 + 10
LR = {10, 01, 11, 01}
RE (LR) = 01 + 10 + 11 + 10 which is regular
Hence, proved.
Property 6. The closure of a regular set is regular.
Proof −
If L = {a, aaa, aaaaa, .......} (Strings of odd length excluding
Null)
i.e., RE (L) = a (aa)*
L* = {a, aa, aaa, aaaa , aaaaa,……………} (Strings of all lengths
excluding Null)
RE (L*) = a (a)*
Hence, proved.
Property 7. The concatenation of two regular sets is regular.
Proof −
Let RE1 = (0+1)*0 and RE2 = 01(0+1)*
Here, L1 = {0, 00, 10, 000, 010, ......} (Set of strings ending in 0)
and L2 = {01, 010,011,.....} (Set of strings beginning with 01)
Then, L1 L2 =
{001,0010,0011,0001,00010,00011,1001,10010,.............}
Set of strings containing 001 as a substring which can be
represented by an RE − (0 + 1)*001(0 + 1)*
Hence, proved.
A Regular Expression can be recursively defined as
follows −
R= a*
Solution : L={a,aa,aaa,aaaa,aaaaa,………….}
The set indicated that there is no NUMM string, So we
R= (a+b)*
Example 4 : Write r.e. to denote thelanguage L over ∑*,where ∑
= {a,b,c} in which every string will be such that any number of a’s
is followed by any number of b’s is followed by any number of c.
Solution :
R= a*b*c*
Example 5 : Write r.e. to denote a language L which accepts all the
strings which begin or end with either 00 or 11.
Solution :
R= L1 + L2
L1= The string which begin with 00 or 11.
L2= The string which begin with 00 or 11.
(00+11) (any number of 0’s or 1’s)
L1 = (00+11) (0+1)* L2 = = (00+11) (0+1)*
Hence
R= [(00+11)(0+1)*]+ [(00+11)(0+1)*]
Example 6 : Write r.e. which denotes a language L over the set ∑ =
{0} having even length of string..
Solution :
L= {ε,00,0000,000000,00000000,…………………...}
Hence
R= (00)*
Example 7 : Write r.e. which denotes a language L over the set ∑ =
{1} having odd length of string..
Solution :
L= {ε, 1, 111, 11111, 1111111,…………………}
Hence
R= 1(11)*
Example 8 : Construct regular expression for the language L over
the set ∑={a,b} in which the total number of a’s are divisible by 3.
Solution :
The total number of a’s are divisible by 3 . So the valid
strings can be
L={baaa,bababa,ababab,…………}
R= (b*a b* a b* a b*)*
Regular Grammar
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 most restricted form of grammar.
Type 3 should be in the given form only : |α |=1
✔ ∑ = {a,b}
✔ Initial state(q0) = A
✔ Final state(F) = B
The RLG corresponding to FA is
A ⇢ bB B ⇢ ∈/aB/bB
A ⇢ Bb B ⇢ ∈/Ba/Bb
It derives the language that contains all the strings which end with
b. i.e.
R = (ab + ba)*
Step 1: As the given expression, R, is of the form (Q)*, so we will create a single
initial state that will also be the final state, having self-loop labeled (ab + ba)
Step 2:
Step 3: As no operators are left, we can say that Fig is the required finite automata (NFA).
Problems :
Chomsky Hierarchy
T0 REL TM
T1 CSL LBA
T2 CFL PDA
T3 RL FA
Type 2: Context Free Grammar
Type-2 grammars generate the context-free languages. The language generated
by the grammar is recognized by a Pushdown automata.
In Type 2,
1. First of all it should be Type 1.
2. Left hand side of production can have only one variable.
| α | = 1.
Their is no restriction on ᵦ . |α |=1
For example,
S –> AB
A –> a ᵦ is ( V + T )*
B –> b
B –> b
Context-Free Grammar (CFG)
CFG stands for context-free grammar. It is is a formal grammar
which is used to generate all possible patterns of strings in a given
formal language. Context-free grammar G can be defined by four
tuples as:
G = (V, T, P, S)
Where,
G is the grammar, which consists of a set of the production rule. It
is used to generate the string of a language.
T is the final set of a terminal symbol. It is denoted by lower case
letters.
Context-Free Grammar (CFG)
V is the final set of a non-terminal symbol. It is denoted by capital
letters.
P is a set of production rules, which is used for replacing
non-terminals symbols(on the left side of the production) in a
string with other terminal or non-terminal symbols(on the right
side of the production).
S is the start symbol which is used to derive the string. We can
derive the string by repeatedly replacing a non-terminal by the
right-hand side of the production until all non-terminal have been
replaced by terminal symbols.
Context-Free Grammar (CFG)
Example 1:
Construct the CFG for the language having any number of a's over
the set ∑= {a}.
Solution:
As we know the regular expression for the above language is
r.e. = a*
Production rule for the Regular expression is as follows:
S → aS rule 1
S→ε rule 2
Now if we want to derive a string "aaaaaa", we can start with start
symbols.
Context-Free Grammar (CFG)
S
aS
aaS rule 1
aaaS rule 1
aaaaS rule 1
aaaaaS rule 1
aaaaaaS rule 1
aaaaaaε rule 2
aaaaaa
•It is a way to show how the derivation can be done to obtain some
string from a given set of production rules. It is also called as the
Parse tree.
b b
S
b b
a
Question 1 :
Draw a derivation tree for the string abaaba for the CFG given by
G where
ε S → ε is choosen
Question 2 :
Draw a derivation tree for the string aabbabba for the CFG given by G where
P={ S→aB|bA A →a|aS|bAA B → b|bS|aBB
Solution
S
aB S →aB
aaBB S →aBB
aabSB B →bS
aabbAB S →bA
aabbaB A →a
aabbabS B →bS
aabbabbA S →bA
aaBbabba A →a
S
a B
a B B
b S b S
b A b A
a a
Leftmost and Rightmost Derivation (LMD and RMD)
Pushdown Automata
• Pushdown automata is a way to implement a CFG in the
same way we design DFA for a regular grammar.
Finite control: The finite control has some pointer which points
the current symbol which is to be read.
Turnstile Notation:
Thus this process of popping 'b' will be repeated unless all the symbols
are read. Note that popping action occurs in state q1 only.
δ(q1, b, a) = (q1, ε)
After reading all b's, all the corresponding a's should get popped.
Hence when we read ε as input symbol then there should be nothing
in the stack. Hence the move will be:
δ(q1, ε, Z) = (q2, ε)
Where
PDA = ({q0, q1, q2}, {a, b}, {a, Z}, δ, q0, Z, {q2})
Push all 0's onto the stack on encountering first 0's. Then if we
read 1, just do nothing. Then read 0, and on each read of 0, pop
one 0 from the stack.
This scenario can be written in the ID form as:
δ(q0, 0, Z) = δ(q0, 0Z)
δ(q0, 0, 0) = δ(q0, 00)
δ(q0, 1, 0) = δ(q1, 0)
δ(q0, 1, 0) = δ(q1, 0)
δ(q1, 0, 0) = δ(q1, ε)
δ(q0, ε, Z) = δ(q2, Z) (ACCEPT state)
Now we will simulate this PDA for the input string "0011100".
δ(q0, 0011100, Z) ⊢ δ(q0, 011100, 0Z)
⊢ δ(q0, 11100, 00Z)
⊢ δ(q0, 1100, 00Z)
⊢ δ(q1, 100, 00Z)
⊢ δ(q1, 00, 00Z)
⊢ δ(q1, 0, 0Z)
⊢ δ(q1, ε, Z)
⊢ δ(q2, Z)
ACCEPT
Derivation is a sequence of production rules. It is used to get
input strings. During parsing, we have to take two decisions,
which are as follows –
Example
Production rules:
E=E+E rule1
E=E-E rule2
E=a|b rule3
Let the input be a-b+a
Now, when we perform the Left Most Derivation, the result will
be as follows −
E=E+E
E=E-E+E from rule2
E=a-E+E from rule3
E=a-b+E from rule3
E=a-b+a from rule3
❑ Pushdown automata is simply an NFA augmented with an "external stack memory". The addition
of stack is used to provide a last-in-first-out memory management capability to Pushdown
automata. Pushdown automata can store an unbounded amount of information on the stack. It
can access a limited amount of information on the stack. A PDA can push an element onto the top
of the stack and pop off an element from the top of the stack. To read an element into the stack,
the top elements must be popped off and are lost.
❑ A PDA is more powerful than FA. Any language which can be acceptable by FA can also be
acceptable by PDA. PDA also accepts a class of language which even cannot be accepted by FA.
Thus PDA is much more superior to FA.
Basic Structure of PDA
S6
S5
Qi Qj S4
S3
Qk
S2
S1
ZO
Stack
(Qj, I3, S6) (Qk, S7)
------- I1 I2 I3 I4 I5 I6 I7 I8 -------
Input Tap
S7
S6
S5
Qi Qj S4
S3
Qk
S2
S1
ZO
Stack
A PDA may or may not read an input symbol, but it has to read the
top of the stack in every transition.
A PDA can be formally described as a 7-tuple
(Q, Σ, Γ, δ, q0, Z0, F):
❑ Q is the finite number of states
❑ Σ is input alphabet
❑ Γ is stack symbols
❑ δ is the transition function:
: Q × (Σ∪{ε}) × Γ Q × Γ * (DPDA)
: Q × (Σ∪{ε}) × Γ set of finite subsets of Q× Γ * (NPDA)
❑ q0 is the initial state (q0 ∈ Q)
❑ Z0 is the initial stack top symbol (Z0 ∈ Γ)
❑ F is a set of accepting states (F ∈ Q)
The following diagram shows a transition in a PDA from a state q1 to state q2,
labeled as a,b → c :
This means at state q1, if we encounter an input string ‘a’ and top symbol of the stack is ‘b’, then
we pop ‘b’, push ‘c’ on top of the stack and move to state q2.
Instantaneous Description (ID)
Instantaneous Description (ID) is an informal notation of how a PDA “computes” a input
string and make a decision that string is accepted or rejected.
A ID is a triple (q, w, α), where:
1. q is the current state.
2. w is the remaining input.
3. α is the stack contents, top at the left.
Turnstile notation
⊢ sign is called a “turnstile notation” and represents one move.
⊢* sign represents a sequence of moves.
Eg- (p, b, T) ⊢ (q, w, α)
This implies that while taking a transition from state p to state q, the input symbol ‘b’ is
consumed, and the top of the stack ‘T’ is replaced by a new string ‘α’
Example : Define the pushdown automata for language {anbn | n > 0}
If at any time the control unit is in state q1, the input symbol read is a, and
the symbol on top of the stack is b, then one of two things can happen: (1)
the control unit goes into state q2 and the string cd replaces b on top of the
stack, or (2) the control unit goes into state q3 with the symbol b removed
from the top of the stack. In our notation we assume that the insertion of a
string into a stack is done symbol by symbol, starting at the right end of the
string.
Example of NPDA Execution
Example: NPDA for accepting the language L = {wwR | w ∈ (a,b)*}
L = {aa, bb, abba, aabbaa, abaaba, ......}
Let's describe to PDA of Language wwR
P=({q0 ,q1 ,qf }, {a,b}, {a,b,Z }, δ, q0 , Z , {qf}) and δ is as follows:
δ(q0, a, Z) |- (q0, aZ)
δ(q0, a, a) |- (q0, aa)
δ(q0, b, Z) |- (q0, bZ)
δ(q0, b, b) |- (q0, bb)
δ(q0, a, b) |- (q0, ab)
δ(q0, b, a) |- (q0, ba)
δ(q0, a, a) |- (q1, ∈)
δ(q0, b, b) |- (q1, ∈)
δ(q1, a, a) |- (q1, ∈)
δ(q1, b, b) |- (q1, ∈)
δ(q1, ∈, Z) |- (qf, Z)
Example: Construct a PDA accepting {anbman | m,n>=1} by empty store
Example: Construct PDA for L ∈{anb2n | n>=1} by empty store
Example: Construct PDA for L ∈{ambm cn | m,n>=1} by empty store
Exercise-1: Construct PDA for L ∈{{a,b}*|#a=#b}
Exercise-2: Construct a PDA for language
n m m n
L = {0 1 2 3 | n>=1, m>=1}
Exercise-3: Construct a PDA for language
n m
L = {0 1 | n >= 1, m >= 1, m > n+2}
R
Exercise-4: Construct PDA for L ∈WCW |W ∈{a,b}*}
CFG to PDA Conversion
.S → 0S1 | A
.A → 1A0 | S | ε
Solution:
The CFG can be first simplified by eliminating unit productions:
.S → 0S1 | 1S0 | ε
Now we will convert this CFG to GNF:
.S → 0SX | 1SY | ε
.X → 1
.Y → 0
The PDA can be:
R1: δ(q, ε, S) = {(q, 0SX) | (q, 1SY) | (q, ε)}
R2: δ(q, ε, X) = {(q, 1)}
R3: δ(q, ε, Y) = {(q, 0)}
R4: δ(q, 0, 0) = {(q, ε)}
R5: δ(q, 1, 1) = {(q, ε)}
Example 2:
Construct PDA for the given CFG, and test whether 0104 is acceptable by this PDA.
1.S → 0BB
2.B → 0S | 1S | 0
Solution:
The PDA can be given as:
1.A = {(q), (0, 1), (S, B, 0, 1), δ, q, S, ?}
The production rule δ can be:
R1: δ(q, ε, S) = {(q, 0BB)} R2: δ(q, ε, B) = {(q, 0S) | (q, 1S) | (q, 0)}
R3: δ(q, 0, 0) = {(q, ε)} R4: δ(q, 1, 1) = {(q, ε)}
Testing 0104 i.e. 010000 against PDA:
1.δ(q, ε010000, S) ⊢ δ(q, 010000, 0BB)
2. ⊢ δ(q, ε10000, BB) R1
3. ⊢ δ(q, 10000,1SB) R3
4. ⊢ δ(q, ε0000, SB) R2
5. ⊢ δ(q, 0000, 0BBB) R1
6. ⊢ δ(q, ε000, BBB) R3
7. ⊢ δ(q, 000, 0BB) R2
8. ⊢ δ(q, ε00, BB) R3
9. ⊢ δ(q, 00, 0B) R2
10. ⊢ δ(q, ε0, B) R3
11. ⊢ δ(q, 0, 0) R2
12. ⊢ δ(q, ε) R3
13. ACCEPT
4
Thus 010 is accepted by the PDA.
Example 3:
Draw a PDA for the CFG given below:
1.S → aSb
2.S → a | b | ε
Solution:
The PDA can be given as:
1.P = {(q), (a, b), (S, a, b, z0), δ, q, z0, q}
The mapping function δ will be:
R1: δ(q, ε, S) = {(q, aSb)} R2: δ(q, ε, S) = {(q, a) | (q, b) | (q, ε)}
R3: δ(q, a, a) = {(q, ε)} R4: δ(q, b, b) = {(q, ε)} R5: δ(q, ε, z0) = {(q, ε)}
Simulation: Consider the string aaabb
1.δ(q, εaaabb, S) ⊢ δ(q, aaabb, aSb) R3
2. ⊢ δ(q, εaabb, Sb) R1
3. ⊢ δ(q, aabb, aSbb) R3
4. ⊢ δ(q, εabb, Sbb) R2
5. ⊢ δ(q, abb, abb) R3
6. ⊢ δ(q, bb, bb) R4
7. ⊢ δ(q, b, b) R4
8. ⊢ δ(q, ε, z0) R5
9. ⊢ δ(q, ε)
0. ACCEPT
Context free grammar to Pushdown automata conversion-1
Example: For
Start M=({q},{a,b},{S,A2,A3,Z0}, δ,q,S, Ø)
G=(V,T,P,S) Symbol
δ(q, ε,z0)={(q,S)}
P: S aA2|aA3 δ(q,a,S)={(q,A2),(q,A3)}
A2 aA2A3|aA3 δ(q,a,A2)={(q,A2A3),(q,A3)}
A3 b δ(q,b,A3)={(q, ε)}
δ(q, ε,z0)={(q, ε)} Ending
Example: S aSbb|a
Convert it into GNF
S aSA|a
A bB
Bb
The corresponding automaton will have three states {q0, q1, q2}, with initial state q0 and final
state q2. First, the start symbol S is put on the stack by
δ(q1, ε, z0)=(q2, ε)
Example
1. S aSbb | aab
2. S aSSS | ab
3. S aABB | aAA
A aBB | a
B bBB | A
Find an npda with two states for the language L = {anbn+1 | n ≥ 0}.
Pumping Lemma for CFG
1. x = uvwyz and
2. |vwy| ≤ n and
3. |vy| ≥ 1 and
4. For all i ≥ 0, uviwyiz is in L
Closure properties of CFL’s
If L1 and L2 are two context free languages, their union L1 ∪ L2 will also be context free. For
example,
L1 = { anbncm | m >= 0 and n >= 0 } and
L2 = { anbmcm | n >= 0 and m >= 0 }
L3 = L1 ∪ L2 = { anbncm ∪ anbmcm | n >= 0, m >= 0 } is also context free.
L1 says number of a’s should be equal to number of b’s and L2 says number of b’s should be
equal to number of c’s. Their union says either of two conditions to be true. So it is also
context free language.
Concatenation
If L1 and If L2 are two context free languages, their concatenation L1.L2 will also be context
free. For example,
L1 = { anbn | n >= 0 } and L2 = { cmdm | m >= 0 }
L3 = L1.L2 = { anbncmdm | m >= 0 and n >= 0} is also context free.
L1 says number of a’s should be equal to number of b’s and L2 says number of c’s should be
equal to number of d’s. Their concatenation says first number of a’s should be equal to
number of b’s, then number of c’s should be equal to number of d’s. So, we can create a
PDA which will first push for a’s, pop for b’s, push for c’s then pop for d’s. So it can be
accepted by pushdown automata, hence context free.
Star Operation
If L1 is context free, its Kleene closure L1* will also be context free. For example,
L1 = { anbn | n >= 0 }
L1* = { anbn | n >= 0 }* is also context free.
Note :So CFL are closed under Kleen Closure.
Intersection
If L1 and If L2 are two context free languages, their intersection L1 ∩ L2 need not be context
free. For example,
L1 = { anbncm | n >= 0 and m >= 0 } and
L2 = (ambncn | n >= 0 and m >= 0 }
L3 = L1 ∩ L2 = { anbncn | n >= 0 } need not be context free.
Complement
complementation of context free language L1 which is ∑* – L1, need not be context free.
The intersection of a context-free language and
a regular language is a context-free language
L1 context free and L2 regular then
L1∩L2 context-free
Deterministic Context-free Languages
Deterministic CFL are subset of CFL which can be recognized by Deterministic PDA.
Deterministic PDA has only one move from a given state and input symbol, i.e., it do not have
choice. For a language to be DCFL it should be clear when to PUSh or POP.
For example, L1= { anbncm | m >= 0 and n >= 0} is a DCFL because for a’s, we can push on stack
and for b’s we can pop. It can be recognized by Deterministic PDA. Similarly for L2= { anbmcm |
m >= 0 and n >= 0}.
On the other hand, L3 = { anbncm ∪ anbmcm | n >= 0, m >= 0 } cannot be recognized by DPDA
because either number of a’s and b’s can be equal or either number of b’s and c’s can be
equal. So, it can only be implemented by NPDA. Thus, it is CFL but not DCFL.
Note : DCFL are closed only under complementation and Inverse Homomorphism.
Decidable Problems Concerning Context-Free Languages
Membership problem: For CFG G1, find if w ∈ L(G) ?
The membership algorithm is: Parser, That is, if we are able to obtain a parse-tree for given word w, then
w∈L(G) else not.
Algorithms: Parsers
❑ Exhaustive search parser
❑ CYK parsing algorithm
C→ cBS
B →bB | bb
A →aCb
S → AB
Q&A
THANKS
Acropolis Institute of Technology &
Research, Indore
www.acropolis.in
(Theory of computation)
(TOC)
Finite Program
….110110 Input port Output port 010λ00…..
State qadv
This definition implies only that there exists a Turing machine M, such that,
for every w ∈ L,
with qf a final state. The definition says nothing about what happens for w
not in L; it may be that the machine halts in a non-final state or that it never
halts and goes into an infinite loop. We can be more demanding and ask that
the machine tell us whether or not any given input is in its language.
Recursive Languages and Recursively Enumerable (RE) Languages
The Halting Problem
?
Deterministic Turing Machine (DTM)
…….. B B 0 1 1 0 0 B B ……..
Finite
Control
❖Two-way, infinite tape, broken into cells, each containing one symbol.
❖Two-way, read/write tape head.
❖An input string is placed on the tape, padded to the left and right infinitely with blanks, read/write
head is positioned at the left end of input string.
❖Finite control, i.e., a program, containing the position of the read head, current symbol being
scanned, and the current state.
❖In one move, depending on the current state and the current symbol being scanned, the TM 1)
changes state, 2) prints a symbol over the cell being scanned, and 3) moves its’ tape head one cell
left or right.
❖Many modifications possible, but Church-Turing declares equivalence of all.
Formal Definition of a DTM
❖ A DTM is a seven-tuple:
M = (Q, Σ, Γ, δ, q0, B, F)
QA finite set of states
Σ A finite input alphabet, which is a subset of Γ– {B}
Γ A finite tape alphabet, which is a strict superset of Σ
B A distinguished blank symbol, which is in Γ
q0 The initial/starting state, q0 is in Q
F A set of final/accepting states, which is a subset of Q
δ A next-move function, which is a mapping (i.e., may be undefined) from
Q x Γ Q x Γ x {L,R}
Intuitively, δ(q,s) specifies the next state, symbol to be written, and the direction of tape head
movement by M after reading symbol s while in state q.
δ(p,X)=(q,Y,L)
p q
. . U X V . . . . U Y V . .
Example of Turing machine:
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.
Accepted Language and Decided Language
A TM accepts a language if it enters into a final state for any input string w. A
language is recursively enumerable (generated by Type-0 grammar) if it is
accepted by a Turing machine.
A TM decides a language if it accepts it and enters into a rejecting state for any
input not in the language. A language is recursive if it is decided by a Turing
machine.
There may be some cases where a TM does not stop. Such TM accepts the
language, but it does not decide it.
Designing a Turing Machine
The basic guidelines of designing a Turing machine have been explained below
with the help of a couple of examples.
Example
Design a TM to recognize all strings consisting of an odd number of a’s.
Solution
The Turing machine M can be constructed by the following moves:
❑ Let q1 be the initial state.
❑ If M is in q1; on scanning α, it enters the state q2 and writes B (blank).
❑ If M is in q2; on scanning α, it enters the state q1 and writes B (blank).
❑ From the above moves, we can see that M enters the state q1 if it scans an even number
of a’s, and it enters the state q2 if it scans an odd number of a’s. Hence q2 is the only
accepting state.
Hence,
M = {{q1, q2}, {a}, {1, B}, δ, q1, B, {q2}}
Accept
n n
TM for {0 1 | n≥1}
n n
TM for {0 1 | n≥0}
Configuration Graph of TM
Prob. : M accepts x iff ∃ of a computation path in GM,x from the start configuration to
accept configuration.
Properties of Configuration Graph
DTM
NDTM
Theorem:
❑ Concatenation: If L1 and If L2 are two recursive languages, their concatenation L1.L2 will
also be recursive.
For Example:
L1= {anbncn|n>=0}
L2= {dmemfm|m>=0}
L3= L1.L2 = {anbncndm emfm|m>=0 and n>=0} is also recursive.
L1 says n no. of a’s followed by n no. of b’s followed by n no. of c’s. L2 says m no. of d’s
followed by m no. of e’s followed by m no. of f’s. Their concatenation first matches no. of a’s,
b’s and c’s and then matches no. of d’s, e’s and f’s. So it can be decided by TM.
❑ Kleene Closure: If L1is recursive, its kleene closure L1* will also be recursive.
For Example: L1= {anbncn|n>=0}
L1*= { anbncn||n>=0}* is also recursive.
A multi-tape Turing machine is a Turing machine with several tapes, each with its own
independently controlled read-write head
The formal definition of a multi-tape Turing machine goes beyond Definition of Turing
Machine, since it requires a modified transition function. Typically, we define an n-tape
machine by M = (Q, Σ, Γ, B, δ, q0, F),
where Q,Σ,Γ,B,q0,F are as in Tuple of Turing Machine, but is
δ : Q × Γ n → Q × Γ n× {L, R} n
specifies what happens on all the tapes. For example, if n = 2, with a current configuration,
then
δ (q0, a, e) = (q1, x, y, L, R)
Multi-tape TM to Single Tap TM
y1
y2
yk
Single Tap TM
y1 y2 … yk
Challenges on Multi-tape TM to Single Tap TM
y1 y2 … yk
Multidimensional Turing Machines
A multidimensional Turing machine is one in which the tape can be viewed as
extending infinitely in more than one dimension. A diagram of a
two-dimensional Turing machine is shown in Figure
Queue Machines = TM
Nondeterministic Turing Machines
δ : Q × Γ → 2Q×Γ×{L, R}.
and
they execute
only one program
Attributes:
• Reprogrammable machine
Description of transitions of M
Input string of M
Tape 1
Three tapes
Description of M
Universal Tape 2
Turing
Machine
Tape Contents of M
Tape 3
State of M
Tape 1
Description of M
Symbols
:
Encoding:
State Encoding
States:
Encoding:
Encoding:
Transition Encoding
Transition:
Encoding:
separator
Turing Machine Encoding
Transitions:
Encoding:
separator
Tape 1 contents of Universal Turing Machine:
binary encoding
of the simulated machine M
Tape 1
A Turing Machine is described with a binary string of 0’s and 1’s
Therefore:
The set of Turing machines
forms a language:
00100100101111,
(Turing Machine 2)
111010011110010101,
…… } ……
Church-Turing Thesis
Turing Paper
J. B. Rosser (1939) addresses the notion of "effective computability" as follows: "Clearly the existence of CC and RC
(Church's and Rosser's proofs) presupposes a precise definition of 'effective'. 'Effective method' is here used in the rather
special sense of a method each step of which is precisely predetermined and which is certain to produce the answer in a
finite number of steps“. Thus the adverb-adjective "effective" is used in a sense of "1a: producing a decided, decisive, or
desired effect", and "capable of producing a result“.
In the following, the words "effectively calculable" will mean "produced by any intuitively 'effective' means whatsoever" and
"effectively computable" will mean "produced by a Turing-machine or equivalent mechanical device". Turing's "definitions"
Statement in Church's and Turing's words
given in a footnote in his 1939 Ph.D. thesis Systems of Logic Based on Ordinals, supervised by Church, are virtually the
same:
" We shall use the expression 'computable function' to mean a function calculable by a machine, and let 'effectively
calculable' refer to the intuitive idea without particular identification with any one of these definitions."
The thesis can be stated as follows:
Every effectively calculable function is a computable function.
It states that a function on the natural numbers is computable by a human being following
an algorithm, ignoring resource limitations, if and only if it is computable by a Turing
machine.
The thesis is named after American mathematician Alonzo Church and the British
mathematician Alan Turing.
– Turing also showed that his A-machines were equivalent to Lambda Calculus of
Church.
– So, can a Turing machine do everything? In other words are there algorithms to solve
every question. Godel’s incompleteness result asserts otherwise.
– But Turing also worked on ideas and concepts that later made profound
impact in AI.
Theory of computation
Unit 5 Complexity Theory
Complexity Theory
Decision Based Problem:- A problem is called a decision problem if its
output is a simple “yes” or “no”. We will phrase many optimization
problems as decision problems. For example, Greedy method, D.P., given
a graph G= (V, E) if there exists any Hamiltonian cycle.
Resources
?
input, or will run forever). Alan Turing proved that general algorithm to solve
the halting problem for all possible program-input pairs cannot exist. A key
part of the proof is, Turing machine was used as a mathematical definition of a
computer and program.
Status of NP Complete problems is another failure story, NP
complete problems are problems whose status is unknown. No
polynomial time algorithm has yet been discovered for any NP
complete problem, nor has anybody yet been able to prove that no
polynomial-time algorithm exist for any of them. The interesting part
is, if any one of the NP complete problems can be solved in
polynomial time, then all of them can be solved.
P class Problem: -
The set of decision-based problems come into the division of P Problems who
can be solved or produced an output within polynomial time. P problems being
easy to solve.
The set of all decision-based problems came into the division of NP Problems
who can't be solved or produced an output within polynomial time but verified
in the polynomial time. NP class contains P class as a subset. NP problems
being hard to solve.
❑ 0/1 Knepsack Problem O(2n)
❑ Traveling Salesman Problem O(2n)
❑ Graph Colouring Problem O(2n)
❑ Scheduling Problem O(2n)
❑ So-Do-Ko Problem O(2n)
❑ ..
❑ ..
Co-NP class Problem: -
The set of all decision-based problems having answer “NO” and verified in
the polynomial time came into the Co-NP class Problems.
Example:
Integer Factorization:
An example of a problem that is known to belong to both NP and co-NP (but not known to be
in P) is integer factorization: given positive integers m and n, determine if m has a factor less
than n and greater than one. Membership in NP is clear; if m does have such a factor, then the
factor itself is a certificate. Membership in co-NP is also straightforward: one can just list the
prime factors of m, all greater or equal to n, which the verifier can confirm to be valid by
multiplication and the AKS primality test. It is presently not known whether there is a
polynomial-time algorithm for factorization, equivalently that integer factorization is in P, and
hence this example is interesting as one of the most natural problems known to be in NP and
co-NP but not known to be in P.
Is P = NP ?
The idea is to find a transformation from L1 to L2 so that the algorithm A2 can be part of an
algorithm A1 to solve L1.
Problem X Polynomial
Problem Y (Salutation in P)
Hard
Mapping in P
Cost of solving X = Cost of solving Y + Reduction Time
Reduction
A B
YES
Instance α of A Poly. Time Instance β of B Poly. Time
Reduction Algorithm to
Algorithm decide B NO
Some one asked to B that, can you touch moon from earth.
B reply, If any person touch moon from earth then me too.
A problem is in NP-complete, if
1. It is in NP
2. It is NP-hard
Reduction in NP-Complete
Co-
NP
Steps of proving NP-Complete:
Concept: - In 3CNF SAT, you have at least 3 clauses, and in clauses, you will
have almost 3 literals or constants
Such as (X+Y+Z)(X+Y+Z)(X+Y+Z)
You can define as (XvYvZ) ˄ (XvYvZ) ˄ (XvYvZ)
V =OR operator
˄ =AND operator
These all the following points need to be considered in 3CNF SAT.
Eulerian Cycle Problem (ECP)
Eulerian Cycle Problem (ECP)
Hamiltonian Cycle Problem
A Hamiltonian cycle in a graph is a cycle that visits each vertex exactly once
Problem Statement
Given A directed graph G = (V,E)