Introduction Formal Definitions and Notation
Deterministic Finite-State Automata
Meenakshi D’Souza
IIIT-Bangalore
Introduction Formal Definitions and Notation
Example DFA 1
DFA for “Odd number of a’s”
b b
a
How a DFA works.
Introduction Formal Definitions and Notation
Example DFA 1
DFA for “Odd number of a’s”
b b
a
e o
a
How a DFA works.
Each state represents a property of the input string read so
far:
State e: Number of a’s seen is even.
State o: Number of a’s seen is odd.
Introduction Formal Definitions and Notation
Example DFA 2
DFA for “Contains the substring abb”
b a a, b
a b b
Each state represents a property of the input string read so far:
Introduction Formal Definitions and Notation
Example DFA 2
DFA for “Contains the substring abb”
b a a, b
a b b
a ab abb
a
Each state represents a property of the input string read so far:
State : Not seen abb and no suffix in a or ab.
State a: Not seen abb and has suffix a.
State ab: Not seen abb and has suffix ab.
State abb: Seen abb.
Introduction Formal Definitions and Notation
Example DFA 3
Accept strings over {0, 1} which have even parity in each length 4
block.
Accept “0101 · 1010”
Reject “0101 · 1011”
DFA for “Even parity checker”
0
1, e 0 2, e 0 3, e
0 1
1 1
0, 1
0, e 1 1
0, o
1 0
1, o 0 2, o 0 3, o
1
Introduction Formal Definitions and Notation
Example DFA 4
Accept strings over {a, b, /, ∗} which don’t end inside a C-style
comment (nested comments not considered).
Scan from left to right till first “/*” is encountered; from
there to next “*/” is first comment; and so on.
Accept “ab/ ∗ aaa ∗ /abba” and “ab/ ∗ aa/ ∗ aa ∗ /bb ∗ /”.
Reject “ab/ ∗ aaa∗” and “ab/ ∗ aa/ ∗ aa ∗ /bb/ ∗ a”.
Introduction Formal Definitions and Notation
Example DFA 4
Accept strings over {a, b, /, ∗} which don’t end inside a C-style
comment (nested comments not considered).
Scan from left to right till first “/*” is encountered; from
there to next “*/” is first comment; and so on.
Accept “ab/ ∗ aaa ∗ /abba” and “ab/ ∗ aa/ ∗ aa ∗ /bb ∗ /”.
Reject “ab/ ∗ aaa∗” and “ab/ ∗ aa/ ∗ aa ∗ /bb/ ∗ a”.
DFA for “C-comment tracker”
/
a, ∗ / a, /
/ ∗ ∗ ∗
out pbc in pec
a a
Introduction Formal Definitions and Notation
Definitions and notation
An alphabet is a finite set of symbols or “letters”. Eg.
A = {a, b, c} or Σ = {0, 1}.
A string or word over an alphabet Σ is a finite sequence of
letters from Σ. Eg. aaba is string over {a, b, c}.
Empty string denoted by .
Set of all strings over Σ denoted by Σ∗ .
What is the “size” or “cardinality” of Σ∗ ?
Introduction Formal Definitions and Notation
Definitions and notation
An alphabet is a finite set of symbols or “letters”. Eg.
A = {a, b, c} or Σ = {0, 1}.
A string or word over an alphabet Σ is a finite sequence of
letters from Σ. Eg. aaba is string over {a, b, c}.
Empty string denoted by .
Set of all strings over Σ denoted by Σ∗ .
What is the “size” or “cardinality” of Σ∗ ?
Infinite but Countable: Can enumerate in lexicographic order:
, a, b, c, aa, ab, . . .
.
Introduction Formal Definitions and Notation
Definitions and notation
An alphabet is a finite set of symbols or “letters”. Eg.
A = {a, b, c} or Σ = {0, 1}.
A string or word over an alphabet Σ is a finite sequence of
letters from Σ. Eg. aaba is string over {a, b, c}.
Empty string denoted by .
Set of all strings over Σ denoted by Σ∗ .
What is the “size” or “cardinality” of Σ∗ ?
Infinite but Countable: Can enumerate in lexicographic order:
, a, b, c, aa, ab, . . .
.
Operation of concatenation on words: String u followed by
string v : written u · v or simply uv .
Eg. aabb · aaa = aabbaaa.
Introduction Formal Definitions and Notation
Definitions and notation: Languages
A language over an alphabet Σ is a set of strings over Σ. Eg.
for Σ = {a, b, c}:
L = {abc, aaba}.
L1 = {, b, aa, bb, aab, aba, baa, bbb, . . .}.
L2 = {}.
L3 = {}.
How many languages are there over a given alphabet Σ?
Introduction Formal Definitions and Notation
Definitions and notation: Languages
A language over an alphabet Σ is a set of strings over Σ. Eg.
for Σ = {a, b, c}:
L = {abc, aaba}.
L1 = {, b, aa, bb, aab, aba, baa, bbb, . . .}.
L2 = {}.
L3 = {}.
How many languages are there over a given alphabet Σ?
Uncountably infinite
Use a diagonalization argument:
a b aa ab ba bb aaa aab aba abb bbb ···
L0 0 1 0 0 0 1 1 0 0 0 0 0 ···
L1 0 0 0 0 0 0 0 0 0 0 0 0 ···
L2 1 1 0 1 0 1 1 0 0 1 0 1 ···
L3 0 0 0 0 0 0 0 0 0 0 0 0 ···
L4 0 1 0 0 0 1 1 0 0 0 0 0 ···
L5 1 1 0 1 0 1 1 0 0 1 0 1 ···
L6 0 1 0 0 0 1 1 0 0 0 0 0 ···
L7 0 0 0 0 0 0 1 0 0 0 1 0 ···
.
.
.
Introduction Formal Definitions and Notation
Definitions and notation: Languages
Concatenation of languages:
L1 · L2 = {u · v | u ∈ L1 , v ∈ L2 }.
Eg. {abc, aaba} · {, a, bb} =
{abc, aaba, abca, aabaa, abcbb, aababb}.
Introduction Formal Definitions and Notation
Definitions and notation: DFA
A Deterministic Finite-State Automaton A over an alphabet Σ is a
structure of the form
(Q, s, δ, F )
where
Q is a finite set of “states”
s ∈ Q is the “start” state
δ : Q × Σ → Q is the “transition function.”
F ⊆ Q is the set of “final” states.
Introduction Formal Definitions and Notation
Definitions and notation: DFA
A Deterministic Finite-State Automaton A over an alphabet Σ is a
structure of the form
(Q, s, δ, F )
where
Q is a finite set of “states”
s ∈ Q is the “start” state
δ : Q × Σ → Q is the “transition function.”
F ⊆ Q is the set of “final” states.
Example of “Odd a’s” DFA:
Here: Q = {e, o}, s = e, F = {o},
and δ is given by:
b b
a
δ(e, a) = o,
δ(e, b) = e,
e o
δ(o, a) = e, a
δ(o, b) = o.
Introduction Formal Definitions and Notation
Definitions and notation: Language accepted by a DFA
δb tells us how the DFA A behaves on a given word u.
Define δb : Q × Σ∗ → Q as
δ(q,
b ) = q
b w · a) = δ(δ(q,
δ(q, b w ), a).
Language accepted by A, denoted L(A), is defined as:
L(A) = {w ∈ Σ∗ | δ(s,
b w ) ∈ F }.
Eg. For A = DFA for “Odd a’s”,
L(A) = {a, ab, ba, aaa, abb, bab, bba, . . .}.
Introduction Formal Definitions and Notation
Regular Languages
A language L ⊆ Σ∗ is called regular if there is a DFA A over
Σ such that L(A) = L.
Examples of regular languages: “Odd a’s”, “strings that don’t
end inside a C-style comment”, {}, any finite language.
All languages over A
Regular
Are there non-regular languages?
Introduction Formal Definitions and Notation
Regular Languages
A language L ⊆ Σ∗ is called regular if there is a DFA A over
Σ such that L(A) = L.
Examples of regular languages: “Odd a’s”, “strings that don’t
end inside a C-style comment”, {}, any finite language.
All languages over A
Regular
Are there non-regular languages?
Yes, uncountably many, since the class of regular languages is
countable while class of all languages is uncountable.