You are on page 1of 21

DISCRETE MATHEMATICS

W W L CHEN
c
W W L Chen, 1991, 2008.

This chapter is available free to all individuals, on the understanding that it is not to be used for nancial gain, and may be downloaded and/or photocopied, with or without permission from the author. However, this document may not be kept on any information storage and retrieval system without permission from the author, unless such system is not accessible to any individuals other than its owners.

Chapter 6
FINITE STATE MACHINES

6.1. Introduction Example 6.1.1. Imagine that we have a very simple machine which will perform addition and multiplication of numbers from the set {1, 2} and give us the answer, and suppose that we ask the machine to calculate 1 + 2. The table below gives an indication of the order of events: TIME STATE INPUT OUTPUT t1 (1) s1 (2) (3) 1 nothing t2 (4) s2 (5) (6) 2 nothing [1] t3 (7) (8) (9) s3 + 3 [1, 2] t4 (10) s1

Here t1 < t2 < t3 < t4 denotes time. We explain the table a little further: (1) The machine is in a state s1 of readiness. (2) We input the number 1. (3) There is as yet no output. (4) The machine is in a state s2 (it has stored the number 1). (5) We input the number 2. (6) There is as yet no output. (7) The machine is in a state s3 (it has stored the numbers 1 and 2). (8) We input the operation +. (9) The machine gives the answer 3. (10) The machine returns to the state s1 of readiness. Note that this machine has only a nite number of possible states. It is either in the state s1 of readiness, or in a state where it has some, but not all, of the necessary information for it to perform its task. On the other hand, there are only a nite number of possible inputs, namely the numbers 1, 2 and the operations + and . Also, there are only a nite number of outputs, namely nothing, junk (when
Chapter 6 : Finite State Machines page 1 of 21

Discrete Mathematics

W W L Chen, 1991, 2008

incorrect input is made) or the right answers. If we investigate this simple machine a little further, then we will note that there are two little tasks that it performs every time we input some information: Depending on the state of the machine and the input, the machine gives an output. Depending on the state of the machine and the input, the machine moves to the next state. Definition. A nite state machine is a 6-tuple M = (S, I, O, , , s1 ), where (a) S is the nite set of states for M ; (b) I is the nite input alphabet for M ; (c) O is the nite output alphabet for M ; (d) : S I O is the output function; (e) : S I S is the next-state function; and (f) s1 S is the starting state. Example 6.1.2. Consider again our simple nite state machine described earlier. Suppose that when the machine recognizes incorrect input (e.g. three numbers or two operations), it gives the output junk and then returns to the initial state s1 . It is clear that I = {1, 2, +, } and O = {j, n, 1, 2, 3, 4}, where j denotes the output junk and n denotes no output. We can see that S = {s1 , [1], [2], [+], [], [1, 1], [1, 2], [1, +], [1, ], [2, 2], [2, +], [2, ]}, where s1 denotes the initial state of readiness and the entries between the signs [ and ] denote the information in memory. The output function and the next-state function can be represented by the transition table below: 1 s1 [1] [2] [+] [] [1, 1] [1, 2] [1, +] [1, ] [2, 2] [2, +] [2, ] n n n n n j j 2 1 j 3 2 output 2 + n n n n n j j 3 2 j 4 4 n n n j j 2 3 j j 4 j j n n n j j 1 2 j j 4 j j 1 [1] [1, 1] [1, 2] [1, +] [1, ] s1 s1 s1 s1 s1 s1 s1 next state 2 + [2] [1, 2] [2, 2] [2, +] [2, ] s1 s1 s1 s1 s1 s1 s1 [+] [1, +] [2, +] s1 s1 s1 s1 s1 s1 s1 s1 s1 [] [1, ] [2, ] s1 s1 s1 s1 s1 s1 s1 s1 s1

Another way to describe a nite state machine is by means of a state diagram instead of a transition table. However, state diagrams can get very complicated very easily, so we shall illustrate this by a very simple example. Example 6.1.3. Consider a simple nite state machine M = (S, I, O, , , s1 ) with S = {s1 , s2 , s3 }, I = {0, 1}, O = {0, 1} and where the functions : S I O and : S I S are described by the transition table below: 0 +s1 + s2 s3
Chapter 6 : Finite State Machines

1 0 0 1

0 s1 s3 s1

1 s2 s2 s2
page 2 of 21

0 0 0

Discrete Mathematics

Chapter 6 : Finite State Machines 63 c WWL Chapter 6 : Finite State MachinesChen, 1991, 2008 63

Here we have indicated that s1 is the starting state. If we use the diagram Here we have indicated that s1 is the starting state. If we use the diagram si si
x,y x,y

sj sj

to describe (si , x) = y and (si , x) = sj , then we can draw the state diagram below: to describe (si , x) = y and (si , x) = sj , then we can draw the state diagram below:
0,0 0,0 1,0 1,0 1,0 1,0

start start

s1 s1

s2 s2
1,1 1,1 0,0 0,0

0,0 0,0

s3 s3 Suppose that we have an input string 00110101, i.e. we input 0, 0, 1, 1, 0, 1, 0, 1 successively, while starting that we have Suppose that we have an input string 00110101, i.e. we input 0, 0, 1, 1, 0, 1, 0, 1 successively, while starting 1 successively, while starting Suppose s . After the an input string 00110101, i.e. we input 0, 0, 1, 1, 0, 1, 0,After the second input 0, we at state s1 . After the rst input 0, we get output 0 and return to state s1 . After the second input 0, we state 1 rst input 0, we get output 0 and return to state s1 . at at state s1output 0 and return to state s .output 0 and return to state s1 . After the second input 0, we . After the again get output 0 rst input 0, we get After the third input output 0 and move to state 0 and move again get output 0 and return to state s1 After the third input 1, we get outputand move to to state again get so on. It and return to state s1 .1 .After the third inputthe1, we getstring 000000101 and state s2 . s2 . And so on. It is not dicult to see that we end up with thewe get output 00000101 and in state . And is not dicult to see that we end up with 1, output string output in state s2 so on. It AndNote thatis notinput stringsee that weis in up ,with the output string 00000101 andthe state s2 . string s2 . Note that the input string 00110101 is in I , the Kleene closure of I, and that the output string . the dicult to 00110101 end I the Kleene closure of I, and that in output Note s2 that the input O , the Kleene closure ofthe Kleene closure of I, and that the output string 00000101 is string 00110101 is in I , O. 00000101 00000101 is in in O , theis in O ,closure of O. Kleene the Kleene closure of O. We conclude this section We conclude this section by going ever so slightly upmarket. We conclude this section byby going ever so slightly upmarket. going ever so slightly upmarket. Example Let us attempt to add two numbers in binary notation together, for example a = 01011 Example 6.1.4. Let us attempt to add two numbers in binary notation together, for example a = 01011 Example 6.1.4. Let us attempt to add two numbers in binary notation together, for example a = 01011 6.1.4. and b = 00101. Note that the extra 0s are there to make sure that the two numbers have the same b = 00101. Note that the extra 0s are there to make sure that the two numbers have the same and b = 00101. Note that the extra 0s are there to make sure that the two numbers have the same and number of digits and that the sum of them has no extra digits. This would normally be done in the number of digits and that the sum of them has no extra digits. This would normally be done in the number of digits and that the sum of them has no extra digits. This would normally be done in the following way: following way: following way: a 0 1 0 1 1 a 0 1 0 1 1 a 0 1 0 1 1 + b b + 0 0 1 0 1 0 0 1 0 1 + b + 0 0 1 0 1 + + c 1 0 0 0 0 c 1 0 0 0 0 c 1 0 0 0 0 Let us write a = a5 a4 a3 a2 a1 and b = b5 b4 b3 b2 b1 , and write xj = (aj , bj ) for every j = 1, 2, 3, 4, 5. We can Let us write a = a5 a4 a3 a2 a1 and b = b5 b4 b3 b2 b1 , and write xj = (aj , bj ) for every j = 1, 2, 3, 4, 5. We can Let us write a = a5 a4 a3 a2 a1 and b = b5 b4 b3 b2 b1 , and write xj = (aj , bj ) for every j = 1, 2, 3, 4, 5. We can now think of the input string as x1 x2 x3 x4 x5 and the output string as c1 c2 c3 c4 c5 , where c = c5 c4 c3 c2 c1 . now think of the input string as x1 x2 x3 x4 x5 and the output string as c1 c2 c3 c4 c5 , where c = c5 c4 c3 c2 c1 . now think of the input string as x1 x2 x3 x4 x5 and the output string as c1 c2 c3 c4 c5 , where c = c5 c4 c3 c2 c1 . Note, however, that when we perform each addition, we have to see whether there is a carry from the Note, however, that when we perform each addition, we have to see whether there is a carry from the Note, however, that when we perform each addition, we have to see whether there is a carry from the previous addition, so that are two possible states at the start of each addition. Let us call them s0 (no previous addition, so that are two possible states at the start of each addition. Let us call them s0 (no previous addition, so that are two possible states at the start of each addition. Let us call them s0 (no carry) and s1 (carry). Then S = {s0 , s1 }, I = {(0, 0), (0, 1), (1, 0), (1, 1)} and O = {0, 1}. The functions carry) and s1 (carry). Then S = {s0 , s1 }, I = {(0, 0), (0, 1), (1, 0), (1, 1)} and O = {0, 1}. The functions carry) and s1 (carry). Then S = {s0 , s1 }, I = {(0, 0), (0, 1), (1, 0), (1, 1)} and O = {0, 1}. The functions : S I O and : S I S can be represented by the following transition table: : S I O and : S I S can be represented by the following transition table: : S I O and : S I S can be represented by the following transition table: (0, 0) (0, 0) 0 0 1 1 output output (0, 1) 1) (1, 0) (0, (1, 0) 1 1 1 1 0 0 0 0 (1, 1) (1, 1) 0 0 1 1 (0, 0) (0, 0) s0 s0 s0 s0
0

+s0 + +s0 + s1 s1

next state next state (0, 1) (1, 0) (0, 1) (1, 0) s0 s0 s0 s0 s1 s1 s1 s1

(1, 1) (1, 1) s1 s1 s1 s1
1

Clearly s0 is the starting state. Clearly s0 is the starting state.

6.2. Pattern Recognition Machines Pattern Recognition Machines 6.2. Pattern Recognition Machines In this section, we study examples of nite state machines that are designed to recognize given patterns. In this section, we study examples of nite state machines that are designed to recognize given patterns. As there is essentially no standard way of constructing such machines, we shall illustrate the underlying As there is essentially no standard way of constructing such machines, we shall illustrate the underlying ideas by examples. The questions in this section will get progressively harder. ideas by examples. The questions in this section will get progressively harder.
Chapter 6 : Finite State Machines page 3 of 21

Discrete Mathematics 64 WW

L Chen : Discrete Mathematics

W W L Chen, 1991, 2008

Remark. InIn the examples that follow, we shall use words likepassive and excited to describe the Remark. the examples that follow, we shall use words like passive and excited to describe the various states of the nite state machines that we are attempting to construct. These words play no role various states of the nite state machines that we are attempting to construct. These words play no role in serious mathematics, and should not be used in exercises or in any examination. It is hoped that by in serious mathematics, and should not be used in exercises or in any examination. It is hoped that by using such words here, the exposition will be a little more light-hearted and, hopefully, aalittle clearer. using such words here, the exposition will be a little more light-hearted and, hopefully, little clearer. Example 6.2.1. Suppose that I I = O = {0, 1}, and that we want todesign aanite state machine that Example 6.2.1. Suppose that = O = {0, 1}, and that we want to design nite state machine that recognizes the sequence pattern 11 in the input string x I . An example of an input string x I recognizes the sequence pattern 11 in the input string x I . An example of an input string x I and its corresponding output string yy O is shown below: and its corresponding output string O is shown below: x = 10111010101111110101 x = 10111010101111110101 yy= 00011000000111110000 = 00011000000111110000 Note that the output digit is 0 when the sequence pattern 11 is not detected and 1 when the sequence Note that the output digit is 0 when the sequence pattern 11 is not detected and 1 when the sequence pattern 11 is detected. In order to achieve this, we must ensure that the nite state machine has at least pattern 11 is detected. In order to achieve this, we must ensure that the nite state machine has at least two states, a passive state when the previous entry is 0 (or when no entry has yet been made), and two states, a passive state when the previous entry is 0 (or when no entry has yet been made), and an excited state when the previous entry is 1. Furthermore, the nite state machine has to observe an excited state when the previous entry is 1. Furthermore, the nite state machine has to observe the following and take the corresponding actions: the following and take the corresponding actions: (1) If it is in its passive state and the next entry is 0, it gives an output 00and remains in its passive (1) If it is in its passive state and the next entry is 0, it gives an output and remains in its passive state. state. (2) If it is in its passive state and the next entry is 1, it gives an output 00and switches to its excited (2) If it is in its passive state and the next entry is 1, it gives an output and switches to its excited state. state. (3) If it is in its excited state and the next entry is 0, it gives an output 0 and switches to its passive (3) If it is in its excited state and the next entry is 0, it gives an output 0 and switches to its passive state. state. (4) If it is in its excited state and the next entry is 1, it gives an output 11and remains in its excited (4) If it is in its excited state and the next entry is 1, it gives an output and remains in its excited state. state. It follows that if we denote by ss1 the passive state and by ss2 the excited state, then we have the It follows that if we denote by 1 the passive state and by 2 the excited state, then we have the state diagram below: state diagram below:
0,0 1,1 1,0 0,0

start We then have the corresponding transition table: We then have the corresponding transition table:

s1

s2

+s + +s11 + ss2 2

0 1 0 1 0 0 0 0 0 1 0 1

0 1 1 0 ss1 ss2 1 2 ss1 ss2 1 2

Example 6.2.2. Suppose again that = O = {0, 1}, and that we want to design nite state machine Example 6.2.2. Suppose again that I I = O = {0, 1}, and that we want to designaanite state machine that recognizes the sequence pattern 111 in the input string x I . An example of the same input that recognizes the sequence pattern 111 in the input string x I . An example of the same input string x I and its corresponding output string O is shown below: string x I and its corresponding output string yy O is shown below: x = 10111010101111110101 x = 10111010101111110101 = 00001000000011110000 yy= 00001000000011110000 In order to achieve this, the nite state machine must now have at least three states, aa passive state In order to achieve this, the nite state machine must now have at least three states, passive state when the previous entry is 0 (or when no entry has yet been made), an expectant state when the when the previous entry is 0 (or when no entry has yet been made), an expectant state when the previous two entries are 01 (or when only one entry has so far been made and it is 1), and an excited previous two entries are 01 (or when only one entry has so far been made and it is 1), and an excited state when the previous two entries are 11. Furthermore, the nite state machine has to observe the state when the previous two entries are 11. Furthermore, the nite state machine has to observe the following and take the corresponding actions: following and take the corresponding actions:
Chapter 6 : Finite State Machines page 4 of 21

Discrete Mathematics

Chapter 6 : Finite State c W W L Chen, 1991, 2008 Machines 65

(1) If it is in its passive state and the next entry is 0, it gives an output and remains in its passive (1) If it is in its passive state and the next entry is 0, it gives an output 00and remains in its passive state. state. (2) If it is in its passive state and the next entry is 1, it gives an output and switches to its (2) If it is in its passive state and the next entry is 1, it gives an output 00 and switches to its expectant state. expectant state. (3) If it is in its expectant state and the next entry is 0, it gives an output 00 and switches to its (3) If it is in its expectant state and the next entry is 0, it gives an output and switches to its passive state. passive state. (4) If it is in its expectant state and the next entry is 1, it gives an output and switches to its (4) If it is in its expectant state and the next entry is 1, it gives an output 00 and switches to its excited state. excited state. (5) If it is in its excited state and the next entry is 0, it gives an output 0 and switches to its passive (5) If it is in its excited state and the next entry is 0, it gives an output 0 and switches to its passive state. state. (6) If it is in its excited state and the next entry is 1, it gives an output and remains in its excited (6) If it is in its excited state and the next entry is 1, it gives an output 11and remains in its excited state. state. If we now denote by 1 the passive state, by 2 the expectant state and by 3 the excited state, If we now denote by ss1 the passive state, by ss2 the expectant state and by ss3 the excited state, then we have the state diagram below: then we have the state diagram below:
0,0

start

s1

1,0 0,0

s2
1,0

0,0

s3
1,1

We then have the corresponding transition table: We then have the corresponding transition table: 0 1 0 1 0 0 0 0 0 0 0 0 0 1 0 1 0 1 1 0 ss1 ss2 1 2 ss1 ss3 1 3 ss1 ss3 1 3

+s + +s11 + ss2 2 ss3 3

Example 6.2.3. Suppose again that = O = {0, 1}, and that we want to design nite state machine Example 6.2.3. Suppose again that I I = O = {0, 1}, and that we want to designaanite state machine that recognizes the sequence pattern 111 in the input string x I , but only when the third 1 in the recognizes the sequence pattern 111 in the input string x I , but only when the third 1 in the that sequence pattern 111 occurs at a position that is a multiple of 3. An example of the same input string sequence pattern 111 occurs at a position that is a multiple of 3. An example of the same input string x I and its corresponding output string O is shown below: x I and its corresponding output string yy O is shown below: x = 10111010101111110101 x = 10111010101111110101 = 00000000000000100000 yy= 00000000000000100000 In order to achieve this, the nite state machine must as before have at least three states, passive In order to achieve this, the nite state machine must as before have at least three states, aa passive state when the previous entry is 0 (or when no entry has yet been made), an expectant state when the state when the previous entry is 0 (or when no entry has yet been made), an expectant state when the previous two entries are 01 (or when only one entry has so far been made and it is 1), and an excited previous two entries are 01 (or when only one entry has so far been made and it is 1), and an excited state when the previous two entries are 11. However, this is not enough, as we also need to keep track state when the previous two entries are 11. However, this is not enough, as we also need to keep track of the entries to ensure that the position of the rst in the sequence pattern 111 occurs at position of the entries to ensure that the position of the rst 11 in the sequence pattern 111 occurs at aa position immediately after a multiple of 3. It follows that if we permit the machine to be at its passive state immediately after a multiple of 3. It follows that if we permit the machine to be at its passive state only either at the start or after 3k entries, where k N, then it is necessary to have two further states only either at the start or after 3k entries, where k N, then it is necessary to have two further states to cater for the possibilities of 0 in at least one of the two entries after passive state and to then to cater for the possibilities of 0 in at least one of the two entries after aa passive state and to then
Chapter 6 : Finite State Machines page 5 of 21

Discrete Mathematics L Chen : Discrete Mathematics 66 WW

W W L Chen, 1991, 2008

delay the return to this passive state. We can have the state diagram below: s3
0,0 1,1 1,0

start

s1
0,0

1,0

s2
0,0

0,0 1,0 0,0 1,0

s4 We then have the corresponding transition table: We then have the corresponding transition table:

s5

+s1 + +s1 + s2 s2 s3 s3 s4 s4 s5 s5

0 0 0 0 0 0 0 0 0 0 0 0

1 1 0 0 0 0 1 1 0 0 0 0

0 0 s4 s4 s5 s5 s1 s1 s5 s5 s1 s1

1 1 s2 s2 s3 s3 s1 s1 s5 s5 s1 s1

Example 6.2.4. Suppose again that II= O = {0, 1}, and that we want to design a nite state machine Suppose again that = O = {0, 1}, and that we want to design a that recognizes the sequence pattern 0101 in the input string x I . An example of the same input string x I and its corresponding output string y O is shown below: x = 10111010101111110101 y = 00000000101000000001 In order to achieve this, the nite state machine must have at least four states, a passive state when the previous two entries are 11 (or when no entry has yet been made), an expectant state when the previous three entries are 000 or 100 or 110 (or when only one entry has so far been made and it is 0), an excited state when the previous two entries are 01, and a cannot-wait state when the previous three entries are 010. Furthermore, the nite state machine has to observe the following and take the corresponding actions: (1) If it is in its passive state and the next entry is 0, it gives an output 0 and switches to its expectant state. (2) If it is in its passive state and the next entry is 1, it gives an output 0 and remains in its passive state. (3) If it is in its expectant state and the next entry is 0, it gives an output 0 and remains in its expectant state. (4) If it is in its expectant state and the next entry is 1, it gives an output 0 and switches to its excited state. (5) If it is in its excited state and the next entry is 0, it gives an output 0 and switches to its cannot-wait state. (6) If it is in its excited state and the next entry is 1, it gives an output 0 and switches to its passive state.
Chapter 6 : Finite State Machines page 6 of 21

Discrete Mathematics

Chapter 6 :: Finite State Machines Chapter 6 Finite State MachinesChen, 1991, 67 67 c WWL 2008

(7) If it is in its cannot-wait state and the next entry is 0, it gives an output 0 and switches to its (7) If it is in its cannot-wait state and the next entry is 0, it gives an output 0 and switches to its (7) If it is in its cannot-wait state and the next entry is 0, it gives an output 0 and switches to its expectant state. expectant state. expectant state. (8) If it is in its cannot-wait state and the next entry is 1, it gives an output 1 and switches to its (8) If it is in its cannot-wait state and the next entry is 1, it gives an output 1 and switches to its excited state. excited state. It follows that if we denote by s1 the passive state, by s2 the expectant state, by s3 the excited It follows that if we denote by s1 the passive state, by s2 the expectant state, by s3 the excited state and by s4 the cannot-wait state, then we have the state diagram below: state and by s4 the cannot-wait state, then we have the state diagram below:
1,0 1,0 0,0 0,0 0,0 0,0

start start

s1 s1
1,0 1,0

s2 s2
0,0 0,0

1,0 1,0 0,0 0,0 1,1 1,1

s3 s3 We then have the corresponding transition table: We then have the corresponding transition table: 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 1 1

s4 s4

+s1 + +s1+ s2 s2 s3 s3 s4 s4
4

0 0 s2 s2 s2 s2 s4 s4 s2 s2
2

1 1 s1 s1 s3 s3 s1 s1 s3 s3
3

Example 6.2.5. Suppose again that II= O = {0, 1}, and that we want to design a nite state machine Example 6.2.5. Suppose again that I = O = {0, 1}, and that we want to design a nite state machine Example 6.2.5. Suppose again that = O = {0, 1}, and that we want to machine that recognizes the sequence pattern 0101 in the input string x I ,, but only when the last 1 in the that recognizes the sequence pattern 0101 in the input string x I , but only when the last 1 in the that recognizes the sequence pattern 0101 in the input string x I but only when the last 1 in the sequence pattern 0101 occurs at a position that is a multiple of 3. An example of the same input string sequence pattern 0101 occurs at a position that is a multiple of 3. An example of the same input string sequence pattern 0101 occurs at a position that is a multiple of 3. An example of the same input string x I and its corresponding output string y O is shown below: x I and its corresponding output string y O is shown below: x I and its corresponding output string y O is shown below: x = 10111010101111110101 x = 10111010101111110101 x = 10111010101111110101 y = 00000000100000000000 y = 00000000100000000000 y = 00000000100000000000 In order to achieve this, the nite state machine must have at least four states s3 , s4 , s5 and s6 , In order to achieve this, the nite state machine must have at least four states s3 ,, s4 ,, s5 and s6 ,, In order to achieve this, the nite state machine must have at least four states s3 s4 s5 and s6 corresponding to the four states before. We need two further states for delaying purposes. If we denote corresponding to the four states before. We need two further states for delaying purposes. If we denote corresponding to the four states before. We need two further states for delaying purposes. If we denote these two states by s1 and s2 , then we can have the state diagram as below: these two states by s1 and s2 ,, then we can have the state diagram as below: these two states by s and s then we can have the state diagram as below:
1 2

s3 s3
1,0 1,0 0,0 0,0 1,0 1,0 0,0 0,0 1,0 1,0

start start

s1 s1

0,0 0,0 1,0 1,0

s2 s2
1,1 1,1

0,0 0,0

s4 s4
0,0 0,0

1,0 1,0

s5 s5

0,0 0,0

s6 s6
Chapter 6 : Finite State Machines page 7 of 21

68 WW Discrete Mathematics

68

W W L Chen : Discrete Mathematics

W W L Chen, 1991, 2008

We then have the corresponding transition table: We then have the corresponding transition table: 0 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1 0 1 1 0 ss2 ss2 22 22 ss3 ss3 33 33 ss4 ss1 44 11 ss2 ss5 22 55 ss6 ss3 66 33 ss4 ss1 44 11

+s1 +s11 + + ss2 22 ss3 33 ss4 44 ss5 55 ss6 66

6.3. An Optimistic Approach 6.3. An Optimistic Approach We now describe an approach which helps greatly in the construction of pattern recognition machines. We now describe an approach which helps greatly in the construction of pattern recognition machines. The idea is extremely simple. We construct rst of all the part of the machine to take care of the The idea is extremely simple. We construct rst of all the part of the machine to take care of the situation when the required pattern occurs repeatly and without interruption. We then complete the situation when the required pattern occurs repeatly and without interruption. We then complete the machine by studying the situation when the wrong input is made at each state. machine by studying the situation when the wrong input is made at each state. WeWe shall illustrate this approach by looking the same ve examples thethe previous section. shall illustrate this approach by looking the same ve examples in in previous section. Example 6.3.1. Suppose that I I = O = {0, 1}, and that we want todesign aanite state machine that Example 6.3.1. Suppose that = O = {0, 1}, and that we want to design nite state machine that recognizes the sequence pattern 11 in the input string x I Consider rst of all the situation when recognizes the sequence pattern 11 in the input string x I . Consider rst of all the situation when . the required pattern occurs repeatly and without interruption. In other words, consider the situation the required pattern occurs repeatly and without interruption. In other words, consider the situation when the input string is 111111 To describe this situation, we have the following incomplete state when the input string is 111111 ........ To describe this situation, we have the following incomplete state diagram: diagram:
1,1 1,1

start

s1 1

1,0 1,0

s2 2

It now remains to study the situation when we have the wrong input at each state. Naturally, with It now remains to study the situation when we have the wrong input at each state. Naturally, with aa wrong input, the output is always 0, so the only unresolved question is to determine the next state. wrong input, the output is always 0, so the only unresolved question is to determine the next state. Note that whenever we get an input 0, the process starts all over again; in other words, we must return Note that whenever we get an input 0, the process starts all over again; in other words, we must return to state 11 We therefore obtain the state diagram as in Example 6.2.1. to state ss1 . We therefore obtain the state diagram as in Example 6.2.1. . Example 6.3.2. Suppose again that O = O 1}, and that we want to design nite a nite state Example 6.3.2. Suppose again that I =I = {0, = {0, 1}, and that we want toa designstate machine machine that recognizes the pattern 111 in the111 in the input string x I . rst of all rst situation that recognizes the sequence sequence pattern input string x I . Consider Consider the of all the situation when the pattern occurs repeatly repeatly and without interruption. In other words, consider required pattern occurs and without interruption. In other words, consider the when the required the situation the input input string is . . . To . . . . To describe this we have the following incomplete situation when when the string is 111111 . 111111describe this situation, situation, we have the following incomplete state diagram: state diagram: start s1 1
1,0 1,0

s2 2
1,0 1,0

s3 3
1,1 1,1

It now remains to study the situation when we have the wrong input at each state. As before, with It now remains to study the situation when we have the wrong input at each state. As before, with aa wrong input, the output is always 0, so the only unresolved question is to determine the next state. wrong input, the output is always 0, so the only unresolved question is to determine the next state.
Chapter 6 : Finite State Machines page 8 of 21

Discrete Mathematics

c WWL Chapter 6 : Finite State MachinesChen, 1991, 2008 69 Chapter 6 : Finite State Machines 69 Chapter 6 : Finite State Machines 69

Note that whenever we get an input 0, the process starts all over again; in other words, we must return to state s1 . We therefore obtain the state diagram as Example 6.2.2. Example . We therefore obtain that state O = {0, 1}, and that we want to design a nite state machine Example1 6.3.3. Suppose again the II= diagram 1},Example 6.2.2. to state s 6.3.3. Suppose again that = O = {0, as and that we want to design a nite state machine that recognizes theSuppose again that111 in the{0, 1}, and that Iwant toonly when the third machine that recognizes the sequence pattern 111 in the input string x I , but only when the third 1 in the Example 6.3.3. sequence pattern I = O = input string x we , but design a nite state 1 in the sequence pattern 111 occursagainposition=in the{0,multiple that Consider only of allnite third machine sequence pattern 111 occurs at a that111 that is a multiple of 3. Consider rst when the situation when Example 6.3.3. sequence pattern I O is a 1}, string 3. I , but design a the state 1 when that recognizes theSuppose at a position that= input and of x we want torst of all the situationin the the required patternsequencerepeatly and that is a multiple of x In , but words, consider thirdsituation the required pattern occurs at a position in the input string 3. In other words, consider the situation that recognizes the pattern 111 without interruption. I other rst when the the 1 when sequence pattern 111 occurs repeatly and without interruption. Consider only of all the situationin the when the pattern 111 occurs repeatly To describe a multiple of 3. we have the following incomplete when when the input stringoccurs at a. position that is this situation, we have the following incomplete state sequence input string is 111111 . . . and without interruption. Consider rst of all the situation state the required pattern is 111111 . . . . To describe this situation, In other words, consider the situation diagram: input string is 111111 . . . . To describe this situation, we have the following incomplete state diagram: the required pattern occurs repeatly and without interruption. In other words, consider the situation when the when the diagram: input string is 111111 . . . . To describe this situation, we have the following incomplete state s3 diagram: s3 s3
1,1 1,1 1,0 1,1 1,0 1,0 1,0 1,0

Note that whenever we get an input 0, the process starts all over again; in other words, we must return Note that whenever we get an input 0, the process starts all over again; in other words, we must return to state s1 . We therefore obtain the state diagram as Example 6.2.2. to state s whenever we get an input state diagram starts all over again; in other words, we must return Note that . We therefore obtain the 0, the process as Example 6.2.2.
1

s2 s1 start s2 s1 start 1,0 It now remains to study the situation when we have the wrong input at each state. As before, with a s2 s1 wrong input at each state. As before, with a start It now remains to study theis always 0, so the only unresolved questionat each state. Asthe nextwith a wrong input, the output situation when we have the It now remains to study the situation when we have the wrong input is to determine before, state. wrong that the machine is situation0, ,so the only unresolved0 question at each state. Asthe next with a Suppose input, to study theisat state swhen we only unresolved question is to note that if the next state. It now remains the output always 3 and the wrong wrong made. We determine before, three wrong input, the output is always 0, so the have theinput is input is to determine the next state. Suppose that the machine pattern shouldand recognized. input 0 question(stonote thatWethe next state. entries are 111, thenoutputis at state s3 , and the wrong input 0 is made. We 0) = s1 . if the next three wrong input, the that is always 0, be the wrong It follows that We determine now have the Suppose that the machine is at state s3 , so the only unresolved is made. is 3 , note that if the next three entries are 111, then that pattern should and recognized. input 0 is made.(s3 , 0) = s1 . Wethe next three following that the machine pattern should be recognized. It follows that (s3 , 0) = thatWe now have the Suppose incomplete that diagram: entries are 111, then state is at state s3 , be the wrong It follows that We note s1 . if now have the following incomplete state pattern should be recognized. It follows that (s , 0) = s . We now have the entries are 111, then that diagram: following incomplete state diagram: 3 1 s3 following incomplete state diagram: s3 s3 0,0 s2 s1 start 1,0 s2 s1 start 1,0 Suppose that the machine is at state s1 , and the wrong input 0 is made. We note that the next s2 s1 start inputs cannot contribute to at pattern, and the wrong input 0 is made. before returning next Suppose that the machine isany state s1 , so we need to delay by two steps We note that theto s1 . Suppose that following incomplete state ,diagram: wrongdelay by two steps We note that theto s . now havethat the machine is at state s1 and the wrong input 0 is made. before returning next Suppose the contribute to at pattern, and the inputs cannot the machine isany state s1 , so we need to input 0 is made. We note that the next 1 inputs cannot following incomplete state diagram: inputs cannot contribute to any pattern, so we need to delay by two steps before returning to s1 . now have the contribute to any pattern, so we need to delay by two steps before returning to s1 . s3 now have the following incomplete state diagram: now have the following incomplete state diagram: s3 s3 0,0 s1 s1 s1
0,0 1,1 0,0 1,1 1,0 1,1 1,0 1,0 0,0 1,0 0,0 1,0 0,0 1,0 1,0 0,0 1,1 0,0 1,1 1,0 1,1 1,0 1,0 1,0

two We two two two We We We

start start start

0,0 0,0

s2 s2 s2

1,0

0,0 1,0 s4 s5 1,0 0,0 s4 s5 1,0 0,0 s s5 Finally, it remains to examine (s2 , 0). We therefore4 obtain the state diagram as Example 6.2.3. 1,0 Finally, it remains to examine (s2 , 0). We therefore obtain the state diagram as Example 6.2.3. Note remains to examine (s2 0). We therefore obtain the state , we have actually introduced the Finally, it that in Example 6.3.3, in ,dealing with the input 0 at state s1diagram as Example 6.2.3. Finally, it remains to examine (s2 , 0). We therefore obtain the state diagram as Example 6.2.3. extra states s4 in Example 6.3.3, in dealing withnot actually at state s1 , we positive identication of the Note that and s5 . These extra states are the input 0 involved with have actually introduced desired pattern,and sare These extra delaying thethe actuallyonestate s1states already present. It follows Note that Example 6.3.3, in states are return to involved , we have actually introduced extra states s4 inbut 5 . essentialin dealing withnot input 0 at of thewith positive identication of the Note that in Example 6.3.3, in dealing with the input 0 at state s1 , we have actually introduced the that at thesesextra states, the outputstates are not actuallyone of the with positive identicationfollows extra states 4 but 5 . essential in should the return to involved have already present. situation desired pattern,and sare These extra delaying always be 0. However, westates to investigate the It of the extra states s4 and s5 . These extra states are not actually involved with positive identication of the for input 0 and 1. states, the outputdelaying always be 0.to one of the states to investigate the It follows desired these extra are essential in should the return However, we have already present. situation that at pattern, but desired pattern, but are essential in delaying the return to one of the states already present. It follows that at these extra for input 0 and 1. states, the output should always be 0. However, we have to investigate the situation that In the next two examples, we do should always be 0. details. Readers are advised to look situation at these extra states, the output not include all the However, we have to investigate the carefully for input 0 and 1. for the argument two examples, wetheynot include all the details. Readers are advised to question is to input 0 and 1.and ensure that do understand the underlying arguments. The main look carefully at In the next In the next and ensure that do understand the underlying arguments. The main look carefully at the argument two examples, wetheynot include all the details. Readers are advised to question is to Chapter 6 : Finite State Machines page of 21 at the argument and ensure that they understand the underlying arguments. The main question9 is to

0,0

610 610

Discrete Mathematics

W W L Chen : Discrete Mathematics W W L Chen : Discrete Mathematics

W W L Chen, 1991, 2008

In the next two examples, we do not include all the details. Readers are advised to look carefully recognize that with a wrong input, we may still be able to recover part of a pattern. For example, if at the argument and wrong input, we may still be able to recover part of a pattern. For example, to ensure understand the question is if recognize that with a is 01011that theygot 01010, where underlying arguments. The main the last three the pattern we want but we the fth input is incorrect, then recognize that with ais 01011 but we got may still be able to recover partincorrect, thenFor example, if wrong input, we 01010, where the fth input is of a pattern. the last three the pattern we want still possibly form the rst three inputs of the pattern 01011. If one is optimistic, inputs in 01010 can the pattern we can is but then is optimistic, inputs in 01010 wantstill01011sevenwe got 01010, where the fth input is incorrect,If one the last three one might hope that the possibly form the are 0101011. rst inputs rst three inputs of the pattern 01011. inputs in 01010 that still rst seven inputs are 0101011. can the possibly form the rst three inputs of the pattern 01011. If one is optimistic, one might hope one might hope that the rst seven inputs are 0101011. Example 6.3.4. Suppose again that I = O = {0, 1}, and that we want to design a nite state Example 6.3.4. Suppose again that I = O =O = in and that we wantxto design a nite rst of all the machine that recognizes the again that I = 0101 {0,the input that we I . to design state machine Example 6.3.4. Suppose sequence pattern {0, 1}, 1}, and string want Consider a nite state machine that recognizes the pattern occurs in the input string x interruption.. In other all the of all the situation when the required sequence 0101 repeatly in the input string Consider Consider rst situation that recognizes the sequence pattern pattern 0101 and without I . x I rst of words, consider situation when the required pattern repeatly and. .without interruption. In otherother words, following occurs repeatly and describe interruption. Inwewords, the consider without this situation, the situation when pattern occurs is 010101 . . To have consider the when the required the input string the situation wheninput input string is 010101 .describedescribe this situation,the following incomplete incomplete state diagram: situation when the the string is 010101 . . . . To . . . To this situation, we have we have the following incomplete state diagram: state diagram: 0,0 s1 s2 start 0,0 s1 s2 start
1,0 1,0

s3 s3

0,0 0,0 1,1 1,1

s4 s4

It now remains to study the situation when we have the wrong input at each state. As before, with a It now remains the output always when we have the wrong input is to determine before, state. It now remains to study theissituation0, so the only unresolved questionat each state. Asthe nextwith a wrong input, to study the situation when we have the wrong input at each state. As before, with a wrong input, the output wrong input, the output iss1always,0, so the (s3 , unresolved question is to 2determine the next state. Recognizing that (s1 , 1) =is always 0) so s2 , only 1) = s1 and (s4 , 0) is to , we therefore obtain the , (s2 0, = the only unresolved question = s determine the next state. Recognizing that (s1 1) 6.2.4. Recognizing that Example= s1 ,, (s2 ,,0) = s2 ,, (s3 ,,1) = s1 and (s4 ,,0) = s2 ,, we therefore obtain the state diagram as (s1 ,,1) = s1 (s2 0) = s2 (s3 1) = s1 and (s4 0) = s2 we therefore obtain the state diagram as Example 6.2.4. state diagram as Example 6.2.4. Example 6.3.5. Suppose again that II= O = {0, 1}, and that we want to design a nite state machine Example 6.3.5. Suppose again that = O = {0, 1}, and that we want to design a nite state machine Example 6.3.5. Suppose again that0101 O = {0, 1}, and thatxwe want to design a nite state machine I= that recognizes the sequence pattern 0101 in the input string x I ,, but only when the last 1 in the that recognizes the sequence pattern in the input string I but only when the last 1 in the that recognizes the sequence pattern 0101 inthat is a multiple x 3. Consider rst of the the situation the I but only when last 1 in the sequence pattern 0101 occurs at a position that input string of 3. ,Consider rst of all the situation sequence pattern 0101 occurs at a position is a multiple of all sequence pattern 0101 occurs at arepeatly and without interruption. Consider rst of all two inputs do position that is a multiple of 3. the situation when the required pattern occurs repeatly and without interruption. Note that the rst two inputs do when the required pattern occurs Note that the rst when the requiredany pattern, so we consider the situation when the input string thexrst two . inputs do pattern occurs repeatly and without interruption. Note that not contribute to any pattern, so we consider the situation when the input string is x1 x2 0101 . .. .. ,, where not contribute to is 1 x2 0101 where not x2 {0, 1}. to any pattern, sosituation, we the situation when the input string is x1 x2 0101 . . . , where contribute we consider x1 ,, x {0, 1}. To describe this situation, we have the following incomplete state diagram: x1 2 To describe this have the following incomplete state diagram: x1 , x2 {0, 1}. To describe this situation, we have the following incomplete state diagram: s3 s3
0,0 0,0 1,0 1,0 0,0 0,0

start start

s1 s1

0,0 1,0 0,0 1,0 1,1 1,1

s2 s2

s4 s4
0,0 0,0

1,0 1,0

s5 s5

s6 s6 It now remains to study the situation when we have the wrong input at each state. As before, with a It now remains to study the situation when we have the wrong input at each state. As before, with a It now remains to study theis always 0, so the only unresolved questionat each state. Asthe nextwith a wrong input, the output situation when we have the wrong input is to determine before, state. wrong input, the output is always 0, so the only unresolved question is to determine the next state. wrong input, the output= s1 ,, (s4 ,,0, so the, (s5 ,,unresolved question is= s4determine the next state. Recognizing that (s3 1) s we therefore obtain the Recognizing that (s3 ,, 1) =issalways 0) = s2 , only 1) = s3 and (s6 ,, 0) =to 4,, we therefore obtain the 1 (s4 0) = s2 (s5 1) = s3 and (s6 0) Recognizing that Example= s1 , (s4 , 0) = s2 , (s5 , 1) = s3 and (s6 , 0) = s4 , we therefore obtain the state diagram as (s3 , 1) 6.2.5. state diagram as Example 6.2.5. state diagram as Example 6.2.5. 6.4. Delay Machines 6.4. Delay Machines 6.4. Delay Machines We now consider a type of nite state machines that is useful in the design of digital devices. These are We now consider a type of nite state machines that is useful in the design of digital devices. These are called k-unit delay machines, where k N. Corresponding to the input x = x x2 x3 . ., the machine will We now consider a machines, where k N. Corresponding to thethe design of1digital..devices. These will called k-unit delay type of nite state machines that is useful in input x = x 1x2 x3 . ., the machine are called k-unit delay machines, where k N. Corresponding to the input x = x1 x2 x3 . . ., the machine will
Chapter 6 : Finite State Machines page 10 of 21

Discrete Mathematics

c WWL 2008 Chapter 6 : Finite State Machines Chen, 1991,611

give the output give the output y = 0 . . . 0 x1 x2 x3 . . . ;


k

in other words, it adds k 0s at the front of the string. An important point to to note that if we want to add add k at the front front ofstring, then the same An important point note is is that if we want to k 0s 0s at the of the the string, then the result result can be obtained by feeding theinto a 1-unit a 1-unit delay machine, then feeding the output same can be obtained by feeding the string string into delay machine, then feeding the output obtained into the same machine, and repeating this process. It process.thatfollows that we need only1-unit delay obtained into the same machine, and repeating this follows It we need only investigate investigate machines. machines. 1-unit delay Example 6.4.1. Suppose that II= {0, 1}. Then aa1-unit delay machine can be described by the state Example 6.4.1. Suppose that = {0, 1}. Then 1-unit delay machine can be described by the state diagram below: diagram below:
0,0

s2
0,0

start

s1
1,0

0,1

1,0

s3
1,1

Note that (si , 0) = i states s2 and s3 , the 2 3

s2 and (s2 , x) = 0; also (si , 1) = s3 and (s3 , x) = 1. It follows that at both 2 2 i 3 3 output is always the previous input.

In In general, I may contain more than 2 elements.Any attempt to construct a 1-unit delay machine general, I may contain more than 2 elements. Any attempt to construct a 1-unit by state diagram is likely to end up in a mess. We therefore seek to understand the ideas behind such a machine, and hope that it will help us in our attempt to construct it. Example 6.4.2. Suppose that II= {0, 1, 2, 3, 4, 5}. Necessarily, there must be a starting state s1 which Suppose that = {0, 1, 2, 3, 4, 5}. Necessarily, there must be a starting state 1 will add the 0 to the front of the string. It follows that (s1 , x) = 0 for every x I. The next question is to study (s1 , x) for every x I. However, we shall postpone this until towards the end of our argument. At this point, we are not interested in designing a machine with the least possible number of states. We simply want a 1-unit delay machine, and we are not concerned if the number of states is more than this minimum. We now let s2 be a state that will delay the input 0. To be precise, we want the state s2 to satisfy the following requirements: (1) For every state s S, we have (s, 0) = s2 . (2) For every state s S and every input x I \ {0}, we have (s, x) = s2 . 2 (3) For every input x I, we have (s2 , x) = 0. 2 Next, we let s3 be a state that will delay the input 1. To be precise, we want the state s3 to satisfy Next, we let s3 be a state that will delay the input 1. To be precise, we want the state s3 to satisfy the following requirements: the following requirements: (1) For every state s S, we have (s, 1) = s3 . (1) For every state s S, we have (s, 1) = s3 . (2) For every state s S and every input x I \ {1}, we have (s, x) = s3 . (2) For every state s S and every input x I \ {1}, we have (s, x) = s3 . (3) For every input x I, we have (s3 , x) = 1. (3) For every input x I, we have (s3 , x) = 1. Similarly, we let s4 , s5 , s6 and s7 be the states that will delay the inputs 2, 3, 4 and 5 respectively. Similarly, we let s4 , s5 , s6 and s7 be the states that will delay the inputs 2, 3, 4 and 5 respectively. Let us look more closely at the state s2 . By requirements (1) and (2) above, the machine arrives at state Let us look more closely at the state s2 . By requirements (1) and (2) above, the machine arrives at state s2 precisely when the previous input is 0; by requirement (3), the next output is 0. This means that s2 precisely when the previous input is 0; by requirement (3), the next output is 0. This means that the previous input must be the same as the next output, which is precisely what we are looking for. the previous input must be the same as the next output, which is precisely what we are looking for.
Chapter 6 : Finite State Machines page 11 of 21

Discrete Mathematics

W W L Chen, 1991, 2008

Now all these requirements are summarized in the table below: 0 +s1 + s2 s3 s4 s5 s6 s7 0 0 1 2 3 4 5 1 0 0 1 2 3 4 5 2 0 0 1 2 3 4 5 3 0 0 1 2 3 4 5 4 0 0 1 2 3 4 5 5 0 0 1 2 3 4 5 0 s2 s2 s2 s2 s2 s2 1 s3 s3 s3 s3 s3 s3 2 s4 s4 s4 s4 s4 s4 3 s5 s5 s5 s5 s5 s5 4 s6 s6 s6 s6 s6 s6 5 s7 s7 s7 s7 s7 s7

Finally, we study (s1 , x) for every x I. It is clear that if the rst input is 0, then the next state must delay this 0. It follows that (s1 , 0) = s2 . Similarly, (s1 , 1) = s3 , and so on. We therefore have the following transition table: 0 +s1 + s2 s3 s4 s5 s6 s7 0 0 1 2 3 4 5 1 0 0 1 2 3 4 5 2 0 0 1 2 3 4 5 3 0 0 1 2 3 4 5 4 0 0 1 2 3 4 5 5 0 0 1 2 3 4 5 0 s2 s2 s2 s2 s2 s2 s2 1 s3 s3 s3 s3 s3 s3 s3 2 s4 s4 s4 s4 s4 s4 s4 3 s5 s5 s5 s5 s5 s5 s5 4 s6 s6 s6 s6 s6 s6 s6 5 s7 s7 s7 s7 s7 s7 s7

6.5. Equivalence of States Occasionally, we may nd that two nite state machines perform the same tasks, i.e. give the same output strings for the same input string, but contain dierent numbers of states. The question arises that some of the states are essentially redundant. We may wish to nd a process to reduce the number of states. In other words, we would like to simplify the nite state machine. The answer to this question is given by the Minimization process. However, to describe this process, we must rst study the equivalence of states in a nite state machine. Suppose that M = (S, I, O, , , s1 ) is a nite state machine. Definition. We say that two states si , sj S are 1-equivalent, denoted by si 1 sj , if (si , x) = (sj , x) = for every x I. Similarly, for every k N, we say that two states si , sj S are k-equivalent, denoted by si k sj , if (si , x) = (sj , x) for every x I k (here (si , x1 . . . xk ) denotes the output string = corresponding to the input string x1 . . . xk , starting at state si ). Furthermore, we say that two states si , sj S are equivalent, denoted by si sj , if si k sj for every k N. = = The ideas that underpin the Minimization process are summarized in the following result. PROPOSITION 6A. (a) For every k N, the relation k is an equivalence relation on S. = (b) Suppose that si , sj S, k N and k 2, and that si k sj . Then si k1 sj . In other words, = = k-equivalence implies (k 1)-equivalence. (c) Suppose that si , sj S and k N. Then si k+1 sj if and only if si k sj and (si , x) k (sj , x) = = = for every x I.
Chapter 6 : Finite State Machines page 12 of 21

Discrete Mathematics

W W L Chen, 1991, 2008

Proof. (a) The proof is simple and is left as an exercise. (b) Suppose that si =k1 sj . Then there exists x = x1 x2 . . . xk1 I k1 such that, writing (si , x) = y1 y2 . . . yk1 and (sj , x) = y1 y2 . . . yk1 , we have that y1 y2 . . . yk1 = y1 y2 . . . yk1 . Suppose now that u I. Then clearly there exist z , z O such that (si , xu) = y1 y2 . . . yk1 z = y1 y2 . . . yk1 z = (sj , xu). Note now that xu I k . It follows that si k sj . = (c) Suppose that si k sj and (si , x) k (sj , x) for every x I. Let x1 x2 . . . xk+1 I k+1 . Clearly = = (si , x1 x2 . . . xk+1 ) = (si , x1 )((si , x1 ), x2 . . . xk+1 ). Similarly (sj , x1 x2 . . . xk+1 ) = (sj , x1 )((sj , x1 ), x2 . . . xk+1 ). Since si k sj , it follows from (b) that = (si , x1 ) = (sj , x1 ). On the other hand, since (si , x1 ) k (sj , x1 ), it follows that = ((si , x1 ), x2 . . . xk+1 ) = ((sj , x1 ), x2 . . . xk+1 ). On combining (1)(4), we obtain (si , x1 x2 . . . xk+1 ) = (sj , x1 x2 . . . xk+1 ). (5) (4) (3) (2) (1)

Note now that (5) holds for every x1 x2 . . . xk+1 I k+1 . Hence si k+1 sj . The proof of the converse is = left as an exercise.

6.6. The Minimization Process In our earlier discussion concerning the design of nite state machines, we have not taken any care to ensure that the nite state machine that we obtain has the smallest number of states possible. Indeed, it is not necessary to design such a nite state machine with the minimal number of states, as any other nite state machine that performs the same task can be minimized. THE MINIMIZATION PROCESS. (1) Start with k = 1. By examining the rows of the transition table, determine 1-equivalent states. Denote by P1 the set of 1-equivalence classes of S (induced by 1 ). = k ). In view of Proposition 6A(b), we (2) Let Pk denote the set of k-equivalence classes of S (induced by = now examine all the states in each k-equivalence class of S, and use Proposition 6A(c) to determine Pk+1 , the set of all (k + 1)-equivalence classes of S (induced by k+1 ). = (3) If Pk+1 = Pk , then increase k by 1 and repeat (2). (4) If Pk+1 = Pk , then the process is complete. We select one state from each equivalence class.
Chapter 6 : Finite State Machines page 13 of 21

Discrete Mathematics

W W L Chen, 1991, 2008

Example 6.6.1. Suppose that I = O = {0, 1}. Consider a nite state machine with the transition table below: 0 s1 s2 s3 s4 s5 s6 0 1 0 0 1 1 1 1 0 0 0 0 0 0 s4 s5 s2 s5 s2 s1 1 s3 s2 s4 s3 s5 s6

For the time being, we do not indicate which state is the starting state. Clearly P1 = {{s1 }, {s2 , s5 , s6 }, {s3 , s4 }}. We now examine the 1-equivalence classes {s2 , s5 , s6 } and {s3 , s4 } separately to determine the possibility of 2-equivalence. Note at this point that two states from dierent 1-equivalence classes cannot be 2equivalent, in view of Proposition 6A(b). Consider rst {s2 , s5 , s6 }. Note that (s2 , 0) = s5 1 s2 = (s5 , 0) = and (s2 , 1) = s2 1 s5 = (s5 , 1). =

It follows from Proposition 6A(c) that s2 2 s5 . On the other hand, = (s2 , 0) = s5 1 s1 = (s6 , 0). = It follows that s2 2 s6 , and hence s5 2 s6 also. Consider now {s3 , s4 }. Note that = = (s3 , 0) = s2 1 s5 = (s4 , 0) = It follows that s3 2 s4 . Hence = P2 = {{s1 }, {s2 , s5 }, {s3 , s4 }, {s6 }}. We now examine the 2-equivalence classes {s2 , s5 } and {s3 , s4 } separately to determine the possibility of 3-equivalence. Consider rst {s2 , s5 }. Note that (s2 , 0) = s5 2 s2 = (s5 , 0) = and (s2 , 1) = s2 2 s5 = (s5 , 1). = and (s3 , 1) = s4 1 s3 = (s4 , 1). =

It follows from Proposition 6A(c) that s2 3 s5 . On the other hand, = (s3 , 0) = s2 2 s5 = (s4 , 0) = It follows that s3 3 s4 . Hence = P3 = {{s1 }, {s2 , s5 }, {s3 , s4 }, {s6 }}. Clearly P2 = P3 . It follows that the process ends. We now choose one state from each equivalence class to obtain the following simplied transition table: 0 s1 s2 s3 s6
Chapter 6 : Finite State Machines

and

(s3 , 1) = s4 2 s3 = (s4 , 1). =

1 1 0 0 0

0 s3 s2 s2 s1

1 s3 s2 s3 s6
page 14 of 21

0 1 0 1

Discrete Mathematics

W W L Chen, 1991, 2008

Next, we repeat the entire process in a more ecient form. We observe easily from the transition table that P1 = {{s1 }, {s2 , s5 , s6 }, {s3 , s4 }}. Let us label these three 1-equivalence classes by A, B, C respectively. We now attach an extra column to the right hand side of the transition table where each state has been replaced by the 1-equivalence class it belongs to. 1 = A B C C B B

0 s1 s2 s3 s4 s5 s6 0 1 0 0 1 1

1 1 0 0 0 0 0

0 s4 s5 s2 s5 s2 s1

1 s3 s2 s4 s3 s5 s6

Next, we repeat the information on the next-state function, again where each state has been replaced by the 1-equivalence class it belongs to. 1 = A B C C B B

0 s1 s2 s3 s4 s5 s6 0 1 0 0 1 1

1 1 0 0 0 0 0

0 s4 s5 s2 s5 s2 s1

1 s3 s2 s4 s3 s5 s6

0 C B B B B A

1 C B C C B B

Recall that to get 2-equivalence, two states must be 1-equivalent, and their next states must also be 1-equivalent. In other words, if we inspect the new columns of our table, two states are 2-equivalent precisely when the patterns are the same. Indeed, P2 = {{s1 }, {s2 , s5 }, {s3 , s4 }, {s6 }}. Let us label these four 2-equivalence classes by A, B, C, D respectively. We now attach an extra column to the right hand side of the transition table where each state has been replaced by the 2-equivalence class it belongs to. 1 = A B C C B B 2 = A B C C B D

0 s1 s2 s3 s4 s5 s6 0 1 0 0 1 1

1 1 0 0 0 0 0

0 s4 s5 s2 s5 s2 s1

1 s3 s2 s4 s3 s5 s6

0 C B B B B A

1 C B C C B B

Next, we repeat the information on the next-state function, again where each state has been replaced
Chapter 6 : Finite State Machines page 15 of 21

Discrete Mathematics

616

W W L Chen : Discrete Mathematics

W W L Chen, 1991, 2008

by the 2-equivalence class it belongs to. by the 2-equivalence class it belongs to. 0 0 0 0 1 1 0 0 0 0 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 s4 s4 s5 s5 s2 s2 s5 s5 s2 s2 s s1
1

s1 s1 s2 s2 s3 s3 s4 s4 s5 s5 s6 s6

1 1 s3 s3 s2 s2 s4 s4 s3 s3 s5 s5 s s6
6

= 1 =1 A A B B C C C C B B B B

0 1 0 1 C C C C B B B B B C B C B C B C B B B B A B A B

= 2 =2 A A B B C C C C B B D D

0 1 0 1 C C C C B B B B B C B C B C B C B B B B A D A D

Recall that to get 3-equivalence, two states must be 2-equivalent, and their next states must also be Recall that to get 3-equivalence, two states must be 2-equivalent, and their next states must also be 2-equivalent. In other words, if we inspect the new columns of our table, two states are 3-equivalent 2-equivalent. In other words, if we inspect the new columns of our table, two states are 3-equivalent precisely when the patterns are the same. Indeed, precisely when the patterns are the same. Indeed, P3 = {{s1 }, {s2 , s5 }, {s3 , s4 }, {s6 }}. P3 = {{s1 }, {s2 , s5 }, {s3 , s4 }, {s6 }}. Let us label these four 3-equivalence classes by A, B, C, D respectively. We now attach an extra column Let us label these four 3-equivalence classes by A, B, C, D respectively. We now attach an extra column to the right hand side of the transition table where each state has been replaced by the 3-equivalence to the right hand side of the transition table where each state has been replaced by the 3-equivalence class it belongs to. class it belongs to. 0 0 0 0 1 1 0 0 0 0 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 s4 s4 s5 s5 s2 s2 s5 s5 s2 s2 s1 s1 1 1 s3 s3 s2 s2 s4 s4 s3 s3 s5 s5 s6 s6 1 = =1 A A B B C C C C B B B B 0 1 0 1 C C C C B B B B B C B C B C B C B B B B A B A B 2 = =2 A A B B C C C C B B D D 0 1 0 1 C C C C B B B B B C B C B C B C B B B B A D A D 3 = =3 A A B B C C C C B B D D

s1 s1 s2 s2 s3 s3 s4 s4 s5 s5 s6 s6

It follows that the process ends. We now choose one state from each equivalence class to obtain the It follows that the process ends. We now choose one state from each equivalence class to obtain the simplied transition table as earlier. simplied transition table as earlier.

6.7. Unreachable States 6.7. Unreachable States There may be a further step following minimization, as it may not be possible to reach some of the states There may be a further step following minimization, as it may not be possible to reach some of the states of a nite state machine. Such states can be eliminated without aecting the nite state machine. of a nite state machine. Such states can be eliminated without aecting the nite state machine. We shall illustrate this point considering an example. We shall illustrate this point byby considering an example. Example 6.7.1. Note that in Example 6.6.1, we have not included any information on the starting Example 6.7.1. Note that in Example 6.6.1, we have not included any information on the starting state. If s1 is the starting state, then the original nite state machine can be described by the following state. If s1 is the starting state, then the original nite state machine can be described by the following state diagram: state diagram:
1,0 1,1 0,0

start

s1
0,1

s3
1,0 1,0

s2
0,1 0,1

0,0

s6
1,0

s4

0,0

s5
1,0

Chapter 6 : Finite State Machines

page 16 of 21

Discrete Mathematics

c WW Chapter 6 : Finite State MachinesL Chen, 1991, 2008 617 Chapter 6 : Finite State Machines 617

After minimization, the nite state machine can be described by the following state diagram: After minimization, the nite state machine can be described by the following state diagram: After minimization, the nite state machine can be described by the following state diagram: 1,0 start start s1 s1
0,1 0,1 0,0 1,1 0,0 1,1 1,0

s3
1,0

0,0 0,0

s2 s2

s3

0,1

1,0

0,1

s6
1,0

s6

1,0 It is clear that in both cases, if we start at state s1 , then it is impossible to reach state s6 . It follows It is clear that in both cases, if we start at state s1 , then it is impossible to reach state s6 . It follows that state s6 is essentially redundant, andat state s , then it is impossible obtain a nite stateIt follows It is clear that in both cases, if we start we may omit it. As a result, we obtain a nite state machine 1 that state s6 is essentially redundant, and we may omit it. As a result, we to reach state s6 . machine described by the transitionredundant, and we may omit it. As a result, we obtain a nite state machine that state by is essentially table described s6 the transition table described by the transition table 01 0 1 0 1 0 1 1 +s1 + 0 1 s0 s3 3 +s2 + 0 1 3 3 s 1+ 1 0 s2 s2 +s1 0 1 s3 s3 1 2 2 2 s3 0 0 s2 s3 s2 1 0 s2 s2 s3 0 0 s2 s3 s3 0 0 s2 s3 or the following state diagram: or the following state diagram: or the following state diagram: 1,0

start start

s1 s1

0,0 1,1 0,0 1,1

s3
1,0

0,0 0,0

1,0

s2 s2

s3

0,1

1,0

0,1

States such as s6 in Example 6.7.1 are said to be unreachable from the starting state, and can therefore besuch as swithout aecting theare said to machine, provided thatthe do not alter the and can States removed 6 in Example 6.7.1 nite state be unreachable from we starting state, starting state. Such unreachable states may be the nitebe unreachable provided starting do not and can therefore States such as s6 inwithout aecting removed state machine, from the that we state,afterwards if they therefore be removed Example 6.7.1 are said to prior to the Minimization process, or alter the starting stillremoved unreachableof the the niteremoved prior to the Minimization process, or the startingifstate. be remain without aecting may be state machine, provided that we do not alter afterwards they state. Such at the end statesprocess. However, if we design our nite state machines with care, then such remain should states may bearise. However,to the design our nite state machines withifcare, then Such unreachable end of the process. still states at the normally not removed prior if we Minimization process, or afterwards they still remain at should of the process. However, if we design our nite state machines with care, then such such statesthe end normally not arise. states should normally not arise. Problems for Chapter 6 Problems for Chapter 6 1. Consider the nite state machine M = (S, I, O, , , s1 ), where I = O = {0, 1}, described by the 1. state diagramnite state machine M = (S, I, O, , , s1 ), where I = O = {0, 1}, described by the Consider the below: state diagram below: 0,0 0,1 0,0 start start
0,0

s1 s1
0,0 0,0

1,0 1,0

0,1

s2 s2

1,1 1,1

1,0 0,0 s 1,0 3 0,0

s3

s4 s4
1,1

1,1 1,1

s5 s5

0,0

a) b) a) c) b) d) c)

Chapter 6 : Finite State Machines

d) Find an input string x I of minimal length and which satises (s5 , x) = s2 .

What is the output string for the input string1,1 110111? What is the last transition state? Repeat part output changing the starting state 110111?3 , s4 and s5 . last transition state? (a) by string for the input string to s2 , s What is the What is the Construct the transition table forstarting state to s , s , s and s . Repeat part (a) by changing the this machine. 2 3 4 5 Find an input string x table for this machine. Construct the transition I of minimal length and which satises (s5 , x) = s2 .
page 17 of 21

States such as s6 in Example 6.7.1 are said to be unreachable from the starting state, and can therefore be removed without aecting the nite state machine, provided that we do not alter the starting state. Such unreachable states may be removed prior to the Minimization process, or afterwards if they still remain at the end of the process. However, if we design our nite state machines L Chen, 1991,then c W W with care, 2008 Discrete Mathematics such states should normally not arise.

Problems for Chapter 6 Problems for Chapter 6 1. Consider the nite state machine M = (S, I, O, , , s1 ), where I = O = {0, 1}, described by the 1. Consider the nite state machine M = (S, I, O, , , s1 ), where I = O = {0, 1}, described by the state diagram below: state diagram below:
0,0 0,1 1,0 0,0 1,0

start

s1
0,0

s2
1,1

1,1

s3
0,0

s4
1,1

s5

618 a) What is the output string for the input string 110111? What is the last transition state? W W L Chen : Discrete Mathematics a) What is the output string for the input string 110111? What is the last transition state? b) Repeat part (a) by changing the starting state to s2 , s3 , s4 and s5 . b) Repeat part (a) by changing the starting state to s2 , s3 , s4 and s5 . c) Construct the transition table for this machine. c)d) Find an the transition table of minimal length and which satises (s , x) = s . Construct input string x I for this machine. 5 2 d) Find an input string |I| I5 and |O| = 2. 2. Suppose that |S| = 3, x = of minimal length and which satises (s5 , x) = s2 . a) How many elements does the set S I have? 2. Suppose that |S| = 3, |I| = 5 and |O| = 2. b) How many dierent functions : S I O can one dene? a) How many elements does the set S I have? b) c) How many dierent functions: : I can one dene? How many dierent functions S S I O S can one dene? c) d) How many dierent nite state machines M =one dene? , s1 ) can one construct? How many dierent functions : S I S can (S, I, O, , d) How many dierent nite state machines M = (S, I, O, , , s1 ) can one construct? 3. Consider the nite state machine M = (S, I, O, , , s1 ), where I = O = {0, 1}, described by the 3. Consider the nite state machine M = (S, I, O, , , s1 ), where I = O = {0, 1}, described by the transition table below: transition table below: 0 1 0 1 0 1 0 1 s 0 0 s s ss2 1 s2
1

0 0 1 1 1 1

ss1 ss2 1 2 s1 s2

a) Construct the state diagram for this machine. a) b) Determine the output strings this the input strings 111, 1010 and 00011. Construct the state diagram for for machine. b) Determine the output strings for the input strings 111, 1010 and 00011. 4. Consider the nite state machine M = (S, I, O, , , 1 ), where I = O = {0, 1}, described by the 4. Consider the nite state machine M = (S, I, O, , , ss1 ), where I = O = {0, 1}, described by the state diagram below: state diagram below:
0,0 0,0 1,0 0,0 1,0

start

s1
1,1

s2

s3
1,0

s6
0,0

1,0

s5
0,0

1,0

s4
0,0

a) b) c) d)

Find the output string for the input string 0110111011010111101. a) Find the output string for the input string 0110111011010111101. Construct the transition table for this machine. b) Construct the transition table for this machine. Determine all possible input strings which give the output string 0000001. c) Determine all possible input strings which give the output string 0000001. Describe in in words what this machine does. d) Describe words what this machine does.
page 18 of 21

Chapter 6 : Finite State Machines

5. Suppose that I = O = {0, 1}. Apply the Minimization process to each of the machines described by the following transition tables:

Discrete Mathematics

W W L Chen, 1991, 2008

5. Suppose that I = O = {0, 1}. Apply the Minimization process to each of the machines described by the following transition tables:

0 s1 s2 s3 s4 s5 s6 s7 s8 0 1 1 0 0 0 0 0

1 0 0 1 1 1 0 1 0

0 s6 s2 s6 s5 s4 s2 s3 s2

1 s3 s3 s4 s2 s2 s6 s8 s7 s1 s2 s3 s4 s5 s6 s7 s8

0 0 0 0 0 0 1 1 0

1 0 0 0 0 0 0 1 0

0 s6 s3 s2 s7 s6 s5 s4 s2

1 s3 s1 s4 s4 s7 s2 s1 s4

6. Consider the nite state machine M = (S, I, O, , , s1 ), where I = O = {0, 1}, described by the transition table below: 0 s1 s2 s3 s4 s5 s6 1 0 0 0 0 0 1 1 1 1 0 1 0 0 s2 s4 s3 s1 s5 s1 1 s1 s6 s5 s2 s3 s2

a) Find the output string corresponding to the input string 010011000111. b) Construct the state diagram corresponding to the machine. c) Use the Minimization process to simplify the machine. Explain each step carefully, and describe your result in the form of a transition table. 7. Consider the nite state machine M = (S, I, O, , , s1 ), where I = O = {0, 1}, described by the transition table below: 0 s1 s2 s3 s4 s5 s6 s7 s8 s9 1 0 0 0 0 0 0 0 0 1 1 1 1 0 1 0 0 1 0 0 s2 s4 s3 s1 s5 s1 s1 s5 s1 1 s1 s6 s5 s2 s3 s2 s2 s3 s2

a) Find the output string corresponding to the input string 011011001011. b) Construct the state diagram corresponding to the machine. c) Use the Minimization process to simplify the machine. Explain each step carefully, and describe your result in the form of a transition table.
Chapter 6 : Finite State Machines page 19 of 21

s6 s7 s8 s9 a) Find the Discrete Mathematics

0 0 0 0

0 0 1 0

s1 s1 s5 s1

s2 s2 s3 s2

output string corresponding to the input string 011011001011. W W L Chen, 1991, 2008 c b) Construct the state diagram corresponding to the machine. c) Use the Minimization process to simplify the machine. Explain each step carefully, and describe your result in the form of a transition table.

8. Consider the nite state machine M = (S, I, O, , , s1 ), where I = O = {0, 1}, described by the 8. Consider the nite state machine M = (S, I, O, , , s1 ), where I = O = {0, 1}, described by the state diagram below: state diagram below:
0,0 1,0 1,1 0,0

start

s1
1,1

s4
1,1

s7
0,1

0,0

0,1

s2
0,0

0,1

s5
0,1

1,0

s8

1,0

s3

1,1

s6
1,0

a) b) c) d)

Write down the output string corresponding to the input string 001100110011. Write down the transition table for the machine. Apply the Minimization process to the machine. Can we further reduce the number of states of the machine? If so, which states can we remove?

9. Suppose that I = O = {0, 1}. a) Design a nite state machine that will recognize the 2-nd, 4-th, 6-th, etc, occurrences of 1s in the input string (e.g. 0101101110). b) Design a nite state machine that will recognize the rst 1 in the input string, as well as the 2-nd, 4-th, 6-th, etc, occurrences of 1s in the input string (e.g. 0101101110). c) Design a nite state machine that will recognize the pattern 011 in the input string. d) Design a nite state machine that will recognize the rst two occurrences of the pattern 011 in the input string. e) Design a nite state machine that will recognize the rst two occurrences of the pattern 101 in the input string, with the convention that 10101 counts as two occurrences. f) Design a nite state machine that will recognize the rst two occurrences of the pattern 0100 in the input string, with the convention that 0100100 counts as two occurrences. 10. Suppose that I = O = {0, 1}. a) Design a nite state machine that will recognize the pattern 10010. b) Design a nite state machine that will recognize the pattern 10010, but only when the last 0 in the sequence pattern occurs at a position that is a multiple of 5. c) Design a nite state machine that will recognize the pattern 10010, but only when the last 0 in the sequence pattern occurs at a position that is a multiple of 7. 11. Suppose that I = {0, 1, 2} and O = {0, 1}. a) Design a nite state machine that will recognize the 2-nd, 4-th, 6-th, etc, occurrences of 1s in the input string. b) Design a nite state machine that will recognize the pattern 1021. 12. Suppose that I = O = {0, 1, 2}. a) Design a nite state machine every input string x1 . . . xk in b) Design a nite state machine every input string x1 . . . xk in
Chapter 6 : Finite State Machines

which gives the output string x1 x1 x2 x3 . . . xk1 corresponding to I k . Describe your result in the form of a state diagram. which gives the output string x1 x2 x2 x3 . . . xk1 corresponding to I k . Describe your result in the form of a state diagram.
page 20 of 21

Discrete Mathematics

W W L Chen, 1991, 2008

13. Suppose that I = O = {0, 1, 2, 3, 4, 5}. a) Design a nite state machine which inserts the digit 0 at the beginning of any string beginning with 0, 2 or 4, and which inserts the digit 1 at the beginning of any string beginning with 1, 3 or 5. Describe your result in the form of a transition table. b) Design a nite state machine which replaces the rst digit of any input string beginning with 0, 2 or 4 by the digit 3. Describe your result in the form of a transition table.

Chapter 6 : Finite State Machines

page 21 of 21

You might also like