Equivalence of DFA and NFA

advertisement
CSE 135: Introduction to Theory of Computation
Equivalence of DFA and NFA
Sungjin Im
University of California, Merced
02-05-2014
Expressive Power of NFAs and DFAs
I
Is there a language that is recognized by a DFA but not by
any NFAs?
Expressive Power of NFAs and DFAs
I
Is there a language that is recognized by a DFA but not by
any NFAs? No!
Expressive Power of NFAs and DFAs
I
Is there a language that is recognized by a DFA but not by
any NFAs? No!
I
Is there a language that is recognized by an NFA but not by
any DFAs?
Expressive Power of NFAs and DFAs
I
Is there a language that is recognized by a DFA but not by
any NFAs? No!
I
Is there a language that is recognized by an NFA but not by
any DFAs? No!
Main Theorem
Theorem
A language L is regular if and only if there is an NFA N such that
L(N) = L.
Main Theorem
Theorem
A language L is regular if and only if there is an NFA N such that
L(N) = L.
In other words:
I
For any DFA D, there is an NFA N such that L(N) = L(D),
and
I
for any NFA N, there is a DFA D such that L(D) = L(N).
Converting DFAs to NFAs
Proposition
For any DFA D, there is an NFA N such that L(N) = L(D).
Converting DFAs to NFAs
Proposition
For any DFA D, there is an NFA N such that L(N) = L(D).
Proof.
Is a DFA an NFA?
.
Converting DFAs to NFAs
Proposition
For any DFA D, there is an NFA N such that L(N) = L(D).
Proof.
Is a DFA an NFA? Essentially yes! Syntactically, not quite. The
formal definition of DFA has δDFA : Q × Σ → Q whereas
δNFA : Q × (Σ ∪ {}) → P(Q).
.
Converting DFAs to NFAs
Proposition
For any DFA D, there is an NFA N such that L(N) = L(D).
Proof.
Is a DFA an NFA? Essentially yes! Syntactically, not quite. The
formal definition of DFA has δDFA : Q × Σ → Q whereas
δNFA : Q × (Σ ∪ {}) → P(Q).
For DFA D = (Q, Σ, δD , q0 , F ), define an “equivalent” NFA
N = (Q, Σ, δN , q0 , F ) that has the exact same set of states, initial
state and final states. Only difference is in the transition function.
δN (q, a) = {δD (q, a)}
for a ∈ Σ and δN (q, ) = ∅ for all q ∈ Q.
Simulating an NFA on Your Computer
NFA Acceptance Problem
Given an NFA N and an input string w , does N accept w ?
Simulating an NFA on Your Computer
NFA Acceptance Problem
Given an NFA N and an input string w , does N accept w ?
How do we write a computer program to solve the NFA
Acceptance problem?
Two Views of Nondeterminsm
Guessing View
Parallel View
At each step, the NFA “guesses”
one of the choices available; the
NFA will guess an “accepting
sequence of choices” if such a
one exists.
At each step the machine “forks”
threads corresponding to each of
the possible next states.
Two Views of Nondeterminsm
Guessing View
Parallel View
At each step, the NFA “guesses”
one of the choices available; the
NFA will guess an “accepting
sequence of choices” if such a
one exists.
Very useful in reasoning about
NFAs and in designing NFAs.
At each step the machine “forks”
threads corresponding to each of
the possible next states.
Two Views of Nondeterminsm
Guessing View
Parallel View
At each step, the NFA “guesses”
one of the choices available; the
NFA will guess an “accepting
sequence of choices” if such a
one exists.
Very useful in reasoning about
NFAs and in designing NFAs.
At each step the machine “forks”
threads corresponding to each of
the possible next states.
Very useful in simulating/running
NFA on inputs.
Algorithm for Simulating an NFA
Algorithm
Keep track of the current state of each of the active threads.
Algorithm for Simulating an NFA
Algorithm
Keep track of the current state of each of the active threads.
Example
0, 1
q0
0, 1
1
q1
Example NFA N
Consider the input w = 111. The
execution (listing only the states of
currently active threads) is
Algorithm for Simulating an NFA
Algorithm
Keep track of the current state of each of the active threads.
Example
0, 1
q0
0, 1
1
q1
Example NFA N
Consider the input w = 111. The
execution (listing only the states of
currently active threads) is
hq0 i
Algorithm for Simulating an NFA
Algorithm
Keep track of the current state of each of the active threads.
Example
0, 1
q0
0, 1
1
q1
Example NFA N
Consider the input w = 111. The
execution (listing only the states of
currently active threads) is
1
hq0 i −→ hq0 , q1 i
Algorithm for Simulating an NFA
Algorithm
Keep track of the current state of each of the active threads.
Example
0, 1
q0
1
0, 1
Consider the input w = 111. The
execution (listing only the states of
currently active threads) is
q1
hq0 i −→ hq0 , q1 i −→ hq0 , q1 , q1 i
1
1
Example NFA N
1
−→ hq0 , q1 , q1 , q1 i
Algorithm
With optimizations
Observations
I
Exponentially growing memory: more threads for longer
inputs. Can we do better?
Algorithm
With optimizations
Observations
I
I
Exponentially growing memory: more threads for longer
inputs. Can we do better?
Exact order of threads is not important
Algorithm
With optimizations
Observations
I
I
Exponentially growing memory: more threads for longer
inputs. Can we do better?
Exact order of threads is not important
I
It is unimportant whether the 5th thread or the 1st thread is in
state q.
Algorithm
With optimizations
Observations
I
I
Exponentially growing memory: more threads for longer
inputs. Can we do better?
Exact order of threads is not important
I
I
It is unimportant whether the 5th thread or the 1st thread is in
state q.
If two threads are in the same state, then we can ignore one
of the threads
Algorithm
With optimizations
Observations
I
I
Exponentially growing memory: more threads for longer
inputs. Can we do better?
Exact order of threads is not important
I
I
It is unimportant whether the 5th thread or the 1st thread is in
state q.
If two threads are in the same state, then we can ignore one
of the threads
I
Threads in the same state will “behave” identically; either one
of the descendent threads of both will reach a final state, or
none of the descendent threads of both will reach a final state
Parsimonious Algorithm in Action
Example
0, 1
q0
0, 1
1
q1
Example NFA N
Consider the input w = 111. The
execution (listing only the states of
currently active threads) is
Parsimonious Algorithm in Action
Example
0, 1
q0
0, 1
1
q1
Example NFA N
Consider the input w = 111. The
execution (listing only the states of
currently active threads) is
{q0 }
Parsimonious Algorithm in Action
Example
0, 1
q0
0, 1
1
q1
Example NFA N
Consider the input w = 111. The
execution (listing only the states of
currently active threads) is
1
{q0 } −→ {q0 , q1 }
Parsimonious Algorithm in Action
Example
0, 1
q0
0, 1
1
q1
Example NFA N
Consider the input w = 111. The
execution (listing only the states of
currently active threads) is
1
1
{q0 } −→ {q0 , q1 } −→ {q0 , q1 }
Parsimonious Algorithm in Action
Example
0, 1
q0
0, 1
1
q1
Consider the input w = 111. The
execution (listing only the states of
currently active threads) is
1
1
Example NFA N
1
{q0 } −→ {q0 , q1 } −→ {q0 , q1 }
−→ {q0 , q1 }
Revisiting NFA Simulation Algorithm
I
Need to keep track of the states of the active threads
I
I
Unordered: Without worrying about exactly which thread is in
what state
No Duplicates: Keeping only one copy if there are multiple
threads in same state
Revisiting NFA Simulation Algorithm
I
Need to keep track of the states of the active threads
I
I
I
Unordered: Without worrying about exactly which thread is in
what state
No Duplicates: Keeping only one copy if there are multiple
threads in same state
How much memory is needed?
Revisiting NFA Simulation Algorithm
I
Need to keep track of the states of the active threads
I
I
I
Unordered: Without worrying about exactly which thread is in
what state
No Duplicates: Keeping only one copy if there are multiple
threads in same state
How much memory is needed?
I
I
If Q is the set of states of the NFA N, then we need to keep a
subset of Q!
Can be done in |Q| bits of memory (i.e., 2|Q| states), which is
finite!!
Constructing an Equivalent DFA
I
The DFA runs the simulation algorithm
Constructing an Equivalent DFA
I
The DFA runs the simulation algorithm
I
DFA remembers the current states of active threads without
duplicates, i.e., maintains a subset of states of the NFA
Constructing an Equivalent DFA
I
The DFA runs the simulation algorithm
I
DFA remembers the current states of active threads without
duplicates, i.e., maintains a subset of states of the NFA
I
When a new symbol is read, it updates the states of the
active threads
Constructing an Equivalent DFA
I
The DFA runs the simulation algorithm
I
DFA remembers the current states of active threads without
duplicates, i.e., maintains a subset of states of the NFA
I
When a new symbol is read, it updates the states of the
active threads
I
Accepts whenever one of the threads is in a final state
Example of Equivalent DFA
0, 1
q0
0, 1
1
q1
Example NFA N
Example of Equivalent DFA
0, 1
q0
0, 1
1
q1
Example NFA N
0, 1
0, 1
{q1 }
{}
0, 1
0
{q0 }
1
{q0 , q1 }
DFA D equivalent to N
Recall . . .
Definition
For an NFA M = (Q, Σ, δ, q0 , F ), string w , and state q1 ∈ Q, we
ˆ 1 , w ) to denote states of all the active threads of
say ∆(q
computation on input w from q1 .
Recall . . .
Definition
For an NFA M = (Q, Σ, δ, q0 , F ), string w , and state q1 ∈ Q, we
ˆ 1 , w ) to denote states of all the active threads of
say ∆(q
computation on input w from q1 . Formally,
w
ˆ 1 , w ) = {q ∈ Q | q1 −→
∆(q
M q}
Formal Construction
Given NFA N = (Q, Σ, δ, q0 , F ), construct DFA
det(N) = (Q 0 , Σ, δ 0 , q00 , F 0 ) as follows.
I
Q0 =
I
q00 =
I
F0 =
Formal Construction
Given NFA N = (Q, Σ, δ, q0 , F ), construct DFA
det(N) = (Q 0 , Σ, δ 0 , q00 , F 0 ) as follows.
I
Q 0 = P(Q)
I
q00 =
I
F0 =
Formal Construction
Given NFA N = (Q, Σ, δ, q0 , F ), construct DFA
det(N) = (Q 0 , Σ, δ 0 , q00 , F 0 ) as follows.
I
I
I
Q 0 = P(Q)
ˆ 0 , )
q 0 = ∆(q
0
F0
=
Formal Construction
Given NFA N = (Q, Σ, δ, q0 , F ), construct DFA
det(N) = (Q 0 , Σ, δ 0 , q00 , F 0 ) as follows.
I
I
I
Q 0 = P(Q)
ˆ 0 , )
q 0 = ∆(q
0
F0
= {A ⊆ Q | A ∩ F 6= ∅}
Formal Construction
Given NFA N = (Q, Σ, δ, q0 , F ), construct DFA
det(N) = (Q 0 , Σ, δ 0 , q00 , F 0 ) as follows.
I
I
I
Q 0 = P(Q)
ˆ 0 , )
q 0 = ∆(q
0
F0
= {A ⊆ Q | A ∩ F 6= ∅}
I δ 0 ({q1 , q2 , . . . qk }, a)
=
Formal Construction
Given NFA N = (Q, Σ, δ, q0 , F ), construct DFA
det(N) = (Q 0 , Σ, δ 0 , q00 , F 0 ) as follows.
I
I
I
Q 0 = P(Q)
ˆ 0 , )
q 0 = ∆(q
0
F0
= {A ⊆ Q | A ∩ F 6= ∅}
ˆ 1 , a) ∪ ∆(q
ˆ 2 , a) ∪ · · · ∪ ∆(q
ˆ k , a)
= ∆(q
I δ 0 ({q1 , q2 , . . . qk }, a)
Formal Construction
Given NFA N = (Q, Σ, δ, q0 , F ), construct DFA
det(N) = (Q 0 , Σ, δ 0 , q00 , F 0 ) as follows.
I
I
I
Q 0 = P(Q)
ˆ 0 , )
q 0 = ∆(q
0
F0
= {A ⊆ Q | A ∩ F 6= ∅}
ˆ 1 , a) ∪ ∆(q
ˆ 2 , a) ∪ · · · ∪ ∆(q
ˆ k , a) or
= ∆(q
more concisely,
[
ˆ
δ 0 (A, a) =
∆(q,
a)
I δ 0 ({q1 , q2 , . . . qk }, a)
q∈A
Correctness
Lemma
For any NFA N, the DFA det(N) is equivalent to it, i.e.,
L(N) = L(det(N)).
Correctness
Lemma
For any NFA N, the DFA det(N) is equivalent to it, i.e.,
L(N) = L(det(N)).
Proof Idea
Need to show
Correctness
Lemma
For any NFA N, the DFA det(N) is equivalent to it, i.e.,
L(N) = L(det(N)).
Proof Idea
Need to show
∀w ∈ Σ∗ . det(N) accepts w iff N accepts w
Correctness
Lemma
For any NFA N, the DFA det(N) is equivalent to it, i.e.,
L(N) = L(det(N)).
Proof Idea
Need to show
∀w ∈ Σ∗ . det(N) accepts w iff N accepts w
ˆ 0 , w ) ∩ F 6= ∅
∀w ∈ Σ∗ .δ̂(q00 , w ) ∈ F 0 iff ∆(q
Correctness
Lemma
For any NFA N, the DFA det(N) is equivalent to it, i.e.,
L(N) = L(det(N)).
Proof Idea
Need to show
∀w ∈ Σ∗ . det(N) accepts w iff N accepts w
ˆ 0 , w ) ∩ F 6= ∅
∀w ∈ Σ∗ .δ̂(q00 , w ) ∈ F 0 iff ∆(q
∗
0
ˆ 0 , w ) ∩ F 6= ∅
∀w ∈ Σ . for A = δ̂(q0 , w ), A ∩ F 6= ∅ iff ∆(q
Correctness
Lemma
For any NFA N, the DFA det(N) is equivalent to it, i.e.,
L(N) = L(det(N)).
Proof Idea
Need to show
∀w ∈ Σ∗ . det(N) accepts w iff N accepts w
ˆ 0 , w ) ∩ F 6= ∅
∀w ∈ Σ∗ .δ̂(q00 , w ) ∈ F 0 iff ∆(q
∗
0
ˆ 0 , w ) ∩ F 6= ∅
∀w ∈ Σ . for A = δ̂(q0 , w ), A ∩ F 6= ∅ iff ∆(q
We will instead prove the stronger claim ∀w ∈ Σ∗ . δ̂(q00 , w ) = A iff
ˆ 0 , w ) = A.
∆(q
Correctness Proof
Lemma
ˆ 0 , w ) = A.
∀w ∈ Σ∗ . δ̂(q00 , w ) = A iff ∆(q
Correctness Proof
Lemma
ˆ 0 , w ) = A.
∀w ∈ Σ∗ . δ̂(q00 , w ) = A iff ∆(q
Proof.
By induction on |w |
Correctness Proof
Lemma
ˆ 0 , w ) = A.
∀w ∈ Σ∗ . δ̂(q00 , w ) = A iff ∆(q
Proof.
By induction on |w |
I
Base Case |w | = 0: Then w = . Now
Correctness Proof
Lemma
ˆ 0 , w ) = A.
∀w ∈ Σ∗ . δ̂(q00 , w ) = A iff ∆(q
Proof.
By induction on |w |
I
Base Case |w | = 0: Then w = . Now
δ̂(q00 , ) = q00
defn. of δ̂
Correctness Proof
Lemma
ˆ 0 , w ) = A.
∀w ∈ Σ∗ . δ̂(q00 , w ) = A iff ∆(q
Proof.
By induction on |w |
I
Base Case |w | = 0: Then w = . Now
δ̂(q00 , ) = q00
ˆ 0 , )
= ∆(q
defn. of δ̂
defn. of q00
Correctness Proof
Lemma
ˆ 0 , w ) = A.
∀w ∈ Σ∗ . δ̂(q00 , w ) = A iff ∆(q
Proof.
By induction on |w |
I
Base Case |w | = 0: Then w = . Now
δ̂(q00 , ) = q00
ˆ 0 , )
= ∆(q
I
defn. of δ̂
defn. of q00
Induction Hypothesis: Assume inductively that the statement
holds ∀w . |w | = n
··→
Correctness Proof
Induction Step
Proof (contd).
I
Induction Step: If |w | = n + 1 then w = ua with |u| = n and
a ∈ Σ.
δ̂(q00 , ua) = δ(δ̂(q00 , u), a)
defn. of δ̂
Correctness Proof
Induction Step
Proof (contd).
I
Induction Step: If |w | = n + 1 then w = ua with |u| = n and
a ∈ Σ.
δ̂(q00 , ua) = δ(δ̂(q00 , u), a)
ˆ 0 , u), a)
= δ(∆(q
defn. of δ̂
ind. hyp.
Correctness Proof
Induction Step
Proof (contd).
I
Induction Step: If |w | = n + 1 then w = ua with |u| = n and
a ∈ Σ.
δ̂(q00 , ua) = δ(δ̂(q00 , u), a)
ˆ 0 , u), a)
= δ(∆(q
[
ˆ
=
∆(q,
a)
defn. of δ̂
ind. hyp.
defn. of δ
ˆ 0 ,u)
q∈∆(q
Correctness Proof
Induction Step
Proof (contd).
I
Induction Step: If |w | = n + 1 then w = ua with |u| = n and
a ∈ Σ.
δ̂(q00 , ua) = δ(δ̂(q00 , u), a)
ˆ 0 , u), a)
= δ(∆(q
[
ˆ
=
∆(q,
a)
defn. of δ̂
ind. hyp.
defn. of δ
ˆ 0 ,u)
q∈∆(q
ˆ 0 , ua)
= ∆(q
ˆ
prop. about ∆
Another Example
0, 1
q1
{q3 }
0
q0
q3
1
q2
Example NFA N
0, 1
0, 1
0
{q0 , q1 }
{}
1
{q2 , q3 }
DFA D0 for N (only relevant states)
Download