Lecture 7 DFA Minimization and Non-Regular Languages
Lecture 7 DFA Minimization and Non-Regular Languages
Non-regular Languages
CSC 320: Foundations of Computer Science
Quinton Yong
[email protected]
DFA State Minimization
• Given a DFA 𝑫, reduce the number of states without changing the language
recognized by 𝑫
0
𝑞1 𝑞3 𝑞6
0 1 0, 1 0, 1
0, 1 0, 1
𝑞0 𝑞5 𝑞7
1 0, 1
1 0, 1 0
𝑞2 𝑞4 𝑞6
0
In general, we cannot collapse states 𝒑 and 𝒒 if there is any string 𝒘 such that:
• when 𝒘 is processed starting at 𝒑, we yield acceptance
𝒘
𝒒 …
DFA State Equivalence
𝒑 … 𝒑 …
For all 𝒘 ∈ 𝚺 ∗ 𝒘 or 𝒘
𝒒 … 𝒒 …
In other words, 𝒑 and 𝒒 have exactly the same role in the DFA in terms of
accepting and not accepting strings
DFA State Minimization Algorithm
We work backwards, and mark which pairs of states cannot be equivalent
• Accept and non-accept states are not equivalent
𝑝, 𝑞 𝒑 𝒒
• If a pair of states 𝒙 and 𝒚 have a transition for the same symbol 𝒂 to non-
equivalent states, they also cannot be equivalent
𝒂 𝒑
𝒙
𝑥, 𝑦
𝒚 𝒂 𝒒
𝒂
𝒊 𝒙
• Continue until no more changes occur 𝑖, 𝑗
𝒂 𝒚
𝒋
DFA State Minimization Algorithm
Let 𝑴 be a DFA with no inaccessible states (first remove unreachable states)
1. Write down all unordered pairs 𝒑, 𝒒 of states in 𝑴
𝑝, 𝑞 𝒑 𝒒
𝒂 𝒂
𝒙 𝒑 𝒊 𝒙 This algorithm produces state
𝑥, 𝑦 𝑖, 𝑗 equivalence: 𝒑, 𝒒 unmarked
𝒂 𝒂
𝒚 𝒒 𝒋 𝒚 if and only if 𝒑~𝒒
DFA State Minimization Algorithm Example
0
𝑞1 𝑞3 𝑞6
0 1 0, 1 0, 1
0, 1 0, 1
𝑞0 𝑞5 𝑞7
1 0, 1
1 0, 1 0
𝑞2 𝑞4 𝑞6
0
1 1
𝑞2 𝑞4 0, 1
0
𝑞0 , 𝑞1 𝑞1 , 𝑞2 𝑞2 , 𝑞3 𝑞3 , 𝑞4 𝑞4 , 𝑞5
𝑞0 , 𝑞2 𝑞1 , 𝑞3 𝑞2 , 𝑞4 𝑞3 , 𝑞5
Write down all unordered pairs
𝒑, 𝒒 of states in 𝑴 𝑞0 , 𝑞3 𝑞1 , 𝑞4 𝑞2 , 𝑞5
𝑞0 , 𝑞4 𝑞1 , 𝑞5
𝑞0 , 𝑞5
DFA State Minimization Algorithm Example
0
𝑞1 𝑞3
0 1 0, 1
0, 1
𝑞0 𝑞5
1 1
𝑞2 𝑞4 0, 1
0
𝑞0 , 𝑞1 𝑞1 , 𝑞2 𝑞2 , 𝑞3 𝑞3 , 𝑞4 𝑞4 , 𝑞5
𝑞0 , 𝑞2 𝑞1 , 𝑞3 𝑞2 , 𝑞4 𝑞3 , 𝑞5
Mark each pair 𝒑, 𝒒 where 𝒑 ∈ 𝑭
𝑞0 , 𝑞3 𝑞1 , 𝑞4 𝑞2 , 𝑞5
and 𝒒 ∉ 𝑭 (or vice versa)
𝑞0 , 𝑞4 𝑞1 , 𝑞5
𝑞0 , 𝑞5
DFA State Minimization Algorithm Example
0
𝑞1 𝑞3
0 1 0, 1
0, 1
𝑞0 𝑞5
1 1
𝑞2 𝑞4 0, 1
0
𝑞0 , 𝑞1 𝑞1 , 𝑞2 𝑞2 , 𝑞3 𝑞3 , 𝑞4 𝑞4 , 𝑞5
𝑞0 , 𝑞2 𝑞1 , 𝑞3 𝑞2 , 𝑞4 𝑞3 , 𝑞5
Mark each pair 𝒑, 𝒒 where 𝒑 ∈ 𝑭
𝑞0 , 𝑞3 𝑞1 , 𝑞4 𝑞2 , 𝑞5
and 𝒒 ∉ 𝑭 (or vice versa)
𝑞0 , 𝑞4 𝑞1 , 𝑞5
𝑞0 , 𝑞5
DFA State Minimization Algorithm Example
0
𝑞1 𝑞3
0 1 0, 1
0, 1
𝑞0 𝑞5
1 1
𝑞2 𝑞4 0, 1
0
𝑞0 , 𝑞1 𝑞1 , 𝑞2 𝑞2 , 𝑞3 𝑞3 , 𝑞4 𝑞4 , 𝑞5
Repeat the until no changes occur:
If there exists an unmarked pair 𝒊, 𝒋 such that: 𝑞0 , 𝑞2 𝑞1 , 𝑞3 𝑞2 , 𝑞4 𝑞3 , 𝑞5
• 𝜹 𝒊, 𝒂 , 𝜹 𝒋, 𝒂 is marked for some 𝒂 ∈ 𝚺 𝑞0 , 𝑞3 𝑞1 , 𝑞4 𝑞2 , 𝑞5
• then mark 𝒊, 𝒋 𝑞0 , 𝑞4 𝑞1 , 𝑞5
𝑞0 , 𝑞5
DFA State Minimization Algorithm Example
0
𝑞1 𝑞3
0 1 0, 1
0, 1
𝑞0 𝑞5
1 1
𝑞2 𝑞4 0, 1
0
𝛿 𝑞0 , 0 , 𝛿(𝑞1 , 0) = {𝑞1 , 𝑞3 }
𝑞0 , 𝑞1 𝑞1 , 𝑞2 𝑞2 , 𝑞3 𝑞3 , 𝑞4 𝑞4 , 𝑞5
Repeat the until no changes occur:
If there exists an unmarked pair 𝒊, 𝒋 such that: 𝑞0 , 𝑞2 𝑞1 , 𝑞3 𝑞2 , 𝑞4 𝑞3 , 𝑞5
• 𝜹 𝒊, 𝒂 , 𝜹 𝒋, 𝒂 is marked for some 𝒂 ∈ 𝚺 𝑞0 , 𝑞3 𝑞1 , 𝑞4 𝑞2 , 𝑞5
• then mark 𝒊, 𝒋 𝑞0 , 𝑞4 𝑞1 , 𝑞5
𝑞0 , 𝑞5
DFA State Minimization Algorithm Example
0
𝑞1 𝑞3
0 1 0, 1
0, 1
𝑞0 𝑞5
1 1
𝑞2 𝑞4 0, 1
0
𝛿 𝑞0 , 0 , 𝛿(𝑞2 , 0) = {𝑞1 , 𝑞4 }
𝑞0 , 𝑞1 𝑞1 , 𝑞2 𝑞2 , 𝑞3 𝑞3 , 𝑞4 𝑞4 , 𝑞5
Repeat the until no changes occur:
If there exists an unmarked pair 𝒊, 𝒋 such that: 𝑞0 , 𝑞2 𝑞1 , 𝑞3 𝑞2 , 𝑞4 𝑞3 , 𝑞5
• 𝜹 𝒊, 𝒂 , 𝜹 𝒋, 𝒂 is marked for some 𝒂 ∈ 𝚺 𝑞0 , 𝑞3 𝑞1 , 𝑞4 𝑞2 , 𝑞5
• then mark 𝒊, 𝒋 𝑞0 , 𝑞4 𝑞1 , 𝑞5
𝑞0 , 𝑞5
DFA State Minimization Algorithm Example
0
𝑞1 𝑞3
0 1 0, 1
0, 1
𝑞0 𝑞5
1 1
𝑞2 𝑞4 0, 1
0
𝛿 𝑞1 , 0 , 𝛿(𝑞2 , 0) = {𝑞3 , 𝑞4 }
𝑞0 , 𝑞1 𝑞1 , 𝑞2 𝑞2 , 𝑞3 𝑞3 , 𝑞4 𝑞4 , 𝑞5
Repeat the until no changes occur:
If there exists an unmarked pair 𝒊, 𝒋 such that: 𝑞0 , 𝑞2 𝑞1 , 𝑞3 𝑞2 , 𝑞4 𝑞3 , 𝑞5
• 𝜹 𝒊, 𝒂 , 𝜹 𝒋, 𝒂 is marked for some 𝒂 ∈ 𝚺 𝑞0 , 𝑞3 𝑞1 , 𝑞4 𝑞2 , 𝑞5
• then mark 𝒊, 𝒋 𝑞0 , 𝑞4 𝑞1 , 𝑞5
𝑞0 , 𝑞5
DFA State Minimization Algorithm Example
0
𝑞1 𝑞3
0 1 0, 1
0, 1
𝑞0 𝑞5
1 1
𝑞2 𝑞4 0, 1
0
𝛿 𝑞1 , 1 , 𝛿(𝑞2 , 1) = {𝑞4 , 𝑞3 }
𝑞0 , 𝑞1 𝑞1 , 𝑞2 𝑞2 , 𝑞3 𝑞3 , 𝑞4 𝑞4 , 𝑞5
Repeat the until no changes occur:
If there exists an unmarked pair 𝒊, 𝒋 such that: 𝑞0 , 𝑞2 𝑞1 , 𝑞3 𝑞2 , 𝑞4 𝑞3 , 𝑞5
• 𝜹 𝒊, 𝒂 , 𝜹 𝒋, 𝒂 is marked for some 𝒂 ∈ 𝚺 𝑞0 , 𝑞3 𝑞1 , 𝑞4 𝑞2 , 𝑞5
• then mark 𝒊, 𝒋 𝑞0 , 𝑞4 𝑞1 , 𝑞5
𝑞0 , 𝑞5
DFA State Minimization Algorithm Example
0
𝑞1 𝑞3
0 1 0, 1
0, 1
𝑞0 𝑞5
1 1
𝑞2 𝑞4 0, 1
0
𝛿 𝑞3 , 0 , 𝛿(𝑞4 , 0) = {𝑞5 , 𝑞5 }
𝑞0 , 𝑞1 𝑞1 , 𝑞2 𝑞2 , 𝑞3 𝑞3 , 𝑞4 𝑞4 , 𝑞5
Repeat the until no changes occur:
If there exists an unmarked pair 𝒊, 𝒋 such that: 𝑞0 , 𝑞2 𝑞1 , 𝑞3 𝑞2 , 𝑞4 𝑞3 , 𝑞5
• 𝜹 𝒊, 𝒂 , 𝜹 𝒋, 𝒂 is marked for some 𝒂 ∈ 𝚺 𝑞0 , 𝑞3 𝑞1 , 𝑞4 𝑞2 , 𝑞5
• then mark 𝒊, 𝒋 𝑞0 , 𝑞4 𝑞1 , 𝑞5
𝑞0 , 𝑞5
DFA State Minimization Algorithm Example
0
𝑞1 𝑞3
0 1 0, 1
0, 1
𝑞0 𝑞5
1 1
𝑞2 𝑞4 0, 1
0
𝛿 𝑞3 , 1 , 𝛿(𝑞4 , 1) = {𝑞5 , 𝑞5 }
𝑞0 , 𝑞1 𝑞1 , 𝑞2 𝑞2 , 𝑞3 𝑞3 , 𝑞4 𝑞4 , 𝑞5
Repeat the until no changes occur:
If there exists an unmarked pair 𝒊, 𝒋 such that: 𝑞0 , 𝑞2 𝑞1 , 𝑞3 𝑞2 , 𝑞4 𝑞3 , 𝑞5
• 𝜹 𝒊, 𝒂 , 𝜹 𝒋, 𝒂 is marked for some 𝒂 ∈ 𝚺 𝑞0 , 𝑞3 𝑞1 , 𝑞4 𝑞2 , 𝑞5
• then mark 𝒊, 𝒋 𝑞0 , 𝑞4 𝑞1 , 𝑞5
𝑞0 , 𝑞5
DFA State Minimization Algorithm Example
0
𝑞1 𝑞3
0 1 0, 1
0, 1
𝑞0 𝑞5
1 1
𝑞2 𝑞4 0, 1
0
𝛿 𝑞3 , 0 , 𝛿(𝑞5 , 0) = {𝑞5 , 𝑞5 }
𝑞0 , 𝑞1 𝑞1 , 𝑞2 𝑞2 , 𝑞3 𝑞3 , 𝑞4 𝑞4 , 𝑞5
Repeat the until no changes occur:
If there exists an unmarked pair 𝒊, 𝒋 such that: 𝑞0 , 𝑞2 𝑞1 , 𝑞3 𝑞2 , 𝑞4 𝑞3 , 𝑞5
• 𝜹 𝒊, 𝒂 , 𝜹 𝒋, 𝒂 is marked for some 𝒂 ∈ 𝚺 𝑞0 , 𝑞3 𝑞1 , 𝑞4 𝑞2 , 𝑞5
• then mark 𝒊, 𝒋 𝑞0 , 𝑞4 𝑞1 , 𝑞5
𝑞0 , 𝑞5
DFA State Minimization Algorithm Example
0
𝑞1 𝑞3
0 1 0, 1
0, 1
𝑞0 𝑞5
1 1
𝑞2 𝑞4 0, 1
0
𝛿 𝑞3 , 1 , 𝛿(𝑞5 , 1) = {𝑞5 , 𝑞5 }
𝑞0 , 𝑞1 𝑞1 , 𝑞2 𝑞2 , 𝑞3 𝑞3 , 𝑞4 𝑞4 , 𝑞5
Repeat the until no changes occur:
If there exists an unmarked pair 𝒊, 𝒋 such that: 𝑞0 , 𝑞2 𝑞1 , 𝑞3 𝑞2 , 𝑞4 𝑞3 , 𝑞5
• 𝜹 𝒊, 𝒂 , 𝜹 𝒋, 𝒂 is marked for some 𝒂 ∈ 𝚺 𝑞0 , 𝑞3 𝑞1 , 𝑞4 𝑞2 , 𝑞5
• then mark 𝒊, 𝒋 𝑞0 , 𝑞4 𝑞1 , 𝑞5
𝑞0 , 𝑞5
DFA State Minimization Algorithm Example
0
𝑞1 𝑞3
0 1 0, 1
0, 1
𝑞0 𝑞5
1 1
𝑞2 𝑞4 0, 1
0
𝛿 𝑞4 , 0 , 𝛿(𝑞5 , 0) = {𝑞5 , 𝑞5 }
𝑞0 , 𝑞1 𝑞1 , 𝑞2 𝑞2 , 𝑞3 𝑞3 , 𝑞4 𝑞4 , 𝑞5
Repeat the until no changes occur:
If there exists an unmarked pair 𝒊, 𝒋 such that: 𝑞0 , 𝑞2 𝑞1 , 𝑞3 𝑞2 , 𝑞4 𝑞3 , 𝑞5
• 𝜹 𝒊, 𝒂 , 𝜹 𝒋, 𝒂 is marked for some 𝒂 ∈ 𝚺 𝑞0 , 𝑞3 𝑞1 , 𝑞4 𝑞2 , 𝑞5
• then mark 𝒊, 𝒋 𝑞0 , 𝑞4 𝑞1 , 𝑞5
𝑞0 , 𝑞5
DFA State Minimization Algorithm Example
0
𝑞1 𝑞3
0 1 0, 1
0, 1
𝑞0 𝑞5
1 1
𝑞2 𝑞4 0, 1
0
𝛿 𝑞4 , 1 , 𝛿(𝑞5 , 1) = {𝑞5 , 𝑞5 }
𝑞0 , 𝑞1 𝑞1 , 𝑞2 𝑞2 , 𝑞3 𝑞3 , 𝑞4 𝑞4 , 𝑞5
Repeat the until no changes occur:
If there exists an unmarked pair 𝒊, 𝒋 such that: 𝑞0 , 𝑞2 𝑞1 , 𝑞3 𝑞2 , 𝑞4 𝑞3 , 𝑞5
• 𝜹 𝒊, 𝒂 , 𝜹 𝒋, 𝒂 is marked for some 𝒂 ∈ 𝚺 𝑞0 , 𝑞3 𝑞1 , 𝑞4 𝑞2 , 𝑞5
• then mark 𝒊, 𝒋 𝑞0 , 𝑞4 𝑞1 , 𝑞5
𝑞0 , 𝑞5
DFA State Minimization Algorithm Example
0
𝑞1 𝑞3
0 1 0, 1
0, 1
𝑞0 𝑞5
1 1
𝑞2 𝑞4 0, 1
0
1 1
𝑞2 𝑞4 0, 1
0
𝑞0 , 𝑞1 𝑞1 , 𝑞2 𝑞2 , 𝑞3 𝑞3 , 𝑞4 𝑞4 , 𝑞5
Equivalent: 𝑞0 , 𝑞2 𝑞1 , 𝑞3 𝑞2 , 𝑞4 𝑞3 , 𝑞5
𝑝1 ∶ 𝑞1 ~𝑞2 𝑞0 , 𝑞3 𝑞1 , 𝑞4 𝑞2 , 𝑞5
𝑝2 ∶ 𝑞3 ~𝑞4 ~𝑞5 𝑞0 , 𝑞4 𝑞1 , 𝑞5
𝑞0 , 𝑞5
DFA State Minimization Algorithm Example
0
Equivalent: 𝑞1 𝑞3
0 1 0, 1
0, 1
𝑝1 ∶ 𝑞1 ~𝑞2 𝑞0 𝑞5
𝑝2 ∶ 𝑞3 ~𝑞4 ~𝑞5 1 1
𝑞2 𝑞4 0, 1
0
0, 1
𝑞0 0, 1 𝑝1 0, 1 𝑝2
Non-regular Languages
All Languages
Regular Languages
• The set of regular languages is the set of all languages recognized by DFAs,
NFAs, and regular expressions
• There also exist languages which cannot be recognized by DFAs, NFAs, nor
regular expressions…
Exercise
Consider the following language:
𝑳 = 𝒘 ∈ 𝟎, 𝟏 ∗ 𝒘 has an equal number of occurrences of 𝟎𝟏 and 𝟏𝟎
Is 𝑳 a regular language?
Exercise
∗
• 𝑳𝟏 = 𝒘 ∈ 𝟎, 𝟏 𝒘 has an equal number of 𝟎s and 𝟏s
?
• 𝑳𝟐 = 𝟎𝒏 𝟏𝒏 𝒏 ≥ 𝟎
?
• 𝑳𝟑 = 𝟎𝟑 𝟏𝒏 𝒏 ≥ 𝟎
Yes. 𝑳𝟑 = 𝑳 𝑹 with 𝑹 = 𝟎𝟎𝟎𝟏∗
Non-Regular Languages
• Technique for proving that languages are non-regular: the pumping lemma
Pumping Lemma
2. 𝒙𝒚 ≤ 𝒑
Notes:
• 𝒚𝒊 means concatenation of 𝒊 copies of substring 𝒚
• Conditions 1 to 3 hold for all strings in 𝐿 that are of length at least 𝒑
• We only use the pumping lemma to prove that languages are non-regular
Using the Pumping Lemma
Let 𝑳 = 𝟎𝒏 𝟏𝒏 𝒏 ≥ 𝟎 . Prove that 𝑳 is non-regular.
• Then all the properties of the pumping lemma must hold for 𝑳:
• For every 𝒔 ∈ 𝑳 and 𝒔 ≥ 𝒑, then there is a way to rewrite 𝒔 = 𝒙𝒚𝒛 such that
1. 𝒚 > 𝟎 (i.e. 𝒚 ≠ 𝜺)
2. 𝒙𝒚 ≤ 𝒑
3. 𝒙𝒚𝒊 𝒛 ∈ 𝑳 for each 𝒊 ≥ 𝟎
Using the Pumping Lemma 𝑳= 𝟎𝒏 𝟏𝒏 𝒏 ≥ 𝟎
Proof continued:
• Choose 𝒔 = 𝟎𝒑 𝟏𝒑
Using the Pumping Lemma 𝑳= 𝟎𝒏 𝟏𝒏 𝒏 ≥ 𝟎
• Choose 𝒔 = 𝟎𝒑 𝟏𝒑
• So, we need to consider all ways of rewriting 𝒔 and show that all of them break
Using the Pumping Lemma 𝑳= 𝟎𝒏 𝟏𝒏 𝒏 ≥ 𝟎
• Choose 𝒔 = 𝟎𝒑 𝟏𝒑
𝒙𝒚𝒛
• Because of property 1, 𝒚 ≠ 𝜺, therefore 𝒚 can be the following: 𝟎…𝟎 𝟏…𝟏
𝒑 𝒑
• Case 1: 𝒚 consists of only 0’s
Prove that 𝑳 = 𝒘 ∈ 𝟎, 𝟏 ∗
𝒘 has an equal number of 𝟎s and 𝟏s is not regular.
Proof:
• We choose 𝒔 = 𝟏𝒑 𝟎𝒑 .
• We choose 𝒔 = 𝟏𝒑 𝟎𝒑 .
• Consider 𝒙𝒚𝟎 𝒛 = 𝒙𝒛. The string 𝒙𝒛 ∉ 𝑳 since it contains less 𝟏’s than 𝟎’s