Download - Finite Automata
![Page 1: Finite Automata](https://reader036.vdocuments.us/reader036/viewer/2022062321/56812a81550346895d8e1339/html5/thumbnails/1.jpg)
Finite Automata
Great Theoretical Ideas In Computer Science
Victor AdamchikDanny Sleator
CS 15-251 Spring 2010
Lecture 20 Mar 30, 2010 Carnegie Mellon University
![Page 2: Finite Automata](https://reader036.vdocuments.us/reader036/viewer/2022062321/56812a81550346895d8e1339/html5/thumbnails/2.jpg)
A machine so simple that you can
understand it in less than one minute
Wishful thinking…
Deterministic Finite Automata
![Page 3: Finite Automata](https://reader036.vdocuments.us/reader036/viewer/2022062321/56812a81550346895d8e1339/html5/thumbnails/3.jpg)
00,1
00
1
1
1
0111 111
11
1
The machine accepts a string if the process ends in a double circle
![Page 4: Finite Automata](https://reader036.vdocuments.us/reader036/viewer/2022062321/56812a81550346895d8e1339/html5/thumbnails/4.jpg)
00,1
00
1
1
1
0111 111
11
1
The machine accepts a string if the process ends in a double
circle
states
start state (q0)accept states
(F)
transitions
![Page 5: Finite Automata](https://reader036.vdocuments.us/reader036/viewer/2022062321/56812a81550346895d8e1339/html5/thumbnails/5.jpg)
Anatomy of a Deterministic Finite Automaton
The alphabet of a finite automaton is the set where the symbols come
from, for example {0,1}
The language of a finite automaton is the set of strings that it accepts
The singular of automata is automaton.
![Page 6: Finite Automata](https://reader036.vdocuments.us/reader036/viewer/2022062321/56812a81550346895d8e1339/html5/thumbnails/6.jpg)
L(M) = All strings of 0s and 1s
The Language L(M) of Machine M
0,1
q0
The language of a finite automaton is the set of strings that it accepts
![Page 7: Finite Automata](https://reader036.vdocuments.us/reader036/viewer/2022062321/56812a81550346895d8e1339/html5/thumbnails/7.jpg)
0 0
1
L(M) ={ w | w has an even number of 1s}
q0
0
q1
1
1
The Language L(M) of Machine M
![Page 8: Finite Automata](https://reader036.vdocuments.us/reader036/viewer/2022062321/56812a81550346895d8e1339/html5/thumbnails/8.jpg)
An alphabet Σ is a finite set (e.g., Σ = {0,1})
A string over Σ is a finite-length sequence of elements of Σ
For x a string, |x| isthe length of x
The unique string of length 0 will be denoted by ε and will be called the
empty or null string
Notation
A language over Σ is a set of strings over Σ
![Page 9: Finite Automata](https://reader036.vdocuments.us/reader036/viewer/2022062321/56812a81550346895d8e1339/html5/thumbnails/9.jpg)
Q is the finite set of states
Σ is the alphabet
: Q Σ → Q is the transition function
q0 Q is the start state
F Q is the set of accept states
A finite automaton is M = (Q, Σ, , q0, F)
L(M) = the language of machine M= set of all strings machine M
accepts
![Page 10: Finite Automata](https://reader036.vdocuments.us/reader036/viewer/2022062321/56812a81550346895d8e1339/html5/thumbnails/10.jpg)
Q = {q0, q1, q2, q3}
Σ = {0,1}
: Q Σ → Q transition function
q0 Q is start state
F = {q1, q2} Q accept states
M = (Q, Σ, , q0, F) where
0 1
q0 q0 q1
q1 q2 q2
q2 q3 q2
q3 q0 q2
q2
00,1
00
1
1
1
q0
q1
q3
M
![Page 11: Finite Automata](https://reader036.vdocuments.us/reader036/viewer/2022062321/56812a81550346895d8e1339/html5/thumbnails/11.jpg)
The finite-state automata are deterministic, if for each pair Q Σ of state and input value there is a unique next state given by the
transition function.
There is another type machine in which there may be several possible next states. Such
machines called nondeterministic.
![Page 12: Finite Automata](https://reader036.vdocuments.us/reader036/viewer/2022062321/56812a81550346895d8e1339/html5/thumbnails/12.jpg)
Build an automaton that accepts all and only those strings that contain 001
{0} 0
1
{00} 0
1
{001}1
0 0,1
EXAMPLE
![Page 13: Finite Automata](https://reader036.vdocuments.us/reader036/viewer/2022062321/56812a81550346895d8e1339/html5/thumbnails/13.jpg)
Build an automaton that accepts all binary numbers
that are divisible by 3,i.e, L = 0, 11, 110, 1001,
1100, 1111, 10010, 10101…
1
0
0
1
1
0
![Page 14: Finite Automata](https://reader036.vdocuments.us/reader036/viewer/2022062321/56812a81550346895d8e1339/html5/thumbnails/14.jpg)
A language is regular if it is recognized by a deterministic
finite automaton
L = { w | w contains 001} is regular
L = { w | w has an even number of 1s} is regular
A language over Σ is a set of strings over Σ
![Page 15: Finite Automata](https://reader036.vdocuments.us/reader036/viewer/2022062321/56812a81550346895d8e1339/html5/thumbnails/15.jpg)
Determine the language recognized by
0
1 0,1
L(M)={1n | n = 0, 1, 2, …}
![Page 16: Finite Automata](https://reader036.vdocuments.us/reader036/viewer/2022062321/56812a81550346895d8e1339/html5/thumbnails/16.jpg)
Determine the language recognized by
L(M)={1, 01}
0 0,1
0,1
1
1
0
![Page 17: Finite Automata](https://reader036.vdocuments.us/reader036/viewer/2022062321/56812a81550346895d8e1339/html5/thumbnails/17.jpg)
Determine the language recognized by
L(M)={0n, 0n10x | n=0,1,2…, and x is any string}
1
0,1
0,1
1
0 0
![Page 18: Finite Automata](https://reader036.vdocuments.us/reader036/viewer/2022062321/56812a81550346895d8e1339/html5/thumbnails/18.jpg)
DFA Membership problem
Determine whether someword belongs to the language.
Theorem: The DFA Membership Problem is solvable in linear
time.
Let M = (Q, Σ, , q0, F) and w = w1...wm. Algorithm for DFA M:
p := q0;
for i := 1 to m do p := (p,wi);if pF then return Yes else return
No.
![Page 19: Finite Automata](https://reader036.vdocuments.us/reader036/viewer/2022062321/56812a81550346895d8e1339/html5/thumbnails/19.jpg)
Equivalence of two DFAs
Given a few equivalent machines, we are naturally interested in the
smallest one with the least number of states.
Definition: Two DFAs M1 and M2 over the same alphabet are equivalent if theyaccept the same language: L(M1) =
L(M2).
![Page 20: Finite Automata](https://reader036.vdocuments.us/reader036/viewer/2022062321/56812a81550346895d8e1339/html5/thumbnails/20.jpg)
Union Theorem
Given two languages, L1 and L2, define the union of L1 and L2 as
L1 L2 = { w | w L1 or w L2 }
Theorem: The union of two regular languages is also a
regular language.
![Page 21: Finite Automata](https://reader036.vdocuments.us/reader036/viewer/2022062321/56812a81550346895d8e1339/html5/thumbnails/21.jpg)
Theorem: The union of two regular languages is also a
regular languageProof (Sketch): Let
M1 = (Q1, Σ, 1, q0, F1) be finite automaton for L1
and
M2 = (Q2, Σ, 2, q0, F2) be finite automaton for L2
We want to construct a finite automaton M = (Q, Σ, , q0, F) that recognizes L = L1
L2
1
2
![Page 22: Finite Automata](https://reader036.vdocuments.us/reader036/viewer/2022062321/56812a81550346895d8e1339/html5/thumbnails/22.jpg)
Idea: Run both M1 and M2 at the same time!
Q= pairs of states, one from M1 and one from M2
= { (q1, q2) | q1 Q1 and q2 Q2 }
= Q1 Q2
![Page 23: Finite Automata](https://reader036.vdocuments.us/reader036/viewer/2022062321/56812a81550346895d8e1339/html5/thumbnails/23.jpg)
Theorem: The union of two regular languages is also a regular language
0 0
q0
0
q1
1
1
0 1
p0
1
p1
0
0
![Page 24: Finite Automata](https://reader036.vdocuments.us/reader036/viewer/2022062321/56812a81550346895d8e1339/html5/thumbnails/24.jpg)
Automaton for Union
0
p0 q0
1
1
0 00
1
1
p0 q1
p1 q0 p1 q1
0 0
![Page 25: Finite Automata](https://reader036.vdocuments.us/reader036/viewer/2022062321/56812a81550346895d8e1339/html5/thumbnails/25.jpg)
The Regular OperationsUnion: A B = { w | w A or w B }
Intersection: A B = { w | w A and w B }
Negation: A = { w | w A }
Reverse: AR = { w1 …wk | wk …w1 A }
Concatenation: A B = { vw | v A and w B }
Star: A* = { w1 …wk | k ≥ 0 and each wi A }
![Page 26: Finite Automata](https://reader036.vdocuments.us/reader036/viewer/2022062321/56812a81550346895d8e1339/html5/thumbnails/26.jpg)
ReverseReverse: AR = { w1 …wk | wk …w1 A }
How to construct a DFA for the reversal of a language?
The direction in which we read a string should
be irrelevant. If we flip transitions around we
might not get a DFA.
![Page 27: Finite Automata](https://reader036.vdocuments.us/reader036/viewer/2022062321/56812a81550346895d8e1339/html5/thumbnails/27.jpg)
The Kleene closure: A*
From the definition of the concatenation, we definite An, n =0, 1, 2, … recursively
A0 = {ε}An+1 = An A
Star: A* = { w1 …wk | k ≥ 0 and each wi A }
A* is a set consisting of concatenations of arbitrary many strings from A.
U
0k
kAA*
![Page 28: Finite Automata](https://reader036.vdocuments.us/reader036/viewer/2022062321/56812a81550346895d8e1339/html5/thumbnails/28.jpg)
The Kleene closure: A*What is A* of A={0,1}?
All binary strings
What is A* of A={11}?
All binary strings of an even number of 1s
![Page 29: Finite Automata](https://reader036.vdocuments.us/reader036/viewer/2022062321/56812a81550346895d8e1339/html5/thumbnails/29.jpg)
Regular Languages Are Closed Under The
Regular Operations
We have seen part of the proof for Union. The proof for intersection
is very similar. The proof for negation is easy.
![Page 30: Finite Automata](https://reader036.vdocuments.us/reader036/viewer/2022062321/56812a81550346895d8e1339/html5/thumbnails/30.jpg)
Theorem: Any finite language is regular
Claim 1: Let w be a string over an alphabet. Then {w} is a regular
language. Proof: By induction on the number of characters. If {a} and {b} are regular
then {ab} is regular
Claim 2: A language consisting of n strings is regular
Proof: By induction on the number of strings. If {a} then L{a} is regular
![Page 31: Finite Automata](https://reader036.vdocuments.us/reader036/viewer/2022062321/56812a81550346895d8e1339/html5/thumbnails/31.jpg)
Input: Text T of length t, string S of length n
Pattern Matching
Problem: Does string S appear inside text T?
a1, a2, a3, a4, a5, …, at
Naïve method:
Cost:Roughly nt comparisons
![Page 32: Finite Automata](https://reader036.vdocuments.us/reader036/viewer/2022062321/56812a81550346895d8e1339/html5/thumbnails/32.jpg)
Automata Solution
Build a machine M that accepts any string with S as a consecutive
substringFeed the text to M
Cost:
As luck would have it, the Knuth, Morris, Pratt algorithm builds M
quickly
t comparisons + time to build M
![Page 33: Finite Automata](https://reader036.vdocuments.us/reader036/viewer/2022062321/56812a81550346895d8e1339/html5/thumbnails/33.jpg)
Grep
Coke Machines
Thermostats (fridge)
Elevators
Train Track Switches
Lexical Analyzers for Parsers
Real-life Uses of DFAs
![Page 34: Finite Automata](https://reader036.vdocuments.us/reader036/viewer/2022062321/56812a81550346895d8e1339/html5/thumbnails/34.jpg)
Are all languages regular?
![Page 35: Finite Automata](https://reader036.vdocuments.us/reader036/viewer/2022062321/56812a81550346895d8e1339/html5/thumbnails/35.jpg)
i.e., a bunch of a’s followed by an equal number of b’s
Consider the language L = { anbn | n > 0 }
No finite automaton accepts this language
Can you prove this?
![Page 36: Finite Automata](https://reader036.vdocuments.us/reader036/viewer/2022062321/56812a81550346895d8e1339/html5/thumbnails/36.jpg)
anbn is not regular. No machine has enough states to keep track of the number of a’s it might encounter
![Page 37: Finite Automata](https://reader036.vdocuments.us/reader036/viewer/2022062321/56812a81550346895d8e1339/html5/thumbnails/37.jpg)
That is a fairly weak argument
Consider the following example…
![Page 38: Finite Automata](https://reader036.vdocuments.us/reader036/viewer/2022062321/56812a81550346895d8e1339/html5/thumbnails/38.jpg)
L = strings where the # of occurrences of the pattern ab is
equal to the number of occurrences of the pattern ba
Can’t be regular. No machine has enough states to keep
track of the number of occurrences of ab
![Page 39: Finite Automata](https://reader036.vdocuments.us/reader036/viewer/2022062321/56812a81550346895d8e1339/html5/thumbnails/39.jpg)
M accepts only the strings with an equal number of ab’s and
ba’s!
b
b a
b
a
a
a
ba
b
![Page 40: Finite Automata](https://reader036.vdocuments.us/reader036/viewer/2022062321/56812a81550346895d8e1339/html5/thumbnails/40.jpg)
L = strings where the # of occurrences of the pattern ab is
equal to the number of occurrences of the pattern ba
Can’t be regular. No machine has enough states to keep
track of the number of occurrences of ab
![Page 41: Finite Automata](https://reader036.vdocuments.us/reader036/viewer/2022062321/56812a81550346895d8e1339/html5/thumbnails/41.jpg)
Let me show you a professional strength proof that anbn is not
regular…
![Page 42: Finite Automata](https://reader036.vdocuments.us/reader036/viewer/2022062321/56812a81550346895d8e1339/html5/thumbnails/42.jpg)
How to prove a language is not regular…
Assume it is regular, hence is accepted by
a DFA M with n states.
Show that there are two strings s1 and s2 which both reach some state in M (usually by pigeonhole principle)
Then show there is some string t such that
string s1t is in the language, but s2t is not.
However, M accepts either both or neither.
![Page 43: Finite Automata](https://reader036.vdocuments.us/reader036/viewer/2022062321/56812a81550346895d8e1339/html5/thumbnails/43.jpg)
Pigeonhole principle:
If we put n objects into m pigeonholes
and if n > m, then at least one pigeonhole
must have more than one item in it.
![Page 44: Finite Automata](https://reader036.vdocuments.us/reader036/viewer/2022062321/56812a81550346895d8e1339/html5/thumbnails/44.jpg)
Theorem: L= {anbn | n > 0 } is not regular
Proof (by contradiction):
Assume that L is regular, M=(Q,{a,b},,q0,F) Consider (q0, ai) for i = 1,2,3, …
There are infinitely many i’s but a finite number of states.
(q0, an)=q and (q0, am) =q, and n m
Since M accepts anbn (q, bn)=qf
(q0, ambn)=( (q0, am),bn)=(q, bn)= qf
It follows that M accepts ambn, and n m
![Page 45: Finite Automata](https://reader036.vdocuments.us/reader036/viewer/2022062321/56812a81550346895d8e1339/html5/thumbnails/45.jpg)
The finite-state automata are deterministic, if for each pair of state and input value there is a unique next state given by the
transition function.
There is another type machine in which there may be several possible next states. Such
machines called nondeterministic.
![Page 46: Finite Automata](https://reader036.vdocuments.us/reader036/viewer/2022062321/56812a81550346895d8e1339/html5/thumbnails/46.jpg)
A NFA is defined using the same notations M = (Q, Σ, , q0, F)as DFA except the transition
function assigns a set of states to each pair Q Σ of state and
input.
Nondeterministic finite automaton (NFA)
Note, every DFA is automatically also a NFA.
![Page 47: Finite Automata](https://reader036.vdocuments.us/reader036/viewer/2022062321/56812a81550346895d8e1339/html5/thumbnails/47.jpg)
Nondeterministic finite automaton
a
a
qk
a
Allows transitions from qk on the same symbol to many states
![Page 48: Finite Automata](https://reader036.vdocuments.us/reader036/viewer/2022062321/56812a81550346895d8e1339/html5/thumbnails/48.jpg)
NFA for {0k | k is a multiple of 2 or 3}
ε
ε
0
0
0
0
0
![Page 49: Finite Automata](https://reader036.vdocuments.us/reader036/viewer/2022062321/56812a81550346895d8e1339/html5/thumbnails/49.jpg)
What does it mean that for a NFA to recognize a string x = x1x2…xk?
1
0
0
0 0
0,1 1
1
s1
s2
s3
s4
s0
Since each input symbol xj (for j>1) takes the previous state to a set of states, we shall use a union of these states.
![Page 50: Finite Automata](https://reader036.vdocuments.us/reader036/viewer/2022062321/56812a81550346895d8e1339/html5/thumbnails/50.jpg)
What does it mean that for a NFA to recognize a string?
Here we are going formally define this.
For a state q and string w, *(q, w) is the set of states that the NFA can reach when it reads the string w starting at the state q.
Thus for NFA= (Q, Σ, , q0, F), the function*: Q x Σ* -> 2Q
is defined by *(q, y xk) = p*(q,y) (p,xk)
![Page 51: Finite Automata](https://reader036.vdocuments.us/reader036/viewer/2022062321/56812a81550346895d8e1339/html5/thumbnails/51.jpg)
Find the language recognized by this NFA
1
0
0
0 0
0,1 1
1
s1
s2
s3
s4
s0
L = {0n, 0n01, 0n11 | n = 0, 1, 2…}
![Page 52: Finite Automata](https://reader036.vdocuments.us/reader036/viewer/2022062321/56812a81550346895d8e1339/html5/thumbnails/52.jpg)
Find the language recognized by this NFA
1
0
1
1 0
1
s0
L = 1* (01, 1, 10) (00)*
0
![Page 53: Finite Automata](https://reader036.vdocuments.us/reader036/viewer/2022062321/56812a81550346895d8e1339/html5/thumbnails/53.jpg)
Nondeterministic finite automaton
Theorem. If the language L is recognized by a NFA M0,
then L is also recognized by a DFA M1.
In other words, if we ask if there is a NFA that is not equivalent to any DFA. The answer is No.
![Page 54: Finite Automata](https://reader036.vdocuments.us/reader036/viewer/2022062321/56812a81550346895d8e1339/html5/thumbnails/54.jpg)
NFA vs. DFA
Advantages.Easier to construct and manipulate.Sometimes exponentially smaller.Sometimes algorithms much easier.
DrawbacksAcceptance testing slower.Sometimes algorithms more complicated.
![Page 55: Finite Automata](https://reader036.vdocuments.us/reader036/viewer/2022062321/56812a81550346895d8e1339/html5/thumbnails/55.jpg)
Study Bee
• DFA• NFA