Finite State Machines – An Informal Introduction

Finite State Machines – An
Informal Introduction
Joseph Spring
School of Computer Science
QC - Foundations of Computation
Discussion Points
Abstract Machines
1. Finite State Machines – Informal
1.
2.
3.
4.
5.
6.
Components of an FSM
State Transition Diagrams
A Simple FSM
FSM’s and Loops
FSM’s and Multiple Transitions
FSM’s and Traps / Multiple Loops
Abstract Machines
• We are interested in the interaction between languages
and machines
• The machines we work with are abstract machines they
exist as abstract models of real world machines.
– So we are involved in modelling and the modelling process
• The machines we explore (finite state machines) are
sometimes referred to as finite state automata
– Abstract models of a digital computer
• We can develop our family of abstract machines from
these very simple FSMs to Turing Machines.
• This development illustrates many of the features of
computer architecture (control units, data
representation, memory structures etc).
• You may consider the classes of languages that fit with
each machine type.
Languages
We need a working definition of a language:
– A formal language is a set of strings of
symbols drawn from some alphabet
– Thus given an alphabet {x, y, z} here is a
language
{xy, xxz, z, yyxzy}
– Here is another language with the same
alphabet:
{x, yyy, zxy, yyxzy, zzzzzzzzzzz}
1. Components of an FSM
• So what is a machine?
– We consider Finite State Machines (FSM)
• FSM’s are machines that
– May be in one of a finite number of states
– Move between states in response to inputs
• Our machines are abstract machines
– We use symbols to represent our inputs or
strings of symbols for more than one input
– The set of all possible symbols is referred to
as the alphabet associated with the machine
1. Components of an FSM
• When a machine receives an input
– It may move to a new state
• If it moves to a new state then this movement is
referred to as a transition
• States
– One state is referred to as the initial state
1
– One or more states may be defined to be
final accepting states
5
5
or
1. Components of an FSM
• A sequence of symbols is recognised as a
string in a formal language if it results in the
FSM moving from the initial state to a final
accepting state
• Strings
– A string abc is by convention applied to an FSM from
left to right
– So first we apply symbol a then symbol b and finally
symbol c
• Any deviation from this convention will be
clearly indicated
2. State Transition Diagrams (STD)
• STD’s are used to represent the set of all
possible behaviours of an FSM
• In STD’s states are
– (as mentioned above) denoted by circles
squares (or a double circle)
– labelled by names, numbers or letters
• In STD’s transitions between states are
– represented by arrows with input symbols
attached to them
2. FSM – State Transition Diagram
2
c
a
c
3
1
b
5
c
4
Machine 1: A Simple FSM
3. A Simple FSM
Final Accepting
State
2
c
a
c
3
1
b
5
c
4
Machine 1: A Simple FSM
This arrow (coming from no other state)
indicates that state 1 is the initial state
3. A Simple FSM
• Initial state
– State 1 (Indicated by ‘starting arrow’)
• Final Accepting State
– State 5 (Shown as a square)
• Strings accepted by the Simple FSM
– acc and bcc
– All other strings are rejected by this FSM
– With rejected strings we say that the machine halted
– We say that the simple FSM accepts the finite formal language
L1={acc, bcc}
3. A Simple FSM
This illustrates a very important point:
• There is a special relationship between
any FSM and the largest language that it
recognises.
– Any given FSM will recognise a particular
language (and any languages which are
subsets of this of course).
• If two machines recognise the same
language, they are equivalent.
– They have the same behaviour (even though
they may look different).
4. FSM and Loops
a
It is possible for states to have
transitions back to themselves
2
c
a
c
3
1
b
4
b
5
c
Machine 2: A Simple
FSM with Loops
4. FSM and Loops
• The previous STD allows for the acceptance of
– ‘a’ when in State 2 and still stay in State 2
– ‘b’ when in State 4 and still stay in State 4
• Loops open up the possibility of accepting an infinite
language
L2 = {acc, aacc, aaacc, aaaacc, …, bcc, bbcc, bbbcc, bbbbcc…}
• We Note:
– This machine is a FSM since there are only 5 states
– It has the potential for infinite behaviour due to the loops in its
structure. It can therefore be associated with an infinite language
4. FSM and Loops
• Loops can involve several states
L3 = {abc, abcabc, abcabcabc, …}
2
b
a
1
a
3
c
4
Machine 3: An FSM with Larger Loops
4. FSM and Loops
• To include zero repetitions of the
language
L3a = {Λ, abc, abcabc, abcabcabc, …}
2
b
a
3
1
c
Machine 3a: An FSM that accepts Λ
5. FSM’s and Multiple Transitions
• Multiple transitions between states can
also occur. For example:
a
c
1
2
3
c
4
b
This accepts the language L4 = {acc, bcc}
Machine 4: A FSM with Multiple Transitions between States
5. FSM’s and Multiple Transitions
• The previous STD could get very cluttered with
multiple arrows as the number of multiple
transitions increases. Hence we adopt the
following shorthand
a,b
1
c
2
3
c
4
Machine 4: A FSM with Multiple Transitions between States
This accepts the language L4 = {acc, bcc}
6. FSM’s and Traps
• One variation on the above STD’s that one
might meet is referred to as a Complete STD’s
• These include transitions for all input symbols
– Note: we distinguish between those strings which are
acceptable and those which are not by including a
trap state from which one can never escape.
a,b
c
c
3
1
2
4
a,b
c
a,b a,b
trap
Machine 4b: FSM with a Trap State
6. FSM’s and Multiple Loops
• Another variation on the above STD’s involves
multiple labels on arrows and loops
a,b
a,b
1
c
2
3
c
4
What language does this FSM accept?
Machine 5: FSM with Multiple Labels on Arrows and Loops
6. FSM’s and Multiple Loops
• Another variation on the above STD’s involves
multiple labels on arrows and loops
a,b
a,b
1
c
2
3
c
4
L5 = {acc, bcc, abcc, bacc, abbcc, abcc, …}
Machine 5: FSM with Multiple Labels on Arrows and Loops
Question
a
Does this machine have the same
language as the previous one?
2
c
a
c
3
1
b
4
b
c
5
• We can think of the machine as
recognising a string from the language OR
we can think of the strings a programs
that control the machine.
• If we assume that we can see what states
we are in the machines respond to the
program by executing the strings
(provided we give valid strings!!).
Summary
Abstract Machines
1. Finite State Machines – Informal
1.
2.
3.
4.
5.
6.
Components of an FSM
State Transition Diagrams
A Simple FSM
FSM’s and Loops
FSM’s and Multiple Transitions
FSM’s and Traps / Multiple Loops