Notes on State Minimization

Similar documents
We define the multi-step transition function T : S Σ S as follows. 1. For any s S, T (s,λ) = s. 2. For any s S, x Σ and a Σ,

More on Finite Automata and Regular Languages. (NTU EE) Regular Languages Fall / 41

Minimization of DFAs

COMP-330 Theory of Computation. Fall Prof. Claude Crépeau. Lec. 5 : DFA minimization

Closure Properties of Regular Languages. Union, Intersection, Difference, Concatenation, Kleene Closure, Reversal, Homomorphism, Inverse Homomorphism

COMP-330 Theory of Computation. Fall Prof. Claude Crépeau. Lec. 9 : Myhill-Nerode Theorem and applications

Computational Models - Lecture 3 1

Equivalence. See section 4.4 of the text

What we have done so far

CMPSCI 250: Introduction to Computation. Lecture #22: From λ-nfa s to NFA s to DFA s David Mix Barrington 22 April 2013

3515ICT: Theory of Computation. Regular languages

Finite Automata and Regular Languages

Equivalence. See section 4.4 of the text

cse303 ELEMENTS OF THE THEORY OF COMPUTATION Professor Anita Wasilewska

THEORY OF COMPUTATION (AUBER) EXAM CRIB SHEET

1 More finite deterministic automata

Fooling Sets and. Lecture 5

HKN CS/ECE 374 Midterm 1 Review. Nathan Bleier and Mahir Morshed

UNIT-VI PUSHDOWN AUTOMATA

Nonregular Languages

Formal Definition of Computation. August 28, 2013

Turing Machines A Turing Machine is a 7-tuple, (Q, Σ, Γ, δ, q0, qaccept, qreject), where Q, Σ, Γ are all finite

Equivalence of DFAs and NFAs

More on Regular Languages and Non-Regular Languages

Theory of Computation

Before we show how languages can be proven not regular, first, how would we show a language is regular?

CSE 135: Introduction to Theory of Computation Optimal DFA

Nondeterministic finite automata

U.C. Berkeley CS278: Computational Complexity Professor Luca Trevisan August 30, Notes for Lecture 1

Nondeterministic Finite Automata

2. Syntactic Congruences and Monoids

Chapter 2: Finite Automata

CMPSCI 250: Introduction to Computation. Lecture #32: The Myhill-Nerode Theorem David Mix Barrington 11 April 2014

Finite Automata. BİL405 - Automata Theory and Formal Languages 1

Java II Finite Automata I

Nondeterministic Finite Automata

Finite Automata. Mahesh Viswanathan

Finite Automata - Deterministic Finite Automata. Deterministic Finite Automaton (DFA) (or Finite State Machine)

Theory of Computation

Outline. Summary. DFA -> Regex. Finish off Regex -> e-nfa -> NFA -> DFA -> Regex Minimization/equivalence (Myhill-Nerode theorem)

DM17. Beregnelighed. Jacob Aae Mikkelsen

CSCE 551 Final Exam, Spring 2004 Answer Key

Computability and Complexity

Lecture 3: Nondeterministic Finite Automata

CS 121, Section 2. Week of September 16, 2013

Languages, regular languages, finite automata

Constructions on Finite Automata

Extended transition function of a DFA

Automata and Formal Languages - CM0081 Non-Deterministic Finite Automata

6.045 Final Exam Solutions

September 11, Second Part of Regular Expressions Equivalence with Finite Aut

Chap. 1.2 NonDeterministic Finite Automata (NFA)

Chapter Five: Nondeterministic Finite Automata

Chapter 6: NFA Applications

Computational Models - Lecture 3 1

Context Free Languages. Automata Theory and Formal Grammars: Lecture 6. Languages That Are Not Regular. Non-Regular Languages

Finite Automata and Regular languages

Properties of Regular Languages. BBM Automata Theory and Formal Languages 1

Nondeterministic Finite Automata. Nondeterminism Subset Construction

Regular Language Equivalence and DFA Minimization. Equivalence of Two Regular Languages DFA Minimization

September 7, Formal Definition of a Nondeterministic Finite Automaton

CSCE 551 Final Exam, April 28, 2016 Answer Key

CS 208: Automata Theory and Logic

CS294: Pseudorandomness and Combinatorial Constructions September 13, Notes for Lecture 5

CISC 4090: Theory of Computation Chapter 1 Regular Languages. Section 1.1: Finite Automata. What is a computer? Finite automata

CS 154 NOTES MOOR XU NOTES FROM A COURSE BY LUCA TREVISAN AND RYAN WILLIAMS

Computational Models - Lecture 3

Notes for Comp 497 (Comp 454) Week 5 2/22/05. Today we will look at some of the rest of the material in Part 1 of the book.

Deterministic Finite Automata. Non deterministic finite automata. Non-Deterministic Finite Automata (NFA) Non-Deterministic Finite Automata (NFA)

Closure under the Regular Operations

CPSC 421: Tutorial #1

Lecture Notes On THEORY OF COMPUTATION MODULE -1 UNIT - 2

CS243, Logic and Computation Nondeterministic finite automata

Mathematics for linguists

T (s, xa) = T (T (s, x), a). The language recognized by M, denoted L(M), is the set of strings accepted by M. That is,

Büchi Automata and their closure properties. - Ajith S and Ankit Kumar

CS 154 Introduction to Automata and Complexity Theory

ECS 120: Theory of Computation UC Davis Phillip Rogaway February 16, Midterm Exam

Prime Languages, Orna Kupferman, Jonathan Mosheiff. School of Engineering and Computer Science The Hebrew University, Jerusalem, Israel

Optimizing Finite Automata

Computational Models: Class 3

CSE 460: Computabilty and Formal Languages. Minimization of FA. S. Pramanik

1 Functions, relations, and infinite cardinality

PS2 - Comments. University of Virginia - cs3102: Theory of Computation Spring 2010

1 Computational Problems

V Honors Theory of Computation

Notes for Lecture 2. Statement of the PCP Theorem and Constraint Satisfaction

Chapter 2: Finite Automata

CS 455/555: Finite automata

Takeaway Notes: Finite State Automata

Ogden s Lemma. and Formal Languages. Automata Theory CS 573. The proof is similar but more fussy. than the proof of the PL4CFL.

FORMAL LANGUAGES, AUTOMATA AND COMPUTATION

Introduction to Kleene Algebra Lecture 9 CS786 Spring 2004 February 23, 2004

Finite Automata (contd)

1. Induction on Strings

Finite State Automata

Computational Models - Lecture 4

arxiv:cs/ v1 [cs.cc] 9 Feb 2007

Warshall s algorithm

Chapter Two: Finite Automata

Transcription:

U.C. Berkeley CS172: Automata, Computability and Complexity Handout 1 Professor Luca Trevisan 2/3/2015 Notes on State Minimization These notes present a technique to prove a lower bound on the number of states of any DFA that recognizes a given language. The technique can also be used to prove that a language is not regular. (By showing that for every k one needs at least k states to recognize the language.) We also present an efficient algorithm to convert a given DFA into a DFA for the same language and with a minimum number of states. 1 Distinguishable and Indistinguishable States It will be helpful to keep in mind the following two languages over the alphabet Σ = {0, 1} as examples: the language EQ = {0 n 1 n n 1} of strings containing a sequence of zeroes followed by an equally long sequence of ones, and the language A = (0 1) 1 (0 1) of strings containing a 1 in the second-to-last position. We start with the following basic notion. Definition 1 (Distinguishable Strings) Let L be a language over an alphabet Σ. We say that two strings x and y are distinguishable with respect to L if there is a string z such that xz L and yz L, or vice versa. For example the strings x = 0 and y = 00 are distinguishable with respect to EQ, because if we take z = 1 we have xz = 01 EQ and yz = 001 EQ. Also, the strings x = 00 and y=01 are distinguishable with respect to A as can be seen by taking z = 0. On the other hand, the strings x = 0110 and y = 10 are not distinguishable with respect to EQ because for every z we have xz EQ and yz EQ. Exercise 2 Find two strings that are not distinguishable with respect to A. The intuition behind Definition 1 is captured by the following simple fact. Lemma 3 Let L be a language, M be a DFA that decides L, and x and y be distinguishable strings with respect to L. Then the state reached by M on input x is different from the state reached by M on input y. Proof: Suppose by contradiction that M reaches the same state q on input x and on input y. Let z be the string such that xz L and yz L (or vice versa). Let us call q the state reached by M on input xz. Note that q is the state reached by M starting from q and given the string z. But also, on input yz, M must reach the same state q, because M reaches state q given y, and then goes from q to q given z. This means that M either accepts both xz and yz, or it rejects both. In either case, M is incorrect and we reach a contradiction. Consider now the following generalization of the notion of distinguishability. Definition 4 (Distinguishable Set of Strings) Let L be a language. A set of strings {x 1,..., x k } is distinguishable if for every two distinct strings x i, x j we have that x i is distinguishable from x j. 1

For example one can verify that {0, 00, 000} are distinguishable with respect to EQ and that {00, 01, 10, 11} are distinguishable with respect to A. We now prove the main result of this section. Lemma 5 Let L be a language, and suppose there is a set of k distinguishable strings with respect to L. Then every DFA for L has at least k states. Proof: If L is not regular, then there is no DFA for L, much less a DFA with less than k states. If L is regular, let M be a DFA for L, let x 1,..., x k be the distinguishable strings, and let q i be the state reached by M after reading x i. For every i j, we have that x i and x j are distinguishable, and so q i q j because of Lemma 3. So we have k different states q 1,..., q k in M, and so M has at least k states. Using Lemma 5 and the fact that the strings {00, 01, 10, 11} are distinguishable with respect to A we conclude that every DFA for A has at least 4 states. For every k 1, consider the set {0, 00,..., 0 k } of strings made of k or fewer zeroes. It is easy to see that this is a set of distinguishable strings with respect to EQ. This means that there cannot be a DFA for EQ, because, if there were one, it would have to have at least k states for every k, which is clearly impossible. 2 State Minimization Let L be a language over an alphabet Σ. We have seen in the previous section the definition of distinguishable strings with respect to L. We say that two strings x and y are indistinguishable, and we write it x L y if they are not distinguishable. That is, x L y means that, for every string z, the string xz belongs to L if and only if the string yz does. By definition, x L y if and only if y L x, and we always have x L x. It is also easy to see that if x L y and y L w then we must have x L w. In other words: Fact 6 The relation L is an equivalence relation over the strings in Σ. As you may remember from such classes as Math55 or CS70, when you define an equivalence relation over a set you also define a way to partition the set into a collection of subsets, called equivalence class. An equivalence class in Σ with respect to L is a set of strings that are all indistinguishable from one another, and that are all distinguishable from all the others not in the set. We denote by [x] the equivalence class that contains the string x. A fancy way of stating Lemma 5 is to say that every DFA for L must have at least as many states as the number of equivalence class in Σ with respect to L. Perhaps surprisingly, the converse is also true: there is always a DFA that has precisely as many states as the number of equivalence classes. Theorem 7 (Myhill-Nerode) Let L be a language over Σ. If Σ has infinitely many equivalence classes with respect to L, then L is not regular. Otherwise, L can be decided by a DFA whose number of states is equal to the number of equivalence classes in Σ with respect to L. Proof: If there are infinitely many equivalence classes, then it follows from Lemma 5 that no DFA can decide L, and so L is not regular. 2

Suppose then that there is a finite number of equivalence class. We define an automaton that has a state for each equivalence class. The start state is the class [ɛ] and every state of the form [x] for x L is a final state. It remains to describe the transition function. From a state [x], reading the character a, the automaton moves to state [xa]. We need to make sure that this definition makes sense. If x L x, then the state [x] and the state [x ] are the same, so we need to verify that the state [xa] and the state [x a] are also the same. That is, we need to verify that, for every string z, xaz L if and only if x az L; this is clearly true because x and x are indistinguishable and so appending the string az makes xaz an element of the language L if and only if it also makes x az an element of the language. So the automaton is well defined. Let now x = x 1 x 2 x n be an input string for the automaton. The automaton starts in [ɛ], then moves to state [x 1 ], and so on, and at the end is in state [x 1 x n ]; this is an accepting state if and only if x L, and so the automaton works correctly on x. The Myhill-Nerode theorem shows that one can use the distinguishability method to prove optimal lower bounds on the number of states of a DFA for a given language, but it does not give an efficient way to construct an optimal DFA. We will now see a polynomial time algorithm that given a DFA finds an equivalent DFA with a minimum number of states. There is even an O(n log n) algorithm for this problem, where n = Q Σ is the size of the input, but we will not see it. Before describing the algorithm, consider the following definition. Definition 8 (Equivalence of states) Let M = (Q, Σ, δ, q 0, F ) be a DFA. We say that two states p, q Q are equivalent, and we write it p q, if for every string x Σ the state that M reaches from p given x is accepting if and only if the state that M reaches from q given x is accepting. An equivalent way to think of the definition is that if we change M so that p is the start state the language that we recognize is the same as if we change M so that q is the start state. We can verify that is an equivalence relation among the set of states Q, and so partitions Q into a set of equivalence classes. The intution behind this definition is that if p q, then it is redundant to have two different states p and q. We would then like to compute the set of equivalence classes of Q with respect to and then construct a new automaton that has only one state for each equivalence class. The problem here is that it is not clear how to compute the relation, since it looks like we need to test infinitely many cases (one for each string x Σ ) in order to verify that p q. Fortunately, there is a shortcut. To discuss the shortcut, we need one more definition. Definition 9 Let M = (Q, Σ, δ, q 0, F ) be a DFA. We say that two states p, q Q are equivalent for length n, and we write it p n q, if for every string x Σ of length n the state that M reaches from p given x is accepting if and only if the state that M reaches from q given x is accepting. In other words, if we change M so that p becomes the start state, the set of strings of length at most n that we accept is precisely the same as if we had made q be the start state. The equivalence relations n can be computed recursively. First, note that p 0 q if and only if p is accepting and q is not (or vice versa). That is, the equivalence classes for 0 are the set F and the set Q F. 3

Suppose we have computed n 1. Now we have that p n q if and only if p n 1 q and for every a Σ we have δ(p, a) n 1 δ(q, a). The second part requires some justification, and we prove it as a lemma below. Lemma 10 Let M = (Q, Σ, δ, q 0, F ) be a DFA. For any two states p, q and integer n 1, we have that p n q if and only if p n 1 q or there is an a Σ such that δ(p, a) n 1 δ(q, a). Proof: If p n q, then there is a string x such that M accepts when starting from p and given x, but it rejects when starting from q and given x. If the length of x is n 1, then we have p n 1 q. Otherwise, let us write x = ax, where a is the first character of x, and let us call p = δ(p, a) and q = δ(q, a). Then the string x has length n 1 and it shows that p n 1 q. For the other direction, if p n 1 q then clearly also p n q. Otherwise, if there is an a such that δ(p, a) n 1 δ(q, a), then let x be the string of length n 1 that shows that δ(p, a) n 1 δ(q, a). Then the string ax of length n shows that p n q. This gives an O( Σ Q ) time algorithm to compute n given n 1, and so an O(n Σ Q ) time algorithm to compute n from scratch. Now we come to an important observation: If, for some k, we have that the relation k is the same as the relation k+1, then also k is the same as n for all n > k. To see that it is true, note that the process that we use to go from k to k+1 is independent of k, and so if it does not change the relation when applied once, it will not change the relation if applied an arbitrary number of times. This shows that the algorithm converges to a fixed partition in Q 1 steps, because it starts with two equivalence classes, it cannot create more than Q equivalence classes, and at each step it must either increase the number of equivalence classes or reach the final partition. In particular, if we let k = Q 1, then we have that k is the same as n for every n k, and it can be computed in time at most O( Q 2 Σ ). We can also see that k is the same relation as. This is true because if p q then certainly p k q. But also, if p q, then there is a string x that shows this is the case, and if we call n the length of x we have that p n q. If n k, then certainly this also means that p k q; if n > k we can still say that p k q because we have observed above that n and k are the same for n > k. We can now finally describe our state minimization algorithm. Given M = (Q, Σ, δ, q 0, F ). Let k = Q 1 and compute the equivalence classes of Q with respect to k. Define a new automaton M = (Q, Σ, δ, q 0, F ) as follows. There is a state in Q for each equivalence class. The initial state q 0 is the equivalence class [q 0]. The set F contain all the equivalence classes that contain final states in F. 1 Define δ ([q], a) = [δ(q, a)]. Remove from Q all the states that are not reachable from q 0. Such states can be easily found doing a depth-first search of the graph of the automaton starting from q 0. The removal of these states does not change the language accepted by automaton because they never occur in a computation. Let M = (Q, Σ, δ, q 0, F ) be the new automaton. 1 Note that an equivalence class contains either only states in F or only states not in F. 4

The reader should verify that our definition of δ makes sense and that the automaton M decides the same language as M. To see that the algorithm constructs an optimal automaton, let t = Q be number of states of the automaton. Every state [q] of the automaton is reachable from [q 0 ], so for every state [q] there is at least a string x [q] such that M reaches [q] when given in input x [q]. Consider now two different states [p] [q]. We have p q in M, and so there must be some string y such that starting from p in M we accept when given y, but starting from q we reject (or vice versa). Also, in M, starting from [p] we accept when given y but starting from [q] we reject. This means that y shows that the strings x [q] and x [p] are distinguishable, and so we have a set of t distinguishable strings, which implies that it is impossible to decide the same language with fewer than t states. 5