Finite Automata: Part Three
Finite Automata: Part Three
Part Three
Hello
Hello Condensed
Condensed Slide
Slide Readers!
Readers!
The
The frss
frss half
half of
of shis
shis lecsurre
lecsurre consisss
consisss
almoss
almoss exclursively
exclursively of
of animasions
animasions of of she
she
surbses
surbses conssrurcsion,
conssrurcsion, which
which aren’s
aren’s presens
presens
here
here in
in she
she condensed
condensed version
version of
of shese
shese
slides.
slides. Your
Your may
may wans
wans so
so see
see she
she furll
furll
version
version of
of she
she slides
slides for
for more
more consexs.
consexs.
Enjoy!
Enjoy!
-Keish
-Keish
Recap from Last Time
Tabular DFAs
1 0
start 0 1 0
q0 q1 q2 q3
Σ
1
0 1
*q0 q1 q0
These
These ssars
ssars
q1 q3 q2
indicase
indicase accepsing
accepsing
ssases.
ssases.
q2 q3 q0
*q3 q3 q3
Tabular DFAs
1 0
start 0 1 0
q0 q1 q2 q3
Σ
1
0 1
*q0 q1 q0
Since
Since shis
shis isis she
she
q1 q3 q2
frss
frss row,
row, is's
is's she
she
ssars
ssars ssase.
ssase.
q2 q3 q0
*q3 q3 q3
A language L is called a regular language
if there exists a DFA D such that ℒ(D) = L.
NFAs
●
An NFA is a
●
Nondeterministic
●
Finite
●
Automaton
●
Can have missing transitions or multiple
transitions defned on the same input
symbol.
●
Accepts if any possible series of choices
leads to an accepting state.
ε-Transitions
●
NFAs have a special type of transition called
the ε-transition.
●
An NFA may follow any number of ε-transitions
at any time without consuming any input.
a
start a a
q0 q1 q2
ε ε
q30 b, ε q4 b q5
b
Massive Parallelism
●
An NFA can be thought of as a DFA that
can be in many states at once.
●
At each point in time, when the NFA
needs to follow a transition, it tries all
the options at the same time.
●
The NFA accepts if any of the states that
are active at the end are accepting
states. It rejects otherwise.
Just how powerful are NFAs?
New Stuf!
NFAs and DFAs
●
Any language that can be accepted by a
DFA can be accepted by an NFA.
●
Why?
●
Every DFA essentially already is an NFA!
●
Question: Can any language accepted by
an NFA also be accepted by a DFA?
●
Surprisingly, the answer is yes!
Thought Experiment:
How would you simulate an NFA in
software?
Σ
start a b a
q₀ q₁ q₂ q₃
a b
{q₀} {q₀, q₁} {q₀}
{q₀, q₁} {q₀, q₁} {q₀, q₂}
{q₀, q₂} {q₀, q₁, q₃} {q₀}
*{q₀, q₁, q₃} {q₀, q₁} {q₀, q₂}
b a
start a
{q₀} {q₀, q₁}
b a
b
a
{q₀, q₂} b {q₀, q₁, q₃}
Once More, With Epsilons!
Answer
AnsweratatPollEv.com/cs103
PollEv.com/cs103or or
text CS103 to 22333 once to join, then A,B,
text CS103 to 22333 once to join, then A, B,C, orDD
C,or
q₂ a b
{q₀, q₃} {q₁, q₄} {q₄}
Σ b {q₁, q₄} Ø {q₂, q₃}
start a {q₄} Ø {q₃}
q₀ q₁
{q₂, q₃}
ε What
Σ Whatshould
shouldthis
thisrow
rowlook
looklike?
like?
q₃ b q₄ A {q₂, q₃} {q₀, q₃, q₄} {q₀, q₃, q₄}
D {q₂, q₃} Ø Ø
Once More, With Epsilons!
text
Answer
textCS103
CS103to
atPollEv.com/cs103
Answerat
to22333
PollEv.com/cs103or
22333once
onceto
tojoin,
or
thenaanumber
join,then number
q₂ a b
{q₀, q₃} {q₁, q₄} {q₄}
Σ b {q₁, q₄} Ø {q₂, q₃}
start a {q₄} Ø {q₃}
q₀ q₁
{q₂, q₃} {q₀, q₃, q₄} {q₀, q₃, q₄}
ε {q₃} {q₄} {q₄}
Σ
{q₀, q₃, q₄} {q₁, q₄} {q₃, q₄}
q₃ b q₄
{q₃, q₄} {q₄} {q₃, q₄}
Ø Ø Ø
How
Howmany
manyof
ofthese
theserows
rowsshould
shouldbe
bemarked
marked
as
asaccepting
acceptingstates?
states?
Once More, With Epsilons!
q₂ a b
*{q₀, q₃} {q₁, q₄} {q₄}
Σ b *{q₁, q₄} Ø {q₂, q₃}
start a {q₄} Ø {q₃}
q₀ q₁
*{q₂, q₃} {q₀, q₃, q₄} {q₀, q₃, q₄}
ε *{q₃} {q₄} {q₄}
Σ
*{q₀, q₃, q₄} {q₁, q₄} {q₃, q₄}
q₃ b q₄
*{q₃, q₄} {q₄} {q₃, q₄}
Ø Ø Ø
The Subset Construction
●
This construction for transforming an NFA into a DFA is
called the subset construction (or sometimes the
powerset construction).
●
Each state in the DFA is associated with a set of states in the NFA.
●
The start state in the DFA corresponds to the start state of the
NFA, plus all states reachable via ε-transitions.
●
If a state q in the DFA corresponds to a set of states S in the NFA,
then the transition from state q on a character a is found as
follows:
– Let S' be the set of states in the NFA that can be reached by following a
transition labeled a from any of the states in S. (This set may be empty.)
– Let S'' be the set of states in the NFA reachable from some state in S' by
following zero or more epsilon transitions.
– The state q in the DFA transitions on a to a DFA state corresponding to the
set of states S''.
●
Read Sipser for a formal account.
The Subset Construction
●
In converting an NFA to a DFA, the DFA's
states correspond to sets of NFA states.
●
Useful fact: |℘(S)| = 2|S| for any fnite
set S.
●
In the worst-case, the construction can
result in a DFA that is exponentially
larger than the original NFA.
●
Interesting challenge: Find a language
for which this worst-case behavior occurs
(there are infnitely many of them!)
A language L is called a regular language
if there exists a DFA D such that ℒ(D) = L.
An Important Result
ε Machine for L1
start
Hey,
Hey, it's
it's De
De
Morgan's
Morgan's laws!
laws!
L1 ∪ L2
Concatenation
String Concatenation
●
If w ∈ Σ* and x ∈ Σ*, the concatenation of w and x,
denoted wx, is the string formed by tacking all the
characters of x onto the end of w.
●
Example: if w = quo and x = kka, the concatenation
wx = quokka.
●
Analogous to the + operator for strings in many
programming languages.
●
Some facts about concatenation:
●
The empty string ε is the identity element for concatenation:
wε = εw = w
●
Concatenation is associative:
wxy = w(xy) = (wx)y
Concatenation
●
The concatenation of two languages L₁
and L₂ over the alphabet Σ is the language
L₁L₂ = { wx ∈ Σ* | w ∈ L₁ ∧ x ∈ L₂ }
=Two views of L₁L₂:
The set of all strings that can be made by
concatenating a string in L₁ with a string in L₂.
The set of strings that can be split into two
pieces: a piece from L₁ and a piece from L₂.
Conceptually similar to the Cartesian
product of two sets, only with strings.
Concatenation Example
●
Let Σ = { a, b, …, z, A, B, …, Z } and consider
these languages over Σ:
●
Noun = { Puppy, Rainbow, Whale, … }
●
Verb = { Hugs, Juggles, Loves, … }
●
The = { The }
●
The language TheNounVerbTheNoun is
●
{ ThePuppyHugsTheWhale,
TheWhaleLovesTheRainbow,
TheRainbowJugglesTheRainbow, … }
Concatenation
●
The concatenation of two languages L₁
and L₂ over the alphabet Σ is the language
L₁L₂ = { wx ∈ Σ* | w ∈ L₁ ∧ x ∈ L₂ }
●
Two views of L₁L₂:
●
The set of all strings that can be made by
concatenating a string in L₁ with a string in L₂.
●
The set of strings that can be split into two
pieces: a piece from L₁ and a piece from L₂.
●
Conceptually similar to the Cartesian
product of two sets, only with strings.
Concatenating Regular Languages
● If L1 and L2 are regular languages, is L1L2?
●
Intuition – can we split a string w into two
strings xy such that x ∈ L1 and y ∈ L2?
● Idea: Run the automaton for L1 on w, and
whenever L1 reaches an accepting state,
start start
optionally hand the rest of w to L2.
● If L2 accepts the remainder, then L1 accepted the
Machine
first part and for L1 is in
the string Machine
L1L2. for L 2
● If L2 rejects the remainder, then the split was
incorrect.
b o o k k e e p e r
Concatenating Regular Languages
● If L1 and L2 are regular languages, is L1L2?
●
Intuition – can we split a string w into two
strings xy such that x ∈ L1 and y ∈ L2?
● Idea: Run the automaton for L1 on w, and
whenever L1 reaches an accepting state,
optionally hand the rest of w to L2.
● If L2 accepts the remainder, then L1 accepted the
first part and the string is in L1L2.
● If L2 rejects the remainder, then the split was
incorrect.
Concatenating Regular Languages
ε
start
ε
Machine for
L2
Machine for
L1
Machine for L1L2
Lots and Lots of Concatenation
●
Consider the language L = { aa, b }
●
LL is the set of strings formed by concatenating pairs of
strings in L.
{ aaaa, aab, baa, bb }
●
LLL is the set of strings formed by concatenating triples
of strings in L.
{ aaaaaa, aaaab, aabaa, aabb, baaaa, baab, bbaa, bbb}
●
LLLL is the set of strings formed by concatenating
quadruples of strings in L.
{ aaaaaaaa, aaaaaab, aaaabaa, aaaabb, aabaaaa,
aabaab, aabbaa, aabbb, baaaaaa, baaaab, baabaa,
baabb, bbaaaa, bbaab, bbbaa, bbbb}
Language Exponentiation
●
We can define what it means to “exponentiate” a
language as follows:
●
L0 = {ε}
●
The set containing just the empty string.
●
Idea: Any string formed by concatenating zero strings
together is the empty string.
●
Ln+1 = LLn
●
Idea: Concatenating (n+1) strings together works by
concatenating n strings, then concatenating one
more.
●
Question: Why define L0 = {ε}?
The Kleene Closure
●
An important operation on languages is
the Kleene Closure, which is defined as
L* = { w ∈ Σ* | ∃n ∈ ℕ. w ∈ Ln }
●
Mathematically:
w ∈ L* if ∃n ∈ ℕ. w ∈ Ln
●
Intuitively, all possible ways of
concatenating zero or more strings in L
together, possibly with repetition.
The Kleene Closure
If L = { a, bb }, then L* = {
ε,
a, bb,
aa, abb, bba, bbbb,
aaa, aabb, abba, abbbb, bbaa, bbabb, bbbba, bbbbbb,
…
}
Think
Think of
of LL* as
*
as the
the set
set of
of strings
strings you
you
can
can make
make ifif you
you have
have aa collection
collection of
of
stamps
stamps –– one
one for
for each
each string
string inin LL ––
and
and you
you form
form every
every possible
possible string
string
that
that can
can be
be made
made from
from those
those stamps.
stamps.
Reasoning about Infinity
●
If L is regular, is L* necessarily regular?
●
⚠ A Bad Line of Reasoning: ⚠
●
L0 = { ε } is regular.
●
L1 = L is regular.
●
L2 = LL is regular
●
L3 = L(LL) is regular
●
…
●
Regular languages are closed under union.
●
So the union of all these languages is
regular.
Reasoning About the Infinite
●
If a series of finite objects all have some
property, the “limit” of that process does
not necessarily have that property.
●
In general, it is not safe to conclude that
some property that always holds in the
finite case must hold in the infinite case.
●
(This is why calculus is interesting).
Idea: Can we directly convert an NFA for
language L to an NFA for language L*?
The Kleene Star
start
Machine for L
The Kleene Star
ε
ε
start ε
Question:
Question: Why
Why add
add the
the new
new
state
state out
out front?
front? Why
Why not
not
just
just make
make the
the old
old start
start Machine for L
state
state accepting?
accepting?
Machine for L*
Closure Properties
●
Theorem: If L₁ and L₂ are regular
languages over an alphabet Σ, then so are
the following languages:
●
L₁
●
L₁ ∪ L₂
●
L₁ ∩ L₂
●
L₁L₂
●
L₁*
●
These properties are called closure
properties of the regular languages.