Previously...
Most of trees!
Snow day
Thanksgiving
TCSS 322 - Discrete Structures II!
http://moodle.insttech.washington.edu/!
Finite-State Machines
30 November 2010
1
Today
Quiz #7
Introduction to finite state machines
TCSS 322 - Discrete Structures II!
http://moodle.insttech.washington.edu/!
Finite-State Machines
30 November 2010
2
Finite-State Machines
Background
Many real-world machines, and many computational
tasks, can be modeled as a set of states, with rules for
switching between these states when given inputs and for
generating outputs.
Examples:
Vending machines
Elevators
Spell checkers
Speech recognizers
Network protocols
TCSS 322 - Discrete Structures II!
http://moodle.insttech.washington.edu/!
Finite-State Machines
30 November 2010
3
Example: A Vending Machine
Assume we want to model a soda machine with the
following functionality.
The machine only takes dimes.
A soda costs 30 cents.
Excess change is refunded immediately (if the machine already
has 30 cents in it and you put a dime in, you get the dime back).
The machine has no “coin return” button.
The soda choices are Pepsi and Coke; each has a button.
How can we do it?
TCSS 322 - Discrete Structures II!
http://moodle.insttech.washington.edu/!
Finite-State Machines
30 November 2010
4
Example: A Vending Machine
Consider it in terms of inputs and outputs.
When the machine has 3 dimes in it, and you press the Coke
button, the machine outputs a Coke. Same for Pepsi.
When the machine has 3 dimes in it, and you put in a dime, the
machine outputs a dime.
When the machine has less than 3 dimes in it, and you put in a
dime, the machine outputs nothing.
When the machine has less than 3 dimes in it, and you push the
Coke or Pepsi button, the machine outputs nothing.
Each of the “when” parts is a state. In each state, some
input results in some output. But this is still somewhat
incomplete...
TCSS 322 - Discrete Structures II!
http://moodle.insttech.washington.edu/!
Finite-State Machines
30 November 2010
5
Example: A Vending Machine
If the machine has a state for “has 3 dimes”, and a state
for “has less than 3 dimes”, how does it know when one
additional dime makes 3?
It needs states for “has no dimes”, “has 1 dime”, “has 2
dimes”...
TCSS 322 - Discrete Structures II!
http://moodle.insttech.washington.edu/!
Finite-State Machines
30 November 2010
6
Example: A Vending Machine
This graph - or state diagram - illustrates the states of the
vending machine, and the state transitions and outputs
that result from each input.
10
S5
10
S15
S25
Edges that have no output and cause no state change
have been omitted to eliminate clutter - for instance,10, 5
5
5 C button
5 in state 5S10 doesnʼt
pressing
the
do anything.
5
5
5, 5
S0
10
S10
10
S20
10
S30
10, 10
C, Coke
Start
P, Pepsi
TCSS 322 - Discrete Structures II!
http://moodle.insttech.washington.edu/!
Finite-State Machines
30 November 2010
7
Example: A Vending Machine
How would we change the machine to also accept
nickels?
S5
10
10
S15
S25
10, 5
5
5
5
5
5
5
5, 5
S0
10
S10
10
S20
10
S30
10, 10
C, Coke
Start
P, Pepsi
TCSS 322 - Discrete Structures II!
http://moodle.insttech.washington.edu/!
Finite-State Machines
30 November 2010
8
Example: A Vending Machine
Add more states, to represent combinations of nickels and
dimes in the machine, and more corresponding edges.
S5
10
10
S15
S25
10, 5
5
5
5
5
5
5
5, 5
S0
10
S10
10
S20
10
S30
10, 10
C, Coke
Start
P, Pepsi
TCSS 322 - Discrete Structures II!
http://moodle.insttech.washington.edu/!
Finite-State Machines
30 November 2010
9
Example: A Vending Machine
How would we add a coin return button?
S5
10
10
S15
S25
10, 5
5
5
5
5
5
5
5, 5
S0
10
S10
10
S20
10
S30
10, 10
C, Coke
Start
P, Pepsi
TCSS 322 - Discrete Structures II!
http://moodle.insttech.washington.edu/!
Finite-State Machines
30 November 2010
10
Example: A Vending Machine
How would we add a coin return button? Add an edge
from each state Sn to S0, with R as input and n as output.
S5
10
10
S15
S25
10, 5
5
5
5
5
5
5
5, 5
S0
10
S10
10
S20
10
S30
10, 10
C, Coke
Start
P, Pepsi
TCSS 322 - Discrete Structures II!
http://moodle.insttech.washington.edu/!
Finite-State Machines
30 November 2010
11
Finite-State Machines
Formally, a Mealy machine (the type of finite-state
machine weʼve been using) is M = (S, I, O, f, g, s0).
S is a finite set of states.
I is a finite input alphabet (the set of possible inputs).
O is a finite output alphabet.
f is a transition function from (state, input) pairs to states.
g is an output function from (state, input) pairs to outputs.
s0 is the initial state.
When we feed an input string to the machine, it goes
through a series of states determined by the transition
function, and generates output determined by the output
function.
TCSS 322 - Discrete Structures II!
http://moodle.insttech.washington.edu/!
Finite-State Machines
30 November 2010
12
Finite-State Machines
A finite-state machine can be represented by a state table
- hereʼs one for our first (dimes-only) soda machine:
f (transition function)
g (output function)
State
10
C
P
10
C
P
S0
S10
S0
S0
-
-
-
S10
S20
S10
S10
-
-
-
S20
S30
S30
S30
S20
S0
S20
S0
10
Coke
Pepsi
TCSS 322 - Discrete Structures II!
http://moodle.insttech.washington.edu/!
Finite-State Machines
30 November 2010
13
Finite-State Machines
The state table and graph representations of a finite-state
machine mean the same thing.
Usually, the graph representation is easier to understand,
10
S5
10
S15
S25
because you can see where/when the state
changes
occur.
10, 5
5
5
5
5
5
5
5, 5
S0
10
S10
10
S20
10
S30
10, 10
C, Coke
P, Pepsi
Start
TCSS 322 - Discrete Structures II!
http://moodle.insttech.washington.edu/!
Finite-State Machines
30 November 2010
14
Finite-State Machines
Detecting Suffixes
Construct a finite-state machine that takes a binary string
as input and outputs a 1 if the input string read so far ends
in at least 5 consecutive 1s (and a 0 otherwise).
What are the input and output alphabets?
What are the states?
What is the transition function?
What is the output function?
TCSS 322 - Discrete Structures II!
http://moodle.insttech.washington.edu/!
Finite-State Machines
30 November 2010
15
Finite-State Machines
Detecting Suffixes
Construct a finite-state machine that takes a binary string
as input and outputs a 1 if the input string read so far ends
in at least 5 consecutive 1s (and a 0 otherwise).
What are the input and output alphabets? {0, 1}
What are the states? {S0, S1, S2, S3, S4, S5}
What is the transition function?
The state number is the number of consecutive 1s weʼve read.
From S(0..4), we go to the next state if we read a 1, and to S0 if we
read a 0.
From S5, we stay put if we read a 1 and go to S0 if we read a 0.
What is the output function?
All transitions except S4 -> S5 and S5 -> S5 output 0.
S4 -> S5 and S5 -> S5 output 1.
TCSS 322 - Discrete Structures II!
http://moodle.insttech.washington.edu/!
Finite-State Machines
30 November 2010
16
Finite-State Machines
Detecting Suffixes
The state table for the machine that takes a binary string
as input and outputs a 1 if the input string read so far ends
in at least 5 consecutive 1s:
f (transition function)
State
g (output function)
S0
0
S0
1
S1
0
0
1
0
S1
S0
S2
0
0
S2
S3
S0
S0
S3
S4
0
0
0
0
S4
S5
S0
S0
S5
S5
0
0
1
1
TCSS 322 - Discrete Structures II!
http://moodle.insttech.washington.edu/!
Finite-State Machines
30 November 2010
17
Finite-State Machines
Detecting Suffixes
The state diagram for the machine that takes a binary
string as input and outputs a 1 if the input string read so
far ends in at least 5 consecutive 1s:
0, 0
0, 0
0, 0
1, 1
0, 0
S0
S1
1, 0
S2
1, 0
S3
1, 0
S4
1, 1
S5
1, 0
0, 0
0, 0
Start
TCSS 322 - Discrete Structures II!
http://moodle.insttech.washington.edu/!
Finite-State Machines
30 November 2010
18
Finite-State Machines
Bit String Manipulation
Construct a finite-state machine that flips every other bit,
starting with the second bit, of an input bit string and
leaves the other bits unchanged - for example, the output
for 01110111 would be 00100010.
Input and output alphabets are {0, 1} (binary strings).
How many states do we need?
TCSS 322 - Discrete Structures II!
http://moodle.insttech.washington.edu/!
Finite-State Machines
30 November 2010
19
Finite-State Machines
Bit String Manipulation
Construct a finite-state machine that flips every other bit,
starting with the second bit, of an input bit string and
leaves the other bits unchanged - for example, the output
for 01110111 would be 00100010.
Input and output alphabets are {0, 1} (binary strings).
How many states do we need?
We can get by with two - one that indicates weʼre on an “even”
bit, and one that indicates weʼre on an “odd” one.
TCSS 322 - Discrete Structures II!
http://moodle.insttech.washington.edu/!
Finite-State Machines
30 November 2010
20
Finite-State Machines
Bit String Manipulation
What does the state table for this machine look like?
What about the state diagram?
TCSS 322 - Discrete Structures II!
http://moodle.insttech.washington.edu/!
Finite-State Machines
30 November 2010
21
Finite-State Machines
Bit String Manipulation
What does the state table for this machine look like?
f (transition function)
State
g (output function)
S0
0
S1
1
S1
0
0
1
1
S1
S0
S0
1
0
What about the state diagram?
TCSS 322 - Discrete Structures II!
http://moodle.insttech.washington.edu/!
Finite-State Machines
30 November 2010
22
Finite-State Machines
Bit String Manipulation
What does the state table for this machine look like?
f (transition function)
State
g (output function)
S0
0
S1
1
S1
0
0
1
1
S1
S0
S0
1
0
What about the state diagram?
0, 0
S0
Start
TCSS 322 - Discrete Structures II!
http://moodle.insttech.washington.edu/!
1, 1
0, 1
S1
1, 0
Finite-State Machines
30 November 2010
23
Finite-State Machines
Telephone Switching
Construct a finite-state machine for a restricted telephone
switching system, with the following characteristics:
The inputs are the digits 0-9.
Calls to 0 and 911 are allowed.
Calls to numbers of the form “1PQX” are allowed, where P is a
digit greater than 1, Q is a 0 or 1, and X is an arbitrary digit.
All other numbers are blocked, and cause an error message (“Iʼm
sorry, your call could not be completed as dialed”) to be
generated.
Digits dialed after a legitimate call are ignored.
An initially-dialed digit that is not 0, 1, or 9 is ignored.
TCSS 322 - Discrete Structures II!
http://moodle.insttech.washington.edu/!
Finite-State Machines
30 November 2010
24
Finite-State Machines
Telephone Switching
0, Operator
(not 1), Error
9
S0
S9
1
(not 1), Error
S91
1
1, 911
SD
(0 or 1), Error
Start
S1
(not 0 or 1)
(not 0 or 1), Error
S2
(0 or 1)
any, Call
S3
TCSS 322 - Discrete Structures II!
http://moodle.insttech.washington.edu/!
Finite-State Machines
30 November 2010
25
Finite-State Machines
We can model all sorts of things with finite-state machines.
Most often, though, we will use them for language
recognition - taking an input string comprised of
characters in some alphabet and determining whether it is
a part of some language (this will be defined later).
Finite-state machines used for language recognition donʼt
generate output - instead, they finish their execution either
in a final state or a non-final state.
If the machine finishes in a final state, the string is in the
language; otherwise, it is not.
TCSS 322 - Discrete Structures II!
http://moodle.insttech.washington.edu/!
Finite-State Machines
30 November 2010
26
Vocabularies and Languages
Before getting into language recognition, some definitions.
A vocabulary V is a finite, nonempty set of elements called
symbols (such as {0, 1}, or {a, b, c, d, e}).
A word over V is a string of finite length of elements of V.
The empty string, or null string, is the string containing no
symbols, and is denoted by λ.
The set of all words over V is denoted by V*.
A language over V is a subset of V*.
Example: if V is the set {0, 1}, then the set of binary strings that
end in 0 - {0, 00, 10, 000, 010, 100, 110, ... } - is a language
over V.
TCSS 322 - Discrete Structures II!
http://moodle.insttech.washington.edu/!
Finite-State Machines
30 November 2010
27
Sets of Strings
We often need to deal with sets of strings when we work
with languages and language recognition - this requires
new terminology.
If A and B are subsets of V* (for some vocabulary V), then
the concatenation of A and B, written AB, is the set of all
strings of the form xy, where x∈A and y∈B.
Example: if A = {01, 11} and B = {10, 11, 111}, what are AB
and BA?
TCSS 322 - Discrete Structures II!
http://moodle.insttech.washington.edu/!
Finite-State Machines
30 November 2010
28
Sets of Strings
We often need to deal with sets of strings when we work
with languages and language recognition - this requires
new terminology.
If A and B are subsets of V* (for some vocabulary V), then
the concatenation of A and B, written AB, is the set of all
strings of the form xy, where x∈A and y∈B.
Example: if A = {01, 11} and B = {10, 11, 111}, what are AB
and BA?
AB = {0110, 0111, 01111, 1110, 1111, 11111}
BA = {1001, 1101, 11101, 1011, 1111, 11111}
TCSS 322 - Discrete Structures II!
http://moodle.insttech.washington.edu/!
Finite-State Machines
30 November 2010
29
Kleene Closures
If A is a subset of V* (for some vocabulary V), the Kleene
closure of A, denoted by A*, is the set consisting of
concatenations of arbitrarily many strings of A. Formally:
A∗ =
∞
!
Ak
k=0
You may recognize this as looking very similar to the
definition of transitive closure.
Example: if B = {0, 1}, what is B*?
TCSS 322 - Discrete Structures II!
http://moodle.insttech.washington.edu/!
Finite-State Machines
30 November 2010
30
Kleene Closures
If A is a subset of V* (for some vocabulary V), the Kleene
closure of A, denoted by A*, is the set consisting of
concatenations of arbitrarily many strings of A. Formally:
A∗ =
∞
!
Ak
k=0
You may recognize this as looking very similar to the
definition of transitive closure.
Example: if B = {0, 1}, what is B*?
B* = {λ, 0, 1, 00, 01, 10, 11, 000, 001, ...}
(all binary strings, including the empty string)
TCSS 322 - Discrete Structures II!
http://moodle.insttech.washington.edu/!
Finite-State Machines
30 November 2010
31
Finite-State Automata
A finite-state machine with no output is called a finite-state
automaton.
Formally, a finite-state automaton M = (S, I, f, s0, F).
S is a finite set of states.
I is a finite input alphabet.
f is a transition function that assigns a next state to every
(state, input) pair.
s0 is the initial (or start) state.
F is a subset of S whose members are final states (also called
accepting states).
We can use state diagrams or state tables for these - in
state diagrams, final states are double circles.
TCSS 322 - Discrete Structures II!
http://moodle.insttech.washington.edu/!
Finite-State Machines
30 November 2010
32
Finite-State Automata - Example
This is a very simple finite state automaton:
1
Start
S0
0, 1
0
S1
Which of these strings leave the automaton in a final
state?
11111
10110
10101
00011
TCSS 322 - Discrete Structures II!
http://moodle.insttech.washington.edu/!
Finite-State Machines
30 November 2010
33
Finite-State Automata - Example
This is a very simple finite state automaton:
1
Start
S0
0, 1
0
S1
Which of these strings leave the automaton in a final
state?
11111
10110
10101
00011
TCSS 322 - Discrete Structures II!
http://moodle.insttech.washington.edu/!
Finite-State Machines
30 November 2010
34
Language Recognition
A string x is recognized or accepted by a finite-state
automaton M if, when x is input into M at the starting state,
M stops in a final state.
The language recognized by M - the set that contains
exactly all the strings that cause M to stop in a final state is called L(M).
Two finite-state automata are called equivalent if they
recognize the same language.
TCSS 322 - Discrete Structures II!
http://moodle.insttech.washington.edu/!
Finite-State Machines
30 November 2010
35
Finite-State Automata - Example
What is the language recognized by this automaton?
1
Start
S0
TCSS 322 - Discrete Structures II!
http://moodle.insttech.washington.edu/!
0, 1
0
S1
Finite-State Machines
30 November 2010
36
Finite-State Automata - Example
What is the language recognized by this automaton?
1
Start
S0
0, 1
0
S1
1* - the set of all binary strings that contain only 1s, plus
the empty string.
TCSS 322 - Discrete Structures II!
http://moodle.insttech.washington.edu/!
Finite-State Machines
30 November 2010
37
Finite-State Automata
What language is recognized by this finite-state
automaton?
0, 1
1
Start
S0
0
1
S2
0
S1
TCSS 322 - Discrete Structures II!
http://moodle.insttech.washington.edu/!
Finite-State Machines
30 November 2010
38
Finite-State Automata
What language is recognized by this finite-state
automaton?
0, 1
1
Start
S0
0
1
S2
0
S1
The language includes the empty string, all binary strings
that start with 01*0, and all binary strings that start with 1.
Thereʼs a nicer way to write this, as weʼll see later.
TCSS 322 - Discrete Structures II!
http://moodle.insttech.washington.edu/!
Finite-State Machines
30 November 2010
39
Finite-State Automata
Construct a finite-state automaton that recognizes all bit
strings with an even number of 1s.
TCSS 322 - Discrete Structures II!
http://moodle.insttech.washington.edu/!
Finite-State Machines
30 November 2010
40
Finite-State Automata
Construct a finite-state automaton that recognizes all bit
strings with an even number of 1s.
0
Start
TCSS 322 - Discrete Structures II!
http://moodle.insttech.washington.edu/!
S0
0
1
1
S1
Finite-State Machines
30 November 2010
41
Next Class
More finite-state automata
Introduction to Turing machines (time permitting)
TCSS 322 - Discrete Structures II!
http://moodle.insttech.washington.edu/!
Finite-State Machines
30 November 2010
42
© Copyright 2026 Paperzz