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

Similar documents
Finite Automata Theory and Formal Languages TMV027/DIT321 LP4 2018

Finite Automata Theory and Formal Languages TMV027/DIT321 LP4 2018

Finite Automata and Formal Languages TMV026/DIT321 LP4 2012

Finite Automata Theory and Formal Languages TMV027/DIT321 LP4 2018

Finite Automata and Formal Languages

COMP4141 Theory of Computation

Subset construction. We have defined for a DFA L(A) = {x Σ ˆδ(q 0, x) F } and for A NFA. For any NFA A we can build a DFA A D such that L(A) = L(A D )

Finite Automata Theory and Formal Languages TMV027/DIT321 LP4 2018

Finite Automata and Formal Languages TMV026/DIT321 LP Useful, Useless, Generating and Reachable Symbols

Automata and Formal Languages - CM0081 Non-Deterministic Finite Automata

Lecture 1: Finite State Automaton

FORMAL LANGUAGES, AUTOMATA AND COMPUTABILITY

Finite Automata and Regular Languages

Extended transition function of a DFA

Finite Automata Theory and Formal Languages TMV027/DIT321 LP Recap: Logic, Sets, Relations, Functions

Finite Automata. Theorems - Unit I SUSAN ELIAS. Professor Department of Computer Science & Engineering Sri Venkateswara College of Engineering

Finite Automata Theory and Formal Languages TMV027/DIT321 LP4 2017

Constructions on Finite Automata

Constructions on Finite Automata

cse303 ELEMENTS OF THE THEORY OF COMPUTATION Professor Anita Wasilewska

Automata and Languages

FORMAL LANGUAGES, AUTOMATA AND COMPUTABILITY

Lecture 5: Minimizing DFAs

CS 154 Formal Languages and Computability Assignment #2 Solutions

1.4 Equivalence Relations and Partitions

Introduction to Formal Languages, Automata and Computability p.1/51

Nondeterministic Finite Automata. Nondeterminism Subset Construction

Finite Automata Theory and Formal Languages TMV027/DIT321 LP4 2018

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

3515ICT: Theory of Computation. Regular languages

Formal Language and Automata Theory (CS21004)

CS154. Non-Regular Languages, Minimizing DFAs

14 Equivalence Relations

Chap. 1.2 NonDeterministic Finite Automata (NFA)

Computational Models - Lecture 4

Equivalence of DFAs and NFAs

Lecture 3: Nondeterministic Finite Automata

What we have done so far

Deterministic finite Automata

Finite Automata Theory and Formal Languages TMV026/TMV027/DIT321 Responsible: Ana Bove

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

Computational Models - Lecture 5 1

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

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

Computational Models: Class 3

CS 322 D: Formal languages and automata theory

CSE 135: Introduction to Theory of Computation Optimal DFA

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

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

UNIT-II. NONDETERMINISTIC FINITE AUTOMATA WITH ε TRANSITIONS: SIGNIFICANCE. Use of ε-transitions. s t a r t. ε r. e g u l a r

Uses of finite automata

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

Introduction to the Theory of Computation. Automata 1VO + 1PS. Lecturer: Dr. Ana Sokolova.

Chapter 6: NFA Applications

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

Notes on Pumping Lemma

UNIT-III REGULAR LANGUAGES

MTH401A Theory of Computation. Lecture 17

CS 208: Automata Theory and Logic

Non-context-Free Languages. CS215, Lecture 5 c

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

Closure under the Regular Operations

Introduction to the Theory of Computation. Automata 1VO + 1PS. Lecturer: Dr. Ana Sokolova.

Lecture 8: Equivalence Relations

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

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

CS 133 : Automata Theory and Computability

Advanced Automata Theory 2 Finite Automata

Computational Models - Lecture 3

FORMAL LANGUAGES, AUTOMATA AND COMPUTABILITY

Pushdown automata. Twan van Laarhoven. Institute for Computing and Information Sciences Intelligent Systems Radboud University Nijmegen

More on Regular Languages and Non-Regular Languages

Unit 6. Non Regular Languages The Pumping Lemma. Reading: Sipser, chapter 1

Theory of Computation 4 Non-Deterministic Finite Automata

Chapter Five: Nondeterministic Finite Automata

Nondeterministic Finite Automata

Formal Models in NLP

Parsing Regular Expressions and Regular Grammars

Minimization of DFAs

Theory of Computation 3 Deterministic Finite Automata

Computational Models - Lecture 3 1

NPDA, CFG equivalence

Computational Models Lecture 2 1

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

Automata Theory for Presburger Arithmetic Logic

Nondeterministic Finite Automata

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

Relations. Relations of Sets N-ary Relations Relational Databases Binary Relation Properties Equivalence Relations. Reading (Epp s textbook)

Lecture 17: Language Recognition

Automata Theory. Lecture on Discussion Course of CS120. Runzhe SJTU ACM CLASS

Formal Languages, Automata and Models of Computation

Computational Models - Lecture 1 1

CS 455/555: Finite automata

Properties of Regular Languages (2015/10/15)

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

Finite Automata Theory and Formal Languages TMV027/DIT321 LP4 2018

CSC Discrete Math I, Spring Relations

FORMAL LANGUAGES, AUTOMATA AND COMPUTABILITY

Lecture 4 Nondeterministic Finite Accepters

Nondeterministic Finite Automata

Transcription:

Finite Automata and Formal Languages TMV026/DIT321 LP4 2012 Lecture 10 Ana Bove April 23rd 2012 Overview of today s lecture: Equivalence of Regular Languages Minimisation of Automata Testing Equivalence of Regular Languages There is no simple algorithm for testing this. We have seen how one can prove that 2 RE are equal, hence the languages they represent are equivalent, but this is not an easy process. We will see now how to test when 2 DFA describe the same language. April 23rd 2012, Lecture 10 TMV026/DIT321 1/20

Testing Equivalence of States in DFA We shall first answer the question: do states p and q behave in the same way? Definition: We say that states p and q are equivalent if for all w, ˆδ(p, w) is an accepting state iff ˆδ(q, w) is an accepting state. Note: We do not require that ˆδ(p, w) = ˆδ(q, w). Definition: If p and q are not equivalent, then they are distinguishable. That is, there exists at least one w such that one of ˆδ(p, w) and ˆδ(q, w) is an accepting state and the other is not. April 23rd 2012, Lecture 10 TMV026/DIT321 2/20 Table-Filling Algorithm This algorithm finds pairs of states that are distinguishable. Then, any pair of states that we do not find distinguishable are equivalent. Let D = (Q, Σ, δ, q 0, F ) be a DFA. The algorithm is as follows: Base case: If p is an accepting state and q is not, the (p, q) are distinguishable. Inductive step: Let p and q be states such that for some symbol a, δ(p, a) = r and δ(q, a) = s with the pair (r, s) known to be distinguishable. Then (p, q) are also distinguishable. (If w distinguishes r and s then aw must distinguish p and q since ˆδ(p, aw) = ˆδ(r, w) and ˆδ(q, aw) = ˆδ(s, w).) April 23rd 2012, Lecture 10 TMV026/DIT321 3/20

Example: Table-Filling Algorithm For the following DFA, we fill the table with an X at distinguishable pairs. a b q 0 q 1 q 2 q 1 q 3 q 4 q 2 q 4 q 3 q 3 q 5 q 5 q 4 q 5 q 5 q 5 q 5 q 5 q 0 q 1 q 2 q 3 q 4 q 5 X X X X X q 4 X X X q 3 X X X q 2 X X q 1 Let us consider the base case of the algorithm. Let us consider the pair (q 0, q 4 ). Let us consider the pair (q 0, q 3 ). Finally, let us consider the pairs (q 3, q 4 ) and (q 1, q 2 ). April 23rd 2012, Lecture 10 TMV026/DIT321 4/20 Example: Table-Filling Algorithm For the following DFA, we fill to table with an X at distinguishable pairs. a q 0 q 1 q 1 q 2 q 2 q 3 q 3 q 4 q 4 q 5 q 5 q 0 q 0 q 1 q 2 q 3 q 4 q 5 X X X X q 4 X X X q 3 X X q 2 X X q 1 X Let us consider the base case of the algorithm. Let us consider the pair (q 0, q 5 ). Let us consider the pair (q 0, q 2 ). We go on with the remaining pairs. April 23rd 2012, Lecture 10 TMV026/DIT321 5/20

Equivalent States Theorem: Let D = (Q, Σ, δ, q 0, F ) be a DFA. If 2 states are not distinguishable by the table-filling algorithm then the states are equivalent. Proof: Let us assume there is a bad pair (p, q) such that p and q are distinguishable but the table-filling algorithm doesn t find them so. If there are bad pairs, let (p, q ) be a bad pair with the shortest string w = a 1 a 2... a n that distinguishes 2 states. Note w is not ǫ otherwise (p, q ) is found distinguishable in the base step. Let δ(p, a 1 ) = r and δ(q, a 1 ) = s. States r and s are distinguished by a 2... a n since this string takes r to ˆδ(p, w) and s to ˆδ(q, w). Now string a 2... a n distinguishes 2 states and is shorter than w which is the shortest string that distinguishes a bad pair. Then (r, s) cannot be a bad pair and hence it must be found distinguishable by the algorithm. Then the inductive part should have found (p, q ) distinguishable. This contradict the assumption that bad pairs exist. April 23rd 2012, Lecture 10 TMV026/DIT321 6/20 Testing Equivalence of Regular Languages We can use the table-filling algorithm to test equivalence of regular languages. Let L and M be 2 regular languages. Let D L = (Q L, Σ, δ L, q L, F L ) and D M = (Q M, Σ, δ M, q M, F M ) be their corresponding DFA. Let us assume Q L Q M = (easy to obtain by renaming). We proceed now as follows. Construct D = (Q L Q M, Σ, δ,, F L F M ). (The initial state is irrelevant.) δ is the union of δ L and δ M as a function. One should now check if the pair (q L, q M ) is equivalent. If so, a string is accepted by D L iff it is accepted by D M. Hence L and M are equivalent languages. April 23rd 2012, Lecture 10 TMV026/DIT321 7/20

Recap from Relations, Partitions and Equivalent Classes Definition: A relation R S S is an equivalence relation iff R is Reflexive: a S, ara Symmetric: a, b S, arb bra Transitive: a, b, c S, arb brc arc Definition: A set P is a partition over the set S if: Every element of P is a non-empty subset of S Elements of P are pairwise disjoint The union of the elements of P is equal to S Definition: The equivalent class of a in S is [a] = {b S arb}. Lemma: a, b S, [a] = [b] iff arb. Theorem: The set of all equivalence classes in S w.r.t R form a partition. This partition is called the quotient and is denoted as S/R. April 23rd 2012, Lecture 10 TMV026/DIT321 8/20 Equivalence of States: An Equivalence Relation The relation state p is equivalent to state q, which we shall denote p q, is an equivalence relation. (Prove it as an exercise!) Reflexive: every state p is equivalent to itself p, p p Symmetric: for any states p and q, if p is equivalent to q then q is equivalent to p p q, p q q p Transitive: for any states p, q and r, if p is equivalent to q and q is equivalent to r then p is equivalent to r. p q r, p q q r p r (See Theorem 4.23 for a proof of the transitivity part.) April 23rd 2012, Lecture 10 TMV026/DIT321 9/20

Partition of States Let D = (Q, Σ, δ, q 0, F ) be a DFA. The table-filling algo. defines the equivalence of states relation over Q. Since this is an equivalence relation we can define the quotient Q/. This quotient gives us a partition of the states into classes/blocks of mutually equivalent states. Example: The partition for the example in slide 4 is the following (note the singleton classes!) {q 0 } {q 1, q 2 } {q 3, q 4 } {q 5 } Example: The partition for the example in slide 5 is the following {q 0, q 3 } {q 1, q 4 } {q 2, q 5 } Note: Classes might also have more than 2 elements. April 23rd 2012, Lecture 10 TMV026/DIT321 10/20 Minimisation of DFA Let D = (Q, Σ, δ, q 0, F ) be a DFA. Q/ allows to build an equivalent DFA with the minimum nr. of states. In addition, this minimum DFA is unique (modulo the name of the states). The algorithm for building the minimum DFA D = (Q, Σ, δ, q 0, F ) is: 1 Eliminate any non accessible state 2 Partition the remaining states with the help of the table-filling algorithm 3 Use each block as a single state in the new DFA 4 The start state is the block containing q 0, the final states are all those blocks containing elements in F 5 δ (S, a) = T if given any q S, δ(q, a) = p for some p T (Actually, the partition guarantees that q S, p T, δ(q, a) = p) April 23rd 2012, Lecture 10 TMV026/DIT321 11/20

Examples Example: The minimal DFA corresponding to the DFA in slide 4 is a, b a, b a, b a, b q 0 q 1 q 2 q 3 q 4 q 5 Example: The minimal DFA corresponding to the DFA in slide 5 is a a a q 0 q 3 q 1 q 4 q 2 q 5 April 23rd 2012, Lecture 10 TMV026/DIT321 12/20 Does the Minimisation Algorithm Give a Minimal DFA? Given a DFA D, the minimisation algorithm gives us a DFA D with the minimal number of states with respect to those of D. Can you see why? But, could there exist a DFA A completely unrelated to D, also accepting the same language and with less states than those in D? Section 4.4.4 in the book shows by contradiction that A cannot exist. Theorem: If D is a DFA and D the DFA constructed from D with the minimisation algorithm described before, then D has as few states as any DFA equivalent to D. April 23rd 2012, Lecture 10 TMV026/DIT321 13/20

Can we Minimise a NFA? One can of course find (in some cases) a smaller NFA, but the algorithm we presented before does not do the job. 0, 1 Example: Consider the following NFA q 0 0 q 1 1 0 q 2 The table-filling algorithm does not find equivalent states in this case. However, the following is a smaller and equivalent NFA for the language. 0, 1 q 0 0 q 1 April 23rd 2012, Lecture 10 TMV026/DIT321 14/20 Functional Representation of the Minimisation Algorithm OBS: we only construct the equivalent classes but not the new DFA! data Q =... deriving (Eq, Show) data S =... delta :: Q -> S -> Q final :: Q -> Bool -- lists with all states and all the symbols states :: [Q] states = [...] alphabet :: [S] alphabet = [...] April 23rd 2012, Lecture 10 TMV026/DIT321 15/20

Func. Representation of the Minimisation Alg. (Cont.) -- swaps elements in a pair swap :: (a,a) -> (a,a) swap (p,q) = (q,p) -- equality in pairs: order doesn t matter here pair_eq :: Eq a => (a,a) -> (a,a) -> Bool pair_eq p q = p == q p == swap q -- remove "repetition" from the list clean :: Eq a => [(a,a)] -> [(a,a)] clean = nubby pair_eq -- maps a function to the elements of the pair map_pair :: (a -> b -> c) -> (a,a) -> b -> (c,c) map_pair f (p,q) x = (f p x, f q x) April 23rd 2012, Lecture 10 TMV026/DIT321 16/20 Func. Representation of the Minimisation Alg. (Cont.) -- all possible pairs of states without repetition all_pairs :: [(Q,Q)] all_pairs = [ (p,q) p <- states, q <- states, p /= q] -- tests to distinguish if a state is final but not the other dist :: (Q,Q) -> Bool dist (p,q) = final p && not(final q) final q && not(final p) --splits a list according to whether the elem. satisfies dist splitby :: [(Q,Q)] -> ([(Q,Q)],[(Q,Q)]) splitby [] = ([],[]) splitby (p:pps) = let (qs,ds) = splitby pps in if dist p then (qs,p:ds) else (p:qs,ds) April 23rd 2012, Lecture 10 TMV026/DIT321 17/20

Func. Representation of the Minimisation Alg. (Cont.) -- equiv ss pps dds gives the equivalent states -- ss: all the symbols in the alphabet -- pps: the pairs of states still to check if distinguishable -- dds: all pairs of states already found distinguishable equiv :: [S] -> [(Q,Q)] -> [(Q,Q)] -> [(Q,Q)] equiv ss pps dds = let dqs = [ pq pq <- pps, or (map (\pp -> elem pp dds) (map (map_pair delta pq) ss))] nds = union dds dqs nps = pps \\ dqs in if not (null dqs) then equiv ss nps nds else clean pps -- if we return (clean nds) we give all distinguishable pairs April 23rd 2012, Lecture 10 TMV026/DIT321 18/20 Func. Representation of the Minimisation Alg. (Cont.) -- group the pairs into classes group_classes :: Eq a => [(a,a)] -> [[a]] group_classes [] = [] group_classes ((p,q):pps) = let pqs = (p,q):[ pr pr <- pps, (fst pr == p fst pr == q snd pr == p snd pr == q)] nps = pps \\ pqs qs = nub ([fst p p <- pqs] ++ [snd p p <- pqs]) in qs : group_classes nps April 23rd 2012, Lecture 10 TMV026/DIT321 19/20

Func. Representation of the Minimisation Alg. (Cont.) -- add the classes with just one state add_singel :: Eq a => [a] -> [[a]] -> [[a]] add_singel [] pps = pps add_singel (q:qs) pps or (map (elem q) pps) = add_singel qs pps add_singel (q:qs) pps = [q] : add_singel qs pps -- gives the base case of the test-filling algo. and the rest (rest,base_dist) = splitby all_pairs -- returns all equivalent classes equiv_classes :: [[Q]] equiv_classes = add_singel states (group_classes (equiv alphabet rest base_dist)) April 23rd 2012, Lecture 10 TMV026/DIT321 20/20