Finite State Machines with Output

8
Computation Theory 1 A.L. Mustafa Ghanem Saeed 1 | Page Computer Science Department Collage of Science Cihan University Finite State Machines with Output (Mealy and Moore Machines) We shall investigate two different models for FA's with output capabilities; these are Moore machine and Mealy machine. Moore Machines: A Moore machine M is the 5 tuple M = (Q,, Г, Ϩ, q0) where 1. A finite set of states q0, q1, q2, …, where q0 is designed as the start state. 2. An alphabet of letters for forming the input string Σ= { a, b, c, …}. 3. An alphabet of possible output characters Г = { x, y, z, …}. 4. A transition function Ϩ as in table that shows for each state and each input letter what state is reached next. 5. Start state as q0. An output table that shows what character from Г is printed by each state that is entered. A Moore machine does not define a language of accepted words, since every input string creates an output string and there is no such thing as a final state. The processing is terminated when the last input letter is read and the last output character is printed. Example 1: Input alphabet: Σ = {a, b} Output alphabet: Г = {0, 1} Names of states: q0, q1, q2, q3. (q0 = start state) Q Ϩ(q, a) Ϩ(q, b) Г(Q) q0 q1 q3 1 q1 q3 q1 0 q2 q0 q3 0 q3 q1 q2 1 Sol: The Moore machine is:

Transcript of Finite State Machines with Output

Page 1: Finite State Machines with Output

Computation Theory 1 A.L. Mustafa Ghanem Saeed

1 | P a g e

Computer Science Department Collage of Science Cihan University

Finite State Machines with Output (Mealy and Moore Machines)

We shall investigate two different models for FA's with output capabilities; these are Moore machine and Mealy machine.

Moore Machines: A Moore machine M is the 5 tuple M = (Q,, Г, Ϩ, q0) where

1. A finite set of states q0, q1, q2, …, where q0 is designed as the

start state. 2. An alphabet of letters for forming the input string Σ= { a, b, c, …}. 3. An alphabet of possible output characters Г = { x, y, z, …}.

4. A transition function Ϩ as in table that shows for each state and each input letter what state is reached next.

5. Start state as q0. An output table that shows what character from Г is printed by each state

that is entered. A Moore machine does not define a language of accepted words, since

every input string creates an output string and there is no such thing as a final state. The processing is terminated when the last input letter is read

and the last output character is printed.

Example 1:

Input alphabet: Σ = {a, b} Output alphabet: Г = {0, 1} Names of states: q0, q1, q2, q3. (q0 = start state)

Q Ϩ(q, a) Ϩ(q, b) Г(Q)

q0 q1 q3 1

q1 q3 q1 0 q2 q0 q3 0

q3 q1 q2 1

Sol: The Moore machine is:

Page 2: Finite State Machines with Output

Computation Theory 1 A.L. Mustafa Ghanem Saeed

2 | P a g e

Computer Science Department Collage of Science Cihan University

Note1: The two symbols inside the circle are separated by a slash "/", on the

left side is the name of the state and on the right is the output from

that state. If the input string is abab to the Moore machine then the output will

be 10010.

Note 2: A Moore machine is very similar to a Finite Automaton (FA), with a few key differences:

• It has no final states. • It does not accept or reject input; instead, it generates output from

input. • Moore machines cannot have nondeterministic states.

Every input gives output not if word belongs to the machine or language

like FA In each state we stop we have to print out what inside that state

(it's content) so the output will be more than input by one because we start with start state and print out its content before we trace the input

string

Example 2: construct a Moore machine that take set of all strings over

Alphabet {a, b} as input and print 1 as output for every occurrence of

'aab' as substring.

Sol: Input alphabet: Σ = {a, b}

Output alphabet: Г = {0, 1}

Page 3: Finite State Machines with Output

Computation Theory 1 A.L. Mustafa Ghanem Saeed

3 | P a g e

Computer Science Department Collage of Science Cihan University

Input string aaababbaabb

State q0q1q2q2q3q1q0q0q1q2q3q0 Output 000010000010

this machine gives 1 after each aab aabaabaaababaab

0001001000100001

Note: Moore machine use as a string recognizer to give us a mark (1) after

each substring so we design a machine put 0 in all states except the one after the one represent end of substring aab

The output of the machine contains 1 for each occurrence of the

substring aab found in the input string.

Example 3: Construct a Moore machine that take set of all strings over

Alphabet {0, 1} as input and produce 'A' as output if input end with (10) or produce 'B' as output if input end with (11) otherwise produce 'C' .

Sol: Input alphabet: Σ = {0, 1}

Output alphabet: Г = {A, B, C}

Exam

Page 4: Finite State Machines with Output

Computation Theory 1 A.L. Mustafa Ghanem Saeed

4 | P a g e

Computer Science Department Collage of Science Cihan University

ple 4: Construct a Moore machine that take set of all strings over

alphabet {a, b} as input and produce '1' as output if number of a's in string mod 3=0 otherwise produce '0'.

Sol: Input alphabet: Σ = {a, b} Output alphabet: Г = {0, 1}

Example 5: Construct a Moore machine that outputs a binary string that

contains a 1 for every double letter substring in an input string composed of a’s and b’s. For example if abba is the input string, 0010 is the

corresponding output.

Sol: Input alphabet: Σ = {a, b}

Output alphabet: Г = {0, 1}

Page 5: Finite State Machines with Output

Computation Theory 1 A.L. Mustafa Ghanem Saeed

5 | P a g e

Computer Science Department Collage of Science Cihan University

Mealy Machines: A Mealy machine M is the 5 tuple, M = (Q, , ,Г, Ϩ, q0) where:

1. A finite set of states q0,q1,q2,…, where q0 is designated the start state

2. An alphabet Σ of input letters 3. An alphabet Г of output characters

4. A finite set of transitions that indicate, for each state and letter of the input alphabet, the state to go to next and the character that is

output.

Note: There are no accept states in Moore and Mealy machine because

they are not a language recognizer, they are an output producer. Its output will be the same length as its input.

Example 1:

Input alphabet: Σ = {a, b} Output alphabet: Г = {0, 1}

Names of states: q0, q1, q2, q3. (q0 = start state)

Q Ϩ(q, a) Г(q, a) Ϩ(q, b) Г(q, b)

q0 q1 0 q3 0

q1 q3 1 Q2 1 q2 q3 0 q3 1

q3 q3 1 q0 1

Sol: The Mealy machine is:

Page 6: Finite State Machines with Output

Computation Theory 1 A.L. Mustafa Ghanem Saeed

6 | P a g e

Computer Science Department Collage of Science Cihan University

Quick review for Binary system complements

As the binary system has base r = 2. So the two types of complements for the binary system are 2's complement and 1's complement.

1's complement

The 1's complement of a number is found by changing all 1's to 0's and

all 0's to 1's. This is called as taking complement or 1's complement. Example of 1's Complement is as follows.

2's complement

The 2's complement of binary number is obtained by adding 1 to the

Least Significant Bit (LSB) of 1's complement of the number.

2's complement = 1's complement + 1

Example of 2's Complement is as follows.

Note: Two’s complement is especially important because it allows us to represent signed

numbers in binary.

Page 7: Finite State Machines with Output

Computation Theory 1 A.L. Mustafa Ghanem Saeed

7 | P a g e

Computer Science Department Collage of Science Cihan University

Two’s complement also provides an easier way to subtract numbers using

addition instead of using the longer.

Example 2:

The following Mealy machine prints out the 1's complement of an input bit string.in other words it flips each digit from a 0 to a 1 or from a 1 to a

0.

Note: If the input is 001010, the output is 110101. This is a case where

the input alphabet and output alphabet are both {0, 1}.

Example 3:

Construct a Mealy machine that take set of all strings over alphabet {0, 1} as input and produce 'E' as output if number of 1's in string mod 2=0

produce 'O' as output if number of 1's in string mod 2=0.

Sol: Input alphabet: Σ = {0, 1}

Output alphabet: Г = {E, O}

Note: The machine represented in in example 2, outputs an E if the

number of 1s read so far is even and an O if it is odd; for example, the translation of 11100101 is OEOOOEEO.

Example 4:

Construct a Mealy machine that take set of all strings over alphabet {0, 1}

and make 2's complement of an input bit string in binary number.

Sol: Input alphabet: Σ = {0, 1}

Output alphabet: Г = {0,1}

Page 8: Finite State Machines with Output

Computation Theory 1 A.L. Mustafa Ghanem Saeed

8 | P a g e

Computer Science Department Collage of Science Cihan University

Note: The machine represented in in example 4, if the input is 1010100,

the output is 0101100. This is a case where the input alphabet and output alphabet are both {0, 1}.

Mealy and Moore machines are equivalent If Mealy machine capture a model, so does a Moore machine

Moore model might require more states to model a system but

simpler to understand

Mealy model might require less states to model but might be more

complex to understand

Note: All examples in Moore machine can be solve in mealy Machine

and vris versa because Mealy and Moore machines are equivalent, you have to try solving them as Homework.