CMSC 330: Organization of Programming Languages. DFAs, and NFAs, and Regexps (Oh my!)

Similar documents
Types of Finite Automata. CMSC 330: Organization of Programming Languages. Comparing DFAs and NFAs. NFA for (a b)*abb.

CMSC 330: Organization of Programming Languages

Types of Finite Automata. CMSC 330: Organization of Programming Languages. Comparing DFAs and NFAs. Comparing DFAs and NFAs (cont.) Finite Automata 2

NFA DFA Example 3 CMSC 330: Organization of Programming Languages. Equivalence of DFAs and NFAs. Equivalence of DFAs and NFAs (cont.

Theory of Computation Regular Languages. (NTU EE) Regular Languages Fall / 38

Theory of Computation Regular Languages

Non-Deterministic Finite Automata. Fall 2018 Costas Busch - RPI 1

Finite Automata. Informatics 2A: Lecture 3. John Longley. 22 September School of Informatics University of Edinburgh

NFAs and Regular Expressions. NFA-ε, continued. Recall. Last class: Today: Fun:

Nondeterminism. Nondeterministic Finite Automata. Example: Moves on a Chessboard. Nondeterminism (2) Example: Chessboard (2) Formal NFA

AUTOMATA AND LANGUAGES. Definition 1.5: Finite Automaton

Convert the NFA into DFA

Anatomy of a Deterministic Finite Automaton. Deterministic Finite Automata. A machine so simple that you can understand it in less than one minute

Non Deterministic Automata. Linz: Nondeterministic Finite Accepters, page 51

Lexical Analysis Finite Automate

Finite Automata. Informatics 2A: Lecture 3. Mary Cryan. 21 September School of Informatics University of Edinburgh

Java II Finite Automata I

Regular Expressions (RE) Regular Expressions (RE) Regular Expressions (RE) Regular Expressions (RE) Kleene-*

Regular expressions, Finite Automata, transition graphs are all the same!!

Chapter Five: Nondeterministic Finite Automata. Formal Language, chapter 5, slide 1

Chapter 2 Finite Automata

CS 275 Automata and Formal Language Theory

Let's start with an example:

Deterministic Finite Automata

CS415 Compilers. Lexical Analysis and. These slides are based on slides copyrighted by Keith Cooper, Ken Kennedy & Linda Torczon at Rice University

Finite Automata Theory and Formal Languages TMV027/DIT321 LP4 2018

CS375: Logic and Theory of Computing

NFAs continued, Closure Properties of Regular Languages

Minimal DFA. minimal DFA for L starting from any other

Finite Automata-cont d

CS:4330 Theory of Computation Spring Regular Languages. Equivalences between Finite automata and REs. Haniel Barbosa

Non-deterministic Finite Automata

Nondeterminism and Nodeterministic Automata

Homework 3 Solutions

Converting Regular Expressions to Discrete Finite Automata: A Tutorial

Non-deterministic Finite Automata

State Minimization for DFAs

More on automata. Michael George. March 24 April 7, 2014

Non Deterministic Automata. Formal Languages and Automata - Yonsei CS 1

FABER Formal Languages, Automata and Models of Computation

1.4 Nonregular Languages

Designing finite automata II

CHAPTER 1 Regular Languages. Contents

CS 301. Lecture 04 Regular Expressions. Stephen Checkoway. January 29, 2018

5.1 Definitions and Examples 5.2 Deterministic Pushdown Automata

First Midterm Examination

GNFA GNFA GNFA GNFA GNFA

Formal Languages and Automata

Intermediate Math Circles Wednesday, November 14, 2018 Finite Automata II. Nickolas Rollick a b b. a b 4

Lecture 6 Regular Grammars

Finite-State Automata: Recap

NFAs continued, Closure Properties of Regular Languages

CSCI 340: Computational Models. Kleene s Theorem. Department of Computer Science

Formal languages, automata, and theory of computation

Non-Deterministic Finite Automata

1.3 Regular Expressions

Where did dynamic programming come from?

CS 373, Spring Solutions to Mock midterm 1 (Based on first midterm in CS 273, Fall 2008.)

Automata and Languages

Fundamentals of Computer Science

Harvard University Computer Science 121 Midterm October 23, 2012

Closure Properties of Regular Languages

Lexical Analysis Part III

CISC 4090 Theory of Computation

Assignment 1 Automata, Languages, and Computability. 1 Finite State Automata and Regular Languages

CMPSCI 250: Introduction to Computation. Lecture #31: What DFA s Can and Can t Do David Mix Barrington 9 April 2014

CHAPTER 1 Regular Languages. Contents. definitions, examples, designing, regular operations. Non-deterministic Finite Automata (NFA)

First Midterm Examination

3 Regular expressions

12.1 Nondeterminism Nondeterministic Finite Automata. a a b ε. CS125 Lecture 12 Fall 2016

Today s Topics Automata and Languages

Automata and Languages

Worked out examples Finite Automata

11.1 Finite Automata. CS125 Lecture 11 Fall Motivation: TMs without a tape: maybe we can at least fully understand such a simple model?

Turing Machines Part One

Turing Machines Part One

Lecture 08: Feb. 08, 2019

Deterministic Finite-State Automata

12.1 Nondeterminism Nondeterministic Finite Automata. a a b ε. CS125 Lecture 12 Fall 2014

1 From NFA to regular expression

Formal Language and Automata Theory (CS21004)

Lecture 09: Myhill-Nerode Theorem

This lecture covers Chapter 8 of HMU: Properties of CFLs

Speech Recognition Lecture 2: Finite Automata and Finite-State Transducers

Some Theory of Computation Exercises Week 1

CS 330 Formal Methods and Models

1 Nondeterministic Finite Automata

Finite Automata Part Three

Automata Theory 101. Introduction. Outline. Introduction Finite Automata Regular Expressions ω-automata. Ralf Huuck.

Normal Forms for Context-free Grammars

Myhill-Nerode Theorem

a,b a 1 a 2 a 3 a,b 1 a,b a,b 2 3 a,b a,b a 2 a,b CS Determinisitic Finite Automata 1

1. For each of the following theorems, give a two or three sentence sketch of how the proof goes or why it is not true.

Midterm 1 Practice. CS 350 Fall 2018 gilray.org/classes/fall2018/cs350/

FORMAL LANGUAGES, AUTOMATA AND COMPUTABILITY. FLAC (15-453) - Spring L. Blum

Homework 4. 0 ε 0. (00) ε 0 ε 0 (00) (11) CS 341: Foundations of Computer Science II Prof. Marvin Nakayama

ɛ-closure, Kleene s Theorem,

CS5371 Theory of Computation. Lecture 20: Complexity V (Polynomial-Time Reducibility)

Speech Recognition Lecture 2: Finite Automata and Finite-State Transducers. Mehryar Mohri Courant Institute and Google Research

19 Optimal behavior: Game theory

Transcription:

CMSC 330: Orgniztion of Progrmming Lnguges DFAs, nd NFAs, nd Regexps (Oh my!) CMSC330 Spring 2018

Types of Finite Automt Deterministic Finite Automt (DFA) Exctly one sequence of steps for ech string All exmples so fr Nondeterministic Finite Automt (NFA) My hve mny sequences of steps for ech string Accepts if ny pth ends in finl stte t end of string More compct thn DFA Ø But more expensive to test whether string mtches CMSC 330 Spring 2018 2

Quiz 1: Which DFA mtches this regexp? b(b +b?) A. B. C. 0 b 1 b 2 b 3 b 4 b 0 1 b b 2 3 0 b 1 02,b D. None of the bove CMSC 330 Spring 2018 3

Quiz 1: Which DFA mtches this regexp? b(b +b?) A. B. C. 0 b 1 b 2 b 3 b 4 b 0 1 b b 2 3 0 b 1 02,b D. None of the bove CMSC 330 Spring 2018 4

Compring DFAs nd NFAs NFAs cn hve more thn one trnsition leving stte on the sme symbol DFAs llow only one trnsition per symbol I.e., trnsition function must be vlid function DFA is specil cse of NFA CMSC 330 Spring 2018 5

Compring DFAs nd NFAs (cont.) NFAs my hve trnsitions with empty string lbel My move to new stte without consuming chrcter ε e-trnsition DFA trnsition must be lbeled with symbol DFA is specil cse of NFA CMSC 330 Spring 2018 6

DFA for ( b)*bb CMSC 330 Spring 2018 7

NFA for ( b)*bb b Hs pths to either S0 or S1 Neither is finl, so rejected bbbb Hs pths to different sttes One pth leds to S3, so ccepts string CMSC 330 Spring 2018 8

NFA for (b b)* b Hs pths to sttes S0, S1 bb Hs pths to S0, S1 Need to use ε-trnsition CMSC 330 Spring 2018 9

Compring NFA nd DFA for (b b)* DFA NFA CMSC 330 Spring 2018 10

NFA Acceptnce Algorithm Sketch When NFA processes string s NFA must keep trck of severl current sttes Ø Due to multiple trnsitions with sme lbel Ø ε-trnsitions If ny current stte is finl when done then ccept s Exmple After processing Ø NFA my be in sttes S1 S2 S3 S1 S2 ε S3 CMSC 330 Spring 2018 11

Forml Definition A deterministic finite utomton (DFA) is 5-tuple (Σ, Q, q 0, F, δ) where Σ is n lphbet Q is nonempty set of sttes q 0 Î Q is the strt stte F Q is the set of finl sttes δ : Q x Σ Q specifies the DFA's trnsitions Ø Wht's this definition sying tht δ is? A DFA ccepts s if it stops t finl stte on s CMSC 330 Spring 2018 12

Forml Definition: Exmple Σ = {0, 1} Q = {S0, S1} q 0 = S0 F = {S1} input stte symbol δ 0 1 S0 S0 S1 S1 S0 S1 or s { (S0,0,S0),(S0,1,S1),(S1,0,S0),(S1,1,S1) } CMSC 330 Spring 2018 13

Nondeterministic Finite Automt (NFA) An NFA is 5-tuple (Σ, Q, q 0, F, δ) where Σ, Q, q0, F s with DFAs δ Q x (Σ È {ε}) x Q specifies the NFA's trnsitions S1 S2 ε Exmple S3 Σ = {} Q = {S1, S2, S3} q 0 = S1 F = {S3} δ = { (S1,,S1), (S1,,S2), (S2,ε,S3) } An NFA ccepts s if there is t lest one pth vi s from the NFA s strt stte to finl stte CMSC 330 Spring 2018 14

Relting REs to DFAs nd NFAs Regulr expressions, NFAs, nd DFAs ccept the sme lnguges! DFA cn reduce NFA cn trnsform cn reduce RE CMSC 330 Spring 2018 15

Reducing Regulr Expressions to NFAs Gol: Given regulr expression A, construct NFA: <A> = (Σ, Q, q 0, F, δ) Remember regulr expressions re defined recursively from primitive RE lnguges Invrint: F = 1 in our NFAs Ø Recll F = set of finl sttes Will define <A> for bse cses: σ, ε, Where σ is symbol in Σ And for inductive cses: AB, A B, A* CMSC 330 Spring 2018 16

Reducing Regulr Expressions to NFAs Bse cse: σ σ <σ> = ({σ}, {S0, S1}, S0, {S1}, {(S0, σ, S1)} ) CMSC 330 Spring 2018 17

Reduction Bse cse: ε <ε> = (, {S0}, S0, {S0}, ) Bse cse: < > = (, {S0, S1}, S0, {S1}, ) CMSC 330 Spring 2018 18

Reduction: Conctention Induction: AB <A> <B> <A> = (Σ A, Q A, q A, {f A }, δ A ) <B> = (Σ B, Q B, q B, {f B }, δ B ) CMSC 330 Spring 2018 19

Reduction: Conctention Induction: AB <A> <B> <A> = (Σ A, Q A, q A, {f A }, δ A ) <B> = (Σ B, Q B, q B, {f B }, δ B ) <AB> = (Σ A È Σ B, Q A È Q B, q A, {f B }, δ A È δ B È {(f A,ε,q B )} ) CMSC 330 Spring 2018 20

Reduction: Union Induction: A B <A> = (Σ A, Q A, q A, {f A }, δ A ) <B> = (Σ B, Q B, q B, {f B }, δ B ) CMSC 330 Spring 2018 21

Reduction: Union Induction: A B <A> = (Σ A, Q A, q A, {f A }, δ A ) <B> = (Σ B, Q B, q B, {f B }, δ B ) <A B> = (Σ A È Σ B, Q A È Q B È {S0,S1}, S0, {S1}, δ A È δ B È {(S0,ε,q A ), (S0,ε,q B ), (f A,ε,S1), (f B,ε,S1)}) CMSC 330 Spring 2018 22

Reduction: Closure Induction: A* <A> = (Σ A, Q A, q A, {f A }, δ A ) CMSC 330 Spring 2018 23

Reduction: Closure Induction: A* <A> = (Σ A, Q A, q A, {f A }, δ A ) <A*> = (Σ A, Q A È {S0,S1}, S0, {S1}, δ A È {(f A,ε,S1), (S0,ε,q A ), (S0,ε,S1), (S1,ε,S0)}) CMSC 330 Spring 2018 24

Quiz 2: Which NFA mtches *? A. B. C. D. CMSC 330 Spring 2018 25

Quiz 2: Which NFA mtches *? A. B. C. D. CMSC 330 Spring 2018 26

Quiz 3: Which NFA mtches b*? A. B. C. D. CMSC 330 Spring 2018 27

Quiz 3: Which NFA mtches b*? A. B. C. D. CMSC 330 Spring 2018 28

Reduction Complexity Given regulr expression A of size n... Size = # of symbols + # of opertions How mny sttes does <A> hve? Two dded for ech, two dded for ech * O(n) Tht s pretty good! CMSC 330 Spring 2018 31

Reducing NFA to DFA DFA cn reduce NFA cn reduce RE CMSC 330 Spring 2018 33

Reducing NFA to DFA NFA my be reduced to DFA By explicitly trcking the set of NFA sttes Intuition Build DFA where Ø Ech DFA stte represents set of NFA current sttes Exmple S1 S2 ε S3 S1 S1, S2, S3 NFA DFA CMSC 330 Spring 2018 34

Algorithm for Reducing NFA to DFA Reduction pplied using the subset lgorithm DFA stte is subset of set of ll NFA sttes Algorithm Input Ø NFA (Σ, Q, q 0, F n, δ) Output Ø DFA (Σ, R, r 0, F d, d) Using two subroutines Ø ε-closure(d, p) (nd ε-closure(d, S)) Ø move(d, p, ) (nd move(d, S, )) CMSC 330 Spring 2018 35

ε-trnsitions nd ε-closure ε We sy p q If it is possible to go from stte p to stte q by tking only e-trnsitions in δ If $ p, p 1, p 2, p n, q Î Q such tht Ø {p,ε,p 1 } Î δ, {p 1,ε,p 2 } Î δ,, {p n,ε,q} Î δ ε-closure(δ, p) Set of sttes rechble from p using ε-trnsitions lone Ø Set of sttes q such tht p ε q ccording to δ Ø ε-closure(δ, p) = {q p ε q in δ } ε Ø ε-closure(δ, Q) = { q p Î Q, p q in δ } Notes Ø ε-closure(δ, p) lwys includes p Ø We write ε-closure(p) or ε-closure(q) when δ is cler from context CMSC 330 Spring 2018 36

ε-closure: Exmple 1 Following NFA contins ε S1 S2 ε S2 S3 ε S1 S3 ε ε Ø Since S1 S2 nd S2 S3 ε ε S1 S2 S3 ε-closures ε-closure(s1) = ε-closure(s2) = ε-closure(s3) = { S3 } ε-closure( { S1, S2 } ) = { S1, S2, S3 } { S2, S3 } { S1, S2, S3 } È { S2, S3 } CMSC 330 Spring 2018 37

ε-closure: Exmple 2 Following NFA contins ε S1 S3 ε S3 S2 ε S1 S2 ε ε Ø Since S1 S3 nd S3 S2 b S1 S2 S3 ε ε ε-closures ε-closure(s1) = ε-closure(s2) = ε-closure(s3) = { S1, S2, S3 } { S2 } { S2, S3 } ε-closure( { S2,S3 } ) = { S2 } È { S2, S3 } CMSC 330 Spring 2018 38

ε-closure Algorithm: Approch Input: NFA (Σ, Q, q 0, F n, δ), Stte Set R Output: Stte Set R Algorithm Let R = R Repet Let R = R Let R = R È {q p Î R, (p, e, q) Î d} Until R = R // strt sttes // continue from previous // new ε-rechble sttes // stop when no new sttes This lgorithm computes fixed point see note linked from project description CMSC 330 Spring 2018 39

ε-closure Algorithm Exmple Clculte ε-closure(d,{s1}) R R ε ε S1 S2 S3 {S1} {S1} {S1} {S1, S2} {S1, S2} {S1, S2, S3} Let R = R Repet Let R= R Let R = R È {q p Î R, (p, e, q) Î d} Until R = R {S1, S2, S3} {S1, S2, S3} CMSC 330 Spring 2018 40

Clculting move(p,) move(δ,p,) Set of sttes rechble from p using exctly one trnsition on Ø Set of sttes q such tht {p,, q} Î δ Ø move(δ,p,) = { q {p,, q} Î δ } Ø move(δ,q,) = { q p Î Q, {p,, q} Î δ } i.e., cn lift move() to strt from set of sttes Q Notes: Ø move(δ,p,) is Ø if no trnsition (p,,q) Î δ, for ny q Ø We write move(p,) or move(r,) when δ cler from context CMSC 330 Spring 2018 41

move(,p) : Exmple 1 Following NFA Σ = {, b } Move move(s1, ) = move(s1, b) = move(s2, ) = move(s2, b) = move(s3, ) = move(s3, b) = { S2, S3 } Ø Ø { S3 } Ø Ø b S1 S2 S3 move({s1,s2},b) = { S3 } CMSC 330 Spring 2018 42

move(,p) : Exmple 2 Following NFA Σ = {, b } Move move(s1, ) = move(s1, b) = move(s2, ) = move(s2, b) = move(s3, ) = move(s3, b) = { S2 } { S3 } { S3 } Ø Ø Ø S1 S2 S3 b move({s1,s2},) = {S2,S3} ε CMSC 330 Spring 2018 43

NFA DFA Reduction Algorithm ( subset ) Input NFA (Σ, Q, q 0, F n, δ), Output DFA (Σ, R, r 0, F d, d ) Algorithm Let r 0 = e-closure(δ,q 0 ), dd it to R While $ n unmrked stte r Î R Mrk r For ech Î S Let E = move(δ,r,) Let e = e-closure(δ,e) If e Ï R Let R = R È {e} Let d = d È {r,, e} Let F d = {r $ s Î r with s Î F n } // DFA strt stte // process DFA stte r // ech stte visited once // for ech letter // sttes reched vi // sttes reched vi e // if stte e is new // dd e to R (unmrked) // dd trnsition r e // finl if include stte in F n CMSC 330 Spring 2018 44

NFA DFA Exmple 1 Strt = e-closure(δ,s1) = { {S1,S3} } R = { {S1,S3} } r Î R = {S1,S3} move(δ,{s1,s3},) = {S2} Ø e = e-closure(δ,{s2}) = {S2} Ø R = R È {{S2}} = { {S1,S3}, {S2} } Ø d = d È {{S1,S3},, {S2}} move(δ,{s1,s3},b) = Ø b S1 S2 S3 {1,3} NFA DFA {2} ε CMSC 330 Spring 2018 45

NFA DFA Exmple 1 (cont.) R = { {S1,S3}, {S2} } r Î R = {S2} move(δ,{s2},) = Ø move(δ,{s2},b) = {S3} Ø e = e-closure(δ,{s3}) = {S3} Ø R = R È {{S3}} = { {S1,S3}, {S2}, {S3} } Ø d = d È {{S2}, b, {S3}} b S1 S2 S3 {1,3} NFA DFA {2} ε b {3} CMSC 330 Spring 2018 46

NFA DFA Exmple 1 (cont.) R = { {S1,S3}, {S2}, {S3} } r Î R = {S3} Move({S3},) = Ø Move({S3},b) = Ø Mrk {S3}, exit loop F d = {{S1,S3}, {S3}} Ø Since S3 Î F n Done! b S1 S2 S3 {1,3} NFA DFA {2} ε b {3} CMSC 330 Spring 2018 47

Quiz 4: Which DFA is equiv to this NFA? NFA: b S0 S1 S2 ε A. S0 S1 b S1, S2 b B. C. S0 S1 S2, S0 S0 S1 b S2, S0 D. None of the bove CMSC 330 Spring 2018 49 b

Quiz 4: Which DFA is equiv to this NFA? NFA: b S0 S1 S2 ε A. S0 S1 b S1, S2 b B. C. S0 S1 S2, S0 S0 S1 b S2, S0 D. None of the bove CMSC 330 Spring 2018 50 b

Actul Answer b S0 S1 S2 NFA: ε S0 S1 b S2, S0 b S1, S0 CMSC 330 Spring 2018 51

Subset Algorithm s Fixed Point Input: NFA (Σ, Q, q 0, F, δ) Output: DFA M Algorithm Let q 0 = ε-closure(δ, q 0 ) Let F = {q 0 } if q 0 F, or otherwise Let M = (Σ, {q 0 }, q 0, F, ) DFA // strting pproximtion of Repet Let M = M // current DFA pprox For ech q Î sttes(m), Î Σ // for ech DFA stte q nd letter Let s = ε-closure(δ, move(δ, q, )) // new subset from q Let F = {s} if s F, or otherwise, // subset contins finl? M = M È (, {s},, F, {(q,, s)}) // updte DFA Until M = M // reched fixed point CMSC 330 Spring 2018 57

Redux: DFA to NFA Exmple 1 q ' ε-closure(δ,s1) = {S1,S3} F = {{S1,S3}} since {S1,S3} {S3} NFA b S1 S2 S3 {1,3} M = { Σ, {{S1,S3}}, {S1,S3}, {{S1,S3}}, } Q q 0 ' F δ' DFA ε CMSC 330 Spring 2018 58

Redux: DFA to NFA Exmple 1 (cont) M = { Σ, {{S1,S3}}, {S1,S3}, {{S1,S3}}, } q = {S1, S3} = s = {S2} Ø since move(δ,{s1, S3},) = {S2} Ø nd e-closure(δ,{s2}) = {S2} F = Ø Ø Since {S2} {S3} = where s = {S2} nd F = {S3} b S1 S2 S3 {1,3} CMSC 330 Spring 2018 59 NFA DFA M = M È ( { S2}},, { S1,S3},,{S2 } { Σ, {{S1,S3},{S2}}, {S1,S3}, {{S1,S3}}, {({S1,S3},,{S2})} } {2} Q q 0 ' F δ' ε

Redux: DFA to NFA Exmple 1 (cont) M = { Σ, {{S1,S3},{S2}}, {S1,S3}, {{S1,S3}}, {({S1,S3},,{S2})} } q = {S2} = b s = {S3} Ø since move(δ,{s2},b) = {S3} Ø nd e-closure(δ,{s3}) = {S3} F = {{S3}} Ø Ø Since {S3} {S3} = {S3} where s = {S3} nd F = {S3} {1,3} M = M È { S3 {{S3}} { S2 b S3 } NFA b S1 S2 S3 DFA { Σ, {{S1,S3},{S2},{S3}}, {S1,S3}, {{S1,S3},{S3}}, {({S1,S3},,{S2}), ({S2},b,{S3})} } Q q 0 ' F δ' CMSC 330 Spring 2018 60 {2} ε b {3}

Anlyzing the Reduction Cn reduce ny NFA to DFA using subset lg. How mny sttes in the DFA? Ech DFA stte is subset of the set of NFA sttes Given NFA with n sttes, DFA my hve 2 n sttes Ø Since set with n items my hve 2 n subsets Corollry Ø Reducing NFA with n sttes my be O(2 n ) CMSC 330 Spring 2018 61

Reducing DFA to RE DFA cn reduce NFA cn trnsform cn trnsform RE CMSC 330 Spring 2018 62

Reducing DFAs to REs Generl ide Remove sttes one by one, lbeling trnsitions with regulr expressions When two sttes re left (strt nd finl), the trnsition lbel is the regulr expression for the DFA CMSC 330 Spring 2018 63

Other Topics Minimizing DFA Hopcroft reduction Complementing DFA Implementing DFA CMSC 330 Spring 2018 66

Minimizing DFAs Every regulr lnguge is recognizble by unique minimum-stte DFA Ignoring the prticulr nmes of sttes In other words For every DFA, there is unique DFA with minimum number of sttes tht ccepts the sme lnguge b S1 S2 S3 c CMSC 330 Spring 2018 67 S1 c S3 S2 b

J. Hopcroft, An n log n lgorithm for minimizing sttes in finite utomton, 1971 Minimizing DFA: Hopcroft Reduction Intuition Look to distinguish sttes from ech other Ø End up in different ccept / non-ccept stte with identicl input Algorithm Construct initil prtition Ø Accepting & non-ccepting sttes Itertively split prtitions (until prtitions remin fixed) Ø Split prtition if members in prtition hve trnsitions to different prtitions for sme input Two sttes x, y belong in sme prtition if nd only if for ll symbols in Σ they trnsition to the sme prtition Updte trnsitions & remove ded sttes CMSC 330 Spring 2018 68

Splitting Prtitions No need to split prtition {S,T,U,V} All trnsitions on led to identicl prtition P2 Even though trnsitions on led to different sttes S P1 X P2 T Y U V Z CMSC 330 Spring 2018 69

Splitting Prtitions (cont.) Need to split prtition {S,T,U} into {S,T}, {U} Trnsitions on from S,T led to prtition P2 Trnsition on from U led to prtition P3 S P1 X P2 P4 U T Z Y b P3 CMSC 330 Spring 2018 70

Resplitting Prtitions Need to reexmine prtitions fter splits Initilly no need to split prtition {S,T,U} After splitting prtition {X,Y} into {X}, {Y} we need to split prtition {S,T,U} into {S,T}, {U} P4 S U T P1 CMSC 330 Spring 2018 71 b P3 X b P2 Y

Minimizing DFA: Exmple 1 DFA b S T R Initil prtitions b Split prtition CMSC 330 Spring 2018 72

Minimizing DFA: Exmple 1 DFA Initil prtitions Accept { R } = P1 b P2 P1 Reject { S, T } = P2 Split prtition? Not required, minimiztion done move(s,) move(t,) = T P2 = T P2 b S T R b move(s,b) move (T,b) = R P1 = R P1 CMSC 330 Spring 2018 73

Minimizing DFA: Exmple 2 b S T R b CMSC 330 Spring 2018 74

Minimizing DFA: Exmple 2 DFA P2 b S T R P1 Initil prtitions Accept Reject { R } { S, T } = P1 = P2 Split prtition? Yes, different prtitions for B move(s,) move(t,) = T P2 = T P2 b P3 move(s,b) move (T,b) DFA lredy miniml = T P2 = R P1 CMSC 330 Spring 2018 75

Complement of DFA Given DFA ccepting lnguge L How cn we crete DFA ccepting its complement? Exmple DFA Ø Σ = {,b} CMSC 330 Spring 2018 78

Complement of DFA Algorithm Add explicit trnsitions to ded stte Chnge every ccepting stte to non-ccepting stte & every non-ccepting stte to n ccepting stte Note this only works with DFAs Why not with NFAs? CMSC 330 Spring 2018 79

Implementing DFAs (one-off) It's esy to build progrm which mimics DFA cur_stte = 0; while (1) { symbol = getchr(); switch (cur_stte) { cse 0: switch (symbol) { cse '0': cur_stte = 0; brek; cse '1': cur_stte = 1; brek; cse '\n': printf("rejected\n"); return 0; defult: printf("rejected\n"); return 0; } brek; } } cse 1: switch (symbol) { cse '0': cur_stte = 0; brek; cse '1': cur_stte = 1; brek; cse '\n': printf("ccepted\n"); return 1; defult: printf("rejected\n"); return 0; } brek; defult: printf("unknown stte; I'm confused\n"); brek; CMSC 330 Spring 2018 80

Implementing DFAs (generic) More generlly, use generic tble-driven DFA given components (Σ, Q, q 0, F, d) of DFA: let q = q 0 while (there exists nother symbol s of the input string) q := d(q, s); if q Î F then ccept else reject q is just n integer Represent d using rrys or hsh tbles Represent F s set CMSC 330 Spring 2018 81

Running Time of DFA How long for DFA to decide to ccept/reject string s? Assume we cn compute d(q, c) in constnt time Then time to process s is O( s ) Ø Cn t get much fster! Constructing DFA for RE A my tke O(2 A ) time But usully not the cse in prctice So there s the initil overhed But then processing strings is fst CMSC 330 Spring 2018 82

Summry of Regulr Expression Theory Finite utomt DFA, NFA Equivlence of RE, NFA, DFA RE NFA Ø Conctention, union, closure NFA DFA Ø e-closure & subset lgorithm DFA Minimiztion, complement Implementtion CMSC 330 Spring 2018 84