Lecture 5: Minimizing DFAs

Similar documents
CS 154, Lecture 4: Limitations on DFAs (I), Pumping Lemma, Minimizing DFAs

CS154. Non-Regular Languages, Minimizing DFAs

FORMAL LANGUAGES, AUTOMATA AND COMPUTABILITY

Minimization of DFAs

Chapter 2: Finite Automata

CSE 311 Lecture 25: Relating NFAs, DFAs, and Regular Expressions. Emina Torlak and Kevin Zatloukal

Lecture 4: More on Regexps, Non-Regular Languages

CSE 311: Foundations of Computing I Autumn 2014 Practice Final: Section X. Closed book, closed notes, no cell phones, no calculators.

Great Theoretical Ideas in Computer Science. Lecture 4: Deterministic Finite Automaton (DFA), Part 2

Lecture 4 Nondeterministic Finite Accepters

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,

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

Lecture 1: Finite State Automaton

CSE 135: Introduction to Theory of Computation Optimal DFA

Nondeterministic Finite Automata

FORMAL LANGUAGES, AUTOMATA AND COMPUTABILITY

Automata and Formal Languages - CM0081 Non-Deterministic Finite Automata

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

What we have done so far

CS 301. Lecture 18 Decidable languages. Stephen Checkoway. April 2, 2018

Formal Language and Automata Theory (CS21004)

CSE 311: Foundations of Computing. Lecture 23: Finite State Machine Minimization & NFAs

Nondeterministic Finite Automata. Nondeterminism Subset Construction

Languages. Non deterministic finite automata with ε transitions. First there was the DFA. Finite Automata. Non-Deterministic Finite Automata (NFA)

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

Finite Universes. L is a fixed-length language if it has length n for some

Nondeterministic Finite Automata

Lecture 2: Regular Expression

Chapter 6: NFA Applications

CS 154, Lecture 3: DFA NFA, Regular Expressions

Chapter Five: Nondeterministic Finite Automata

Nondeterministic finite automata

CMSC 330: Organization of Programming Languages

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

CS 154. Finite Automata, Nondeterminism, Regular Expressions

Finite Automata. Wen-Guey Tzeng Computer Science Department National Chiao Tung University

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

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

CS243, Logic and Computation Nondeterministic finite automata

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

Automata and Languages

Recap DFA,NFA, DTM. Slides by Prof. Debasis Mitra, FIT.

Regular Expressions. Definitions Equivalence to Finite Automata

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

Computational Models - Lecture 5 1

Harvard CS 121 and CSCI E-207 Lecture 6: Regular Languages and Countability

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

Algorithms for NLP

CS 154, Lecture 2: Finite Automata, Closure Properties Nondeterminism,

Name: Student ID: Instructions:

CSE 105 THEORY OF COMPUTATION

1 Showing Recognizability

CS 154 Introduction to Automata and Complexity Theory

3515ICT: Theory of Computation. Regular languages

Regular Expressions and Language Properties

Computational Models: Class 3

CS 455/555: Mathematical preliminaries

Finite Automata and Regular Languages

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

Computational Models Lecture 8 1

Finite Automata and Formal Languages TMV026/DIT321 LP Testing Equivalence of Regular Languages

Computational Models - Lecture 3

cse303 ELEMENTS OF THE THEORY OF COMPUTATION Professor Anita Wasilewska

16.1 Countability. CS125 Lecture 16 Fall 2014

Computational Models - Lecture 1 1

Computability and Complexity

CS154, Lecture 10: Rice s Theorem, Oracle Machines

Chap. 2 Finite Automata

Lecture 13: Foundations of Math and Kolmogorov Complexity

Intro to Theory of Computation

Homomorphisms and Efficient State Minimization

DFA to Regular Expressions

CS 482 Problem Set 2 Solutions Dan FitzGerald, dpf7

What languages are Turing-decidable? What languages are not Turing-decidable? Is there a language that isn t even Turingrecognizable?

CS 154 NOTES CONTENTS

CSE 105 THEORY OF COMPUTATION. Spring 2018 review class

Equivalence of DFAs and NFAs

Fooling Sets and. Lecture 5

Incorrect reasoning about RL. Equivalence of NFA, DFA. Epsilon Closure. Proving equivalence. One direction is easy:

CSE 311 Lecture 23: Finite State Machines. Emina Torlak and Kevin Zatloukal

Let us first give some intuitive idea about a state of a system and state transitions before describing finite automata.

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

CSE 105 THEORY OF COMPUTATION

Computational Models Lecture 8 1

Uses of finite automata

Chapter 2: Finite Automata

2017/08/29 Chapter 1.2 in Sipser Ø Announcement:

Automata & languages. A primer on the Theory of Computation. Laurent Vanbever. ETH Zürich (D-ITET) September,

CMPSCI 250: Introduction to Computation. Lecture #23: Finishing Kleene s Theorem David Mix Barrington 25 April 2013

Computational Models Lecture 2 1

Theory of Computation (VI) Yijia Chen Fudan University

Lecture 3: Nondeterministic Finite Automata

Computational Models #1

Theory of Computation

Computational Models Lecture 2 1

CSE 311: Foundations of Computing. Lecture 26: More on Limits of FSMs, Cardinality

CSE 105 THEORY OF COMPUTATION

Chap. 2 Finite Automata

Computational Models - Lecture 3 1

Classes and conversions

Transcription:

6.45 Lecture 5: Minimizing DFAs

6.45 Announcements: - Pset 2 is up (as of last night) - Dylan says: It s fire. - How was Pset? 2

DFAs NFAs DEFINITION Regular Languages Regular Expressions 3

4

Some Languages Are Not Regular: Limitations on DFAs/NFAs a.k.a. Lower Bounds on DFAs/NFAs 5

Minimizing DFAs 6

Does this DFA have a minimal number of states? NO 7

8

Is this minimal? How can we tell in general? 9

DFA Minimization Theorem: For every regular language A, there is a unique (up to re-labeling of the states) minimal-state DFA M* such that A = L(M*). Furthermore, there is an efficient algorithm which, given any DFA M, will output this unique M*. If such algorithms existed for more general models of computation, that would be an engineering breakthrough!!

In general, there isn t a uniquely minimal NFA

Distinguishing states with strings For a DFA M = (Q, Σ,, q, F), and q Q, let M q be the DFA equal to (Q, Σ,, q, F) Def. w Σ* distinguishes states p and q if: M p accepts w M q rejects w p M w accept OR p M w reject q w reject q w accept 2

Distinguishing states with strings For a DFA M = (Q, Σ,, q, F), and q Q, let M q be the DFA equal to (Q, Σ,, q, F) Def. w Σ* distinguishes states p and q if: M p and M q have different outputs on input w p M w accept OR p M w reject q w reject q w accept 3

Distinguishing two states Def. w Σ* distinguishes states q and q 2 M p and M q have different outputs on w iff I m in q or q 2, but which? How can I tell? 4

Distinguishing two states Def. w Σ* distinguishes states p and q iff M p and M q have different outputs on w Here read this Ok, I m accepting! Must have been p W Ok, I m rejecting! Must have been q 5

Fix M = (Q, Σ,, q, F) and let p, q Q Let M p = (Q, Σ,, p, F) and M q = (Q, Σ,, q, F) Definition(s): State p is distinguishable from state q iff there is a w Σ* that distinguishes p and q iff there is a w Σ* so that M p accepts w M q rejects w State p is indistinguishable from state q iff p is not distinguishable from q iff for all w Σ*, M p accepts w M q accepts w Big Idea: Pairs of indistinguishable states are redundant! 6

Which pairs of states are distinguishable? q, q q 2 Are q and q distinguishable? q 3 The empty string ε distinguishes all final states from all non-final states 7

q, q q 2 Are q and q 3 distinguishable? q 3 The string distinguishes q and q 3 8

q, q q 2 Are q and q 2 distinguishable? q 3 The string distinguishes q and q 2 In this DFA, all pairs of states are distinguishable! 9

Fix M = (Q, Σ,, q, F) and let p, q, r Q Define a binary relation on the states of M: p q iff p is indistinguishable from q p q iff p is distinguishable from q Proposition: is an equivalence relation p p (reflexive) p q q p (symmetric) p q and q r p r (transitive) Proof? Just look at the definition! p q means for all w, M p accepts w M q accepts w 2

Fix M = (Q, Σ,, q, F) and let p, q, r Q Therefore, the relation partitions Q into disjoint equivalence classes Proposition: is an equivalence relation Q [q] := { p p q } q q 2

22

Algorithm: MINIMIZE-DFA Input: DFA M Output: DFA M MIN such that:. L(M) = L(M MIN ) not reachable from start 2. M MIN has no inaccessible states 3. M MIN is irreducible for all states p q of M MIN, p and q are distinguishable Theorem: Every M MIN satisfying,2,3 is the unique minimal DFA equivalent to M 23

States of M MIN Intuition: = Equivalence classes of states of M We ll uncover these equivalent states with a dynamic programming algorithm 24

The Table-Filling Algorithm Input: DFA M = (Q, Σ,, q, F) Output: () D M = { (p, q) p, q Q and p q } (2) EQUIV M = { [q] q Q } Idea: We know how to find those pairs of states that the string ε distinguishes Use this and iteration to find those pairs distinguishable with longer strings The pairs of states left over will be indistinguishable 25

The Table-Filling Algorithm Input: DFA M = (Q, Σ,, q, F) Output: () D M = { (p, q) p, q Q and p q } (2) EQUIV M = { [q] q Q } q q q i q n q q q i q n 26

The Table-Filling Algorithm Input: DFA M = (Q, Σ,, q, F) Output: () D M = { (p, q) p, q Q and p q } (2) EQUIV M = { [q] q Q } q q Base Case: For all (p, q) such that p accepts and q rejects mark p q q i q n q q q i q n 27

q q q i D D q n D q q q i q n The Table-Filling Algorithm Input: DFA M = (Q, Σ,, q, F) Output: () D M = { (p, q) p, q Q and p q } (2) EQUIV M = { [q] q Q } Base Case: For all (p, q) such that p accepts and q rejects mark p q Iterate: If there are states p, q and symbol σ Σ satisfying: (p, ) = p mark p q (q, ) = q Repeat until no more D s can be added 28

q Are q and q 2 distinguishable? q q 2 q 3 D D D D D D Are q and q distinguishable? Are q and q 2 distinguishable? q q q 2 q 3, q q q 2 q 3 29

Claim: If (p, q) is marked D by the Table-Filling algorithm, then p q Proof: Induction on the number of iterations n in the algorithm when (p, q) is marked D n = : If (p, q) is marked D in the base case, then exactly one of them is final, so ε distinguishes p and q I.H. For all (p, q ) marked D in the first n iterations, p q Suppose (p, q) is marked D in (n + )th iteration. To be marked, there must be states p, q such that:. p = (p, ) and q = (q, ), for some Σ 2. (p, q ) is marked D p q (by induction) So there s a w s.t. M p and M q have different output on w Then, the string w distinguishes p and q! 3

Claim: If (p, q) is not marked D by the Table-Filling algorithm, then p q Proof (by contradiction): Suppose the pair (p, q) is not marked D by the algorithm, yet p q (call this a bad pair ) Then there is a string w such that w > and: M p and M q have different outputs on w (Why is w >?) Of all such bad pairs, let (p, q) be a pair with a minimum-length distinguishing string w 3

Claim: If (p, q) is not marked D by the Table-Filling algorithm, then p q Proof (by contradiction): Suppose the pair (p, q) is not marked D by the algorithm, yet p q (call this a bad pair ) Of all such bad pairs, let (p, q) be a pair with a minimum-length distinguishing string w M p and M q have different outputs on w We have w = w, for some string w and some Σ Let p = (p, ) and q = (q, ). Then (p, q ) is also a bad pair! (p,q ) distinguished by w But then (p, q ) has a SHORTER distinguishing string, w Contradiction! 32

Algorithm MINIMIZE Input: DFA M Output: Equivalent minimal-state DFA M MIN. Remove all inaccessible states from M 2. Run Table-Filling algorithm on M to get: EQUIV M := { [q] q is an accessible state of M } 3. Define: M MIN = (Q MIN, Σ, MIN, q MIN, F MIN ) Q MIN = EQUIV M, q MIN = [q ], F MIN = { [q] q F } MIN ( [q], ) = [ ( q, ) ] Claim: L(M MIN ) = L(M) (well-defined??) 33

The MINIMIZE Algorithm in Pictures. Remove all inaccessible states M q q 34

The MINIMIZE Algorithm in Pictures 2. Run Table-Filling to get equiv classes M [q] := { p p q } q q 35

The MINIMIZE Algorithm in Pictures 3. Define M MIN with states = equiv classes M q q States of M MIN = Equivalence classes of states of M 36

q q q q D q 3 q 2 q 2 D q 3 D D q q q 2 q 3 37

q q q 4, q, q 5 q D q 2 q 3 q 3 D D q 4 D D q 5 D D D D q q q 3 q 4 q 5 38

q q q 4, q q 5 q D q 3 q 3 D D q 4 D D q 5 D D D D q q q 3 q 4 q 5 39

Thm: M MIN is the unique minimal DFA equivalent to M Claim: Let M be any DFA where L(M )=L(M MIN ) and M has no inaccessible states and M is irreducible. Then there is an isomorphism between M and M MIN Suppose we have proved the Claim is true. Assuming the Claim we can prove the Thm: Proof of Thm: Let M be any minimal DFA for M. Since M is minimal, M has no inaccessible states and is irreducible (otherwise, we could make M smaller!) By the Claim, there is an isomorphism between M and the DFA M MIN that is output by MINIMIZE(M). That is, M MIN is isomorphic to every minimal M. 4

Thm: M MIN is the unique minimal DFA equivalent to M Claim: Let M be any DFA where L(M )=L(M MIN ) and M has no inaccessible states and M is irreducible. Then there is an isomorphism between M and M MIN Proof: We recursively construct a map from the states of M MIN to the states of M Base Case: q MIN q Recursive Step: If p p q q Then q q 4

Base Case: q MIN q Recursive Step: If p p Then q q q q 42

Base Case: q MIN q Recursive Step: If p p q Then q q Claim: Map is an isomorphism. Need to prove: The map is defined everywhere The map is well defined The map is a bijection (one-to-one and onto) The map preserves all transitions: If p p then MIN (p, ) (p, ) (this follows from the definition of the map!) q 43

Base Case: q MIN q Recursive Step: If p p The map is defined everywhere That is, for all states q of M MIN there is a state q of M such that q q If q M MIN, there is a string w such that M MIN is in state q after reading in w q Then q q q Let q be the state of M after reading in w. Claim: q q (proof by induction on w ) 44

Base Case: q MIN q Recursive Step: If p p The map is onto: q q such that q q q Then q q Want to show: For all states q of M there is a state q of M MIN such that q q For every q in M there is a string w such that M reaches state q after reading in w Let q be the state of M MIN after reading in w. Claim: q q (proof by induction on w ) q 45

Base Case: q MIN q Recursive Step: If p p The map is well defined: q! q such that q q Suppose there are states q and q such that q q and q q We show that q and q are indistinguishable, so it must be that q = q (why?) q Then q q q 46

Suppose there are states q and q such that q q and q q Assume for contradiction q and q are distinguishable M MIN M q MIN u q w Accept q u q w Accept q MIN v Contradiction! w q Reject q v q w Reject 47

Map is -to-: p q, p q and q q q q Proof by contradiction. Suppose there are states p ¹ q such that p q and q q If p q, then p and q are distinguishable M MIN M q MIN u p w Accept q u q w Accept q MIN v q w Reject q v Contradiction! w q Reject 48

How can we prove that two regular expressions are equivalent? 49