NFA to DFA conversion and regular expressions

Slides:



Advertisements
Similar presentations
CSC 3130: Automata theory and formal languages Andrej Bogdanov The Chinese University of Hong Kong Properties.
Advertisements

CSC 361NFA vs. DFA1. CSC 361NFA vs. DFA2 NFAs vs. DFAs NFAs can be constructed from DFAs using transitions: Called NFA- Suppose M 1 accepts L 1, M 2 accepts.
Nondeterministic Finite Automata CS 130: Theory of Computation HMU textbook, Chapter 2 (Sec 2.3 & 2.5)
1 Introduction to Computability Theory Lecture3: Regular Expressions Prof. Amos Israeli.
1 Introduction to Computability Theory Lecture4: Regular Expressions Prof. Amos Israeli.
1 Introduction to Computability Theory Lecture3: Regular Expressions Prof. Amos Israeli.
1 Regular Expressions. 2 Regular expressions describe regular languages Example: describes the language.
CSC 3130: Automata theory and formal languages Andrej Bogdanov The Chinese University of Hong Kong Regular.
1 Single Final State for NFAs and DFAs. 2 Observation Any Finite Automaton (NFA or DFA) can be converted to an equivalent NFA with a single final state.
CSC 3130: Automata theory and formal languages Andrej Bogdanov The Chinese University of Hong Kong Nondeterminism.
CSC 3130: Automata theory and formal languages Andrej Bogdanov The Chinese University of Hong Kong Limitations.
Fall 2004COMP 3351 Another NFA Example. Fall 2004COMP 3352 Language accepted (redundant state)
FORMAL LANGUAGES, AUTOMATA AND COMPUTABILITY
1 A Single Final State for Finite Accepters. 2 Observation Any Finite Accepter (NFA or DFA) can be converted to an equivalent NFA with a single final.
CSC 3130: Automata theory and formal languages Andrej Bogdanov The Chinese University of Hong Kong DFA to regular.
Theory of Computation, Feodor F. Dragan, Kent State University 1 Regular expressions: definition An algebraic equivalent to finite automata. We can build.
CSC 3130: Automata theory and formal languages Andrej Bogdanov The Chinese University of Hong Kong Closure.
CSC 3130: Automata theory and formal languages Andrej Bogdanov The Chinese University of Hong Kong NFA to DFA.
CSCI 3130: Automata theory and formal languages Andrej Bogdanov The Chinese University of Hong Kong Text search.
CHAPTER 1 Regular Languages
INHERENT LIMITATIONS OF COMPUTER PROGAMS CSci 4011.
CSCI 3130: Automata theory and formal languages Andrej Bogdanov The Chinese University of Hong Kong NFA to.
CSC 3130: Automata theory and formal languages Andrej Bogdanov The Chinese University of Hong Kong DFA minimization.
UNIT - I Formal Language and Regular Expressions: Languages Definition regular expressions Regular sets identity rules. Finite Automata: DFA NFA NFA with.
CSCI 3130: Automata theory and formal languages Andrej Bogdanov The Chinese University of Hong Kong Pushdown.
CSC 3130: Automata theory and formal languages Andrej Bogdanov The Chinese University of Hong Kong Pushdown.
INHERENT LIMITATIONS OF COMPUTER PROGRAMS CSci 4011.
 2004 SDU Lecture4 Regular Expressions.  2004 SDU 2 Regular expressions A third way to view regular languages. Say that R is a regular expression if.
1 Some Properties of Regular Languages. 2 Properties Concatenation:Star: Union: Are regular Languages For regular languages and.
Complexity and Computability Theory I Lecture #5 Rina Zviel-Girshin Leah Epstein Winter
CSCI 3130: Formal languages and automata theory Andrej Bogdanov The Chinese University of Hong Kong Decidable.
1/29/02CSE460 - MSU1 Nondeterminism-NFA Section 4.1 of Martin Textbook CSE460 – Computability & Formal Language Theory Comp. Science & Engineering Michigan.
Theory of Computation Automata Theory Dr. Ayman Srour.
Nondeterminism The Chinese University of Hong Kong Fall 2011
CIS Automata and Formal Languages – Pei Wang
FORMAL LANGUAGES AND AUTOMATA THEORY
CSE 105 theory of computation
CSE 105 theory of computation
Chapter 2 Finite Automata
Regular Expressions Sections:1.3 page 63 September 17, 2008
Single Final State for NFA
Jaya Krishna, M.Tech, Assistant Professor
Decision Properties of Regular Languages
Pushdown automata and CFG ↔ PDA conversions
LR(1) grammars The Chinese University of Hong Kong Fall 2010
More on DFA minimization and DFA equivalence
Closure Properties for Regular Languages
Non-Deterministic Finite Automata
CS 154, Lecture 3: DFANFA, Regular Expressions.
Nondeterministic Finite Automata
NFA vs DFA DFA: For every state q in S and every character  in , one and only one transition of the following form occurs:  q q’ NFA: For every state.
NFAs, DFAs, and regular expressions
FORMAL LANGUAGES, AUTOMATA, AND COMPUTABILITY
CSCI 2670 Introduction to Theory of Computing
Decidable and undecidable languages
Chapter 4 Properties of Regular Languages
CS21 Decidability and Tractability
Chapter 1 Regular Language - 02
CS21 Decidability and Tractability
LR(1) grammars The Chinese University of Hong Kong Fall 2011
CSCI 2670 Introduction to Theory of Computing
Chapter 1 Regular Language
Pushdown automata The Chinese University of Hong Kong Fall 2011
Text search and closure properties
CSCI 2670 Introduction to Theory of Computing
CHAPTER 1 Regular Languages
CSCI 2670 Introduction to Theory of Computing
Nondeterminism The Chinese University of Hong Kong Fall 2010
CSCI 2670 Introduction to Theory of Computing
CSE 105 theory of computation
Presentation transcript:

NFA to DFA conversion and regular expressions The Chinese University of Hong Kong Fall 2010 CSCI 3130: Automata theory and formal languages NFA to DFA conversion and regular expressions Andrej Bogdanov http://www.cse.cuhk.edu.hk/~andrejb/csc3130

NFAs are as powerful as DFAs Obviously, an NFA can do everything a DFA can do How about the other way? YES If L is the language of some NFA, then it is also the language of some DFA.

Converting NFAs into DFAs We will show a general way to convert every NFA into an equivalent DFA Step 1: Simplify NFA by eliminating e-transitions Step 2: Convert simplified NFA (without es) We do this first

NFA to DFA conversion intuition 0, 1 NFA: 1 q0 q1 q2 DFA: 1 q0 or q1 q0 or q2 q0 1 1

NFA to DFA conversion intuition 0, 1 NFA: 1 q0 q1 q2 DFA: 1 {q0, q1} {q0, q2} q0 1 1

General method NFA DFA states q0, q1, …, qn Æ, {q0}, {q1}, {q0,q1}, …, {q0,…,qn} one for each subset of states in the NFA initial state q0 {q0} transitions d d’({qi1,…,qik}, a) = d(qi1, a) ∪…∪ d(qik, a) accepting states F Í Q F’ = {S: S contains some state in F}

Converting via the general method 0, 1 NFA: 1 q0 q1 q2 0, 1 1 DFA: {q0, q1} {q0, q2} {q0, q1, q2} {q1, q2} {q0} {q1} {q2} Æ

Converting via the general method After eliminating the dead states and transitions, we end up with the same picture 1 1 {q0} {q0, q1} 1 0, 1 1 1 Æ {q1} {q0, q2} {q0, q1, q2} 0, 1 {q2} {q1, q2} 1

Why the method works At the end, the DFA accepts when it is in a state that contains some accepting state of NFA So the DFA accepts only when the NFA accepts too After reading n symbols, the DFA is in state {qi1,…,qik} if and only if the NFA is in one of the states qi1,…,qik

Converting NFAs into DFAs We will show a general way to convert every NFA into an equivalent DFA Step 1: Simplify NFA by eliminating e-transitions Step 2: Convert simplified NFA (without es)

Eliminating e-transitions , 1  eNFA: q0 q1 q2 Transition table of corresponding NFA: inputs 1 q0 {q0, q1, q2} {q1, q2} q1 {q0, q1, q2} Æ states q2 Æ Æ Accepting states of NFA: q0, q1, q2

Eliminating e-transitions , 1  NFA: q0 q1 q2 0, 1 NFA without es: q0 q1 q2 0, 1

Eliminating e-transitions: General method For every pair of states qi, qj and every symbol a Î S, replace every path like For every accept state qf, make accepting all states connected to it via es: q4 qi q5 a e qi q5 q2 q0 qj e a qi a qi qj a e q9 q7 q3 qf

Eliminating e-transitions , 1  NFA: q0 q1 q2 , 1 NFA without es: q0 q1 q2 , 1

Regular expressions

String concatenation s = 011 t = 101 st = 011101 ts = 101011 ss = 011011 sst = 011011101 s = a1…an st = a1…anb1…bm t = b1…bm

Operations on languages The concatenation of languages L1 and L2 is The n-th power of Ln is The union of L1 and L2 is L1L2 = {st: s  L1, t  L2} Ln = {s1s2...sn: s1, s2, ..., sn  L} L1  L2 = {s: s  L1 or s  L2}

Example L1 = {0, 01} L2 = {e, 1, 11, 111, …} any number of 1s L1L2 = {0, 01, 011, 0111, …}  {01, 011, 0111, …} = {0, 01, 011, 0111, …} 0 followed by any number of 1s L12 = {00, 001, 010, 0101} L22 = L2 L2n = L2 (n ≥ 1) L1  L2 = {0, 01, e, 1, 11, 111, ...}

Operations on languages The star of L are all strings made up of zero or more chunks from L: This is always infinite, and always contains e Example: L1 = {01, 0}, L2 = {e, 1, 11, 111, …}. What is L1* and L2*? L* = L0  L1  L2  …

Example L1 = {0, 01} L2 = {e, 1, 11, 111, …} any number of 1s L12 = {00, 001, 010, 0101} L22 = L2 L2n = L2 (n ≥ 1) L1*: 00100001 is in L1* 00110001 is not in L1* L2* = L20  L21  L22  … 10010001 is not in L1* = {e}  L21  L22  … = L2 L1* are all strings that start with 0 and do not contain consecutive 1s L2* = L2

Constructing languages with operations Let’s say S = {0, 1} We can construct languages by starting with simple ones, like {0}, {1} and combining them {0}({0}{1})* 0(0+1)* all strings that start with 0 ({0}{1}*)({1}{0}*) 01*+10* 0 followed by any number of 1s, or 1 followed by any number of 0s

Regular expressions A regular expression over S is an expression formed using the following rules: The symbol Æ is a regular expression The symbol e is a regular expression For every a  S, the symbol a is a regular expression If R and S are regular expressions, so are R+S, RS and R*. A language is regular if it is represented by a regular expression

Examples S = {0, 1} 01* = 0(1*) = {0, 01, 011, 0111, …} 0 followed by any number of 1s (01*)(01) = {001, 0101, 01101, 011101, …} 0 followed by any number of 1s and then 01

Examples 0+1 = {0, 1} strings of length 1 (0+1)* any string (0+1)*010 any string that ends in 010 (0+1)*01(0+1)* any string that contatins the pattern 01

Examples ((0+1)(0+1))*+((0+1)(0+1)(0+1))* all strings whose length is even or a mutliple of 3 = strings of length 0, 2, 3, 4, 6, 8, 9, 10, 12, ... ((0+1)(0+1))* strings of even length (0+1)(0+1) strings of length 2 ((0+1)(0+1)(0+1))* strings of length a multiple of 3 (0+1)(0+1)(0+1) strings of length 3

Examples ((0+1)(0+1)+(0+1)(0+1)(0+1))* strings that can be broken in blocks, where each block has length 2 or 3 (0+1)(0+1)+(0+1)(0+1)(0+1) strings of length 2 or 3 (0+1)(0+1) strings of length 2 (0+1)(0+1)(0+1) strings of length 3

Examples ((0+1)(0+1)+(0+1)(0+1)(0+1))* strings that can be broken in blocks, where each block has length 2 or 3 e ✓ 1 ✗ 10 ✓ 011 ✓ 00110 ✓ 011010110 ✓ this includes all strings except those of length 1 ((0+1)(0+1)+(0+1)(0+1)(0+1))* = all strings except 0 and 1

Examples (1+01+001)*(+0+00) ends in at most two 0s there can be at most two 0s between consecutive 1s there are never three consecutive 0s Guess: (1+01+001)*(+0+00) = {x: x does not contain 000} e 00 0110010110 0010010

Examples Write a regular expression for all strings with two consecutive 0s. S = {0, 1} (anything) 00 (anything else) (0+1)*00(0+1)*

Examples Write a regular expression for all strings that do not contain two consecutive 0s. S = {0, 1} 0110101101010 blocks ending in 1 last block (1 + 01) ... at most one 0 in every block ending in 1 (e + 0) ... and at most one 0 in the last block (1 + 01)*(e + 0)

Examples Write a regular expression for all strings with an even number of 0s. S = {0, 1} even number of zeros = (two zeros)* two zeros = 1*01*01* (1*01*01*)*

Main theorem for regular languages A language is regular if and only if it is the language of some DFA NFA regular expression DFA regular languages

Road map   regular expression NFA NFA without e DFA

Examples: regular expression → NFA q0 q1 q0 q1  q2 q3 q4 q5 1 M2 q0’ q1’  M2

Regular expressions A regular expression over S is an expression formed using the following rules: The symbol Æ is a regular expression The symbol e is a regular expression For every a  S, the symbol a is a regular expression If R and S are regular expressions, so are R+S, RS and R*.

General method Æ e regular expr NFA q0 q0 a q0 q1 symbol a RS    q0 MR MS q1

General method continued regular expr NFA MR   R + S q0 q1   MS     R* q0 MR q1

Road map    regular expression NFA NFA without e DFA