Professional Documents
Culture Documents
Lecture 4 Closure and NFAs
Lecture 4 Closure and NFAs
Lecture 4 Closure and NFAs
• We can also have closure properties for classes of languages (e.g. regular
languages)
Theorem: If 𝑳𝟏 and 𝑳𝟐 are regular languages over alphabet Σ, then the language
𝑳𝟏 ∪ 𝑳𝟐 is a regular language
Proof: Since 𝑳𝟏 and 𝑳𝟐 are regular languages, then there exist DFAs 𝑴𝟏 and 𝑴𝟐
where 𝑳𝟏 = 𝑳(𝑴𝟏 ) and 𝑳𝟐 = 𝑳(𝑴𝟐 )
Proof idea: Construct a DFA 𝑴 that accepts exactly the strings accepted by 𝑴𝟏 as
well as the strings accepted by 𝑴𝟐
Example: Σ = 0, 1
• 𝑳𝟏 : set of strings that, after a possible prefix of 1s, consists of at least one 0
followed by at least one symbol
1 0, 1
𝑴1
𝑞1 0 𝑝𝑎 0, 1
𝑝𝑏
0, 1
𝑴2
𝑞2 0, 1 0, 1 𝑠𝑏
𝑠𝑎
Regular Languages are Closed under Union
1 0, 1 0, 1
𝑴1 𝑴2
𝑞1 0 𝑝𝑎 0, 1 𝑞2 0, 1 0, 1 𝑠𝑏
𝑝𝑏 𝑠𝑎
0 𝑝𝑎 , 𝑠𝑎
𝑞1 , 𝑞2
1 𝑞1 , 𝑠𝑎
• 𝒒𝟎 = (𝒒𝟏 , 𝒒𝟐 )
• Start from the state pair with starts states of 𝑴𝟏 and 𝑴𝟐
• 𝑭= 𝒓𝟏 , 𝒓𝟐 | 𝒓𝟏 ∈ 𝑭𝟏 or 𝒓𝟐 ∈ 𝑭𝟐
• Strings are accepted if at least one DFA accepts
𝑴 recognizes 𝑳𝟏 ∪ 𝑳𝟐
Regular Languages are Closed under Union
• Same example as before (fully worked out):
1 0, 1 0, 1
𝑴1 𝑴2
𝑞1 0 𝑝𝑎 0, 1 𝑞2 0, 1 0, 1 𝑠𝑏
𝑝𝑏 𝑠𝑎
𝑳 𝑴 = 𝑳𝟏 ∪ 𝑳𝟐
Transition table for 𝑴
𝑝𝑏 , 𝑠𝑎 𝑸 𝟎 𝟏
0, 1 0, 1 𝑞1 , 𝑞2 (𝑝𝑎 , 𝑠𝑎 ) (𝑞1 , 𝑠𝑎 )
0, 1 𝑞1 , 𝑠𝑎 (𝑝𝑎 , 𝑠𝑏 ) (𝑞1 , 𝑠𝑏 )
0, 1
𝑝𝑎 , 𝑞2 𝑝𝑏 , 𝑞2 𝑝𝑏 , 𝑠𝑏 (𝑞1 , 𝑠𝑏 ) (𝑝𝑎 , 𝑠𝑏 ) (𝑞1 , 𝑠𝑏 )
0, 1 0, 1 (𝑝𝑎 , 𝑞2 ) (𝑝𝑏 , 𝑠𝑎 ) (𝑝𝑏 , 𝑠𝑎 )
(𝑝𝑎 , 𝑠𝑎 ) (𝑝𝑏 , 𝑠𝑏 ) (𝑝𝑏 , 𝑠𝑏 )
𝑴 𝑝𝑎 , 𝑠𝑎 𝑝𝑎 , 𝑠𝑏
0 0 0 (𝑝𝑎 , 𝑠𝑏 ) (𝑝𝑏 , 𝑠𝑏 ) (𝑝𝑏 , 𝑠𝑏 )
(𝑝𝑏 , 𝑞2 ) (𝑝𝑏 , 𝑠𝑎 ) (𝑝𝑏 , 𝑠𝑎 )
𝑞1 , 𝑞2
1 𝑞1 , 𝑠𝑎
1 𝑞1 , 𝑠𝑏
1
(𝑝𝑏 , 𝑠𝑎 ) (𝑝𝑏 , 𝑠𝑏 ) (𝑝𝑏 , 𝑠𝑏 )
(𝑝𝑏 , 𝑠𝑏 ) (𝑝𝑏 , 𝑠𝑏 ) (𝑝𝑏 , 𝑠𝑏 )
Regular Languages are Closed under Union
Recall:
• 𝑳𝟏 : set of strings that, after a possible prefix of 1s, consists of at least one 0
followed by at least one symbol
• 𝑳𝟐 : set of all strings of length exactly 1
𝑝𝑏 , 𝑠𝑎
0, 1 0, 1
0, 1
0, 1
𝑝𝑎 , 𝑞2 𝑝𝑏 , 𝑞2 𝑝𝑏 , 𝑠𝑏
0, 1 0, 1
𝑝𝑎 , 𝑠𝑎 𝑝𝑎 , 𝑠𝑏
0 0 0
𝑞1 , 𝑞2
1 𝑞1 , 𝑠𝑎
1 𝑞1 , 𝑠𝑏
1
Regular Languages are Closed under Union
Recall:
• 𝑳𝟏 : set of strings that, after a possible prefix of 1s, consists of at least one 0
followed by at least one symbol
• 𝑳𝟐 : set of all strings of length exactly 1
𝑝𝑏 , 𝑠𝑎
0, 1 0, 1
0, 1
0, 1
𝑝𝑎 , 𝑞2 𝑝𝑏 , 𝑞2 𝑝𝑏 , 𝑠𝑏
0, 1 0, 1
0
𝑝𝑎 , 𝑠𝑎 𝑝𝑎 , 𝑠𝑏
0 0 0
𝑞1 , 𝑞2
1 𝑞1 , 𝑠𝑎
1 𝑞1 , 𝑠𝑏
1
Regular Languages are Closed under Union
Recall:
• 𝑳𝟏 : set of strings that, after a possible prefix of 1s, consists of at least one 0
followed by at least one symbol
• 𝑳𝟐 : set of all strings of length exactly 1
𝑝𝑏 , 𝑠𝑎
0, 1 0, 1
0, 1
0, 1
𝑝𝑎 , 𝑞2 𝑝𝑏 , 𝑞2 𝑝𝑏 , 𝑠𝑏
0, 1 0, 1
11100
𝑝𝑎 , 𝑠𝑎 𝑝𝑎 , 𝑠𝑏
0 0 0
𝑞1 , 𝑞2
1 𝑞1 , 𝑠𝑎
1 𝑞1 , 𝑠𝑏
1
Regular Languages are Closed under Intersection
Theorem: If 𝑳𝟏 and 𝑳𝟐 are regular languages over alphabet Σ, then the language
𝑳𝟏 ∩ 𝑳𝟐 is a regular language
Proof: Since 𝑳𝟏 and 𝑳𝟐 are regular languages, then there exist DFAs 𝑴𝟏 and 𝑴𝟐
where 𝑳𝟏 = 𝑳(𝑴𝟏 ) and 𝑳𝟐 = 𝑳(𝑴𝟐 )
Proof idea: We can create a DFA which simulates both machines concurrently
in the exact same way as the union closure proof.
However, now the strings that are accepted by the new DFA are the strings which
are accepted by both 𝑴𝟏 and 𝑴𝟐 (instead of either one)
Regular Languages are Closed under Intersection
Proof continued:
Let 𝑴𝟏 = 𝑸𝟏 , 𝚺, 𝜹𝟏 , 𝒒𝟏 , 𝑭𝟏 and 𝑴𝟐 = 𝑸𝟐 , 𝚺, 𝜹𝟐 , 𝒒𝟐 , 𝑭𝟐 be DFAs.
We construct DFA 𝑴 = 𝑸, 𝚺, 𝜹, 𝒒𝟎 , 𝑭 as follows:
• 𝑸= 𝒓𝟏 , 𝒓𝟐 | 𝒓𝟏 ∈ 𝑸𝟏 , 𝒓𝟐 ∈ 𝑸𝟐 ,
• 𝒒𝟎 = (𝒒𝟏 , 𝒒𝟐 )
The DFA construction is exactly the same
• 𝑭= 𝒓𝟏 , 𝒓𝟐 | 𝒓𝟏 ∈ 𝑭𝟏 𝐚𝐧𝐝 𝒓𝟐 ∈ 𝑭𝟐 as before, except for which states we
make accept states
𝑴 recognizes 𝑳𝟏 ∩ 𝑳𝟐
Regular Languages are Closed under Intersection
• 𝑳 𝑴 = 𝑳𝟏 ∩ 𝑳𝟐 (where 𝑳𝟏 and 𝑳𝟐 are same as union closure example)
1 0, 1 0, 1
𝑴1 𝑴2
𝑞1 0 𝑝𝑎 0, 1 𝑞2 0, 1 0, 1 𝑠𝑏
𝑝𝑏 𝑠𝑎
• Notice that the strings in 𝑳𝟏 all have length at least 2 and the strings in 𝑳𝟐 have
length exactly 1, so the 𝑳𝟏 ∩ 𝑳𝟐 = ∅
The only accept state
𝑝𝑏 , 𝑠𝑎
is unreachable
0, 1 0, 1
0, 1
0, 1
𝑝𝑎 , 𝑞2 𝑝𝑏 , 𝑞2 𝑝𝑏 , 𝑠𝑏
0, 1 0, 1
𝑝𝑎 , 𝑠𝑎 𝑝𝑎 , 𝑠𝑏
0 0 0
𝑞1 , 𝑞2
1 𝑞1 , 𝑠𝑎
1 𝑞1 , 𝑠𝑏
1
Regular Languages are Closed under Concatenation
• We will introduce a different computational model which makes this easier but
has the same computational power as a DFA
Deterministic Computation
Nondeterminism:
0 𝜀
0
Nondeterministic Finite Automata (NFA)
0, 1 1
0 𝜀
0
Differences between NFA and DFA state diagrams:
1. Transitions go from states to sets of states
• From a state 𝒒, reading symbol 𝒂 can transition to more than one state
• Formally 𝜹: 𝑄 × Σ ∪ 𝜀 → 𝓟(𝑄)
𝜺
2. We allow empty transitions (𝜺-transitions)
• Can take this transition without reading any symbol
𝜹 𝟎 𝟏 𝜺
𝑞1 {𝑞1 } {𝑞1 , 𝑞2 } {𝑞4 }
𝑞2 {𝑞3 , 𝑞4 } {𝑞4 } {𝑞3 }
𝑞3 ∅ {𝑞4 } ∅ State diagram omits
𝑞4 {𝑞4 } {𝑞4 } ∅ transitions into ∅
NFA Example 1
0, 𝜀
0, 1
𝑴𝟏 1 𝑞2 𝑞3
0, 1
𝑞1
1
𝑞4
𝜀
0, 1
What is the 𝑳 𝑴𝟏 ?
• Every string in 𝚺 ∗ is accepted
• 𝑳 𝑴𝟏 = 𝚺 ∗
NFA Example 2
0, 𝜀
0, 1
𝑴𝟐 1 𝑞2 𝑞3
0
𝑞1
𝜀
𝑞4
𝜀
0, 1
𝜹 𝟎 𝟏 𝜺
𝑞1 {𝑞1 } {𝑞1 , 𝑞2 } {𝑞4 }
𝑞2 {𝑞3 , 𝑞4 } ∅ {𝑞3 }
𝑞3 ∅ ∅ {𝑞4 }
𝑞4 {𝑞4 } {𝑞4 } ∅
NFA Example 2
0, 𝜀
0, 1
𝑴𝟐 1 𝑞2 𝑞3
0
𝑞1
𝜀
𝑞4
𝜀
0, 1
Does 𝑳 𝑴𝟐 = 𝑳 𝑴𝟏 ?
• Every string in 𝚺 ∗ is also accepted by 𝑴𝟐
• 𝑳 𝑴𝟐 = 𝑳 𝑴𝟏
NFA Example 3
0, 𝜀
0
𝑴𝟑 1 𝑞2 𝑞3
0
𝑞1
𝜀
𝑞4
𝜀
0
𝜹 𝟎 𝟏 𝜺
𝑞1 {𝑞1 } {𝑞2 } {𝑞4 }
𝑞2 {𝑞3 , 𝑞4 } ∅ {𝑞3 }
𝑞3 ∅ ∅ {𝑞4 }
𝑞4 {𝑞4 } ∅ ∅
NFA Example 3
0, 𝜀
0
𝑴𝟑 1 𝑞2 𝑞3
0
𝑞1
𝜀
𝑞4
𝜀
0
Does 𝑳 𝑴𝟑 = 𝑳 𝑴𝟏 ?
• No, not every string in 𝚺 ∗ is accepted by 𝑴𝟑
Then 𝑴 accepts 𝒘 if
• we can write 𝒘 = 𝒚𝟏 𝒚𝟐 … 𝒚𝒎 with 𝒚𝒊 ∈ 𝚺 ∪ {𝜺}
• and there is a sequence of states 𝒓𝟎 , 𝒓𝟏 , 𝒓𝟐 , … , 𝒓𝒎 in 𝑸 such that
1. 𝒓𝟎 = 𝒒𝟎
2. 𝜹 𝒓𝒊 , 𝒚𝒊+𝟏 = 𝒓𝒊+𝟏
3. 𝒓𝒎 ∈ 𝑭