Nondeterministic Finite State Machines: Nondeterminism

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 28

03/09/2017

Nondeterministic
Finite State Machines

Nondeterminism

Imagine adding to a programming language the


function choice in either of the following forms:

1. choose (action 1;;


action 2;;

action n )

2. choose(x from S: P(x))

1
03/09/2017

Implementing Nondeterminism
before the first choice choose makes

first call to choose first call to choose


choice 1 choice 2

second call to choose second call to choose


choice 1 choice 2

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 is a finite set of states


Σ is an alphabet
s ∈ K is the initial state
A ⊆ K is the set of accepting states, and
∆ is the transition relation. It is a finite subset of

(K × (Σ ∪ {ε}) )× K
state input or empty string state
Where
you are What Where
× Cartesian product you see you go

Accepting by an NDFSM

M accepts a string w iff there exists some path along


which w drives M to some element of A.

The language accepted by M, denoted L(M), is the set


of all strings accepted by M.

3
03/09/2017

4
03/09/2017

Sources of Nondeterminism

What differ from determinism?

Analyzing Nondeterministic FSMs


Two approaches:

• Explore a search tree:

• Follow all paths in parallel

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}.

M = (K, Σ, ∆, s, A) = ({q0, q1, q2 , q3}, {a, b}, ∆, q0, {q3}),


where
∆ = {((q0, a), q1), ((q0, ε), q1),
((q1, a), q2), ((q2, a), q3), How many elements
does ∆ have?
((q3, b), q3)} 13

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)}

The Missing Letter Language


Let Σ = {a, b, c, d}.

Let LMissing =
{w : there is a symbol ai ∈ Σ not appearing in w}.

Try to make a DFSM for Lmissing

First develop machine for set of strings containing


ALL 4 characters, then reverse.

18

9
03/09/2017

The Missing Letter Language

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

Pattern Matching with NDFSMs

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

Checking from the End

L = {w ∈ {a, b}* :
the fourth to the last character is a}

25

12
03/09/2017

Checking from the End

L = {w ∈ {a, b}* :
the fourth to the last character is a}

26

Another Pattern Matching Example

L = {w ∈ {0, 1}* : w is the binary encoding of a


positive integer that is divisible by 16 or is
odd}

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

ε Transitions – eps function

eps(q) = {p ∈ K : (q, w) |-*M (p, w)}.

eps(q) is the closure of {q} under the relation


{(p, r) : there is a transition (p, ε, r) ∈ ∆}.

It simply means
How shall we compute eps(q)? the states
reachable without
consuming input.

32

An Algorithm to Compute eps(q)

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:

For each NDFSM, there is an equivalent DFSM.

36

Nondeterministic and
Deterministic FSMs
Theorem: For each NDFSM, there is an equivalent DFSM.
Proof: By construction:

Given a NDFSM M = (K, Σ, ∆, s, A),


we construct M' = (K', Σ, δ', s', A'), where

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

An Algorithm for Constructing the


Deterministic FSM

1. Compute the eps(q)’s.

2. Compute s' = eps(s).

3. Compute δ‘.

4. Compute K' = a subset of P(K).

5. Compute A' = {Q ∈ K' : Q ∩ A ≠ ∅}.

The algorithm
1. Proves NDFSM ≡ DFSM
2. Allows us to solve problems using 38
NDFSM then construct equivalent DFSM

The Algorithm ndfsm-to-dfsm


ndfsmtodfsm(M: NDFSM) =
1. For each state q in KM do:
1.1 Compute eps(q).
2. s' = eps(s)
3. Compute δ':
3.1 active-states = {s'}.
3.2 δ' = ∅.
3.3 While there exists some element Q of active-states for
which δ' has not yet been computed do:
For each character c in ΣM do:
new-state = ∅.
For each state q in Q do:
For each state p such that (q, c, p) ∈ ∆ do:
new-state = new-state ∪ eps(p).
Add the transition (Q, c, new-state) to δ'.
If new-state ∉ active-states then insert it.
4. K' = active-states.
5. A' = {Q ∈ K' : Q ∩ A ≠ ∅ }. 39

18
03/09/2017

40

41

19
03/09/2017

42

Another Example

43

20
03/09/2017

An Example – Optional Substrings


L = {w ∈ {a, b}* : w is made up of 0 to 2 a’s
followed by zero or more b’s}.
b

50

The Number of States May Grow


Exponentially
|Σ| = n

No. of states after 0 chars:  n 


=1
No. of new states after 1 char:   =n
 n − 1
 n 
No. of new states after 2 chars:  
 n − 2 = n(n-1)/2
No. of new states after 3 chars:  n  = n(n-1)(n-2)/6
 
 n − 3
51
Total number of states after n chars: 2n

21
03/09/2017

If the Original FSM is Deterministic


M

1. Compute the eps(q)s:


2. s' = eps(q0) =
3. Compute δ'
({q0}, odd, {q1}) ({q0}, even, {q0})
({q1}, odd, {q1}) ({q1}, even, {q0})
4. K' = {{q0}, {q1}}
5. A' = { {q1} }
M' = M 53

A Deterministic FSM Interpreter


dfsmsimulate(M: DFSM, w: string) =
1. st = s.
2. Repeat
2.1 c = get-next-symbol(w).
2.2 If c ≠ end-of-file then
2.2.1 st = δ(st, c).
until c = end-of-file.
3. If st ∈ A then accept else reject.

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:

1. Convert the NDFSM to a deterministic one:


• Conversion can take time and space 2|K|.
• Time to analyze string w: O(|w|)

2. Simulate the behavior of the nondeterministic one by


constructing sets of states "on the fly" during execution
• No conversion cost
• Time to analyze string w: O(|w| × |K|2)

3. Do a depth-first search of all paths through the


nondeterministic machine.

23
03/09/2017

Finite State Transducers

Finite State Transducers


• A finite state transducer (FST) is a finite state machine, that
transduces (translates) an input string into an output string.
• instead of {0,1} as in FSMs (acceptors / recognizers)
• input tape, output tape
• Moore machine and Mealy machine

• Moore machine: outputs are determined by the current


state alone (and do not depend directly on the input)
• Advantage of the Moore model is a simplification of the behavior

• Mealy machine: output depends on current state and input

24
03/09/2017

Moore and Mealy


Edward F. Moore (1925 – 2003)
• Professor of Math and CS in UW-madison
• Memorial resolution

George H. Mealy (1927 – 2010)

worked at the Bell Laboratories in 1950's and was a Harvard University


professor in 1970's

http://boards.ancestry.com/surnames.mealy/56.1.1/mb.ashx

Moore Machine
A Moore machine M = (K, Σ, O, δ, D, s, A), where:

• K is a finite set of states


• Σ is an input alphabet
• O is an output alphabet
• s ∈ K is the initial state
• A ⊆ K is the set of accepting states, (not important for some app.)
• δ is the transition function from (K × Σ) to K,
• D is the output function from K to O*.

M outputs each time it lands in a state.

A Moore machine M computes a function f(w) iff, when it


reads the input string w, its output sequence is f(w).

25
03/09/2017

A Simple US Traffic Light Controller

Mealy Machine
A Mealy machine M = (K, Σ, O, δ, s, A), where:

• K is a finite set of states


• Σ is an input alphabet
• O is an output alphabet
• s ∈ K is the initial state
• A ⊆ K is the set of accepting states (not important for some app.)
• δ is the transition function from (K × Σ) to (K × O*)

M outputs each time it takes a transition.

A Mealy machine M computes a function f(w) iff, when it


reads the input string w, its output sequence is f(w).

26
03/09/2017

An Odd Parity Generator


After every four bits, output a fifth bit such that
each group of five bits has odd parity.

0000 1000 1111

A Bar Code Scanner

0 12345 67890 5

27
03/09/2017

A Bar Code Scanner

28

You might also like