Sunteți pe pagina 1din 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 6: : Finite State Machines 63 c W W L Chen, 1991, 2008 Chapter Finite State Machines 63

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

sj j s

to describe describe ( (s si, , x) = y and (si , x) = sj , then we can draw the state diagram below: to i 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

s 1 s1

s 2 s2
1, 1 1 ,1 0, ,0 0 0

0, ,0 0 0

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

+s s0+ + + 0 s 1 s1

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

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

Clearly s s0 is the the starting starting state. state. 0 is Clearly

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

Discrete 64 Mathematics WW

L Chen : Discrete Mathematics

W W L Chen, 1991, 2008

Remark. the examples that follow, we shall use words like Remark. InIn the examples that follow, we shall use words likepassive passiveand andexcited excitedto todescribe describethe the various variousstates statesof ofthe thenite nitestate statemachines machinesthat thatwe weare areattempting attemptingto toconstruct. construct. These Thesewords wordsplay playno norole role in in serious serious mathematics, mathematics, and and should should not not be be used used in in exercises exercises or or in in any any examination. examination. It It is is hoped hoped that thatby by using using such such words words here, here, the the exposition exposition will will be be a a little little more more light-hearted light-hearted and, and, hopefully, hopefully,aalittle littleclearer. clearer. Example that II == OO == {0 ,0 1 ,}and that we want to Example 6.2.1. 6.2.1. Suppose Suppose that { ,} 1 , and that we want todesign designaanite nitestate statemachine machinethat that recognizes the sequence pattern 11 in the input string x I . An example of an input string recognizes the sequence pattern 11 in the input string x I . An example of an input string x x I I and and its its corresponding corresponding output output string string yy O O is is shown shown below: below: x x= =10111010101111110101 10111010101111110101 yy= 00011000000111110000 = 00011000000111110000 Note Note that that the the output output digit digit is is 0 0 when when the the sequence sequence pattern pattern 11 11 is is not not detected detected and and 1 1 when when the the sequence sequence pattern pattern11 11is isdetected. detected. In Inorder orderto toachieve achievethis, this,we wemust mustensure ensurethat thatthe thenite nitestate statemachine machinehas hasat atleast least two two states, states, a a passive passive state state when when the the previous previous entry entry is is 0 0 (or (or when when no no entry entry has has yet yet been been made), made), and and an an excited excited state state when when the the previous previous entry entry is is 1. 1. Furthermore, Furthermore, the the nite nite state state machine machine has has to to observe observe the the following following and and take take the the corresponding corresponding actions: actions: (1) (1) If Ifit itis isin inits itspassive passivestate stateand andthe thenext nextentry entryis is0, 0,it itgives givesan anoutput output00and andremains remainsin inits itspassive passive state. state. (2) (2) If Ifit itis isin inits itspassive passivestate stateand andthe thenext nextentry entryis is1, 1,it itgives givesan anoutput output00and andswitches switchesto toits itsexcited excited state. state. (3) (3) If Ifit itis isin inits itsexcited excitedstate stateand andthe thenext nextentry entryis is0, 0,it itgives givesan anoutput output0 0and andswitches switchesto toits itspassive passive state. state. (4) (4) If Ifit itis isin inits itsexcited excitedstate stateand andthe thenext nextentry entryis is1, 1,it itgives givesan anoutput output11and andremains remainsin inits itsexcited excited state. state. It 11 the 22 the It follows follows that that if if we we denote denote by by ss the passive passive state state and and by by ss the excited excited state, state, then then we we have have the the state diagram below: state diagram below:
0 ,0 1 ,1 1 ,0 0 ,0

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

s1

s2

+ + + ss + 11 s 2 s2

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

0 1 0 1 s ss 11 s22 s 1 s1 ss 22

Example 6.2.2. 6.2.2. Suppose Suppose again that { 1 , and that we want to designaanite nitestate statemachine machine Example again that II == OO == {0 ,0 1,} ,}and that we want to design . An example of the same input that recognizes recognizes the the sequence sequence pattern pattern 111 111 in in the the input input string string x x I I that . An example of the same input and its corresponding output string y O string x x I I is shown shown below: below: string and its corresponding output string y O is x= =10111010101111110101 10111010101111110101 x =00001000000011110000 00001000000011110000 yy= In In order order to to achieve achieve this, this, the the nite nite state state machine machine must must now now have have at at least least three three states, states, aa passive passive state state when when the the previous previous entry entry is is 0 0 (or (or when when no no entry entry has has yet yet been been made), made), an an expectant expectant state state when when the the previous previoustwo twoentries entriesare are01 01(or (orwhen whenonly onlyone oneentry entryhas hasso sofar farbeen beenmade madeand andit itis is1), 1),and andan anexcited excited state state when when the the previous previous two two entries entries are are 11. 11. Furthermore, Furthermore, the the nite nite state state machine machine has has to to observe observe the the following following and and take take the the corresponding corresponding actions: actions:
Chapter 6 : Finite State Machines page 4 of 21

Discrete Mathematics

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

(1) If Ifit itis isin inits itspassive passivestate stateand andthe thenext nextentry entryis is0, 0,it itgives givesan anoutput output00and andremains remainsin inits itspassive passive (1) state. state. (2) If If it it is is in in its its passive passive state state and and the the next next entry entry is is 1, 1, it it gives gives an an output output 00 and and switches switches to to its its (2) expectant state. state. expectant (3) If If it it is is in in its its expectant expectant state state and and the the next next entry entry is is 0, 0, it it gives gives an an output output 00 and and switches switches to to its its (3) passive state. passive state. (4) If If it it is is in in its its expectant expectant state state and and the the next next entry entry is is 1, 1, it it gives gives an an output output 00 and and switches switches to to its its (4) excited state. state. excited (5) If Ifit itis isin inits itsexcited excitedstate stateand andthe thenext nextentry entryis is0, 0,it itgives givesan anoutput output0 0and andswitches switchesto toits itspassive passive (5) state. state. (6) If Ifit itis isin inits itsexcited excitedstate stateand andthe thenext nextentry entryis is1, 1,it itgives givesan anoutput output11and andremains remainsin inits itsexcited excited (6) state. state. If we we now now denote denote by by ss the passive passive state, state, by by ss the expectant expectant state state and and by by ss the excited excited state, state, If 11 the 22 the 33 the then we we have have the the state state diagram diagram below: below: then
0 ,0

start

s1

1 ,0 0 ,0

s2
1 ,0

0 ,0

s3
1 ,1

We then then have have the the corresponding corresponding transition transition table: table: We 0 1 0 1 0 0 0 0 0 0 0 0 0 1 0 1 0 1 0 1 s ss 11 s22 s s11 ss 33 s ss 11 s33

+ + + ss + 11 s s22 ss 33

Example 6.2.3. 6.2.3. Suppose Suppose again that { 1 , and that we want to designaanite nitestate statemachine machine Example again that II == OO == {0 ,0 1,} ,}and that we want to design , but only when the third 1 in the that recognizes the sequence pattern 111 in the input string x I that recognizes the sequence pattern 111 in the input string x I , but only when the third 1 in the sequence pattern pattern 111 111 occurs occurs at at a a position position that that is is a a multiple multiple of of 3. 3. An An example example of of the the same same input input string string sequence and its corresponding output string y O is shown below: x I x I and its corresponding output string y O is shown below: x= =10111010101111110101 10111010101111110101 x =00000000000000100000 00000000000000100000 yy= In order order to to achieve achieve this, this, the the nite nite state state machine machine must must as as before before have have at at least least three three states, states, aa passive passive In statewhen whenthe theprevious previousentry entryis is0 0(or (orwhen whenno noentry entryhas hasyet yetbeen beenmade), made),an anexpectant expectantstate statewhen whenthe the state previoustwo twoentries entriesare are01 01(or (orwhen whenonly onlyone oneentry entryhas hasso sofar farbeen beenmade madeand andit itis is1), 1),and andan anexcited excited previous state when when the the previous previous two two entries entries are are 11. 11. However, However, this this is is not not enough, enough, as as we we also also need need to to keep keep track track state of the the entries entries to to ensure ensure that that the the position position of of the the rst rst 11 in in the the sequence sequence pattern pattern 111 111 occurs occurs at at aa position position of immediately after after a a multiple multiple of of 3. 3. It It follows follows that that if if we we permit permit the the machine machine to to be be at at its its passive passive state state immediately only either either at at the the start start or or after after 3 3 k entries, entries, where where k k N N then it it is is necessary necessary to to have have two two further further states states only k ,, then to cater cater for for the the possibilities possibilities of of 0 0 in in at at least least one one of of the the two two entries entries after after aa passive passive state state and and to to then then to
Chapter 6 : Finite State Machines page 5 of 21

Discrete Mathematics 66 W W L Chen : Discrete Mathematics

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 We then then have have the the corresponding corresponding transition transition table: table:

s5

+ + +s s1 1+ s s2 2 s s3 3 s s4 4 s s5 5

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 s s4 4 s s5 5 s s1 1 s s5 5 s s1 1

1 1 s s2 2 s s3 3 s s1 1 s s5 5 s s1 1

Example 6.2.4. Suppose Suppose again thatII= =O O= ={{ ,1 andthat thatwe wewant want to to design design a a nite state machine again that 00 ,1 }} , ,and 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 67 Chapter 6 6 :: Finite Finite State State Machines Machines 67 c W W L Chen, 1991, 2008

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

s s1 1
1 ,, 0 1 0

s s2 2
0 ,, 0 0 0

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

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

s s4 4

+ + +s s1 1+ s s2 2 s s3 3 s s4 4
4

0 0 s s2 2 s s2 2 s s4 4 s s2 2
2

1 1 s s1 1 s s3 3 s s1 1 s s3 3
3

Example Suppose again that = O = and that we want to Example 6.2.5. Suppose again that II O = {{ 0 , } , to machine Example 6.2.5. 6.2.5. Suppose again that I= = O = {0 0,1 ,1 1} },,and andthat thatwe wewant want to design design a a nite nite state state machine machine , that recognizes the sequence pattern 0101 in the input string x I but only when the last 1 that recognizes the sequence pattern 0101 in the input string x I , but only when the last in the that recognizes the sequence pattern 0101 in the input string x I , but only when the last 1 1 in in the 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 x I and its corresponding output string y O is shown below: x I I and and its its corresponding corresponding output output string string y y O O is is shown shown below: below: x x = 10111010101111110101 x= = 10111010101111110101 10111010101111110101 y = 00000000100000000000 y = 00000000100000000000 y = 00000000100000000000 In order to achieve this, the nite state machine must have at least four states s ,s s4 ,s s5 and s s6 , In 3, 4, 5 and 6, In order order to to achieve achieve this, this, the the nite nite state state machine machine must must have have at at least least four four states states s s3 3, s 4, s 5 and s 6, 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 s and s , then we can have the state diagram as below: these 1 and 2, these two two states states by by s s1 and s s2 , then then we we can can have have the the state state diagram diagram as as below: below:
1 2

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

start start

s s1 1

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

s s2 2
1 ,, 1 1 1

0 ,, 0 0 0

s s4 4
0 ,, 0 0 0

1 ,, 0 1 0

s s5 5

0 ,, 0 0 0

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

68 Mathematics WW Discrete

68

W W L Chen : Discrete Mathematics

W W L Chen, 1991, 2008

We then then have have the the corresponding corresponding transition transition table: table: We 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 0 1 s 2 s 2 ss 22 22 s s 3 s 3 s33 33 s 4 s 1 ss 44 11 s 2 s 5 ss 22 55 s 6 s 3 ss 66 33 s 4 s 1 ss 44 11

+ + 1 + ss + 11 s 2 s22 3 ss 33 4 ss 44 5 ss 55 6 ss 66

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

start

s1 1

1, ,0 0 1

s2 2

Itnow nowremains remainsto tostudy studythe thesituation situationwhen whenwe wehave havethe thewrong wronginput inputat ateach eachstate. state. Naturally, Naturally,with withaa It 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 state ss We therefore therefore obtain obtain the the state state diagram diagram as as in in Example Example 6.2.1. 6.2.1. 1 to .. We 11 Example 6.3.2. 6.3.2.Suppose Suppose again that O 0, 1}that , and that we toa design a nite state Example again that I =I O= ={ 0, = 1},{and we want to want design nite state machine . Consider rst of all the machine that recognizes the sequence pattern 111 in the input string x I that recognizes the sequence pattern 111 in the input string x I . Consider rst of all the situation situation the pattern required occurs pattern occurs repeatly and without interruption. In other words, consider when the when required repeatly and without interruption. In other words, consider the the situation when the input string is 111111 . . . . To describe this situation, we have the following situation when the input string is 111111 . . . . To describe this situation, we have the following incomplete incomplete state diagram: state diagram: start s1 1
1, ,0 0 1

s2 2
1, ,0 0 1

s3 3
1, ,1 1 1

Itnow nowremains remainsto tostudy studythe thesituation situationwhen whenwe wehave havethe thewrong wronginput inputat ateach eachstate. state. As Asbefore, before,with withaa It 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 W W L Chen, 1991, 2008 Chapter 6 : Finite State Machines 69 Chapter 6 : Finite State Machines 69 Chapter 6 : Finite State Machines 69

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

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

s2 s1 start 1 ,0 s2 s 1 start 1 ,0 It now remains to study the situation when we have the wrong input each state. As before, with a s2 at each s start when we have 1 It now study the situation the input state. before, a wrong input, to the output always 0, so the question is each to determine nextwith state. It now remains remains to study theis situation when we only have unresolved the wrong wrong input at at state. As Asthe before, with a wrong input, the output is always 0, so the only unresolved question is to determine the next state. Suppose that the machine is at state s and the wrong 0 question is input made.at We thatAs if the the nextwith three It now remains to study the when we have theinput wrong each state. before, a wrong input, the output issituation always 0, the only unresolved is to note determine next state. 3 ,so Suppose that the machine is state s and the wrong input 0 is We note if the next three 3, entries are 111, then that pattern should be recognized. It follows that is ( sto 0) = that s1 . We now have the wrong input, the output isat always 0, the only unresolved determine next state. Suppose that the machine is at state s and the wrong input 0 question is made. made. We that if the the next three 3 , note 3 , so entries are 111, then that should be It that s , 0) = . We now have the following incomplete state diagram: Suppose that the machine is at state s3 , and the wrong input 0 is made. We note that if the next three entries are 111, then that pattern pattern should be recognized. recognized. It follows follows that ( ( s3 =s s1 now have the 3 , 0) 1 . We following incomplete state diagram: entries are 111, then state that pattern should be recognized. It follows that (s3 , 0) = s1 . We now have the following incomplete diagram: 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 s2 made. We note that the next s1 start inputs cannot contribute toisany pattern, we need to delay by steps We before returning s1 . Suppose that the machine at state s1 , so and the wrong input 0 two is made. note that theto next Suppose that the machine is at s , and the wrong input 0 is made. We note that the next now have the following incomplete state diagram: Suppose that the machine isany at state state s1 , and the wrong input 0 is made. We note that the next inputs cannot contribute to pattern, so we need to delay by two steps before returning to s1 . 1 inputs cannot contribute to so . inputs cannot contribute to any any pattern, pattern, so we we need need to to delay delay by by two two steps steps before before returning returning to to s s1 now have the following incomplete state diagram: 1. 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 s4 obtain s5 diagram as Example 6.2.3. Finally, it remains to examine (s2 , 0). We therefore the state 1 ,0 Finally, it remains to examine (s2 , 0). We therefore obtain the state diagram as Example 6.2.3. Note in Example 6.3.3, the input 0 at state s1diagram , we have actually introduced the Finally, it that remains to examine (in s2 ,dealing 0). We with therefore obtain the state 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 s and s . These extra states are not actually involved with positive identication of Note that 6.3.3, in dealing with the input 0 at state s1 , we have actually introduced the 4 in Example 5 desired pattern, but are essential indealing delaying the return to0one of the states already present. It follows Note that Example 6.3.3, in the input at state s with , we positive have actually introduced extra states s4 in and s5 . These extra states with are not actually involved identication of the Note that in Example 6.3.3, in dealing with the input 0 at state s11 , we have actually introduced the that at these extra states, the output should always be 0. However, we have to investigate the situation extra states s4 and extra states are not actually involved positive identication of the desired pattern, but sare essential in delaying the return to one of the with states already present. It follows 5 . These extra states s4 and s5 . These extra states are not actually involved with positive identication of the for input 0 and 1. desired pattern, but are essential in delaying the return one of the already present. follows that at these extra states, the output should always be 0.to However, westates have to investigate the It situation 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 at the these extra states, the output always be 0. details. However, we have to advised investigate the situation two examples, we do should not include all the Readers are to look carefully for input 0 next and 1. for input 0 and 1. at the and examples, ensure that understand the underlying arguments. The main is to In argument the next two wethey do not include all the details. Readers are advised to question look carefully In argument the next two wethey do not include all the details. Readers are advised to question look carefully at the and examples, ensure that understand the underlying arguments. The main is to Chapter 6 : Finite State Machines page 9 of 21 at the argument and ensure that they understand the underlying arguments. The main question 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 ensure that they understand the underlying arguments. The main question is to recognize that a is wrong input, stillwhere be able tofth recover part of a pattern. For example, if the pattern wewith want 01011 but we we gotmay 01010, the input is incorrect, then the last three recognize that with a wrong input, we may still be able to recover part of a pattern. For example, if the pattern we want is 01011 but form we got where the fth input is incorrect, then last three inputs in 01010 can still possibly the01010, rst three inputs of the pattern 01011. If one the is optimistic, the pattern we want is 01011 but we got 01010, where the fth input is incorrect, then the last three inputs in 01010 can still form the are rst three inputs of the pattern 01011. If one is optimistic, one might hope that the possibly rst seven inputs 0101011. inputs in 01010 can still possibly form theare rst three inputs of the pattern 01011. If one is optimistic, one might hope that the rst seven inputs 0101011. 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 IO ==O { ,the 1}, that and we that we want design a nite state machine that recognizes the sequence pattern 0101 in input string xto I . to Consider of all the Example 6.3.4. Suppose again that I = {0= , 1} ,0and want design a nite rst state machine machine that recognizes the pattern sequence pattern 0101 in the input string x I . In Consider rst all the situation when the repeatly and without interruption. other words, consider that recognizes the required sequence patternoccurs 0101 in the input string x I . Consider rst of all the of situation situation the required pattern occurs repeatly without interruption. Inwe other words, consider the situation when the input string is 010101 . . .without . and To describe this situation, have the following when thewhen required pattern occurs repeatly and interruption. In other words, consider the the situation when the input string is 010101 . . . . To describe this situation, we have the following incomplete state diagram: situation when the input string is 010101 . . . . To describe this situation, we have the following incomplete incomplete state diagram: state diagram: 0 ,0 s1 s2 start 0 ,0 s1 s2 start
1 ,0 1 ,0

s3 s3

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

s2 s2

s4 s4
0 ,0 0 ,0

1 ,0 1 ,0

s5 s5

s6 s6 It now now remains remains to to study study the the situation situation when when we we have have the the wrong wrong input input at at each each state. state. As As before, before, with with a a It It now remains study theis situation when we only have unresolved the wrong input at state. Asthe before, a wrong input, to the output is always 0, 0, so the the only unresolved question is each to determine determine the nextwith state. wrong input, the output always so question is to next state. wrong input, the iss always only question to determine the next state. Recognizing that ( (output s3 , 1) 1) = = s1 , ( (s s4 ,0, 0)so = the s2 , ( (s s5 ,unresolved 1) = =s s3 and ( (s s6 , 0) 0)is = s4 we therefore therefore obtain the Recognizing that s 0) = s 1) = s ,, we obtain the 3, 1, 4, 2, 5, 3 and 6, 4 Recognizing that ( s , 1) = s , ( s , 0) = s , ( s , 1) = s and ( s , 0) = s , we therefore obtain the state diagram diagram as as Example Example 6.2.5. state 6.2.5. 3 1 4 2 5 3 6 4 state diagram as Example 6.2.5. 6.4. Delay 6.4. DelayMachines 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 = x1digital x2 x3 . .devices. ., the machine will We now consider a machines, type of nite state machines that is useful in the design These will are called k -unit delay where k N. Corresponding to the input x = of x1 x2 x3 . . ., the machine 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 W W L Chen, 1991,611 2008 Chapter 6 : Finite State Machines

give give the the output 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 is that if we to add k 0sk at the of the then the same An important point note is that if want we want to add 0s at front the front ofstring, the string, then the result can be obtained by feeding the string into a 1-unit delay machine, then feeding the output obtained same result can be obtained by feeding the string into a 1-unit delay machine, then feeding the output into the same repeating process. It process. follows that we need only investigate delay obtained into machine, the same and machine, and this repeating this It follows that we need only1-unit investigate machines. 1-unit delay machines. Example 6.4.1. Suppose that II= 00 ,1 }} . . Then Example 6.4.1. Suppose that ={{ ,1 Thenaa1-unit 1-unit delay delay machine machine can can be be described described by by the the state state diagram below: diagram below:
0 ,0

s2
0 ,0

start

s1
1 ,0

0 ,1

1 ,0

s3
1 ,1

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

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

general, may contain more than elements.Any Anyattempt attemptto to construct construct a a 1-unit 1-unit delay machine In In general, II may contain more than 22 elements. 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 Suppose that = {, 01 ,, 12 ,, 23 ,, 34 ,, 45 ,} 5. }.Necessarily, Necessarily,there theremust mustbe bea a starting starting state state s1 1 which 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, be a state that will delay the input 1. To be precise, we want the state s3 to Next, we we let let s s3 to satisfy satisfy 3 be a state that will delay the input 1. To be precise, we want the state s3 the following requirements: the following requirements: (1) . (1) For For every every state state s s S S, , we we have have ( (s, s, 1) 1) = =s s3 3. (2) . (2) For For every every state state s s S S and and every every input input x x I I\ \{ {1 1} }, , we we have have ( (s, s, x x) )= =s s3 3. (3) For every input x I , we have ( s , x ) = 1. 3 (3) For every input x I , we have (s3 , x) = 1. Similarly, , s5, and s7 be ,s s6 be the the states states that that will will delay delay the the inputs inputs 2, 2, 3, 3, 4 4 and and 5 5 respectively. respectively. Similarly, we we let let s s4 4 , s5 6 and s7 Let us look more closely at the state s . By requirements (1) and (2) above, the machine arrives 2 Let us look more closely at the state s2 . By requirements (1) and (2) above, the machine arrives at at state state s precisely when the previous input is 0; by requirement (3), the next output is 0. This means 2 s2 precisely when the previous input is 0; by requirement (3), the next output is 0. This means that that the the previous previous input input must must be be the the same same as as the the next next output, output, which which is is precisely precisely what what we we are are looking looking for. 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. k1 sj . Then there exists x = x1 x2 . . . xk1 I k1 such that, writing (si , x) = (b) Suppose that si = 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 ). (2) Let Pk denote the set of k -equivalence classes of S (induced by =k ). In view of Proposition 6A(b), we 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 also. Consider now {s3 , s4 }. Note that =2 s6 , and hence s5 (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 the 2-equivalence 2-equivalence class class it it belongs belongs to. to. by 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 s 4 s5 s 5 s2 s 2 s5 s 5 s2 s 2 s1 s
1

s1 s 1 s2 s 2 s3 s 3 s4 s 4 s5 s 5 s6 s 6

1 1 s3 s 3 s2 s 2 s4 s 4 s3 s 3 s5 s 5 s6 s
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 C B B C B C B B B B A B B A

=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 C B B C B C B B B B A D D A

Recall that that to to get get 3-equivalence, 3-equivalence, two two states states must must be be 2-equivalent, 2-equivalent, and and their their next next states states must must also also be be Recall 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 when the the patterns patterns are are the the same. same. Indeed, Indeed, precisely P3 = {{ {{s s1 }, ,{ {s s2 ,s s5 }, ,{ {s s3 ,s s4 }, ,{ {s s6 }}. . 3 = 1} 2, 5} 3, 4} 6 }} P Let us us label label these these four four 3-equivalence 3-equivalence classes classes by by A, A, B, B, C, C, D D respectively. respectively. We We now now attach attach an an extra extra column column Let 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 it belongs belongs to. to. class 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 4 s s5 5 s s2 2 s s5 5 s s2 2 s s1 1 s 1 1 s3 3 s s2 2 s s4 4 s s3 3 s s5 5 s s6 6 s =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 C B B C B C B B B B A B B A =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 C B B C B C B B B B A D D A =3 3 = A A B B C C C C B B D D

s1 1 s s2 2 s s3 3 s s4 4 s s5 5 s s6 6 s

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

6.7. Unreachable Unreachable States 6.7. States There may may be be a a further further step step following following minimization, minimization, as as it it may may not not be be possible possible to to reach reach some some of of the the states states There of a nite state machine. Such states can be eliminated without a ecting 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 example. We shall illustrate this point byby considering anan example. Example 6.7.1. Notethat thatin inExample Example6.6.1, 6.6.1, we we have have not not included included any any information information on on the the starting starting Example 6.7.1. Note state. If s is the starting state, then the original nite state machine can be described by the following 1 is the starting state, then the original nite state machine can be described by the following state. If s1 state diagram: diagram: state
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 W W L Chen, 1991, 2008 Chapter 6 : Finite State Machines 617 Chapter 6 : Finite State Machines 617

After minimization, minimization, the the nite nite state state machine machine can can be be described described by by the the following following state state diagram: diagram: After 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 s is essentially redundant, and we may s omit it. it As a result, we obtain a nite machine 6 It is clear both cases, if we start state to reach state state s6 . It follows 1 , then that state that s6 is in essentially redundant, andat we may omit it. Asisa impossible result, we obtain a nite state machine described by the transition table that state s is essentially redundant, and we may omit it. As a result, we obtain a nite state machine 6 the transition table described by described by the transition table 1 1 0 0 0 1 0 1 1 +s1 + 0 1 s0 s 3 3 +s2 0 0 1 1+ 3 s2 3 1 s 2 +s1 + 0 1 s3 s3 1 0 2 2 s3 2 s 0 s 3 2 s2 1 0 s2 s2 s3 0 0 s2 s3 s3 0 0 s2 s3 or the following state diagram: or the the following following state state diagram: diagram: or 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 removed aecting the nite state machine, provided that we do not alter the starting States as swithout 6 in Example 6.7.1 are said to be unreachable from the starting state, and can state. Such unreachable states may be removed prior to the Minimization process, or afterwards if they States such as s in Example 6.7.1 are said to be unreachable from the starting state, and can 6 therefore be removed without a ecting the nite state machine, provided that we do not alter thetherefore starting still remain at the end of the process. However, if we design our nite state machines with care, then 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 ifstate. they such states should normally not arise. Such unreachable states may be removed prior to the Minimization process, or afterwards if they still still remain at the end of the process. However, if we design our nite state machines with care, then remain at the end of the process. However, if we design our nite state machines with care, then such such states should 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 diagram below: 1. state Consider the nite state machine M = (S , I , O, , , s1 ), where I = O = {0, 1}, described by the 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 .

,1 What is the output string for the input string1110111? What is the last transition state? Repeat part (a) by changing the starting state to s , s s4 and 2 3 5 . last transition state? What is the output string for the input string 110111? , What is s the Construct the transition table for this machine. Repeat part (a) by changing the starting state to s2 , s3 , s4 and s5 . Find an input x table I of for minimal length and which satises (s5 , x) = s2 . Construct the string transition this machine. 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 with care, then c WWL Chen, 1991, 2008 Discrete Mathematics such states should normally not arise.

Problems for Chapter 6 Problems for Chapter 6 1. Consider Consider the the nite nite state state machine machine M M = =( (S S, ,I I, ,O O, , , , s1), ), where where I I= =O O= ={ {0 0, ,1 1} }, , described described by by the the 1. , ,s 1 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 Wthe W output L Chenstring : Discrete Mathematics for the input string 110111? What is the last transition state? 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) Construct transition forminimal this machine. Find an the input string x table I of length and which satises (s5 , x) = s2 . d) Find an input string x I of minimal length and which satises (s5 , x) = s2 . 2. Suppose that |S| = 3, |I| = 5 and |O| = 2. 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? How many dierent functions SI I S can one dene? b) c) How many dierent functions : S : O can one dene? How many dierent nite state =one (S , dene? I , O, , , s1 ) can one construct? c) d) How many dierent functions : Smachines I SM can 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 0 s 0 1 0 s 1 s ss 12 s2
1

0 1 0 1 1 1

ss s 11 s 22 s1 s2

a) Construct the state diagram for this machine. a) b) Construct the the state diagram for this machine. Determine output strings for the input strings 111, 1010 and 00011. b) Determine the output strings for the input strings 111, 1010 and 00011. 4. Consider Consider the the nite nite state state machine machine M M= = (( S I O ,, ,ss ), where where I I= =O O= ={ { 0 1 } described by by the the 4. S ,,I ,,O ,,, ), 0 ,,1 } ,, described 11 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 thethe output string for for thethe input string 0110111011010111101. a) Find output string input string 0110111011010111101. Construct thethe transition table for for this machine. b) Construct transition table this machine. Determine all all possible input strings which give thethe output string 0000001. c) Determine possible input strings which give output string 0000001. Describe in words what this machine does. d) Describe in 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. c W W L Chen, 1991, 2008 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. ), where I = O = {0, 1}, described by the 8. Consider Consider the the nite nite state state machine machine M M= =( (S S, ,I I, ,O O, , , , , , s s1 1 ), 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

S-ar putea să vă placă și