Unit 1
Unit 1
AND AUTOMATA
UNIT I
1
UNIT 1
2
Introduction of Automaton
An automata is an abstract self-propelled computing device which follows a predetermined sequence of operations
automatically.
Automata-based programming is a programming paradigm in which the program or its part, is thought as a model of a
finite state machine or any other formal automation.
input: switch
3
Introduction of Automaton
1. Electric Switch
2. Fan Regulator
4
Simple Computer
Example:
OTHER EXAMPLES
1.Electric Switch
input : switch
Output : light bulb 2.Fan Regulator
BATTERY
Actions : flip switch 3.Automatic door controller
States : on, off
4.Binary string with divisible by 4
f
bulb is on if and only if there was an odd number of flips
start off on
f
5
Types of Automata
6
Introduction of Formal Proof - Basic Definitions
7
3. Empty string: e or ε denotes the empty sequence of symbols.
4. Language over alphabet Σ - a set of strings over Σ.
◦ Notation: L.
◦ Examples:
◦ {0, 00, 000, ...} is an "infinite" language over the binary alphabet.
◦ {a, b, c} is a "finite" language over the English alphabet.
8
Binary relations on strings
9
3. Substring - u is a substring of v if there are x and y such that v = xuy.
◦ Examples:
◦ let is a substring of appleton since appleton =
app.let.on
◦ 0 is a substring of 0 since 0 = epsilon.0.epsilon
10
Introduction to Mathematical concepts
Inductive Proof
Deductive Proof
11
INDUCTIVE PROOF
Induction Principle:
If we prove S(i) and we prove that for all n>=I, S(n)=>S(n+1), then we
may conclude that s(n) for all n>=1.
It has two parts.
1.Basis part.
2.Inductive part.
12
Basis step: We have to show S(i) for a particular integer value “i”, here
“i “ may be zero or one.
13
Problems
Induction Principle:
If we prove S(i) and we prove that for all n>=I, S(n)=>S(n+1), then we may
conclude that s(n) for all n>=1.
14
Basis:
Put n=0 in the equ 1
RHS= (0 (0+1) (2*0+1))/6 => 0
LHS = i=0 ∑ n (02) => 0
i.e., LHS = RHS.
Inductive step:
Put n =1 in the equ 1
S(n) = (n(n+1)(2n+1))/6.
= (2n3+2n2+n2+n)/6. ---> Equ 2
15
Put n=n+1 in the equ 1
S(n+1) => ((n+1)(n+2)(2n+3))/6.
=> ((n2+3n+2) (2n+3))/6.
=> (2n3+6n2+4n+3n2+9n+6)/6.->equ 3
From the induction principle:
= S(n+1) =S(n) + (n+1)2.
= ((2n3+3n2+n)/6) + (n+1)2
16
= ((2n3+3n2+n)/6) + (n2+2n+1)
Cross multiplying the ‘6’ on the numerator.
= ((2n3+3n2+n + 6n2+12n+6))/6.
= ((2n3+9n2+13n+6))/6. --🡪 equ 4
Equ 4 satisfies equ 3
Hence proved.
17
Deductive proof:
Consists of sequence of statements whose truth lead us from some
initial statement called the hypothesis or the give statement to a
conclusion statement.
Deductive Principle:
The theorem is proved when we go from a hypothesis H to a conclusion
C is the atatement “ if H then C”. i.e C is deduced from H
18
Additional forms of proof:
Proof of sets
Proof by contradiction
Proof by counter example
19
Direct proof (AKA) Constructive proof: If p is true then q is true
Eg: if a and b are odd numbers then product is also an odd number.
Odd number can be represented as 2n+1
a=2x+1, b=2y+1 product of a X b = (2x+1) X (2y+1)= 2(2xy+x+y)+1 =
2z+1 (odd number)
20
Proof by Contrapositve
The Contrapositive of the statement “ ifH then C” is “if not C then not
H”.A statement and its contrapositive are either both true or both false.
Theorem R U (S ∩ T) =(R U S) ∩ (R U T)
21
22
Proof by Contradiction:
23
Regular Expressions
24
Regular Expressions
• Example:
25
Recursive Definition
Are regular
Expressions
26
Regular Expressions
A regular expression:
27
Regular Expressions
Example
28
Regular Expressions
29
Regular Expressions
• Regular expression:
30
Regular Expressions
• Regular expression
• Regular expression
31
32
Example Problems in Regular Expressions
33
Solve Problems in Regular Expressions
34
Deterministic Finite Automata (DFA)
35
Formal definition of DFA
DFA M = (Q, Σ, δ, s, F)
Where,
◦ Q is finite set of states
◦ Σ is input alphabet
◦ s ∈ Q is initial state
◦ F ⊆ Q is set of final states
-
◦ δ: Q X Σ > Q
36
DFA
37
How does a DFA work?
After reading input string,
◦ if DFA state final, input accepted
◦ if DFA state not final, input rejected
Language of DFA -- set of all strings accepted by DFA.
38
Pictorial representation of
DFA
39
Formal definition of FA
40
41
Transition Diagram
42
Example
43
Transition table
44
Example
45
Extending the Transition functions to the strings
46
47
Language of DFA
48
Example: Diagram of DFA
2n + 1
L = {a | n >= 0}
Answer:
L = {a, aaa, aaaaa, ...}
49
Problem 1
Given a DFA accepting the following language over the alphabet {0,1}
50
In this problem ,
“00” as 2 inputs and it needs 3 states
51
Formal definition of L(M)
L(M) - Language accepted by M
Define δ*:
◦ δ*(q, ε) = q
◦ δ*(q, wσ) = δ(δ*(q,w),σ)
52
Problem 2
53
Regular Languages
Definition: A Language is regular iff there is a DFA that
accepts it.
Examples:
◦φ
◦ {ε}
◦ Σ*
◦ {w in {0,1}* | second symbol of w is a 1} Exercise
◦ {w in {0,1}* | second last symbol of w is a 1} Exercise
◦ {w in {0,1}* | w contains 010 as a substring} - (importance?)
54
Closure properties of Regular Languages
Regular languages are closed under:
◦ Union
◦ Notation: ∪
◦ Intersection
◦ Notation: ∩
55
Examples
Let Σ = {a,b}.
Let L1 = { w in Σ* | w has even number of a's}.
◦ Is L1 regular?
L2 = { w in Σ* | w has odd number of b's}.
◦ Is L2 regular?
L1 ∪ L2 = ?
◦ Ans: {w in Σ* | w has even a's or odd b's}.
L1 ∩ L2 = ?
◦ Ans: {w in Σ* | w has even a's and odd b's}.
56
Problem 3
57
Problem 4
58
Problem 5
59
Problem 6
60
Problem 8
61
Problem 9
62
Problem 10
Given a DFA accepting the following language over the alphabet {0,1}
Set of all the strings with 3 consecutive 0’s (not necessarily at the end).
63
Problem 11:
Given a DFA accepting the following language over the alphabet {0,1}
64
Problem 13
Given a DFA accepting the following language over the alphabet {0,1}
65
Problem 14
Given a DFA accepting the following language over the alphabet {0,1}
66
Problem 14
:
Consider the following transition diagram and to check the input string
110111,011101 is accepted or not.
67
Transition table:
0 1
🡪 A A B
B C B
C A D
*D A B
68
Input: 110111
δ( A,є) =A.
δ( A,1) =B.
δ( A,11) =δ( A,1),1) =δ( B,1) =B.
δ( A,110) =δ( A,11),0) =δ( B,0) = C.
δ( A,1101) =δ( A,110),1) =δ( C,1) = D.
δ( A,11010) =δ( A,1101),0) =δ( D,1) = B.
δ( A,110101) =δ( A,11010),0) =δ( B,1) = B.
It does not reach the final state.
The given string is not accepted by DFA.
69
Input: 011101
δ( A,є) =A.
δ( A,0) =A.
δ( A,01) =δ( A,0),1) =δ( A,1) =B.
δ( A,011) =δ( A,01),1) =δ( B,1) = B.
δ( A,0111) =δ( A,011),1) =δ( B,1) = B.
δ( A,01110) =δ( A,0111),0) =δ( B,0) = C.
δ( A,011101) =δ( A,011101),0) =δ( C,1) = D.
It reaches the final state.
The given string is accepted by DFA.
70
Problem 15
Give a DFA accepting the following language over the alphabet {0,1}.
All the strings with a substring 01 and to check the given input string 10010 is
accepted or not by a DFA.
0 1
q0 q1 q0
q1 q1 q2
*q2 q2 q2
71
Input 10010
δ( q0,є) =q0.
δ( q0,1) =q0.
δ( q0,10) =δ( q0,1),0) =δ( q0,0) =q1.
δ( q0,100) =δ( q0,10),0) =δ( q1,0) =q1.
δ( q0,1001) =δ( q0,100),1) =δ( q1,1) =q2.
δ( q0,10010) =δ( q0,1001),0) =δ( q2,0) =q2.
It reaches the final state
The given string is accepted by DFA.
72
Problem16 :
Design a DFA to accept the language
L ={w/w has an even number of 0’s and even number of 1’s}and to check the given
input strings are 110101 and 10010 is accepted or not by an DFA.
73
Let A be a DFA which has A={Q,∑,δ,q0,F)
Transition diagram:
74
Input: 110101
δ( q0,є) =q0.
δ( q0,1) =q1.
δ( q0,11) =δ( q0,1),1) =δ( q1,1) =q0.
δ( q0,110) =δ( q0,11),0) =δ( q0,0) =q2.
δ( q0,1101) =δ( q0,110),1) =δ( q2,1) =q3.
δ( q0,11010) =δ( q0,1101),0) =δ( q3,0) =q1.
δ( q0,110101) =δ( q0,11010),0) =δ( q1,1) =q0.
It reaches the final state. The given string is accepted
by DFA.
75
Input: 10010
δ( q0,є) =q0.
δ( q0,1) =q1.
δ( q0,10) =δ( q0,1),0) =δ( q1,0) =q3.
δ( q0,100) =δ( q0,10),0) =δ( q3,0) =q1.
δ( q0,1001) =δ( q0,100),1) =δ( q1,1) =q0.
δ( q0,10010) =δ( q0,1001),0) =δ( q0,0) =q2.
The state q2 is not a accepting state.
The given string is not accepted by DFA.
76
Nondeterministic Finite Automaton (NFA)
Generalization of DFA. Allows:
◦ 0 or more next states for the same (q, σ).
◦ Guessing
◦ Transitions labeled by the empty string.
◦ Changing state without reading input
Motivation: Flexibility.
◦ Easier to prove many closure properties.
77
How does an NFA work?
w is accepted by an NFA provided there is a sequence of guesses that
leads to a final state.
Language accepted by NFA is the set of all strings accepted by it.
78
Nondeterministic Finite Automata:
o Generalize FAs by adding nondeterminism,allowing several alternative computations
on the same input string.
o Ordinary deterministic FAs follow one path on each input.
79
Nondeterministic Finite Automata:
80
Nondeterministic Finite Automata:
NFA Example
81
Nondeterministic Finite Automata:
NFA Example
82
Problem 1
83
Formal definition of NFA
Notation: Σe = Σ U {e}.
NFA M = (Q, Σ, Δ, s, F) where:
◦ Q - finite set of states
◦ Σ - input alphabet
◦ s - initial state
◦ F ⊆ Q - set of final states
◦ Δ is a subset of Q X Σe X Q.
84
Formal definition of NFA acceptance
Δ*(q0, 1) = ?
◦ Ans: {q0, q1}
Δ*(q0, 11) = ?
◦ Ans: {q0, q1, q2}
85
NFA acceptance
86
NFA vs. DFA
Is NFA more powerful than DFA?
◦ Ans: No.
Theorem:
◦ For every NFA M there is an equivalent DFA M'
Proof Idea:
◦ NFA is in a set of states at any point during reading a string.
◦ DFA will use a lot of states to keep track of this.
Important Assumption:
◦ No transition labeled by epsilon.
(Will get rid of this assumption later.)
87
Equivalent DFA construction
NFA M = (Q, Σ, Δ, s, F)
DFA M' = (Q', Σ, δ, s', F') where:
◦ Q' = 2Q
◦ s' = {s}
◦ F' = {P | P ∩ F is nonempty}
◦ δ({p1, p2, pm}, σ) = Δ*(p1, σ) ∪ Δ*(p2, σ) ∪ ... ∪ Δ*(pm, σ)
i.e. find all the states that can be reached on σ from all the
NFA states in a DFA state.
88
Example: Equivalent DFA construction
NFA
89
Equivalent DFA construction
90
How to handle epsilon transitions?
91
Handling epsilon transitions (contd.)
Extend e-closure to sets of states by:
◦ e-closure({s1, ... , sm}) = e-closure(s1) ∪ ... ∪ e-closure(sm)
Now let
s' = e-closure({s}).
and,
δ({p1,..., pm}, σ) = e-closure(Δ*(p1, σ)) ∪ ... ∪ e-closure(Δ*(pm, σ))
92
Example: Handling epsilon transitions.
93
DFA = ?
94
Language Operations
1. Concatenation. Notation: L·L' or just LL'
◦ L · L' = {uv | u in L, v in L'}.
2. Kleene Star. Notation: L*
◦ L* = { w in Σ* | w = w1...wk for some k >= 0 and each wi in L}.
95
Closure properties of regular languages.
Previously we saw closure under ∪ and ∩.
New: Regular languages are closed under
◦ Concatenation
◦ Kleene star
◦ Complement.
96
Examples
97
Examples
98
Construction for L∙L'
L’’ = (K,Σ,Δ,s,F)
K = K 1 ∪ K2
s = s1
F = F2
Δ = Δ1 ∪ Δ2 ∪ F1 X {e} X
{s2}
99
L* and L'*
L*
M = (K, Σ, Δ, s, F)
K = {s} ∪ K1
F = {s} ∪ F1
Δ = Δ1 ∪ F1 X {e} X {s1} ∪ {(s, e,
s1)}
Given M1 = (K1, Σ, Δ1, s1, F1)
L’*
100
Complement of L and L'
Complement of L
Complement of L’
101
General Construction for Complement
DFA M = (K, Σ, δ, s, F)
K = K1
s = s1
F = K - F1
δ = δ1
L(M) = Complement of L(M1)
DFA M1 = (K1, Σ, δ1, s1, F1)
Exercise: Will this construction work for NFAs?
Explain your answer.
102
Regular expressions versus FA's
Regular expressions generate exactly the class of regular languages.
Theorem:
◦ (a) For every regular expression there is an equivalent NFA
◦ (b) For every DFA there is an equivalent regular expression.
Proof of (a):
◦ For φ, the NFA is:
103
Parse Tree for (a U b)* ∙ b
* b
a b
104
Example: NFA for (a U b)*b
aUb
105
Example (contd.) : NFA for (a U b)*b
(aUb)*
106
Example (contd.) : NFA for (a U b)*b
(a U b)*b
107
DFA → regular expression
Easier to do:
◦ DFA → GNFA → regular expression.
108
DFA → regular expression (contd.)
Idea:
◦ Convert DFA → special GNFA.
◦ Eliminate all states, except start and final state, one state at a time.
◦ Output the label on the single transition left at the end.
109
Eliminating state q{rip}
R4
qi qj
R1 R3
qrip
110
Constructing regular expression.
111
Constructing regular expression (contd.)
Added: a new start state and a new final state with empty transitions
112
Constructing regular expression (contd.)
qrip
Before we take out qrip, we have to see
all the paths to the qrip state and
all possible transitions.
We take out the qrip state and it leaves us with a 3 state Finite Automata
113
Constructing regular expression (contd.)
qrip
We take out the qrip state and we are left with the regular expression
114
Converting NFA to DFA
115
Converting NFA to DFA
116
Converting NFA to DFA
117
Converting NFA to DFA
118
Converting NFA to DFA
119
Converting NFA to DFA
120
Converting NFA to DFA
121
FINITE AUTOMATA WITH
OUTPUTS
The finite automata which we considered earlier have binary output
i.e. they accept the string or do not accept the string.
◦ decided on the basis of reachability of the final state by the initial state.
Now we remove this restriction and consider the model where the
outputs can be chosen from some other alphabet.
FINITE AUTOMATA WITH OUTPUTS
The value of the output function Z{t} is
◦ In the most general case it is a function of the present state q(t) and
the present input x(t)
Z(t) = λ(q(t), x(t)) (Mealy Machine)
◦ λ 🡪 output function
◦ If Z(t) depends only on the present state
Z(t) = λ(q(t)) (Moore Machine)
◦ Moore 🡪 convenient in automata theory
Moore Machine
six tuple (Q, Σ, Δ, λ, δ, q0):
◦ Q is a finite set of states
◦ Σ is the input alphabet
◦ Δ is the output alphabet
◦ δ is the transition function Σ x Q 🡪 Q.
◦ λ is the output function mapping Q 🡪 Δ and
◦ q0 is the initial state
Next state δ
Present State Output
a=0 a=1
🡪q0 q3 q1 0
q1 q1 q2 1
q2 q2 q3 0
q3 q3 q0 0
Mealy Machine
a six tuple (Q, Σ, Δ, λ, δ, q0)
◦ all the symbols except λ have the same meaning
λ is the output function mapping Σ x Q 🡪 Δ
Next States
🡪q1 q3 0 q2 0
q2 q1 1 q4 0
q3 q2 1 q1 1
q4 q4 1 q3 0
Mealy to Moore
Consider the machine described by the transition table given
below:
Next State
Present input a = 1
input a = 0
State
State Output State Output
🡪q1 q3 0 q2 0
q2 q1 1 q4 0
q3 q2 1 q1 1
q4 q4 1 q3 0
Mealy to Moore
Solution
At the first stage develop the procedure so that both
the machines accept exactly the same set of input
sequences.
We look into next state column for any state (say qi)
and determine the number of different outputs
associated with qi in that column.
Now we split into several different states, the number
of such states being equal to the number of different
outputs associated with qi.
Mealy to Moore
For example in this problem q1 is associated with
one output 1 and q2 is associated with two
outputs 0 and 1.
Similarly q3 and q4 are associated with the outputs
1 and 0, 1.
Now we split q2 into q20 and q21.
Similarly q4 is split into q40 and q41.
Mealy to Moore
Now the given table is reconstructed for the new
states as given below:
Next State
Present
input a = 0 input a = 1
State
State Output State Output
🡪q1 q3 0 q20 0
q20 q1 1 q40 0
q21 q1 1 q40 0
q3 q21 1 q1 1
q40 q41 1 q3 0
q41 q41 1 q3 0
Mealy to Moore
The pair of states and outputs in the next state column can be rearranged
as given below:
Next State
Present State Output
a=0 a=1
🡪q1 q3 q20 1
q20 q1 q40 0
q21 q1 q40 1
q3 q21 q1 0
q40 q41 q3 0
q41 q41 q3 1
Mealy to Moore
Here we observe that the initial sate q1 is associated with output 1.
This means that with input ε we get an output of 1, if the machine
starts at state q1.
◦ Moore accepts 0-length sequence but not Mealy
To overcome this
◦ we must neglect the response of a Moore machine to input ε
(or)
◦ we must add a new starting state q0.(state transitions are identical with
those of q1 whose o/p is 0)
Mealy to Moore
Next State
Present State Output
a=0 a=1
🡪q0 q3 q20 0
q1 q3 q20 1
q20 q1 q40 0
q21 q1 q40 1
q3 q21 q1 0
q40 q41 q3 0
q41 q41 q3 1
Moore to Mealy
Consider the Moore machine described by the transition table given the
table below:
Next state δ
Present State Output
a=0 a=1
🡪q0 q3 q1 0
q1 q1 q2 1
q2 q2 q3 0
q3 q3 q0 0
Moore to Mealy
Must follow the reverse procedure of converting Mealy machine
into Moore machine.
In Moore machine
◦ For every input symbol we form the pair consisting of the next state
and the corresponding output and reconstruct the table for mealy
machine.
Moore to Mealy
For example the states q3 and q1 in the next state
column should be associated with outputs 0 and 1
respectively.
Next State
Present State input a = 0 input a = 1
State Output State Output
🡪q0 q3 0 q1 1
q1 q1 1 q2 0
q2 q2 0 q3 0
q3 q3 0 q0 0
Applications of the Concepts AND New Developments
Applications include:
◦ Text processing (editors, etc.)
◦ Compiler design
◦ Verification through model checking
Development’s include :
DNA Computing
Quantum Turing Machines
138