Hierarchy of languages
1
Regular Languages
Context-Free Languages
Recursive Languages
Recursively Enumerable Languages
Non-Recursively Enumerable Languages
Deterministic Finite State Automata (DFA)
……..
2
Finite
Control
0 | 1 | 1 | 0 | 0 | | | | |
1 0 0 1 1
q0 q0 q1 q0 q0 q0
L = {all strings with zero or more 0’s}
3
q0
q1
0
0
1
1
4
Note:
but a machine cannot accept multiple languages!
you can call them by any names!
Sigma = {0, 1} ≡ {a, b}
States = {q0, q1} ≡ {u, v}, as long as they have
identical (isomorphic) transition table
M1
M2
….
M-inf
L
1 0 0 1 1
q0 q3 q1 q2 q2 q2 accept string
5
q0
q1
0
0
1
q2
1
1
0
q3
1
0
a c c c b accepted
q0 q0 q1 q2 q2 q2
a a c rejected
q0 q0 q0 q1
6
q1
q0
q2
a
b
a
b
c
c
a/b/c
7
q1
q0
q2
a
b
a
b
c
c
a/b/c
Inductive Proof (sketch): that the machine correctly accepts strings with at least two c’s
Proof goes over the length of the string.
Base: x a string with |x|=0. state will be q0 => rejected.
Inductive hypothesis: |x|= integer k, & string x is rejected - in state q0 (x must have zero c),
OR, rejected – in state q1 (x must have one c),
OR, accepted – in state q2 (x has already with two c’s)
Inductive steps: Each case for symbol p, for string xp (|xp| = k+1), the last symbol p = a, b or c
| xa | xb | xc |
x ends in q0 | q0 =>reject (still zero c => should reject) | q0 =>reject (still zero c => should reject) | q1 =>reject (still zero c => should reject) |
x ends in q1 | q1 =>reject (still one c => should reject) | q1 =>reject (still one c => should reject) | q2 =>accept (two c now=> should accept) |
x ends in q2 | q2 =>accept (two c already => should accept) | q2 =>accept (two c already => should accept) | q2 =>accept (two c already => should accept) |
Formal Definition of a DFA
M = (Q, Σ, δ, q0, F)
Q A finite set of states
Σ A finite input alphabet
q0 The initial/starting state, q0 is in Q
F A set of final/accepting states, which is a subset of Q
δ A transition function, which is a total function from Q x Σ to Q
δ: (Q x Σ) –> Q δ is defined for any q in Q and s in Σ, and
δ(q,s) = q’ is equal to some state q’ in Q, could be q’=q
Intuitively, δ(q,s) is the state entered by M after reading symbol s while in state q.
8
Q = {q0, q1}
Σ = {0, 1}
Start state is q0
F = {q0}
δ:
0 1
q0 q1 q0
q1 q0 q1
9
| |
| |
q0
q1
0
0
1
1
Q = {q0, q1, q2}
Σ = {a, b, c}
Start state is q0
F = {q2}
δ: a b c
q0 q0 q0 q1
q1 q1 q1 q2
q2 q2 q2 q2
10
| | |
| | |
| | |
q1
q0
q2
a
b
a
b
c
c
a/b/c
Extension of δ to Strings
δ^ : (Q x Σ*) –> Q
δ^(q,w) – The state entered after reading string w having started in state q.
Formally:
1) δ^(q, ε) = q, and
2) For all w in Σ* and a in Σ
δ^(q,wa) = δ (δ^(q,w), a)
11
δ^(q0, 011) = δ (δ^(q0,01), 1) by rule #2
= δ (δ ( δ^(q0,0), 1), 1) by rule #2
= δ (δ (δ (δ^(q0, λ), 0), 1), 1) by rule #2
= δ (δ (δ(q0,0), 1), 1) by rule #1
= δ (δ (q1, 1), 1) by definition of δ
= δ (q1, 1) by definition of δ
= q1 by definition of δ
12
q0
q1
0
0
1
1
δ^ (q,a) = δ(δ^(q, ε), a) by definition of δ^, rule #2
= δ(q, a) by definition of δ^, rule #1
δ^ (q, a1a2…an) = δ(δ(…δ(δ(q, a1), a2)…), an)
δ^(q, a1a2…an) = δ(q, a1a2…an)
13
δ(q0, 011) = δ (δ(q0,01), 1) by rule #2
= δ (δ (δ(q0,0), 1), 1) by rule #2
= δ (δ (q1, 1), 1) by definition of δ
= δ (q1, 1) by definition of δ
= q1 by definition of δ
14
q1
q0
q2
1
1
0
0
1
0
δ(q1, 10) = δ (δ(q1,1), 0) by rule #2
= δ (q1, 0) by definition of δ
= q2 by definition of δ
15
0
q1
q0
q2
1
1
0
1
0
Definitions related to DFAs
L(M) = {w | w is in Σ* and δ(q0,w) is in F}
L(M) = {w | w is in Σ* and w is accepted by M}
16
Σ* - L(M).
Regular: L1 = {x | x is a string of 0's and 1's containing an even number of 1's} and
Not-regular: L2 = {x | x = 0n1n for some n >= 0}
17
L(M) = {x | x is a string of 0’s and 1’s and |x| >= 2}
Prove this by induction
18
q1
q0
q2
0/1
0/1
0/1
L(M) = {x | x is a string of (zero or more) a’s, b’s and c’s such
that x does not contain the substring aa}
Logic:
In Start state (q0): b’s and c’s: ignore – stay in same state
q0 is also “accept” state
First ‘a’ appears: get ready (q1) to reject
But followed by a ‘b’ or ‘c’: go back to start state q0
When second ‘a’ appears after the “ready” state: go to reject state q2
Ignore everything after getting to the “reject” state q2
19
q2
q0
a
a/b/c
a
q1
b/c
b/c
L(M) = {x | x is a string of a’s, b’s and c’s such that x
contains the substring aba}
Logic: acceptance is straight forward, progressing on each expected symbol
However, rejection needs special care, in each state (for DFA, we will see this becomes easier in NFA, non-deterministic machine)
20
q2
q0
a
a/b/c
b
q1
c
b/c
a
b/c
q3
a
L(M) = {x | x is a string of a’s and b’s such that x
contains both aa and bb}
First do, for a language where ‘aa’ comes before ‘bb’
Then do its reverse; and then parallelize them.
Remember, you may have multiple “final” states, but only one “start” state
21
q0
b
q7
q5
q4
q6
b
b
b
a
q2
q1
q3
a
a
a
b
a/b
b
a
a
a
b
For {}: For {ε}:
For Σ*: For Σ+:
22
0/1
q0
0/1
q0
q1
q0
0/1
0/1
0/1
q0
q1
0/1
23
0/1
q1
q0
q3
1
0/1
q2
0/1
Is this a DFA?
No, but it is a Non-deterministic Finite Automaton
Nondeterministic Finite State�Automata (NFA)
M = (Q, Σ, δ, q0, F)
Q A finite set of states
Σ A finite input alphabet
q0 The initial/starting state, q0 is in Q
F A set of final/accepting states, which is a subset of Q
δ A transition function, which is a total function from Q x Σ to 2Q
δ: (Q x Σ) –> 2Q :2Q is the power set of Q, the set of all subsets of Q δ(q,s) :The set of all states p such that there is a transition
labeled s from q to p
δ(q,s) is a function from Q x S to 2Q (but not only to Q)
24
Q = {q0, q1, q2}
Σ = {0, 1}
Start state is q0
F = {q2}
δ: 0 1
q0
q1
q2
25
{q0, q1} | {} |
{} | {q1, q2} |
{q2} | {q2} |
q1
q0
q2
0
1
0
1
0/1
Q = {q0, q1, q2 , q3 , q4}
Σ = {0, 1}
Start state is q0
F = {q2, q4}
δ: 0 1
q0
q1
q2
q3
q4
26
{q0, q3} | {q0, q1} |
{} | {q2} |
{q2} | {q2} |
{q4} | {} |
{q4} | {q4} |
q0
0/1
0
0
q3
q4
0/1
q1
q2
0/1
1
1
27
q0 q0 q0 q0
q3 q3 q1
q4 q4 accepted
Complexity: O(|x|*n), for running over a string x
28
0
0
1
q0
0/1
0
q3
q4
q1
q2
1
1
0
0/1
0/1
q0 q0 q0 q0
q3 q1 q3
not accepted
29
0
1
0
q0
0/1
0
q3
q4
q1
q2
1
1
0
30
L = {x | x is in Σ* and x contains ab}
Is L a subset of L(M)? Or, does M accepts all string in L?
Is L(M) a subset of L? Or, does M rejects all strings not in L?
31
q1
q0
q2
a
a/b/c
b
a/b/c
L = {x | x is in Σ* and the third to the last symbol in x is b}
Is L a subset of L(M)?
Is L(M) a subset of L?
32
q1
q0
b
q3
a/b
a/b
q2
a/b
Extension of δ to Strings and Sets of States
33
q0
0
1
q1
q4
q3
0
1
q2
0
0
1
0
0
Extension of δ to Strings and Sets of States
Given δ: (Q x Σ) –> 2Q define δ#: (2Q x Σ) –> 2Q as follows:
1) δ#(R, a) = δ(q, a) for all subsets R of Q, and symbols a in Σ
δ#({p},a) = δ(q, a) by definition of δ#, rule #1 above
= δ(p, a)
δ({q0, q2}, 0) These now make sense, but previously
δ({q0, q1, q2}, 0) they did not.
34
δ({q0, q2}, 0) = δ(q0, 0) U δ(q2, 0)
= {q1, q3} U {q3, q4}
= {q1, q3, q4}
δ({q0, q1, q2}, 1) = δ(q0, 1) U δ(q1, 1) U δ(q2, 1)
= {} U {q2, q3} U {}
= {q2, q3}
35
Given δ: (2Q x Σ) –> 2Q define δ^: (2Q x Σ*) –> 2Q as follows:
δ^(R,w) – The set of states M could be in after processing string w, having started from any state in R.
Formally:
2) δ^(R, ε) = R for any subset R of Q
3) δ^(R,wa) = δ (δ^(R,w), a) for any w in Σ*, a in Σ, and
subset R of Q
δ^(R, a) = δ(δ^(R, ε), a) by definition of δ^, rule #3 above
= δ(R, a) by definition of δ^, rule #2 above
δ({q0, q2}, 0110) These now make sense, but previously
δ({q0, q1, q2}, 101101) they did not.
36
What is δ({q0}, 10)?
Informally: The set of states the NFA could be in after processing 10,
having started in state q0, i.e., {q1, q2, q3}.
Formally: δ({q0}, 10) = δ(δ({q0}, 1), 0)
= δ({q0}, 0)
= {q1, q2, q3}
Is 10 accepted? Yes!
37
q0
0
1
q1
q3
0
1
q2
1
1
0
What is δ({q0, q1}, 1)?
δ({q0 , q1}, 1) = δ({q0}, 1) ∪ δ({q1}, 1)
= {q0} ∪ {q2, q3}
= {q0, q2, q3}
What is δ({q0, q2}, 10)?
δ({q0 , q2}, 10) = δ(δ({q0 , q2}, 1), 0)
= δ(δ({q0}, 1) U δ({q2}, 1), 0)
= δ({q0} ∪ {q3}, 0)
= δ({q0,q3}, 0)
= δ({q0}, 0) ∪ δ({q3}, 0)
= {q1, q2, q3} ∪ {}
= {q1, q2, q3}
38
δ({q0}, 101) = δ(δ({q0}, 10), 1)
= δ(δ(δ({q0}, 1), 0), 1)
= δ(δ({q0}, 0), 1)
= δ({q1 , q2, q3}, 1)
= δ({q1}, 1) U δ({q2}, 1) U δ({q3}, 1)
= {q2, q3} U {q3} U {}
= {q2, q3}
Is 101 accepted? Yes! q3 is a final state.
39
Definitions for NFAs
L(M) = {w | w is in Σ* and δ({q0},w) contains at least one state in F}
L(M) = {w | w is in Σ* and w is accepted by M}
40
Equivalence of DFAs and NFAs
41
Q = {q0, q1, q2}
Σ = {a, b, c}
Start state is q0
F = {q2}
δ: a b c
q0 q0 q0 q1
q1 q1 q1 q2
q2 q2 q2 q2
42
| | |
| | |
| | |
q1
q0
q2
a
b
a
b
c
c
a/b/c
Q = {q0, q1, q2}
Σ = {a, b, c}
Start state is q0
F = {q2}
δ: a b c
q0 {q0} {q0} {q1}
q1 {q1} {q1} {q2}
q2 {q2} {q2} {q2}
43
| | |
| | |
| | |
q1
q0
q2
a
b
a
b
c
c
a/b/c
The above is just a formal statement of the observation from the previous slide.
44
Let M = (Q, Σ, δ,q0,F).
Define a DFA M’ = (Q’, Σ, δ’,q’0,F’) as:
Q’ = 2Q Each state in M’ corresponds to a
= {Q0, Q1,…,} subset of states from M
where Qu = [qi0, qi1,…qij]
F’ = {Qu | Qu contains at least one state in F}
q’0 = [q0]
δ’(Qu, a) = Qv iff δ(Qu, a) = Qv
45
Q = {q0, q1}
Σ = {0, 1}
Start state is q0
F = {q0}
δ: 0 1
q0
q1
46
{q1} | {} |
{q0, q1} | {q1} |
q1
q0
0
0/1
0
-->q0
δ for DFA: 0 1 q1
->q0
[q1]
[ ]
47
q1
q0
0
0/1
0
{q1} write as [q1] | {} write as [ ] |
| |
| |
δ: 0 1
->q0
[q1]
[ ]
[q01]
48
q1
q0
0
0/1
0
{q1} write as [q1] | {} |
{q0,q1} write as [q01] | {q1} |
| |
| |
δ: 0 1
->q0
[q1]
[ ]
[q01]
49
q1
q0
0
0/1
0
{q1} write as [q1] | {} |
{q0,q1} write as [q01] | {q1} |
[ ] | [ ] |
| |
δ: 0 1
->q0
[q1]
[ ]
[q01]
50
q1
q0
0
0/1
0
{q1} write as [q1] | {} |
{q0,q1} write as [q01] | {q1} |
[ ] | [ ] |
[q01] | [q1] |
δ({q0}, 0) = {q1} => δ’([q0], 0) = [q1]
δ({q0}, 1) = {} => δ’([q0], 1) = [ ]
δ({q1}, 0) = {q0, q1} => δ’([q1], 0) = [q0q1]
δ({q1}, 1) = {q1} => δ’([q1], 1) = [q1]
δ({q0, q1}, 0) = {q0, q1} => δ’([q0q1], 0) = [q0q1]
δ({q0, q1}, 1) = {q1} => δ’([q0q1], 1) = [q1]
δ({}, 0) = {} => δ’([ ], 0) = [ ]
δ({}, 1) = {} => δ’([ ], 1) = [ ]
51
[ ]
1
0
[q0q1]
1
[q1]
0
0/1
[q0]
1
0
(if) Suppose there exists an NFA M’ such that L = L(M’). Then by Lemma 2 there exists an DFA M such that L = L(M).
(only if) Suppose there exists an DFA M such that L = L(M). Then by Lemma 1 there exists an NFA M’ such that L = L(M’).
52
δ(R, 0) = δ(δ(R, ε), 0)
= δ(R, 0)
= δ(q, 0)
= {} Since R = {}
Q = {q0, q1, q2} δ: 0 1
Σ = {0, 1}
Start state is q0 q0
F = {q0}
q1
q2
53
{q0, q1} | { } |
{q1} | {q2} |
{q2} | {q2} |
54
0/1
q1
q0
q3
1
0/1
q2
0/1
Now, can you convert this NFA to a DFA?
NFAs with ε Moves
M = (Q, Σ, δ, q0, F)
Q A finite set of states
Σ A finite input alphabet
q0 The initial/starting state, q0 is in Q
F A set of final/accepting states, which is a subset of Q
δ A transition function, which is a total function from Q x Σ U {ε} to 2Q
δ: (Q x (Σ U {ε})) –> 2Q
δ(q,s) -The set of all states p such that there is a
transition labeled a from q to p, where a
is in Σ U {ε}
55
δ: 0 1 ε
q0 - A string w = w1w2…wn is processed
as w = ε*w1ε*w2ε* … ε*wnε*
q1 - Example: all computations on 00:
0 ε 0
q2 q0 q0 q1 q2
:
q3
56
q0
ε
0/1
q2
1
0
q1
0
q3
ε
0
1
{q0} | { } | {q1} |
{q1, q2} | {q0, q3} | {q2} |
{q2} | {q2} | { } |
{ } | { } | { } |
Informal Definitions
57
ε-closure
ε-closure(q0) = {q0, q1, q2} ε-closure(q2) = {q2}
ε-closure(q1) = {q1, q2} ε-closure(q3) = {q3}
ε-closure(P) = ε-closure(q)
ε-closure({q1, q2}) = {q1, q2}
ε-closure({q0, q3}) = {q0, q1, q2, q3}
58
q0
ε
0/1
q2
1
0
q1
0
q3
ε
0
1
Extension of δ to Strings and Sets of States
59
q0
ε
0/1
q2
1
0
q1
0
q3
ε
0
1
Given δ: (Q x (Σ U {ε})) –> 2Q define δ#: (2Q x (Σ U {ε})) –> 2Q as follows:
1) δ#(R, a) = δ(q, a) for all subsets R of Q, and symbols a in Σ U {ε}
δ#({p},a) = δ(q, a) by definition of δ#, rule #1 above
= δ(p, a)
δ({q0, q2}, 0) These now make sense, but previously
δ({q0, q1, q2}, 0) they did not.
60
What is δ({q0 , q1, q2}, 1)?
δ({q0 , q1, q2}, 1) = δ(q0, 1) U δ(q1, 1) U δ(q2, 1)
= { } U {q0, q3} U {q2}
= {q0, q2, q3}
What is δ({q0, q1}, 0)?
δ({q0 , q1}, 0) = δ(q0, 0) U δ(q1, 0)
= {q0} U {q1, q2}
= {q0, q1, q2}
61
Given δ: (2Q x (Σ U {ε})) –> 2Q define δ^: (2Q x Σ*) –> 2Q as follows:
δ^(R,w) – The set of states M could be in after processing string w, having starting from any state in R.
Formally:
2) δ^(R, ε) = ε-closure(R) - for any subset R of Q
3) δ^(R,wa) = ε-closure(δ(δ^(R,w), a)) - for any w in Σ*, a in Σ, and
subset R of Q
62
δ({q0}, 0) = {q0}
δ^({q0}, 0) = ε-closure(δ(δ^({q0}, ε), 0)) By rule #3
= ε-closure(δ(ε-closure({q0}), 0)) By rule #2
= ε-closure(δ({q0, q1, q2}, 0)) By ε-closure
= ε-closure(δ(q0, 0) U δ(q1, 0) U δ(q2, 0)) By rule #1
= ε-closure({q0} U {q1, q2} U {q2})
= ε-closure({q0, q1, q2})
= ε-closure({q0}) U ε-closure({q1}) U ε-closure({q2})
= {q0, q1, q2} U {q1, q2} U {q2}
= {q0, q1, q2}
δ(q0, 0) - Processes 0 as a single symbol, without ε transitions.
δ^(q0 , 0) - Processes 0 using as many ε transitions as are possible.
63
δ^({q0}, 01) = ε-closure(δ(δ^({q0}, 0), 1)) By rule #3
= ε-closure(δ({q0, q1, q2}), 1) Previous slide
= ε-closure(δ(q0, 1) U δ(q1, 1) U δ(q2, 1)) By rule #1
= ε-closure({ } U {q0, q3} U {q2})
= ε-closure({q0, q2, q3})
= ε-closure({q0}) U ε-closure({q2}) U ε-closure({q3})
= {q0, q1, q2} U {q2} U {q3}
= {q0, q1, q2, q3}
64
Definitions for NFA-ε Machines
L(M) = {w | w is in Σ* and δ^({q0},w) contains at least one state in F}
L(M) = {w | w is in Σ* and w is accepted by M}
65
Equivalence of NFAs and NFA-εs
66
The above is just a formal statement of the observation from the previous slide.
67
Let M = (Q, Σ, δ,q0,F) be an NFA-ε.
Define an NFA M’ = (Q, Σ, δ’,q0,F’) as:
F’ = F U {q} if ε-closure(q) contains at least one state from F
F’ = F otherwise
δ’(q, a) = δ^(q, a) - for all q in Q and a in Σ
68
69
q0
ε
0/1
q2
1
0
q1
0
q3
ε
0
1
q2
q1
q3
q0
70
q0
ε
0/1
q2
1
0
q1
0
q3
ε
0
1
q2
q1
q3
q0
71
q0
ε
0/1
q2
1
0
q1
0
q3
ε
0
1
q2
q1
q3
q0
0
0
0
72
q0
ε
0/1
q2
1
0
q1
0
q3
ε
0
1
q2
q1
q3
q0
0/1
0/1
0/1
1
73
q0
ε
0/1
q2
1
0
q1
0
q3
ε
0
1
q2
q1
q3
q0
0/1
0/1
0/1
1
0
0
74
q0
ε
0/1
q2
1
0
q1
0
q3
ε
0
1
q2
q1
q3
q0
0/1
0/1
0/1
1
0/1
0/1
1
1
75
q0
ε
0/1
q2
1
0
q1
0
q3
ε
0
1
q2
q3
q0
0/1
0/1
0/1
1
0/1
0/1
1
1
0
q1
76
q0
ε
0/1
q2
1
0
q1
0
q3
ε
0
1
q2
q1
q3
q0
0/1
0/1
0/1
1
0/1
0/1
1
1
0/1
(if) Suppose there exists an NFA-ε M’ such that L = L(M’). Then by Lemma 2 there exists an NFA M such that L = L(M).
(only if) Suppose there exists an NFA M such that L = L(M). Then by Lemma 1 there exists an NFA-ε M’ such that L = L(M’).
77