Document

advertisement
Lecture Three:
Finite Automata
Amjad Ali
Finite Automata, Lecture 3, slide 1
DFA:
Deterministic Finite Automaton

An informal definition (formal version later):
 A diagram with a finite number of states
represented by circles
 An arrow points to one of the states, the unique
start state

Double circles mark any number of the states as
accepting states

For every state, for every symbol in , there is
exactly one arrow labeled with that symbol going
to another state (or back to the same state)
Finite Automata, Lecture 3, slide 2
DFAs Define Languages




Given any string over , a DFA can read the
string and follow its state-to-state transitions
At the end of the string, if it is in an accepting
state, we say it accepts the string
Otherwise it rejects
The language defined by a DFA is the set of
strings in * that it accepts
Finite Automata, Lecture 3, slide 3
The 5-Tuple
A DFA M is a 5-tuple M = (Q, , , q0, F), :
where Q is the finite set of states
 is the alphabet (that is, a finite set of symbols)
  (Q    Q) is the transition function
q0  Q is the start state
F  Q is the set of accepting states

Q is the set of states

Drawn as circles in the diagram

We often refer to individual states as qi

The definition requires at least one: q0, the start state
 is the alphabet (that is, a finite set of symbols)

Finite Automata, Lecture 3, slide 4
The 5-Tuple
A DFA M is a 5-tuple M = (Q, , , q0, F), where:
Q is the finite set of states
 is the alphabet (that is, a finite set of symbols)
  (Q    Q) is the transition function
q0  Q is the start state
F  Q is the set of accepting states

F is the set of all those in Q that are accepting states
Drawn as double circles in the diagram
 is the transition function

A function (q,a) that takes the current state q and next input
symbol a, and returns the next state

Represents the same information as the arrows in the diagram


Finite Automata, Lecture 3, slide 5
b
Example:
a
a
q0
q1
b


This DFA defines {xa | x  {a,b}*}
Formally, M = (Q, , , q0, F), where

Q = {q0,q1}

 = {a,b}

F = {q1}


Start sate q0
(q0,a) = q1, (q0,b) = q0, (q1,a) = q1, (q1,b) = q0
a
q0
q1

q1
q1
b
q0
q0
We could just say M = ({q0,q1}, {a,b}, , q0, {q1})
Finite Automata, Lecture 3, slide 6
Regular Languages
For any DFA M = (Q, , , q0, F), L(M) denotes the
language accepted by M, which is
L(M) = {x  * | *(q0, x)  F}.
A regular language is one that is L(M) for some DFA M.


To show that a language is regular, give a DFA for it. We'll see
additional ways later
To show that a language is not regular is much harder; we'll see
how later
Finite Automata, Lecture 3, slide 7
Example #1:
a
Q = {q0, q1, q2}
Σ = {a, b, c}
Start state is q0
F = {q2}
a
q0
q0


q0
b
b
q0
c
q1
q1
q1
q1
q2
q2
q2
q2
q2
a/b/c
a
c
q1
c
b
Since δ is a function, at each step M has exactly one option.
It follows that for a given string, there is exactly one
computation.
Finite Automata, Lecture 3, slide 8
q2
Example #2:
1
0
1
q1
q0
0
1
q0


0
q0
0
q1
1
q0
1
q0
q0
One state is final/accepting, all others are rejecting.
The above DFA accepts those strings that contain an even
number of 0’s
Finite Automata, Lecture 3, slide 9
Example #3:
a
a/b/c
a
c
q0
q1
a
q0
c
q0
a

q2
b
b
q0
c
c
q1
a
q0
c
q2
b
q2
accepted
q2
c
q0
rejected
q1
Accepts those strings that contain at least two c’s
Finite Automata, Lecture 3, slide 10
Example #4:

Give a DFA M such that:
L(M) = {x | x is a string of 0’s and 1’s and |x| >= 2}
0/1
0/1
q0
Finite Automata, Lecture 3, slide 11
0/1
q1
q2
Example #5:

Give a DFA M such that:
L(M) = {x | x is a string of (zero or more) a’s and b’s such
that x does not contain the substring aa}
b
a/b
a
q0
q1
b
Finite Automata, Lecture 3, slide 12
a
q2
Example #6:

Give a DFA M such that:
L(M) = {x | x is a string of a’s, b’s and c’s such that x
contains the substring aba}
b
a
a/b
a
q0
b
q1
b
Finite Automata, Lecture 3, slide 13
a
q2
q3
Example #7:

Give a DFA M such that:
L(M) = {x | x is a string of a’s and b’s such that x
contains both aa
a and bb}
a
q1
b
q2
a
q0
a
a
q3
b
a/b
q7
b
b
b
q4
Finite Automata, Lecture 3, slide 14
b
a
q5
b
q6
a
Example #8:

Let Σ = {0, 1}. Give DFAs for {}, {ε}, Σ*, and Σ+.
For {}:
For {ε}:
0/1
0/1
q0
For Σ*:
q0
0/1
q1
For Σ+:
0/1
q0
Finite Automata, Lecture 3, slide 15
0/1
q0
0/1
q1
Definitions for 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 M = (Q, Σ, δ,q0,F) be a DFA. Then the language accepted
by M is the set:
L(M) = {w | w is in Σ* and δ^(q0,w) is in F}
Another equivalent definition:
L(M) = {w | w is in Σ* and w is accepted by M}
Let L be a language. Then L is a regular language iff there
exists a DFA M such that L = L(M).
Let M1 = (Q1, Σ1, δ1, q0, F1) and M2 = (Q2, Σ2, δ2, p0, F2) be
DFAs. Then M1 and M2 are equivalent iff L(M1) = L(M2).
Finite Automata, Lecture 3, slide 16

Notes:




A DFA M = (Q, Σ, δ,q0,F) partitions the set Σ* into two sets: L(M)
and
Σ* - L(M).
If L = L(M) then L is a subset of L(M) and L(M) is a subset of L.
Similarly, if L(M1) = L(M2) then L(M1) is a subset of L(M2) and L(M2)
is a subset of L(M1).
Some languages are regular, others are not. For example, if
L1 = {x | x is a string of 0's and 1's containing an even
number of 1's} and
L2 = {x | x = 0n1n for some n >= 0}
then L1 is regular but L2 is not.
Finite Automata, Lecture 3, slide 17

Questions:
 How do we determine whether or not a given
language is regular?
 How could a program “simulate” a DFA?
Finite Automata, Lecture 3, slide 18
The * Function




The  function gives 1-symbol moves
We'll define * so it gives whole-string results (by applying zero
or more  moves)
A recursive definition:
 *(q,) = q
*
 For all w in Σ and a in Σ
*(q,wa) = (*(q,w),a)
That is:
 For the empty string, no moves
 For any string wa (w is any string and a is any final symbol)
first make the moves on w, then one final move on a
Finite Automata, Lecture 3, slide 19
M Accepts x


*(q,w) is the state of M ends up in,
starting from state q and reading all of
string w
So *(q0,w) tells us whether M accepts w :
A string w  * is accepted by a DFA M = (Q, , , q0, F)
if and only if *(q0, w)  F.
Finite Automata, Lecture 3, slide 20
1
0
Example #1:
q1
q0
0



What is δ*(q0, 011)? Informally, it is the state entered by M after
processing 011 having started in state q0.
Formally:
δ*(q0, 011) = δ (δ*(q0,01), 1)
by rule #2
= δ (δ ( δ*(q0,0), 1), 1)
by rule #2
= δ (δ (δ (δ*(q0, λ), 0), 1), 1) by rule #2
= δ (δ (δ(q0,0), 1), 1)
by rule #1
= δ (δ (q1, 1), 1)
by definition of δ
= δ (q1, 1)
by definition of δ
= q1
by definition of δ
Is 011 accepted? No, since δ*(q0, 011) = q1 is not a final state.
Finite Automata, Lecture 3, slide 21
1
1
Example #2:
q0
1
1
0
0
q2
q1
0

What is δ(q0, 011)? Informally, it is the state entered by M after
processing 011 having started in state q0.
Formally:

δ(q0, 011) = δ (δ*(q0,01), 1)
= δ (δ (δ*(q0,0), 1), 1)
= δ (δ(δ(δ*(q0 , λ), 0),1), 1)
= δ (δ(δ(q0 , 0), 1),1)
= δ (δ(q1 , 1),1)
= δ (q1 , 1)
= q1
Is 011 accepted? No, since δ(q0, 011) = q1 is

Finite Automata, Lecture 3, slide 22
by
by
by
by
by
by
rule #2
rule #2
rule #2
definition of δ
definition of δ
definition of δ
not a final state.
1
Example #3:
q0
1
0
1
0
q2
q1
0


What is δ(q0, 100)?
δ(q0, 100) = δ (δ*(q0,10), 0)
= δ (δ (δ*(q0,1), 0), 0)
= δ (δ(δ(δ*(q0 , λ), 1),0), 0)
= δ (δ(δ(q0 , 1), 0),0)
= δ (δ(q0 , 0),0)
= δ (q1 , 0)
= q2
by
by
by
by
by
by
rule #2
rule #2
rule #2
definition of δ
definition of δ
definition of δ
Is 100 accepted? Yes, since δ(q0, 100) = q2 is a final state.
Finite Automata, Lecture 3, slide 23
Formal Definition of DFA
A DFA M is a 5-tuple M = (Q, , , q0, F) :
where Q is the finite set of states
 is the alphabet (that is, a finite set of symbols)
  (Q    Q) is the transition function
q0  Q is the start state
F  Q is the set of accepting states
Formal Definition of Computation
Let M = (Q, , , q0, F) and w = w1w2w3w4…wn be a string where each wi 
∑. Then M accepts w if a sequence of states r0, r1, …, rn in Q exists with three
conditions:
1. r0 = q0
2.  (ri, wi+1) = ri+1 for i = 0, …, n-1
3. rn  F
Finite Automata, Lecture 3, slide 24
a, b
b
Example #1:
q0
a
b
Q = {q0, q1, q2}
Σ = {a, b}
Start state is q0
F = {q2}
δ:
q0
a
q1
b
q0
q1
q2
q0
q2
q2
q2
Finite Automata, Lecture 3, slide 25
a
q1
q2
w= a b a b a b a a b
w = w1 w2 w3 w4 w5 w6 w7 w8 w9 , |w| = 9 , n = 9
r0 = q0
 (ri , wi+1) = ri+1 for i = 0, …, 8
a, b
b
a
a
q2
q0
q1
b
a
q0
r0
b
q1
r1
a
q0
r2
b
q1
r3
1.  (r0 , w1) = r1
 (q0 , a) = q1
5.  (r4 , w5) = r5
 (q0 , a) = q1
9.  (r8 , w9) = r9
 (q2 , b) = q2
a
q0
r4
b
q1
r5
a
q0
r6
a
q1
r7
b
q2
r8
q2
r9
2.  (r1 , w2) = r2 3.  (r2 , w3) = r3
 (q1 , b) = q0
 (q0 , a) = q1
6.  (r5 , w6) = r6 7.  (r6 , w7) = r7
 (q1 , b) = q0
,
 (q0 , a) = q1
4.  (r3 , w4) = r4
 (q1 , b) = q0
8.  (r7 , w8) = r8
 (q1 , a)= q2
As r9=q2 and q2 F, then String is accepted
Finite Automata, Lecture 3, slide 26
DFA Applications
Finite Automata, Lecture 3, slide 27
We have seen how DFAs can be used to define
formal languages. In addition to this formal
use, DFAs have practical applications. DFAbased pieces of code lie at the heart of many
commonly used computer programs.
Finite Automata, Lecture 3, slide 28
DFA Applications

Programming language processing


Command language processing


Scanning phase: dividing source file into
"tokens" (keywords, identifiers, constants,
etc.), skipping whitespace and comments
Typed command languages often require
the same kind of treatment
Text pattern matching

Unix tools like awk, egrep, and sed, mail
systems like ProcMail, database systems
like MySQL, and many others
Finite Automata, Lecture 3, slide 29
More DFA Applications

Signal processing


Speech processing and other signal
processing systems use finite state models
to transform the incoming signal
Controllers for finite-state systems


Hardware and software
A wide range of applications, from
industrial processes to video games
Finite Automata, Lecture 3, slide 30
Download