unit-1 FiniteAutomata
unit-1 FiniteAutomata
2
Why Study Automata Theory?
Finite automata are a useful model for many important kinds of software and
hardware:
1. Software for designing and checking the behavior of digital
circuits
2. The lexical analyzer of a typical compiler, that is, the compiler component
that breaks the input text into logical units
3. Software for scanning large bodies of text, such as collections of Web
pages, to find occurrences of words, phrases or other patterns
4. Software for verifying systems of all types that have a finite number of
distinct states, such as communications protocols of protocols for secure
exchange information
3
(A pioneer of automata theory)
Alan Turing (1912-1954)
• Father of Modern Computer Science
• English mathematician
• Studied abstract machines called
Turing machines even before
computers existed
• Heard of the Turing test?
4
Theory of Computation: A
Historical Perspective
1930s • Alan Turing studies Turing machines
• Decidability
• Halting problem
1940-1950s • “Finite automata” machines studied
• Noam Chomsky proposes the
“Chomsky Hierarchy” for formal
languages
1969 Cook introduces “intractable” problems
or “NP-Hard” problems
1970- Modern computer science: compilers,
computational & complexity theory5evolve
Languages & Grammars
• Languages: “A language is a
Or “words” collection of sentences of finite
length all constructed from a
finite alphabet of symbols”
• Grammars: “A grammar can be
regarded as a device that
enumerates the sentences of a
language” - nothing more,
nothing less
7
Automata and Complexity,
• Automata theory:
Automata theory deals with the theory with which these machines work.. An
automaton can be in various states. One state is the state the automaton starts in and
the others are the final or exit states.
8
Finite Automata Model
Structural Representations
finite automata can be represented by input tape
and finite control.
•Input tape: It is a linear tape having some
number of cells. Each input symbol is placed in
each cell.
•Finite control: The finite control decides the
next state on receiving particular input from
input tape. The tape reader reads the cells one by
one from left to right, and at a time only one
input symbol is read.
9
Automata and Complexity
Computational Complexity Theory:
•Computational Complexity Theory deals with the efficiency
with which a computer can solve a problem. Time and Space
are considered to be the two vectors responsible for a problems
efficiency.
10
The Central Concepts of
Automata Theory
11
Alphabet:
An alphabet is a finite, non-empty set of symbols
• We use the symbol ∑ (sigma) to denote an
alphabet
• Examples:
– Binary: ∑ = {0,1}
– All lower case letters: ∑ = {a,b,c,..z}
– Alphanumeric: ∑ = {a-z, A-Z, 0-9}
– DNA molecule letters: ∑ = {a,c,g,t}
12
Strings
A string or word is a finite sequence of symbols
chosen from ∑
• Empty string is (or “epsilon”)
Length of a String:
• Length of a string w, denoted by “|w|”, is equal to
the number of (non- ) characters in the string
– E.g., x = 010100 |x| = 6
– x = 01 0 1 00 |x| = ?
– xy = concatentation of two strings x and y
13
Powers of an alphabet
Kleene Star
Definition: The Kleene star, Σ * , is a unary operator
on a set of symbols or strings, Σ, that gives the
infinite set of all possible strings of all possible
lengths over Σ including λ.
Representation: Σ * = Σ0 U Σ1 U Σ2 U… Σp where
Σp is the set of all possible strings of length p.
Example: If Σ = {a, b}, Σ *= {λ, a, b, aa, ab, ba, bb,
………..}
14
Powers of an alphabet
Kleene Closure / Plus
Definition: The set Σ + is the infinite set of all
possible strings of all possible lengths over Σ
excluding λ.
Representation: Σ + = Σ1 U Σ2 U Σ3
Σ + = Σ* − { λ }
Example: If Σ = { a, b } , Σ+ ={ a, b, aa, ab, ba, bb,
………..}
15
Languages
L is a said to be a language over alphabet ∑, only if L ∑*
this is because ∑* is the set of all strings (of all possible length
including 0) over the given alphabet ∑
Examples:
1. Let L be the language of all strings consisting of n 0’s followed by n
1’s:
L = {, 01, 0011, 000111,…}
2. Let L be the language of all strings of with equal number of 0’s and 1’s:
L = {, 01, 10, 0011, 1100, 0101, 1010, 1001,…}
NO 16
The Membership Problem
Given a string w ∑*and a language L over
∑, decide whether or not w L.
Example:
Let w = 100011
Q) Is w the language of strings with equal
number of 0s and 1s?
17
Finite Automata : Examples
action
• On/Off switch state
19
Formal Definition of a DFA
• A DFA is a five-tuple:
M = (Q, Σ, δ, q0, F)
δ:
0 1
q0 q1 q0
q1 q0 q1
21
• Revisit example #2:
a a a/b/c
Q = {q0, q1, q2}
c c
Σ = {a, b, c} q0 q1 q2
Start state is q0
F = {q2} b b
δ: a b c
q0 q0 q0 q1
q1 q1 q1 q2
q2 q2 q2 q2
δ^ : (Q x Σ*) –> Q
δ^(q,w) – The state entered after reading string w having started in state q.
Formally:
1) δ^(q, ε) = q, and
2) For all w in Σ* and a in Σ
δ^(q,wa) = δ (δ^(q,w), a)
23
• Recall Example #1: 1
0
q0 q1 1
0
• What is δ^(q0, 011)? Informally, it is the state entered by M after processing
011 having started in state q 0.
• Formally:
• Therefore:
• Is 10 accepted? No, since δ(q0, 10) = q1 is not a final state. The fact
that δ(q1, 10) = q2 is irrelevant, q1 is not the start state!
27
Definitions related to DFAs
• Let M = (Q, Σ, δ,q0,F) be a DFA and let w be in Σ*. Then w is accepted by M
iff δ(q0,w) = p for some state p in F.
• Let M1 = (Q1, Σ1, δ1, q0, F1) and M2 = (Q2, Σ2, δ2, p0, F2) be DFAs. Then M1
28
and M2 are equivalent iff L(M1) = L(M2).
• Notes:
– A DFA M = (Q, Σ, δ,q0,F) partitions the set Σ* into two sets: L(M) and
Σ* - L(M).
– Similarly, if L(M1) = L(M2) then L(M1) is a subset of L(M2) and L(M2) is a subset of
L(M1).
• Can you write a program to “simulate” a given DFA, or any arbitrary input DFA?
0/1
0/1 0/1
q0 q1 q2
30
• Give a DFA M such that:
b/c a/b/c
a
a
q0 q1 q2
b/c
Logic:
In Start state (q0): b’s and c’s: ignore – stay in same state
q0 is also “accept” state
First ‘a’ appears: get ready (q1) to reject
But followed by a ‘b’ or ‘c’: go back to start state q0
When second ‘a’ appears after the “ready” state: go to reject state q2
Ignore everything after getting to the “reject” state q2 31
• Give a DFA M such that:
b/c a a/b/c
a a
b
q0 q1 q2 q3
c
b/c
32
• Give a DFA M such that:
q0 a b q7
b
b a
b a
q4 q5 q6
b
Remember, you may have multiple “final” states, but only one “start”
state 33
• Let Σ = {0, 1}. Give DFAs for {}, {ε}, Σ*, and Σ+.
0/1
q0 q0 q1
34
• Problem: Third symbol from last is 1
0/1
1 0/1 0/1
q0 q1 q2 q3
Is this a DFA?
35
Nondeterministic Finite State
Automata (NFA)
• An NFA is a five-tuple:
M = (Q, Σ, δ, q0, F)
36
δ(q,s) is a function from Q x S to 2Q (but not only to Q)
• Example #1: one or more 0’s followed by one or more 1’s
0 1 0/1
Q = {q0, q1, q2}
0 1
Σ = {0, 1} q0 q1 q2
Start state is q0
F = {q2}
δ: 0 1
q0 {q0, q1} {}
{} {q1, q2}
q1
{q2} {q2}
q2
37
• Example #2: pair of 0’s or pair of 1’s as substring
0/1 0/1
Q = {q0, q1, q2 , q3 , q4}
Σ = {0, 1} 0 0
q0 q3 q4
Start state is q0
1 0/1
F = {q2, q4}
1
δ: 0 1 q1 q2
q0 {q0, q3} {q0, q1}
{} {q2}
q1
{q2} {q2}
q2
{q4} {}
q3
{q4} {q4}
q4 38
• Notes:
– δ(q,s) may not be defined for some q and s (what does that mean?)
– δ(q,s) may map to multiple q’s
– A string is said to be accepted if there exists a path from q0 to some state
in F
– A string is rejected if there exist NO path to any state in F
– The language accepted by an NFA is the set of all accepted strings
q0 0 q0 0 q0 1 q0
q3 q3 q1
q4 q4 accepted
0 0
q0 q3 q4
1 0/1
1
q1 q2
40
• Another example (010):
q0 0 q0 1 q0 0 q0
q3 q1 q3
not accepted
• All paths have been explored, and none lead to an accepting state.
0/1
0 0
q0 q3 q4
1
1
q1 q2
41
• Question: Why non-determinism is useful?
–Non-determinism = Backtracking
–Compressed information
–Non-determinism hides backtracking
–Programming languages, e.g., Prolog, hides backtracking => Easy to
program at a higher level: what we want to do, rather than how to do it
–Useful in algorithm complexity study
–Is NDA more “powerful” than DFA, i.e., accepts type of languages that any
DFA cannot?
42
• Let Σ = {a, b, c}. Give an NFA M that accepts:
a/b/c a/b/c
a b
q0 q1 q2
a/b
b a/b a/b q3
q0 q1 q2
Is L a subset of L(M)?
Is L(M) a subset of L?
44
Extension of δ to Strings and Sets of States
• What we currently have: δ : (Q x Σ) –> 2Q
• We will do this in two steps, which will be slightly different from the
book, and we will make use of the following NFA.
0
0 1
q0 q1 q2
0 1 0
0
q3 q4
1
0 45
Extension of δ to Strings and Sets of States
• Step #1:
• Note that:
δ#({p},a)=
q{ p }
δ(q, a) by definition of δ#, rule #1 above
= δ(p, a)
47
• Step #2:
Formally:
1 0
0 q1 1 q2
q0
0 1
1
q3
Informally: The set of states the NFA could be in after processing 10,
having started in state q0, i.e., {q1, q2, q3}.
51
Definitions for NFAs
52
Equivalence of DFAs and NFAs
• Do DFAs and NFAs accept the same class of languages?
– Is there a language L that is accepted by a DFA, but not by any NFA?
– Is there a language L that is accepted by an NFA, but not by any DFA?
53
• Consider the following DFA: 2 or more c’s
a a a/b/c
Q = {q0, q1, q2}
c c
Σ = {a, b, c} q0 q1 q2
Start state is q0
F = {q2} b b
δ: a b c
q0 q0 q0 q1
q1 q1 q1 q2
q2 q2 q2 q2
54
• An Equivalent NFA:
a a a/b/c
Q = {q0, q1, q2}
c c
Σ = {a, b, c} q0 q1 q2
Start state is q0
F = {q2} b b
δ: a b c
q0 {q0} {q0} {q1}
55
• Lemma 1: Let M be an DFA. Then there exists a NFA M’ such that
L(M) = L(M’).
56
• Lemma 2: Let M be an NFA. Then there exists a DFA M’ such that L(M) =
L(M’).
• Proof: (sketch)
q’0 = [q0]
δ: 0 1
q0 {q1} {}
58
• Example of creating a DFA out of an NFA (as per the constructive
proof): 0/1
0
q0 q1
0
-->q0
δ for DFA: 0 1 q1
->q{q0 1} {}
write as
write as
[q1] []
[q1]
[]
59
• Example of creating a DFA out of an NFA (as per the constructive
proof): 0/1
0
q0 q1
0
δ: 0 1
->q{q0 1} {}
write as
[q1]
{q ,q } {q1}
] 0 1as
[q1write
[q01]
[]
[q01]
60
• Example of creating a DFA out of an NFA (as per the constructive
proof): 0/1
0
q0 q1
0
δ: 0 1
->q{q0 1} {}
write as
[q1]
{q ,q } {q1}
] 0 1as
[q1write
[q01]
[] []
[]
[q01]
61
• Example of creating a DFA out of an NFA (as per the constructive
proof): 0/1
0
q0 q1
0
δ: 0 1
->q{q0 1} {}
write as
[q1]
{q ,q } {q1}
] 0 1as
[q1write
[q01]
[] []
[ ] [q ] [q1]
01
[q01]
62
• Construct DFA M’ as follows:
1
0/1
1 [q0]
0 [q1]
[]
1
[q0q1]
0
δ({q0}, 0) = {q1} => δ’([q0], 0) = [q1]
δ({q0}, 1) = {} => δ’([q0], 1) = [ ]
δ({q1}, 0) = {q0, q1} => δ’([q1], 0) = [q0q1]
δ({q1}, 1) = {q1} => δ’([q1], 1) = [q1]
δ({q0, q1}, 0) = {q0, q1} => δ’([q0q1], 0) = [q0q1]
δ({q0, q1}, 1) = {q1} => δ’([q0q1], 1) = [q1]
δ({}, 0) = {} => δ’([ ], 0) = [ ]
δ({}, 1) = {} => δ’([ ], 1) = [ ] 63
• Theorem: Let L be a language. Then there exists an DFA M such
that L = L(M) iff there exists an NFA M’ such that L = L(M’).
• Proof:
(if) Suppose there exists an NFA M’ such that L = L(M’). Then by
Lemma 2 there exists an DFA M such that L = L(M).
(only if) Suppose there exists an DFA M such that L = L(M). Then by
Lemma 1 there exists an NFA M’ such that L = L(M’).
64
• Note: Suppose R = {}
= {} Since R = {}
q2 {q2} {q2}
65
• Problem: Third symbol from last is 1
0/1
1 0/1 0/1
q0 q1 q2 q3
66
NFAs with ε Moves
• An NFA-ε is a five-tuple:
M = (Q, Σ, δ, q0, F)
δ: (Q x (Σ U {ε})) –> 2Q
δ(q,s) -The set of all states p such that there is a
transition labeled a from q to p, where a
is in Σ U {ε}
• Sometimes referred to as an NFA-ε other times, simply as an NFA.
67
• Example: q3
1
0 0 0/1
ε ε
q0 q1 q2
1 0
δ: 0 1 ε
q0 {q0} {} {q1} - A string w = w1w2…wn is
processed
{q1, q2} {q0, q3} {q2} as w = ε*w1ε*w2ε* … ε*wnε*
q1 - Example: all computations on 00:
{q2} {q2} {} 0
ε 0
q2 q0 q0 q1 q2
{} {} {}
:
q3 68
Informal Definitions
• The language accepted by M is the set of all strings from Σ * that are accepted
by M.
69
ε-closure
• Define ε-closure(q) to denote the set of all states reachable from q by zero or
more ε transitions.
ε-closure(P)=
qP ε-closure(q) q3
1
• Examples: 0 0 0/1
ε ε
q0 q1 q2
ε-closure({q1, q2}) = {q1, q2} 1 0
ε-closure({q0, q3}) = {q0, q1, q2, q3} 70
Extension of δ to Strings and Sets of States
• What we currently have: δ : (Q x (Σ U {ε})) –> 2Q
q3
1
0 0 0/1
ε ε
q0 q1 q2
1 0
71
• Step #1:
• Note that:
= δ(p, a)
Given δ: (2Q x (Σ U {ε})) –> 2Q define δ^: (2Q x Σ*) –> 2Q as follows:
Formally:
δ({q0}, 0) = {q0}
76
Definitions for NFA-ε Machines
77
Equivalence of NFAs and NFA-εs
• Do NFAs and NFA-ε machines accept the same class of languages?
– Is there a language L that is accepted by a NFA, but not by any NFA -ε?
– Is there a language L that is accepted by an NFA -ε, but not by any DFA?
78
• Lemma 1: Let M be an NFA. Then there exists a NFA-ε M’ such
that L(M) = L(M’).
79
• Lemma 2: Let M be an NFA-ε. Then there exists a NFA M’ such
that L(M) = L(M’).
• Proof: (sketch)
• Notes:
– δ’: (Q x Σ) –> 2Q is a function
– M’ has the same state set, the same alphabet, and the same start state as M
80
– M’ has no ε transitions
• Example:
q3
1
0 0 0/1
ε ε
q0 q1 q2
1 0
• Step #1:
– Same state set as M
– q0 is the starting state q3
q0 q1 q2
81
• Example:
q3
1
0 0 0/1
ε ε
q0 q1 q2
1 0
• Step #2:
– q0 becomes a final state q3
q0 q1 q2
82
• Example:
q3
1
0 0 0/1
ε ε
q0 q1 q2
1 0
• Step #3:
q3
0
0
q0 q1 q2
83
• Example:
q3
1
0 0 0/1
ε ε
q0 q1 q2
1 0
• Step #4:
q3
1
0/1
0/1
q0 q1 q2
0/1
84
• Example:
q3
1
0 0 0/1
ε ε
q0 q1 q2
1 0
• Step #5:
q3
1
0/1
0
0/1 0
q0 q1 q2
0/1
85
• Example:
q3
1
0 0 0/1
ε ε
q0 q1 q2
1 0
• Step #6:
q3
1
0/1 1
0/1
0/1 0/1
q0 q1 q2
1
0/1
86
• Example:
q3
1
0 0 0/1
ε ε
q0 q1 q2
1 0
• Step #7:
q3
1
0/1 1 0
0/1
0/1 0/1
q0 q1 q2
1
0/1
87
• Example:
q3
1
0 0 0/1
ε ε
q0 q1 q2
1 0
1
0/1 1 0/1
0/1
0/1 0/1
q0 q1 q2
1
0/1
88
• Theorem: Let L be a language. Then there exists an NFA M such
that L= L(M) iff there exists an NFA-ε M’ such that L = L(M’).
• Proof:
(if) Suppose there exists an NFA-ε M’ such that L = L(M’). Then by
Lemma 2 there exists an NFA M such that L = L(M).
(only if) Suppose there exists an NFA M such that L = L(M). Then by
Lemma 1 there exists an NFA-ε M’ such that L = L(M’).
89