Top Banner
FSA FSA Basic Design Proving a property of an FSA Finite State Automata Design Nicholas Mainardi 1 Dipartimento di Elettronica e Informazione Politecnico di Milano [email protected] March 26, 2020 1 Mostly based on Alessandro Barenghi’s material, enriched by few additional examples.
30

Finite State Automata Design - Politecnico di Milano · 2020-03-26 · Finite State Automata (Automi a stati niti AF) are the simplest among abstract computing models ... automaton,

Jul 31, 2020

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Finite State Automata Design - Politecnico di Milano · 2020-03-26 · Finite State Automata (Automi a stati niti AF) are the simplest among abstract computing models ... automaton,

FSA FSA Basic Design Proving a property of an FSA

Finite State Automata Design

Nicholas Mainardi1

Dipartimento di Elettronica e InformazionePolitecnico di Milano

[email protected]

March 26, 2020

1Mostly based on Alessandro Barenghi’s material, enriched by few additionalexamples.

Page 2: Finite State Automata Design - Politecnico di Milano · 2020-03-26 · Finite State Automata (Automi a stati niti AF) are the simplest among abstract computing models ... automaton,

FSA FSA Basic Design Proving a property of an FSA

Finite State Automata

What are FSAs?

Finite State Automata (Automi a stati finiti AF) are thesimplest among abstract computing models

They are still able to describe a good deal of useful systems

They are characterized by a finite memory capacity

In a sense, every real world computing machine is a FSA (as itonly has a limited number of memory cells)

However modeling it as such is not a good idea (too manystates, roughly 22

43 ' 10300000000000)

Page 3: Finite State Automata Design - Politecnico di Milano · 2020-03-26 · Finite State Automata (Automi a stati niti AF) are the simplest among abstract computing models ... automaton,

FSA FSA Basic Design Proving a property of an FSA

Formalization

A recognizer FSA is formally defined as a quintuple(Q, I, δ, q0,F), where:

Q, is the set of states of the automataI is the alphabet of the input string which will be checkedδ : Q× I 7→ Q the transition functionq0 ∈ Q the (unique) initial state from where the automatonstartsF ⊆ Q the set of final accepting states of the automaton

Page 4: Finite State Automata Design - Politecnico di Milano · 2020-03-26 · Finite State Automata (Automi a stati niti AF) are the simplest among abstract computing models ... automaton,

FSA FSA Basic Design Proving a property of an FSA

Regular expressions cheat sheet

A small RE cheat sheet

Symbols belonging to the input alphabet are usually lowercase

s.t : s concatenated with t

r | s : either r or s

Parentheses force a precedence when reading the regexpr

s∗: an arbitrary number of occurrences of s (Kleene’s Star)

s+: one or more occurrences of s (equivalent to s(s)∗)

s?: s is optional → zero or one occurrence of s (equivalent tos | ε)

Page 5: Finite State Automata Design - Politecnico di Milano · 2020-03-26 · Finite State Automata (Automi a stati niti AF) are the simplest among abstract computing models ... automaton,

FSA FSA Basic Design Proving a property of an FSA

Introduction

An oven knob

We will start looking at the design for a simple automatonrepresenting an oven knob

The oven may be On, Off or on Grill position.

The knob turns left and right: left raises the temperature,right lowers it.

Page 6: Finite State Automata Design - Politecnico di Milano · 2020-03-26 · Finite State Automata (Automi a stati niti AF) are the simplest among abstract computing models ... automaton,

FSA FSA Basic Design Proving a property of an FSA

A first attempt

Off On Grill

l

r

l

r

But this oven starts in an undefined state...

Page 7: Finite State Automata Design - Politecnico di Milano · 2020-03-26 · Finite State Automata (Automi a stati niti AF) are the simplest among abstract computing models ... automaton,

FSA FSA Basic Design Proving a property of an FSA

A first attempt

Offstart On Grill

l

r

l

r

This one’s better, but we’d like to accept only when weremembered to turn it off at the end of the cooking...

Page 8: Finite State Automata Design - Politecnico di Milano · 2020-03-26 · Finite State Automata (Automi a stati niti AF) are the simplest among abstract computing models ... automaton,

FSA FSA Basic Design Proving a property of an FSA

A first attempt

Offstart On Grill

l

r

l

r

Ok, but what if the knob is turned further left (resp. right) whenit’s already on the “Grill” (resp.“Off”) position?

Page 9: Finite State Automata Design - Politecnico di Milano · 2020-03-26 · Finite State Automata (Automi a stati niti AF) are the simplest among abstract computing models ... automaton,

FSA FSA Basic Design Proving a property of an FSA

A first attempt

Offstart On Grill

l

r

r

l

l

r

Great. What sequences of actions (language strings) does thisrecognize? L = (r | l(l(l)∗r)∗r)∗ = (r | l(l+r)∗r)∗

Page 10: Finite State Automata Design - Politecnico di Milano · 2020-03-26 · Finite State Automata (Automi a stati niti AF) are the simplest among abstract computing models ... automaton,

FSA FSA Basic Design Proving a property of an FSA

A first recognizer

Let’s try the other way around: design a FSA recognizing aspecific language

Example: L = (01 | 1)∗, that is the language where every zerois followed by at least a one (i.e. there cannot be twoconsecutive zeros)

q0start q1

0

1

1

δ(q1, 0) is undefined here!

Page 11: Finite State Automata Design - Politecnico di Milano · 2020-03-26 · Finite State Automata (Automi a stati niti AF) are the simplest among abstract computing models ... automaton,

FSA FSA Basic Design Proving a property of an FSA

The error state

The previous FSA does not explicitly represent error states

As this may result in issues when performing operation amongautomata, we’ll add it

q0start q1 error

0

1

1

0

0 | 1

The δ(·, ·) function is now complete (safer to performcomplement).

Page 12: Finite State Automata Design - Politecnico di Milano · 2020-03-26 · Finite State Automata (Automi a stati niti AF) are the simplest among abstract computing models ... automaton,

FSA FSA Basic Design Proving a property of an FSA

Complement

How-To

Given a recognizer FSA for the language L, the onerecognizing the complement of the language LC can beobtained flipping the termination condition

Basically: all the accepting states become non accepting andvice-versa

Take care: the error state is nothing more than a commonnon accepting state and must be included in the process.

A safe way to take it into account is to complete theautomaton before building the complementary one

Page 13: Finite State Automata Design - Politecnico di Milano · 2020-03-26 · Finite State Automata (Automi a stati niti AF) are the simplest among abstract computing models ... automaton,

FSA FSA Basic Design Proving a property of an FSA

Complement

Let’s take as an example the recognizer for L = 0(0|1)∗

The recognizer looks like this :

Partial δ(·, ·)

q0start

q10

0

1

Complete δ(·, ·)

q0start

q1

error

0

1

0 | 1

0 | 1

Page 14: Finite State Automata Design - Politecnico di Milano · 2020-03-26 · Finite State Automata (Automi a stati niti AF) are the simplest among abstract computing models ... automaton,

FSA FSA Basic Design Proving a property of an FSA

Complement

The recogniser for LC is thus :

Recogniser for (0|1)∗ \ L

q0start q1 error

0

1

0 | 1 0 | 1

Page 15: Finite State Automata Design - Politecnico di Milano · 2020-03-26 · Finite State Automata (Automi a stati niti AF) are the simplest among abstract computing models ... automaton,

FSA FSA Basic Design Proving a property of an FSA

Intersection

The recogniser automaton for the intersection of twolanguages L ∩M can be built starting from the onesrecognising them

The steps to be followed are :1 Build the state set as the Cartesian product of the state sets

(hint: label the states with the combination of the two originallabels, that is 〈li ,mi 〉)

2 Set the initial state to the one obtained via the Cartesianproduct of l0 and m0

3 Start deriving the δ function from the initial state: simplycheck the original deltas and choose the destination stateobtained as the product of the destinations. If at least one ofthe two original deltas is undefined, than δ is undefined too.

4 Mark as final only the states obtained as the product of twofinal states: 〈li ,mi 〉 ∈ F if and only if li ∈ Fl ∧mi ∈ Fm

Page 16: Finite State Automata Design - Politecnico di Milano · 2020-03-26 · Finite State Automata (Automi a stati niti AF) are the simplest among abstract computing models ... automaton,

FSA FSA Basic Design Proving a property of an FSA

Example intersection

We want to obtain a recogniser for L ∩M

Recogniser for L

l0start

l10

0 | 1

Recogniser for M

m0start

m1

1

0 | 1

Page 17: Finite State Automata Design - Politecnico di Milano · 2020-03-26 · Finite State Automata (Automi a stati niti AF) are the simplest among abstract computing models ... automaton,

FSA FSA Basic Design Proving a property of an FSA

Intersection Automaton

Automaton for L ∩M = ∅ including unreachable states)

l0,m0start l0,m1

l1,m0 l1,m1

0

10 | 1

Page 18: Finite State Automata Design - Politecnico di Milano · 2020-03-26 · Finite State Automata (Automi a stati niti AF) are the simplest among abstract computing models ... automaton,

FSA FSA Basic Design Proving a property of an FSA

Union

Given the automata Al = 〈Ql, I, δl , l0,Fl〉 andAm = 〈Qm, I, δm,m0,Fm〉 for, respectively, languages L and M, therecogniser automaton A = 〈Q, I, δ, q0,F〉 for L ∪M:

Q = Ql ×Qm ∪Ql ∪Qm, that is the Cartesian product of thestate sets is added to the state sets of the original automata

∀li ∈ Ql,mj ∈ Qm, a ∈ I, δ(〈li ,mj〉, a) is equal to:

〈δl(li , a), δm(mj , a)〉 if δl(li , a) 6= ⊥ ∧ δm(mj , a) 6= ⊥δl(li , a) if δl(li , a) 6= ⊥ ∧ δm(mj , a) = ⊥δm(mj , a) if δl(li , a) = ⊥ ∧ δm(mj , a) 6= ⊥⊥ if δl(li , a) = ⊥ ∧ δm(mj , a) = ⊥

q0 = 〈l0,m0〉F = Fl ×Qm ∪ Fm ×Ql ∪ Fl ∪ Fm, that is a state is marked asfinal if it is the product of at least one final state of theoriginal automata

Page 19: Finite State Automata Design - Politecnico di Milano · 2020-03-26 · Finite State Automata (Automi a stati niti AF) are the simplest among abstract computing models ... automaton,

FSA FSA Basic Design Proving a property of an FSA

Union Automaton

Automaton for L ∪M = {0, 1}+ (including unreachable states)

l0,m0

start

l1

m1 l0,m1

l1,m0

l1,m1

l0

m0

0

1 0 | 1

0 | 1

1

0

0

1 0 | 1

0

1

Page 20: Finite State Automata Design - Politecnico di Milano · 2020-03-26 · Finite State Automata (Automi a stati niti AF) are the simplest among abstract computing models ... automaton,

FSA FSA Basic Design Proving a property of an FSA

Union: Alternative Strategy

It is possible to use complement and intersection to computethe recognizer automaton for L ∪M

The key idea is to exploit De Morgan’s Law applied to setoperations: ¬(a ∪ b) = (¬a) ∩ (¬b)

It is thus possible to derivea ∪ b = ¬(¬(a ∪ b)) = ¬((¬a) ∩ (¬b))

Therefore, by applying in sequence two complements, anintersection and another complement we obtain the unionautomaton

Page 21: Finite State Automata Design - Politecnico di Milano · 2020-03-26 · Finite State Automata (Automi a stati niti AF) are the simplest among abstract computing models ... automaton,

FSA FSA Basic Design Proving a property of an FSA

Vasco Rossi’s lyrics

Vasco Rossi’s lyrics clearly belong to the language L = (e+h|la)+.Therefore, we can recognize Vasco’s lyric with a FSA:

FSA Recognizer

q0start q1

q3 q4 q2

e

l

h

e

e

l

al

e

Page 22: Finite State Automata Design - Politecnico di Milano · 2020-03-26 · Finite State Automata (Automi a stati niti AF) are the simplest among abstract computing models ... automaton,

FSA FSA Basic Design Proving a property of an FSA

Proving Properties on an FSA

The following automaton recognizes L = (0|1)∗012k+1.

q0start

q1

q2

0

1

0

1

0,1

Even if it is possible to intuitively check that this is right, weneed to provide a formal proof

The most straightforward way to prove it is by induction onthe steps performed

Page 23: Finite State Automata Design - Politecnico di Milano · 2020-03-26 · Finite State Automata (Automi a stati niti AF) are the simplest among abstract computing models ... automaton,

FSA FSA Basic Design Proving a property of an FSA

Mathematical Induction

The idea is to prove by induction (on the steps performed bythe automaton):

That all the strings in the language lead the computation in anaccepting stateThat all the strings not in the language lead the computationto a non accepting state

To this end, we partition the set of possible strings I ∗ in asmany sets as the number of states of the completeautomaton, associating each set to a state of the automaton:

q0 Strings without a 0, i.e. L1 = 1∗ (/∈ L)q1 Strings with at least a 0 and ending with an even number of 1,

i.e. L2 = {(0|1)∗012k , k ≥ 0} (/∈ L)q2 Strings with at least a 0 and ending with an odd number of 1,

i.e. L3 = {(0|1)∗012k+1, k ≥ 0} (∈ L)

Page 24: Finite State Automata Design - Politecnico di Milano · 2020-03-26 · Finite State Automata (Automi a stati niti AF) are the simplest among abstract computing models ... automaton,

FSA FSA Basic Design Proving a property of an FSA

Mathematical Induction

Building hypotheses and checking base cases

Our purpose is thus to prove that:1 ∀s ∈ L1, δ∗(q0, s) = q02 ∀s ∈ L2, δ∗(q0, s) = q13 ∀s ∈ L3, δ∗(q0, s) = q2

These three propositions will work for us as inductionhypotheses

Since we are performing the induction on the steps of theautomaton, the base cases are represented by the shorteststrings from each class

Let’s start from checking the base cases:1 δ(q0, ε) = q0 and δ(q0, 1) = q0 ? Yes.2 δ(q0, 011) = q1 and δ(q0, 0) = q1 ? Yes.3 δ(q0, 01) = q2 ? Yes.

Page 25: Finite State Automata Design - Politecnico di Milano · 2020-03-26 · Finite State Automata (Automi a stati niti AF) are the simplest among abstract computing models ... automaton,

FSA FSA Basic Design Proving a property of an FSA

Mathematical Induction

Proofs

1 ∀s ∈ L1 : δ∗(q0, s = s ′.i) = δ(δ∗(q0, s′), i) = δ(δ∗(q0, s

′), 1) =δ(q0, 1), since s ′ ∈ L1. Then, δ(q0, 1) = q0, thus ∀s ∈ L1,δ∗(q0, s) = q0 is true

2 ∀s ∈ L2 : δ∗(q0, s = s ′.i) = δ(δ∗(q0, s′), i). Two cases:

i = 0 ∨ i = 11 i = 0 . δ(qi , 0) = q1∀qi ∈ Q2 i = 1. δ(δ∗(q0, s

′), 1) = δ(q2, 1), since s ′ ∈ L3.Then,δ(q2, 1) = q1

Thus, ∀s ∈ L2, δ∗(q0, s) = q1 is true

3 ∀s ∈ L3:δ∗(q0, s = s ′.i) = δ(δ∗(q0, s

′), i) = δ(δ∗(q0, s′), 1) = δ(q1, 1),

since s ′ ∈ L2. Then, δ(q1, 1) = q2, thus ∀s ∈ L3,δ∗(q0, s) = q2 is true

Page 26: Finite State Automata Design - Politecnico di Milano · 2020-03-26 · Finite State Automata (Automi a stati niti AF) are the simplest among abstract computing models ... automaton,

FSA FSA Basic Design Proving a property of an FSA

Translators

It is possible to enrich the computation model of a recogniserFSA with the ability to translate languages

The FSA is only able to translate languages where the sourceis recognisable

A translation move is characterised by reading at most onecharacter and outputting zero or more characters

A typical example is the find-replace function of a commoneditor

Page 27: Finite State Automata Design - Politecnico di Milano · 2020-03-26 · Finite State Automata (Automi a stati niti AF) are the simplest among abstract computing models ... automaton,

FSA FSA Basic Design Proving a property of an FSA

Formalization

A transducer FSA is formally defined as a 7-tuple(Q, I, δ, q0,F,O, η), where:

Q, is the set of states of the automataI is the alphabet of the input string which will be checkedδ : Q× I 7→ Q the transition functionq0 ∈ Q the (unique) initial state from where the automatonstartsF ⊆ Q the set of final accepting states of the automatonO the output alphabet (may coincide with I)η : Q× I 7→ O∗ the transduction function

Page 28: Finite State Automata Design - Politecnico di Milano · 2020-03-26 · Finite State Automata (Automi a stati niti AF) are the simplest among abstract computing models ... automaton,

FSA FSA Basic Design Proving a property of an FSA

Example

We want to build a transducer automaton accepting the inputlanguage L = (ab)+c(ba)∗

The target language is Lt = (dd)+ef ∗

The translation map τ is defined asτ((ab)nc(ba)m) = d2nef (m÷2), n ≥ 1,m ≥ 0

Since only a finite memory is required to perform the ÷2operation, the language can be translated by a FSA.

Page 29: Finite State Automata Design - Politecnico di Milano · 2020-03-26 · Finite State Automata (Automi a stati niti AF) are the simplest among abstract computing models ... automaton,

FSA FSA Basic Design Proving a property of an FSA

Transducer FSA

L to Lt transducer

q0start q1 q2

q5 q4 q3q6

a/d b/d

a/dc/e

b/εa/εb/ε

a/f

Page 30: Finite State Automata Design - Politecnico di Milano · 2020-03-26 · Finite State Automata (Automi a stati niti AF) are the simplest among abstract computing models ... automaton,

FSA FSA Basic Design Proving a property of an FSA

Data Compression

We want to compress words from the languageL = {an1bk1 . . . anibki . . . anNbkN c | N ≥ 1 ∧ ∀i ≤ N(1 ≤ ni ≤4 ∧ 1 ≤ ki ≤ 4)} into a more compact, equivalently expressiveformat Lt = n1k1 . . . niki . . . nNkN | N ≥ 1.

L to Lt transducer

q0start

q1a

q2a q3a q4a

q1b q2b q3b q4b

qf

a/ε

b/1

a/ε

a/ε

b/2

a/ε

b/3b/4

b/ε

a/1

c/1

b/ε

a/2

c/2

b/ε

a/3

c/3c/4

a/4