Professional Documents
Culture Documents
Nondeterministic Finite State Machines: Nondeterminism
Nondeterministic Finite State Machines: Nondeterminism
Nondeterministic Finite State Machines: Nondeterminism
Nondeterministic
Finite State Machines
Nondeterminism
1
03/09/2017
Implementing Nondeterminism
before the first choice choose makes
Nondeterminism
• What it means/implies
– We could guess and our guesses would lead
us to the answer correctly (if there is an
answer).
2
03/09/2017
Definition of an NDFSM
M = (K, Σ, ∆, s, A), where:
(K × (Σ ∪ {ε}) )× K
state input or empty string state
Where
you are What Where
× Cartesian product you see you go
Accepting by an NDFSM
3
03/09/2017
4
03/09/2017
Sources of Nondeterminism
10
5
03/09/2017
Optional Substrings
L = {w ∈ {a, b}* : w is made up of an optional a
followed by aa followed by zero or more b’s}.
11
Optional Substrings
L = {w ∈ {a, b}* : w is made up of an optional a
followed by aa followed by zero or more b’s}.
12
6
03/09/2017
Optional Substrings
L = {w ∈ {a, b}* : w is made up of an optional a
followed by aa followed by zero or more b’s}.
Multiple Sublanguages
L = {w ∈ {a, b}* : w = aba or |w| is even}.
14
7
03/09/2017
Multiple Sublanguages
L = {w ∈ {a, b}* : w = aba or |w| is even}.
15
Multiple Sublanguages
L = {w ∈ {a, b}* : w = aba or |w| is even}.
16
8
03/09/2017
Multiple Sublanguages
L = {w ∈ {a, b}* : w = aba or |w| is even}.
M = (K, Σ, ∆, s, A) = ({q0, q1, q2, q3, q4, q5, q6}, {a, b}, ∆,
q0, {q4, q5}), where
Do you start to
∆ = {((q0, ε), q1), ((q0, ε), q5), feel the power of
nondeterminism?
((q1, a), q2), ((q2, b), q3), ((q3, a), q4),
17
((q5, a), q6), ((q5, b), q6), ((q6, a), q5), ((q6, b), q5)}
Let LMissing =
{w : there is a symbol ai ∈ Σ not appearing in w}.
18
9
03/09/2017
20
Pattern Matching
L = {w ∈ {a, b, c}* : ∃x, y ∈ {a, b, c}* (w = x abcabb y)}.
A DFSM:
21
10
03/09/2017
Pattern Matching
L = {w ∈ {a, b, c}* : ∃x, y ∈ {a, b, c}* (w = x abcabb y)}.
A DFSM:
An NDFSM:
22
L = {w ∈ {a, b}* :
∃x, y ∈ {a, b}* : w = x aabbb y or
w = x abbab y }
23
11
03/09/2017
Multiple Keywords
L = {w ∈ {a, b}* : ∃x, y ∈ {a, b}*
((w = x abbaa y) ∨ (w = x baba y))}.
24
L = {w ∈ {a, b}* :
the fourth to the last character is a}
25
12
03/09/2017
L = {w ∈ {a, b}* :
the fourth to the last character is a}
26
27
13
03/09/2017
Another NDFSM
L1= {w ∈ {a, b}*: aa occurs in w}
L2= {x ∈ {a, b}*: bb occurs in x}
L3= {y : ∈ L1 or L2 }
L4= L1L2
M1 = M2=
M3=
M4=
28
A “Reel” Example
29
14
03/09/2017
It simply means
How shall we compute eps(q)? the states
reachable without
consuming input.
32
eps(q: state) =
result = {q}.
While there exists some p ∈ result and
some r ∉ result and
some transition (p, ε, r) ∈ ∆ do:
Insert r into result.
Return result.
33
15
03/09/2017
An Example of eps
eps(q0) =
eps(q1) =
eps(q2) =
eps(q3) = 34
Simulating a NDFSM
ndfsmsimulate(M: NDFSM, w: string) =
1. current-state = eps(s).
2. While any input symbols in w remain to be read do:
1. c = get-next-symbol(w).
2. next-state = ∅.
3. For each state q in current-state do:
For each state p such that (q, c, p) ∈ ∆ do:
next-state = next-state ∪ eps(p).
4. current-state = next-state.
3. If current-state contains any states in A, accept. Else
reject.
35
16
03/09/2017
Nondeterministic and
Deterministic FSMs
Clearly: {Languages accepted by a DFSM} ⊆
{Languages accepted by a NDFSM}
More interestingly:
Theorem:
36
Nondeterministic and
Deterministic FSMs
Theorem: For each NDFSM, there is an equivalent DFSM.
Proof: By construction:
K' = P(K)
May create many
s' = eps(s) unreachable states
A' = {Q ⊆ K : Q ∩ A ≠ ∅}
δ'(Q, a) = ∪{eps(p): p ∈ K and
(q, a, p) ∈ ∆ for some q ∈ Q}
37
17
03/09/2017
3. Compute δ‘.
The algorithm
1. Proves NDFSM ≡ DFSM
2. Allows us to solve problems using 38
NDFSM then construct equivalent DFSM
18
03/09/2017
40
41
19
03/09/2017
42
Another Example
43
20
03/09/2017
50
21
03/09/2017
Input: aabaa
58
22
03/09/2017
A NDFSM Interpreter
ndfsmsimulate(M = (K, Σ, ∆, s, A): NDFSM, w: string) =
1. Declare the set st.
2. Declare the set st1.
3. st = eps(s).
4. Repeat
4.1 c = get-next-symbol(w).
4.2 If c ≠ end-of-file then do
st1 = ∅.
For all q ∈ st do
For all r ∈ ∆(q, c) do
st1 = st1 ∪ eps(r).
st = st1.
If st = ∅ then exit.
until c = end-of-file.
5. If st ∩ A ≠ ∅ then accept else reject. 59
Nondeterministic FSMs as
Algorithms
Real computers are deterministic, so we have three choices
if we want to execute an NDFSM:
23
03/09/2017
24
03/09/2017
http://boards.ancestry.com/surnames.mealy/56.1.1/mb.ashx
Moore Machine
A Moore machine M = (K, Σ, O, δ, D, s, A), where:
25
03/09/2017
Mealy Machine
A Mealy machine M = (K, Σ, O, δ, s, A), where:
26
03/09/2017
0 12345 67890 5
27
03/09/2017
28