Toan Roi Rac 2 Nguyen An Khuong, Huynh Tuong Nguyen Dm2 Ch4 Automata (Cuuduongthancong - Com)
Toan Roi Rac 2 Nguyen An Khuong, Huynh Tuong Nguyen Dm2 Ch4 Automata (Cuuduongthancong - Com)
Nguyen An Khuong,
Huynh Tuong Nguyen,
Chapter 4
Bui Hoai Thang
Automata
Discrete Mathematics II
Contents
Motivation
Regular expression or
rationnal expression
(Materials drawn from this chapter in: Non-deterministic
- Peter Linz. An Introduction to Formal Languages and Automata, (5th Ed.), finite automata
Jones & Bartlett Learning, 2011. Deterministic finite
- John E. Hopcroft, Rajeev Motwani and Jeffrey D. Ullamn. Introduction to automata
Automata Theory, Languages, and Computation (3rd Ed.), Prentice Hall, Recognized languages
2006. Determinisation
1 Motivation
Non-deterministic
finite automata
5 Deterministic finite automata Deterministic finite
automata
Recognized languages
6 Recognized languages Determinisation
7 Determinisation
ng.com https://fb.com/tailieudientucntt
4.2
Automata
Introduction
Nguyen An Khuong,
Huynh Tuong Nguyen,
Bui Hoai Thang
Standard states of a process in operating system
Contents
Resource
Motivation
Regular expression or
rationnal expression
Resource Non-deterministic
finite automata
Recognized languages
CPU Resource Determinisation
Running
ng.com https://fb.com/tailieudientucntt
4.3
Automata
Why study automata theory?
Nguyen An Khuong,
Huynh Tuong Nguyen,
Bui Hoai Thang
A useful model
for many important kinds of software and hardware
Contents
1 designing and checking the behaviour of digital circuits
Motivation
2 lexical analyser of a typical compiler: a compiler component Alphabets, words and
that breaks the input text into logical units languages
Regular expression or
3 scanning large bodies of text, such as collections of Web rationnal expression
4 verifying pratical systems of all types that have a finite Deterministic finite
automata
number of distinct states, such as communications protocols Recognized languages
of protocols for secure exchange information, etc. Determinisation
ng.com https://fb.com/tailieudientucntt
4.4
Automata
Alphabets, symbols
Nguyen An Khuong,
Huynh Tuong Nguyen,
Bui Hoai Thang
Definition
Alphabet Σ (bảng chữ cái) is a finite and non-empty set of
symbols (or characters).
For example:
Contents
• Σ = {a, b} Motivation
Deterministic finite
Remark automata
Recognized languages
Σ is almost always all available characters (lowercase letters, Determinisation
capital letters, numbers, symbols and special characters such as
space or newline).
But nothing prevents to imagine other sets.
ng.com https://fb.com/tailieudientucntt
4.5
Automata
Strings (words)
Nguyen An Khuong,
Huynh Tuong Nguyen,
Bui Hoai Thang
Definition
Deterministic finite
automata
∗ Determinisation
The purpose aims to analyze a string of Σ in order to know
whether it belongs or not to L.
ng.com https://fb.com/tailieudientucntt
4.6
Automata
Example
Nguyen An Khuong,
Huynh Tuong Nguyen,
Bui Hoai Thang
Let Σ = {0, 1}
• ∅ is a language over Σ . It’s called the empty language. Alphabets, words and
languages
∗
• Σ is a language over Σ . It’s called the universal language. Regular expression or
rationnal expression
• {ε} is a language over Σ . Non-deterministic
finite automata
• {0, 00, 001} is also a language over Σ .
Deterministic finite
• The set of strings which contain an odd number of 0 is a language automata
Determinisation
• The set of strings that contain as many of 1 as 0 is a language
over Σ.
ng.com https://fb.com/tailieudientucntt
4.7
Automata
String concatenation
Nguyen An Khuong,
Huynh Tuong Nguyen,
Bui Hoai Thang
Concatenating the empty string ε and the string 110 is the string Motivation
Regular expression or
rationnal expression
Definition
Non-deterministic
String concatenation is an application of Σ∗ × Σ∗ to Σ∗ . finite automata
Recognized languages
Determinisation
ng.com https://fb.com/tailieudientucntt
4.8
Automata
Languages
Nguyen An Khuong,
Huynh Tuong Nguyen,
Bui Hoai Thang
Specifying languages
A language can be specified in several ways:
a) enumeration of its words, for example:
• L1 = {ε, 0, 1},
• L2 = {a, aa, aaa, ab, ba}, Contents
• L3 = {ε, ab, aabb, aaabbb, aaaabbbb, . . .}, Motivation
b) a property, such that all words of the language have this property Alphabets, words and
languages
but other words have not, for example: Regular expression or
• L4 = {an bn |n = 0, 1, 2, . . .}, rationnal expression
• L5 = {uu−1 |u ∈ Σ∗ }, Non-deterministic
finite automata
• L6 = {u ∈ {a, b}∗ |na (u) = nb (u)} where na (u) denotes the Deterministic finite
automata
number of letter ’a’ in word u.
Recognized languages
c) its grammar, for example:
Determinisation
• Let G = (N, T, P, S) where
N = {S}, T = {a, b}, P = {S → aSb, S → ab}
i.e. L(G) = {an bn |N ≥ 1} since
S ⇒ aSb ⇒ a2 Sb2 ⇒ . . . ⇒ an Sbn
ng.com https://fb.com/tailieudientucntt
4.9
Automata
Operations on languages
Nguyen An Khuong,
Huynh Tuong Nguyen,
L, L1 , L2 are languages over Σ Bui Hoai Thang
• union
L1 ∪ L2 = {u ∈ Σ∗ | u ∈ L1 or u ∈ L2 },
• intersection
L1 ∩ L2 = {u ∈ Σ∗ | u ∈ L1 and u ∈ L2 },
• difference Contents
L1 \ L2 = {u ∈ Σ∗ | u ∈ L1 and u 6∈ L2 }, Motivation
• complement Alphabets, words and
L = Σ∗ \ L, languages
L∗ = Li = L0 ∪ L ∪ L2 ∪ · · · ∪ Li ∪ · · · , Determinisation
i=0
We will use also the notation L+
[∞
L+ = Li = L ∪ L2 ∪ · · · ∪ Li ∪ · · · .
i=1
The union, product and iteration
ng.com are called regular operations.
https://fb.com/tailieudientucntt
4.10
Automata
Example
Nguyen An Khuong,
Huynh Tuong Nguyen,
Bui Hoai Thang
Non-deterministic
• caab, caaa, cab, caca, cabac, finite automata
Recognized languages
Determinisation
ng.com https://fb.com/tailieudientucntt
4.11
Automata
Exercise
Nguyen An Khuong,
Huynh Tuong Nguyen,
Bui Hoai Thang
Let Σ = {a, b, c}
Give at least 3 strings for each of the following languages
1) all strings with exactly one ’a’.
2) all strings of even length. Contents
Regular expression or
5) all strings not ending with ’a’. rationnal expression
10) all strings including at least one ’a’ and whose the first appearance
of ’a’ is not followed by a ’c’.
ng.com https://fb.com/tailieudientucntt
4.12
Automata
Exercise
Nguyen An Khuong,
Huynh Tuong Nguyen,
Bui Hoai Thang
Motivation
3) bbb, Alphabets, words and
languages
4) aab,
Regular expression or
5) cc, rationnal expression
Non-deterministic
6) aaaabaaaa = a4 ba4 , finite automata
Deterministic finite
7) cabbbbaaaaaaaaab = cab3 a9 b, automata
Determinisation
9) baaaaabaac = ba5 ba2 c,
10) baca ?.
ng.com https://fb.com/tailieudientucntt
4.13
Automata
Regular expressions
Nguyen An Khuong,
Huynh Tuong Nguyen,
Bui Hoai Thang
Regular expressions (biểu thức chính quy)
Permit to specify a language with strings consist of letters and ε,
parentheses (), operating symbols +, ., ∗. This string can be
empty, denoted ∅.
Contents
Regular expression or
• product of concatenation rationnal expression
Non-deterministic
• transitive closure ∗ finite automata
Deterministic finite
automata
Determinisation
Regular expression or
(x + y) + z ≡ x + (y + z) rationnal expression
Non-deterministic
(xy)z ≡ x(yz) finite automata
∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ Determinisation
(x + y) ≡ (x + y) ≡ (x + y ) ≡ (x + y )
(x + y)∗ ≡ (x∗ y ∗ )∗
(x∗ )∗ ≡ x∗
∗
x x ≡ xx∗
ng.com https://fb.com/tailieudientucntt
xx∗ + ε ≡ x∗ 4.15
Automata
Regular expressions
Nguyen An Khuong,
Huynh Tuong Nguyen,
Bui Hoai Thang
Contents
Non-deterministic
finite automata
Deterministic finite
automata
Recognized languages
Determinisation
ng.com https://fb.com/tailieudientucntt
4.16
Automata
Exercise
Nguyen An Khuong,
Huynh Tuong Nguyen,
Bui Hoai Thang
Let Σ = {a, b, c}
Give at least 3 words for each language represented by the
following regular expressions Contents
1) a∗ + b∗ , Motivation
Regular expression or
3) b(ca + ac)(aa)∗ + a∗ (a + b), rationnal expression
Deterministic finite
automata
Example Recognized languages
ng.com https://fb.com/tailieudientucntt
4.17
Automata
Exercise
Nguyen An Khuong,
Huynh Tuong Nguyen,
Bui Hoai Thang
Motivation
∗
2) b , Alphabets, words and
languages
3) aab + cab∗ ac,
Regular expression or
4) b(ca + ac)(aa)∗ + a∗ (a + b), rationnal expression
Non-deterministic
5) (aaaabaaa)2∗ c, finite automata
Deterministic finite
6) b+ ac (b+ = bb∗ ), automata
Determinisation
∗
Define a (simple) regular expression representing the language L .
ng.com https://fb.com/tailieudientucntt
4.18
Automata
Finite automata
Nguyen An Khuong,
Huynh Tuong Nguyen,
Bui Hoai Thang
Regular expression or
rationnal expression
b Non-deterministic
a, b finite automata
Deterministic finite
automata
q0 q1 Recognized languages
Determinisation
Regular expression
b∗ (a + b)
ng.com https://fb.com/tailieudientucntt
4.19
Automata
Exercise
Nguyen An Khuong,
Huynh Tuong Nguyen,
Let Σ = {a, b} Bui Hoai Thang
4) a4 ba4 , Motivation
7) ba5 b2 , Non-deterministic
finite automata
2
8) bab a? Deterministic finite
automata
are accepted by the following finite automata? Recognized languages
Determinisation
a b
b
a
q0 q1 q2
b
ng.com a https://fb.com/tailieudientucntt
4.20
Automata
Exercise
Nguyen An Khuong,
Huynh Tuong Nguyen,
Bui Hoai Thang
Give regular expression for the following finite automata.
a b
b
a Contents
q0 q1 q2
Motivation
b
Alphabets, words and
a languages
Regular expression or
rationnal expression
Deterministic finite
automata
b b
Recognized languages
a, b Determinisation
q0 q1
a
ng.com https://fb.com/tailieudientucntt
4.21
Automata
Nondeterministic finite automata
Nguyen An Khuong,
Huynh Tuong Nguyen,
Bui Hoai Thang
Definition
A nondeterministic finite automata (NFA, Ôtômat hữu hạn phi
đơn định) is mathematically represented by a 5-tuples
(Q, Σ, q0 , δ, F ) where Contents
Motivation
• Q a finite set of states.
Alphabets, words and
• Σ is the alphabet of the automata. languages
Regular expression or
• q0 ∈ Q is the initial state. rationnal expression
Non-deterministic
• δ : Q × Σ → Q is a transition function. finite automata
Recognized languages
Determinisation
Remark
According to an event, a state may go to one or more states.
ng.com https://fb.com/tailieudientucntt
4.22
Automata
NFA with empty symbol ε
Nguyen An Khuong,
Huynh Tuong Nguyen,
Bui Hoai Thang
b a a, b Regular expression or
rationnal expression
ε b Non-deterministic
finite automata
q0 q1 q2 Deterministic finite
automata
Recognized languages
Determinisation
ng.com https://fb.com/tailieudientucntt
4.23
Automata
Exercise
Nguyen An Khuong,
Huynh Tuong Nguyen,
Bui Hoai Thang
Contents
Deterministic finite
automata
Recognized languages
Determinisation
ng.com https://fb.com/tailieudientucntt
4.24
Automata
Exercise
Nguyen An Khuong,
Huynh Tuong Nguyen,
Bui Hoai Thang
Let Σ = {a, b, c}
Construct an accepting finite automata for languages represented
by the following regular expressions.
Contents
• E1 = a∗ + b,
Motivation
• E2 = b∗ , Alphabets, words and
languages
∗
• E3 = aab + cab ac, Regular expression or
rationnal expression
• E4 = b(ca + ac)(aa)∗ + a∗ (a + b),
Non-deterministic
• E5 = (aaaabaaa)2∗ c, finite automata
Deterministic finite
• E6 = b+ ac (b+ = bb∗ ), automata
Recognized languages
• E7 = (b + c)ab + (ba(c + ab2 + a3 + a4 + b)∗ )∗ ,
Determinisation
ng.com https://fb.com/tailieudientucntt
4.25
Automata
Deterministic finite automata
Nguyen An Khuong,
Huynh Tuong Nguyen,
Bui Hoai Thang
Definition
A deterministic finite automata (DFA, Ôtômat hữu hạn đơn định)
is given by a 5-tuplet (Q, Σ, q0 , δ, F ) with
Contents
• Q a finite set of states. Motivation
Recognized languages
Condition Determinisation
ng.com https://fb.com/tailieudientucntt
4.26
Automata
Example
Nguyen An Khuong,
Huynh Tuong Nguyen,
Bui Hoai Thang
Let Σ = {a, b}
Hereinafter, a deterministic and complete automata that
recognizes the set of strings which contain an odd number of a.
b b Contents
Motivation
a
Alphabets, words and
languages
q0 q1 Regular expression or
rationnal expression
a Non-deterministic
finite automata
Deterministic finite
automata
Recognized languages
Determinisation
• Q = {q0 , q1 },
• δ(q0 , a) = q1 , δ(q0 , b) = q0 , δ(q1 , a) = q0 , δ(q1 , b) = q1 ,
• F = {q1 }.
ng.com https://fb.com/tailieudientucntt
4.27
Automata
Configurations and executions
Nguyen An Khuong,
Huynh Tuong Nguyen,
Bui Hoai Thang
Let A = (Q, Σ, q0 , δ, F )
A configuration (cấu hình) of automata A is a couple (q, u) where
q ∈ Q and u ∈ Σ∗ . Contents
Regular expression or
rationnal expression
Non-deterministic
An execution (thực thi) of automata A is a sequence of finite automata
Deterministic finite
configurations automata
(q0 , u0 ) . . . (qn , un ) such that Recognized languages
ng.com https://fb.com/tailieudientucntt
4.28
Automata
Exercise
Nguyen An Khuong,
Huynh Tuong Nguyen,
Bui Hoai Thang
Soit Σ = {0, 1}
Regular expression or
rationnal expression
Soit Σ = {a, b}
Non-deterministic
finite automata
• Give an automata that accepts all strings containing 2 characters Deterministic finite
a. automata
Recognized languages
• Give an execution of this automata on aabb, ababb and bbaa.
Determinisation
ng.com https://fb.com/tailieudientucntt
4.29
Automata
Recognized languages
Nguyen An Khuong,
Huynh Tuong Nguyen,
Bui Hoai Thang
Definition
A language L over an alphabet Σ, defined as a sub-set of Σ∗ , is
Contents
recognized if there exists a finite automata accepting all strings of Motivation
L. Alphabets, words and
languages
Determinisation
ng.com https://fb.com/tailieudientucntt
4.30
Automata
Example
Nguyen An Khuong,
Huynh Tuong Nguyen,
Bui Hoai Thang
Sub-string ab
Construct a DFA that recognizes the language over the alphabet
{a, b} containing the sub-string ab.
Contents
Motivation
q1 q1 q2 Determinisation
q2 ∗ q2 q2
ng.com https://fb.com/tailieudientucntt
4.31
Automata
Example
Nguyen An Khuong,
Huynh Tuong Nguyen,
Bui Hoai Thang
Automata
Contents
Motivation
b Transition table Alphabets, words and
languages
q0 q1 a b
Regular expression or
rationnal expression
→ q0∗ q2 q1 Non-deterministic
finite automata
b q1 q3 q0 Deterministic finite
a a a a q2 q0 q3 automata
q3 q1 q2 Recognized languages
Determinisation
b →: start state
∗
: final state(s)
q2 q3
b
ng.com https://fb.com/tailieudientucntt
4.32
Automata
Equivalent automatons
Nguyen An Khuong,
Huynh Tuong Nguyen,
Bui Hoai Thang
q0 p0 p3
a Contents
Motivation
b b b b a a Regular expression or
rationnal expression
a Non-deterministic
finite automata
q1 q2 p1 a p2
Deterministic finite
automata
Recognized languages
a Determinisation
b b
ng.com https://fb.com/tailieudientucntt
4.33
Automata
Equivalent automatons
Nguyen An Khuong,
Huynh Tuong Nguyen,
Bui Hoai Thang
q0 p0 p3
a Contents
Motivation
b a b b a a Regular expression or
rationnal expression
a Non-deterministic
finite automata
q1 q2 p1 a p2
Deterministic finite
automata
Recognized languages
a Determinisation
b b
ng.com https://fb.com/tailieudientucntt
4.34
Automata
From NFA to DFA
Nguyen An Khuong,
Transition table Huynh Tuong Nguyen,
Bui Hoai Thang
a b
→ {0} {1} {0}
Given a NFA {1} {0, 2} {1}
{0, 2}∗ {1} {0, 2}
b b
Contents
a Corresponding DFA
Motivation
a Regular expression or
rationnal expression
Non-deterministic
{0} {1} finite automata
a Deterministic finite
automata
ε Recognized languages
a a Determinisation
b {0, 2}
ng.com https://fb.com/tailieudientucntt
4.35
Automata
Exercise
Nguyen An Khuong,
Huynh Tuong Nguyen,
Let Σ = {a, b, c} Bui Hoai Thang
b, c b, c
a, ε
Contents
0 1 Motivation
Non-deterministic
finite automata
c, ε Deterministic finite
automata
Recognized languages
a b, c Determinisation
b, ε ε
0 1 2
ε
ng.com https://fb.com/tailieudientucntt
4.37
Automata
Exercise
Nguyen An Khuong,
Huynh Tuong Nguyen,
Bui Hoai Thang
Let Σ = {a, b, c}
Contents
Determine finite automata, not necessarily deterministic,
Motivation
recognizing the following languages:
Alphabets, words and
languages
• L1 = {a, ab, ca, cab, acc},
Regular expression or
• L2 = { set of words of even number of a}, rationnal expression
Non-deterministic
• L3 = { set of words containing ab and ending with b}. finite automata
Deterministic finite
Then, determine the corresponging DFAs. automata
Recognized languages
Determinisation
ng.com https://fb.com/tailieudientucntt
4.38
Automata
Exercise
Nguyen An Khuong,
Huynh Tuong Nguyen,
Bui Hoai Thang
Let Σ = {a, b, c}
Construct accepting DFAs for languages represented by the
following regular expressions.
Contents
• E1 = a∗ + b,
Motivation
• E2 = b∗ , Alphabets, words and
languages
∗
• E3 = aab + cab ac, Regular expression or
rationnal expression
• E4 = b(ca + ac)(aa)∗ + a∗ (a + b),
Non-deterministic
• E5 = (aaaabaaa)2∗ c, finite automata
Deterministic finite
• E6 = b+ ac (b+ = bb∗ ), automata
Recognized languages
• E7 = (b + c)ab + (ba(c + ab2 + a3 + a4 + b)∗ )∗ ,
Determinisation
ng.com https://fb.com/tailieudientucntt
4.39