0% found this document useful (0 votes)
19 views346 pages

TOC Syllabus

hgghgyjuffh

Uploaded by

Riya Singh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
19 views346 pages

TOC Syllabus

hgghgyjuffh

Uploaded by

Riya Singh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 346

Automata theory

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:
∑ = {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.....}.

•A string with zero occurrences of symbols is known as an empty


string. It is represented by ε.

•The number of symbols in a string w is called the length of a


string. It is denoted by |w|.

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

•Finite automata are used to recognize patterns.


•It takes the string of symbol as input and changes its state
accordingly. When the desired symbol is found, then the
transition occurs.
•At the time of transition, the automata can either move to the
next state or stay in the same state.
•Finite automata have two states, Accept state or Reject state.
When the input string is processed successfully, and the
automata reached its final state, then it will accept.
Formal Definition of FA

A finite automaton is a collection of 5-tuple (Q, ∑, δ, q0, F),

where:

Q: finite set of states

∑: finite set of the input symbol

q0: initial state

F: final state

δ: Transition function
Finite Automata Model:

Finite automata can be represented by input tape and finite


control.
Input tape: It is a linear tape having some number of cells. Each
input symbol is placed in each cell.
Finite control: The finite control decides the next state on
receiving particular input from input tape. The tape reader reads
the cells one by one from left to right, and at a time only one input
symbol is read.
Types of Automata:
There are two types of finite automata:

•DFA(deterministic finite automata)


•NFA(non-deterministic finite automata)
1. DFA
DFA refers to deterministic finite automata. Deterministic refers to the
uniqueness of the computation. In the DFA, the machine goes to one state
only for a particular input character. DFA does not accept the null move.

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.

Some important points about DFA and NFA:


Every DFA is NFA, but NFA is not DFA.
There can be multiple final states in both NFA and DFA.
Transition Diagram

A transition diagram or state transition diagram is a directed


graph which can be constructed as follows:

•There is a node for each state in Q, which is represented by the


circle.
•There is a directed edge from node q to node p labeled a if δ(q, a)
= p.
•In the start state, there is an arrow with no source.
•Accepting states or final states are indicating by a double circle.

Some Notations that are used in the transition diagram:


There is a description of how a DFA operates:

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:

NFA with ∑ = {0, 1} accepts all strings starting with 1.

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

The transition table is basically a tabular representation of the


transition function. It takes two arguments (a state and a symbol) and
returns a state (the "next state").

A transition table is represented by the following things:

•Columns correspond to input symbols.


•Rows correspond to states.
•Entries correspond to the next state.
•The start state is denoted by an arrow with no source.
•The accept state is denoted by a double circle .
Example 1

Solution:
Transition table of given DFA is as follows:

Present State Next state for Input 0 Next State of Input 1

→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.

•The arrow marked to q0 indicates that it is a start state and circle


marked to q2 indicates that it is a final state.
Example 2

Solution:
Transition table of given NFA is as follows:

Present State Next state for Input 0 Next State of Input 1


→q0 q0 q1
q1 q1, q2 q2
q2 q1 q3
*q3 q2 q2
Explanation:
•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 q0 and on input 1 the next
state will be q1.
•In the second row, when the current state is q1, on input 0 the next
state will be either q1 or q2, 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 q1, and on 1 input the next state will be q3.
•In the fourth row, when the current state is q3 on input 0, the next
state will be q2, and on 1 input the next state will be q2.
DFA (Deterministic finite automata)

•DFA refers to deterministic finite automata. Deterministic refers to


the uniqueness of the computation. The finite automata are called
deterministic finite automata if the machine is read an input string
one symbol at a time.
•In DFA, there is only one path for specific input from the current
state to the next state.
•DFA does not accept the null move, i.e., the DFA cannot change state
without any input character.
•DFA can contain multiple final states. It is used in Lexical Analysis in
Compiler.
In the following diagram, we can see that from state q0 for input
a, there is only one path which is going to q1. Similarly, from q0,
there is only one path for input b going to q2.
Formal Definition of DFA

A DFA is a collection of 5-tuples same as we described in the definition


of FA.

•Q: finite set of states


•∑: finite set of the input symbol
•q0: initial state
•F: final state
•δ: Transition function

Transition function can be defined as:

δ: 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:

DFA with ∑ = {0, 1} accepts all starting with 0.

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:

DFA with ∑ = {0, 1} accepts all ending with 0.


Solution:

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:

When three consecutive 1's occur the DFA will be:

Here two consecutive 1's or single 1 is acceptable, hence

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)

•NFA stands for non-deterministic finite automata. It is easy to construct an

NFA than DFA for a given regular language.

•The finite automata are called NFA when there exist many paths for

specific input from the current state to the next state.

•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

exceptions, it contains multiple next states, and it contains ε transition.


In the following image, we can see that from state q0 for input a, there
are two next states q1 and q2, similarly, from q0 for input b, the next
states are q0 and q1. Thus it is not fixed or determined that with a
particular input where to go next. Hence this FA is called
non-deterministic finite automata.
Formal definition of NFA:

NFA also has five states same as DFA, but with different
transition function, as shown follows:

δ: Q x ∑ →2Q

where,

Q: finite set of states


∑: finite set of the input symbol
q0: initial state
F: final state
δ: Transition function
Graphical Representation of an NFA

An NFA can be represented by digraphs called state diagram. In which:

•The state is represented by vertices.


•The arc labeled with an input character show the transitions.
•The initial state is marked with an arrow.
•The final state is denoted by the double circle
Example 1:
Q = {q0, q1, q2} ∑ = {0, 1} q0 = {q0} F = {q2}

Solution:
Transition diagram:

Present State Next state for Input 0 Next State of Input 1


→q0 q0, q1 q1
q1 q2 q0
*q2 q2 q1, q2
Example 2:

NFA with ∑ = {0, 1} accepts all strings with 01.

Present State Next state for Next State of


Input 0 Input 1
→q0 q1 ε

q1 ε q2

*q2 q2 q2
Example 3:

NFA with ∑ = {0, 1} and accept all string of length atleast 2.

Present State Next state for Input 0 Next State of Input 1


→q0 q1 q1
q1 q2 q2
*q2 ε ε
Example 1:

Design a NFA for the transition table as given below:

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:

Hence, NFA would be:


Example 3:
Design an NFA with ∑ = {0, 1} in which double '1' is followed
by double '0'.

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

δ'(q, a) = ε-closure(δ(δ^(q, ε),a))

Where (δ^(q, ε)) = ε-closure (q)


Example:
Convert the following NFA with ε to NFA without ε.

Solutions: We will first obtain ε-closures of q0, q1 and q2 as


follows:

ε-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:

δ'(q0, a) = ε-closure(δ(δ^(q0, ε),a))


= ε-closure(δ(ε-closure(q0),a))
= ε-closure(δ(q0, a))
= ε-closure(q1)
= {q1, q2}

δ'(q0, b) = ε-closure(δ(δ^(q0, ε),b))


= ε-closure(δ(ε-closure(q0),b))
= ε-closure(δ(q0, b))

δ'(q1, a) = ε-closure(δ(δ^(q1, ε),a))
= ε-closure(δ(ε-closure(q1),a))
= ε-closure(δ(q1, q2), a)
= ε-closure(δ(q1, a) ∪ δ(q2, a))
= ε-closure(Ф ∪ Ф)

δ'(q1, b) = ε-closure(δ(δ^(q1, ε),b))


= ε-closure(δ(ε-closure(q1),b))
= ε-closure(δ(q1, q2), b)
= ε-closure(δ(q1, b) ∪ δ(q2, b))
= ε-closure(Ф ∪ q2)
= {q2}
δ'(q2, a) = ε-closure(δ(δ^(q2, ε),a))
= ε-closure(δ(ε-closure(q2),a))
= ε-closure(δ(q2, a))
= ε-closure(Ф)

δ'(q2, b) = ε-closure(δ(δ^(q2, ε),b))


= ε-closure(δ(ε-closure(q2),b))
= ε-closure(δ(q2, b))
= ε-closure(q2)
= {q2}
Now we will summarize all the computed δ' transitions:
1.δ'(q0, a) = {q0, q1}
2.δ'(q0, b) = Ф
3.δ'(q1, a) = Ф
4.δ'(q1, b) = {q2}
5.δ'(q2, a) = Ф
6.δ'(q2, b) = {q2}
The transition table can be:

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 ε

Where q2 is FINAL STATE.

0 1 2

Ɛ Ɛ
qo q1 q2
Output :

State/ Input 0 1 2

*q0 {q0,q1,q2} {q1,q2} {q2}

*q1 Ǿ {q1,q2} {q2}

*q2 Ǿ Ǿ {q2}
Conversion from NFA with ε to DFA

Non-deterministic finite automata(NFA) is a finite automata where for some


cases when a specific input is given to the current state, the machine goes to
multiple states or more than 1 states. It can contain ε move. It can be
represented as M = { Q, ∑, δ, q0, F}.

Where

Q: finite set of states


∑: finite set of the input symbol
q0: initial state
F: final state
δ: Transition function

NFA with ∈ move: If any FA contains ε transaction or move, the finite


automata is called NFA with ∈ move.
ε-closure: ε-closure for a given state A means a set of states which can be
reached from the state A with only ε(null) move including the state A itself.
Steps for converting NFA with ε to DFA:

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 −

Q is a finite set of states.


∑ is a finite set of symbols called the input alphabet.
O is a finite set of symbols called the output alphabet.
δ is the input transition function where δ: Q × ∑ → Q
X is the output transition function where X: Q × ∑ → O
q0 is the initial state from where any input is processed (q0 ∈ Q).
The state table of a Mealy Machine is shown below −

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

Moore machine is an FSM whose outputs depend on only the


present state.

A Moore machine can be described by a 6 tuple (Q, ∑, O, δ, X, q0)


where −

Q is a finite set of states.


∑ is a finite set of symbols called the input alphabet.
O is a finite set of symbols called the output alphabet.
δ is the input transition function where δ: Q × ∑ → Q
X is the output transition function where X: Q → O
q0 is the initial state from where any input is processed (q0 ∈ Q).
The state table of a Moore Machine is shown below −

Present Next State


Output
state Input = 0 Input = 1
→a b c x2
b b d x1
c c d x2
d d d x3
The state diagram of the above Moore Machine is −
Mealy Machine vs. Moore Machine

Mealy Machine Moore Machine


Output depends both upon the present Output depends only upon the present
state and the present input state.

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.

Mealy machines react faster to inputs. In Moore machines, more logic is


They generally react in the same clock required to decode the outputs resulting
cycle. in more circuit delays. They generally
react one clock cycle later.
Moore Machine to Mealy Machine

Input − Moore Machine


Output − Mealy Machine
Step 1 − Take a blank Mealy Machine transition table format.
Step 2 − Copy all the Moore Machine transition states into this
table format.
Step 3 − Check the present states and their corresponding
outputs in the Moore Machine state table; if for a state
Qi output is m, copy it into the output columns of the Mealy
Machine state table wherever Qi appears in the next state.
Example
Let us consider the following Moore machine −

Present Next State


Output
State a=0 a=1
→a d b 1
b a d 0
c c c 0
d b a 1
Step 1 & 2 −

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 1 − Calculate the number of different outputs for each state


(Qi) that are available in the state table of the Mealy 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.......

Step 3 − If the output of the initial state is 1, insert a new initial


state at the beginning which gives 0 output.
Example
Let us consider the following Mealy Machine −

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 −

•N 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 N ∪ ∑ and least one symbol of α belongs to
N.
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
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
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 → ε


The set of all strings that can be derived from a grammar is
said to be the language generated from that grammar.

A language generated by a grammar G is a subset formally


defined by

L(G)= {W|W ∈ ∑*}

If L(G1) = L(G2), the Grammar G1 is equivalent to the


Grammar G2.
Example

If there is a grammar
G:
V = {S, A, B}
T = {a, b}
P = {S → AB, A → a, B → b}

Here S produces AB, and we can replace A by a, and B by b.


Here, the only accepted string is ab, i.e.,
L(G) = {ab}
Example
Suppose we have the following grammar −
G:
V = {S, A, B}
T = {a, b}
P = {S → AB, A → aA|a, B → bB|b}
The language generated by this grammar −
L(G) = {ab, a2b, ab2, a2b2, ………}
= {am bn | m ≥ 1 and n ≥ 1}
Chomsky Hierarchy
Chomsky Hierarchy
•This Chomsky Hierarchy is the complete framework of TOC.

•Noom Chomsky introduce in 1956 this Chomsky hierarchy .

•Noom Chomsky is not only computer expert also he is a


politician, philosopher, scientist and many more field .

•Formal Language which is used in Computer Science is define


by using 4 touple introduce by Noom Chomiskey.
Grammar
Grammar G can be defined by four tuples as:

G= (V, T, P, S)

Where,

G - describes the grammar

V - describes a finite set of non-terminal symbols

T - describes a finite set of terminal symbols.

P - describes a set of production rules

S - is the start symbol.


Chomsky Hierarchy
According to Chomsky hierarchy, grammars is divided

into 4 types:

•Type 0 known as unrestricted grammar.

•Type 1 known as context sensitive grammar.

•Type 2 known as context free grammar.

•Type 3 known as Regular Grammar.


Type 0: Unrestricted Grammar
Type-0 grammars or unrestricted grammars include all formal grammars.
Type 0 grammar language are recognized by turing machine. These languages
are also known as the Recursively Enumerable languages. Grammar

Production in the form of α→ ᵦα is ( V + T)* V ( V + T)*


V : Non Terminals
T : Terminals.

ᵦ is ( V + T )*.

In type 0 there must be at least one variable on 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 the context-sensitive languages. The
language generated by the grammar are recognized by the Linear Bound
Automata.

In Type 1 :

I. First of all Type 1 grammar should be Type 0.

II. Count of symbol in α is Less then or equal to ᵦ


| α | <= | ᵦ| α is ( V + T)* V ( V + T)*

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

V –> VT */ T* (left-regular grammar) ᵦ is T OR VT in case of


(or) Left regular Grammer
V –> T*V /T* (right-regular grammar)

for example (Left) for example (Right) ᵦ is T OR TV in case of


S –> a S –> a Right regular Grammer
S-> Ababb S-> babbA
S-> abc S-> abc
S-> Aaababa S-> aababaA
Chomsky Hierarchy

Grammar Language Machine

T0 REL TM
T1 CSL LBA
T2 CFL PDA

T3 RL FA
Closure Properties
In an automata theory, there are different closure properties for

regular languages. They are as follows −

1. Union

2. Intersection

3. concatenation

4. Kleene closure

5. Complement
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


Regular Set
Regular sets are the which are accepted by finite automata.

Example : L = {ε,00,0000,000000……}

i.e. the set of even number of zeros.

We can represent this set by a finite automata as shown in figure.

M=({q0,q1}, {0}, δ, q0, q0)

The given set L is said to be a regular set because it is represented by finite

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 −

•ε 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}
Regular Regular Set
Expressions
(0 + 10*) L = { 0, 1, 10, 100, 1000, 10000, … }
(0*10*) L = {1, 01, 10, 010, 0010, …}
(0 + ε)(1 + ε) L = {ε, 0, 1, 01}
(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, …………..}
(11)* Set consisting of even number of 1’s including empty string,
So L= {ε, 11, 1111, 111111, ……….}
(aa)*(bb)*b Set of strings consisting of even number of a’s followed by
odd number of b’s , so L = {b, aab, aabbb, aabbbbb, aaaab,
aaaabbb, …………..}
(aa + ab + ba + String of a’s and b’s of even length can be obtained by
bb)* concatenating any combination of the strings aa, ab, ba and
bb including null, so L = {aa, ab, ba, bb, aaab, aaba,
…………..}
Example 1 : Write the regular expression for the
language accepting all combinations of a’s over the set ∑
= {a}

R= a*

That is Kleen Closure of a


Example 2 : Design the Reguler Expression for the language
accepting all combinations of a’s except the null string over ∑ = {a}

Solution : L={a,aa,aaa,aaaa,aaaaa,………….}
The set indicated that there is no NUMM string, So we

can denote r.e. as R= a+


Example 3 : Design the Reguler Expression for the language
containing all the strings containing any number of a’s and b’s.

Solution : This will give the set as L = { ε


,a,aa,ab,b,,ba,bab,abab…any combination of a and b}

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

V –> VT */ T* (left-regular grammar) ᵦ is T OR VT in case of


(or) Left regular Grammer
V –> T*V /T* (right-regular grammar)

for example (Left) for example (Right) ᵦ is T OR TV in case of


S –> a S –> a Right regular Grammer
S-> Ababb S-> babbA
S-> abc S-> abc
S-> Aaababa S-> aababaA
The language generated by type-3 grammar is a regular
language, for which a FA can be designed. The FA can
also be converted into type-3 grammar.

Example: FA for accepting strings that start with b

✔ ∑ = {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).
Automata to Regular Expression
Rules
Example : Construct Finite Automata for the regular expression,

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:

A. As the least precedency operator in the expression is a


union(+). So we will introduce parallel edges (parallel self-loops
here) for ‘ab’ and ‘ba’,.
B. Now we have two labels with concatenation operators (no operator
mentioned between two variables is concatenation), so we remove them one
by one by introducing new states, q1 and q2 .

Step 3: As no operators are left, we can say that Fig is the required finite automata (NFA).
Problems :
Chomsky Hierarchy

Grammar Language Machine

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

The r.e. = a* can generate a set of string {ε, a, aa, aaa,.....}. We


can have a null string because S is a start symbol and rule 2
gives S → ε.
Context-Free Grammar (CFG)
Example 2:

Construct a CFG for the regular expression (0+1)*


Solution:
The CFG can be given by,
Production rule (P):
S → 0S | 1S
S→ε
The rules are in the combination of 0's and 1's with the start
symbol. Since (0+1)* indicates {ε, 0, 1, 01, 10, 00, 11, ....}. In this
set, ε is a string, so in the rule, we can set the rule S → ε.
Context-Free Grammar (CFG)
Example 3: Construct a grammar for the language containing string
of at least two a’s.
Solution:
Let G = (V,T,P,S)
V={S,A} T={a,b} P = {S→Aa Aa A
A→Aa | bA |ε}
The rule S→AaAaA is something in which the two a’s are
maintained since at lest to a’s should be there in the strings. And
A→aA | bA |ε gives any combination of a’s and b’s i.e. this rules
gives the strings of (a+b)*.
Context-Free Grammar (CFG)
Thus the logic for this example will be
(anything) a (anything) a (anything)
So before a or after a there could be any combination of a’s and
b’s.
Derivation tree
•Derivation tree is a graphical representation for the derivation of
the given production rules of the context free grammar (CFG).

•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.

•The Parse tree follows the precedence of operators.


•The deepest subtree is traversed first. So, the operator in the
parent node has less precedence over the operator in the subtree.
Properties
The properties of the derivation tree are given below −

•The root node is always a node indicating the start symbols.


•The derivation is read from left to right.
•The leaf node is always the terminal node.
•The interior nodes are always the non-terminal nodes.
Derivation tree
S

b b
S

b b

a
Question 1 :
Draw a derivation tree for the string abaaba for the CFG given by
G where

P={ S→aSa S →bSb S → a|b|ε }


S
Solution :
a S a We have choosen S→aSa

We have choosen S→aSb


b S b

We have choosen S→aSa


a S a

ε 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.

• A DFA can remember a finite amount of information, but a


PDA can remember an infinite amount of information.

• 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.
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.

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. In PDA, the
stack is used to store the items temporarily.
Formal definition of PDA:

The PDA can be defined as a collection of 7 components:

Q: the finite set of states


∑: the input set
Γ: a stack symbol which can be pushed and popped from the stack
q0: the initial state
Z: a start symbol which is in Γ.
F: a set of final states
δ: mapping function which is used for moving from current state to
next state.
Instantaneous Description (ID)

ID is an informal notation of how a PDA computes an input string


and make a decision that string is accepted or rejected.

An instantaneous description is a triple (q, w, α) where:

q describes the current state.


w describes the remaining input.
α describes the stack contents, top at the left.

Turnstile Notation:

⊢ sign describes the turnstile notation and represents one move.

⊢* sign describes a sequence of moves.


For example,
(p, b, T) ⊢ (q, w, α)

In the above example, while taking a transition from state p to q,


the input symbol 'b' is consumed, and the top of the stack 'T' is
represented by a new string α.
Example 1:

Design a PDA for accepting a language {anb2n | n>=1}.

Solution: In this language, n number of a's should be followed by 2n number of


b's. Hence, we will apply a very simple logic, and that is if we read single 'a', we
will push two a's onto the stack. As soon as we read 'b' then for every single 'b'
only one 'a' should get popped from the stack.

The ID can be constructed as follows:


δ(q0, a, Z) = (q0, aaZ)
δ(q0, a, a) = (q0, aaa)
Now when we read b, we will change the state from q0 to q1 and start
popping corresponding 'a'. Hence,
δ(q0, b, a) = (q1, ε)

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})

We can summarize the ID as:


δ(q0, a, Z) = (q0, aaZ)
δ(q0, a, a) = (q0, aaa)
δ(q0, b, a) = (q1, ε)
δ(q1, b, a) = (q1, ε)
δ(q1, ε, Z) = (q2, ε)
Now we will simulate this PDA for the input string "aaabbbbbb".

δ(q0, aaabbbbbb, Z) ⊢ δ(q0, aabbbbbb, aaZ)


⊢ δ(q0, abbbbbb, aaaaZ)
⊢ δ(q0, bbbbbb, aaaaaaZ)
⊢ δ(q1, bbbbb, aaaaaZ)
⊢ δ(q1, bbbb, aaaaZ)
⊢ δ(q1, bbb, aaaZ)
⊢ δ(q1, bb, aaZ)
⊢ δ(q1, b, aZ)
⊢ δ(q1, ε, Z)
⊢ δ(q2, ε)
ACCEPT
Example 2:
Design a PDA for accepting a language {0n1m0n | m, n>=1}.

Solution: In this PDA, n number of 0's are followed by any


number of 1's followed n number of 0's. Hence the logic for
design of such PDA will be as follows:

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 –

•We have to decide the non-terminal which is to be replaced.


•We have to decide the production rule by which the
non-terminal will be replaced.

Two options to decide which non-terminal has to be replaced


with the production rule are as follows −

•Left most derivation


•Right most derivation.
Left Most Derivation
In the leftmost derivation, the input is scanned and then
replaced with the production rule from left side to right. So, we
have to read that input string from left to right.

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

Finally, the given string is parsed


Example

Let any set of production rules in a CFG be


X → X+X | X*X |X| a
over an alphabet {a}.
The leftmost derivation for the string "a+a*a" may be −
X → X+X → a+X → a + X*X → a+a*X → a+a*a
Acropolis Institute of Technology &
Research, Indore
www.acropolis.in
(Theory of computation)
(TOC)

By: AJAY VARMA


Theory of computation Pushdown Automata
Unit 4 Context Free Language
PUSHDOWN AUTOMATA (PDA)
❑ Pushdown automata is a way to implement a CFG in the same way we design DFA for a regular
grammar. A DFA can remember a finite amount of information, but a PDA can remember an
infinite amount of information.

❑ 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

A pushdown automaton is a way to implement a context-free grammar in a similar


way we design DFA for a regular grammar. A DFA can remember a finite amount of
information, but a PDA can remember an infinite amount of information.
Basically a pushdown automaton is:
“ε -NFA" + "a stack"
A pushdown automaton has three components:
❑ 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.
------- I1 I2 I3 I4 I5 I6 I7 I8 -------
Input Tap

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}

Solution : M = where Q = { q0, q1 } and Σ = { a, b } and Γ = { A, Z } and


δ is given by :
δ( q0, a, Z ) = { ( q0, AZ ) }
δ( q0, a, A) = { ( q0, AA ) }
δ( q0, b, A) = { ( q1, ∈) }
δ( q1, b, A) = { ( q1, ∈) }
δ( q1, ∈, Z) = { ( q1, ∈) }

Let us see how this automata works for aaabbb


Note:

❑ The above pushdown automaton is deterministic in nature because there is


only one move from a state on an input symbol and stack symbol.
❑ The non-deterministic pushdown automata can have more than one move
from a state on an input symbol and stack symbol.
❑ It is not always possible to convert non-deterministic pushdown automata
to deterministic pushdown automata.
❑ Expressive Power of non-deterministic PDA is more as compared to
expressive deterministic PDA as some languages which are accepted by
NPDA but not by deterministic PDA which will be discussed in next article.
❑ The push down automata can either be implemented using acceptance by
empty stack or acceptance by final state and one can be converted to
another.
Non-deterministic Pushdown Automata

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.
NPDA

Suppose the set of transition rules of an NPDA contains

δ(q1, a, b) = {(q2 ,cb ),(q3, ∈)}

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

The first symbol on R.H.S. production must be a terminal symbol. The


following steps are used to obtain PDA from CFG is:
Step 1: Convert the given productions of CFG into GNF.
Step 2: The PDA will only have one state {q}.
Step 3: The initial symbol of CFG will be the initial symbol in the PDA.
Step 4: For non-terminal symbol, add the following rule:
δ(q, ε, A) = (q, α)
Where the production rule is A → α
Step 5: For each terminal symbols, add the following rule:
δ(q, a, a) = (q, ε) for every terminal symbol
Example 1:
Convert the following grammar to a PDA that accepts the same
language.

.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

Convert G into GNF


For each production
A aϒ in G
a is a terminal and
ϒ is either string of variable or empty string
Then
δ(q,a,A)=(q, ϒ) in M

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

δ(q0, ε, z0)=(q1, Sz0)


The production S → aSA will be simulated in the pda by removing S from the stack and
replacing it with SA, while reading a from the input. Similarly, the rule S → a should cause
the pda to read an a while simply removing S. Thus, the two productions are represented
in the pda by

δ(q1, a, S)={(q1, SA), (q1, ε)}


In an analogous manner, the other productions give
δ(q1, b, A)=(q1, B)
δ(q1, b, B)=(q1, ε)
The appearance of the stack start symbol on top of the stack signals the completion of the
derivation and the pda is put into its final state by

δ(q1, ε, z0)=(q2, ε)
Example

S aA, δ(q0, ε, Z) = {(q1, SZ)}


A aABC | bB | a, δ(q1, ε, Z) = {(qf, Z)}
B b,
δ(q1, a, S) = {(q1, A)}
C c
δ(q1, a, A) = {(q1, ABC),(q1, ε)}
δ(q1, b, A) = {(q1, B)}
δ(q1, b, B) = {(q1, ε)}
δ(q1, c, C) = {(q1, ε)}
The sequence of moves made by M in processing aaabc is

(q0, aaabc, Z) ⊢ (q1, aaabc, SZ)


⊢ (q1, aabc, AZ)
⊢ (q1, abc, ABCZ)
⊢ (q1, bc, BCZ)
⊢ (q1, c,CZ)
⊢ (q1, ε, Z)
⊢ (qf , ε, Z)
Exercise: Construct an npda that accepts the language generated by the
grammar

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

Context-free languages are closed under:


❑ Union
❑ Concatenation
❑ Kleene Star operation

Context-free languages are not closed under:


Intersection
Complement
Union

Context-free languages are closed under: Union


L1 is context free and L2 is context free then L1∪L2 is context-free

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

Context-free languages are closed under: Concatenation


L1 is context free and L2 is context free then L1L2 is context-free

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

Context-free languages are closed under: Star Operation


L is context free then (L)*is context-free

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

Context-free languages are not closed under: intersection


L1 is context free and L2 is context free then L1∩L2 not necessarily context-free

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

Empty Language: Is L(G) =Ø ?


Algorithm:
1. Remove useless symbols
2. Check if start symbol is useless? If yes, then L(G) =f else not.

Infinite Language Problem: Is L = L(G) an infinite language?


Algorithm:
1. remove useless symbols
2. remove null and unit productions
3. create dependency graph for variables
4. if there is a loop in the dependency graph, then L is infinite language else not.
Example:

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)

By: AJAY VARMA


Turing Machines
Theory of computation
Recursive Language
Unit 5
Recursively Enumerable
Language
TURING MACHINE
A Turing Machine is an accepting device which accepts the languages
(recursively enumerable set) generated by type 0 grammars. It was invented in
1936 by Alan Turing.
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.
Work tape
1 0 1 0 0 1

Finite Program
….110110 Input port Output port 010λ00…..
State qadv

Advice input tape


1 0 1 1 0 1
Advice output tape
0 1 1 0 1 0 0 1 1 1 0
Turing Machines

❑ Turing machine (TM) = formal model of a computer running a particular program


❑ We use the simplicity of the TM model to prove formally that there are specific
problems (languages) that the TM cannot solve
❑ Similar to a finite automaton but with an unlimited and unrestricted memory
Infinite tape is unlimited memory
Tape head can read and write symbols and move around the tape
Initially tape contains only the input string and is blank every where for work space
To store information, TM can write on the tape
TM continues computing until it decides to produce an output
Outputs accept and reject obtained by entering designated accepting and reject
states
If TM does not enter an accepting or reject state, it will go on forever, never halting
Three Possible Outcomes of TM

✔ TM may halt and accept the input


✔ TM may halt and reject the string
✔ TM may not halt at all (In Loop)
States
✔ Initial State
✔ Intermediate States
✔ Finial States
❑Accept State
❑Reject State
Summary of differences between TM and FA
1. TM can both read from tape and write on it
2. Read-write head can move both to the left and right
3. The tape is infinite
4. The special states for accepting and rejecting take immediate effect
5. TM need not read the entire input to accept
Difference between Turing Machine and Current Machine is that—:

Turing Machine can’t random access but


current machine do(by register or RAM)

Convensnal Model of computation


That are related (or equivalent) to TM
Register , RAM

Unconvensnal Model of computation


DNA Computing , Membering Computing,
Molicular Computing , Quantam Computing
Three language classes

– Recursive = TM halts for all input strings (decides a language)


– Recursively Enumerable = TM can accept the strings in the language but
cannot tell for certain that a string is not in the language
– Non-RE = no TM can even recognize the members of the language in the RE
sense

A language L on Σ is said to be recursive if there exists a Turing machine M


that accepts L and that halts on every w in Σ+. In other words, a language is
recursive if and only if there exists a membership algorithm for it.
Containment of Type 3 ⊆ Type 2 ⊆ Type 1 ⊆ Type 0
A language L is said to be recursively enumerable if there exists a
Turing machine that accepts it.

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

An example of a recursive enumerable problem that is also


un-decidable
Does a given Turing Machine M halt on a given input w?

?
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:

Turing machine M = (Q, X, Σ, δ, q0, B, F) with


❑ Q = {q0, q1, q2, qf}
❑ Γ = {a, b,1}
❑ Σ = {1}
❑ q0= {q0}
❑ B = blank symbol
❑ F = {qf }
δ is given by:

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}}

where δ is given by:


Example :

Consider the Turing machine M described by the transition table.


Describe the processing of (a) 011 , (b) 0011 , (c) 001 using IDs.
Which of the above string accepted by M?

Present Tape Symbol


State 0 1 x y b
q1 xRq2 bRq5
q2 0Rq2 yLq3 yRq2
q3 0Lq4 xRq5 yLq3
q4 0Lq4 xRq1
q5 yRq5 bRq6
(q6)
Solution
Example: L = {0n1n | n≥1} Strategy: w = 000111

Accept
n n
TM for {0 1 | n≥1}
n n
TM for {0 1 | n≥0}
Configuration Graph of TM

Configuration Tuple: (State, Tape Content, Tape head)


❑ A Configuration Graph of M on x is (denoted as GM,x) is a graph whose vertices are the
configuration of G w.r.t. x.
❑ There is an edge from Configuration C1 to C2 if the TM M can go from C1 to C2 in one step.
❑ Graphical representation of computation of M on x.
❑ A computation path is a path in GM,x from the start configuration.

Prob. : M accepts x iff ∃ of a computation path in GM,x from the start configuration to
accept configuration.
Properties of Configuration Graph

Configuration graph is defined w.r.t. a TM and an input.


If M is deterministic then the out degree of every vertex in GM,x <=1.
If M is Non-deterministic then the out degree can be arbitrary.
Out degree of accept and reject configurations are 0.
Technically, GM,x can be infinite, but if the size of tape is bounded then GM,x is
finite.
Although every configuration is part of GM,x , but w.l.o.g. (without lose of
generality) , we can only lock at configurations that are reachable from start
configuration.
GM,x has a unique start and accept configuration.
Configuration Graph of Deterministic TM and Non-deterministic TM

DTM

NDTM
Theorem:

A class of language accepted by DTM and NDTM are equal.


NDTM is more powerful then DTM
1. Turing machine for Checking- is Binary no even or not.
Exercise

❑ {w | w is in {0,1}* and w ends with a 0}


❑ {w | w is in {0,1}* and w ends in 00}
❑ {w | w is in {0,1}* and w contains at least two 0’s}
❑ {w | w is in {0,1}* and w contains at least one 0 and one 1}
❑ {WWR | W is in {a,b}* }
❑ {W$W | W is in {a,b}* }
❑ {W | W is in {a,b}* and even no of a’s }
❑ W={{anb2n }|n>0}
❑ W={{0n1n0n }|n>0}
❑ {W | W is even no of palindrome over {a,b} }
❑ {W | W is odd no of palindrome over {a,b} }
Recursive languages are closed under:
❑Union
❑Intersection
❑Concatenation
❑complementation
❑Kleene closure (star operator)
❑Homomorphism, and inverse homomorphism

RE languages are closed under: RE languages are not closed under:


❑Union ❑complementation
❑ intersection
❑ concatenation
❑ Kleene closure
❑ Union: If L1 and If L2 are two recursive languages, their union L1∪L2 will also be recursive
because if TM halts for L1 and halts for L2, it will also halt for L1∪L2.

❑ 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.

❑ Intersection and complement: If L1 and If L2 are two recursive languages, their


intersection L1 ∩ L2 will also be recursive.
For Example: L1= {anbncndm|n>=0 and m>=0}
L2= {anbncndn|n>=0 and m>=0}
L3=L1 ∩ L2 = { anbncndn |n>=0} will be recursive.
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.
Multi-tape TM
Multi-tape Turing Machines

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

1. Increasing length of string on a Tape.


If the symbol needs to be added for yi then push yi+1 to yk One cell
to the right. Use this symbol to keep track of Tape head

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

where U and D specify movement of the


δ : Q × Γ→ Q × Γ ×{L, R, U, D}, read-write head up and down, respectively.
Two Stack Machines

Turing Machine Two-stack PDA Machine


Change state. Change state.
Write a tape symbol in cell scanned. The input symbol read.
Move the head left or right. Replace the symbol of each stack with
a string of zero or more stack symbols.
Two Stack Machines
Queue Machines

Queue Machines = TM
Nondeterministic Turing Machines

δ : Q × Γ → 2Q×Γ×{L, R}.

Example If a Turing machine has transitions specified by


δ (q0,a) = {(q1,b, R), (q2,c, L)},
it is nondeterministic. The moves

and

The class of deterministic Turing machines and the class of nondeterministic


Turing machines are equivalent.
A Universal Turing Machine
“A Turing machine as presented in “M = (Q,Σ,Γ,δ,q0,B,F), ”is a special purpose
computer. Once δ is defined, the machine is restricted to carrying out one
particular type of computation. Digital computers, on the other hand, are
general-purpose machines that can be programmed to do different jobs at
different times. Consequently, Turing machines cannot be considered equivalent
to general-purpose digital computers.”
A limitation of Turing Machines:

Turing Machines are “hardwired”

they execute
only one program

Real Computers are re-programmable


Solution: Universal Turing Machine

Attributes:

• Reprogrammable machine

• Simulates any other Turing Machine


Universal Turing Machine
simulates any Turing Machine M

Input of Universal Turing 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

We describe Turing machine M


as a string of symbols:

We encode M as a string of symbols


Alphabet Encoding

Symbols
:
Encoding:
State Encoding

States:

Encoding:

Head Move Encoding


Move:

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:

each string of this language is


the binary encoding of a Turing Machine
Language of Turing Machines

L = { 010100101, (Turing Machine 1)

00100100101111,
(Turing Machine 2)
111010011110010101,

…… } ……
Church-Turing Thesis

Alonzo Church (1903-1995) Alan Turing (1912-1954)

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.

Turing stated it this way:


"It was stated ... that 'a function is effectively calculable if its values can be found by some purely mechanical process.' We
may take this literally, understanding that by a purely mechanical process one which could be carried out by a
machine. The development ... leads to ... an identification of computability† with effective calculability."
Church’s hypothesis

In computability theory, the Church–Turing thesis (also known as computability


thesis, the Turing–Church thesis, the Church–Turing conjecture, Church's thesis, Church's
conjecture, and Turing's thesis) is a hypothesis about the nature of computable functions.

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.

❑ Anything that is computable is computable by a Turing Machine


❑ Conversely, the set of functions computed by a Turing Machine is the set of ALL and
ONLY computable functions
CHURCH–TURING’S THESIS

– The Church-Turing Thesis provides the definition of algorithm


necessary to resolve Hilbert’s tenth problem.
Our intuitive understanding of algorithms coincides with Turing machine algorithms.

– Thus, Turing machines capture our intuitive idea of computation.


– Indeed, we are more interested in algorithms themselves. Once we believe that
TMs precisely capture algorithms, we will shift our focus to algorithms rather than
low-level descriptions of TMs.
Lambda Calculus

– In 1936, Church introduced Lambda Calculus as a formal description of all computable


functions.
Lambda calculus (also written as λ-calculus) is a formal system in mathematical logic for
expressing computation based on function abstraction and application using variable
binding and substitution.
Lambda calculus consists of constructing lambda terms and performing reduction
operations on them. In the simplest form of lambda calculus, terms are built using only
the following rules:
Syntax Name Description
A character or string representing a parameter or
x Variable
mathematical/logical value
Function definition (M is a lambda term). The variable x
(λx.M) Abstraction
becomes bound in the expression.

(M N) Application Applying a function to an argument. M and N are lambda terms.


– Independently, Turing had introduced his A-machines in 1936 too.

– 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.

– If there is TM solving a problem, does there exist an equivalent TM that halts?


What else did Turing do?
– Alan Turing characterized computable functions by building a machine.
Though theoretical this gave rise to the idea of computers.

– 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.

Decision Procedure: - Decision problem solve by any algorithm.


Problem Algorithm
Desired
Result
Expected
Solvable Time

Resources

Complexity Theory:- Study of resources to solve a problem by a


particular algorithm.
We have been writing about efficient algorithms to solve complex
problems, like shortest path, Euler graph, minimum spanning tree,
etc. Those were all success stories of algorithm designers. In this
post, failure stories of computer science are discussed.
Can all computational problems be solved by a computer?
There are computational problems that can not be solved by algorithms even
with unlimited time. For example Turing Halting problem (Given a program
and an input, whether the program will eventually halt when run with that

?
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.

❑ Linear Search O(n)


❑ Binary Search O(log n)
❑ Insertion Sort O(n2)
❑ Merge Sort O(n log n)
❑ ..
❑ ..
NP class Problem: -

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 ?

This the most famous problem in computer science, and one of


the most important outstanding questions in the mathematical
sciences. In fact, the Clay Institute is offering one million dollars for a
solution to the problem (Stephen Cook's write up on the Clay
website is quite good). It's clear that P is a subset of NP. The open
question is whether or not NP problems have deterministic
polynomial time solutions. It is largely believed that they do not.
Here is an outstanding recent article on the latest (and the
importance) of the P = NP problem:
Tractable(Decidable Problems) and
Intractable(Un-decidable Problems)

1<log n<n1/2<n< n log n<n2<n3<- -- - - <2n<3n<- -!n- - <nn


Decidable Problems

A problem is decidable if we can construct a Turing machine which will halt in


finite amount of time for every input and give answer as ‘yes’ or ‘no’. A
decidable problem has an algorithm to determine the answer for a given input.
Examples
Equivalence of two regular languages: Given two regular languages, there is an
algorithm and Turing machine to decide whether two regular languages are
equal or not.
Finiteness of regular language: Given a regular language, there is an algorithm
and Turing machine to decide whether regular language is finite or not.
Emptiness of context free language: Given a context free language, there is an
algorithm whether CFL is empty or not.
Un-decidable Problems

A problem is un-decidable if there is no Turing machine which will always halt in


finite amount of time to give answer as ‘yes’ or ‘no’. An un-decidable problem has no
algorithm to determine the answer for a given input.
Examples
Ambiguity of context-free languages: Given a context-free language, there is no
Turing machine which will always halt in finite amount of time and give answer
whether language is ambiguous or not.
Equivalence of two context-free languages: Given two context-free languages, there
is no Turing machine which will always halt in finite amount of time and give answer
whether two context free languages are equal or not.
Everything or completeness of CFG: Given a CFG and input alphabet, whether CFG
will generate all possible strings of input alphabet (∑*)is un-decidable.
Regularity of CFL, CSL, REC and RE: Given a CFL, CSL, REC or RE, determining
whether this language is regular is un-decidable.
What is Reduction?
Let L1 and L2 be two decision problems. Suppose algorithm A2 solves L2. That is, if y is an
input for L2 then algorithm A2 will answer Yes or No depending upon whether y belongs to
L2 or not.

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

Instance of X Algorithm Solution to


For Y Problem X

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.

1. If A is reducible to B and B in P then A in P.


2. A is not in P implies B is not in P.
NP-complete class: -

A problem is in NP-complete, if
1. It is in NP
2. It is NP-hard
Reduction in NP-Complete

If A and B are two Problem. If A is reducible to B in polynomial time then B is


NP-Hard.
If B is also in NP then B is NP-Complete.
Summary

Example : Turing halting Problem

Example : Vertex cover Problem

Example : Hamiltonian Cycle Problem, Prime Factor,


Scheduling, Understanding DNA, TSP.
Example : Shortest path problem

Co-
NP
Steps of proving NP-Complete:

Step-1: Prove that B is in NP.


Step-2: Select an NP-Complete language A.
Step-3: Construct a function f that maps members of A to members of B.
Step-4: Show that x is in A iff f(x) is in B.
Step-5: Show that f can be computed in polynomial time P.
Problems:

1. Shortest path in Graph


2. Longest path in Graph
3. Euler graph
4. Minimum spanning tree
5. 2 SAT Problem
6. 3 SAT Problem
7. Sudoku
8. TSP
9. Knapsack
10. 3 color Problem
Here we will emphasize NPC
Boolean Satisfiability Problem

Boolean Satisfiability or simply SAT is the problem of determining if a


Boolean formula is satisfiable or unsatisfiable.

Satisfiable : If the Boolean variables can be assigned values such that


the formula turns out to be TRUE, then we say that the formula is
satisfiable.

Unsatisfiable : If it is not possible to assign such values, then we say


that the formula is unsatisfiable.
Boolean formula ~(P ˄ Q) is Satisfiable for
Boolean formula P ˄ ~P is not Satisfiable
2-Satisfiability (2-SAT) Problem
Boolean Formulas
3 CNF SAT

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)

Does Hamiltonian Cycle Problem ∈ NP?


Hamiltonian Circuit Problem
Certificate: List of vertices on Hamiltonian Cycle To check if this list is
actually a solution to the Hamiltonian cycle problem, one counts the
vertices to make sure they are all there, then checks that each is
connected to the next by an edge, and that the last is connected to
the first.

It takes time proportional to n, because there are n vertices to count


and n edges to check. n is a polynomial, so the check runs in
polynomial time.

Therefore, Hamiltonian Cycle ∈ NP.


Compression between Hamiltonian and Euler Circuit
NP-complete Partitioning Problems

Partition problem is to determine whether a given set can be partitioned into


two subsets such that the sum of elements in both subsets is the same.

Given S = {3,1,1,2,2,1}, a valid solution to the partition problem is the two


sets S1 = {1,1,1,2} and S2 = {2,3}. Both sets sum to 5, and they partition S. Note
that this solution is not unique. S1 = {3,1,1} and S2 = {2,2,1} is another solution.
Not every multiset of positive integers has a partition into two subsets with
equal sum. An example of such a set is S = {2,5}.
NP-complete Partitioning Problems
Q&A
THANKS

You might also like