CSE 135: Introduction to Theory of Computation Equivalence of DFA and NFA

Similar documents
Extended transition function of a DFA

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

Nondeterministic Finite Automata

CSE 135: Introduction to Theory of Computation Nondeterministic Finite Automata

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

CSE 135: Introduction to Theory of Computation Optimal DFA

Lecture 1: Finite State Automaton

September 7, Formal Definition of a Nondeterministic Finite Automaton

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

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

Formal Models in NLP

Automata and Formal Languages - CM0081 Non-Deterministic Finite Automata

Finite Automata. Mahesh Viswanathan

Chapter 6: NFA Applications

CSE 105 Theory of Computation Professor Jeanne Ferrante

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

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 )

CSE 105 THEORY OF COMPUTATION

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

Nondeterminism. September 7, Nondeterminism

CS 208: Automata Theory and Logic

CS 154, Lecture 3: DFA NFA, Regular Expressions

E.g. The set RE of regular expressions is given by the following rules:

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

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,

COMP4141 Theory of Computation

Finite Automata and Languages

Formal Language and Automata Theory (CS21004)

Finite Automata and Regular Languages

CSE 105 THEORY OF COMPUTATION

Chap. 1.2 NonDeterministic Finite Automata (NFA)

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

Inf2A: The Pumping Lemma

Finite Automata Theory and Formal Languages TMV027/DIT321 LP4 2018

Constructions on Finite Automata

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

Computational Models Lecture 2 1

Takeaway Notes: Finite State Automata

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

Computational Models Lecture 2 1

1 Showing Recognizability

Chapter Five: Nondeterministic Finite Automata

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

CSE 105 THEORY OF COMPUTATION

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

ω-automata Automata that accept (or reject) words of infinite length. Languages of infinite words appear:

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

Deterministic Finite Automata (DFAs)

Deterministic Finite Automata (DFAs)

Non-deterministic Finite Automata (NFAs)

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

Computational Theory

CS243, Logic and Computation Nondeterministic finite automata

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

CSE 105 THEORY OF COMPUTATION

CSE 105 THEORY OF COMPUTATION

CSE 105 THEORY OF COMPUTATION

DFA to Regular Expressions

Chapter 5. Finite Automata

Theory of Computation

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

Theory of computation: initial remarks (Chapter 11)

Intro to Theory of Computation

FORMAL LANGUAGES, AUTOMATA AND COMPUTABILITY

CSE 105 Homework 3 Due: Monday October 23, Instructions. should be on each page of the submission.

FORMAL LANGUAGES, AUTOMATA AND COMPUTABILITY

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

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

Finite Automata. Dr. Neil T. Dantam. Fall CSCI-561, Colorado School of Mines. Dantam (Mines CSCI-561) Finite Automata Fall / 35

CSE 105 THEORY OF COMPUTATION

Closure under the Regular Operations

CSE 105 THEORY OF COMPUTATION

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

CSE 105 THEORY OF COMPUTATION

Finite Automata (contd)

NFAs continued, Closure Properties of Regular Languages

Propositional Logic: Models and Proofs

CSCE 551 Final Exam, April 28, 2016 Answer Key

CS 322 D: Formal languages and automata theory

CS 154. Finite Automata, Nondeterminism, Regular Expressions

Computer Sciences Department

2. Elements of the Theory of Computation, Lewis and Papadimitrou,

11-711: Algorithms for NLP Homework Assignment #1: Formal Language Theory Solutions

Suppose h maps number and variables to ɛ, and opening parenthesis to 0 and closing parenthesis

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

Nondeterministic finite automata

front pad rear pad door

CSCI 2200 Foundations of Computer Science Spring 2018 Quiz 3 (May 2, 2018) SOLUTIONS

Nondeterministic Finite Automata. Nondeterminism Subset Construction

CSE 105 THEORY OF COMPUTATION. Spring 2018 review class

Computability Theory

Lecture Notes On THEORY OF COMPUTATION MODULE -1 UNIT - 2

Non-Deterministic Finite Automata

Constructions on Finite Automata

Deterministic Finite Automata (DFAs)

CSE 105 THEORY OF COMPUTATION

Examples of Regular Expressions. Finite Automata vs. Regular Expressions. Example of Using flex. Application

FORMAL LANGUAGES, AUTOMATA AND COMPUTABILITY

Finite State Automata

Lecture 4: More on Regexps, Non-Regular Languages

Transcription:

CSE 135: Introduction to Theory of Computation Equivalence of DFA and NFA Sungjin Im University of California, Merced 02-03-2015

Expressive Power of NFAs and DFAs Is there a language that is recognized by a DFA but not by any NFAs?

Expressive Power of NFAs and DFAs Is there a language that is recognized by a DFA but not by any NFAs? No!

Expressive Power of NFAs and DFAs Is there a language that is recognized by a DFA but not by any NFAs? No! Is there a language that is recognized by an NFA but not by any DFAs?

Expressive Power of NFAs and DFAs Is there a language that is recognized by a DFA but not by any NFAs? No! Is there a language that is recognized by an NFA but not by any DFAs? No!

Main Theorem Theorem A language L is regular if and only if there is an NFA N such that L(N) = L.

Main Theorem Theorem A language L is regular if and only if there is an NFA N such that L(N) = L. In other words: For any DFA D, there is an NFA N such that L(N) = L(D), and for any NFA N, there is a DFA D such that L(D) = L(N).

Converting DFAs to NFAs Proposition For any DFA D, there is an NFA N such that L(N) = L(D).

Converting DFAs to NFAs Proposition For any DFA D, there is an NFA N such that L(N) = L(D). Proof. Is a DFA an NFA?.

Converting DFAs to NFAs Proposition For any DFA D, there is an NFA N such that L(N) = L(D). Proof. Is a DFA an NFA? Essentially yes! Syntactically, not quite. The formal definition of DFA has δ DFA : Q Σ Q whereas δ NFA : Q (Σ {ɛ}) P(Q)..

Converting DFAs to NFAs Proposition For any DFA D, there is an NFA N such that L(N) = L(D). Proof. Is a DFA an NFA? Essentially yes! Syntactically, not quite. The formal definition of DFA has δ DFA : Q Σ Q whereas δ NFA : Q (Σ {ɛ}) P(Q). For DFA D = (Q, Σ, δ D, q 0, F ), define an equivalent NFA N = (Q, Σ, δ N, q 0, F ) that has the exact same set of states, initial state and final states. Only difference is in the transition function. δ N (q, a) = {δ D (q, a)} for a Σ and δ N (q, ɛ) = for all q Q.

Simulating an NFA on Your Computer NFA Acceptance Problem Given an NFA N and an input string w, does N accept w?

Simulating an NFA on Your Computer NFA Acceptance Problem Given an NFA N and an input string w, does N accept w? How do we write a computer program to solve the NFA Acceptance problem?

Two Views of Nondeterminsm Guessing View At each step, the NFA guesses one of the choices available; the NFA will guess an accepting sequence of choices if such a one exists. Parallel View At each step the machine forks threads corresponding to each of the possible next states.

Two Views of Nondeterminsm Guessing View At each step, the NFA guesses one of the choices available; the NFA will guess an accepting sequence of choices if such a one exists. Very useful in reasoning about NFAs and in designing NFAs. Parallel View At each step the machine forks threads corresponding to each of the possible next states.

Two Views of Nondeterminsm Guessing View At each step, the NFA guesses one of the choices available; the NFA will guess an accepting sequence of choices if such a one exists. Very useful in reasoning about NFAs and in designing NFAs. Parallel View At each step the machine forks threads corresponding to each of the possible next states. Very useful in simulating/running NFA on inputs.

Algorithm for Simulating an NFA Algorithm Keep track of the current state of each of the active threads.

Algorithm for Simulating an NFA Algorithm Keep track of the current state of each of the active threads. Example 0, 1 0, 1 q 0 1 q 1 Consider the input w = 111. The execution (listing only the states of currently active threads) is Example NFA N

Algorithm for Simulating an NFA Algorithm Keep track of the current state of each of the active threads. Example 0, 1 0, 1 q 0 1 q 1 Consider the input w = 111. The execution (listing only the states of currently active threads) is q 0 Example NFA N

Algorithm for Simulating an NFA Algorithm Keep track of the current state of each of the active threads. Example 0, 1 0, 1 q 0 1 q 1 Consider the input w = 111. The execution (listing only the states of currently active threads) is q 0 1 q 0, q 1 Example NFA N

Algorithm for Simulating an NFA Algorithm Keep track of the current state of each of the active threads. Example 0, 1 0, 1 q 0 1 q 1 Example NFA N Consider the input w = 111. The execution (listing only the states of currently active threads) is 1 1 q 0 q 0, q 1 q 0, q 1, q 1 1 q 0, q 1, q 1, q 1

Algorithm With optimizations Observations Exponentially growing memory: more threads for longer inputs. Can we do better?

Algorithm With optimizations Observations Exponentially growing memory: more threads for longer inputs. Can we do better? Exact order of threads is not important

Algorithm With optimizations Observations Exponentially growing memory: more threads for longer inputs. Can we do better? Exact order of threads is not important It is unimportant whether the 5 th thread or the 1 st thread is in state q.

Algorithm With optimizations Observations Exponentially growing memory: more threads for longer inputs. Can we do better? Exact order of threads is not important It is unimportant whether the 5 th thread or the 1 st thread is in state q. If two threads are in the same state, then we can ignore one of the threads

Algorithm With optimizations Observations Exponentially growing memory: more threads for longer inputs. Can we do better? Exact order of threads is not important It is unimportant whether the 5 th thread or the 1 st thread is in state q. If two threads are in the same state, then we can ignore one of the threads Threads in the same state will behave identically; either one of the descendent threads of both will reach a final state, or none of the descendent threads of both will reach a final state

Parsimonious Algorithm in Action Example 0, 1 0, 1 q 0 1 q 1 Consider the input w = 111. The execution (listing only the states of currently active threads) is Example NFA N

Parsimonious Algorithm in Action Example 0, 1 0, 1 q 0 1 q 1 Consider the input w = 111. The execution (listing only the states of currently active threads) is {q 0 } Example NFA N

Parsimonious Algorithm in Action Example 0, 1 0, 1 q 0 1 q 1 Consider the input w = 111. The execution (listing only the states of currently active threads) is {q 0 } 1 {q 0, q 1 } Example NFA N

Parsimonious Algorithm in Action Example 0, 1 0, 1 q 0 1 q 1 Consider the input w = 111. The execution (listing only the states of currently active threads) is {q 0 } 1 {q 0, q 1 } 1 {q 0, q 1 } Example NFA N

Parsimonious Algorithm in Action Example 0, 1 0, 1 q 0 1 q 1 Example NFA N Consider the input w = 111. The execution (listing only the states of currently active threads) is {q 0 } 1 {q 0, q 1 } 1 {q 0, q 1 } 1 {q 0, q 1 }

Revisiting NFA Simulation Algorithm Need to keep track of the states of the active threads Unordered: Without worrying about exactly which thread is in what state No Duplicates: Keeping only one copy if there are multiple threads in same state

Revisiting NFA Simulation Algorithm Need to keep track of the states of the active threads Unordered: Without worrying about exactly which thread is in what state No Duplicates: Keeping only one copy if there are multiple threads in same state How much memory is needed?

Revisiting NFA Simulation Algorithm Need to keep track of the states of the active threads Unordered: Without worrying about exactly which thread is in what state No Duplicates: Keeping only one copy if there are multiple threads in same state How much memory is needed? If Q is the set of states of the NFA N, then we need to keep a subset of Q! Can be done in Q bits of memory (i.e., 2 Q states), which is finite!!

Constructing an Equivalent DFA The DFA runs the simulation algorithm

Constructing an Equivalent DFA The DFA runs the simulation algorithm DFA remembers the current states of active threads without duplicates, i.e., maintains a subset of states of the NFA

Constructing an Equivalent DFA The DFA runs the simulation algorithm DFA remembers the current states of active threads without duplicates, i.e., maintains a subset of states of the NFA When a new symbol is read, it updates the states of the active threads

Constructing an Equivalent DFA The DFA runs the simulation algorithm DFA remembers the current states of active threads without duplicates, i.e., maintains a subset of states of the NFA When a new symbol is read, it updates the states of the active threads Accepts whenever one of the threads is in a final state

Example of Equivalent DFA (1) 0, 1 0, 1 q 1 0 q 1 Example NFA N

Example of Equivalent DFA (1) 0, 1 0, 1 0, 1 0, 1 {q 1 } {} q 1 0 q 1 0 0, 1 Example NFA N 1 {q 0 } {q 0, q 1 } DFA D equivalent to N

Example of Equivalent DFA (1) 0, 1 0, 1 0, 1 0, 1 {q 1 } {} q 1 0 q 1 0 0, 1 Example NFA N 1 {q 0 } {q 0, q 1 } DFA D equivalent to N

Example of Equivalent DFA (1) 0, 1 0, 1 0, 1 0, 1 {q 1 } {} q 1 0 q 1 0 0, 1 Example NFA N 1 {q 0 } {q 0, q 1 } DFA D equivalent to N

Example of Equivalent DFA (1) 0, 1 0, 1 0, 1 0, 1 {q 1 } {} q 1 0 q 1 0 0, 1 Example NFA N 1 {q 0 } {q 0, q 1 } DFA D equivalent to N We can remove states unreachable from the initial state. How to find such states?

Example of Equivalent DFA (1) 0, 1 0, 1 0, 1 0, 1 {q 1 } {} q 1 0 q 1 0 0, 1 Example NFA N 1 {q 0 } {q 0, q 1 } DFA D equivalent to N We can remove states unreachable from the initial state. How to find such states? Use DFS or BFS!

Example of Equivalent DFA (2) ɛ q 1 0 q 0 q 3 1 q 2 ɛ Example NFA N ɛ

Example of Equivalent DFA (2) 0, 1 0, 1 q 1 {q 3 } {} ɛ 0 q 0 q 3 0 0, 1 1 q 2 ɛ 1 {q Example NFA N 0, q 1 } {q 2, q 3 } ɛ DFA D ɛ for N ɛ (only relevant states)

Example of Equivalent DFA (3) 0, 1 0, 1 q ɛ 0 q 0 0 q 00 1 q p ɛ Example NFA N

Example of Equivalent DFA (3) 0, 1 0, 1 q ɛ 0 q 0 0 q 00 1 q p ɛ Example NFA N 1 0 1 0 0 1 0 {q ɛ } {q ɛ, q 0, q 00 } {q ɛ, q p } 1 {q ɛ, q 0, q 00, q p } DFA D equivalent to N

Formal Construction Given NFA N = (Q, Σ, δ, q 0, F ), construct DFA det(n) = (Q, Σ, δ, q 0, F ) as follows. Q = q 0 = F =

Formal Construction Given NFA N = (Q, Σ, δ, q 0, F ), construct DFA det(n) = (Q, Σ, δ, q 0, F ) as follows. Q = P(Q) q 0 = F =

Formal Construction Given NFA N = (Q, Σ, δ, q 0, F ), construct DFA det(n) = (Q, Σ, δ, q 0, F ) as follows. Q = P(Q) q 0 = ˆ (q 0, ɛ) F =

Formal Construction Given NFA N = (Q, Σ, δ, q 0, F ), construct DFA det(n) = (Q, Σ, δ, q 0, F ) as follows. Q = P(Q) q 0 = ˆ (q 0, ɛ) F = {A Q A F }

Formal Construction Given NFA N = (Q, Σ, δ, q 0, F ), construct DFA det(n) = (Q, Σ, δ, q 0, F ) as follows. Q = P(Q) q 0 = ˆ (q 0, ɛ) F = {A Q A F } δ ({q 1, q 2,... q k }, a) =

Formal Construction Given NFA N = (Q, Σ, δ, q 0, F ), construct DFA det(n) = (Q, Σ, δ, q 0, F ) as follows. Q = P(Q) q 0 = ˆ (q 0, ɛ) F = {A Q A F } δ ({q 1, q 2,... q k }, a) = ˆ (q 1, a) ˆ (q 2, a) ˆ (q k, a)

Formal Construction Given NFA N = (Q, Σ, δ, q 0, F ), construct DFA det(n) = (Q, Σ, δ, q 0, F ) as follows. Q = P(Q) q 0 = ˆ (q 0, ɛ) F = {A Q A F } δ ({q 1, q 2,... q k }, a) = ˆ (q 1, a) ˆ (q 2, a) ˆ (q k, a) or more concisely, δ (A, a) = ˆ (q, a) q A

Correctness Lemma For any NFA N, the DFA det(n) is equivalent to it, i.e., L(N) = L(det(N)).

Correctness Lemma For any NFA N, the DFA det(n) is equivalent to it, i.e., L(N) = L(det(N)). Proof Idea Need to show

Correctness Lemma For any NFA N, the DFA det(n) is equivalent to it, i.e., L(N) = L(det(N)). Proof Idea Need to show w Σ. det(n) accepts w iff N accepts w

Correctness Lemma For any NFA N, the DFA det(n) is equivalent to it, i.e., L(N) = L(det(N)). Proof Idea Need to show w Σ. det(n) accepts w iff N accepts w w Σ.ˆδ(q 0, w) F iff ˆ (q 0, w) F

Correctness Lemma For any NFA N, the DFA det(n) is equivalent to it, i.e., L(N) = L(det(N)). Proof Idea Need to show w Σ. det(n) accepts w iff N accepts w w Σ.ˆδ(q 0, w) F iff ˆ (q 0, w) F w Σ. for A = ˆδ(q 0, w), A F iff ˆ (q 0, w) F

Correctness Lemma For any NFA N, the DFA det(n) is equivalent to it, i.e., L(N) = L(det(N)). Proof Idea Need to show w Σ. det(n) accepts w iff N accepts w w Σ.ˆδ(q 0, w) F iff ˆ (q 0, w) F w Σ. for A = ˆδ(q 0, w), A F iff ˆ (q 0, w) F We will instead prove the stronger claim w Σ. ˆδ(q 0, w) = A iff ˆ (q 0, w) = A.

Correctness Proof Lemma w Σ. ˆδ(q 0, w) = A iff ˆ (q 0, w) = A.

Correctness Proof Lemma w Σ. ˆδ(q 0, w) = A iff ˆ (q 0, w) = A. Proof. By induction on w

Correctness Proof Lemma w Σ. ˆδ(q 0, w) = A iff ˆ (q 0, w) = A. Proof. By induction on w Base Case w = 0: Then w = ɛ. Now

Correctness Proof Lemma w Σ. ˆδ(q 0, w) = A iff ˆ (q 0, w) = A. Proof. By induction on w Base Case w = 0: Then w = ɛ. Now ˆδ(q 0, ɛ) = q 0 defn. of ˆδ

Correctness Proof Lemma w Σ. ˆδ(q 0, w) = A iff ˆ (q 0, w) = A. Proof. By induction on w Base Case w = 0: Then w = ɛ. Now ˆδ(q 0, ɛ) = q 0 defn. of ˆδ = ˆ (q 0, ɛ) defn. of q 0

Correctness Proof Lemma w Σ. ˆδ(q 0, w) = A iff ˆ (q 0, w) = A. Proof. By induction on w Base Case w = 0: Then w = ɛ. Now ˆδ(q 0, ɛ) = q 0 defn. of ˆδ = ˆ (q 0, ɛ) defn. of q 0 Induction Hypothesis: Assume inductively that the statement holds w. w = n

Correctness Proof Induction Step Proof (contd). Induction Step: If w = n + 1 then w = ua with u = n and a Σ. ˆδ(q 0, ua) = δ(ˆδ(q 0, u), a) defn. of ˆδ

Correctness Proof Induction Step Proof (contd). Induction Step: If w = n + 1 then w = ua with u = n and a Σ. ˆδ(q 0, ua) = δ(ˆδ(q 0, u), a) defn. of ˆδ = δ( ˆ (q 0, u), a) ind. hyp.

Correctness Proof Induction Step Proof (contd). Induction Step: If w = n + 1 then w = ua with u = n and a Σ. ˆδ(q 0, ua) = δ(ˆδ(q 0, u), a) defn. of ˆδ = δ( ˆ (q 0, u), a) ind. hyp. = ˆ (q, a) defn. of δ q ˆ (q 0,u)

Correctness Proof Induction Step Proof (contd). Induction Step: If w = n + 1 then w = ua with u = n and a Σ. ˆδ(q 0, ua) = δ(ˆδ(q 0, u), a) defn. of ˆδ = δ( ˆ (q 0, u), a) ind. hyp. = ˆ (q, a) defn. of δ q ˆ (q 0,u) = ˆ (q 0, ua) prop. about ˆ