Regular Languages - informatik.uni-freiburg.deki/teaching/ws1112/acs2/acs2-04-handout.pdf · ACS...
Transcript of Regular Languages - informatik.uni-freiburg.deki/teaching/ws1112/acs2/acs2-04-handout.pdf · ACS...
ACS II: Regular Languages
1
Regular Languages
Bernhard Nebel und Christian Becker-Asano
ACS II: Regular Languages
2
Overview
Deterministic finite automata Regular languages Nondeterministic finite automata Closure operations Regular expressions Nonregular languages The pumping lemma
ACS II: Regular Languages
3
Finite automata An intuitive example: supermarket door controller
Probabilistic counterparts exist Markov chains, Bayesian nets, etc. Not in this course
Transition table for the automatic door controler:
neither front rear both
closed closed open closed closed
open closed open open open
closed open
front
neither
rear both
neither
front rear both
State diagram for the automatic door controler
front pad
rear pad
door
Top view of an automatic door
ACS II: Regular Languages
4
Finite automata (ctd.)
M1
ACS II: Regular Languages
5
Finite automata: example
0 1
M1
Which language is accepted by M1 ?
ACS II: Regular Languages
6
Finite automaton M1 and language A
M1
ACS II: Regular Languages
7
Finite automata M2 and M3
q1 q2
0 1 1
0 State diagram of the two-state finite automaton M2
q1 q2
0 1 1
0 State diagram of the two-state finite automaton M3
ACS II: Regular Languages
8
Finite automaton M4
ACS II: Regular Languages
9
Finite automaton M5
ACS II: Regular Languages
10
Formal definition of computation
ACS II: Regular Languages
11
Designing finite automata
Design automaton for language consisting of binary strings with an odd number of 1s
Design first states Then transitions Start state and accept states
ACS II: Regular Languages
12
Another example
Design an automaton to recognize the language of binary strings containing the string 001 as substring
We have four possibilities: 1. we haven‘t seen any symbol of the pattern yet, or 2. we have seen a 0, or 3. we have seen a 00, or 4. we have seen the pattern 001
ACS II: Regular Languages
13
The regular operations
ACS II: Regular Languages
14
Regular languages are closed under …
ACS II: Regular Languages
15
Proof 1.25 (by construction)
ACS II: Regular Languages
16
Proof 1.25 (by construction, ctd.)
ACS II: Regular Languages
17
Example
q1 q2
0 0,1 1 p2 p1 p3
1 0,1
0 0
1
ACS II: Regular Languages
18
Regular languages are closed under …
Non deterministic finite automata
ACS II: Regular Languages
19
Non deterministic finite automata (NFA)
Deterministic (DFA) One successor state ε transitions not allowed
Non deterministic (NFA) Several successor states possible ε transitions possible
q2 q1 q3 q4
0,1 0,1
1 0,ε 1
ACS II: Regular Languages
20
Deterministic vs. non deterministic computation Figure 15
ACS II: Regular Languages
21
Example run
q1
q1
q3 q2 q1
q3 q1
q2 q1 q3 q4
q4
q4
q2 q1 q3
q3 q1
q4
q4
0
0
1
1
0
1
q2 q1 q3 q4
0,1 0,1
1 0,ε 1
Input: w = 010110
ACS II: Regular Languages
22
Another NFA
ACS II: Regular Languages
23
Nondeterministic finite automaton
ACS II: Regular Languages
24
Example 1.18
0 1
ACS II: Regular Languages
25
Formal definition of computation
ACS II: Regular Languages
26
Every NFA has an equivalent DFA
ACS II: Regular Languages
27
Equivalence NFA and DFA
Two machines are equivalent if they recognize the same language
Theorem 1.39: Every nondeterministic finite automaton has an equivalent deterministic finite automaton.
Corollary 1.40: A language is regular if and only if some nondeterministic finite automaton recognizes it.
ACS II: Regular Languages
28
Proof: Theorem 1.39
ACS II: Regular Languages
29
Proof: Theorem 1.39 (ctd.)
ACS II: Regular Languages
30
Proof: Theorem 1.39 (ctd.)
ACS II: Regular Languages
31
Proof: Theorem 1.39 (ctd.)
ACS II: Regular Languages
32
An example
The resulting DFA (after removing redundant states)
The resulting DFA
ACS II: Regular Languages
33
Closure under the regular operations
ACS II: Regular Languages
34
Proof of Theorem 1.45
The class of regular languages is closed under the union operation.
Idea:
ACS II: Regular Languages
35
Proof of Theorem 1.45 (ctd.)
ACS II: Regular Languages
36
Proof of Theorem 1.47
The class of regular languages is closed under the concatenation operation.
Idea:
ACS II: Regular Languages
37
Proof of Theorem 1.47 (ctd.)
ACS II: Regular Languages
38
Proof of Theorem 1.49
The class of regular languages is closed under the star operation.
Idea:
ACS II: Regular Languages
39
Proof of Theorem 1.49 (ctd.)
ACS II: Regular Languages
40
Regular expressions
ACS II: Regular Languages
41
Regular expressions: examples (1)
ACS II: Regular Languages
42
Regular expressions: examples (2)
ACS II: Regular Languages
43
Applications of regular expressions
ACS II: Regular Languages
44
Equivalence of RE and NFA
Theorem 1.54 (page 66): A language is regular if and only if some regular expression describes it.
Two directions to consider:
Lemma 1.55 (page 67): If a language is described by some regular expression, then it is regular.
Lemma 1.60 (page 69): If a language is regular, then it can be described by some regular expression.
ACS II: Regular Languages
45
Proof of Lemma 1.55
ACS II: Regular Languages
46
Proof of Lemma 1.55, case 1
a
ACS II: Regular Languages
47
Proof of Lemma 1.55, cases 2 & 3
ACS II: Regular Languages
48
Proof of Lemma 1.55, cases 4, 5 & 6
ACS II: Regular Languages
49
ACS II: Regular Languages
50
a:
b:
ab:
ab∪a
(ab∪a)*
Example 1.56: NFA for (ab∪a)* a
b
a b ε
a b ε
a ε
ε
a b ε
a ε
ε
ε
ε
ε
ACS II: Regular Languages
51
Exercise: NFA for (a∪b)*aba
a:
b:
a∪b
(a∪b)*
a
b
a
b ε
ε
a
b ε
ε ε
ε
ε
ACS II: Regular Languages
52
a
b ε
ε ε
a
b ε
ε ε
Example: NFA for (a∪b)*aba (cont.)
aba:
(a∪b)*aba:
a b ε a ε
a b ε a ε
a b ε a ε ε
ε
ε
ACS II: Regular Languages
53
Lemma 1.60
Lemma 1.60 (page 69): If a language is regular, then it can be described by a regular expression.
Two steps DFA into GNFA (generalized nondeterministic finite automaton) Convert GNFA into regular expression
ACS II: Regular Languages
54
GNFAs
Labels are regular expressions
Two states q and r are connected in both directions (fully connected)
Exception: One direction only Start state (exiting
transition arrows) Accept state (only one!)
(only incoming transition arrows)
qstart
qaccept
b ab
Ø
b*
ab*
ab∪ba a*
(aa)*
aa
ACS II: Regular Languages
55
Generalized NFA
ACS II: Regular Languages
56
ACS II: Regular Languages
57
Convert DFA into GNFA
ACS II: Regular Languages
58
Convert GNFA into regular expression
3 state DFA 5 state GNFA 4 state GNFA
2 state GNFA 3 state GNFA Regular Expression
ACS II: Regular Languages
59
Ripping of states
q2 q1
(R1)
Replace one state by the corresponding RE
q2
qrip
q1
R4
R1
R2
R3
(R1)(R2) (R1)(R2)* (R3) ∪R4 (R1)(R2)* (R3) (R1)(R2)*
ACS II: Regular Languages
60
Convert(G)
ACS II: Regular Languages
61
Example
Step 2: rip state 2
Step 3: rip state 1:
DFA: Step 1: convert into GNFA
ACS II: Regular Languages
62
Another Example
1 2 a
a
3
b b a
b
s
1 2 a
a
3
b b a
b
ε a
ε
ε
s
2
3 a
b
a
ε
ε
a
aa ∪b
ab
b
ba ∪a
bb
Rip 1: Rip 2:
GNFA: DFA:
s
3
a a(aa ∪b)*
a(aa ∪b)*ab ∪b (ba ∪a) (aa ∪b)* ∪ε
(ba ∪a) (aa ∪b)*ab ∪ bb
s a Rip 3:
(a(aa ∪b)*ab ∪b)((ba ∪a) (aa ∪b)*ab ∪ bb)*((ba ∪a) (aa ∪b)* ∪ε) ∪a(aa ∪b)*
ACS II: Regular Languages
63
Induction Proof q2
qrip
q1
R4
R1
R2
R2
q2 q1
qrip
(R1)(R2)* (R3) ∪R4
ACS II: Regular Languages
64
Induction Proof (ctd.)
ACS II: Regular Languages
65
Induction Proof (ctd.)
q2
qrip
q1
R4
R1
R2
R2
q2 q1
qrip
(R1)(R2)* (R3) ∪R4
ACS II: Regular Languages
66
Nonregular Languages
Finite Automata have a finite memory Are the following languages regular ?
Mathematical proof necessary
ACS II: Regular Languages
67
The pumping lemma
ACS II: Regular Languages
68
Proof Idea Let M be a DFA recognizing A. Assign p to be the number of states in M. Show that string s, with length at least p, can be broken into xyz.
Now prove that all three conditions are met.
ACS II: Regular Languages
69
Proof: Pumping Lemma
ACS II: Regular Languages
70
Pumping Lemma (cont.)
ACS II: Regular Languages
71
Nonregular languages: example 1
ACS II: Regular Languages
72
Nonregular languages: example 2
ACS II: Regular Languages
73
Nonregular languages: example 3
ACS II: Regular Languages
74
Nonregular languages: example 4
ACS II: Regular Languages
75
Example exam question
ACS II: Regular Languages
76
Summary
Deterministic finite automata Regular languages Nondeterministic finite automata Closure operations Regular expressions Nonregular languages The pumping lemma