Lec 05
Lec 05
Ashutosh Trivedi
b a a
b
Department of Computer Science and Engineering,
Indian Institute of Technology Bombay.
Ashutosh Trivedi – 1 of 24
Ashutosh Trivedi DFA Equivalence and Minimization
DFA Equivalence and Minimization
Ashutosh Trivedi – 2 of 24
Ashutosh Trivedi DFA Equivalence and Minimization
DFA Equivalence and Minimization
Ashutosh Trivedi – 2 of 24
Ashutosh Trivedi DFA Equivalence and Minimization
How to minimize a DFA?
Two observations:
– Removing unreachable states: removing states unreachable from the
start state does not change the language accepted by a DFA.
Ashutosh Trivedi – 3 of 24
Ashutosh Trivedi DFA Equivalence and Minimization
How to minimize a DFA?
Two observations:
– Removing unreachable states: removing states unreachable from the
start state does not change the language accepted by a DFA.
– Merging equivalent states: merging equivalent states does not change
the language accepted by a DFA.
Ashutosh Trivedi – 3 of 24
Ashutosh Trivedi DFA Equivalence and Minimization
How to minimize a DFA?
Two observations:
– Removing unreachable states: removing states unreachable from the
start state does not change the language accepted by a DFA.
– Merging equivalent states: merging equivalent states does not change
the language accepted by a DFA.
Algorithms:
1. Breadth-first search or depth-first search (to identify reachable states)
2. table-filling algorithm (by E. F. Moore) (other algorithms exist due to
Hopcroft and Brzozowski)
Ashutosh Trivedi – 3 of 24
Ashutosh Trivedi DFA Equivalence and Minimization
Table-filling algorithm
– Two states are distinguishable if they are not equivalent.
– Formally, two states q1 , q2 are distinguishable, if there exists a string
w ∈ Σ∗ such that exactly one of δ̂(q1 , w) and δ(q2 , w) is an accepting
state.
– Table-filling algorithm is recursive discovery of distinguishable pairs.
Ashutosh Trivedi – 4 of 24
Ashutosh Trivedi DFA Equivalence and Minimization
Table-filling algorithm
– Two states are distinguishable if they are not equivalent.
– Formally, two states q1 , q2 are distinguishable, if there exists a string
w ∈ Σ∗ such that exactly one of δ̂(q1 , w) and δ(q2 , w) is an accepting
state.
– Table-filling algorithm is recursive discovery of distinguishable pairs.
– Basis: Pair (p, q) is distinguishable if p ∈ F and q 6∈ F.
Ashutosh Trivedi – 4 of 24
Ashutosh Trivedi DFA Equivalence and Minimization
Table-filling algorithm
– Two states are distinguishable if they are not equivalent.
– Formally, two states q1 , q2 are distinguishable, if there exists a string
w ∈ Σ∗ such that exactly one of δ̂(q1 , w) and δ(q2 , w) is an accepting
state.
– Table-filling algorithm is recursive discovery of distinguishable pairs.
– Basis: Pair (p, q) is distinguishable if p ∈ F and q 6∈ F. why?
Ashutosh Trivedi – 4 of 24
Ashutosh Trivedi DFA Equivalence and Minimization
Table-filling algorithm
– Two states are distinguishable if they are not equivalent.
– Formally, two states q1 , q2 are distinguishable, if there exists a string
w ∈ Σ∗ such that exactly one of δ̂(q1 , w) and δ(q2 , w) is an accepting
state.
– Table-filling algorithm is recursive discovery of distinguishable pairs.
– Basis: Pair (p, q) is distinguishable if p ∈ F and q 6∈ F. why?
– Induction: Pair (p, q) is distinguishable if states δ(p, a) and δ(q, a) are
distinguishable for some a ∈ Σ.
Ashutosh Trivedi – 4 of 24
Ashutosh Trivedi DFA Equivalence and Minimization
Table-filling algorithm
– Two states are distinguishable if they are not equivalent.
– Formally, two states q1 , q2 are distinguishable, if there exists a string
w ∈ Σ∗ such that exactly one of δ̂(q1 , w) and δ(q2 , w) is an accepting
state.
– Table-filling algorithm is recursive discovery of distinguishable pairs.
– Basis: Pair (p, q) is distinguishable if p ∈ F and q 6∈ F. why?
– Induction: Pair (p, q) is distinguishable if states δ(p, a) and δ(q, a) are
distinguishable for some a ∈ Σ. why?
Ashutosh Trivedi – 4 of 24
Ashutosh Trivedi DFA Equivalence and Minimization
Table-filling algorithm
– Two states are distinguishable if they are not equivalent.
– Formally, two states q1 , q2 are distinguishable, if there exists a string
w ∈ Σ∗ such that exactly one of δ̂(q1 , w) and δ(q2 , w) is an accepting
state.
– Table-filling algorithm is recursive discovery of distinguishable pairs.
– Basis: Pair (p, q) is distinguishable if p ∈ F and q 6∈ F. why?
– Induction: Pair (p, q) is distinguishable if states δ(p, a) and δ(q, a) are
distinguishable for some a ∈ Σ. why?
3. Return D ISTINGUISHABLE.
Ashutosh Trivedi – 4 of 24
Ashutosh Trivedi DFA Equivalence and Minimization
Correctness of Table-Filling Algorithm
Theorem
If two states are not distinguished by table-filling algorithm, then they are
equivalent.
Ashutosh Trivedi – 5 of 24
Ashutosh Trivedi DFA Equivalence and Minimization
Correctness of Table-Filling Algorithm
Theorem
If two states are not distinguished by table-filling algorithm, then they are
equivalent.
Ashutosh Trivedi – 5 of 24
Ashutosh Trivedi DFA Equivalence and Minimization
Correctness of Table-Filling Algorithm
Theorem
If two states are not distinguished by table-filling algorithm, then they are
equivalent.
Ashutosh Trivedi – 5 of 24
Ashutosh Trivedi DFA Equivalence and Minimization
Correctness of Table-Filling Algorithm
Theorem
If two states are not distinguished by table-filling algorithm, then they are
equivalent.
Ashutosh Trivedi – 5 of 24
Ashutosh Trivedi DFA Equivalence and Minimization
Correctness of Table-Filling Algorithm
Theorem
If two states are not distinguished by table-filling algorithm, then they are
equivalent.
Ashutosh Trivedi – 5 of 24
Ashutosh Trivedi DFA Equivalence and Minimization
Correctness of Table-Filling Algorithm
Theorem
If two states are not distinguished by table-filling algorithm, then they are
equivalent.
Ashutosh Trivedi – 5 of 24
Ashutosh Trivedi DFA Equivalence and Minimization
Correctness of Table-Filling Algorithm
Theorem
If two states are not distinguished by table-filling algorithm, then they are
equivalent.
Ashutosh Trivedi – 5 of 24
Ashutosh Trivedi DFA Equivalence and Minimization
Correctness of Table-Filling Algorithm
Theorem
If two states are not distinguished by table-filling algorithm, then they are
equivalent.
Ashutosh Trivedi – 6 of 24
Ashutosh Trivedi DFA Equivalence and Minimization
Minimization of DFAs
Ashutosh Trivedi – 6 of 24
Ashutosh Trivedi DFA Equivalence and Minimization
Minimization of DFAs
Theorem
A≡ is the minimum and unique (up to state renaming) DFA equivalent to A.
Ashutosh Trivedi – 6 of 24
Ashutosh Trivedi DFA Equivalence and Minimization
Proof of Minimality
Theorem
A≡ is the minimum and unique (up to state renaming) DFA equivalent to A.
Proof.
– The proof is by contradiction.
– Assume that there is a DFA B whose size is smaller than A≡ and
accepts that same language.
– Compute equivalent states of A≡ and B using table-filling algorithm.
– The initial states of both DFAs must be equivalent. (Why?)
– After reading any string w from their initial states, both DFAs will go
to states that are equivalent. (Why?)
– For every state of A≡ there is an equivalent state in B.
– Since the number of states of B are less than that of A≡ , there must be
at least two states p, q of A≡ that are equivalent to some state of B.
– Hence p and q must be equivalent, a contradiction.
Ashutosh Trivedi – 7 of 24
Ashutosh Trivedi DFA Equivalence and Minimization
DFA Equivalence and Minimization
Myhill-Nerode Theorem
Pumping Lemma
Ashutosh Trivedi – 8 of 24
Ashutosh Trivedi DFA Equivalence and Minimization
Myhill-Nerode Theorem
Ashutosh Trivedi – 9 of 24
Ashutosh Trivedi DFA Equivalence and Minimization
Myhill-Nerode Theorem
Ashutosh Trivedi – 10 of 24
Ashutosh Trivedi DFA Equivalence and Minimization
Myhill-Nerode Theorem
Proof.
The proof is in two parts.
– If L is regular, then a string-equivalence relation ≡L with finitely
many classes can be given by states of DFA accepting L.
Ashutosh Trivedi – 11 of 24
Ashutosh Trivedi DFA Equivalence and Minimization
Myhill-Nerode Theorem
Proof.
The proof is in two parts.
– If L is regular, then a string-equivalence relation ≡L with finitely
many classes can be given by states of DFA accepting L. How?
Ashutosh Trivedi – 11 of 24
Ashutosh Trivedi DFA Equivalence and Minimization
Myhill-Nerode Theorem
Proof.
The proof is in two parts.
– If L is regular, then a string-equivalence relation ≡L with finitely
many classes can be given by states of DFA accepting L. How?
– If there is a string-equivalence relation ≡L with finitely many classes,
one can find a DFA accepting L.
Ashutosh Trivedi – 11 of 24
Ashutosh Trivedi DFA Equivalence and Minimization
Myhill-Nerode Theorem
Proof.
The proof is in two parts.
– If L is regular, then a string-equivalence relation ≡L with finitely
many classes can be given by states of DFA accepting L. How?
– If there is a string-equivalence relation ≡L with finitely many classes,
one can find a DFA accepting L. How?
Ashutosh Trivedi – 11 of 24
Ashutosh Trivedi DFA Equivalence and Minimization
Applying Myhill-Nerode Theorem
Ashutosh Trivedi – 12 of 24
Ashutosh Trivedi DFA Equivalence and Minimization
Applying Myhill-Nerode Theorem
Equivalently,
Ashutosh Trivedi – 12 of 24
Ashutosh Trivedi DFA Equivalence and Minimization
Applying Myhill-Nerode Theorem
Theorem
The language L = {0n 1n : n ≥ 0} is not regular.
Ashutosh Trivedi – 13 of 24
Ashutosh Trivedi DFA Equivalence and Minimization
Applying Myhill-Nerode Theorem
Theorem
The language L = {0n 1n : n ≥ 0} is not regular.
Proof.
1. The proof is by contradiction.
2. Assume that L is regular.
Ashutosh Trivedi – 13 of 24
Ashutosh Trivedi DFA Equivalence and Minimization
Applying Myhill-Nerode Theorem
Theorem
The language L = {0n 1n : n ≥ 0} is not regular.
Proof.
1. The proof is by contradiction.
2. Assume that L is regular.
3. By Myhill-Nerode theorem, there is a string-equivalence relation ≡L
over L with finitely equivalence classes.
Ashutosh Trivedi – 13 of 24
Ashutosh Trivedi DFA Equivalence and Minimization
Applying Myhill-Nerode Theorem
Theorem
The language L = {0n 1n : n ≥ 0} is not regular.
Proof.
1. The proof is by contradiction.
2. Assume that L is regular.
3. By Myhill-Nerode theorem, there is a string-equivalence relation ≡L
over L with finitely equivalence classes.
4. Let us consider the set of strings {0, 00, 000, . . . , 0i , . . .}.
Ashutosh Trivedi – 13 of 24
Ashutosh Trivedi DFA Equivalence and Minimization
Applying Myhill-Nerode Theorem
Theorem
The language L = {0n 1n : n ≥ 0} is not regular.
Proof.
1. The proof is by contradiction.
2. Assume that L is regular.
3. By Myhill-Nerode theorem, there is a string-equivalence relation ≡L
over L with finitely equivalence classes.
4. Let us consider the set of strings {0, 00, 000, . . . , 0i , . . .}.
5. It must be the case that some two string 0m and 0n , with m 6= n are
mapped to same equivalence class.
Ashutosh Trivedi – 13 of 24
Ashutosh Trivedi DFA Equivalence and Minimization
Applying Myhill-Nerode Theorem
Theorem
The language L = {0n 1n : n ≥ 0} is not regular.
Proof.
1. The proof is by contradiction.
2. Assume that L is regular.
3. By Myhill-Nerode theorem, there is a string-equivalence relation ≡L
over L with finitely equivalence classes.
4. Let us consider the set of strings {0, 00, 000, . . . , 0i , . . .}.
5. It must be the case that some two string 0m and 0n , with m 6= n are
mapped to same equivalence class.
6. It implies that for all strings w ∈ Σ∗ we have that 0m .w ∈ L iff 0n .w ∈ L.
Ashutosh Trivedi – 13 of 24
Ashutosh Trivedi DFA Equivalence and Minimization
Applying Myhill-Nerode Theorem
Theorem
The language L = {0n 1n : n ≥ 0} is not regular.
Proof.
1. The proof is by contradiction.
2. Assume that L is regular.
3. By Myhill-Nerode theorem, there is a string-equivalence relation ≡L
over L with finitely equivalence classes.
4. Let us consider the set of strings {0, 00, 000, . . . , 0i , . . .}.
5. It must be the case that some two string 0m and 0n , with m 6= n are
mapped to same equivalence class.
6. It implies that for all strings w ∈ Σ∗ we have that 0m .w ∈ L iff 0n .w ∈ L.
7. However, 0m 1m ∈ L but 0n 1m 6∈ L, a contradiction.
8. Hence L is not regular.
Ashutosh Trivedi – 13 of 24
Ashutosh Trivedi DFA Equivalence and Minimization
Applying Myhill-Nerode Theorem
Theorem
The language L = {0n 1n : n ≥ 0} is not regular.
Proof.
Ashutosh Trivedi – 14 of 24
Ashutosh Trivedi DFA Equivalence and Minimization
Applying Myhill-Nerode Theorem
Theorem
The language L = {0n 1n : n ≥ 0} is not regular.
Proof.
1. From Myhill-Nerode theorem, a language L is nonregular if and only
if there exists an infinite subset M of Σ∗ where any two elements of M
are distinguishable with respect to L.
Ashutosh Trivedi – 14 of 24
Ashutosh Trivedi DFA Equivalence and Minimization
Applying Myhill-Nerode Theorem
Theorem
The language L = {0n 1n : n ≥ 0} is not regular.
Proof.
1. From Myhill-Nerode theorem, a language L is nonregular if and only
if there exists an infinite subset M of Σ∗ where any two elements of M
are distinguishable with respect to L.
2. Consider the set M = {0i : i ≥ 0}.
Ashutosh Trivedi – 14 of 24
Ashutosh Trivedi DFA Equivalence and Minimization
Applying Myhill-Nerode Theorem
Theorem
The language L = {0n 1n : n ≥ 0} is not regular.
Proof.
1. From Myhill-Nerode theorem, a language L is nonregular if and only
if there exists an infinite subset M of Σ∗ where any two elements of M
are distinguishable with respect to L.
2. Consider the set M = {0i : i ≥ 0}.
3. Since any two string in M are distinguishable with respect to L (i.e.
0m 0n ∈ L but 0n 1m 6∈ L for n 6= m), it follows from Myhill-Nerode
theorem that L is a non-regular language.
Ashutosh Trivedi – 14 of 24
Ashutosh Trivedi DFA Equivalence and Minimization
Some languages are not regular!
Ashutosh Trivedi – 15 of 24
Ashutosh Trivedi DFA Equivalence and Minimization
Some languages are not regular!
Ashutosh Trivedi – 15 of 24
Ashutosh Trivedi DFA Equivalence and Minimization
DFA Equivalence and Minimization
Myhill-Nerode Theorem
Pumping Lemma
Ashutosh Trivedi – 16 of 24
Ashutosh Trivedi DFA Equivalence and Minimization
Pumping Lemma
Ashutosh Trivedi – 17 of 24
Ashutosh Trivedi DFA Equivalence and Minimization
Pumping Lemma
Ashutosh Trivedi – 17 of 24
Ashutosh Trivedi DFA Equivalence and Minimization
Pumping Lemma
Why?
Ashutosh Trivedi – 17 of 24
Ashutosh Trivedi DFA Equivalence and Minimization
Pumping Lemma
Ashutosh Trivedi – 17 of 24
Ashutosh Trivedi DFA Equivalence and Minimization
Pumping Lemma
Ashutosh Trivedi – 17 of 24
Ashutosh Trivedi DFA Equivalence and Minimization
Pumping Lemma
Ashutosh Trivedi – 17 of 24
Ashutosh Trivedi DFA Equivalence and Minimization
A simple observation about DFA
0 1
1
start E O
start E . start E .
0 0
E . E .
1 0
O . E .
0 1
E . O .
Ashutosh Trivedi – 18 of 24
Ashutosh Trivedi DFA Equivalence and Minimization
A simple observation about DFA
Ashutosh Trivedi – 20 of 24
Ashutosh Trivedi DFA Equivalence and Minimization
Pumping Lemma
Theorem (Pumping Lemma for Regular Languages)
If L is a regular language, then there exists a constant (pumping length) p such
that for every string w ∈ L s.t. |w| ≥ p there exists a division of w in strings x, y,
and z s.t. w = xyz such that
1. |y| > 0,
2. |xy| ≤ p, and
3. for all i ≥ 0 we have that xyi z ∈ L.
Ashutosh Trivedi – 21 of 24
Ashutosh Trivedi DFA Equivalence and Minimization
Applying Pumping Lemma
Ashutosh Trivedi – 22 of 24
Ashutosh Trivedi DFA Equivalence and Minimization
Applying Pumping Lemma
Theorem
Prove that the language L = {0n 1n } is not regular.
Ashutosh Trivedi – 23 of 24
Ashutosh Trivedi DFA Equivalence and Minimization
Applying Pumping Lemma
Theorem
Prove that the language L = {0n 1n } is not regular.
Proof.
1. State the contrapositive of Pumping lemma.
2. Let p be an arbitrary number.
3. Consider the string 0p 1p ∈ L. Notice that |0p 1p | ≥ p.
Ashutosh Trivedi – 23 of 24
Ashutosh Trivedi DFA Equivalence and Minimization
Applying Pumping Lemma
Theorem
Prove that the language L = {0n 1n } is not regular.
Proof.
1. State the contrapositive of Pumping lemma.
2. Let p be an arbitrary number.
3. Consider the string 0p 1p ∈ L. Notice that |0p 1p | ≥ p.
4. Only way to break this string in xyz triplets such that |xy| ≤ p and
y 6= ε is to choose y = 0k for some 1 ≤ k ≤ p.
Ashutosh Trivedi – 23 of 24
Ashutosh Trivedi DFA Equivalence and Minimization
Applying Pumping Lemma
Theorem
Prove that the language L = {0n 1n } is not regular.
Proof.
1. State the contrapositive of Pumping lemma.
2. Let p be an arbitrary number.
3. Consider the string 0p 1p ∈ L. Notice that |0p 1p | ≥ p.
4. Only way to break this string in xyz triplets such that |xy| ≤ p and
y 6= ε is to choose y = 0k for some 1 ≤ k ≤ p.
5. For each such triplet, there exists an i (say i = 0) such that xyi z 6∈ L.
6. Hence L is non-regular.
Ashutosh Trivedi – 23 of 24
Ashutosh Trivedi DFA Equivalence and Minimization
Proving a language Regular
Proving Regularity
Pumping Lemma is necessary but not sufficient condition for regularity.
Ashutosh Trivedi – 24 of 24
Ashutosh Trivedi DFA Equivalence and Minimization
Proving a language Regular
Proving Regularity
Pumping Lemma is necessary but not sufficient condition for regularity.
Verify that this language satisfies the pumping condition, but is not
regular!
Ashutosh Trivedi – 24 of 24
Ashutosh Trivedi DFA Equivalence and Minimization