0% found this document useful (0 votes)
26 views

Finite Automata

Uploaded by

alibayhya7
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)
26 views

Finite Automata

Uploaded by

alibayhya7
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/ 144

Theory of Computation

(Finite Automata)

Pramod Ganapathi
Department of Computer Science
State University of New York at Stony
Brook

January 24, 2021


Contents

Contents
Deterministic Finite Automata
(DFA) Regular Languages
Regular Expressions
Nondeterministic Finite Automata
(NFA) Transformations
Non-Regular Languages
Deterministic Finite Automata (DFA)
Electric bulb

Problem
Design
the logic
behind
an
electric
bulb.
Electric bulb

Problem
Design the logic behind an electric
bulb.
Solution
Diagra
m.

Analysis.
States = {nolight, light}, Input = {off,
on} Finite Automaton.
on

of
f
Multispeed fan

Problem
Design the logic behind a
multispeed fan.
Multispeed fan

Problem
Design the logic behind a
multispeed fan.
Solution
Diagra Finite
m. Automaton.
Off
0
1 3
2
1 3
Analysis.
States = {0, 1, 2,
3} 2
Input = { , Ç}
Automatic doors

Problem
Design the logic behind automatic doors in
Walmart.
Automatic doors
Solution
Diagra
m.

Analysis.
States = {close, open}, Input = {left, right,
neither} Finite Automaton.
neither
left, right left,
right

close open

neither
Basic features of finite automata

A finite automaton is a simple computer with


extremely limited memory
A finite automaton has a finite set of states
Current state of a finite automaton changes when it
reads an input symbol
A finite automaton acts as a language acceptor i.e.,
outputs “yes” or “no”
Why should you care?

Deterministic Finite Automata (DFA) are


everywhere. ATMs
Ticket machines
Vending
machines Traffic
signal systems
Calculators
Digital watches
Automatic
doors
Elevators
Washing machines
Dishwashing
machines
Thermostats
Train switches
(CS)
Why should you care?

Probabilistic Finite Automata (PFA) are


everywhere, too. Speech recognition
Optical character
recognition
Thermodynamics
Statistical
mechanics
Chemical reactions
Information theory
Queueing theory
PageRank
algorithm
Statistics
Reinforcement
learning
Price changes in
finance Genetics
What is a decision problem?

Definition
A decision problem is a computational problem with
a ‘yes’ or ‘no’ answer.
A computer that solves a decision problem is a
decider. Input to a decider: A string w
Output of a decider: Accept (w is in the language) or
Reject (w is not in the language)
w Decid yes/
er no
What is a decision problem?

English Acce
word pt

Other Rejec
word t

Language = English language = {milk, food, sleep, . . .}


D Accept Not in language = {zffgb, cdcapqw, . . .} D
Reject
What is a decision problem?

Some Acce
strings pt

Other Rejec
strings t
How does a DFA work?
Problem
Does the DFA accept the string
bbab?
a
a b b
star q0 q2
t q1 a,
b
How does a DFA work?
Problem
Does the DFA accept the string
bbab?
a a
b
star q0 q2
t qb1 a, b
Solution
The DFA accepts the string bbab. The computation is:
1. Start in state q0
2. Read b, follow transition from q0 to q1.
3. Read b, follow transition from q1 to q1.
4. Read a, follow transition from q1 to q2.
5. Read b, follow transition from q2 to q1.
6. Accept because the DFA is in an accept state q1 at
the end of the input.
How does a DFA work?
Problem
Does the DFA accept the string
aaba?
a
a b b
star q0 q2
t q1 a,
b
How does a DFA work?
Problem
Does the DFA accept the string
aaba?
a a
b
star q0 q2
t qb1 a, b
Solution
The DFA rejects the string aaba. The computation is:
1. Start in state q0
2. Read a, follow transition from q0 to q0.
3. Read a, follow transition from q0 to q0.
4. Read b, follow transition from q0 to q1.
5. Read a, follow transition from q1 to q2.
6. Reject because the DFA is in a reject state q2 at the
end of the input.
How does a DFA work?

a b
a
b
bbab q0 q1 q2 Acce
pt
a,
b

a b
a
b
aaba q0 q1 q2 Rejec
t
a,
b
How does a DFA work?

Problem
What language does the DFA
accept?

a b b
a
start q0 q2
q1 a,
b
How does a DFA work?

Problem
What language does the DFA
accept?
a a
b
start q0 q2
q1 b a, b
Examples
The DFA accepts the following strings:
b, ab, bb, aabbbb, ababababab, . . . D ends
with b baa, abaa, ababaaaaaa, . . . D ends with b followed
by even a’s The DFA rejects the following strings:
a, ba, babaaa, . . .
What language does the DFA accept?
Construct DFA for Σ = { a }

Problem
Construct a DFA that accepts all strings from the
language
L = {ϵ, a, aa, aaa, aaaa, . . .}
Construct DFA for Σ = { a }

Problem
Construct a DFA that accepts all strings from the
language
L = {ϵ, a, aa, aaa, aaaa, . . .}
Solution
Language L: Σ∗ = {ϵ, a, aa, aaa, aaaa, . . .}
Expression: a∗
Deterministic Finite Automaton (DFA) M :
a

start q0
Construct DFA for Σ = { a }

Problem
Construct a DFA that accepts all strings from the
language
L = {}
Construct DFA for Σ = { a }

Problem
Construct a DFA that accepts all strings from the
language
L = {}
Solution
Language L: $ = D Empty
{} language
Expression: $
DFA M : a

start
q0
Construct DFA for Σ = { a }

Problem
Construct a DFA that accepts all strings from the
language
L = {a, aa, aaa, aaaa, . . .}
Construct DFA for Σ = { a }

Problem
Construct a DFA that accepts all strings from the
language
L = {a, aa, aaa, aaaa, . . .}
Solution
Language L: Σ∗ − { ϵ} = {a, aa, aaa, aaaa, . . .}
Expression: a +
DFA M :
a
a
start q0 q1
Construct DFA for Σ = { a }

Problem
Construct a DFA that accepts all strings from the
language
L = {ϵ}
Construct DFA for Σ = { a }

Problem
Construct a DFA that accepts all strings from the
language
L = {ϵ}
Solution
Language L: = {ϵ}
Expression: ϵ
DFA M :
a
a
start q0 q1
Construct DFA for Σ = { a }

Problem
Construct a DFA that accepts all strings from the
language
L = {aaa}
Construct DFA for Σ = { a }

Problem
Construct a DFA that accepts all strings from the
language
L = {aaa}
Solution
Language L:
{aaa}
Expression: aaa
DFA M :
a
a

a
Construct DFA for Σ = { a }

Problem
Construct a DFA that accepts all strings from the
language
L = {strings with even size}
Construct DFA for Σ = { a }

Problem
Construct a DFA that accepts all strings from the
language
L = {strings with even size}
Solution
Language L: {ϵ, aa, aaaa, aaaaaa, . . .}
Expression: (aa)∗
DFA M :
a
start q0

q a
Construct DFA for Σ = { a }

Problem
Construct a DFA that accepts all strings from the
language
L = {strings with odd size}
Construct DFA for Σ = { a }

Problem
Construct a DFA that accepts all strings from the
language
L = {strings with odd size}
Solution
Language L: {a, aaa, aaaaa, . . .}
Expression: a(aa)∗
DFA M :
a
start q0

q a
Construct DFA for Σ = { a }

Problem
Construct a DFA that accepts all strings from the
language
L = {strings of size divisible by 3}
Construct DFA for Σ = { a }

Problem
Construct a DFA that accepts all strings from the
language
L = {strings of size divisible by 3}
Solution
Language L: {ϵ, aaa, aaaaaa, aaaaaaaaa, . . .}
Expression: (aaa)∗
DFA M :
a a
start q0 q1
q2

a
Construct DFA for Σ = { a }

Problem
Construct a DFA that accepts all strings from the
language
L = {strings of size not divisible by 3}
Construct DFA for Σ = { a }

Problem
Construct a DFA that accepts all strings from the
language
L = {strings of size not divisible by 3}
Solution
Language L: {a, aa, aaaa, aaaaa, . . .}
Expression: (a ∪ aa)(aaa)∗
DFA M :
a a
start q0 q1 q2

a
Construct DFA for Σ = { a }

Problem
Construct a DFA that accepts all strings from the
language
L = {strings of size divisible by 6}
Construct DFA for Σ = { a }

Problem
Construct a DFA that accepts all strings from the
language
L = {strings of size divisible by 6}
Solution
Language L: {ϵ, aaaaaa, aaaaaaaaaaaa, . . .}
Expression: (aaaaaa)∗
DFA M :
a a a a a
start q0 q1 q2 q3 q4
q5

a
Construct DFA for Σ = { a }

Problem
Construct a DFA that accepts all strings from the
language
L = {strings of size divisible by 6}
Solution
Language L: {ϵ, aaaaaa, aaaaaaaaaaaa, . . .}
Expression: (aaaaaa)∗
DFA M :
a a a a a
start q0 q1 q2 q3 q4
q5

a
Can you think of another approach?
Construct DFA for Σ = { a }

Problem
Construct a DFA that accepts all strings from the
language
L = {strings of size divisible by 6}
Solution
Let n = string
size
Observation
n mod 6 = 0 ⇐⇒
n mod 2 = 0 and
n mod 3 = 0
Idea
Build DFA M 1 for n mod 2 =
0. Build DFA M2 for n mod 3
= 0. Run M 1 and M2 in
parallel.
Construct DFA for Σ = { a }

Problem
Construct a DFA that accepts all strings from the
language
L = {strings with size n where n mod 4 = 2}
Construct DFA for Σ = { a }

Problem
Construct a DFA that accepts all strings from the
language
L = {strings with size n where n mod 4 = 2}
Solution
Language L: {aaa, aaaaaaa, aaaaaaaaaaa, . . .}
Expression: aa(aaaa)∗
DFA M :
a a a
start rem 0 rem 1 rem 2 rem
3

a
What about strings with size n where n mod k = i?
Construct DFA for Σ = { a }

More Problems
Construct a DFA that accepts all strings from the
language L =
{strings with size n } such that
n2 − 5n + 6 = 0
n ∈ [4, 37]
n is a perfect cube
n is a prime number
n satisfies a mathematical function f (n)
Specifying a DFA

The specification of DFA consists of:


A (finite) alphabet
A (finite) set of states
Which state is the start
state? Which states are the
final states?
What is the transition from
each state, on each input
character?
What is a deterministic finite automaton (DFA)?

Deterministic = Events can be determined


precisely Finite = Finite and small amount
of space used Automaton = Computing
machine
What is a deterministic finite automaton (DFA)?

Deterministic = Events can be determined


precisely Finite = Finite and small amount
of space used Automaton = Computing
machine

Definition
A deterministic finite automaton (DFA) M is
a 5-tuple
M = (Q , Σ, δ, q0, F ), where,
1. Q: A finite set (set of states). D Space
(computer memory)
2. Σ: A finite set (alphabet).
3. δ : Q × Σ → Q is the transition function.
D Time
(comput
ation)
Acceptance and rejection of strings

Definition
A DFA accepts a string w = w1w2 . . . wk iff there
exists a sequence of states r0, r 1 , . . . , r k such that
the current state starts from the start state and
ends at a final state when all the symbols of w have
been read.
A DFA rejects a string iff it does not accept it.
What is a regular language?

Definition
We say that a DFA M accepts a language L if
L = {w | M accepts w}.
A language is called a regular language if some DFA
accepts or recognizes it.
Construct DFA for Σ = {a, b}

Problem
Construct a DFA that accepts all strings from the
language
L = {strings with odd number of b’s}
Construct DFA for Σ = {a, b}

Problem
Construct a DFA that accepts all strings from the
language
L = {strings with odd number of b’s}
Solutio
n
States
qodd: DFA is in this state if it has read odd
b’s. qeven: DFA is in this state if it has read
even b’s.
Construct DFA for Σ = {a, b}

Problem
Construct a DFA that accepts all strings from the
language
L = {strings with odd number of b’s}
Solution
Language L: {strings with odd number
of b’s} Expression: a∗b(a ∪ ba∗b)∗ or
a∗ba∗(ba∗ba∗)∗ DFA M : a a
b

star
qeve qodd
t n b
Construct DFA for Σ = {a, b}

Problem
Construct a DFA that accepts all strings from the
language
L = {strings with odd number of b’s}
Solution (continued)
DFA M is specified
as
Set of states is Q = {qeven,
qodd} Set of symbols is Σ =
{a, b} Start state is qeven a b
Set of accept states qis F =qeven
even qodd

{qodd} qodd qodd qeven

Transition function δ is:


δ
Construct DFA for Σ = {a, b}

Problem
Construct a DFA that accepts all strings from the
language
L = {strings containing bab}
Construct DFA for Σ = {a, b}

Problem
Construct a DFA that accepts all strings from the
language
L = {strings containing bab}
Solutio
n
States
qb: DFA is in this state if the last symbol read was b,
but the substring bab has not been read.
qba: DFA is in this state if the last two symbols read
were ba, but the substring bab has not been read.
qbab: DFA is in this state if the substring bab has been
read in the input string.
q: In all other cases, the DFA is in this state.
Construct DFA for Σ = {a, b}

Problem
Construct a DFA that accepts all strings from the
language
L = {strings containing bab}
Solution (continued)
Language L: {strings containing
bab} Expression: (a∗b+aa)∗bab(a
∪ b)∗ DFA M : a,
a
b
b b a b
start q qb qba qbab

a
Construct DFA for Σ = {a, b}

Problem
Construct a DFA that accepts all strings from the
language
L = {strings containing bab}
Solution (continued)
DFA M is specified
as
Set of states is Q =
{q, qb, qba, qbab}
Transition function δ is:
Set of symbols is Σ δ a b
= {a, b} q q qb
Start state is q qb qba qb
Set of accept states qba q qbab
is F = {qbab} qbab qbab qbab
Closure properties of regular languages

Properties
Let L 1 and L 2 be regular
languages. Then, the following
languages are regular.
Complement. L 1 = { x | x ∈ Σ∗ and x /∈
L 1 } . Union. L 1 ∪ L 2 = { x | x ∈ L 1 or
x ∈ L 2 }.
Star. L∗ = { x x . . . x | k ≥ 0 and each x ∈
Intersection.
1 L 1 2∩ L 2k = { x | x ∈ L 1 and
1 i x ∈
L }. 1
L 2 }. Concatenation. L 1 · L 2 = { x y | x ∈ L 1
and y ∈ L 2 }.
Closure properties for languages

Operation
Languag L 1 ∪ L 2 L 1 ∩ L2 L' L1 L2 L∗ LR LT
e
Regular ✓ ✓ ✓ ✓ ✓ ✓ ✓
DCFL C C ✓ C C C C
CFL ✓ C C ✓ ✓ ✓ ✓
Recursiv ✓ ✓ ✓ ✓ ✓ ✓ C
e
R.E. ✓ ✓ C ✓ ✓ ✓ ✓
L 1 ∪ L 2 = Union of L 1 and L 2
L 1 ∩ L 2 = Intersection of L 1 and
L 2 L ' = Complement of L
L 1 L 2 = Concatenation of L 1 and
L2
L ∗ = Powers of
L L R = Reverse
of L
L T = Finite
transduction of L
(may include:
intersection/
Construct DFA for L 1 ∪ L 2

Problem
Construct a DFA that accepts all strings from the
language
L = {strings with size multiples of 2 or 3} where Σ =
{a}
Solution
Language L 1 = {strings with size
multiples of 2} Language L 2 = {strings
with size multiples of 3}
a
start a p0 a
start q0 q1 q2

p1 a
Construct DFA for L 1 ∪ L 2

Solution (continued)
Language L 1 ∪ L 2 = {strings with size multiples of
2 and 3} a a a
start q0 q1
start p0 p1
q2
a a

star (p0, q0) (p0, q1) (p0, q2)


t
a a a a
a a

(p1, q0) (p1, q1) (p1, q2)


Construct DFA for L 1 ∪ L 2

Union
Let M 1 accept L 1 , where M 1 = (Q 1 , Σ, δ1, q1, F 1 )
Let M2 accept L 2 , where M2 = (Q 2 , Σ, δ2, q2, F 2 )
Let M accept L 1 ∪ L 2 , where M = (Q , Σ, δ, q, F ). Then
Q = {(r 1 , r 2 ) | r 1 ∈ Q 1 and r2 ∈ Q 2 } D Cartesian
product
δ((r 1 , r 2 ), a) = (δ 1 (r 1 , a), δ2(r2, a)) ∀(r1, r 2 ) ∈ Q , a ∈ Σ
q0 = (q1, q2)
F = {(r 1 , r 2 ) | r 1 ∈ F 1 or r2 ∈ F 2 }
Construct DFA for L 1 ∩ L 2

Problem
Construct a DFA that accepts all strings from the
language
L = {strings with size multiples of 2 and 3} where Σ
= {a}
Solution
Language L 1 = {strings with size
multiples of 2} Language L 2 = {strings
with size multiples of 3}
a
start a p0 a
start q0 q1 q2

p1 a
Construct DFA for L 1 ∩ L 2

Solution (continued)
Language L 1 ∩ L 2 = {strings with size multiples of
2 and 3} a a a
start q0 q1
start p0 p1
q2
a a

star (p0, q0) (p0, q1) (p0, q2)


t
a a a a
a a

(p1, q0) (p1, q1) (p1, q2)


Construct DFA for L 1 ∩ L 2

Intersection
Let M 1 accept L 1 , where M 1 = (Q 1 , Σ, δ1, q1, F 1 )
Let M2 accept L 2 , where M2 = (Q 2 , Σ, δ2, q2, F 2 )
Let M accept L 1 ∩ L 2 , where M = (Q , Σ, δ, q, F ). Then
Q = {(r 1 , r 2 ) | r 1 ∈ Q 1 and r2 ∈ Q 2 } D Cartesian
product
δ((r 1 , r 2 ), a) = (δ 1 (r 1 , a), δ2(r2, a)) ∀(r1, r 2 ) ∈ Q , a ∈ Σ
q0 = (q1, q2)
F = {(r 1 , r 2 ) | r 1 ∈ F 1 and r2 ∈ F 2 }
Problems for practice

Problems
Assume Σ = {a, b} unless otherwise mentioned.
Construct DFA’s for the following languages and
generalize:
L = {w | |w| =
2} L = {w | |w|
≤ 2} L = {w | |
w| ≥ 2}
L = {w | n a (w)
= 2}
L = {w | n a (w)
≤ 2}
L = {w | n a (w)
≥ 2}
L = {w | n a (w)
mod 3 = 1}
Problems for practice

Problems (continued)
L = {b | binary number b mod 3 = 1} for Σ = {0,
1}
L = {t | ternary number t mod 4 = 3} for Σ =
{0, 1, 2}
L = {w | w starts with
a} L = {w | w
contains a} L = {w |
w ends with a}
L = {w | w starts with
ab} L = {w | w
contains ab} L = {w |
w ends with ab}
L = {w | w starts with
a and ends with b}
L = {w | w starts and ends with different
Problems for practice

Problems (continued)
L = {w | every a in w is followed by bb}
L = {w | every a in w is never followed
by bb} L = {w | w = ambn for m, n ≥ 1}
L = {w | w = ambn for m, n ≥ 0}
L = {w | w = a m b n c l for m, n, l ≥ 1} for Σ = {a,
b, c} L = {w | w = a m b n c l for m, n, l ≥ 0} for Σ
= {a, b, c} L = {w | second symbol from left
end of w is a}
L = {w | second symbol from right end of
w is a} L = {w | w = a3bxa3 such that x ∈
{a, b}∗}
Equivalence of different computation models

Two machines or computational models are


computationally equivalent if they accept/recognize
the same language.
The following models are computationally
equivalent: DFA, regular expressions, NFA, and
regular grammars.

DFA

Regular Regular Regula


expressio Languag r
ns es gramm
ar

NFA
Closure properties for languages

Operation
Languag L 1 ∪ L 2 L 1 ∩ L2 L¯ L 1 ◦ L2 L∗
e
DFA Easy Easy Easy Hard Hard
Regex Easy Hard Hard Easy Easy
NFA Easy Hard Hard Easy Easy

L 1 ∪ L 2 = Union of L 1 and L 2
L 1 ∩ L 2 = Intersection of L 1
and L 2 L¯ = Complement of L
L 1 ◦ L 2 = Concatenation of L 1
and L 2 L∗ = Powers of L
Regular Expressions
Example

Example
Arithmetic expression.
(5 + 3) × 4 = 32 = Number
Regular expression.
(a ∪ b)a∗ = {a, b, aa, ba, aaa, baa, . . .} = Regular
language
Application
Regular expressions in Linux.
Used to find patterns in filenames, file
content etc. Used in Linux tools such as
awk, grep, and Perl.
Google search:
http://www.googleguide.com/advanced_
operators_reference.html
What is a regular expression?

Definition
The following are regular expressions.
ϵ, $, a ∈ Σ.
If R 1 and R 2 are regular expressions, then the
following are regular expressions.
(Union.) R 1 ∪ R 2
(Concatenation.) R 1 ◦ R 2
(Kleene star.) 1∗
R
Examples

Regular language Regular expression


{} $
{ϵ} ϵ
{a} a
{a, b} a∪ b
{a}{b} ab
{a} ∗ = {ϵ, a, aa, aaa, . . .} a∗

{aab} ∗ {a, ab} (aab)∗(a ∪ ab)


({aa, bb} ∪ {a, b}{aa} ∗ {ab, (aa ∪ bb ∪ (a ∪ b)(aa)∗(ab ∪ ba))∗
ba})∗
Equality
Two regular expressions are equal if they describe
the same regular language. E.g.:
(a∗b∗)∗ = (a ∪ b)∗ab(a ∪ b)∗ ∪ b∗a∗ = (a ∪ b)∗ = Σ∗
Examples

Exampl
es
Let Σ = a ∪ b, R+ = R R ∗, and Rk = `R ˛¸
···
R xk
L = {w | |w| = 2} times
R = ΣΣ
L = {w | |w| ≤ 2}
R = ϵ ∪ Σ ∪ ΣΣ
L = {w | |w| ≥ 2}
R = ΣΣΣ ∗
L = {w | n a (w) = 2}
R = b∗ab∗ab∗
L = {w | n a (w) ≤ 2}
R = b∗ ∪ b∗ab∗ ∪
b∗ab∗ab∗ L = {w |
n a (w) ≥ 2}
R = b∗ab∗ab∗(ab∗)∗
Rules

Beware of $ and ϵ
Suppose R is a regular
expression.
R ∪$=
RR R∪ ◦ϵ ϵmay
= not equal
RR
(e.g.: R = 0, L ( R ) = {0}, L ( R ∪ ϵ) = {0,
ϵ})
R ◦ $ may not equal
R
(e.g.: R = 0, L ( R ) = {0}, L ( R ◦ $) =
$)
Rules

Rules
Suppose R 1 , R 2 , R 3 are regular expressions.
Then
R1 $ = $R1 = $
R 1 ϵ = ϵR 1 = R 1 ∪ $ = $ ∪ R 1 =
R1 R1 ∪ R1 = R1
R 1 ∪ R2 = R2 ∪ R1
R1 (R2 ∪ R3 ) = R1 R2 ∪
R 1 R 3 ( R 1 ∪ R 2 )R 3 = R 1 R 3
(ϵ ∪ RR12 )R∗3= ϵR∪1 (RR12)+
∪ R 3=) R =
1

((RR1∗1( ϵR ∪ R13 ) = ϵ ∪ R1 ) R1∗ = R
2 )R

$R∗1∗R=( 2ϵ∪ R 2 = R ∗R 1
R 11(R22 R 1 ) ∗ =
( R11R∪2 )R∗ R)∗
(R 1 = R1∗ R ∗

) R1 = R 2∗R 1) ∗R

( 2 ( 2 2
Construct a regex for Σ = {a, b}

Problem
Construct a regular expression to describe the
language
L = {w | n a (w) is odd}
Construct a regex for Σ = {a, b}

Problem
Construct a regular expression to describe the
language
L = {w | n a (w) is odd}
Solution
b∗ab∗(ab∗expressions.
Incorrect a)∗b∗ D
b∗a(b∗ab∗ab∗)∗ Why?
Correct D
expressions. Why?
D
b∗ab∗(b∗ab∗ab∗)∗ Why?
b∗ab∗(ab∗ab∗)∗ D
Why?
b∗a(b∗ab∗a)∗b∗
D
b∗a(b ∪ ab∗a)∗
Why?
(b ∪ ab∗a)∗ab∗ D
Why?
Construct a regex for Σ = {a, b}

Problem
Construct a regular expression to describe the
language L =
{w | w ends with b and does not contain aa}
Construct a regex for Σ = {a, b}

Problem
Construct a regular expression to describe the
language L =
{w | w ends with b and does not contain aa}
Solution
A string not containing aa means that every a in the
string:
- is immediately followed by b, or
- is the last symbol of the string
Each string in the language has to end with b.
Hence, every a in each string of the language is
immediately followed by b
Regular expression is: (b ∪ ab)+
Construct a regex to recognize identifiers in C

Problem
Identifiers are the names you supply for variables,
types, func- tions, and labels.
Construct a regular expression to recognize the
identifiers in the C programming language i.e., L =
{identifiers in C}
Construct a regex to recognize identifiers in C

Problem
Identifiers are the names you supply for variables,
types, func- tions, and labels.
Construct a regular expression to recognize the
identifiers in the C programming language i.e., L =
{identifiers in C}
Solution
C identifier = FirstLetter
OtherLetters FirstLetter = English
letter or underscore
OtherLetters = Alphanumeric letters or
underscore Let L = {a, . . . , z, A, . . . , Z } and
D = {0, 1, . . . , 9}
Regular expression is:
R = FirstLetter ◦
OtherLetters FirstLetter =
Construct a regex to recognize decimals in C

Problem
Construct a regular expression to recognize the
decimal num- bers in the C programming language
i.e.,
L = {decimal numbers in C}
Examples: 14, +1, −12, 14.3, −.99, 16., 3E14, −1.00E2,
4.1E−
1, and .3E + 2
Construct a regex to recognize decimals in C

Problem
Construct a regular expression to recognize the
decimal num- bers in the C programming language
i.e.,
L = {decimal numbers in C}
Examples: 14, +1, −12, 14.3, −.99, 16., 3E14, −1.00E2,
4.1E−
1, and .3E + 2
Solution
C decimal number = Sign Decimals
Exponent Let D = {0, 1, . . . , 9}
Regular expression is:
R = Sign ◦ Decimals ◦
Exponent Sign = ( + ∪ − ∪ ϵ)
Decimals = ( D + ∪ D + .D ∗ ∪
D ∗ .D + )
Nondeterministic Finite Automata (NFA)
Example NFA’s
Examples
e
a x
star ϵ
t
b u
s ϵ
s u n ϵ
star
t
p y
s ϵ

Difference DFA NFA


Multiple 1 exiting arrow ≥ 0 exiting arrows
transitions
Epsilon C ✓
transitions
Missing No missing transitions Missing transitions
transitions mean transitions to
What is the intuition behind nondeterminism?

Intuition
Nondeterministic computation = Parallel computation
(NFA searches all possible paths in a graph to the
accept state) When NFA has multiple choices for
the same input symbol, think of it as a process
forking multiple processes for parallel computation.
A string is accepted if any of the parallel processes
accepts the string.
Nondeterministic computation = Tree of
possibilities (NFA magically guesses a right path
to the accept state)
Root of the tree is the start of the computation.
Every branching point is the decision-making point
consisting of multiple choices.
Machine accepts a string if any of the paths from the
root of the tree to a leaf leads to an accept state.
Why care for NFA’s?

Uses of NFA’s
Constructing NFA’s is easier than directly
constructing DFA’s for many problems.
Hence, construct NFA’s and then convert them to
DFA’s. NFA’s are easier to understand than DFA’s.
Construct NFA for Σ = {0, 1}

Problem
Construct a NFA that accepts all strings from the
language
L = {strings containing 11 or 101}
Solution
0, 1 ϵ 0, 1
1 1
start q1 q2 q3
q4
0

How does the machine work for the input


010110? What is the equivalent DFA for
solving the problem?
Construct NFA for Σ = {0, 1}

Solution
(continued)

Source: Anil Maheshwari and Michiel Smid’s Theory of


Computation
Construct NFA for Σ = { a }

Problem
Construct a NFA that accepts all strings from the
language
L = {strings of size multiples of 2 or 3 or 5}
Solution
2 a
ϵ
a
ϵ a a
star 3
t
ϵ a a a a a
5

What is the equivalent DFA for


solving the problem?
What is a nondeterministic finite automaton (NFA)?

Nondeterministic = Event paths cannot be determined


precisely Finite = Finite and small amount of space
used
Automaton = Computing machine

Definition
A nondeterministic finite automaton (NFA) M is a 5-
tuple
M = (Q , Σ, δ, q0, F ), where,
1. Q:
3 δ :AQfinite ∪ ϵ)(set
× (Σ set → P of states). D Spacefunction,
is the transition (computer
. memory)
(Q) where
P (Q) is the power set of Q. D Time
2. Σ: A finite set (alphabet).
(computation)
4. q0: The start state (belongs to Q).
5. F : The set of accepting/final states, where F ∈ Q.
Convert NFA to DFA

Problem
Convert the NFA to a
DFA.

Source: Anil Maheshwari and Michiel Smid’s Theory of


Computation
Construct DFA for the given NFA

Solution
NFA M is specified as
Set of states is Q = {1,
2, 3} Set of symbols is Σ
= {a, b} Start state is 1
Set of accept states is F
= {1}
a
Transition function δ is: b ϵ
δ1 {3} $ {2}
2 {1} $ $
3 { 2 } {2, 3} $

How do you convert this NFA to


DFA?
Construct DFA for the given NFA

Solution
NFA M is specified as
Set of states is Q = {1,
2, 3} Set of symbols is Σ
= {a, b} Start state is 1
Set of accept states is F
= {1}
a
Transition function δ is: b ϵ
δ1 {3} $ {2}
2 {1} $ $
3 { 2 } {2, 3} $

How do you convert this NFA to DFA?


If NFA has states Q, then construct a DFA with states
P (Q).
Construct DFA for the given NFA

Solution
(continued)
{1, 2}
$ −→a $ −→a ?
$ →−b $ {1, 2}
a
→−b {3}
{ 1 } −→ $ →−b ?
{ 2 } −→a {1, 3}
a
{1, 2} −→
{2, 3}? →−b
{ 2 } →−b $ {1,
? 3}
{ 3 } −→a →−b2, 3}
{1, ?
{2} {2,
−→ ?a3}
a
−→ ?
{1, 2, 3}
{ 3 } →−b
b
{2, 3} →− ?
Construct DFA for the given NFA
Solution
(continued)

Source: Anil Maheshwari and Michiel Smid’s Theory of


Computation
Construct DFA for the given NFA

Solution
(continued)

Source: Anil Maheshwari and Michiel Smid’s Theory of


Computation
Construct DFA for the given NFA

Convert NFA to DFA


Let N = (Q , Σ, δ, q, F ) be the NFA.
Let M = (Q ' , Σ, δ', q', F ' ) be the DFA. Then
Q ' = P (Q) D Power set
of Q
q' = C є ({q}) D ϵ-closure of the
start state F ' = { S ∈ Q ' | S ∩ F /= $ } D S ∩
F /= $ means that S contains at least one accept
state of N
δ' : Q ' × Σ → Q ' is defined as follows:
For all state S ∈ Q ' and for all letter a ∈ Σ,
t
δ ' (S, a) = s∈S C є (δ(s, a))
Union of NFA

Source: Margaret Fleck and Sariel Har-Peled’s Notes on Theory of


Computation
Concatenation of NFA

Source: Margaret Fleck and Sariel Har-Peled’s Notes on Theory of


Computation
Star of NFA

Source: Margaret Fleck and Sariel Har-Peled’s Notes on Theory of


Computation
Construct a NFA for (aa ∪ aab)∗b

Problem
Construct a NFA for the regular expression (aa ∪
aab)∗b.
Construct a NFA for (aa ∪ aab)∗b

Problem
Construct a NFA for the regular expression (aa ∪
aab)∗b.
Solution

Source: John Martin’s Introduction to Languages and the Theory of


Computation.
Construct a NFA for (aab)∗(a ∪ aba)∗

Problem
Construct a NFA for the regular expression (aab)∗(a ∪
aba)∗.
Construct a NFA for (aab)∗(a ∪ aba)∗

Problem
Construct a NFA for the regular expression (aab)∗(a ∪
aba)∗.
Solution

Source: John Martin’s Introduction to Languages and the Theory of


Computation.
Non-Regular Languages
Regular or non-regular languages

Problems
Let Σ = {a, b} unless mentioned otherwise. Check if
the lan- guages are regular or non-regular (C):
L = {w | w = a n and n ≤ 10100}
L = {w | w = a n and n ≥ 1}
L = {w | w = ambn and m, n ≥ 1}
L = {w | w = a∗b∗}
L = {w | w = anbn and n ≥ 1}
L = {ww R | |w| = 3}
L = {ww R | |w| ≥ 1}
L = {w | w = w R and |w| ≥ 1}
L = {w | w = a 2 i + 1 b 3 j + 2 and i, j ≥ 1}
L = {w | w = a n and n is a
square} L = {w | w = a n and n
2
is a prime} L = {w | w = a i b j
and i, j ≥ 1}
Regular or non-regular languages

Problems
Let Σ = {a, b} unless mentioned otherwise. Check if
the lan- guages are regular or non-regular (C):
L = {w | w = a n and n ≤ 10100}
L = {w | w = a n and n ≥ 1}
L = {w | w = ambn and m, n ≥ 1}
L = {w | w = a∗b∗}
L = {w | w = anbn and n ≥
1} . . . . . . . . . . . . . . . . . . . . . . . . . . C
L = {ww R | |w| = 3}
L = {ww R | |w| ≥
1} . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . C
L = {w | w = w R and |w| ≥ 1} . . . . . . . . . . . . . . . . .
.........C
L = {w | w = a 2 i + 1 b 3 j + 2 and i, j ≥ 1}
L = {w | w = a n and n is a
Regular or non-regular languages

Problems (continued)
L = {w | n a (w) = n b (w)}
L = {w | n a (w) mod 3 ≥ nb(w) mod 5}
L = {w | w = a i b j and j > i ≥ 1}
L = { wxw R | x ∈ Σ∗, |w|, |x| ≥ 1, and |x| ≤ 5}
L = { wxw R | x ∈ Σ∗ and |w|, |x| ≥ 1}
L = { xw w R y | x, y ∈ Σ∗ and |w|, |x|, |y| ≥ 1}
L = { xw w R | x ∈ Σ∗ and |w|, |x| ≥ 1}
L = {ww R y | y ∈ Σ∗ and |w|, |y| ≥ 1}
Regular or non-regular languages

Problems (continued)
L = {w | n a (w) = n b (w)} . . . . . . . . . . . . . . . . . . . . . .
... ......C
L = {w | n a (w) mod 3 ≥ nb(w) mod 5}
L = {w | w = a i b j and j > i ≥
1} .......................C
L = { wxw R | x ∈ Σ∗, |w|, |x| ≥ 1, and |x| ≤ 5} . . . . . .
... .C
L = { wxw R | x ∈ Σ∗ and |w|, |x| ≥ 1}
L = { xw w R y | x, y ∈ Σ∗ and |w|, |x|, |y| ≥ 1}
L = { xw w R | x ∈ Σ∗ and |w|, |x| ≥ 1} . . . . . . . . . . . .
... ....C
L = {ww R y | y ∈ Σ∗ and |w|, |y| ≥
1} ...................C
How to prove that certain languages are not regular?

Pumping lemma
Many languages are not regular.
Pumping lemma is a method to prove that certain
languages are not regular.
Pumping property
If a language is regular, then it must have the
pumping property. If a language does not have the
pumping property, then the language is not regular.
D Proof by
contraposition
How to prove languages non-regular using pumping
lemma? Proof by contradiction.
Assume that the language is regular.
Show that the language does not have the pumping
property. Contradiction! Hence, the language has to
Pumping property of regular languages

Suppose a DFA M with s number of states accepts a


very long string w such that |w| ≥ s from a language
L.
From pigeonhole principle, at least one state is
visited twice. This implies that the string went
through a loop.
Pumping property of regular languages

Observations
Suppose string w has more characters than the
number of states in the DFA, i.e., |w| ≥ s
String w can be split into three parts i.e., w = xyz
where
x: string before the first loop
y: string of the first loop
z: string after the first loop (might contain loops)
Loop must appear i.e., |y| ≥ 1
(x and z can be empty)
Loop must appear in the first s characters of w i.e., |
xy| ≤ s
Pumping property of regular languages

Idea
An infinite number of strings can be pumped with
loop length and they must also be in the language.
Formally, for all i ≥ 0, xy i z must be in the language.
xz, xyz, xyyz, xyyyz, etc must also belong to the
language.
Pumping lemma for regular languages

Theorem
Suppose L is a regular language over alphabet Σ.
Suppose L is accepted by a finite automaton M having
s states. Then, every long string w ∈ L satisfying |w| ≥
s can be split into three strings w = xyz such that the
following three conditions are true.
|xy| ≤ s.
|y| ≥ 1.
For every i ≥ 0, the string xy i z also belongs to L.
L = {a n b n | n ≥ 0} is non-regular

Problem
Prove that L = {a n b n | n ≥ 0} is not a regular
language.
L = {a n b n | n ≥ 0} is non-regular

Problem
Prove that L = {a n b n | n ≥ 0} is not a regular
language.
Solution
Suppose L is regular. Then it must satisfy pumping
Let w = xyz = ap aq s s arbs
property. Suppose w = a b .
where |xy| ≤ s, |y| ≥ 1, and p + q + r
= s. Also, xy i z must belong to L for
all i ≥ 0. But, xyyz is not in L.
Reason: xyyz = apaqaqarbs = as+qbs /∈
L. xyyz has more a’s than b’s.
Contradiction! Hence, L is not
regular.
L = {w | na(w) = n b (w)} is non-
regular
Problem
Prove that L = {w | n a (w) = n b (w)} is not a regular
language.
L = {w | na(w) = n b (w)} is non-
regular
Problem
Prove that L = {w | n a (w) = n b (w)} is not a regular
language.
Solution
Suppose L is regular. Then it must satisfy pumping
Let w = xyz
property. = ϵ w (ab)
Suppose 1 s (ab)s−1
= (ab) .
We have |xy| ≤ s and |y| ≥ 1.
Also, xy i z must belong to L for all i
≥ 0.
xy i z belongs to L for all i ≥ 0.
No contradiction! Hence, L is
regular.
L = {w | na(w) = n b (w)} is non-
regular
Problem
Prove that L = {w | n a (w) = n b (w)} is not a regular
language.
Solution
Suppose L is regular. Then it must satisfy pumping
Let w = xyz
property. = ϵ w (ab)
Suppose 1 s (ab)s−1
= (ab) .
We have |xy| ≤ s and |y| ≥ 1.
Also, xy i z must belong to L for all i ≥ 0.
xy i z belongs to L for all i ≥ 0.
No contradiction! Hence, L is regular.
Mistakes
Incorrect solution! Why? Multiple reasons:
1. If we cannot find a contradiction, that does not prove
anything.
2. We must try for all possible values of x, y such that |
xy| ≤ s.
L = {w | na(w) = n b (w)} is non-
regular
Problem
Prove that L = {w | n a (w) = n b (w)} is not a regular
language.
Solution
Suppose L is regular. Then it must satisfy pumping
Let w = xyz = ap aq s s arbs
property. Suppose w = a b .
where |xy| ≤ s, |y| ≥ 1, and p + q + r
= s. Also, xy i z must belong to L for
all i ≥ 0. But, xyyz is not in L.
Reason: xyyz = apaqaqarbs = as+qbs /∈
L. xyyz has more a’s than b’s.
Contradiction! Hence, L is not
regular.
L = {w | na(w) = n b (w)} is non-
regular
Problem
Prove that L = {w | n a (w) = n b (w)} is not a regular
language.
Solution
Suppose L is regular. Then it must satisfy pumping
Let w = xyz = ap aq s s arbs
property. Suppose w = a b .
where |xy| ≤ s, |y| ≥ 1, and p + q + r
= s. Also, xy i z must belong to L for
all i ≥ 0. But, xyyz is not in L.
Reason: xyyz = apaqaqarbs = as+qbs /∈
L. xyyz has more a’s than b’s.
Contradiction! Hence, L is not
regular.
Takeaway
1. Reduction! Reduce a problem to
Superset of a non-regular language

Problem
{a n b n } is a subset of {w | n a (w) = n b (w)}.
We used the fact that {a n b n } is non-regular to
prove that
{w | n a (w) = n b (w)} is non-regular.
Is a superset of a non-regular language non-regular?
Superset of a non-regular language

Problem
{a n b n } is a subset of {w | n a (w) = n b (w)}.
We used the fact that {a n b n } is non-regular to
prove that
{w | n a (w) = n b (w)} is non-regular.
Is a superset of a non-regular language non-regular?
Solutio
n
No!
Σ is a superset of every non-regular

language. But, it is regular.


L = {w | na(w) = n b (w)} is non-
regular

Problem
Prove that L = {w | n a (w) = n b (w)} is not a regular
language.
Solution (without using pumping
lemma) Suppose L is regular.
We know that L ' = {w | w = a i b j , i, j ≥ 0} is
regular. As regular languages are closed under
intersection,
L ∩ L ' must also be regular.
We see that L ∩ L ' = {w | w = anbn and n ≥ 0}.
But, this language was earlier proved to be non-
regular. Contradiction! Hence, L is not regular.
L = { w w } is non-regular
Problem
Prove that L = {ww} is not a regular
language.
L = { w w } is non-
regular
Problem
Prove that L = {ww} is not a regular language.
Solution
Suppose L is regular. Then it must satisfy pumping
property. Suppose ww = asas.
p a1 as−p−1as
Let ww = xyz = a
We have |xy| ≤ s and |y| ≥ 1.
Also, xy i z must belong to L for all i
≥ 0. But, xyyz is not in L.
Reason: xyyz = apa1a1as−p−1ap = a s + 1 a s /∈
L. xyyz has odd number of a’s.
Contradiction! Hence, L is not regular.
L = { w w } is non-
regular
Problem
Prove that L = {ww} is not a regular language.
Solution
Suppose L is regular. Then it must satisfy pumping
property. Suppose ww = asas.
p a1 as−p−1as
Let ww = xyz = a
We have |xy| ≤ s and |y| ≥ 1.
Also, xy i z must belong to L for all i
≥ 0. But, xyyz is not in L.
Reason: xyyz = apa1a1as−p−1ap = a s + 1 a s /∈
L. xyyz has odd number of a’s.
Contradiction! Hence, L is not regular.
Mistakes
Incorrect solution! Why?
1. We must try all possible values of x, y
such that |xy| ≤ s.
2 4
L = { w w } is non-
regular

Problem
Prove that L = {ww} is not a regular
language.
L = { w w } is non-
regular

Problem
Prove that L = {ww} is not a regular language.
Solution
Suppose L is regular. Then it must satisfy pumping
property. Suppose ww p
= qasbsasrbss. s s
Let ww = xyz = a a abab
where |xy| ≤ s, |y| ≥ 1, and p + q + r
= s. Also, xy i z must belong to L for
all i ≥ 0. But, xyyz is not in L.
Reason: xyyz = apaqaqarbsasbs = as+qbsasbs /∈
L. Contradiction! Hence, L is not regular.
L = {w | w = a n , n is a square} is non-
regular
Problem
2
Prove that L = {w | w = an } is not a regular
language.
L = {w | w = a n , n is a square} is non-regular

Problem
2
Prove that L = {w | w = an } is not a regular
language. Solution
Suppose L is regular. Then it must satisfy pumping
2
property. Suppose w = as .
p q a r a s −s
2
Let w = xyz = a a
where |xy| ≤ s, |y| ≥ 1, and p + q + r
= s. Also, xy i z must belong to L for
all i ≥ 0. But, xyyz is not in L.
2 2
Reason: xyyz = apaqaqaras −s = as +q /∈
L. Because, s2 < s2 + q < (s + 1)2.
Contradiction! Hence, L is not regular.
L = {w | w = a n , n is prime} is non-
regular

Problem
Prove that L = {w | w = a n , n is prime} is not
regular.
L = {w | w = a n , n is prime} is non-regular

Problem
Prove that L = {w | w = a n , n is prime} is not
regular.
Solution
Suppose L is regular. p
Then
q r
it must satisfy pumping
Let w = xyz = a a a
property. Suppose w = a , where m is prime and m ≥
m

where |xy| ≤ s, |y| ≥ 1, and p + q + r =


s.
m. Also, xy i z must belong to L for all i
≥ 0.
But, x y m + 1 z is not in L.
Reason: x y m + 1 z = a p a q ( m + 1 ) a r = a m ( q + 1 ) /∈
L. Contradiction! Hence, L is not regular.
L = {w | w = ambn, m > n } is non-regular

Problem
Prove that L = {w | w = ambn, m > n } is not
regular.
L = {w | w = ambn, m > n } is non-regular

Problem
Prove that L = {w | w = ambn, m > n } is not regular.
Solution
Suppose L is regular. Then it must satisfy pumping
property. Supposep w = qas+1 brs .s
Let w = xyz = a a ab
where |xy| ≤ s, |y| ≥ 1, and p + q + r = s
+ 1. Also, xy i z must belong to L for all i
≥ 0.
But, xz is not in L. D Pumping
down Reason: xz = a a b = a b /∈ L.
p r s p + r s

Because, p + r ≤ s i.e., #a’s is not greater than


#b’s. Contradiction! Hence, L is not regular.
L = {w | w = ambn, m /= n } is non-regular

Problem
Prove that L = {w | w = ambn, m /= n } is not
regular.
L = {w | w = ambn, m /= n } is non-regular

Problem
Prove that L = {w | w = ambn, m /= n } is not regular.
Solution
Suppose L is regular. Then it must satisfy pumping
property. Supposep w = qasbs+s!r. s+s!
Let w = xyz = a a ab
where |xy| ≤ s, |y| ≥ 1, and p + q + r
= s. Also, xy i z must belong to L for
all i ≥ 0. But, xy i z is not in L for
some i.
We pump aq to get as+s! bs+s! .
Reason: xy i z = a p a q i a r b s + s ! = a s + ( i− 1 ) q b s + s ! /∈
L. This means (i − 1)q = s! =⇒ i = s!/q + 1.
Contradiction! Hence, L is not regular.
L = {w | w = ambn, m /= n } is non-regular

Problem
Prove that L = {w | w = ambn, m /= n } is not regular.
Solution (without using pumping
lemma) Suppose L is regular.
We know that L ' = {w | w = a i b j , i, j ≥ 0} is
regular. Let L ' ' = {w | w = anbn, n ≥ 0}.
As regular languages are closed under intersection and
comple- mentation, L = L ' − L ' ' = L ' ∩ L¯'' is regular.
This implies that L ' ' is regular.
But, the language L ' ' was earlier proved to be non-
regular. Contradiction! Hence, L is not regular.

You might also like