CS242 - Module 3
CS242 - Module 3
26/12/2021
Theory of Computing
Recommended Reading
https://people.eecs.berkeley.edu/~sseshia/172/lectures/Lecture4.pdf
https://slideplayer.com/slide/13617026/
https://www.tutorialspoint.com/automata_theory/pdf/moore_and_mealy_machin
es.pdf
This Presentation is mainly dependent on the textbook: Introduction to Automata Theory, Languages, and Computation: Global Edition, 3rd edition (2013) PHI
by John E. Hopcroft, Rajeev Motwani and Jeffrey D. Ullman
• Finite Automata with output (Mealy and Moore machines)
Two Types of Automata with Outputs
Differ in how outputs are produced
• Moore Machine:
• Outputs are independent of the inputs, i.e. outputs are produced from
within the state of the state machine
• Mealy Machine:
• Outputs can be determined by the present state alone, or by the present
state and the present inputs, i.e. outputs are produced as the machine makes
a transition from one state to another
• Any Moore machine can be turned into a Mealy machine (and vice
versa)
7
Moore Machine
8
Mealy Machine
The Mealy State Machine
generates outputs based on:
The Present State, and
The Inputs to the M/c.
So, same state can generate
a,b
many different patterns of State 1 q,r
output signals, depending on
the inputs.
Outputs are shown on Input condition that
i,j must exist in order
transitions since they are x,y
determined in the same way as to execute these
is the next state. transitions from
State 1
Output condition that
results from being in State 2
a particular present state
9
FSM as Recognizer/Translator
• Outputs a ‘0’ if an even # of 1’s is received and outputs a ‘1’
otherwise
• What is state?
• Two states: whether an even # of 1s have been received, or an odd # of 1s
have been received
0/1 0
10
Formal Definition
A Moore machine (finite-state machine with output ) is a six-
tuple M = (S, I, O, f, g, s0)
consists of:
▪ A finite set S of states
▪ A finite input alphabet I
▪ A finite output alphabet O
▪ A transition function f that assigns to each sate and input
pair a new state
▪ An output function g that assigns to each sate and input
pair an output
▪ An initial state s0
11
Examples (1)
The following state table describes a finite-state machine with
S = {s0, s1, s2, s3} , I = {0, 1} and O = {0, 1} the values of the
transition function f are displayed in the first two columns,
and the values of the output function g are displayed in the
last two columns.
State Transition Table State Transition Diagram
12
Examples (2)
Construct the state table for the finite-state machine with the
following state diagram.
State Transition Table State Transition Diagram
13
Examples (3)
Find the output string generated by the following finite-state
machine if the input string is 101011.
The obtained output is 001000 as shown in the table.
14
Examples (4)
Unit-Delay Machine produces as output the input string
delayed by a specific amount of time.
Input: x1x2x3…xn output: 0x1x2x3…xn-1
Previous input
is 1
Previous input
is 0
Unit-Delay Machine 15
• Regular expressions
Definition of a Regular Expression
R is a regular expression if it is:
1. R = a, if a ∈ .
2. R = ε, standing for the language {ε}.
3. R = Ø, standing for the empty language.
4. R = R1+R2, where R1 and R2 are regular expressions, and ‘+’
signifies union.
5. R = R1R2, where R1 and R2 are regular expressions and ‘R1R2’
signifies concatenation.
6. R = R’*, where R’ is a regular expression and ‘*’ signifies
closure.
7. R = (R’), where R’ is a regular expression and ‘()’ signifies
parenthesizing.
8. R is a regular expression if it is obtained by a finite number of
applications of 1-7.
9. Nothing else is a regular expression.
19
Regular expression Operators (1)
◼ Union of two languages:
◼ L U M = All strings that are either in L or M or in
both
◼ Note: A union of two languages produces a third
language
◼ Concatenation of two languages:
◼ L . M = All strings that are of the form xy
s.t., x L and y M
◼ The dot (.) operator is usually omitted
◼ i.e., L . M is written as LM
20
Regular expression Operators (2)
◼ Kleene Closure (*) of a given language L:
◼ L0= {}
◼ L1= {w | for some w L}
◼ L2= { w1w2 | w1 L, w2 L (duplicates allowed)}
◼ Li= { w1w2…wi | all w’s chosen L (duplicates allowed)}
◼ “i” here refers to how many strings to concatenate from the parent language L to
produce strings in the language Li
◼ (Note: the choice of each wi is independent)
◼ L* = Ui≥0 Li (arbitrary number of concatenations)
Example:
◼ Let L = { 1, 00}
◼ L0= {}
◼ L1= {1,00}
◼ L2= {11,100,001,0000}
◼ L3= {111,1100,1001,10000,000000,00001,00100,0011}
◼ L* = L0 U L1 U L2 U …
21
Regular expression Operators (3)
Kleene Closure (special notes)
◼ L* is an infinite set iff |L| ≥ 1 and L ≠ {}
◼ If L = Φ, then L* = {}
22
Building Regular Expressions
◼ Let E and F be a any two regular expressions and the
language represented by E is L(E) and F is L(F),
◼ Then:
◼ (E) = E
◼ L(E + F) = L(E) U L(F)
◼ L(E F) = L(E) L(F)
◼ L(E*) = (L(E))*
23
Example: Regular Expressions
◼ L = { w | w is a binary string which does not contain two consecutive 0s or two
consecutive 1s anywhere)
◼ E.g., w = 01010101 is in L, while w = 10010 is not in L
◼ Goal: Build a regular expression for L
◼ Four cases for w:
◼ Case A: w starts with 0 and |w| is even
◼ Case B: w starts with 1 and |w| is even
◼ Case C: w starts with 0 and |w| is odd
◼ Case D: w starts with 1 and |w| is odd
◼ Regular expression for the four cases:
◼ Case A: (01)*
◼ Case B: (10)*
◼ Case C: 0(10)*
◼ Case D: 1(01)*
◼ Since L is the union of all 4 cases:
◼ Reg Exp for L = (01)* + (10)* + 0(10)* + 1(01)*
◼ If we introduce then the regular expression can be simplified to:
◼ Reg Exp for L = ( +1)(01)*( +0)
24
Precedence Rule of Operators
◼ Highest to lowest
◼ * (star)
◼ . (concatenation)
◼ + operator
◼ Example:
◼ 01* + 1 = ( 0 . ((1)*) ) + 1
25
• Finite Automata and Regular Expressions
FA & RegEx
◼ To show that they are interchangeable, consider the
following theorems:
◼ Theorem 1: For every DFA A there exists a regular
expression R such that L(R)=L(A)
◼ Theorem 2: For every regular expression R there exists an
-NFA E such that L(E)=L(R)
-NFA NFA
Theorem 2 Kleene Theorem
RegEx DFA
Theorem 1
27
DFA to RegEx construction
Informally, trace all distinct paths (traversing cycles only
once) from the start state to each of the final states and
enumerate all the expressions along the way
DFA Reg Ex
Theorem 1
Example: 1 0 0,1
q0 0 q1 1 q2
1*00*1(0+1)*
28
RegEx to -NFA construction
RegEx -NFA
Theorem 2
Example: (0+1)*01(0+1)*
(0+1)* 01 (0+1)*
0
0
0 1
1
1
29
Converting a RegEx to an Automata
◼ We have shown we can convert an automata to a
RegEx. To show equivalence we must also go the
other direction, convert a RegEx to an automaton.
◼ We can do this easily by converting a RegEx to an ε-
NFA
◼ Inductive construction
◼ Start with a simple basis, use that to build more complex
parts of the NFA
RE to ε-NFA (1)
◼ Basis:
R=a
a
R=ε ε
R=Ø
RE to ε-NFA (2)
ε S ε
R=S+T
ε ε
T
ε
R = ST
S T
ε ε
R= S*
S
ε
RE to ε-NFA Example (1)
a
a
b
b
a ε b
ab
RE to ε-NFA Example (2)
ab+a
a ε b
ε ε
a
ε ε
(ab+a)*
a ε b
ε ε
ε ε
a
ε ε
ε
RE to ε-NFA Example (3)
ab+a
a ε b
ε ε
a
ε ε
(ab+a)*
a ε b
ε ε
ε ε
a
ε ε
ε
• Applications of Regular Expressions
Applications of Regular Expressions
• Offers a declarative way to express the pattern of any
string we want to accept
• E.g., 01*+ 10*
37
• Algebraic Laws for Regular Expressions
Algebraic Laws of Regular Expressions
◼ Commutative:
◼ E+F=F+E
◼ Associative:
◼ (E + F) + G = E + (F + G)
◼ (EF) G = E (FG)
◼ Identity:
◼ E+Φ=E
◼ E=E=E
◼ Annihilator:
◼ ΦE = EΦ = Φ
◼ Distributive:
◼ E (F + G) = EF + EG
◼ (F + G) E = FE + GE
◼ Idempotent: E + E = E
◼ Involving Kleene closures:
◼ (E*)* = E*
◼ Φ* =
◼ * =
◼ E+ = E E*
◼ E? =+E
39
Test
Let R and S be two regular expressions. Then:
1. ((R*)*)* = R* ?
2. (R+S)* = R* + S* ?
40
Checking a Law
◼ Suppose we are told that the law
(R + S)* = (R*S*)*
holds for regular expressions. How would we check that this claim is
true?
1. Convert the RegEx’s to DFA’s and minimize the DFA’s to see if they
are equivalent.
2. We can use the “concretization” test:
◼ Think of R and S as if they were single symbols, rather than
placeholders for languages, i.e., R = {0} and S = {1}.
◼ Test whether the law holds under the concrete symbols. If so, then this
is a true law, and if not then the law is false.
Concretization Test
This Presentation is mainly dependent on the textbook: Introduction to Automata Theory, Languages, and Computation: Global Edition, 3rd edition (2013) PHI
by John E. Hopcroft, Rajeev Motwani and Jeffrey D. Ullman
Thank You