0% found this document useful (0 votes)
659 views

MITWPU - Unit 1-Theory of Computation-Merged

Uploaded by

Rutumbara Chakor
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
659 views

MITWPU - Unit 1-Theory of Computation-Merged

Uploaded by

Rutumbara Chakor
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 299

MIT-WPU

T.Y. B.Tech

Theory of Computation
(CS314)

7/28/2021 1
Course Objective & Course Outcomes
• Course Objectives:
By participating in and understanding all facets of this Course a student will be able:
1. To understand the basics of automata theory and its operations.
2. To understand problem classification and problem solving by machines.
3. To study computing machines by describing, classifying and comparing
different types of computational models.
4. To understand the fundamentals of decidability and computational complexity.

• Course Outcomes:
After successful completion of this course students will be able:
1. To construct Finite State Machines to solve problems in computing.
2. To build mathematical expressions and syntax verification for the formal
languages.
3. To construct and analyze Push Down Automata and Turing Machine for formal
languages.
4. To classify the problems of decidability and complexity. 2
7/28/2021
Text Books & Reference Books
• Text Books
• John C. Martin, Introduction to Language and Theory of Computation, TMH,
3rd Edition, ISBN: 978-0-07-066048-9.
• Vivek Kulkarni, Theory of computation, Oxford University Press, ISBN-13: 978-0-
19-808458-7.

• Reference Books

• K.L.P Mishra,N. Chandrasekaran,Theory of Computer Science (Automata, Languages


and Computation), Prentice Hall India, 2nd Edition.
• Michael Sipser, Introduction to the Theory of Computation,CENGAGE Learning, 3rd
Edition, ISBN:13:978-81-315-2529-6.
• Daniel Cohen, Introduction to Computer Theory, Wiley India, 2nd Edition, ISBN:
9788126513345.
• Kavi Mahesh, Theory of Computation: A Problem Solving Approach, 1st Edition,
Wiley-India, ISBN: 978-81-265-3311-4.

7/28/2021 3
Important Instructions
• Be ready Be Attentive

• Tutorials : 4

• Keep a separate notebook for TOC

• Add your profile picture and use only MITWPU email-id for TOC communications and
submissions.

• Tutorial Instructions:
⮚Tutorial on Assignment/Ruled pages

⮚Before uploading label your pages with your Rollno., Name, Tutorialno.

⮚Upload pdf with name :Tut1_FirstNameLastname

⮚You maybe asked to keep your video ON anytime during the tutorial

7/28/2021 4
Unit I
• Introduction to Formal language, Basic concepts: Symbol, Alphabet, String
• Introduction to Finite Automata, State transition graph, Transition table,
Acceptance of a string, Acceptance of a Language
• Deterministic finite Automata (DFA)-Formal Definition
• NonDeterministic finite Automata (NFA)-Formal Definition
• NonDeterministic finite Automata (NFA) with epsilon transition
• Equivalence of NFA and DFA, Conversion from NFA to DFA, Conversion from
NFA with epsilon transition to DFA
• Finite Automata with output: Moore and Mealy Machine, Moore to Mealy
conversion, Mealy to Moore conversion

7/28/2021 5
Planner - Lectures
Lecture No Topics Covered
1 Introduction to TOC, Introduction to Formal language, Basic
concepts: Symbol, Alphabet, String
2 Introduction to FA, State transition graph, Transition table,
Acceptance of a string, Acceptance of a Language,
3 DFA -Formal Definition, NFA-Formal Definition, NFA with epsilon
transition, Equivalence of NFA and DFA
4 Conversion from NFA to DFA

5 Conversion from NFA to DFA (cont), Conversion from NFA with


epsilon transition to DFA
Topics
6 Conversion from NFA with epsilon transition to DFA (cont)

7 Minimization of FA

8 Finite Automata with output: Moore and Mealy Machine


Moore to Mealy conversion
9 Mealy to Moore conversion

Tutorial topics:
FA, DFA, NFA, FSM, Conversion from NFA to DFA

7/28/2021 6
About TOC
• Course is about models of computation, their power, and relationships.
• Hierarchy of models of increasing power:
-DFA < PDA < TM.
For each model, we study:
-What can be computed.
-What cannot be computed.

• Automata theory is the study of abstract computing devices or


“machines”

7/28/2021 7
History of TOC
• 1930-45: Alan Turing studied Turing machine
Describe boundary between what a computing machine could do and
what it could not do
• 1940-50: Researchers studies Finite Automata
• Late 1950s: N. Chomsky studied formal
“Grammars”
• 1969-70: S. Cook extended Turing’s study
Separated problems that can be solved efficiently by computers and the
problems that can be solved theoretically, but takes so much time by
computers to solve

7/28/2021 8
Automata theory
• The study of abstract 'mathematical' machines or systems and
the computational problems that can be solved using these
machines.

• These abstract machines are called automata.

• Automata theory is also closely related to formal language theory,


as the automata are often classified by the class of formal
languages they are able to recognize.

• An automaton can be a finite representation of a formal language


that may be an infinite set.

• Automata are used as theoretical models for computing machines,


and are used for proofs about computability.

7/28/2021 9
Application of TOC
• Finite Automata:
• Software for designing and checking behavior of
digital circuits
• The “Lexical Analyzer” of a compiler
• Software for scanning large bodies of text
E.g. to find occurrences of words in collections of
web pages
• Software for verifying systems of all types that have a
finite number of distinct states
E.g. communication Protocols
7/28/2021 10
Application of TOC (cont…)
• Turing Machine:
• Prototype/Abstract of modern computer
• To find out what a computer can do and what it can
not do
• Help us understand what we can expect from our
software
• To decide, whether we can solve the problem, which
requires more time to solve, in less amount of time.

7/28/2021 11
Basic Definitions

• Difference Between Formal Language and Natural Language


Formal Language Natural Language
1. All the rules are stated explicitly 1. Not necessarily rules to be stated
explicitly. It is a medium of
communication.
2. Considered as Symbols on papers 2. Expressions of ideas in Natural way.
3. In formal Languages we are 3. We are interested in Meaning than
interested in the form of strings of only strings.
symbols and not meaning

7/28/2021 12
Basic Definitions
• Alphabet - a finite set of symbols.
Notation: Σ .
E.g.: Binary alphabet {0,1},
English alphabet {a,...,z,!,?}

• String over an alphabet Σ - a finite sequence of symbols from Σ.


E.g: 0100 is a string over the binary alphabet.
a!? is a string over the English alphabet.

• Language over alphabet Σ - a set of strings over Σ.


Notation: L.
E.g:
{0, 00, 000, ...} is an "infinite" language over the binary alphabet.
{a, b, c} is a "finite" language over the English alphabet.
7/28/2021 13
Basic Definitions (cont…)
• Empty string: e or ε denotes the empty sequence of symbols.

• Empty Language: Empty set of strings.


Notation: Ф

• Regular Language- a finite set of symbols.


L is called regular Language if there is some automaton that accepts L.
E.g.Σ* , Even length of strings, strings containing abba are regular Languages.

7/28/2021 14
Basic Machine and Finite State Machine

• Basic Machine:
• A machine that recognizes input set I and
produces output set O,
where I & O are finite.
• Machine Function(MAF): I-> O
• Deals with what output is generated, not how
output is generated

E.g.: Switch

7/28/2021 15
Basic machine
It is the most primitive machine with a set of inputs I and set of output O.
E.g. AND,OR,NOR… gates , Vending machine, Decimal to binary
converter, Electric fan…
Weighing machine : I/p- Coin
O/p- Printed weight ticket
It just maps input set to output set.
Mapping function is called Machine function MAF I -> O

E.g.for AND gate MAF:

I O
(0,0) 0

(0,1) 0

(1,0) 0

(1,1) 1
Limitations:
Cannot remember the intermediate states.

7/28/2021 16
Finite State machine(FSM)
In FSM the internal state of machine changes when it
receives I/P.
It has a pair of functions:
Machine Fn MAF : I x S -> 0

State Fn STF : I x S -> S

Where S: finite or internal state of machine


I: set of i/p symbols
O: set of o/p symbols

For a given i/p there will be always only one final state.

e.g. Binary Adder, Divisibility by 3 tester..

7/28/2021 17
Finite Automata
• 5-tuple:
• M= (Q,Σ, 𝛿, qo, F)
where,
• Q: Finite set of states
• Σ: Finite input alphabet
• 𝛿: STF that maps Q X Σ to Q, i.e. Q X Σ Q
• qo: Initial state of FA, qo ∈ Q
• F: Set of final states, F ⊆ Q

7/28/2021 18
Finite Automata (cont…)
Input

String

Output

Finite
Automaton Accept/Reject

7/28/2021 19
Block diagram of FA
0 0 1 Input Tape

Tape Head
Finite Control
q0

• Tape is divided into units ,each containing one


symbol of the i/p string from Σ.
• Read the current letter of input under the tape head.
• Transit to a new state depending on the current input
and the current state, as dictated by the transition
function δ(qi,a)=qj.
• Halt after consuming the entire input.

7/28/2021 20
Acceptance of a String
• A string ‘x’ is said to be accepted by an FA
(given by):

M= (Q,Σ, 𝛿, qo, F)
if 𝛿 (qo,x) = p,

for some p ∈ F (i.e. p is a member of F)

7/28/2021 21
Acceptance of a Language
• If there is a language L such that:

L = {x | 𝛿 (qo,x) = p, for some p ∈ F},


Then it is said to be accepted by the FA, M

7/28/2021 22
Finite Automata (cont…)
• Finite number of states
• No memory to store intermediate results
• Limited power due to lack of memory
• Input string is considered to be accepted, if the FA resides in any
of the final states
• Input string is considered to be rejected, if the FA resides in any
of the non-final states

7/28/2021 23
Preferred Notation for FA

❑Transition Diagram

❑Transition Table

7/28/2021 24
Transition Diagram
Transition diagram for FA(Q,Ʃ,q0,F,δ) is a graph
defined as:
⮚For each state there is a node.
⮚For each state q in Q and for input symbol in Ʃ .
let δ(q,a)=p then
a
q p

⮚There is arrow into start state. q0

⮚Final states marked by double circle.


qf

7/28/2021 25
Transition Table

7/28/2021 26
Deterministic Finite Automata
• An FA is said to be deterministic, if for every state there is a
unique input symbol, which takes the state to the required next
unique state

• Given a state Sj, the same input symbol does not cause the FA to
move into more than one state-there is always a unique next state
for an input symbol
0
0
S0 S1

1 1

0 S2

7/28/2021 27
Designing FA
• Accept strings consisting of a & b, that has at
least one ‘a’
i)Min string :a
ii)∑ ={a,b}
b

a
q0 q1 a,b

7/28/2021 28
Finite Automata Example
❑FA for language over {1,0} in which every
string ends with 10.
0
1

1 0
Q0 Q1 Qf

0 1

7/28/2021 29
FA(DFA & NFA) Examples
1. Construct a FA to recognize language containing strings starting
with ‘10’
2. Construct a FA to recognize language containing strings ending
with ‘101’
3. Construct a FA to recognize language of strings with exactly two
0s anywhere
4. Construct a FA to recognize language containing strings
starting with ‘a’
5. Construct a FA to identify string containing even number of
‘a’s over ∑ ={a}
7/28/2021 30
Non-Deterministic Finite Automata
• NFA has the power to be in several states at once

• Each NFA accepts a language that is also accepted by some DFA

• NFAs are often easier than DFAs

• We can always convert an NFA to a DFA, but the latter may have
exponentially more states than the NFA (a rare case)

• The difference between the DFA and the NFA is the type of transition
function δ

• For a NFA δ is a function that takes a state and input symbol as


arguments
(like the DFA transition function), but returns a set of zero or more
states
(rather than returning exactly one state, as the DFA must)
7/28/2021 31
NFA Definition

• 5-tuple:
• M= (Q,Σ, 𝛿, qo, F)
where,
• Q: Finite set of states
• Σ: Finite input alphabet
• 𝜹: STF that maps Q X 𝜮 to Q, i.e. Q X 𝜮 2Q
• qo: Initial state of FA, qo ∈ Q
• F: Set of final states, F ⊆ Q

7/28/2021 32
Example : pair of 0’s or pair of 1’s

Q = {q0, q1, q2 , q3 , q4} 0/ 0/


Σ = {0, 1} 1 1
0 q 0 q
Start state is q0 q0
4
F = {q2, q4} 3
1 0/
δ: 0 1 1
q0 q 1 q
1 2
q1 {q0, q3} {q0, q1}

q2 {} {q2}
q3
{q2} {q2}
q4
{q4} {}

{q4} {q4}
7/28/2021 33
NFA

7/28/2021 34
NFA Examples
1. Construct a FA to recognize language containing strings starting
with ‘10’
2. Construct a FA to recognize language containing strings ending
with ‘101’
3. Construct a FA to recognize language of strings with exactly two
0s anywhere
4. Construct a FA that recognizes the language of strings that end in
01

7/28/2021 35
NFA

Example: NFA that recognizes the language of strings that end in 01

0,1

1
q0 0 q1 q2

7/28/2021 36
Equivalence of NFA and DFA
For every NFA, there exists an equivalent DFA.
NFA and DFA have equal powers
• Example: NFA vs DFA

Example NFA Equivalent DFA

Identify the language

7/28/2021 37
Conversion of NFA to DFA
(Method I)

• For every NFA, there is an equivalent DFA


• Let us consider Q’= 2Q, set of states for resulting DFA
Q’ X 𝜮 Q’
• Find all transitions from every state in Q’ on reading
each input symbols
• Every combination of states can be considered as a new
state in the resulting DFA and can be given a new label

7/28/2021 38
Conversion of NFA to DFA
Method I (cont…)

• In the resulting DFA, there should be a unique state


resulting from an input to the previous state
• The initial state remains unchanged for the resultant
DFA
• All possible subsets of Q (i.e. 2Q) are the possible
states for the resultant DFA
• Final state in resulting DFA is all the states
containing final state of given NFA

7/28/2021 39
Conversion of NFA to DFA
Method I (cont…)
Example:
Convert the NFA : M = ({q0, q1}, {0, 1}, 𝛿, q0,
{q1}) to its equivalent DFA
State Transition Table 𝜹 for example NFA
0 1

0,1
q0 q1

7/28/2021 40
Conversion of NFA to DFA
Method I (cont…)

State Transition table for resultant NFA State Transition Graph for resultant NFA

7/28/2021
41
Conversion of NFA to DFA
Method II

• Easier and direct


• Takes lesser number of steps and time to build
an equivalent DFA
• Instead of considering the set Q’=2Q and then
removing non-required states, consider only
those states that are required

7/28/2021 42
Conversion of NFA to DFA
Method II (cont…)

• Start building an equivalent DFA with the


transition diagram of given NFA
• Find the transition, one state at a time
• If the next state of a given transition is a new
combination, add that to the set of states for
the resultant DFA

7/28/2021 43
Conversion of NFA to DFA
Method II (cont…)
• Example:
– Convert the NFA: M = ({q0, q1}, {0, 1}, 𝛿, q0,
{q1}) to its equivalent DFA
State Transition Table 𝜹 for example NFA Transition Graph for example NFA

7/28/2021 44
Conversion of NFA to DFA
Method II (cont…)
DFA construction from the given NFA:
(a) Step 1 (b) Step 2 (c) Step 3 (d) Step 4 (e)Final DFA

7/28/2021 45
DFA Minimization
• Identify equivalent states and keep any one of them.

• Replace remaining states by that one.

• Identify unreachable states and remove them

• Identify dead (or trap) states and remove them

7/28/2021
46
DFA Minimization (cont..)
• Equivalent states:
– go to same next state on reading the same input
symbol
– same type (final or non-final)
• Unreachable states:
– Can not be reached from initial state on reading any
input symbol
• Dead (or Trap) states:
– Have no outgoing transitions, except to themselves
7/28/2021 47
DFA Minimization Rules
• One non-final state can be replaced by its equivalent
non-final state only

• One final state can be replaced by its equivalent


final state only

• Initial state can not be replaced by any other state

• One final state can not be replaced by a non- final state or


one non-final state can not be replaced by a final state

7/28/2021 48
DFA Minimization Rules (cont…)
• Replacing state A by state B means deleting all entries related to state A
i.e. deleting all the transitions for state A from the state transition table.

• If more equivalent states are found after applying all the rules,
repeat the same five steps to further reduce the DFA.

Example

𝜮 0 1
Q
p p,q p

q r r

r s -

s s s

7/28/2021 49
DFA Minimization (cont…)
State Transition Table of the DFA to be minimized

7/28/2021 50
DFA Minimization (cont…)
Minimized DFA

7/28/2021 51
DFA Minimization (cont…)
Further minimized DFA

7/28/2021
52
NFA with 𝜀 − Transitions
• 5-tuple:
• M= (Q,Σ, 𝛿, qo, F) where,
• Q: Finite set of states
• Σ: Finite input alphabet
• 𝜹: STF that maps Q x (𝜮∪ { 𝜺} ) to 2Q
i.e. Q X (𝜮 ∪{𝜺 }) 🡪 2Q
• qo: Initial state of FA, qo ∈ Q
• F: Set of final states, F ⊆ Q

7/25/21 53
NFA with 𝜀 − Transitions
(cont..)
• Example:
Transition Graph for example NFA with 𝛆 − 𝐭𝐫𝐚𝐧𝐬𝐢𝐭𝐢𝐨𝐧𝐬

7/25/2021 54
NFA with ℇ moves Example

7/28/2021 55
NFA with 𝜀 − Transitions
(Examples)
• Example:
Transition Graph for example NFA with 𝛆 − 𝐭𝐫𝐚𝐧𝐬𝐢𝐭𝐢𝐨𝐧𝐬

7/25/2021 56
NFA with 𝜀 − Transitions
(Examples)
• Example:
Convert the NFA with 𝜀 −transitions given in the figure to its equivalent DFA

State Transition table for example NFA with State Transition Graph for example NFA
𝛆 −transitions with 𝛆 −transitions

57
NFA with 𝜀 − Transitions
(Examples)
• Example:
Transition Graph for example NFA with 𝛆 − 𝐭𝐫𝐚𝐧𝐬𝐢𝐭𝐢𝐨𝐧𝐬

7/25/2021 58
Significance of NFA with 𝜀 − Transitions

• Helps to split complex language acceptance problems into smaller


once.
• Solution to these problems can be integrated with the help of ϵ -
transition.
• Concatenation ,parallel path can be connected with the ϵ -
transition.

7/25/2021 59
C Conversion of NFA with 𝜀 − Transitions to
DFA

• Indirect Conversion Method


• Direct Conversion Method
NFA with 𝜀 moves

Indirect Method
Direct Method
NFA without 𝜀
moves

Indirect Method Direct Method

Equivalent DFA

7/29/201 60
9
Indirect Conversion Method

1. Construct the NFA without 𝜀 − 𝑡𝑟𝑎𝑛𝑠𝑖𝑡𝑖𝑜𝑛𝑠 from the given


NFA with 𝜀 − move
2. Construct an equivalent DFA with this newly constructed NFA
without 𝜀 − 𝑡𝑟𝑎𝑛𝑠𝑖𝑡𝑖𝑜𝑛𝑠 (using existing methods)

7/25/2021 61
Indirect Conversion Method
(cont…)

• 𝜀-closure of a state:
• Set of all states p, such that there is a path from state q to
state p labeled ‘𝜀’, is known as 𝜀-closure (q)
• Set of all the states having distance zero from state q
• Every state is at distance zero from itself
• Denoted by 𝛿^

7/28/2021
7/29/201 62
9
Indirect Conversion Method
(cont…)
• Example:
Convert the NFA with 𝜀 −transitions given in the figure to its equivalent DFA

State Transition table for example NFA with State Transition Graph for example NFA
𝛆 −transitions with 𝛆 −transitions

63
Indirect Conversion Method
(cont…)

1. Conversion of NFA with 𝜀 −transitions to NFA without


𝜀 −transitions:
State Transition Graph for NFA without 𝛆-transitions

State Transition Table for NFA without 𝛆-transitions

64
Indirect Conversion Method
(cont…)
2. Conversion of NFA without 𝛆 −transitions to an equivalent DFA:
(a) Step 1 (b) Step 2 (c) Final DFA

65
Indirect Conversion Method
(cont…)
2. Conversion of NFA without 𝛆 −transitions to an equivalent DFA:

State Transition table for the equivalent DFA Reduced State Transition table for the
equivalent DFA

66
Direct Conversion Method
• Begin with the initial state
• Go on adding the states as & when required to the diagram
• Follow the same process until there exists no state without
having the transitions specified
• Each state label consists of:
• Name of state label
• Combination of all the state symbols, which are reachable
from the given state

67
Direct Conversion Method (cont…)
• Start building an equivalent DFA with the transition diagram of
given NFA
• Find the transition, one state at a time
• If the next state of a given transition is a new combination, add
that to the set of states for the resultant DFA

68
Direct Conversion Method
(cont…)
• Example (Refer slide no. 53 for example NFA with 𝜺 −transitions
Resultant DFA (a) Step 1 (b) Step 2 (c) Step 3 (d) Final DFA

69
Convert Є-NFA to DFA example
a b c Є

p {P} {q} {r}

q {q} {r} {p}

*r {r} {p} {q}

Solution Є-closure(p) = { p}
NFA Є-closure(q) = { p, q}
a b c Є-closure(r) ={ p, q,r}

p {P} {p,q} {p,q,r}

q {p,q} {p,q,r} {p,q,r} DFA using direct method


a b c
*r {p,q,r} {p,q,r} {p,q,r}
[p] [p] [pq] [pqr]
[pq] [pq] [pqr] [pqr]
*[pqr] [pqr] [pqr] [pqr]

76
Convert Є-NFA to DFA example
0 1 Є

A {A} {B}

B {C} {D}

C {B} {D}

*D {D} {D}

Solution
NFA
0 1 DFA using direct method

A {A,B,C,D} {D} 0 1
B {C,D} {D} [A] [ABCD] [D]
[ABCD] [ABCD] [BD]
C --- {B,D}
*[D] -[D] [D]
*D {D} {D} [BD} [CD] [D]
[CD} [D] [BD]

77
FA with output
Machine generates an output on every input. The value
of output is a function of current state and the current
input.

Such machines are characterized by two behaviors


State transition function(δ)
Output function(λ)

Types of Automata with transition


1) Mealy machine
2) Moore machine

78
Mealy Machine (cont…)
• Six-tuple
• M= (Q,Σ, Δ, 𝛿, 𝜆, qo)
where,
• Q: Finite set of states
• Σ: Finite input alphabet
• Δ: An output alphabet
• 𝛿: STF that maps Q X 𝛴 to Q,
• i.e. Q X 𝛴 Q
• 𝝀: Machine function (MAF); 𝝀: Q X 𝜮 𝜟
• qo: Initial state of the machine, qo ∈ Q
79
Mealy Machine Example
Mealy machine for exclusive-or of the two most-recent
input values

80
Mealy Machine
• The output symbol at any given time depends on the current
input symbol and the current state (i.e.transition)
• An output symbol is associated with the transition

81
Mealy Machine
• A machine with finite number of states, and for which the output
symbol at any given time is a function of (i.e. it depends on) the
current input symbol as well as the current state of the machine
• Example:
• Construct a Mealy machine to find 2’s complement of a given binary
number
Example Mealy Machine

82
Moore Machine (cont…)
• Six-tuple
• M= (Q,Σ, Δ, 𝛿, 𝜆, qo)
where,
• Q: Finite set of states
• Σ: Finite input alphabet
• Δ: An output alphabet
• 𝛿: STF that maps Q X 𝛴 to Q,
• i.e. Q X 𝛴 Q
• 𝝀: Machine function (MAF); 𝝀: Q 𝜟
• qo: Initial state of the machine, qo ∈ Q

83
Moore Machine (cont…)
• Example:
• Construct a Moore machine that takes set of all strings over {a,b} as an
input and prints ‘1’ as an output for every occurrence of ‘ab’ as a
substring

Example Moore Machine

84
Moore Machine
• The output symbol at any given time depends only upon the
current state of the machine (and not on the input symbol read)
• An output symbol is associated with each state
• When the machine is in particular state, it generates the output,
irrespective of the input that caused the transition

85
Moore Machine Example

7/28/2021 86
Moore Machine Example
Machine that counts occurrences of aab

7/28/2021 87
Conversion of Moore Machine to
Mealy Machine
• Moore Machine:
• M1= (Q,Σ, Δ, 𝛿, 𝜆, qo)
• Equivalent Mealy Machine:
• M2= (Q,Σ, Δ, 𝛿, 𝜆′,
qo) where,
• 𝛌′ 𝐪, 𝐚 = 𝛌 (𝛅(q, a))

88
Moore to Mealy Machine
❑Let us take moore machine and it’s transition
Table.

7/28/2021 89
Moore to Mealy Machine
❑It’s transition Table.

7/28/2021 90
Algorithm
Step1:Construct an empty mealy machine using
all states of moore machine as shown in Table

7/28/2021 91
Algorithm
Step 2: Next state for each state can also be directly found fro
m moore machine transition Table as:

7/28/2021 92
Algorithm
Step 3: As we can see output corresponding to each
input in moore machine transition table. Use this to fill the Output
entries.

7/28/2021 93
Algorithm
Step4:As we can see from above
table, q10 and q11 are similar to each other (same value of next st
ate and Output for different Input). Similarly, q20 and q21 are also
similar. So, q11 and q21 can be
eliminated.

7/28/2021 94
Final Mealy Machine
Number of states in Mealy machine can’t be greater than
number of states in Moore machine

7/28/2021 95
Conversion of Moore Machine to
Mealy Machine (cont…)
• Example:
• Convert the given Moore machine to an equivalent
Mealy Machine
Example Moore Machine Equivalent Mealy Machine

96
Conversion of Mealy Machine to
Moore Machine
• Mealy Machine:
• M1= (Q,Σ, Δ, 𝛿, 𝜆, qo)
• Equivalent Moore Machine:
• M2= ([QX Δ]),Σ, Δ, 𝛿′, 𝜆′, [qo, bo]
where,
• bo is an arbitrarily selected member of Δ
• 𝜹′ 𝒒, 𝒃 , 𝒂= [𝜹 (𝒒, 𝒂), 𝝀 (q, 𝒂)]
• 𝝀′ ([q, b]) = b
b: output symbol a: input
symbol

97
Mealy to Moore Machine
❑Let us take mealy
machine and it’s transition
Table.

7/28/2021 98
Algorithm
Step 1.Find states having more than 1 outputs
associated with them.
Here we have q1 and q2.

Step 2. Create two states for these states.


For q1, two states will be q10 (state with output 0)
and q11 (state with output 1). Similarly for q2, two
states will be q20 and q21.

7/28/2021 99
Algorithm
Step 3. Create an empty moore machine with new
generated state. For moore machine, Output will be
associated to each state irrespective of inputs.

7/28/2021 100
Algorithm
Step 4. Fill the entries of next state using mealy machine tr
ansition table shown in Table. For q0 on
input 0, next state is q10 (q1 with output 0).
Similarly, for q0 on input 1, next state is q20 (q2 with output 0).

7/28/2021 101
Final Moore Machine

7/28/2021 102
Conversion of Mealy Machine to
Moore Machine (cont…)
• Example:
• Convert the given Mealy Machine to an equivalent
Moore Machine
Example Mealy Machine Equivalent Moore Machine

103
Comparison of Moore and Mealy
Machine
Moore Machine Mealy Machine

Output depends only upon Output depends upon both


current current
state input symbol and current state
Generally, it has Generally, it has
more states than fewer states than
Mealy machine Mealy machine
If the input string is of length n, then If the input string is of length n, then
the output string is of length n+1. the output string is also of length n
This is because first state also
produces output
𝜆: Q Δ 𝜆: Q X 𝛴Δ

104
Module II
Regular Expression

1
Syllabus and Planner
Lecture Topic Book
1 Formal definition and Construction of Regular Expression of the T1,T2
given Language

2 Identities of Regular Expressions Construction of Regular T1,T2


Expression of the given Language

3 Construction of Language from the RE T2

4 FA and RE, DFA to RE Using Arden’s Theorem, Closure T2,R3


properties of RLs, Applications of Regular Expressions

5 RE to DFA (RE to e-NFA to DFA and RE to DFA Direct T2,R3


Method)
6 Pumping Lemma for RL , T1,T2
Interconversion between Left Linear and Right linear Grammar

7 Closure properties of RLs , Applications of Regular Expressions T1,T2

Tutorial No Topic
1 Construction of Regular Expression of the given Language, Construction of Language
from the RE, DFA to RE conversion Using Arden’s Theorem,

2
Text Books & Reference Books
• Text Books
1. Michael Sipser “Introduction to the Theory of Computation” CENGAGE Learning, 3rd
Edition ISBN-13:978-81-315-2529-6
2. Vivek Kulkarni, “Theory of Computation”, Oxford University Press, ISBN-13: 978-0-
19-808458-7

• Reference Books
1. Hopcroft Ulman, “Introduction To Automata Theory, Languages And Computations”,
Pearson Education Asia, 2nd Edition
2. Daniel. A. Cohen, “Introduction to Computer Theory” Wiley-India, ISBN:978-
81-265-1334-5
3. K.L.P Mishra ,N. Chandrasekaran ,“Theory Of Computer Science (Automata, Languages
and Computation)”, Prentice Hall India,2nd Edition
4. John C. Martin, “Introduction to Language and Theory of Computation”, TMH, 3rd
Edition ISBN: 978-0-07-066048-9

5. Kavi Mahesh, “Theory of Computation: A Problem Solving Approach”, Wiley- 3


India, ISBN: 978-81-265-3311-4
What is Regular expression

• Regular expressions are short notations that can denote complex and infinite regular
languages.

• In arithmetic, we can use the operations + and × to build up expressions such as


(5 + 3) × 4 .
• The value of the arithmetic expression is the number 32.

• Similarly, we can use the regular operations to build up expressions describing


languages, which are called regular expressions.
Example : (0 ∪ 1)0∗.

• The value of a regular expression is a language:


language of consisting of all strings starting with a 0 or a 1 followed by zero or any number
of 0s.

4
Definition of a Regular Expression
1. Regular expressions over Σ, include letters, ∅(empty set) and
ε(empty string of length zero).

2. Every symbol a € Σ is a regular expression over Σ.

3. If R1 and R2 are regular expressions over Σ, then so are


(R1+R2),(R1.R2) and (R1)*
[Where ‘+’ indicates union,
‘.’ indicates concatenation,
‘*’ indicates closure or repetitive concatenation]

4. Regular expressions are only those that are obtained using rules1-3.

5
Operators of RE

Regular Expression operators:


1. The Star operator :Closure
if r = a* then
L(r) = {€, a, aa, aaa, aaaa, ….}

2. The Dot operator :Concatenation


if r = a . b then
L(r) = {ab}

3. The Plus operator :Union


if r = a + b then
L(r) = {a, b}
6
Continued…
1. “a + b” stands for either a or b (parallel)

2. “a . b” stands for a followed by b (series)

3. “a*” stands for any no of occurrences of a(Closure).

a a
q0 q1 q0 a q1 b q2 q0

b
(2) (3)
(1)
7
Continued…
Concatenation of 2 sets:
U.V ={x|x=uv, u ⊆ U and v ⊆ V}
UV ≠ VU
U(VW)=(UV)W
E.g. U={000,111},V={101,010}

Closure of a set:
S*=S0 U S1 U S2…..
Where S0 ={€} and Si =Si-1.S for i>0
e.g. S={01,11}
S1= S0 .S ={€}.{01,11}
S2= S1 .S ={01,11} {01,11}
…..
S*={€ , 01,11,0101,0111,1101,1111,……}
8
Precedence of Regular Expression operators

The Star operator :Closure


The Dot operator :Concatenation
The Plus operator :Union
E.g.
r = 01*+1
L(r)={Set of all strings over {0,1} consisting of 1 or a 0 followed by
zero or more number of 1s}

r =(01)*+1
L(r)={ Set of all strings over {0,1} consisting of 1 or zero or more
number of 01}

r =0(1*+1)
L(r)={ Set of all strings over {0,1} starting with 0 followed by single
one or zero or more number of 1’s.
9
Identities of regular expressions
1. R U Φ=R
Adding the empty language to any other language will not change it.

2. Φ.R = R.Φ = Φ
if R = 0, then L(R) = {0} but L(R ◦ ∅) = ∅.

3. €.R =R.€ =R
Joining the empty string to any string will not change it

4. €*= € and Φ*= €


5. R+R= R
6. R*R *= R*
7. RR* = R*R
8. (R*)=R*
9. € +RR *= R*= € +R*R
10. (PQ)*P = P(QP)*
11. (P + Q)* = (P*Q*)* = (P*+Q*)*
12. (P+Q)R=PR+QR and R(P+Q)=RP+RQ
10
Examples

True or False?

Let R and S be two regular expressions. Then:

1. ((R*)*)* = R* ?

2. (R+S)* = R* + S* ?

11
Construction of RE from regular Language
Write RE to represent L over ∑* where ∑={0,1}:

1. Set of all strings of 0’s and 1’s over {0,1}


Ans: ∑={0,1},
L={0,1,10,01,11….}
R=(0 + 1)*

2. Set of all strings in which 0 is followed by any number 1’s


Ans: ∑={0,1},
L={0,01,011,0111,….}
R=01*
3. L={01, 10}
R=01 ∪ 10

4. The language of all binary strings over ∑={0,1} starting with 01


R=01(0+1)*

5. The language of all binary strings ending at 01


R=(0+1)*01.

6. The language of all binary strings having substring 01


12
R=(0+1)*01(0+1)*
Continued…
7. If L(r) ={aaa,aab,aba,abb,baa,bab,bba,bbb} find r.
R=(a+b)(a+b)(a+b)

8. If L(r) ={a,c,ab,cb,abb,cbb,abbb….} find r.


R=(a+c).b*

9. If L(r) ={€,x,xx,xxx,xxxx,xxxxx} find r.


R=(€+x) (€+x) (€+x) (€+x) (€+x)

10. The set of all strings of 0’s and 1’s such that the tenth
symbol from the right is 1.
R=(0+1)*.1.(0+1)9
13
Construction of Regular Language from RE
Describe the language represented by following R.E.
1) r=(0|1)*011
Ans: ∑={0,1},
L{r}=Set of all strings over {0,1} such that all strings end with 011
2) r= 0*1*2*
Ans: ∑={0,1,2},
L{r}=Set of strings over {0,1,2} with zero or more number of 0’s,
followed by zero or more number of 1’s,
followed by zero or more number of 2’s

3) r= 00*11*22*
Ans:∑={0,1,2},
L{r}=Set of all strings over {0,1,2}such that every string will have at least one 0
followed by at least one 1 followed by at least one 2.

4) r= (0*1*)*000(0+1)*
L=Set of all strings over {0,1} with 3 consecutive 0’s.

5)r= (0 ∪ ε)(1 ∪ ε)
L= {ε, 0, 1, 01}
14
Construction of Regular Language from RE

6) R= (∑∑)*
L={w| wє∑ and is a string of even length}

7) R= (∑∑∑)*
L={w| wє∑ and length of w is a multiple of 3}

8) Φ*= ?
L= €

9) R . € =?
L= R

15
Equivalence of FA and RE
•Finite automata (DFA and NFA) and regular languages are
equivalent.
•Every regular language can be recognized (accepted) by a finite
automata and,
•conversely, for every finite automata there is a regular language
which is the language accepted by the finite automata.

16
RE to NFA
1) a.b

2) a+b

17
3) a*

18
Example
Construct an NFA for the regular expression, (a +
b)* ab. Convert the NFA to its equivalent DFA .

Solution:
It is expected to construct a DFA that recognizes the
regular set: R = (a+b)· a · b

Let us first build the NFA with ε moves and the convert
the same to DFA.

The TG for NFA with ε moves is as follows,

19
Let us convert this NFA with ε -moves to its equivalent DFA
using a direct method.
20
RE to NFA

We have relabelled the states as well.


Let us see if we can minimize it.
21
The STF for the DFA looks like,

22
23
RE to FA

1. R=(a+b).(a+b)*

2. R=a.(a+b)*

24
Arden's Theorem
Statement −
Let P and Q be two regular expressions.
If P does not contain null string(ε),
then R = Q + RP has a unique solution that is R = QP*

Proof −
R = Q + (Q + RP)P [After putting the value R = Q + RP]
= Q + QP + RPP
When we put the value of R recursively again and again, we get the
following equation −
R = Q + QP + QP2 + QP3…..
R = Q (ε + P + P2 + P3 + …. )
R = QP* [As P* represents (ε + P + P2 + P3 + ….) ]
Hence, proved.

25
Arden's Theorem Example 1

Solution:
The state equations for the given DFA are:
q0 = q0 b + q2 a + ε
q1 = q0 a
q2 = q1 a + q1 b + q2 b
26
Arden's Theorem
q2 = q1 a + q1 b + q2 b
Substituting for q1 in q2,
q2 = q0aa + q0ab + q2 b
q2= q0 a (a + b) + q2 b
q2 = q0 a (a + b)b* ... using Arden's Theorem(R = Q + RP has a
unique solution that is R = QP*)
Substituting for q2 in q0,
q0 = q0 b + q2 a + ε
q0 = q0 b + q0 a (a + b)b* a + ε
q0 = q0 (b + a (a + b)b* a ) + ε
q0 = ε (b + a (a + b)b* a )* ... using Arden's Theorem

Hence, q0 = (b + a (a + b)b* a )*
q0 being the only final state for the DFA,
R= (b + a (a + b)b* a )* 27
Example 2.

Construct RE for the given FA.

q1=q1.a+q2.b+є
q2=q1.a+q2.b+q3.a
q3=q2.a

28
Using q3in q2 (q3=q2.a)
q2 =q1.a+q2.b+q3.a
q2=q1.a+q2.b+q2.a.a
q2=q1.a+q2(b+aa) {R = Q + RP has a unique solution that is R = QP*)
Applying Arden’s Theorem,
q2=q1.a(b+aa)*

Using q2 in q1 (q2=q1.a(b+aa)*)
q1=q1.a+q2.b+є
q1=q1.a+q1.a(b+aa)*.b+ є
q1= є+q1[a+a(b+aa)*.b] {R = Q + RP has a unique solution that is R = QP*)
Applying Arden’s Theorem,
q1= є. [a+a(b+aa)*.b]*
q1= [a+a(b+aa)*.b]*

q2= [a+a(b+aa)*.b]*.a(b+aa)* {q2=q1.a(b+aa)*}


q3= [a+a(b+aa)*.b]*.a(b+aa)*.a {q3=q2.a}
Is the RE for given FA
29
Limitations of RE

1. FA does not have the capacity to remember large amount


of information.

2. The head can not move in reverse direction.

3. It cannot recognize the languages which are not regular.


e.g. Palindrome.

4. FA cannot multiply the numbers.

30
31
32
33
34
35
Pumping lemma for Regular Languages
is used to prove that a language is not Regular

Let L be a regular language and M(Q,,,q1,F) is the finite automata with n


states.
Let L is accepted by M.
Let x L and | x | ≥ n , then x can be written as uvw ,where
(i) |v| > 0

(ii) |uv| ≤ n

(iii) uviw L for all i ≥0 , where vi denotes that v is repeated or pumped i times .
Pumping lemma Application

Example: Show that the given language


L={ ambm } is not regular
Ex 1. Show that the language L= {ambm} is not regular.
Answer:
Step 1:
Let us assume that L is regular and L is accepted by a FA with n states.

Step 2: Let us choose a string x= ambm


| x | = 2m >= n
Let us write x as uvw, with |v| > 0 and |uv|<=n
Since, |uv|<=n, u must be the form of as.
Since, |uv| <=n, v must be of the form ar | r>0
Now, ambm can be written as as ar am-s-r bm
as is u , ar is v , am-s-r bm is w

Step 3: Let us check whether uviw for i=2 belongs to L.


uv2w = as (ar)2 am-s-r bm = as a2r am-s-r bm = as+2r+m-s-rbm = am+r bm
Since r>0 , Number of a’s in am+r bm is greater than number of b’s .
Therefore, uv2w ⊄ L.
Hence by the contradiction we can say that the given language is not regular.
Q. Show that the language L= {xx|xє(a,b)*} is not regular.
Answer:
Step 1:
Let us assume that L is regular and L is accepted by a FA with n states.

Step 2: Let us choose a string x= amb, xx= amb.amb


| xx | = m+1+m+1=2m+2 >= n
Let us write x as uvw, with |v| > 0 and |uv|<=n
Since, |uv| <=n, u must be of the form as
Since, |uv|<=n, v must be the form of ar | r>0

Now, xx= amb.amb can be written as as ar am-s-rb.amb


as is u , ar is v , am-s-rb.amb is w

Step 3: Let us check whether uviw for i=2 belongs to L.


uv2w = as (ar)2 am-s-r b.amb = as a2r am-s-r b.amb
= as+2r+m-s-rb.amb = am+r b.amb ≠ L
Therefore, uv2w ⊄ L.
Hence by the contradiction we can say that the given language is not regular.
Closure properties of Regular Languages

Regular Grammar : A grammar is regular if it has rules of form A -> a or


A -> aB or A -> ɛ where ɛ is a special symbol called NULL.

Regular Languages : A language is regular if it can be expressed in terms


of regular expression.

40
Closure properties of Regular Languages
Union : If L1 and If L2 are two regular languages, their union L1 ∪ L2 will also
be regular.

For example,
L1 = {an | n ≥ 0} and L2 = {bn | n ≥ 0}
L3 = L1 ∪ L2 = {an ∪ bn | n ≥ 0} is also regular.

Intersection : If L1 and If L2 are two regular languages, their intersection L1


∩ L2 will also be regular.

For example,
L1= {am bn | n ≥ 0 and m ≥ 0} and L2= {am bn ∪ bn am | n ≥ 0 and m ≥ 0}
L3 = L1 ∩ L2 = {am bn | n ≥ 0 and m ≥ 0} is also regular.

41
Closure properties of Regular Languages
Concatenation : If L1 and If L2 are two regular languages, their
concatenation L1.L2 will also be regular.

For example,
L1 = {an | n ≥ 0} and L2 = {bn | n ≥ 0}
L3 = L1.L2 = {am . bn | m ≥ 0 and n ≥ 0} is also regular.

Kleene Closure : If L1 is a regular language, its Kleene closure L1* will


also be regular.

For example,
L1 = (a ∪ b)
L1* = (a ∪ b)*
42
Closure properties of Regular Languages
Complement

If L(G) is 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.

For example,
L(G) = {an | n > 3}
L’(G) = {an | n <= 3}

43
Application of Regular Expression

• Application in Linux
Example : Text File Search , Unix tool: egrep
Editing Commands , cw :Change word

• Application in Search Engine

• Web application

• Regular Expressions in Lexical Analysis


Example : C programming language
Left linear grammar to right linear grammar

Every left linear grammar can be represented by an equivalent


right linear grammar

The conversion process involves drawing of an intermediate


transition graph .

Steps as follows :
1. Represent the left linear grammar using the transition graph
2. Interchange the start state as end state
3. Reverse the direction of all transitions
4. Write Right linear grammar from the transition graph

45
Examples
Q. Write an equivalent right linear grammar from the given left
linear grammar
S->C0|A0|B1
A->A1|C0|B1|0
B->B1|1
C->A0

Answer : After following the conversion steps


S-> 1B|0A
A-> 0C|1A|0
B-> 1B|1A|1
C-> 0C|0

46
Examples
Q. Write an equivalent right linear grammar from the given left linear
grammar

S->S10|0

Answer: After following the conversion steps


S->0X|0
X->10X|10

47
Right linear grammar to Left linear grammar

Every right linear grammar can be represented an equivalent left


linear grammar .
Conversion process involves drawing of an intermediate transition
graph.

Steps to follow :
1. represent the right linear grammar using the transition graph .
2.interchange the start state as final state
3. Reverse the direction of all transition
4. Write left linear grammar from the transition graph .

48
Examples

Q. Write an equivalent left linear grammar from the


given right linear grammar .
S->bB|b
B-> Bc
B->aB
C->a
B->b

Answer : After following the conversion steps


S->b|Bb|Ca
B->Ba|b
C->Bb
49
Examples

Q. Write an equivalent left linear grammar from the given right


linear grammar.
S->0A|1B
A->0C|1A|0
B->1B|1A|1
C->0|0A

Answer : After following the conversion steps


S-> C0|A0|B1
A->A1|C0|B1|0
B->B1|1
C->A0

50
Theory of Computation
TY BTech

CONTEXT FREE GRAMMAR(CFG)


&
PUSH DOWN AUTOMATA(PDA)
Course Objectives & Course Outcomes
Course Objectives:
•To understand the basics of automata theory and its operations.
•To understand problem classification and problem solving by machines.
•To study computing machines by describing, classifying and comparing different types
of computational models.
•To understand the fundamentals of decidability and computational complexity.

Course Outcomes:
• After completion of this course students will be able:
•To construct finite state machines to solve problems in computing.
•To write mathematical expressions and syntax verification for the formal languages.
•To construct and analyze Push Down Automata and Turing Machine for formal
languages.
•To express the understanding of decidability and complexity.
Text Books & Reference Books

• Text Books
•John C. Martin, Introduction to Language and Theory of Computation, TMH,
3rdEdition, ISBN: 978-0-07-066048-9.
•Vivek Kulkarni, Theory of Computation, Oxford University Press, ISBN-13: 978-0-
19-808458-7.
• Reference Books
•K.L.P Mishra,N. Chandrasekaran,Theory of Computer Science (Automata, Languages
and Computation), Prentice Hall India, 2nd Edition.
•Michael Sipser, Introduction to the Theory of Computation,CENGAGE Learning, 3rd
Edition, ISBN:13:978-81-315-2529-6.
•Daniel Cohen, Introduction to Computer Theory, Wiley India, 2nd Edition, ISBN:
9788126513345.
•Kavi Mahesh, Theory of Computation: A Problem Solving Approach, 1st Edition,
Wiley-India, ISBN: 978-81-265-3311-4.
Unit III – CFG & PDA
CFG
Formal definition of Grammar, Chomsky Hierarchy, CFG : Formal
definition of CFG, Derivations, Parse Tree, Ambiguity in grammars and
languages, Language Specification using CFG, Normal Forms: Chomsky
Normal Form and Greibach Normal Form. Closure properties of CFL.

Pushdown automata : Definition, Acceptance of PDA by final State


and Empty Stack, Designing PDA, Equivalence of Pushdown automata
and CFG, Deterministic Pushdown Automata, Nondeterministic
Pushdown Automata.
Planner - Lectures
Lecture No Topics Covered
1 Chomsky Hierarchy, CFG-Formal Definition, CFL
2 CFG <-> CFL, Derivation, Parse Tree
3 Ambiguity, CNF
4 Conversion of CFG to CNF, Closure Properties
5 GNF, PDA, Formal Definition, ID
6 Acceptance of a string, Designing PDA
7 PDA and NPDA Examples
8 Converting PDA to CFG

Planner - Tutorial
Tutorial No Topics Covered
1 Designing CFG for a CFL, Derivation of a string,
Ambiguous Grammer
2 Converting CFG to CNF, Designing PDA
3 Designing PDA, Design CFG for a PDA
5
Introduction

•Finite Automata accept all regular languages and only regular languages

•Many simple languages are non regular:

- {anbn : n = 0, 1, 2, …}
- {w : w a is palindrome}

and there is no finite automata that accepts them.


• Context-Free Languages are a larger class of languages that encompasses
all regular languages and many others, including the two above.
Context-Free Languages and Regular
Languages

Context-Free Languages

n n R
{a b : n  0} {ww }

Regular Languages
a *b * ( a  b) *
Formal Definition of a Grammar

Grammar: G  V , T , S , P 

Set of
variables
Set of Start Set of
terminal variable productions
symbols
Language for the Grammar

Grammar:
SaSb
Sє

Language of the grammar:

L  {a b : n  0}
n n
A Convenient Notation
*
We write: S  aaabbb
for zero or more derivation steps
Instead of:
S  aSb  aaSbb  aaaSbbb  aaabbb
Generalizing:
w1  w2  w3    wn
Trivially:
*
w  w
Formal Definition-CFG

Definition: A context-free grammar is a 4-tuple (V, T, P, S),


where:

• V is a set (each element in V is called nonterminal)


• T is an alphabet (each character in T is called terminal)
• P, the set of rules, is a subset of V  (T  V)*
If (,)  P, we write production   

 is called a sentential form

• S, the start symbol, is one of the symbols in V


Example of Context-Free Grammar

productions
P={SaSb, Sє}

G  V , T , S , P 

V  {S }
T  {a, b} start variable
variables
terminals
Context-Free Language

A language L is context-free
if there is a context-free grammar G
with L  L(G )
Context-Free Language-Example 1

L  {a b : n  0 }
n n

is a context-free language
since context-free grammar : G

{SaSb | є}
generates L(G )  L
Context-Free Language – Example 2
Context-free grammar : G
{SaSa | bSb | є}
Example derivations:
S  aSa  abSba  abba
S  aSa  abSba  abaSaba  abaaba

L(G )  {ww : w {a, b}*}


R

Palindromes of even length


Derivation and Parse Trees

Consider the following example grammar


with 5 productions:

SAB AaaA|ϵ BBb|ϵ


Leftmost and Rightmost Derivation
Consider the following example grammar
with 5 productions:
SAB AaaA|ϵ BBb|ϵ
Leftmost derivation: for the string aab

S  AB  aaAB  aaB  aaBb  aab

Rightmost derivation: for the string aab

S  AB  ABb  Ab  aaAb  aab


Leftmost and Rightmost Derivation
SAB AaaA|ϵ BBb|ϵ
Leftmost derivation:
S  AB  aaAB  aaB  aaBb  aab
Rightmost derivation:
S  AB  ABb  Ab  aaAb  aab
S

Give same A B

derivation tree a a A B b

ϵ ϵ
Context Free Language for a CFG
Find the CFL generated for the given CFG.
1. SaSb | ab

L(G)={an bn | n>=1}

2. SaB |bA
Aa |aS |bAA
Bb |bS |aBB

L(G)={x|x containing equal no of a’s and b’s}


CFG for a CFL
1. Write the grammar for generating strings over
Σ={a}, containing any(zero or more) number of a’s.
S  a | aS | ϵ

2. Find the CFG represented by the RE (a+b)*


S  aS | bS | ϵ

3. Write the grammar for all strings consisting of a’s


and b’s with atleast 2 a’s.
SAaAaA
AaA | bA | ϵ
Ambiguous Grammar

A context-free grammar G is ambiguous


if there is a string w L(G ) which has:

two different derivation trees


or
two leftmost derivations

(Two different derivation trees give two


different leftmost derivations and vice-versa)
Ambiguous Grammar – An Example

E  E  E | E  E | (E) | a

string a  a  a has two derivation trees


E E

E  E E  E

a E  E E  E a

a a a a
Ambiguous Grammar – An Example

E  E  E | E  E | (E) | a

string a  a  a has two leftmost derivations


E  E  E  a E  a EE
 a  a E  a  a*a
E  EE  E  EE  a EE
 a  aE  a  aa
Removing Ambiguity
Equivalent
Ambiguous
Non-Ambiguous
Grammar
Grammar
E E E
E  E T |T
E  E E
T T  F | F
E  (E )
E a F  (E ) | a
generates the same
language
E  E T T T  F T  a T  a T F
 a  F F  a  aF  a  aa
E
E  E T |T
T T F | F E  T

F  (E) | a T T  F
Unique derivation
tree and leftmost F F a
derivation
For the string
a+a*a
a a
Another ambiguous grammar
S→iCtS|iCtSeS|a
C→b
• For the string ibtibtaea
• Two leftmost and rightmost derivations.
• Grammar is ambiguous.
The equivalent unambiguous grammar is:
S → A| B
A→iCtAeA|a
B→iCtS|iCtAeB
C→b
Simplification of Context Free Grammar
A CFG G can be simplified as:
1. Each variable and terminal of the CFG should appear in the
derivation if at least one word in the L(G).
2. There should not be any production of the form AB where A
and B are both non-terminals.

Simplification techniques are:


1.Removal of Useless Symbols
2.Removal of Unit Productions
3.Elimination of  production

27
Removal of Useless symbol
Ex.1 Consider the following grammar:
G=({S,A},{1,0}, P, S)
Where P consists of the following productions:
S1 0 |0 S 1 |1 S 0 |A |S S
Simplify the grammar by removing the useless symbols if any.

Ex.2 Consider the following grammar:


G= ({S,A,B}, {a}, P, S}
Where P consists of the following productions:
S A B | a
Aa
Simplify the grammar by removing the useless symbols if any.
28
Removal of Unit Production
• A production rule of the form AB where A and B are both non-
terminals is called a unit production.
• All the other productions (including  productions) are non-unit
productions.

Ex.1 Consider the following grammar:


G= ({A, B},{a, b}, P, A}
Where P consists of :
AB
Ba | b
Simplify the grammar by removing the unit productions if any.

29
Removal of Unit Production
Ex.2Consider the following grammar:
S a | X b | a Y a | b | a a
XY
Yb|X
Simplify the grammar by removing the unit productions if any.

30
Removal of  - Production
A production of the form A  where A is non-terminal is known
as  - Production.

Ex.1 Consider the following grammar,


S a S a | b S b | 
Simplify the grammar by eliminating  - Productions if any.

Ex.2 Consider the following grammar,


Sa|Xb|aYa
XY | 
Y b | X
Simplify the grammar by eliminating  - Productions if any.

31
Chomsky Hierarchy

1. Type 0 grammar (Unrestricted Grammar)


2. Type 1 grammar (Context Sensitive Grammar)
3. Type 2 grammar (Context Free Grammar)
4. Type 3 grammar (Regular Grammar)
Chomsky Hierarchy
• Type 3 grammar:
It is also called as regular grammar.
A α
Recognized by FSM.
Two types of Regular Grammar are:
1. Left Linear Grammar 2. Right Linear Grammar

• Type 2 grammar:
It is also called as context free grammar.
Aα where A is NT and α is sentential form.
Start symbol of the Grammar can also appear on RHS.
Recognized by PDA(Pushdown Automata)
Chomsky Hierarchy
• Type 1 grammar:
It is context sensitive grammar or context dependent.
1. α  β where length of β is at least as much as the length of α
except S .
2. The rule S  is allowed only if start symbol S does not appear
on RHS.
3. Productions are of the form
α1Aα2 α1 β α2 (β ≠ )
Recognized by TM(Turing Machine).

34
Chomsky Hierarchy

• Type 0 grammar:
It is unrestricted grammar that is no restriction on production.
α β (α ≠ )
Recognized by TM(Turing machines)
These languages are known as Recursively Enumerable Languages.

35
Normal Forms

There are certain standard ways of writing CFG.


They satisfy certain restrictions on the productions in the CFG.
Then the G is said to be in Normal forms.

1. Chomsky Normal Form (CNF)


2. Greibach Normal Form (GNF)
Chomsky Normal Form(CNF)

A CFG is in CNF if every production is of the form

A a, where A is NT and a is T
A  BC where A,B and C are NTs
S   is in G if  belongs to L(G).

When  is in L(G),
we assume that S does not appear on the RHS of any production.
e.g. G is :
SAB| 
A a
Bb
Construction of G in CNF

Step 1. Elimination of null productions and unit productions using


previous method.
Let the G is (V,T,P,S)
Step 2. Elimination of terminals on RHS.
Step 3. Restricting the number of variables on RHS.
Reduce to CNF

1. S aAD
A aB| bAB
3. S ~S |[ S ɔ ) S ]|p|q
Bb
Dd
4. SABA
A aA | ϵ
2. S aAbB
B bB | ϵ
A aA | a
B bB | b
Greibach Normal Form

A context free grammar is said to be in Greibach


Normal Form if all productions are in the following
form:
A → αX
• A is a non terminal symbols
• α is a terminal symbol
• X is a sequence of non terminal
symbols.
It may be empty.
Lemma1- CFG
If ABɤ
where A & B are NT and there are production of the form
B  β1 |β2 | β3 … | βs
then
replace A  Bɤ by
A βj ɤ 1<=i<=s

e.g. ABab
B aA |bB |aa |AB
Then
A aAab | bBab | aaab |ABab
Lemma2- CFG
If A Aα1 | Aα2 | Aα3 ---| Aαɤ |β1 | β2 | β3--- | βs
Such that
βi ‘s do not start with A then
A  β1 | β2 | β3--- | βs
A  β1 Z| β2 Z | β3 Z--- | βs Z

Z  α1 | α2 | α3 ---| αɤ
Z α1 Z | α2 Z | α3 Z ---| αɤ Z

e.g. A aBD |bDB |c


A AB | AD
α1 = B α2 = D β1 =aBD β2 = bDB β3 = c then
A aBD |bDB |c , A aBDZ | bDBZ |cZ
Z B|D , Z  BZ | DZ
Closure properties of CFL
CFLs are closed under:
Union
Concatenation
Kleene closure operator
Substitution
Homomorphism, inverse homomorphism
Reversal
CFLs are not closed under:
Intersection
Difference
Complementation

43
Push Down Automata
Context-Free Languages

Context-Free Pushdown
Grammars Automata

stack

automaton
Pushdown Automaton - PDA
Input String

Stack

States
Initial Stack Symbol

Stack Stack

stack
$ z top
head

bottom special symbol


Appears at time 0
Formal definition of PDA
The PDA is as:
A =(Q,Σ, Г ,δ,q0,Z0,F)
Where
Q : A finite set of states
Σ : A finite set of input symbols
Г : A finite stack alphabet or pushdown symbols
δ : the transition function Q X(Σ U {ϵ } ) X Г to the set of finite
subsets of Q X Г*
q0: the start state
Z0 : the start symbol(pushdown symbol)
F : the set of accepting state or final states
Transition Function

δ :The transition function is a triple δ(q,a,x)


where
1. q is a state in Q
2. a is either an input symbol in Σ or a =ϵ, the empty string,
3. x is a stack symbol, that is a member of Г.

The output of δ is a finite set of pairs (p,ɤ )


Where
p is the new state and
ɤ is the string of stack symbols that replaces x at the top of the
stack.
Instantaneous Description of a PDA

The configuration of a PDA by a triple (q,w,ɤ) where

1. q is the state
2. w is the remaining input
3. ɤ is the stack contents
we show the top of the stack at the left end of ɤ and
the bottom at the right end.
Such a triple is called an Instantaneous Description
or ID of a PDA.
δ(q, a, b) = (q, c)

stack
b top c
h Replace h
e e
$ $
δ(q, a, b) = (q, cb)

stack c
b top b
h Push h
e e
$ $
δ(q, a, b) = (q, ϵ)

stack
b top
h Pop h
e e
$ $
δ(q, a, b) = (q, b)

stack
b top b
h No Change h
e e
$ $
Acceptance by PDA

Acceptance of input strings by PDA can be defined in terms of final


states or in terms of PDS(pushdown store).
Let A= (Q,Σ, Г ,δ,q0,Z0,F) be a PDA.

The set accepted by final state is defined by


T(A) ={w ϵ Σ* | (q0,w,Z0 ) Ⱶ * (qf , ʌ, α ) for some qf ϵ F and α ϵ Г*}

The set accepted by null store(or empty store)is defined by


N(A) = {w ϵ Σ* | (q0,w,Z0 ) Ⱶ * (q , ʌ, ʌ ) for some q ϵ Q }
PDA for L={anbn | n>0 }
Logic:
W

Let
A= (Q, Σ, Г , δ, q0, Z0, F)
Q={q0,q1},
Σ={a,b},
Г={a,Z0},
F={q1}
is a PDA.
δ (Transition Function) by Final State is
Move no State input stack Move
symbol
1 q0 a Z0 (q0,aZ0)
2 q0 a a (q0,aa)
3 q0 b a (q1, )
4 q1 b a (q1,)
5 q1  Z0 (q1, Z0)

Acceptance of a string aabb


(q0, aabb , Z0)
Ⱶ (q0, abb , aZ0)
Ⱶ (q0, bb , aaZ0)
Ⱶ (q1, b , aZ0)
Ⱶ (q1,  , Z0)
δ (Transition Function) by Empty stack or Empty
store or Null stack is
Move no State input stack Move
symbol
1 q0 a Z0 (q0,aZ0)
2 q0 a a (q0,aa)
3 q0 b a (q1, )
4 q1 b a (q1)
5 q1  Z0 (q1, )

Acceptance of a string aabb


(q0, aabb , Z0)
Ⱶ (q0, abb , aZ0)
Ⱶ (q0, bb , aaZ0)
Ⱶ (q1, b , aZ0)
Ⱶ (q1,  , Z0)
Ⱶ (q1,  , )
Ex: PDA to accept language of palindromes with the
marker. i.e. L={xcxr | x  {a,b}*}

Let
A= (Q, Σ, Г , δ, q0, Z0, F) is a PDA.
where
Q={q0, q1, qf},
Σ={a, b, c},
Г={a, b, Z0},
F={qf}
δ (Transition Function) by Final State is
Move State input stack symbol Move
no
1 q0 a Z0 (q0,aZ0)
2 q0 b Z0 (q0,bZ0)
3 q0 a a (q0,aa)
4 q0 b b (q0,bb)
5 q0 a b (q0, ab)
6 q0 b a (q0, ba)
7 q0 c Z0 (q1, Z0)
8 q0 c a (q1, a)
9 q0 c b (q1, b)
10 q1 a a (q1, )
11 q1 b b (q1, )
12 q1  Z0 (qf, Z0)
δ (Transition Function) by Empty stack or Empty
store or Null stack is
Move no State input stack Move
symbol
1 q0 a Z0 (q0,aZ0)
2 q0 b Z0 (q0,bZ0)
3 q0 a a (q0,aa)
4 q0 b b (q0,bb)
5 q0 a b (q0, ab)
6 q0 b a (q0, ba)
7 q0 c Z0 (q1,Z0)
8 q0 c a (q1,a)
9 q0 c b (q1,b)
10 q1 a a (q1, )
11 q1 b b (q1, )
12 q1  Z0 (q1,  )
Deterministic and non-deterministic PDA

DPDA:
transition function is :
Q X Σ X Г  Q X Г*
e.g. δ(q,a,Z) is either empty or a singleton.
δ(q,a,Z) ≠ Ø

NPDA:
Q X Σ U {ϵ } X Г  finite subsets of Q X Г*
e.g. δ(q,a,Z) = {(p1,ɤ1) ,(p2, ɤ2)…..(pm, ɤm)}
DPDA and NPDA
NPDA and DPDA

• For every NPDA, there may not exist an equivalent DPDA.

• The NPDA can accept any CFL, while DPDA is a special case of
NPDA that accepts only a subset of the CFLs accepted by the
NPDA.

• Thus, DPDA is less powerful than NPDA.


NPDA to accept language of palindromes without
the marker. L={xxr | x  {a,b}*}

Let
A= (Q, Σ, Г , δ, q0, Z0, F) is a PDA.
where
Q={q0, q1, qf},
Σ={a, b},
Г={a, b, Z0},
F={qf}
NPDA to accept language of all palindrome strings

Move no State input stack symbol Move


1 q0 a Z0 {(q0,aZ0), (q1, Z0)}
2 q0 b Z0 {(q0,bZ0), (q1, Z0)}
3 q0 a a {(q0,aa),(q1,a)}
4 q0 b a {(q0,ba), (q1,a)}
5 q0 a b {(q0, ab), (q1,b)}
6 q0 b b {(q0, bb), (q1,b)}
7 q0  Z0 {(q1,Z0)}
8 q0  a {(q1,a)}
9 q0  b {(q1,b)}
10 q1 a a {(q1, )}
11 q1 b b {(q1, )}
12 q1  Z0 {(qf, Z0)}
CFG to PDA
Theorem: If L is a CFL then we can construct a PDA A accepting L by empty
store ie. L=N(A).
Proof: We construct A by making use of productions in G.
Let L=L(G) where G=(V, T, P, S) is a CFG.
We construct PDA A as
A= (Q, Σ, Г , δ, q, Z0,F)
where Σ=T
Г is (V U T)
Z0 = S
F=Ф
δ is defined as
R1 : δ(q, , A) = {(q,  ) | A  is in P}
R2 : δ(q, a, a) = {(q,  ) } for every a in Σ.
CFG to PDA
1 .Construct a PDA for the CFG
S 0BB
B0S | 1S |0
Test whether 0104 is in N(A).

We construct PDA A as
A = (Q, Σ, Г , δ, q, Z0,F)
Q = {q}
Σ = {0,1} Move State input stack symbol Move
Г = {S, B, 0, 1} no
Z0= S 1 q  S {(q,0BB)}
F=Ф 2 q  B {(q,0S), (q,1S), (q,0)}
3 q 0 0 {(q, )}
4 q 1 1 {(q,)}
CFG to PDA
1 .Construct a PDA for the CFG
S 0BB
B0S | 1S |0
Test whether 0104 is in N(A).

2. Convert the grammar


SaSb | A
AbSa |S| 

To a PDA that accepts the same language by empty stack.


Thank You…!!!

71
Theory of Computation
TY BTech

Unit IV
Turing Machine
Course Objective & Course Outcomes
• Course Objectives:
1. To Study computing machines by describing, classifying and comparing
different types of computational models.
2. Encourage students to study Theory of Computability and Complexity.
• Course Outcomes:
After successful completion of this course students will be able to:
1. Construct finite state machines to solve problems in computing
2. Write mathematical expressions for the formal languages
3. Apply well defined rules for syntax verification
4. Construct and analyze Push Down, Post and Turing Machine for formal languages
5. Express the understanding of the decidability and Undecidability problems
6. Express the understanding of computational complexity.
Text Books & Reference Books
• Text Books
1. Michael Sipser “Introduction to the Theory of Computation” CENGAGE
Learning, 3rd Edition ISBN-13:978-81-315-2529-6
2. Vivek Kulkarni, “Theory of Computation”, Oxford University Press,
ISBN-13: 978-0-19-808458-7

• Reference Books
1. Hopcroft Ulman, “Introduction To Automata Theory, Languages And
Computations”, Pearson Education Asia, 2nd Edition
2. Daniel. A. Cohen, “Introduction to Computer Theory” Wiley-India, ISBN:978-
81-265-1334-5
3. K.L.P Mishra ,N. Chandrasekaran ,“Theory Of Computer Science (Automata,
Languages and Computation)”, Prentice Hall India,2nd Edition
4. John C. Martin, “Introduction to Language and Theory of Computation”, TMH,
3rd Edition ISBN: 978-0-07-066048-9
5. Kavi Mahesh, “Theory of Computation: A Problem Solving Approach”, Wiley-
India, ISBN: 978-81-265-3311-4
Unit 4
Unit IV: Turing Machine
Formal definition of a Turing machine, Design of Turing
Machines, Recursive Languages and Recursively Enumerable
Languages, Universal Turing Machine, Nondeterministic Turing
machines. Designing TM Concept of Decidability, Un-
decidability, Halting Problem of TM

- 7 Hrs

Source of Slides: Introduction to Automata Theory, Languages, and Computation


By John E. Hopcroft, Rajeev Motwani and Jeffrey D. Ullman
and Theory of Computation by Vivek Kulkarni
Church-Turing’s Thesis

Everything that is algorithmically computable


is computable by a Turing machine
Turing Machine
• Invented by Alan Turing in 1936

• A simple mathematical model of a general purpose


computer

• It is capable of performing any calculation which can be


performed by any computing machine
The Language Hierarchy

a b c ?
n n n ww ?

Context-Free Languages
a nb n ww R
Regular Languages
a* a *b *
Language Accepted by TM

n n
a b c n ww

Context-Free Languages
n
a b n R
ww NDPA
Regular Languages
Finite
a * a *b * Automata
Elements of a Turing Machine
• A TM consists of the following:
• A tape head : read / write a symbol at a time, move to
left/right
• An infinite tape: contains square cells in which
symbols can be written
• A finite set of symbols
• A finite set of states

Finite control

… B B X1 X2 … Xi Xn B B …
Turing Machine
• A TM consists of a finite control (i.e. a finite state
automaton) that is connected to an infinite tape.
• Initially the input consists of a finite-length string of
symbols and is placed on the tape.
• To the left of the input and to the right of the input,
extending to infinity, are placed blanks.
• The tape head is initially positioned at the leftmost cell
holding the input.
Finite control

… B B X1 X2 … Xi Xn B B …
Turing Machine
• In one move the TM will:

• Change state, which may be the same as the current state

• Write a tape symbol in the current cell, which may be the same as
the current symbol

• Move the tape head left or right one cell


Formal Definition of TM
Formally, the Turing Machine is denoted by the 7-tuple:

M = (Q, , Γ, δ, q0, B, F)

Q = finite states of the control


 = finite set of input symbols, which is a subset of Γ below
Γ = finite set of tape symbols

δ = transition function. δ : (Q x Γ) -> (Q x Γ x D)


e.g. δ(q, X) = (p, Y, {L, R, N})
Where p = next state, Y = new symbol written on the tape,
D = direction to move the tape head(left, right, No move)

q0= start state for finite control


B = blank symbol. This symbol is in Γ but not in .
F = set of final or accepting states of Q.
The Tape
No boundaries -- infinite length
...... ......

Read-Write head

The head moves Left or Right


The Tape
...... ......

Read-Write head

The head at each time step:

1. Reads a symbol
2. Writes a symbol
3. Moves Left or Right
Transition Function
Takes two arguments:
1. A state, in Q
2. A tape symbol in Γ

δ(q, Z) is either undefined or a triple of the form (p, Y, D)


where
p is a state
Y is the new tape symbol
D is a direction, L or R
Example:
Time 0
...... a b a c ......

Time 1
...... a b k c ......

1. Reads a
2. Writes k
3. Moves Left
Time 1
...... a b k c ......

Time 2
...... a f k c ......

1. Reads b
2. Writes f
3. Moves Right
The Input String
Input string Blank symbol

...... ␢ ␢ a b a c ␢ ␢ ␢ ......

head
Head starts at the leftmost position
of the input string
‘␢’ are treated as left and right brackets for the
input written on the tape.
States & Transitions
Read Write
Move Left

q1 a b, L q2

Move Right

q1 a b, R q2
...... ␢ ␢ a b a c ␢ ␢ ␢ ......

Example:

q1 a b, R q2
q1
current state
Time 1
...... ␢ ␢ a b a c ␢ ␢ ␢ ......

q1

Time 2
...... ␢ ␢ a b b c ␢ ␢ ␢ ......

q2

q1 a b, R q2
Time 1
...... ␢ ␢ a b a c ␢ ␢ ␢ ......

q1

Time 2
...... ␢ ␢ a b b c ␢ ␢ ␢ ......

q2

q1 a b, L q2
Time 1
...... ␢ ␢ a b a c ␢ ␢ ␢ ......

q1

Time 2
...... ␢ ␢ a b b c g ␢ ␢ ......

q2

q1 ␢ g, L q2
Instantaneous Descriptions(ID) of a Turing
Machine

• Sometimes it is useful to describe what a TM does in terms of its


ID (instantaneous description), just as we did with the PDA

• The ID shows all non-blank cells in the tape, pointer to the cell the
head is over with the name of the current state
– use the turnstile symbol ├ to denote the move.
– As before, to denote zero or many moves, we can use
├*
Instantaneous Descriptions(ID) of a TM

␢ ␢ c a b a ␢ ␢

q1
ca q1 ba
(1) For constructing the ID, we simply insert the current state in the input
string to the left of the symbol under the R/W head.

(2) We observe that the blank symbol may occur as part of the left or right
substring.
␢ x a y b ␢ ␢ ␢ x a y b ␢ ␢

q2 q0

q2 xayb  x q0 ayb

Fall 2006 Costas Busch - RPI 26


Instantaneous Descriptions(ID) of a TM
Example
Acceptance of Input

If machine halts
Accept Input
in a final state

If machine halts
in a non-final state
Reject Input or
If machine enters
an infinite loop
Example 1 :aa*

a a, R

␢ ␢, L
q0 q1
␢ ␢ a a a ␢ ␢ ␢

q0

a a, R

␢ ␢, L
q0 q1
␢ ␢ a a a ␢ ␢

q0

a a, R

␢ ␢, L
q0 q1
␢ ␢ a a a ␢ ␢

q0

a a, R

␢ ␢, L
q0 q1
␢ ␢ a a a ␢ ␢

q0

a a, R

␢ ␢,L
q0 q1
␢ ␢ a a a ␢ ␢

q1

a a, R Halt & Accept

␢ ␢, L
q0 q1
Rejection Example

Time 0 ␢ ␢ a b a ␢ ␢

q0

a a, R

q0 ␢ ␢, L q1
Time 1 ␢ ␢ a b a ␢ ␢

q0
No possible Transition
a a, R Halt & Reject

q0 ␢ ␢, L q1
Infinite Loop Example

b b, L
a a, R

␢ ␢, L
q0 q1
␢ ␢ a b a ␢ ␢

q0

b b, L
a a, R

␢ ␢, L
q0 q1
␢ ␢ a b a ␢ ␢

q0

b b, L
a a, R

q0 ␢ ␢, L
q1
␢ ␢ a b a ␢ ␢

q0

b b, L
a a, R

q0 ␢ ␢,L q1
Time 2 ␢ ␢ a b a ␢ ␢
q0
Time 3 ␢ ␢ a b a ␢ ␢
q0
Time 4 ␢ ␢ a b a ␢ ␢
q0

Time 5 ␢ ␢ a b a ␢ ␢
q0
... Infinite Loop
Because of the infinite loop:

• The final state cannot be reached

• The machine never halts

• The input is not accepted


Design of TM steps
1. Definition of TM
2. Logic
3. Transition function
4. Instantaneous Description of a string
Turing Machine Examples
1. Design TM to recognize all strings of even number of 1’s.
Assume the string is made up of only 1’s
Solution
M = ({q0, q1}, {1}, {1}, δ, q0, B, {q0})
1 B
q0 q1BR Accept

q1 q0BR Reject

Instantaneous Description: w= 1111


q01111B |- B q1111B |- BBq011B |- BBB q11B|- BBBBq0B|-
Accept
Turing Machine Examples
2. Design a TM which can compute a concatenation function over
∑={1}
If the pair of words {w1,w2} is the input, output has to be w1w2.
B 1 1 B 1 1 1 B B 1 1 1 1 1 B B

Solution
M = ({q0, q1}, {1}, {1}, δ, q0, B, {q0})
Logic: Replace separating symbol ‘B’ by ‘1’
Replace rightmost ‘1’ by ‘B’
δ: 1 B
q0 q01R q11R
q1 q1 1R q2BL
q2 q3BR
*q3 Accept
Turing Machine Examples
3. Design TM that will replace every occurrence of substring
11 by 10 keeping everything intact
Solution
M = ({q0, q1}, {0,1}, {0,1}, δ, q0, B, {q0})
0 1 B
q0 q00R q11R Accept

q1 q00R q00R Accept

Instantaneous Description: w= 01101110110


4. Design a TM for L= {0n1n |n>=1}

0 1 x y B
q0 q1, x, R - - q3, y, R q4, B,
N(accept)
q1 q1, 0, R q2, y, L - q1, y, R -
q2 q2, 0, L - q0, x, R q2, y, L -
q3 - - - q3, y, R q4, B,
R(accept)
q4 - - - - -

Instantaneous Description: w= 0011


Practice Problems

5. Design TM that recognizes strings


containing equal number of 0’s and 1’s

6. Design TM that checking if a set of


parentheses are well-formed
Turing Machine Examples
5. Design TM that recognizes strings containing equal
number of 0’s and 1’s
Solution Instantaneous Description: w= 01101110110
M = ({q0, q1, q2, q3, q4}, {0,1}, {0,1, *}, δ, q0, B, {q0})
0 1 * ;
q0 q1*R q3*R q0*R q4N(Accept)

q1 q10R q2*L q1*R q4N(Reject)

q2 q20L q21L q2*L q0 ; R

q3 q2*L q31R q3*R q4N(Reject)

q4 - - - -
Turing Machine Examples
6. Design TM that checking if a set of parentheses are well-
formed
M = ({q0, q1, q2, q3,}, {(,)}, {(,), *, ;, O, Rp, Lp}, δ, q0, B, {q3})
Simplified Functional Matrix:

( * ) ; Rp Lp O
q0 R R q1*L q2L - - -

q1 q0*R L L q3RpN - - -

q2 q3LpN L L q3ON - - -

q3 Final state

Instantaneous Description: w= (( ))
) *L * L
( R

q0 q1
* R ( *R
) L

; L
; Rp N
( Lp N
* L
q2 q3
Halt

) L ; ON

; ( ( ) ) ( ) ;
q0
Complexity of Turing Machine
The complexity of a TM is directly proportional to the size of the
functional matrix. In other words, we can say that the
complexity of a TM depends on the number of symbols that are
being used and the number of states of the TM. Hence:
Complexity of a TM = |  |  | Q | (or | I |  | S | ),
where,
|  | = Cardinality of tape alphabet (i.e., number of tape
symbols), and
| Q | = Number of states of the TM.
 For example,
If  = {1, 0, a, c, ; , *} and Q = {q0, q1, q2, q3, q4 = halt}
Then, the complexity of the TM = |  |  | Q | = 6  5 = 30
Recursively Enumerable
and
Recursive
Languages
Recursively Enumerable and Recursive Languages

Non Recursively Enumerable

Recursively Enumerable

Recursive
Recursively Enumerable and Recursive
Languages
A TM recognizes a language iff it accepts all and only
those strings in the language.

A language L is called Turing-recognizable or recursively


enumerable iff some TM recognizes L.

A TM decides a language L iff it accepts all strings in L


and rejects all strings not in L.
A language L is called decidable or recursive iff some
TM decides L.
Recursively Enumerable and Recursive
Languages

Definition:
A language is recursive if some Turing machine
accepts it and halts on any input string

In other words:
A language is recursive if there is a membership
algorithm for it
Recursive and Recursively Enumerable Languages
To summarize we can say that,
 Recursively Enumerable Set
 A set S of words over Σ is said to be recursively enumerable,
if there is a TM over Σ, which accepts every word in S and
either rejects or loops for every word in ~S (~S = Σ*  S ).
This can be represented as:
 Accept TM = S
 Reject (TM)  loop (TM) = Σ*  S
 Recursive Set
 A set S of words over Σ is said to be recursive, if there is a
TM over Σ, which accepts every word in S and rejects every
word in ~ S (~S
= Σ*  S). This can be represented as:
 Accept (TM) = S
 Reject (TM) = Σ * S
 Loop (TM) = 
Recursively Enumerable and Recursive
Languages

A language is called Turing-recognizable or recursively


enumerable (r.e.) if some TM recognizes it
A language is called decidable or recursive if some
TM decides it

r.e. recursive
languages languages
Multi Tape Turing Machine
 Multi-tape Turing machines have
k number of independent tapes,
having their own read/write
heads. These machines have
independent control over all the
heads—any of these can move
and read/write their own tapes.
All these tapes are unbounded at
both the ends just as in the single-
tape TM.
 Multi-tape TM and single-tape
TM are equivalent in power
(except for some difference in
execution time)
Universal Turing Machine
Universal Turing Machine (UTM)
 Turing machine that can simulate any other Turing Machine

 It accepts the encoded Functional Matrix of any other TM as input on


its tape (Program area)

 It also accepts the data on which the other TM needs to be simulated


(Data area of the tape)

 UTM needs an imitation algorithm that can simulate the functional


matrix of any other TM (System area)

 Functional Matrix of such a UTM is analogous to an Operating


System

 UTM is analogous to a modern Computer!


Universal Turing Machine
Tape 1
Three tapes

Description of M
Universal
Tape 2
Turing
Machine
Tape Contents of M

Tape 3

State of M
64
Non-Deterministic Turing Machine
A nondeterministic Turing Machine (NTM) differs
from the deterministic variety by having a transition
function δ such that for each state q and tape symbol
X, δ(q, X) is a set of triples
{(q1,Y1,D1), (q2,Y2,D2), ……….. (qk,Yk,Dk)}

Where k is any finite integer. The NTM can choose, at


each step, any of the triples to be the next move. It
cannot, however, pick a state from one, a tape symbol
from another, a the direction from yet another.
NTM and DTM
Theorem: Every NTM has an equivalent (deterministic) TM
Proof: Simulate NTM with a deterministic TM
Cstart

• computations of M are a tree


rej acc • nodes are configurations
• fanout is b = maximum number
of choices in transition function
• leaves are accept/reject
configurations.
Simulating NTM M with a deterministic TM:
Breadth-first search of tree
• if M accepts: we will encounter accepting leaf and accept
• if M rejects: we will encounter all rejecting leaves, finish traversal
of tree, and reject
• if M does not halt on some branch: we will not halt as that branch is
infinite…
Simulating NTM M with a deterministic TM:
o use a 3 tape TM:
• tape 1: input tape (read-only)
• tape 2: simulation tape (copy of M’s tape at point
corresponding to some node in the tree)
• tape 3: which node of the tree we are exploring (string in
{1,2,…b}*)
o Initially, tape 1 has input, others blank
NTM and DTM
Solvable and Semi-Solvable Problems
 Solvable problem: TM when applied to such a problem, always
eventually terminates with the correct “yes” or “no” answer
 A class of all such problems is called as Recursive language
 The mathematical functions that denote these type of problems are called as
Total Recursive Functions
 Simple Examples - multiplication, addition, concatenation and many other

 Semi-solvable problem: TM when applied to such a problem, always


eventually terminates with correct answer when answer is “yes” and may
or may not terminate when the correct answer is “no”
 A class of all such problems is called as Recursively Enumerable language
 The mathematical functions that denote these type of problems are called as
Partial Recursive Functions
 Simple Examples - division, factorial and many other
Halting Problem and Unsolvablity
 For a given input for any general TM two
cases arise,
 The machine may halt after a finite
number of steps
 The machine may not ever halt no
matter how long it runs
 Given any TM, problem of algorithmically
determining whether it ever halts or not, is
called as the Halting Problem
 The halting problem is unsolvable
Proof :-Halting problem is undecidable

• Proof by contradiction
• There is a Turing machine T that will decide the halting
problem.<M> this is the description of Turing machine M
and string W . T write "accept" when M halts on w, and
reject If M does not halts on W
Proof :-Halting problem is undecidable

We build a Turing machine Tm and here we


standardizing T so when T write yes and halt then Tm
will goes into loop forever
Proof :-Halting problem is undecidable

we build Tc with the help of TM. Here we take Tc as input which


is the description of Turing machine M and we write it in this way
d(M) now we copies it to obtain the string d(M)*d(M), where * is
a symbol that break up the two copies of d(M) and then provide
d(M)*d(M) to the Turing machine Tm .
Proof :-Halting problem is undecidable

What Turing machine Tc does when a string given to it which


describe Tc itself
d(Tc) is given as input to Tc it make copy of it and build the
string d(Tc)*d(Tc) and allot to standardized T.so the altered T
is specified a description of Tc and string d(Tc)
END

You might also like