FLA Unit I PDF

Download as pdf or txt
Download as pdf or txt
You are on page 1of 141

18CSC301T – FORMAL LANGUAGE

AND AUTOMATA

UNIT I

1
UNIT 1

• Minimization of DFA
• Introduction to Automaton
• Problems related to Minimization of DFA
• Mathematical concepts
• Regular Languages : Equivalence of Finite
• Formal Languages: Strings, Languages, Properties Automata and Regular Languages
• Finite Representation : Regular Expressions • Equivalence of Finite Automata and Regular
Grammars
• Problems related to regular expressions
• Problems related to Equivalence of Finite
• Finite Automata :Deterministic Finite Automata Automata and Regular Languages and Regular
• Nondeterministic Finite Automata Grammars
• Variants of Finite Automata :Two-way Finite
• Finite Automaton with €- moves Automaton Mealy Machines
• Problems related to Deterministic and • Properties of Regular Languages: Closure
Nondeterministic Finite Automata Properties
• Problems related to Finite Automaton with €- • Set Theoretic Properties & Other Properties
moves
• Pumping Lemma

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.

What is Automata Theory?


• Automata theory is the study of abstract computational devices
• Abstract devices are (simplified) models of real computations
• Computations happen everywhere: On your laptop, on your cell phone, in nature, …
Example:

input: switch
BATTER output: light bulb
Y
actions: flip switch
states: on, off

3
1. Electric Switch

2. Fan Regulator

3. Automatic door controller

4. Binary string with divisible by 4

4
Simple Computer

Example:
OTHER EXAMPLES

1.Electric Switch
input : switch
Output : light bulb 2.Fan Regulator
BATTER
Y
Actions : flip switch 3.Automatic door controller
States : on, off 4.Binary string with divisible by 4

f
on bulb is on if and only if there was an odd number of flips
start off
f

5
Types of Automata

finite automata Devices with a finite amount of memory.


Used to model “small” computers.
push-down Devices with infinite memory that can be
automata accessed in a restricted way.
Used to model parsers, etc.

Turing Devices with infinite memory.


Machines Used to model any computer.

6
Introduction of Formal Proof - Basic Definitions

1. Alphabet - a finite set of symbols.


◦ Notation: Σ .
Examples: Binary alphabet {0,1},
English alphabet {a,...,z,!,?,...}
2. String over an alphabet Σ - a finite sequence of symbols
from Σ.
◦ Notation: (a) Letters u, v, w, x, y, and z denote strings.
(b) Convention: concatenate the symbols. No
parentheses or commas used.
◦ Examples: 0000 is a string over the binary alphabet.
a!? is a string over the English alphabet.

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.

5. Empty language - empty set of strings. Notation: Φ.


6. Binary operation on strings: Concatenation of two strings u.v -
concatenate the symbols of u and v.
◦ Notation: uv
◦ Examples:
◦ 00.11 = 0011.
◦ ε.u = u.ε = u for every u. (identity for concatenation)

8
Binary relations on strings

1. Prefix - u is a prefix of v if there is a w such that v =


uw.
◦ Examples:
◦ ε is a prefix of 0 since 0 = ε0
◦ apple is a prefix of appleton since appleton = apple.ton

2. Suffix - u is a suffix of v if there is a w such that v =


wu.
◦ Examples:
◦ 0 is a suffix of 0 since 0 = ?
◦ ton is a suffix of appleton since ?

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

Observe that prefix and suffix are special cases


of substring.

10
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

11
Introduction to Mathematical concepts

Inductive Proof
Deductive Proof

12
INDUCTIVE PROOF

It is a special form of proof that deals about the objects in recursion.

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.

13
Basis step: We have to show S(i) for a particular integer value “i”, here
“i “ may be zero or one.

Inductive step: We assume n>=i, where “i “ is the basis integer and we


have to show that if S(n) then S(n+1) i.e., S(n) =>S(n+1).

14
Problems

Prove by an induction method on “n”


i=0 ∑ n i2 =((n(n+1)(2n+1))/6).
Solution:
i=0 ∑ n i2 =((n(n+1)(2n+1))/6). ----> Equ 1

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.

15
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

16
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

17
= ((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.

18
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

19
Additional forms of proof:
Proof of sets
Proof by contradiction
Proof by counter example

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

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

22
23
Proof by Contradiction:

H and not C implies falsehood.

24
Regular Expressions

25
Regular Expressions

• Regular expressions describe regular languages

• Example:

describes the language

26
Recursive Definition

Given regular expressions and

Are regular
Expressions

27
Regular Expressions

A regular expression:

Not a regular expression:

28
Regular Expressions

: language of regular expression

Example

29
Regular Expressions

• For regular expressions and

30
Regular Expressions

• Regular expression:

31
Regular Expressions

• Regular expression

• Regular expression

= { all strings containing substring 00 }

32
33
Example Problems in Regular Expressions

34
Solve Problems in Regular Expressions

35
Deterministic Finite Automata (DFA)

36
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

37
DFA

The DFA has:


◦ a finite set of states
◦ 1 special state - initial state
◦ 0 or more special states - final states
◦ input alphabet
◦ transition table containing
(state, symbol) -> next state

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

39
Pictorial representation of
DFA

(q,σ) -> q'

40
Formal definition of FA

41
42
Transition Diagram

43
Example

44
Transition table

A transition table is a conventional tabular representation of functions like δ


that takes the two argument and returns another state.
The row of the table corresponding to the states.
The column of the table corresponding to the inputs.

45
Example

46
Extending the Transition functions to the strings

47
48
Language of DFA

49
Example: Diagram of DFA
2n + 1
L = {a | n >= 0}

Answer:
L = {a, aaa, aaaaa, ...}

50
Problem 1

Given a DFA accepting the following language over the alphabet {0,1}
Set of all the strings ending with 00.

51
In this problem ,
“00” as 2 inputs and it needs 3 states

52
Formal definition of L(M)
L(M) - Language accepted by M
Define δ*:
◦ δ*(q, ε) = q
◦ δ*(q, wσ) = δ(δ*(q,w),σ)

Definition: L(M) = { w in Σ* | δ* (s,w) in F }.

53
Problem 2

Example: L(M) = {w in {a,b}* | w contains


even no. of a's}

54
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?)

55
Closure properties of Regular Languages
Regular languages are closed under:
◦ Union
◦ Notation: ∪
◦ Intersection
◦ Notation: ∩

L1 ∪ L2 is regular if L1 and L2 are regular.


L1 ∩ L2 is regular if L1 and L2 are regular.

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

By closure properties, both these are regular.

57
Problem 3

DFA of L1 = {w in {a,b}* | w has even


number of a's}.

58
Problem 4

DFA of L2 = { w in {a,b}* | w has odd number


of b's}.

59
Problem 5

DFA of L1 ∪ L2 = {w in {a,b}* | w has even


a's or odd b's}.

60
Problem 6

DFA of L1 ∩ L2 = {w in {a,b}* | w has even


a's and odd b's}.

61
Problem 8

DFA of A = {w | w contains at least one 1 and an


even number of 0s follow the last 1}

62
Problem 9
Given a DFA accepting the following language over the
alphabet {0,1} Set of all the strings ending with 00.

63
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).

64
Problem 11:

Given a DFA accepting the following language over the alphabet {0,1}

Set of all the strings with a 011 as a substring.

65
Problem 13

Given a DFA accepting the following language over the alphabet {0,1}

Set of all the strings beginning with a 101.

66
Problem 14

Given a DFA accepting the following language over the alphabet {0,1}

Set of all the strings with three consecutive zeros.

67
Problem 14

:
Consider the following transition diagram and to check the input string
110111,011101 is accepted or not.

68
Transition table:

Let A be a DFA which has A={Q,∑,δ,q0,F)

0 1
A A B

B C B

C A D
*D A B

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

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

71
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

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

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

It has the 4 states like q0,q1,q2,q3.


q0: Number of 1’s and number of 0’s are even.
q1: Number of 0’s is even and number of 1’s is odd.
q2: Number of 0’s is odd and number of 1’s is even.
q3: Number of 0’s and number of 1’s are odd.

74
Let A be a DFA which has A={Q,∑,δ,q0,F)
Transition diagram:

75
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.
76
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.

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

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

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

80
Nondeterministic Finite Automata:

81
Nondeterministic Finite Automata:
NFA Example

82
Nondeterministic Finite Automata:
NFA Example

83
Problem 1

Example: {w in {0,1}* | the second last


symbol of w is a 1}

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

If (p, u, q) in Δ, then NFA in state p can read u and go to q.

85
Formal definition of NFA acceptance

Define Δ*(q, w) as a set of states: p ε Δ*(q, w) if there is a


directed path from q to p labeled w
◦ Example: consider NFA of Lecture 3

Δ*(q0, 1) = ?
◦ Ans: {q0, q1}

Δ*(q0, 11) = ?
◦ Ans: {q0, q1, q2}

86
NFA acceptance

w is accepted by NFA M iff Δ*(q0, w) ∩ F is nonempty.

L(M) = {w in Σ* | w is accepted by M}.

87
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.)

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

89
Example: Equivalent DFA construction

NFA

90
Equivalent DFA construction

91
How to handle epsilon transitions?

Define e-closure of state q as Δ*(q, ε).


◦ notation: e-closure(q).
Example:

92
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, σ))

to complete construction of DFA.

93
Example: Handling epsilon transitions.

94
DFA = ?

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

Examples: if L = {a(2n+1) | n >= 0}. L' = {b(2n) | n > = 0}.


LL' = ?
◦ Ans: LL' = {a(2n+1) b(2m) | n, m > = 0}
L* = ?
◦ Ans: {an | n >= 0}
U, ., * are called regular operations.

96
Closure properties of regular languages.
Previously we saw closure under ∪ and ∩.
New: Regular languages are closed under
◦ Concatenation
◦ Kleene star
◦ Complement.

97
Examples

L = {w in {a,b}* | w has even a’s }

98
Examples

L' = {w in {a,b}* | w has at least one b}

99
Construction for L∙L'

L’’ = (K,Σ,Δ,s,F)
K = K 1 ∪ K2
s = s1
F = F2
Δ = Δ1 ∪ Δ2 ∪ F1 X {e} X
{s2}

100
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’*

101
Complement of L and L'

Complement of L

Complement of L’

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

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

◦ For σ, the NFA is:

◦ For composite regular expressions: use closure under ∪, · and *

104
Parse Tree for (a U b)* ∙ b

* b

a b

105
Example: NFA for (a U b)*b

aUb

106
Example (contd.) : NFA for (a U b)*b

(aUb)*

107
Example (contd.) : NFA for (a U b)*b

(a U b)*b

108
DFA → regular expression
Easier to do:
◦ DFA → GNFA → regular expression.

GNFA (Generalized NFA)


◦ labels of transitions can be regular expressions.

Need special GNFA that satisfies:


(1) start state has no incoming transition
(2) only one final state
(3) final state has no outgoing transition.

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

110
Eliminating state q{rip}

R4

qi qj

R1 R3
qrip

(R1 )(R2)* (R3) ∪


R2 (R4)
qi qj

111
Constructing regular expression.

DFA L = {w in {a, b}* | w has odd number of b's }

112
Constructing regular expression (contd.)

Added: a new start state and a new final state with empty transitions

113
Constructing regular expression (contd.)

Eliminate states one-by-one

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

114
Constructing regular expression (contd.)

qrip

We take out the qrip state and we are left with the regular expression

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
Converting NFA to DFA

122
123
124
125
126
127
128
129
130
131
132
133
134
Applications of the Pumping Lemma

135
136
Applications of the Pumping Lemma

137
138
139
140
141

You might also like