SLIDE 1 1
CS 301 - Lecture 2 Regular Languages and Nondeterministic Finite Automata
Fall 2008
Review
– Alphabets, strings, languages
- Deterministic Finite Automata (DFA)
– Definition and Use
– Regular Languages – Nondeterministic Finite State Automata – Equivalence with DFA
Languages Accepted by DFAs
– The language contains all input strings accepted by
– = { strings that drive to a final state}
M
( )
M L M
( )
M L M
Example
q
1
q
2
q
3
q
4
q a b b a
5
q a a b b b a, b a,
( ) { }
abba M L = M
accept
SLIDE 2 2
Another Example
q
1
q
2
q
3
q
4
q a b b a
5
q a a b b b a, b a,
( ) { }
abba ab M L , , λ = M
accept accept accept
Formally
- For a DFA
- Language accepted by :
( )
F q Q M , , , , δ Σ = M
( ) ( ) { }
F w q w M L ∈ Σ ∈ = , * : * δ
q q′
w
F q ∈ ′
Observation
( ) ( ) { }
F w q w M L ∉ Σ ∈ = , * : * δ
q q′
w
F q ∉ ′
M
More Examples
a b b a, b a, q
1
q
2
q
( )
} : { ≥ = n b a M L
n accept trap state
SLIDE 3 3
( )
M L
= { all strings with prefix }
ab
a b b a, q
1
q
2
q accept b a,
3
q a b
( )
M L = { all strings without substring } 001 λ 00 001 1 1 1 1 ,
Regular Languages
- A language is regular if there is a DFA
such that
- All regular languages form a language family
L M
( )
M L L =
{ }
abba
{ }
abba ab, , λ
} : { ≥ n b an
{ all strings with prefix }
ab
{ all strings without substring } 001 Examples of regular languages: There exist automata that accept these Languages
SLIDE 4
4
Another Example
{ } { }
* , : b a w awa L ∈ =
a b b a, a b b a q
2
q
3
q
4
q
( )
M L L =
There exist languages which are not Regular:
} : { ≥ = n b a L
n n
There is no DFA that accepts such a language (we will prove this later in the semester) Example: 1
q
2
q
3
q
a a a
q } {a
Alphabet =
Nondeterministic Finite Accepter (NFA)
1
q
2
q
3
q
a a a
q
Two choices
} {a
Alphabet =
Nondeterministic Finite Accepter (NFA)
SLIDE 5
5
No transition 1
q
2
q
3
q
a a a
q
Two choices No transition
} {a
Alphabet =
Nondeterministic Finite Accepter (NFA)
a a
q
1
q
2
q
3
q
a a
First Choice
a a a
q
1
q
2
q
3
q
a a a
First Choice
a a
q
1
q
2
q
3
q
a a
First Choice
a
SLIDE 6
6
a a
q
1
q
2
q
3
q
a a a “accept”
First Choice
All input is consumed a a
q
1
q
2
q
3
q
a a
Second Choice
a a a
q
1
q
2
q
a a
Second Choice
a 3
q
a a
q
1
q
2
q
a a a 3
q
Second Choice
No transition: the automaton hangs
SLIDE 7 7
a a
q
1
q
2
q
a a a 3
q
Second Choice
should we reject aa? Input cannot be consumed An NFA accepts a string: when there is a computation of the NFA that accepts the string all the input is consumed and the automaton is in a final state
AND When To Accept a String Example
aa
is accepted by the NFA:
q
1
q
2
q
3
q
a a a
“accept”
q
1
q
2
q
a a a 3
q
“reject??” because this computation accepts aa But this only tells us that choice didn’t work…. a
q
1
q
2
q
3
q
a a
Rejection example
a
SLIDE 8
8
a
q
1
q
2
q
3
q
a a a
First Choice
a
q
1
q
2
q
3
q
a a a
First Choice
“reject??”
Second Choice
a
q
1
q
2
q
3
q
a a a
Second Choice
a
q
1
q
2
q
a a a 3
q
SLIDE 9 9
Second Choice
a
q
1
q
2
q
a a a 3
q
“reject??” An NFA rejects a string: when there is no computation of the NFA that accepts the string:
- All the input is consumed and the
automaton is in a non final state
- The input cannot be consumed
OR
Example
a is rejected by the NFA:
q
1
q
2
q
a a a 3
q “reject??” q
1
q
2
q
a a a 3
q
“reject??” All possible computations lead to rejection
Rejection example
a a
q
1
q
2
q
3
q
a a a a
SLIDE 10
10
a a
q
1
q
2
q
3
q
a a a
First Choice
a a a
q
1
q
2
q
3
q
a a
First Choice
a a No transition: the automaton hangs a a
q
1
q
2
q
3
q
a a a “reject??”
First Choice
a Input cannot be consumed a a
q
1
q
2
q
3
q
a a
Second Choice
a a
SLIDE 11 11
a a
q
1
q
2
q
a a
Second Choice
a 3
q
a a a
q
1
q
2
q
a a a 3
q
Second Choice
No transition: the automaton hangs a a a
q
1
q
2
q
a a a 3
q
Second Choice
“reject??” a Input cannot be consumed
aaa is rejected by the NFA:
q
1
q
2
q
3
q
a a a
“reject??”
q
1
q
2
q
a a a 3
q
“reject??” All possible computations lead to rejection
SLIDE 12
12
1
q
2
q
3
q
a a a
q
Language accepted:
} {aa L =
Lambda Transitions
1
q
3
q
a
q λ
2
q
a a a 1
q
3
q
a
q λ
2
q
a a a 1
q
3
q
a
q λ
2
q
a
SLIDE 13
13
a a 1
q
3
q
a
q λ
2
q
a (read head does not move) a a 1
q
3
q
a
q λ
2
q
a a a 1
q
3
q
a
q λ
2
q
a “accept” String is accepted
aa
all input is consumed a a 1
q
3
q
a
q λ
2
q
a Rejection Example a
SLIDE 14
14
a a 1
q
3
q
a
q λ
2
q
a a a a 1
q
3
q
a
q λ
2
q
a (read head doesn’t move) a a a 1
q
3
q
a
q λ
2
q
a a No transition: the automaton hangs a a 1
q
3
q
a
q λ
2
q
a “reject??” String is rejected
aaa
a Input cannot be consumed
SLIDE 15
15
Language accepted:
} {aa L =
1
q
3
q
a
q λ
2
q
a
Another NFA Example
q
1
q
2
q a b λ λ
3
q a b q
1
q
2
q a b λ λ
3
q q
2
q a b λ λ
3
q a b
1
q
SLIDE 16
16 a b q
1
q a b λ λ
3
q
2
q a b q
1
q a b λ λ
3
q
2
q
“accept”
q a b λ λ a b
Another String
a b
1
q
2
q
3
q q a b λ λ a b a b
1
q
2
q
3
q
SLIDE 17
17 q a b λ λ a b a b
1
q
2
q
3
q q a b λ λ a b a b
1
q
2
q
3
q q a b λ λ a b a b
1
q
2
q
3
q q a b λ λ a b a b
1
q
2
q
3
q
SLIDE 18
18 q a b λ λ a b a b
1
q
2
q
3
q a b a b q a b λ λ
1
q
2
q
3
q
“accept”
{ } { }
+
= = ab ababab abab ab L ... , , ,
Language accepted
q
1
q
2
q a b λ λ
3
q
Another NFA Example
q
1
q
2
q 1 1 , λ
SLIDE 19 19 q
1
q
2
q 1 1 , λ
Language accepted
(redundant state)
Remarks:
- The symbol never appears on the
input tape
λ q
2
M q
1
M {} = ) M ( L
1
q
2
q
1
q a a a q
1
q a
} { = ) (
1
a M L
2
M
1
M } { = ) (
2
a M L
NFA DFA
- NFAs are interesting because we can
express languages easier than DFAs
Formal Definition of NFAs
)
F q Q M , , , , δ Σ = : Q : δ : q : F
Set of states, i.e. {
}
2 1
, , q q q : Σ
Input aplhabet, i.e. {
}
b a,
Transition function Initial state Final states
SLIDE 20
20
( ) { }
1 0 1
, q q = δ 1 1 , λ
Transition Function
q
1
q
2
q δ
Note the function returns a set…. What did a DFA transition funciton return?
q 1 1 , λ } , { ) , (
2 1
q q q = δ
1
q
2
q q 1 1 , λ
1
q
2
q } , { ) , (
2
q q q = λ δ q 1 1 , λ
1
q
2
q ∅ = ) 1 , ( 2 q δ
SLIDE 21 21
Extended Transition Function
q λ
5
q
4
q
3
q
2
q
1
q a a a b λ
( ) { }
1 0,
* q a q = δ
( ) { }
5 4
, , * q q aa q = δ
q λ
5
q
4
q
3
q
2
q
1
q a a a b λ
( ) { }
3 2
, , , * q q q ab q = δ
q λ
5
q
4
q
3
q
2
q
1
q a a a b λ
Formally ( )
w q q
i j
, * δ ∈
: there is a walk from to with label i
q
j
q w
i
q
j
q
w
k
w σ σ σ
2 1
=
1
σ
2
σ
k
σ
i
q
j
q
SLIDE 22
22
The Language of an NFA
q λ
5
q
4
q
3
q
2
q
1
q a a a b λ
( ) {
}
5 4
, , * q q aa q = δ ) (M L aa∈
{ }
5 0,q
q F = F ∈
q λ
5
q
4
q
3
q
2
q
1
q a a a b λ
( ) {
}
3 2
, , , * q q q ab q = δ
( )
M L ab∈
{ }
5 0,q
q F = F ∈
q λ
5
q
4
q
3
q
2
q
1
q a a a b λ
{ }
5 0,q
q F =
( ) {
}
5 4
, , * q q abaa q = δ F ∈
q λ
5
q
4
q
3
q
2
q
1
q a a a b λ
{ }
5 0,q
q F =
( ) { }
1 0,
* q aba q = δ
( )
M L aba∉ F ∉
SLIDE 23 23
q λ
5
q
4
q
3
q
2
q
1
q a a a b λ
( ) { } { }
} { * aa ab M L ∪ = λ
Formally
- The language accepted by NFA is:
- where
- and there is some
M
( ) { }
,... , ,
3 2 1
w w w M L = } , ,..., , { ) , ( * …
k j i m
q q q w q = δ F qk ∈
(final state)
q
k
q w w w
) , ( *
0 w
q δ
( )
M L w∈ F qk ∈
i
q
j
q
What’s Next
– Linz Chapter 1, 2.1-2.3 (skip 2.4), 3.1, 3.2 – JFLAP Startup, Chapter 1, 2.1, (skip 2.3), 4
- Next Lecture Topics from Chapter 2.3, 3.1 and 3.2
– Equivalence of NFA and DFA – Regular Expressions and Regular Languages
- Quiz 1 in Recitation on Wednesday 9/17
– Covers Linz 1.1, 1.2, 2.1, 2.2, 2.3, and JFLAP 1, 2.1 – Closed book, but you may bring one sheet of 8.5 x 11 inch paper with any notes you like. – Quiz will take the full hour
– Homework 1 Due Today – Homework 2 Available By Friday Morning, – Homework 2 Due Next Thursday