0% found this document useful (0 votes)
18 views36 pages

TCS Lect 9 - 10 FSM With Output

Uploaded by

vanshikaedu0105
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views36 pages

TCS Lect 9 - 10 FSM With Output

Uploaded by

vanshikaedu0105
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 36

FSM with Output

Prepared By: Mr. Vaibhav Ambhire


Content
• Definition of Moore Machine
• Definition of Mealy Machine
• Examples

Prepared By: Mr. Vaibhav Ambhire


FSM with Output
• DFA and NFA are machines which specifies output 'YES' or 'NO' by reaching to
Final State or Non-Final State respectively

• Sometimes it is necessary to specify output other than Yes or No

• Hence we have Finite Automata with output

• There are two types of Machines:


1. Moore Machine

2. Mealy Machine

Prepared By: Mr. Vaibhav Ambhire


Moore Machine
• Next state is decided by Current State and Current input symbol

• The Output symbol depends only on Present State of the machine

• Formal Definition:

M = ( Q , Σ , Δ , δ , λ, q0 )

Where Q is a finite set of internal states

Σ is a finite set of symbols called the input alphabet

Δ is Output Alphabet

δ : Q X Σ → Q is a Transition Function (State Function)

q0 is an initial state q0 ε Q

λ : Q → Δ is a output Function Prepared By: Mr. Vaibhav Ambhire


Moore Machine 1

• Given Moore Machine can be described as


• M = ( { q0, q1, q2 }, { 0, 1 }, { 0, 1 }, δ , λ, q0 ) 0
q0 / 1 q1 / 1
• Transition Table:

Current Input Input Output 1


State 0 1 λ 0
q0 q1 q2 1 1

q1 q2 q1 1 q2 / 0
q2 q2 q0 0

Output Function:
λ ( q0 ) = 1 0

λ ( q1 ) = 1
λ ( q2 ) = 0 Prepared By: Mr. Vaibhav Ambhire
Moore Machine 1

• Given Moore Machine can be described as


• M = ( { q0, q1, q2 }, { 0, 1 }, { 0, 1 }, δ , λ, q0 ) 0
q0 / 1 q1 / 1

Transition Function ( δ ):
1
δ ( q0 , 0 ) = q1 δ ( q0 , 1 ) = q2 0
δ ( q1 , 0 ) = q2 δ ( q1 , 1 ) = q1 1

δ ( q2 , 0 ) = q2 δ ( q2 , 1 ) = q0 q2 / 0

• If the length of the input string is n


Then the length of output string is n + 1
0

For given machine if input is: 0 1 1 0


Then output is: 1 1 1 1 0 Prepared By: Mr. Vaibhav Ambhire
Mealy Machine
• The Output symbol depends on Present State and Present Input Symbol of the machine

• Formal Definition:

M = ( Q , Σ , Δ , δ , λ, q0 )

Where Q is a finite set of internal states

Σ is a finite set of symbols called the input alphabet

Δ is Output Alphabet

δ : Q X Σ → Q is a Transition Function (State Function)

q0 is an initial state q0 ε Q

λ : Q X Σ → Δ is a output Function Prepared By: Mr. Vaibhav Ambhire


Mealy Machine 0/0

• Given Mealy Machine can be described as


• M = ( { q0, q1, q2 }, { 0, 1 }, { 0, 1 }, δ , λ, q0 ) 1/0

• Transition Table: q0 q1

Input 0 Input 1 0/1


Current 1/1
State
State Output State Output 1/0
q2
q0 q2 1 q1 0
q1 q1 0 q2 1
q2 q2 0 q0 0
Output Function:
λ ( q0 , 0 ) = 1 λ ( q0 , 1 ) = 0 0/0
λ ( q1, 0 ) = 0 λ ( q1 , 1 ) = 1
λ ( q2, 0 ) = 0 λ ( q2 , 1 ) = 0 Prepared By: Mr. Vaibhav Ambhire
Examples on Moore Machine
0
Ex. 1 Design a Moore machine to generate 1's complement of given binary number

Solution:
0
For 1's complement if input is 0 then output will be 1.
q0 / 0 q1 / 1
If input is 1 then output will be 0.

Transition Diagram for Moore Machine can be drawn as 0


1 1
Transition Table: Above FSM can be represented as
q2 / 0
Current Input Input Output M = ( Q , Σ , Δ , δ , λ, q0 )
State 0 1 λ Where Q = { q0, q1, q2 }
q0 q1 q2 1
Σ = { 0, 1 }
q1 q1 q2 1
Δ = { 0, 1 } 1
q2 q1 q2 0
q0 = q0
δ and λ are shown in transition table
Prepared By: Mr. Vaibhav Ambhire
Examples on Moore Machine
0
Ex. 1 Design a Moore machine to generate 1's complement of given binary number

Solution:
0
Simulation for the input: 1 1 0 1
q0 / 0 q1 / 1

0
1 1
Input 1 1 0 1
State q0 q2 q2 q1 q2 q2 / 0
Output 0 0 0 1 0

Prepared By: Mr. Vaibhav Ambhire


Examples on Moore Machine
Ex. 2 Design a Moore machine for binary number input sequence such that if it has a substring 101 then machine
outputs A, if input has substring 110, it outputs B, otherwise it gives output C

Solution:

Partial Transition Diagram for Moore Machine can be drawn as

1 0 1
q0 / C q1 / C q2 / C q3 / A

1
0
q4 / C q5 / B

Prepared By: Mr. Vaibhav Ambhire


Examples on Moore Machine
Ex. 2 Design a Moore machine for binary number input sequence such that if it has a substring 101 then machine
outputs A, if input has substring 110, it outputs B, otherwise it gives output C

Solution:

Transition Diagram for Moore Machine can be drawn as Transition Table:


0
0 0 State/
0 1
Output
Input λ
1 0 1 q0 q0 q1 C
q0 / C q1 / C q2 / C q3 / A q1 q2 q4 C
q2 q0 q3 C
1 1 q3 q2 q4 A
q4 q5 q4 C
0 1
q4 / C q5 / B q5 q0 q3 B

0
Prepared By: Mr. Vaibhav Ambhire
Examples on Moore Machine
Ex. 2 Design a Moore machine for binary number input sequence such that if it has a substring 101 then machine
outputs A, if input has substring 110, it outputs B, otherwise it gives output C

Solution:

Transition Diagram for Moore Machine can be drawn as


0
0 0 Above FSM can be represented as
M = ( Q , Σ , Δ , δ , λ, q0 )
1 0 1
q0 / C q2 / C q3 / A Where Q = { q0, q1, q2, q3, q4, q5 }
q1 / C
Σ = { 0, 1 }
Δ = { A, B, C }
1 1
q0 = q0
0 1 δ and λ are shown in transition table
q4 / C q5 / B
1

0
Prepared By: Mr. Vaibhav Ambhire
Examples on Moore Machine
Ex. 2 Design a Moore machine for binary number input sequence such that if it has a substring 101 then machine
outputs A, if input has substring 110, it outputs B, otherwise it gives output C

Solution:

Simulation for String: 1 0 1 1 0 0


0
0 0

1 0 1 Input State output


q0 / C q1 / C q2 / C q3 / A q0 C
1 q1 C

1 1 0 q2 C
1 q3 A
0 1
q4 / C q5 / B 1 q4 C

1 0 q5 B
0 q0 C
0
Prepared By: Mr. Vaibhav Ambhire
Examples on Mealy Machine 0/0

Ex. 3 Design a Mealy machine to generate 2's complement of given binary number q0
Solution:
1/ 1
For designing 2's complement of binary number, we assume that input is read from LSB to
MSB

Suppose Given Number is 1 0 1 1 q1


2's complement: 0 1 0 1

Suppose Given Number is 1 0 1 0

2's complement: 0 1 1 0 1/0


0/1
We can observe that to get output

We keep binary input as it is until we read first 1

Keep that 1 as it is and then change remaining 1's by 0's and 0's by 1's

Transition Diagram can be drawn as

Prepared By: Mr. Vaibhav Ambhire


Examples on Mealy Machine 0/0

Ex. 3 Design a Mealy machine to generate 2's complement of given binary number q0
Solution:
1/ 1
Transition Table:

Above FSM can be represented as


q1
Input 0 Input 1 M = ( Q , Σ , Δ , δ , λ, q0 )
Current
State Where Q = { q0, q1 }
State Output State Output
Σ = { 0, 1 }
q0 q0 0 q1 1 1/0
Δ = { 0, 1 } 0/1
q1 q1 1 q1 0
q0 = q0
δ and λ are shown in transition table

Prepared By: Mr. Vaibhav Ambhire


Examples on Mealy Machine 0/0

Ex. 3 Design a Mealy machine to generate 2's complement of given binary number q0
Solution:
1/ 1
Simulation for the input: 1 0 1 1

q1
Input 1 1 0 1
State q0 q1 q1 q1 q1
Output 1 0 1 0
1/0
0/1

Prepared By: Mr. Vaibhav Ambhire


Examples on Mealy Machine 1/ 0

Ex.4 Design a Mealy machine to increment given binary number by 1 q0


Solution:
0/1
For designing incrementor of binary number, we assume that input is read from LSB to MSB

Suppose Given Number is 1 0 1 1


q1
Resultant Number: 1 1 0 0

Suppose Given Number is 1 1 0 1

Resultant Number: 1 1 1 0
0/0
We can observe that to get output 1/ 1

We invert each binary input '1' until we read first 0

Once we get first 0 we will replace it by 1 and then keep remaining bits as it is

Transition Diagram can be drawn as

Prepared By: Mr. Vaibhav Ambhire


Examples on Mealy Machine 1/ 0

Ex.4 Design a Mealy machine to increment given binary number by 1 q0


Solution:
0/1

Above FSM can be represented as


q1
Input 0 Input 1 M = ( Q , Σ , Δ , δ , λ, q0 )
Current
State Where Q = { q0, q1 }
State Output State Output
Σ = { 0, 1 }
q0 q1 1 q0 0 0/0
Δ = { 0, 1 } 1/ 1
q1 q1 0 q1 1
q0 = q0
δ and λ are shown in transition table

Prepared By: Mr. Vaibhav Ambhire


Examples on Mealy Machine 1/ 0

Ex.4 Design a Mealy machine to increment given binary number by 1 q0


Solution:
0/1
Simulation for the sting : 1 0 1 1

q1
Input 1 1 0 1
State q0 q0 q0 q1 q1
Output 0 0 1 1
0/0
1/ 1

Prepared By: Mr. Vaibhav Ambhire


Examples
0 1
Ex.1 The Moore machine to determine
1 0 residue mod 3 for binary number is
q0 / 0 q1 / 1 q2 / 2 given.

1 0

Transition Diagram can be drawn as above


Solution:
There will be three states representing the
If we have original no 'n' and we put 0 at the right side of it remainder
then value become 2n q0 represents state with remainder 0
If we have original no 'n' and we put '1' at the right side of it q1 represents state with remainder 1
then value become 2n+1 q2 represents state with remainder 2

Prepared By: Mr. Vaibhav Ambhire


Examples
0 1
Ex.1 The Moore machine to determine
1 0 residue mod 3 for binary number is
q0 / 0 q1 / 1 q2 / 2 given.

1 0

Transition Diagram can be drawn as above


Current Input Input Output
Above FSM can be represented as
State 0 1 λ
M = ( Q , Σ , Δ , δ , λ, q0 ) q0 q0 q1 0
Where Q = { q0, q1, q2 } q1 q2 q0 1
Σ = { 0, 1 } q2 q1 q2 2

Δ = { 0, 1 }
q0 = q0
δ and λ are shown in transition table Prepared By: Mr. Vaibhav Ambhire
Examples
0 1
Ex.1 The Moore machine to determine
1 0 residue mod 3 for binary number is
q0 / 0 q1 / 1 q2 / 2 given.

1 0

Simulation for the string: 1010


Current Input Input Output
State 0 1 λ
Input 1 0 1 0 q0 q0 q1 0
State q0 q1 q2 q2 q1 q1 q2 q0 1
Output 0 1 2 2 1 q2 q1 q2 2

Prepared By: Mr. Vaibhav Ambhire


Examples
0 /A 0/B Ex.2 Construct Mealy machine for given
1/B problem:
q0 q1
For input binary string the output symbol
1 /A
toggle between A and B whenever there is
input symbol '1'

Assume that A is initial output symbol


Solution: Example:
State q0 generates output symbol as 'A'
For input 0101 output will be ABBA
State q1 generates output symbol as 'B'
For input 0010 output will be AABB

Prepared By: Mr. Vaibhav Ambhire


0 /A 0/B Examples
1/B

q0 q1 Ex.2 Construct Mealy machine for given


problem:
1 /A
For input binary string the output symbol
toggle between A and B whenever there is
Input 0 Input 1
Current input symbol '1'
State
State Output State Output Assume that A is initial output symbol
q0 q0 A q1 B Example:
q1 q1 B q0 A
For input 0101 output will be ABBA
Above FSM can be represented as
For input 0010 output will be AABB
M = ( Q , Σ , Δ , δ , λ, q0 )
Where Q = { q0, q1} Σ = { 0, 1 }
Δ = { A, B } q0 = q0
δ and λ are shown in transition table
Prepared By: Mr. Vaibhav Ambhire
0 /A 0/B Examples
1/B

q0 q1 Ex.2 Construct Mealy machine for given


problem:
1 /A
For input binary string the output symbol
toggle between A and B whenever there is
input symbol '1'

Assume that A is initial output symbol


Simulation for the string: 0011 Example:

For input 0101 output will be ABBA


Input 0 0 1 1
State q0 q0 q0 q1 q0 For input 0010 output will be AABB
Output A A B A

Prepared By: Mr. Vaibhav Ambhire


Example
Ex. 3 Design a Moore machine to convert each occurrence of substring abb by aba

Solution:

Transition Diagram for Moore Machine can be drawn as


Transition Table:

a a State/ Output
b Input a b
λ
a b b q0 q1 q0 b
q0 / b q1 / a q2 / b q3 / a q1 q1 q2 a
q2 q1 q3 b
a q3 q1 q0 a
b

Prepared By: Mr. Vaibhav Ambhire


Example
Ex. 3 Design a Moore machine to convert each occurrence of substring abb by aba

Solution:

Transition Diagram for Moore Machine can be drawn as

a a
b
Above FSM can be represented as
a b b M = ( Q , Σ , Δ , δ , λ, q0 )
q0 / b q1 / a q2 / b q3 / a
Where Q = { q0, q1, q2, q3 }

a
Σ = { a, b }
b
Δ = { a, b }
q0 = q0
δ and λ are shown in transition
table
Prepared By: Mr. Vaibhav Ambhire
Example
Ex. 3 Design a Moore machine to convert each occurrence of substring abb by aba

Solution:

Simulation for the string: a b b a b a

a a
b

a b b Input State output


q0 / b q1 / a q2 / b q3 / a q0 b
a q1 a
a b q2 b
b b q3 a
a q1 a
b q2 b
a q1 a

Prepared By: Mr. Vaibhav Ambhire


Example
Ex. 4 Give mealy machine to each occurrence of substring 120 to 121 where Σ = { 0, 1, 2 }

Solution:

Transition Diagram for Mealy Machine can be drawn as

2/2
1/1 1/1
0/0

1/1 2/2 0/1


q0 q1 q2 q3

1/1
0/0
0/0 2/2
2/2

Prepared By: Mr. Vaibhav Ambhire


Input 2
Input 0 Input 1
Example Current
State State Output
Ex. 4 Give mealy machine to each occurrence of substring 120 to 121 where
State Σ Output
= { 0, 1, 2State
} Output
Solution: q0 q0 0 q1 1 q0 2
Transition Diagram for Mealy Machine can be drawn as q1 q0 0 q1 1 q2 2
q2 q3 1 q1 1 q0 2
2/2
q3 q0 0 q1 1 q0 2
1/1 1/1
0/0

1/1 2/2 0/1


q0 q1 q2 q3

1/1
0/0
0/0 2/2
2/2

Prepared By: Mr. Vaibhav Ambhire


Input 2
Input 0 Input 1
Example Current
State State Output
Ex. 4 Give mealy machine to each occurrence of substring 120 to 121 where
State Σ Output
= { 0, 1, 2State
} Output
Solution: q0 q0 0 q1 1 q0 2
Transition Diagram for Mealy Machine can be drawn as q1 q0 0 q1 1 q2 2
q2 q3 1 q1 1 q0 2
2/2
q3 q0 0 q1 1 q0 2
1/1 1/1
0/0

1/1 2/2 0/1 Above FSM can be represented as


q0 q1 q2 q3 M = ( Q , Σ , Δ , δ , λ, q0 )
Where Q = { q0, q1, q2, q3}
1/1
0/0 Σ = { 0, 1, 2 }
0/0 2/2 Δ = { 0, 1, 2 }
2/2 q0 = q0
δ and λ are shown in transition
table
Prepared By: Mr. Vaibhav Ambhire
Example
Ex. 4 Give mealy machine to each occurrence of substring 120 to 121 where Σ = { 0, 1, 2 }

Solution:

Simulation for the string 1 2 1 2 0 1

Input 2 Input State output


Input 0 Input 1
Current
State q0
State Output
State Output State Output 1 q1 1
q0 q0 0 q1 1 q0 2 2 q2 2
q1 q0 0 q1 1 q2 2 1 q1 1
q2 q3 1 q1 1 q0 2 2 q2 2
q3 q0 0 q1 1 q0 2 0 q3 1
1 q1 1

Prepared By: Mr. Vaibhav Ambhire


Comparison Between Moore and Mealy Machine
M O O R E M ACHI NE M E A LY M ACHI N E

• Output: Associated with States • Output: Associated with Transitions

• If length of input is n then length of • If length of input is n then length of output


output is ( n + 1 ) is also n

• Output Function λ : Q → Δ • Output Function: λ : Q X Σ → Δ

0 0/B
• Example: • Example:
1 1/B

q1/B q0 q1
q0/A

1 1 /A
Prepared By: Mr. Vaibhav Ambhire
FSM with Output
• Applications:

1. Text Editors
- Processing Text
- Uses Regular Expressions for substituting strings
2. Lexical Analysis
- Separate Tokens and recognize them

• Limitations

1. Well formedness of Parenthesis


2. Checking palindrome of given language

Prepared By: Mr. Vaibhav Ambhire


Thank You …

Prepared By: Mr. Vaibhav Ambhire

You might also like