CS 154, Lecture 3: DFA NFA, Regular Expressions

Similar documents
CS 154. Finite Automata, Nondeterminism, Regular Expressions

CS 154. Finite Automata vs Regular Expressions, Non-Regular Languages

FORMAL LANGUAGES, AUTOMATA AND COMPUTABILITY

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

Lecture 4: More on Regexps, Non-Regular Languages

FORMAL LANGUAGES, AUTOMATA AND COMPUTABILITY

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

Equivalence of DFAs and NFAs

CSE 105 THEORY OF COMPUTATION

FORMAL LANGUAGES, AUTOMATA AND COMPUTABILITY

TDDD65 Introduction to the Theory of Computation

Intro to Theory of Computation

COM364 Automata Theory Lecture Note 2 - Nondeterminism

Non-deterministic Finite Automata (NFAs)

Inf2A: Converting from NFAs to DFAs and Closure Properties

CS21 Decidability and Tractability

Nondeterministic Finite Automata

Closure under the Regular Operations

Deterministic Finite Automata (DFAs)

Nondeterminism. September 7, Nondeterminism

CS 530: Theory of Computation Based on Sipser (second edition): Notes on regular languages(version 1.1)

CS 455/555: Finite automata

Fooling Sets and. Lecture 5

Theory of Computation (II) Yijia Chen Fudan University

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

Chapter Five: Nondeterministic Finite Automata

CSE 105 THEORY OF COMPUTATION

Computational Models Lecture 2 1

Theory of Computation (I) Yijia Chen Fudan University

Computational Models Lecture 2 1

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

CS243, Logic and Computation Nondeterministic finite automata

CS21 Decidability and Tractability

Finite Automata and Regular languages

Nondeterminism and Epsilon Transitions

Deterministic Finite Automata (DFAs)

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

CSE 105 Theory of Computation Professor Jeanne Ferrante

Theory of Languages and Automata

Deterministic Finite Automata (DFAs)

Regular Languages. Problem Characterize those Languages recognized by Finite Automata.

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

Uses of finite automata

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

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,

Regular Expressions. Definitions Equivalence to Finite Automata

CS 154 Introduction to Automata and Complexity Theory

CISC 4090 Theory of Computation

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

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

Lecture 3: Nondeterministic Finite Automata

Sri vidya college of engineering and technology

Theory of Computation Lecture 1. Dr. Nahla Belal

Introduction to Languages and Computation

Theory of Computation p.1/?? Theory of Computation p.2/?? Unknown: Implicitly a Boolean variable: true if a word is

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

CSE 135: Introduction to Theory of Computation Nondeterministic Finite Automata (cont )

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

Finite Automata Part Two

CPS 220 Theory of Computation REGULAR LANGUAGES

CSE 105 THEORY OF COMPUTATION

Nondeterministic finite automata

front pad rear pad door

Nondeterministic Finite Automata

What we have done so far

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

Harvard CS 121 and CSCI E-207 Lecture 4: NFAs vs. DFAs, Closure Properties

Outline. CS21 Decidability and Tractability. Regular expressions and FA. Regular expressions and FA. Regular expressions and FA

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

Computational Theory

Finite Automata Part Two

Nondeterministic Finite Automata. Nondeterminism Subset Construction

Chapter 5. Finite Automata

Chap. 1.2 NonDeterministic Finite Automata (NFA)

CSC236 Week 10. Larry Zhang

Computer Sciences Department

CSE 105 THEORY OF COMPUTATION

Proving languages to be nonregular

Deterministic Finite Automata (DFAs)

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

Automata: a short introduction

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

GEETANJALI INSTITUTE OF TECHNICAL STUDIES, UDAIPUR I

CSE 105 THEORY OF COMPUTATION

CHAPTER 1 Regular Languages. Contents

Text Search and Closure Properties

Clarifications from last time. This Lecture. Last Lecture. CMSC 330: Organization of Programming Languages. Finite Automata.

CS:4330 Theory of Computation Spring Regular Languages. Finite Automata and Regular Expressions. Haniel Barbosa

CSE 105 THEORY OF COMPUTATION

Closure Properties of Context-Free Languages. Foundations of Computer Science Theory

Further discussion of Turing machines

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

Lecture 7 Properties of regular languages

CSE 105 THEORY OF COMPUTATION

CSE 105 THEORY OF COMPUTATION

Computational Models - Lecture 3

1 More finite deterministic automata

Computational Models - Lecture 1 1

UNIT-III REGULAR LANGUAGES

Chapter 6: NFA Applications

Transcription:

CS 154, Lecture 3: DFA NFA, Regular Expressions

Homework 1 is coming out

Deterministic Finite Automata Computation with finite memory

Non-Deterministic Finite Automata Computation with finite memory and verified guessing

From NFAs to DFAs Input: NFA N = (Q, Σ,, Q 0, F) Output: DFA M = (Q, Σ,, q 0, F ) To learn if an NFA accepts, we could do the computation in parallel, maintaining the set of all possible states that can be reached Idea: Set Q = 2 Q

* From NFAs to DFAs: Subset Construction Input: NFA N = (Q, Σ,, Q 0, F) Output: DFA M = (Q, Σ,, q 0, F ) Q = 2 Q : Q Σ Q (R, ) = ε( (r, ) ) r R q 0 = ε(q 0 ) * F = { R Q f R for some f F } For S Q, the ε-closure of S is ε(s) = {q q reachable from some s S by taking 0 or more ε transitions}

Example of the ε-closure ε({q 0 }) = {q 0, q 1, q 2 } ε({q 1 }) = {q 1, q 2 } ε({q 2 }) = {q 2 }

Given: NFA N = ( {1,2,3}, {a,b},, {1}, {1} ) Construct: Equivalent DFA M = (2 {1,2,3}, {a,b},, {1,3}, ) N 1 a {1,3} a M {3} b a b ε a b b b a,b a, b 2 3 ε({1}) = {1,3} {2} a {2,3} {1}, {1,2}? a b {1,2,3} a

Reverse Theorem for Regular Languages Theorem: The reverse of a regular language is also a regular language If a language can be recognized by a DFA that reads strings from right to left, then there is an normal DFA that accepts the same language Proof? Given a DFA for a language L, reverse its arrows and flip its start and accept states, getting an NFA. Convert that NFA back to a DFA!

Using NFAs in place of DFAs can make proofs about regular languages much easier! Remember this on homework/exams!

Union Theorem using NFAs?

Regular Languages are closed under concatenation Concatenation: A B = { vw v A and w B } Given DFAs M 1 and M 2, connect the accept states of M 1 to the start states of M 2 0 0,1 1 0 ε 0 0,1 1 0,1 1 1 1 1 0 0 0 ε 0 0 0 1 L(N) = L(M 1 ) L(M 2 ) 1 1

Regular Languages are closed under star A* = { s 1 s k k 0 and each s i A } Let M be a DFA, and let L = L(M) We can construct an NFA N that recognizes L* ε 0 0,1 1 ε 1 0 0 1 ε

Formally, the construction is: Input: DFA M = (Q, Σ,, q 1, F) Output: NFA N = (Q, Σ,, q 0, F ) Q = Q {q 0 } F = F {q 0 } (q,a) = (q,a) {q 1 } {q 1 } if q Q and a ε if q F and a = ε if q = q 0 and a = ε if q = q 0 and a ε else

Regular Languages are Closed Under Star How would we prove that this NFA construction works? Want to show: L(N) = L* 1. L(N) L* 2. L(N) L*

1. L(N) L* Assume w = w 1 w k is in L* where w 1,,w k L We show N accepts w by induction on k Base Cases: k = 0 k = 1 (w = ε) (w L) Inductive Step: Assume N accepts all strings v = v 1 v k L*, v i L Let u = u 1 u k u k+1 L*, u j L Since N accepts u 1 u k (by induction) and M accepts u k+1, N also accepts u (by construction)

2. L(N) L* Assume w is accepted by N; we want to show w L* If w = ε, then w L* I.H. N accepts u and takes at most k ε-transitions u L* u ε u L(N), so u L* By I.H. Let w be accepted by N with k+1 ε-transitions. Write w as w=uv, where v is the substring read after the last ε- transition v ε v L accept w = uv L*

Closure Properties for Regular Languages Union: A B = { w w A or w B } Intersection: A B = { w w A and w B } Complement: A = { w Σ* w A } Reverse: A R = { w 1 w k w k w 1 A, w i Σ} Concatenation: A B = { vw v A and w B } Star: A* = { s 1 s k k 0 and each s i A } Theorem: if A and B are regular then so are: A B, A B, A, A R, A B, and A*

Regular Expressions Computation as simple, logical description A totally different way of thinking about computation: What is the complexity of describing the strings in the language?

Inductive Definition of Regexp Let Σ be an alphabet. We define the regular expressions over Σ inductively: For all Σ, is a regexp ε is a regexp is a regexp If R 1 and R 2 are both regexps, then (R 1 R 2 ), (R 1 + R 2 ), and (R 1 )* are regexps

Precedence Order: * then then + Example: R 1 *R 2 + R 3 = ( ( R 1 * ) R 2 ) + R 3

Definition: Regexps Represent Languages The regexp Σ represents the language { } The regexp ε represents {ε} The regexp represents If R 1 and R 2 are regular expressions representing L 1 and L 2 then: (R 1 R 2 ) represents L 1 L 2 (R 1 + R 2 ) represents L 1 L 2 (R 1 )* represents L 1 *

Regexps Represent Languages For every regexp R, define L(R) to be the language that R represents A string w Σ* is accepted by R (or, w matches R) if w L(R) Examples: 0, 010, and 01010 match (01)*0 110101110100100 matches (0+1)*0

Assume Σ = {0,1} { w w has exactly a single 1 } 0*10* { w w contains 001 } (0+1)*001(0+1)*

Assume Σ = {0,1} What language does the regexp * represent? {ε}

Assume Σ = {0,1} { w w has length 3 and its 3rd symbol is 0 } (0+1)(0+1)0(0+1)*

Assume Σ = {0,1} { w every odd position in w is a 1 } (1(0 + 1))*(1 + ε)

Assume Σ = {0,1} { w w has equal number of occurrences of 01 and 10} = { w w = 1, w = 0, or w = ε, or w starts with a 0 and ends with a 0, or w starts with a 1 and ends with a 1 } Claim: A string w has equal occurrences of 01 and 10 w starts and ends with the same bit. 1 + 0 + ε + 0(0+1)*0 + 1(0+1)*1

DFAs NFAs Regular Expressions! L can be represented by some regexp L is regular

L can be represented by some regexp L is regular

L can be represented by some regexp L is regular Base Cases (R has length 1): Given any regexp R, we will construct an NFA N s.t. N accepts exactly the strings accepted by R R = Proof by induction on the length of the regexp R R = ε R =

Induction Step: Suppose every regexp of length < k represents some regular language. Consider a regexp R of length k > 1 Three possibilities for R: R = R 1 + R 2 R = R 1 R 2 R = (R 1 )*

Induction Step: Suppose every regexp of length < k represents some regular language. Consider a regexp R of length k > 1 Three possibilities for R: R = R 1 + R 2 R = R 1 R 2 R = (R 1 )* By induction, R 1 and R 2 represent some regular languages, L 1 and L 2 But L(R) = L(R 1 + R 2 ) = L 1 L 2 so L(R) is regular, by the union theorem!

Induction Step: Suppose every regexp of length < k represents some regular language. Consider a regexp R of length k > 1 Three possibilities for R: R = R 1 + R 2 R = R 1 R 2 R = (R 1 )* By induction, R 1 and R 2 represent some regular languages, L 1 and L 2 But L(R) = L(R 1 R 2 ) = L 1 L 2 so L(R) is regular by the concatenation theorem

Induction Step: Suppose every regexp of length < k represents some regular language. Consider a regexp R of length k > 1 Three possibilities for R: R = R 1 + R 2 R = R 1 R 2 R = (R 1 )* By induction, R 1 and R 2 represent some regular languages, L 1 and L 2 But L(R) = L(R 1 *) = L 1 * so L(R) is regular, by the star theorem

Induction Step: Suppose every regexp of length < k represents some regular language. Consider a regexp R of length k > 1 Three possibilities for R: R = R 1 + R 2 R = R 1 R 2 R = (R 1 )* By induction, R 1 and R 2 represent some regular languages, L 1 and L 2 But L(R) = L(R 1 *) = L 1 * so L(R) is regular, by the star theorem Therefore: If L is represented by a regexp, then L is regular

Give an NFA that accepts the language represented by (1(0 + 1))* ε 1 0,1 ε Regular expression: ( 1(0+1) )*

Generalized NFAs (GNFA) L can be represented by a regexp L is a regular language Idea: Transform an NFA for L into a regular expression by removing states and re-labeling the arcs with regular expressions Rather than reading in just letters from the string on a step, we can read in entire substrings

Generalized NFA (GNFA) Is aaabcbcba accepted or rejected? Is bba accepted or rejected? Is bcba accepted or rejected? This GNFA recognizes L(a*b(cb)*a)

NFA Add unique start and accept states

NFA While the machine has more than 2 states: Pick an internal state, rip it out and re-label the arrows with regexps, to account for paths through the missing state 0 01*0 0 1

GNFA While the machine has more than 2 states: In general: R(q 1,q 3 ) R(q 1,q 2 )R(q 2,q 2 )*R(q 2,q 3 ) + R(q 1,q 3 ) R(q 1,q 2 ) R(q 2,q 3 ) q 1 q 2 q 3 R(q 2,q 2 )

a a,b ε a*b(a*b)(a+b)* b ε q 0 q 1 q 2 q 3 R(q 0,q 3 ) = (a*b)(a+b)* represents L(N)

DFAs NFAs DEFINITION Regular Languages Regular Expressions

Parting thoughts: Regular Languages can be defined by their closure properties NFA=DFA, does it mean that nondeterminism is free for Finite Automata? Questions?