You are on page 1of 77

Unit-4:

Finite State Machines

MTH-352
Discrete Mathematics
FSM

• finite-state machines with outputs and no


outputs,
• deterministic and nondeterministic finite-state
automaton,
• turing machines,
• definition, examples, and computations
Information-Processing Machine

• An Information-Processing machine is a
device/system that
– Accepts input
– Produce output

Information
INPUT Processing OUTPUT

Machine
Examples:

Bulb can be considered as an information-processing


machine with input signals as on and off position of
switch and the output being either Light or Dark .

An adder is an information-processing machine with


input of two decimal numbers and the output is Sum
of the entered numbers.

• 9 7 16
Examples:

A vending machine is an information-processing


machine with input signals as coins deposited and
selection of product; the output is the product and the
change.
Examples:

A Computer is an information-processing machine with


user’s program and data as input signals and the
desired result as output.
Difference in the examples
In bulb and Adder the output signal at any instant
depends only on the input signal at that instant, and
does not depend on the input signals before that
instant.
i.e. whenever the input signal is UP the output signal is LIGHT,
and whenever the input signal is DOWN the output signal is
DARK.
Similarly, in the case of the adder, the sum of the two input
numbers at that instant, and is completely independent of the
numbers that were added together earlier.
Difference in the examples

While for Vending machine the output signal at


any instant depends not only on the input
signal at that instant but also on the preceding
input signals.
Let the input signal for a vending machine is 5, 10 and 15
rupee and the output is either a coke or nothing.
Suppose coke costs 30 rupees.
Then, corresponding to the sequence of input signals 10, 10,
10 there is output Nothing, Nothing, coke
For Input 5, 10, 15 O/P will be Nothing , Nothing, coke
So what is the difference?

• Difference is of the memory!


Types of Machines
• Machines may be with memory and without
memory.
• machine without memory: its output at any
instance depends only on the input at that
instant.
• machine with memory (it can remember what
has happened in the past): its output at any
instant depends on the input at that instant as
well as on inputs at previous instant.
Machine states

• Machine state (S)


– The complete internal condition of the machine.
– A state represents a summary of the past history
of the machine.

• The current state and the next input determines the


next state to be occupied.
Simple Example:
Machine without memory

t=1 t=0 t=1


t=0 t=0
A B A A B
B B C
C C A
C
t=1
t=1

t=0
Finite State Machine (FSM)

• A finite-state machine (FSM) is an abstract model


of a machine which
• accept discrete inputs,
• produces discrete output
• and has the internal memory to keep track of
certain information about previous inputs.
Finite State Machine (FSM)

• At any instant of time the machine can be in any


of the finite number of states.
• The next state of the machine any point in time is
determined by the present state and subsequent
input and produces an output.
Finite-State Machine (components)

• A finite-state machine is defined by M = (S, I, O, f,


g, s0)
– A finite set of states S,
– A finite set of inputs I,
– a finite output alphabet O (If available)
– Transition function (f): A function defining the
transitions from one state to another, for a specified
input, and
– an output function g that assigns to each state and input
pair an output
– An initial state s0,
Components of a finite state machine

• State Tables
- Table giving information of transitions of
states
• State diagrams
– Are labeled digraphs
Diagraph Notation

State: States are represented by circles, The state name appears


within the circle

Initial state

q
Transition (on input q) :The transitions are represented
by arcs. The arcs are labeled with the input that
initiates the transition.
Types of FSM
Finite State Machine with Output

• Finite state machine with output generates


an output on every input.
• The value of the output is a function of
current state and the current input.
• These machines do not have final
state/states.
Types of Finite-State Machines with output

• Mealy machines: outputs correspond to


transitions between states.
• Moore machine: output is determined
only by the state.
Mealy machine Example:

Here outputs correspond to transitions


between states.
Moore machine Example:

Here Output is determined only by the state.


Example:1

• Consider the following FSM with output shown


with help of transition table and state diagram
• Draw the state diagrams for the finite-state
Example:2
machines with the given state table
Example:3

• Find the output generated from the input string


01110 for the finite-state machine with the state
table.
Example:4

• Draw the state diagrams for the finite-state


machines with the given state table
Example:5

• Find the output generated from the input string


01110 for the finite-state machine with the state
table
Example:6

• Construct the state diagram for the finite-


state machine with the state table
Sol.

• The state diagram for the finite-state


machine with the state table
Example:7

• Construct the state table for the finite-state


machine with the state diagram
Sol.

• The state table for the finite-state machine with


the state diagram
Example:8

• Find the output generated from the input string


01110 for the finite-state machine with the state
table.

• Sol. a) 01010 b) 01000


Finite state Automata

• Finite-state machine with no output are


called as finite-state automata.
• Instead of producing output, these
machines have final states.
Finite state Automata

A finite-state automaton M = (S, I, f, s0, F) consists of :


•a finite set S of states,
•a finite input alphabet I, a transition function f that
assigns a next state to every pair of state and input
•an initial or start state s0,
•and a subset F of S consisting of final (or
accepting states).
Final (or acceptance) state are indicated by two concentric
circles
Why Finite state Automata?

• One of the most important applications of finite-


state machines is in language recognition.
• This application plays a fundamental role in the
design and construction of compilers for
programming languages.
• Voice search feature in Apps, Google!
Example

• Construct the state diagram for the finite-state


automaton M = (S, I, f, s0, F), where S ={s0, s1, s2,
s3}, I = {0, 1}, F = {s0, s3}, and the transition
function f given in Table below
Example

• Construct the state diagram for the finite-state


automaton M = (S, I, f, s0, F), where S ={s0, s1, s2,
s3}, I = {0, 1}, F = {s0, s3}, and the transition
function f given in Table below
Finite Automaton types

• Finite Automaton can be classified into two


types −
• Deterministic Finite Automaton (DFA)
• Non-deterministic Finite Automaton
(NDFA / NFA)
Deterministic Finite Automaton (DFA)

• The finite-state automata discussed so far are


deterministic, because for each pair of state and
input value there is a unique next state given by
the transition function.
Deterministic Finite Automaton (DFA)

• In DFA, for each input symbol, one can determine


the state to which the machine will move.
• Hence, it is called Deterministic Automaton.
• As it has a finite number of states, the machine is
called Deterministic Finite
Machine or Deterministic Finite Automaton.
Example DFA

c
Non-Deterministic Finite Automaton (NDFA)

• In NDFA, for a particular input symbol, the machine can


move to any combination of the states in the machine.
• In other words, the exact state to which the machine moves
cannot be determined. Hence, it is called Non-
deterministic Automaton.
• As it has finite number of states, the machine is
called Non-deterministic Finite Machine or Non-
deterministic Finite Automaton.
Example NDFA

c
Example

• Find the state diagram for the


nondeterministic finite-state automaton with
the state table shown below with the final
states as s2 and s3.

c
Example

• Find the state table for the nondeterministic


finite-state automaton with the state
diagram as follows:

c
FINITE STATE MACHINES AS MODELS OF
PHYSICAL SYSTEMS

• What type of machines are Microwave,


phone, tablet, washing machine, ticket
machine, ATM ?
• All of these systems are reactive: their
actions depend on the inputs they receive.
• Their response to a particular stimulus (a
signal, or a piece of input) is not the same
on every occasion.
Example:
Parking ticket machine
• It will not print a ticket when you press the
button unless you have already inserted
some money.
• The response to the print button depends on
the previous history of the use of the
system: its memory.
Parking ticket machine

• The machine's memory is represented by a


set of states.
• For example, 1=awaiting coins, 2=ready to
print
• The set of possible states for a given
machine is written S = {1,2}
• output alphabet: = { print ticket, return
coins }
Example:
Designing a modulo 3 operator
• It receives a sequence of 0s, 1s, and 2s as
input and produces output equal to the
modulo 3 sum of the digits in the input
sequence.
• Let A, B and C are state corresponding that
the modulo 3 sum of all input digits is 0, 1
or 2.
• Draw state table and state diagram
OUTPUT
A 0
B 1
C 2
Example: BINARY ADDITION

• Describe a finite state machine M which can perform


binary addition.
• Suppose that the machine is given the input
1101011
+0111011,
then we want to have the output to be the binary sum
10100110
• Thus the input is the string of pairs of digits to be added:
• 11, 11, 00, 11, 01, 11, 10
the output should be the string 0, 1, 1, 0, 0, 1, 0, 1
Example: BINARY ADDITION

The input symbols are I = {00, 11, 10, 01}


Since,
0+0=0
1+1=0( 1 carry)
0+1=1+0=1
1+1+1=1 (1 carry)
and the output are O = {0, 1, 0c, 1c}
Since there are four output the machine can have four states:
S = {s0, s1, s2, s3}
Example: BINARY ADDITION

• Now we have define the I, O, S


• Lets us define the output function g for the different
outputs.
• Let,
• g(s0)=0
• g(s1)=1
• g(s2)=0c
• g(s3)=1c
• The next required is the transition function to define the
function.
Example: BINARY ADDITION

• By considering all possibilities, we arrive at the


• transition table given below:

State 00 10 01 11 output

S0 S0 S1 S1 S2 0

S1 S0 S1 S1 S2 1

S2 S1 S2 S2 S3
0c
S3 S1 S2 S2 S3 1c
Example: BINARY ADDITION

• Construct state table for the output function g for the


different outputs.
• Let,
• g(s0)=0
• g(s1)=0c
• g(s2)=1
• g(s3)=1c
• The next required is the transition function to define the
function.
Example: BINARY ADDITION

• By considering all possibilities, we arrive at the


• transition table given below:

State 00 10 01 11 output

S0 S0 S2 S2 S1 0

S1 S2 S1 S1 S3 0c
S2 S0 S2 S2 S1 1

S3 S2 S1 S1 S3 1c
Example: BINARY ADDITION

• Draw the corresponding state diagram :


TURING MACHINES
Turing Machines Explained

• A Turing machine consists of an infinitely long


tape, which has been divided up into cells.
• Each cell can contain either a 1, a 0, or an empty
space.
• Above one cell of the tape is a head, which can
either move left or right, and can read the symbols
written in the cells.
• The head is also capable of erasing symbols and
writing new symbols into the cells.
Turing Machines Explained
Turing Machines Explained

• How machine works?


• There is a set of instructions provided to the
machine that provide directions that the
head will move in which direction, which
values it erases, and which values it enters.
Turing Machines Explained
Turing Machines Explained

• In the shown machine, the head is in state 2,


and the instructions says
• If it reads a 0, then erase it and write a 1,
move one cell to the right and change your
state to 5.
• (There’ll also be instructions in the case it
reads a 1.)
Turing Machines Explained

• State 5 might say, if you’re reading a 1,


erase it, write a 0, and move one cell to the
left. Then go to state 7.
Turing Machines Explained
Constructing a Turing Machines

• Lets have a tape, a head, some initial instructions as


follows, and place some 1s and 0s on the tape to begin
with.
Constructing a Turing Machines
Constructing a Turing Machines

• The first symbol seen by the head is a 0, and the instructions say
that when reading a 0, it should write a 1 and move left.
• Moving left, it then reads a 1, which according to the instructions
means it should write a 0 and move left.
• It then reads another 1, writes a 0 and moves left, and finally reads
a blank at which point the program halts.
• So this Turing machine is designed to flip bits. It changes 0s to 1s
and 1s to 0s.
• The instructions for this Turing machine only had one state, but
more complex Turing machines can be built using multiple states.


Example: A Turing Machines

Explain the Turing machine with three states and explain the
instructions of working
Definition of Turing Machines

• A Turing machine T = (S, I, f, s0) consists


of a finite set S of states, an alphabet I
containing the blank symbol B,
• a partial function f from
S × I to S × I × {R, L},
(state, symbol) to (state, symbol, direction)
• and a starting state s0.
• The function is partial as for some (state,
symbol) pairs the function f may be
undefined, but for a pair for which it is
defined, there is a unique (state, symbol,
direction) triple associated to this pair.
Working of Turing Machines
• At each step, the control unit reads the current tape symbol x. If the
control unit is in state s and if the partial function f is defined for
the pair (s, x) with f (s, x) = (s′, x′, d), the control unit
• 1. enters the state s′,
• 2. writes the symbol x′ in the current cell, erasing x, and
• 3. moves right one cell if d = R or moves left one cell if d = L.
• Thus Turing machine can be written as a five-tuple (s, x, s′, x′, d).
• If the partial function f is undefined for the pair (s, x), then the
Turing machine T will halt.
Working of Turing Machines
• A common way to define a Turing machine is to specify a set
of five-tuples of the form (s, x, s′, x′, d).
• At the beginning of its operation a Turing machine is
assumed to be in the initial state s0 and to be positioned over
the leftmost nonblank symbol on the tape.
• If the tape is all blank, the control head can be positioned
over any cell.
Example

• What is the final tape when the Turing machine T defined


by the seven five-tuples (s0, 0, s0, 0, R), (s0, 1, s1, 1, R),
(s0, B, s3, B, R), (s1, 0, s0, 0, R), (s1, 1, s2, 0, L)
• (s1, B, s3, B, R), and (s2, 1, s3, 0, R)
• is run on the tape shown
Example
(s0, 0, s0, 0, R),

(s0, 1, s1, 1, R),

(s1, 0, s0, 0, R),

(s0, 1, s1, 1, R),

(s1, 1, s2, 0, L)

(s2, 1, s3, 0, R)

Not defined
.

You might also like