Finite Automata What can be computed with finite memory? input tape

advertisement
Finite Automata
What can be computed with finite memory?
input tape
0 1 1 0
state
···
Finite Automata
What can be computed with finite memory?
input tape
0 1 1 0
state
···
Finite Automata
What can be computed with finite memory?
input tape
0 1 1 0
state
···
A “Wild” Finite Automaton
The input is the numbers pressed by the
user
The correct code is 1234
Actually the door is opened if
{0, . . . , 9}∗ 1234{0, . . . , 9}∗ is entered
The machine needs to remember if the
input given so far contains the
subsequence 1234
Finite Automata: Informal Definition
1
0
1
q1
q2
0
The machine M:
States: q1 and q2
Start state: q1 (arrow from nowhere)
Accept state: q2 (double circle)
State transitions: arrows
Finite Automata: Informal Definition
1
0
1
q1
q2
0
The machine M:
On input string s = s1 s2 · · · sn , M operates as follows:
Begins in start state q1 and reads the string s from left to
right
When reading symbol si it follows the transition labeled si
from the current state
After reading sn , the last symbol in the string, it
accepts s if it is in an accept state
rejects s if it is not in an accept state
Finite Automata: Informal Definition
1
0
1
q1
q2
0
The machine M:
On input:
011
Finite Automata: Informal Definition
1
0
1
q1
q2
0
The machine M:
On input:
011 ACCEPT
Finite Automata: Informal Definition
1
0
1
q1
q2
0
The machine M:
On input:
011 ACCEPT
10
Finite Automata: Informal Definition
1
0
1
q1
q2
0
The machine M:
On input:
011 ACCEPT
10 REJECT
Finite Automata: Informal Definition
1
0
1
q1
q2
0
The machine M:
On input:
011 ACCEPT
10 REJECT
110
Finite Automata: Informal Definition
1
0
1
q1
q2
0
The machine M:
On input:
011 ACCEPT
10 REJECT
110 REJECT
A “Wild” Finite Automaton
The input is the numbers pressed by the
user
The correct code is 1234
Actually the door is opened if
{0, . . . , 9}∗ 1234{0, . . . , 9}∗ is entered
The machine needs to remember if the
input given so far contains the
subsequence 1234
A “Wild” Finite Automaton
The input is the numbers pressed by the
user
The correct code is 1234
Actually the door is opened if
{0, . . . , 9}∗ 1234{0, . . . , 9}∗ is entered
The machine needs to remember if the
input given so far contains the
subsequence 1234
0, 2, 3, 5, . . . , 9
0, . . . , 9
0, 2, . . . , 9
qs
0, 2, 4, . . . , 9
1
q1
2
3
q12
1
0, 3, . . . , 9
1
1
q123
4
q1234
Representation of finite automata
State diagram
1
0
1
q1
q2
0
Transition table
→ q1
F q2
0
q1
q1
1
q2
q2
Definition of DFAs
Definition
A deterministic finite automaton (DFA) is a 5-tuple
(Q, Σ, δ, q0 , F ) where
Q is a finite set called the states
Σ is a finite set called the alphabet
δ : Q × Σ → Q is the transition function
q0 ∈ Q is the start state
F ⊆ Q is the set of accept states
The language recognized by a DFA
Definition
Let M = (Q, Σ, δ, q0 , F ) be a DFA and s = s1 s2 · · · sn a string
over Σ. M accepts s if there is a sequence of states r0 , r1 , . . . rn
from Q such that
r0 = q0 ,
δ(ri , si+1 ) = ri+1 (i = 0, . . . , n − 1), and
rn ∈ F
The language recognized by a DFA
Definition
Let M = (Q, Σ, δ, q0 , F ) be a DFA and s = s1 s2 · · · sn a string
over Σ. M accepts s if there is a sequence of states r0 , r1 , . . . rn
from Q such that
r0 = q0 ,
δ(ri , si+1 ) = ri+1 (i = 0, . . . , n − 1), and
rn ∈ F
Definition
M recognizes language L if L = {s | M accepts s}
L(M) denotes the language recognized by M
Regular Languages
Definition
A language is a regular
language if some DFA
recognizes it
Nondeterminism
The machines we have seen so far have been
deterministic. The next state follows uniquely from the
current state and the input symbol
In a nondeterministic machine several possible next states
may follow from the current state and input symbol. These
possibilities can be thought of as being explored in parallel
Understanding the power of nondeterminism is a central
topic in the theory of computation (and this course)
Example of a nondeterminstic finite automaton (NFA)
0, 1
p
1
q
0, 1
r
Example of a nondeterminstic finite automaton (NFA)
0, 1
p
1
q
0, 1
r
An NFA accepts a string s if, after reading the last symbol
of s, at least one of its active states is an accept state
An NFA rejects a string s if, after reading the last symbol of
s, none if its active states is an accept state
Example of a nondeterminstic finite automaton (NFA)
0, 1
p
1
q
0, 1
r
Example of a nondeterminstic finite automaton (NFA)
0, 1
p
0101
1
q
0, 1
r
Example of a nondeterminstic finite automaton (NFA)
0, 1
p
0101
1
q
0, 1
r
Example of a nondeterminstic finite automaton (NFA)
0, 1
p
0101
1
q
0, 1
r
Example of a nondeterminstic finite automaton (NFA)
0, 1
p
0101
1
q
0, 1
r
Example of a nondeterminstic finite automaton (NFA)
0, 1
p
0101
1
q
0, 1
r
Example of a nondeterminstic finite automaton (NFA)
0, 1
p
0101
1
q
0, 1
r
Example of a nondeterminstic finite automaton (NFA)
0, 1
p
0101
1
q
0, 1
r
Example of a nondeterminstic finite automaton (NFA)
0, 1
p
0101
1
q
0, 1
r
Example of a nondeterminstic finite automaton (NFA)
0, 1
p
0101
1
q
0, 1
r
Example of a nondeterminstic finite automaton (NFA)
0, 1
p
0101 REJECT
1
q
0, 1
r
Example of a nondeterminstic finite automaton (NFA)
0, 1
p
110
1
q
0, 1
r
Example of a nondeterminstic finite automaton (NFA)
0, 1
p
110
1
q
0, 1
r
Example of a nondeterminstic finite automaton (NFA)
0, 1
p
110
1
q
0, 1
r
Example of a nondeterminstic finite automaton (NFA)
0, 1
p
110
1
q
0, 1
r
Example of a nondeterminstic finite automaton (NFA)
0, 1
p
110
1
q
0, 1
r
Example of a nondeterminstic finite automaton (NFA)
0, 1
p
110
1
q
0, 1
r
Example of a nondeterminstic finite automaton (NFA)
0, 1
p
110
1
q
0, 1
r
Example of a nondeterminstic finite automaton (NFA)
0, 1
p
110 ACCEPT
1
q
0, 1
r
Example of a NFA with ε transitions
1
0
1
q1
q2
0
0, 1
p
1
q
0, 1
r
Example of a NFA with ε transitions
1
0
1
q1
q2
ε
0
s
0, 1
ε
p
1
q
0, 1
r
Example of a NFA with ε transitions
1
0
1
q1
q2
ε
0
s
0, 1
ε
p
01
1
q
0, 1
r
Example of a NFA with ε transitions
1
0
1
q1
q2
ε
0
s
0, 1
ε
p
01
1
q
0, 1
r
Example of a NFA with ε transitions
1
0
1
q1
q2
ε
0
s
0, 1
ε
p
01
1
q
0, 1
r
Example of a NFA with ε transitions
1
0
1
q1
q2
ε
0
s
0, 1
ε
p
01 ACCEPT
1
q
0, 1
r
Definition of NFAs
The power set of Q, written P(Q), is the set of all subsets of Q
Example
If A = {1, 2} then P(A) = {∅, {1}, {2}, {1, 2}}
Definition of NFAs
Definition
A nondeterministic finite automaton (NFA) is a 5-tuple
(Q, Σ, δ, q0 , F ) where
Q is a finite set called the states
Σ is a finite set called the alphabet
δ : Q × (Σ ∪ {ε}) → P(Q) is the transition function
q0 ∈ Q is the start state
F ⊆ Q is the set of accept states
The language recognized by a NFA
Definition
Let N = (Q, Σ, δ, q0 , F ) be a NFA and s = s1 s2 · · · sm a string
over Σ. N accepts s if there is a sequence of states r0 , r1 , . . . rn
from Q and s can be written as s = s1 , s2 . . . , sn where each
si ∈ Σ ∪ {ε} such that
r0 = q0 ,
ri+1 ∈ δ(ri , si+1 ) (i = 0, . . . , n − 1), and
rn ∈ F
Equivalence of DFAs and NFAs
Definition
Two machines are equivalent if they recognize the same
language
Equivalence of DFAs and NFAs
Theorem
Every NFA has an equivalent DFA
Equivalence of DFAs and NFAs
Theorem
Every NFA has an equivalent DFA
Proof idea.
Given a NFA we need to construct a DFA that simulate the NFA
The DFA need to keep track of the set of active states of
the NFA at each step
If k is the number of states of the NFA, then the DFA might
need up to 2k states (one for each subset of states of the
NFA)
So, the states of the DFA should be P(Q) where Q is the
states of the NFA
Equivalence of DFAs and NFAs
Theorem
Every NFA has an equivalent DFA
Notation: Let E (R) be the set of states that can be reached
from R using 0 or more ε transitions.
Equivalence of DFAs and NFAs
Theorem
Every NFA has an equivalent DFA
Notation: Let E (R) be the set of states that can be reached
from R using 0 or more ε transitions.
Proof.
The subset construction: Given a NFA N = (Q, Σ, δ, q0 , F ) we
construct an equivalent DFA M = (Q ′ , Σ, δ′ , q0′ , F ′ ) where
Q ′ = P(Q)
q0′ = E ({q0 })
F ′ = {R ∈ Q ′ | R ∩ F 6= ∅}
Equivalence of DFAs and NFAs
Theorem
Every NFA has an equivalent DFA
Notation: Let E (R) be the set of states that can be reached
from R using 0 or more ε transitions.
Proof.
The subset construction: Given a NFA N = (Q, Σ, δ, q0 , F ) we
construct an equivalent DFA M = (Q ′ , Σ, δ′ , q0′ , F ′ ) where
For R ∈ Q ′ and a ∈ Σ,
δ′ (R, a) = {q ∈ Q | q ∈ E (δ(r , a)) for some r ∈ R}
δ′ (R, a) is the set of all states that can be reached (in the
NFA) by first following a transition labeled a from a state in
R and then following 0 or more ε transitions
Equivalence of DFAs and NFAs: Example
1
0
1
q1
q2
ε
0
s
0, 1
ε
p
→ {s, q1 , p}
1
q
0
{q1 , p}
0, 1
r
1
{q2 , p, q}
Equivalence of DFAs and NFAs: Example
1
0
1
q1
q2
ε
0
s
0, 1
ε
p
→ {s, q1 , p}
{q1 , p}
1
q
0
{q1 , p}
{q1 , p}
0, 1
r
1
{q2 , p, q}
{q2 , p, q}
Equivalence of DFAs and NFAs: Example
1
0
1
q1
q2
ε
0
s
0, 1
ε
p
→ {s, q1 , p}
{q1 , p}
F {q2 , p, q}
1
q
0
{q1 , p}
{q1 , p}
{q1 , p, r }
0, 1
r
1
{q2 , p, q}
{q2 , p, q}
{q2 , p, q, r }
Equivalence of DFAs and NFAs: Example
1
0
1
q1
q2
ε
0
s
0, 1
ε
p
→ {s, q1 , p}
{q1 , p}
F {q2 , p, q}
F {q1 , p, r }
1
q
0
{q1 , p}
{q1 , p}
{q1 , p, r }
{q1 , p}
0, 1
r
1
{q2 , p, q}
{q2 , p, q}
{q2 , p, q, r }
{q2 , p, q}
Equivalence of DFAs and NFAs: Example
1
0
1
q1
q2
ε
0
s
0, 1
ε
p
→ {s, q1 , p}
{q1 , p}
F {q2 , p, q}
F {q1 , p, r }
F {q2 , p, q, r }
1
q
0
{q1 , p}
{q1 , p}
{q1 , p, r }
{q1 , p}
{q1 , p, r }
0, 1
r
1
{q2 , p, q}
{q2 , p, q}
{q2 , p, q, r }
{q2 , p, q}
{q2 , p, q, r }
Equivalence of DFAs and NFAs: Example
1
0
1
q1
q2
ε
0
s
0, 1
ε
p
1
0, 1
q
r
1
{q2 , p, q}
1
1
{s, q1 , q}
1
0
0
{q1 , p, r }
0
{q1 , p}
0
0
{q2 , p, q, r }
1
Equivalence of DFAs and NFAs
Theorem
Every NFA has an equivalent DFA
Corollary
Every language recognized by a NFA can be recognized by a
DFA
Download