Upload
haque
View
220
Download
0
Embed Size (px)
Citation preview
1
CS 301 - Lecture 2 Regular Languages and Nondeterministic Finite
Automata Fall 2008
Review • Languages and Grammars
– Alphabets, strings, languages • Deterministic Finite Automata (DFA)
– Definition and Use • Today:
– Regular Languages – Nondeterministic Finite State Automata – Equivalence with DFA
Languages Accepted by DFAs • Take DFA
• Definition: – The language contains all input
strings accepted by
– = { strings that drive to a final state}
M
( )MLM
( )ML M
Example
0q 1q 2q 3q 4qa b b a
5q
a a bb
ba,
ba,
( ) { }abbaML =
M
accept
2
Another Example
0q 1q 2q 3q 4qa b b a
5q
a a bb
ba,
ba,
( ) { }abbaabML ,,λ=
M
accept accept accept
Formally • For a DFA
• Language accepted by :
( )FqQM ,,,, 0δΣ=
M( ) ( ){ }FwqwML ∈Σ∈= ,*:* 0δ
0q q′w Fq ∈′
Observation
• Language rejected by : ( ) ( ){ }FwqwML ∉Σ∈= ,*:* 0δ
0q q′w Fq ∉′
M
More Examples
a
b ba,
ba,
0q 1q 2q
( ) }0:{ ≥= nbaML n
accept trap state
3
( )ML = { all strings with prefix } ab
a b
ba,
0q 1q 2q
accept
ba,3q
ab
( )ML = { all strings without substring } 001
λ 0 00 001
1
0
110
0 1,0
Regular Languages
• A language is regular if there is a DFA such that
• All regular languages form a language family
L M
( )MLL =
{ }abba
{ }abbaab,,λ}0:{ ≥nban
{ all strings with prefix } ab{ all strings without substring } 001
Examples of regular languages:
There exist automata that accept these Languages
4
Another Example { }{ }*,: bawawaL ∈=
a
b
ba,
a
b
ba
0q 2q 3q
4q ( )MLL =
There exist languages which are not Regular:
}0:{ ≥= nbaL nn
There is no DFA that accepts such a language
(we will prove this later in the semester)
Example:
1q 2q
3q
a
a
a
0q
}{aAlphabet =
Nondeterministic Finite Accepter (NFA)
1q 2q
3q
a
a
a
0q
Two choices
}{aAlphabet =
Nondeterministic Finite Accepter (NFA)
5
No transition
1q 2q
3q
a
a
a
0q
Two choices No transition
}{aAlphabet =
Nondeterministic Finite Accepter (NFA)
a a
0q
1q 2q
3q
a
a
First Choice
a
a a
0q
1q 2q
3q
a
a
a
First Choice
a a
0q
1q 2q
3q
a
a
First Choice
a
6
a a
0q
1q 2q
3q
a
a
a “accept”
First Choice
All input is consumed a a
0q
1q 2q
3q
a
a
Second Choice
a
a a
0q
1q 2qa
a
Second Choice
a
3q
a a
0q
1q 2qa
a
a
3q
Second Choice
No transition: the automaton hangs
7
a a
0q
1q 2qa
a
a
3q
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:
0q
1q 2q
3q
a
a
a
“accept”
0q
1q 2qa
a
a
3q
“reject??” because this computation accepts aa
But this only tells us that choice didn’t work….
a
0q
1q 2q
3q
a
a
Rejection example
a
8
a
0q
1q 2q
3q
a
a
a
First Choice
a
0q
1q 2q
3q
a
a
a
First Choice
“reject??”
Second Choice
a
0q
1q 2q
3q
a
a
a
Second Choice
a
0q
1q 2qa
a
a
3q
9
Second Choice
a
0q
1q 2qa
a
a
3q “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:
0q
1q 2qa
a
a
3q “reject??” 0q
1q 2qa
a
a
3q
“reject??”
All possible computations lead to rejection
Rejection example
a a
0q
1q 2q
3q
a
a
a
a
10
a a
0q
1q 2q
3q
a
a
a
First Choice
a a a
0q
1q 2q
3q
a
a
First Choice
a
a
No transition: the automaton hangs
a a
0q
1q 2q
3q
a
a
a “reject??”
First Choice
aInput cannot be consumed
a a
0q
1q 2q
3q
a
a
Second Choice
a
a
11
a a
0q
1q 2qa
a
Second Choice
a
3q
a a a
0q
1q 2qa
a
a
3q
Second Choice
No transition: the automaton hangs
a
a a
0q
1q 2qa
a
a
3q
Second Choice
“reject??”
aInput cannot be consumed
aaa is rejected by the NFA:
0q
1q 2q
3q
a
a
a
“reject??”
0q
1q 2qa
a
a
3q
“reject??”
All possible computations lead to rejection
12
1q 2q
3q
a
a
a
0q
Language accepted: }{aaL = Lambda Transitions
1q 3qa0q λ 2q a
a a
1q 3qa0q λ 2q a
a a
1q 3qa0q λ 2q a
13
a a
1q 3qa0q λ 2q a
(read head does not move)
a a
1q 3qa0q λ 2q a
a a
1q 3qa0q λ 2q a
“accept”
String is accepted aa
all input is consumed
a a
1q 3qa0q λ 2q a
Rejection Example
a
14
a a
1q 3qa0q λ 2q a
a a a
1q 3qa0q λ 2q a
(read head doesn’t move)
a
a a
1q 3qa0q λ 2q a
a
No transition: the automaton hangs
a a
1q 3qa0q λ 2q a
“reject??”
String is rejected aaa
a
Input cannot be consumed
15
Language accepted: }{aaL =
1q 3qa0q λ 2q a
Another NFA Example
0q 1q 2qa b λ
λ
3q
a b
0q 1q 2qa b λ
λ
3q 0q 2qa b λ
λ
3q
a b
1q
16
a b
0q 1qa b λ
λ
3q2q
a b
0q 1qa b λ
λ
3q2q“accept”
0q a b λ
λ
a b
Another String
a b
1q 2q 3q 0q a b λ
λ
a b a b
1q 2q 3q
17
0q a b λ
λ
a b a b
1q 2q 3q 0q a b λ
λ
a b a b
1q 2q 3q
0q a b λ
λ
a b a b
1q 2q 3q 0q a b λ
λ
a b a b
1q 2q 3q
18
0q a b λ
λ
a b a b
1q 2q 3q
a b a b
0q a b λ
λ
1q 2q 3q“accept”
{ }
{ }+=
=
ab
ababababababL ...,,,
Language accepted
0q 1q 2qa b λ
λ
3q
Another NFA Example
0q 1q 2q0
11,0
λ
19
0q 1q 2q0
11,0
λ
Language accepted
(redundant state)
Remarks: • The symbol never appears on the input tape
λ
0q2M
0q1M
{}=)M(L 1
• Simple automata:
0q
2q
1qaa
a
0q 1qa
}{=)( 1 aML
2M1M
}{=)( 2 aML
NFA DFA
• NFAs are interesting because we can express languages easier than DFAs
Formal Definition of NFAs
•
( )FqQM ,,,, 0δΣ=
:Q
:δ:0q
:F
Set of states, i.e. { }210 ,, qqq
:Σ Input aplhabet, i.e. { }ba,Transition function
Initial state
Final states
20
( ) { }10 1, qq =δ
0
11,0
λ
Transition Function
0q 1q 2q
δ
Note the function returns a set…. What did a DFA transition funciton return?
0q0
11,0
λ
},{)0,( 201 qqq =δ
1q 2q
0q0
11,0
λ
1q 2q
},{),( 200 qqq =λδ
0q0
11,0
λ
1q 2q
∅=)1,( 2qδ
21
Extended Transition Function
0q
λ
5q4q
3q2q1qaaab λ
( ) { }10,* qaq =δ ( ) { }540 ,,* qqaaq =δ
0q
λ
5q4q
3q2q1qaaab λ
( ) { }0320 ,,,* qqqabq =δ
0q
λ
5q4q
3q2q1qaaab λ
Formally ( )wqq ij ,*δ∈ : there is a walk from to
with label iq jq
w
iq jq
w
kw σσσ 21=1σ 2σ kσ
iq jq
22
The Language of an NFA
0q
λ
5q4q
3q2q1qaaab λ
( ) { }540 ,,* qqaaq =δ )(MLaa∈
{ }50,qqF =
F∈
0q
λ
5q4q
3q2q1qaaab λ
( ) { }0320 ,,,* qqqabq =δ ( )MLab∈
{ }50,qqF =
F∈
0q
λ
5q4q
3q2q1qaaab λ
{ }50,qqF =
( ) { }540 ,,* qqabaaq =δF∈
0q
λ
5q4q
3q2q1qaaab λ
{ }50,qqF =
( ) { }10,* qabaq =δ ( )MLaba∉F∉
23
0q
λ
5q4q
3q2q1qaaab λ
( ) { } { } }{* aaabML ∪= λ
Formally
• The language accepted by NFA is:
• where
• and there is some
M
( ) { },...,, 321 wwwML =
},,...,,{),(* 0 …kjim qqqwq =δ
Fqk ∈ (final state)
0q kq
w
w
w
),(* 0 wqδ( )MLw∈
Fqk ∈
iq
jq
What’s Next • Read
– 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 – Homework 1 Due Today – Homework 2 Available By Friday Morning, – Homework 2 Due Next Thursday