Computational Models Lecture 2 1

Similar documents
Computational Models Lecture 2 1

Computational Models - Lecture 1 1

Non-Deterministic Finite Automata

Computational Models: Class 3

Computational Models - Lecture 5 1

What we have done so far

Computational Models #1

CS 154. Finite Automata, Nondeterminism, Regular Expressions

FORMAL LANGUAGES, AUTOMATA AND COMPUTABILITY

Computational Models - Lecture 3 1

Computational Models: Class 1

Computational Models - Lecture 3

Computational Models - Lecture 3 1

Recitation 2 - Non Deterministic Finite Automata (NFA) and Regular OctoberExpressions

Computational Models Lecture 8 1

Computational Models - Lecture 4 1

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

CS 154, Lecture 3: DFA NFA, Regular Expressions

Theory of Computation (I) Yijia Chen Fudan University

COM364 Automata Theory Lecture Note 2 - Nondeterminism

Computational Models Lecture 8 1

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

CS21 Decidability and Tractability

Nondeterministic Finite Automata

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

Critical CS Questions

Computational Models Lecture 8 1

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

Lecture 3: Nondeterministic Finite Automata

Equivalence of DFAs and NFAs

Computational Theory

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

Computational Models: Class 5

Computational Models - Lecture 4 1

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

Chapter Five: Nondeterministic Finite Automata

FORMAL LANGUAGES, AUTOMATA AND COMPUTABILITY

CS21 Decidability and Tractability

Theory of Computation (II) Yijia Chen Fudan University

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

Theory of Languages and Automata

Non-deterministic Finite Automata (NFAs)

Computational Models - Lecture 4

Harvard CS 121 and CSCI E-207 Lecture 6: Regular Languages and Countability

Introduction to Languages and Computation

Computational Models - Lecture 5 1

Computer Sciences Department

Time Magazine (1984)

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

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

3515ICT: Theory of Computation. Regular languages

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

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

FORMAL LANGUAGES, AUTOMATA AND COMPUTABILITY

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

INF Introduction and Regular Languages. Daniel Lupp. 18th January University of Oslo. Department of Informatics. Universitetet i Oslo

CS 455/555: Finite automata

Automata and Formal Languages - CM0081 Non-Deterministic Finite Automata

UNIT-III REGULAR LANGUAGES

Lecture 4 Nondeterministic Finite Accepters

Lecture 1: Finite State Automaton

cse303 ELEMENTS OF THE THEORY OF COMPUTATION Professor Anita Wasilewska

Nondeterministic Finite Automata

Automata: a short introduction

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

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

acs-04: Regular Languages Regular Languages Andreas Karwath & Malte Helmert Informatik Theorie II (A) WS2009/10

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

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

CSC173 Workshop: 13 Sept. Notes

Intro to Theory of Computation

Nondeterministic Finite Automata. Nondeterminism Subset Construction

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

Theory of computation: initial remarks (Chapter 11)

Foundations of

Regular Expressions. Definitions Equivalence to Finite Automata

front pad rear pad door

Theory of Computation Lecture 1. Dr. Nahla Belal

Extended transition function of a DFA

Chap. 1.2 NonDeterministic Finite Automata (NFA)

Closure under the Regular Operations

CSE 105 Theory of Computation Professor Jeanne Ferrante

Finite Automata and Regular languages

September 7, Formal Definition of a Nondeterministic Finite Automaton

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

Inf2A: Converting from NFAs to DFAs and Closure Properties

Name: Student ID: Instructions:

CS243, Logic and Computation Nondeterministic finite automata

Computational Models Lecture 5

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,

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

Automata and Languages

Lecture 4: More on Regexps, Non-Regular Languages

Nondeterministic finite automata

Regular expressions and Kleene s theorem

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

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

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

Nondeterminism and Epsilon Transitions

COMP4141 Theory of Computation

Transcription:

Computational Models Lecture 2 1 Handout Mode Iftach Haitner. Tel Aviv University. October 30, 2017 1 Based on frames by Benny Chor, Tel Aviv University, modifying frames by Maurice Herlihy, Brown University. Iftach Haitner (TAU) Computational Models, Lecture 2 October 30, 2017 1 / 69

Computational Models - Lecture 2 Non-Deterministic Finite Automata (NFA) Closure of Regular Languages Under,, Regular expressions Equivalence with finite automata Sipser s book, 1.1 1.3 Iftach Haitner (TAU) Computational Models, Lecture 2 October 30, 2017 2 / 69

Part I Non-Deterministic Finite Automata Iftach Haitner (TAU) Computational Models, Lecture 2 October 30, 2017 3 / 69

DFA formal definition, (reminder) Definition 1 (DFA) A deterministic finite automaton (DFA) is a 5-tuple (Q, Σ, δ, q 0, F), where Q is a finite set called the states, Σ is a finite set called the alphabet, δ : Q Σ Q is the transition function, q 0 Q is the start state, and F Q is the set of accept states. Iftach Haitner (TAU) Computational Models, Lecture 2 October 30, 2017 4 / 69

Formal model of computation, (reminder) Definition 2 M = (Q, Σ, δ, q 0, F ) accepts w Σ if δ(q 0, w) F. Definition 3 ( δ) For DFA M = (Q, Σ, δ, q 0, F), define δ : Q Σ Q by δ(q, w) = { δ( δ(q, w1,...,n 1 ), w n ), n = w 1 q, w = ε.. Iftach Haitner (TAU) Computational Models, Lecture 2 October 30, 2017 5 / 69

The language of a DFA, (reminder) Definition 4 The language of a DFA M, denoted L(M), is the set of strings that M accepts. Definition 5 A language is called regular, if some deterministic finite automaton accepts it. Iftach Haitner (TAU) Computational Models, Lecture 2 October 30, 2017 6 / 69

NFA non-deterministic Finite Automata 0,1 0,1 0 0 q q 1 q 2 3 1 q 4 May have more than one transition labeled with the same symbol, May have no transitions labeled with a certain symbol, May have transitions labeled with ε, the symbol of the empty string. Will deal with this latter Every DFA is also an NFA. Iftach Haitner (TAU) Computational Models, Lecture 2 October 30, 2017 7 / 69

Non-deterministic computation 0,1 0,1 0 0 q q 1 q 2 3 1 q 4 What happens when more than one transition is possible? The machine splits into multiple copies Each branch follows one possibility Together, branches follow all possibilities. If the input doesn t appear, that branch dies. Automaton accepts if some branch accepts. Iftach Haitner (TAU) Computational Models, Lecture 2 October 30, 2017 8 / 69

Computation on 1001 0,1 0,1 0 0 q q 1 q 2 3 1 q 4 symbol 1 q 1 0 q 1 0 q 1 q 2 1 q q 1 q 2 3 q 1 q 4 Iftach Haitner (TAU) Computational Models, Lecture 2 October 30, 2017 9 / 69

Why non-determinism? Theorem 6 (Informal, to be proved soon) Deterministic and non-deterministic finite automata, accept exactly the same set of languages. Q.: So why do we need NFA s? Design a finite automaton for the language L all binary strings with a 1 in their third-to-the-last position? Iftach Haitner (TAU) Computational Models, Lecture 2 October 30, 2017 10 / 69

NFA for L 0,1 1 0,1 q q 1 q 2 3 0,1 q 4 Guesses which symbol is third from the last, and checks that indeed it is a 1. If guess is premature, that branch dies, and no harm occurs. Iftach Haitner (TAU) Computational Models, Lecture 2 October 30, 2017 11 / 69

DFA for L Have 8 states, encoding the last three observed letters. A state for each string in {0, 1} 3. Add transitions on modifying the suffix, give the new letter. Mark as accepting, the strings 1 0 0 q 000 0 0 q 100 q 010 q 110 1 1 1 0 0 0 q 001 q 101 1 q 011 1 q 111 1 1 DFA has few bugs... Iftach Haitner (TAU) Computational Models, Lecture 2 October 30, 2017 12 / 69

NFA Formal Definition Let P(Q) denote the powerset of Q (i.e., all subsets of Q). Definition 7 (NFA) A non-deterministic finite automaton is a 5-tuple (Q, Σ, δ, S, F ), where Q is a finite set called the states Σ is a finite set called the alphabet δ : Q Σ P(Q) is the transition function S Q is the set of starting states F Q are the set of accepting states We sometimes consider an NFA (Q, Σ, δ, q 0, F). This is merely a syntactic sugar for the NFA (Q, Σ, δ, {q 0 }, F) Iftach Haitner (TAU) Computational Models, Lecture 2 October 30, 2017 13 / 69

Example 0,1 0,1 0 0 q q 1 q 2 3 1 q 4 N 1 = (Q = {q 1, q 2, q 3, q 4 }, Σ = {0, 1}, δ, S = {q 1 }, F = {q 4 }) for δ defined by 0 1 q 1 {q 1, q 2 } {q 1 } q 2 {q 3 } q 3 {q 4 } q 4 {q 4 } {q 4 } Note that is a valid output for δ Iftach Haitner (TAU) Computational Models, Lecture 2 October 30, 2017 14 / 69

Formal model of computation Definition 8 N = (Q, Σ, δ, S, F ) accepts w Σ, if δ N (S, w) F. Definition 9 ( δ) For NFA N = (Q, Σ, δ, S, F ), define δ N : P(Q) Σ P(Q) by: { Q for Q Q, δn (Q, w = ε,, w) = q δ N (Q,w 1,...,n 1 ) δ(q, w n), n = w 1.. When clear from he context we will write δ (i.e., omitting the N ). Iftach Haitner (TAU) Computational Models, Lecture 2 October 30, 2017 15 / 69

An equivalent definition Definition 10 (Equivalent definition) N = (Q, Σ, δ, S, F ) accepts w = w 1,..., w n Σ n, if if r 0,..., r n Q s.t. r 0 S r n F r i+1 δ(r i, w i+1 ), for all 0 i < n. Iftach Haitner (TAU) Computational Models, Lecture 2 October 30, 2017 16 / 69

Equivalence of NFA s and DFA s Easy: For any DFA M there exists a NFA N such that L(N) = L(M). Other direction is also true. Theorem 11 For any NFA N there exists a DFA M such that L(N) = L(M). Given an NFA N, we construct a DFA M, that accepts the same language. Make DFA emulates all possible NFA states. As consequence of the construction, if the NFA has k states, the DFA has 2 k states (an exponential blow up). Iftach Haitner (TAU) Computational Models, Lecture 2 October 30, 2017 17 / 69

Equivalence of NFA s and DFA s, the DFA Let N = (Q, Σ, δ, S, F). Construction 12 (M = (Q M, Σ, δ M, d 0, F M )) Q M = {[R]: R Q}. d 0 = [S] F M = {[R] Q M : R F } For [R] Q M and σ Σ, let δ M ([R], σ) = [ δ N (R, σ)] (= [ r R δ(r, σ)] ). To prove equivalence, we need to prove that δ N (S, w) F δ M (d 0, w) F M The above is an immediate corollary of the following claim: Claim 13 [ δ N (S, w)] = δ M (d 0, w) for every w Σ. Iftach Haitner (TAU) Computational Models, Lecture 2 October 30, 2017 18 / 69

Proving [ δ N (S, w)] = δ M (d 0, w) The proof is by induction on the length of w. w = 0, by definition. Assume for words of length (m 1), and let x = yσ, where y is a word of length (m 1) and σ Σ. Let Q y = δ N (S, y) and d y = δ M (d 0, y). Compute δ M (d 0, x) = δ M (d y, σ) (By definition of δ M ) = δ M ([Q y ], σ) (By i.h) = [ δ N (Q y, σ)] (By definition of δ M ) = [ ] δ(q, σ) (By definition of δ N ) q Q y = [ δ(q, σ) ] q δ N (S,y) = [ δ N (S, x)]. (By definition of δ N ) But how come we encode infinite parallel threads into a single thread? Iftach Haitner (TAU) Computational Models, Lecture 2 October 30, 2017 19 / 69

Example: NFA DFA Non-Deterministic Automata: Iftach Haitner (TAU) Computational Models, Lecture 2 October 30, 2017 20 / 69

Example: NFA DFA Deterministic automata - set of states: Iftach Haitner (TAU) Computational Models, Lecture 2 October 30, 2017 21 / 69

Example: NFA DFA Transitions from [{q 0 }]: Iftach Haitner (TAU) Computational Models, Lecture 2 October 30, 2017 22 / 69

Example: NFA DFA Transitions from [{q 0, q 1 }]: Transitions from [ ] and [{q 1 }]? Iftach Haitner (TAU) Computational Models, Lecture 2 October 30, 2017 23 / 69

NFA with ε-moves 0,1 0,1 0 0,ε q q 1 q 2 3 1 q 4 What is the interpretation of ε transitions? What will happen with 101? How to distinguish ε (that stands form the empty word) from the ε above (that stands free transition ) Iftach Haitner (TAU) Computational Models, Lecture 2 October 30, 2017 24 / 69

Example: NFA with ε-moves L = {a i b j c k i, j, k 0} Iftach Haitner (TAU) Computational Models, Lecture 2 October 30, 2017 25 / 69

NFA Formal definition with ε-moves Transition function δ is going to be different. Let P(Q) denote the powerset of Q. Let Σ ε denote the set Σ {ε}. Definition 14 (NFA, with ε-moves) A non-deterministic finite automaton is a 5-tuple (Q, Σ, δ, S, F ): Q is a finite set called the states Σ is a finite set called the alphabet δ : Q Σ ε P(Q) is the transition function S Q is the set of starting state F Q is the set of accepting states Iftach Haitner (TAU) Computational Models, Lecture 2 October 30, 2017 26 / 69

Example 0,1 0,1 0 0,ε q q 1 q 2 3 1 q 4 N 1 = (Q, Σ, δ, S, F ): Q = {q 1, q 2, q 3, q 4 }, Σ = {0, 1}, S = {q 1 } and F = {q 4 }. δ is 0 1 ε q 1 {q 1, q 2 } {q 1 } q 2 {q 3 } {q 3 } q 3 {q 4 } q 4 {q 4 } {q 4 } Iftach Haitner (TAU) Computational Models, Lecture 2 October 30, 2017 27 / 69

Formal model of computation, with ε-moves Definition 15 N = (Q, Σ, δ, S, F ) accepts w Σ, if δ N (S, w) F. Definition 16 For NFA N = (Q, Σ, δ, S, F ), let E(q) = {q Q : q can be reached from q by 0 or more ε transitions} (i.e., {q : q 1,..., q k Q s.t.q 1 = q q k = q i [k 1] q i+1 δ(q i, ε)}) E(Q ) = q Q E(q). Q: is it always the case that q E(q)? Yes Definition 17 ( δ) For NFA N = (Q, Σ, δ, S, F ), define δ N : P(Q) Σ P(Q) by: { E(Q for Q Q, δn (Q ( ), w = ε,, w) = ) E r δ(q,w 1,...,w n 1 ) δ(r, w n), n = w 1.. When does N accept the empty string? Iftach Haitner (TAU) Computational Models, Lecture 2 October 30, 2017 28 / 69

An equivalent definition For a (Σ ε ), let d(a) Σ be a without the ε symbols. Example: d(ε01εε3ε) = 013 Definition 18 (Equivalent definition) N = (Q, Σ, δ, S, F ) accepts w Σ, if exist a = (a 1 a 2... a k ) (Σ ε ) k and r 0,..., r k Q s.t. w = d(a). r 0 S r k F r i+1 δ(r i, a i+1 ), for all 0 i < k. Iftach Haitner (TAU) Computational Models, Lecture 2 October 30, 2017 29 / 69

Removing ε-transitions Given NFA N = (Q, Σ, δ, S, F) with ε-transitions, we create an equivalent NFA N = (Q, Σ, δ, S, F) with no ε-transitions. S = E(S) δ (q, a) = E(δ(q, a)) It is not hard to prove that δ N (S, w) = δ N (S, w) for any w Σ. Thus, L(N) = L(N ). Iftach Haitner (TAU) Computational Models, Lecture 2 October 30, 2017 30 / 69

Example: Removing ε-transitions Non-Deterministic Automata with ε-transitions The non-deterministic automata without ε-transitions S = {q 0, q 1, q 2, q 3 } δ is a b c q 0 q 1 {q 1, q 2, q 3 } q 2 {q 2, q 3 } q 3 {q 3 } Iftach Haitner (TAU) Computational Models, Lecture 2 October 30, 2017 31 / 69

Part II Closure of Regular Languages, Revisited Iftach Haitner (TAU) Computational Models, Lecture 2 October 30, 2017 32 / 69

Regular languages, revisited By definition, a language is regular if it is accepted by some DFA. Corollary 19 A language is regular if and only if it is accepted by some NFA. This is an alternative way of characterizing regular languages. We will now use the equivalence to show that regular languages are closed under the regular operations (union, concatenation, star). Iftach Haitner (TAU) Computational Models, Lecture 2 October 30, 2017 33 / 69

Closure under union (alternative proof) N 1 N 2 Iftach Haitner (TAU) Computational Models, Lecture 2 October 30, 2017 34 / 69

Closure under union, cont. N 1 ε ε N 2 Iftach Haitner (TAU) Computational Models, Lecture 2 October 30, 2017 35 / 69

Closure under union cont.. NFA N 1 = (Q 1, Σ, δ 1, S 1, F 1 ) accept L 1, and NFA N 2 = (Q 2, Σ, δ 2, S 2, F 2 ) accept L 2. wlg. Q 1 Q 2 =.(?) Define NFA N = (Q = {q 0 } Q 1 Q 2, Σ, δ, S = {q 0 }, F = F 1 F 2 ), δ 1 (q, a) q Q 1 for δ(q, a) = δ 2 (q, a) q Q 2 S 1 S 2 q = q 0 and a = ε Alternatively, let S = S 1 S 2 and omit the last line of δ. Claim 20 L(N) = L(N 1 ) L(N 2 ). Proof: Easily follow by the the alternative definition of acceptance by NFA, DIY... Iftach Haitner (TAU) Computational Models, Lecture 2 October 30, 2017 36 / 69

Closure under concatenation N 1 N 2 Iftach Haitner (TAU) Computational Models, Lecture 2 October 30, 2017 37 / 69

Closure under concatenation, cont. N 1 ε ε N 2 Remark: Final states are exactly those of N 2. Iftach Haitner (TAU) Computational Models, Lecture 2 October 30, 2017 38 / 69

Closure under concatenation, cont.. NFA N 1 = (Q 1, Σ, δ 1, S 1, F 1 ) accept L 1 NFA N 2 = (Q 2, Σ, δ 2, S 2, F 2 ) accept L 2 Define NFA N = (Q 1 Q 2, Σ, δ, S 1, F 2 ): δ 1 (q, a) q Q 1 a ε δ δ(q, a) = 1 (q, a) (q Q 1 \ F 1 ) a = ε δ 1 (q, a) S 2 q F 1 a = ε δ 2 (q, a) q Q 2 Claim 21 L(N) = L 1 L 2. Proof: Need to prove w L(N) w L 1 L 2. We assume wlg. that N 1 and N 2 have no ε moves. Iftach Haitner (TAU) Computational Models, Lecture 2 October 30, 2017 39 / 69

Proving w L(N) L 1 L 2 Let w L 1 L 2 : = w 1 L 1 and w 2 L 2, s.t. w = w 1 w 2 = r 1 0,... r 1 w 1 and r 2 0,... r 2 w 2, s.t. r 1 0 S 1 (2) r 1 w 1 F 1 (3) i: r 1 i+1 δ 1(r 1 i, w 1 i+1 ). r 2 0 S 2 (2) r 2 w 2 F 2 (3) i: r 2 i+1 δ 2(r 2 i, w 2 i+1 ). = r0 1,... r 1 w 1, r 0 2,... r 2 proves that w L(N) w 2 Let w L(N) be of length k : = j and r 0... r k+1 s.t. for (a 1,..., a k+1 ) = (w 1,..., w j, ε, w j+1,... w k ): r 0 S 1 and r k+1 F 2 i: ri+1 δ(r i, a i+1 ). r 0... r j proves that (w 1,..., w j ) L 1 r j+1... r k proves that (w j+1,..., w k ) L 2 = w L 1 L 2 Iftach Haitner (TAU) Computational Models, Lecture 2 October 30, 2017 40 / 69

Closure under star Iftach Haitner (TAU) Computational Models, Lecture 2 October 30, 2017 41 / 69

Closure under star, cont. Let N = (Q, Σ, δ, S, F) accepting L, assuming wlg. that q 0 / Q. Define N = (Q = Q {q 0 }, Σ, δ, S = {q 0 }, F = {q 0 }): δ(q, a) q Q a ε δ δ(q, ε) q / F a = ε (q, a) = δ(q, ε) {q 0 } q F a = ε S q = q 0 a = ε Claim 22 L(N ) = L(N). Proof: DIY Iftach Haitner (TAU) Computational Models, Lecture 2 October 30, 2017 42 / 69

Summary Regular languages are closed under union concatenation star Non-deterministic finite automata are equivalent to deterministic finite automata but much easier to use in some proofs and constructions. Iftach Haitner (TAU) Computational Models, Lecture 2 October 30, 2017 43 / 69

Part III Regular Expressions Iftach Haitner (TAU) Computational Models, Lecture 2 October 30, 2017 44 / 69

Regular expressions Notation for building up languages by describing them as expressions, e.g., (0 1)0. 0 and 1 are shorthand for the set (languages) {0} and {1} so 0 1 = {0, 1}. 0 is shorthand for {0}. Concatenation, is implicit. So 0 10 stands for {w {0, 1}: w has exactly a single 1}. Just like in arithmetic, operations have precedence: star first concatenation next union last parentheses used to change default order i.e., ab (ab) Q.: What does (0 1)0 stand for? Remark: Regular expressions are often used in text editors or shell scripts. Iftach Haitner (TAU) Computational Models, Lecture 2 October 30, 2017 45 / 69

Regular expressions formal definition Definition 23 A string R is a regular expression over alphabet Σ, if R is of form a for some a Σ ε (R 1 R 2 ) for regular expressions R 1 and R 2 (R 1 R 2 ) for regular expressions R 1 and R 2 (R1 ) for regular expression R 1 R(Σ) denotes all (finite) regular expression over Σ. Parenthesis and are omitted when their role is clear from the context. Iftach Haitner (TAU) Computational Models, Lecture 2 October 30, 2017 46 / 69

Formal definition, cont. Definition 24 The language L(R) of regular expression R, is defined by R L(R) a {a} ε {ε} (R 1 R 2 ) L(R 1 ) L(R 2 ) (R 1 R 2 ) L(R 1 ) L(R 2 ) (R1 ) L(R 1) Isn t this definition circular? Iftach Haitner (TAU) Computational Models, Lecture 2 October 30, 2017 47 / 69

Examples of regular expressions For Σ = {0, 1}, write regular expression for the following languages: The third letter from the end is 1 (0 1) 1(0 1) 2 The number of 1 s is even (0 10 1) The number of 1 s is odd (0 10 1) 10 Iftach Haitner (TAU) Computational Models, Lecture 2 October 30, 2017 48 / 69

Part IV Regular Expressions and Regular Languages Iftach Haitner (TAU) Computational Models, Lecture 2 October 30, 2017 49 / 69

A remarkable fact Theorem 25 A language is described by a regular expression iff it is regular. =: Given a regular language, construct a regular expression describing it. = : Given a regular expression, construct an NFA accepting its language. Iftach Haitner (TAU) Computational Models, Lecture 2 October 30, 2017 50 / 69

Given RE R, build NFA accepting it (= ) a 1. R = a, for some a Σ 2. R = ε 3. R = Iftach Haitner (TAU) Computational Models, Lecture 2 October 30, 2017 51 / 69

Given R, Build NFA Accepting It (= ), cont. ε N 1 ε N 2 N 1 ε ε N 2 R = (R 1 R 2 ) R = (R 1 R 2 ) Iftach Haitner (TAU) Computational Models, Lecture 2 October 30, 2017 52 / 69

Examples a a b b ba b ε a ε b ε a ba U a ε a Formal proof by induction on the length of the regular expression Iftach Haitner (TAU) Computational Models, Lecture 2 October 30, 2017 53 / 69

Regular expression from a DFA ( =) Easy for DFAs theat accepts finite languages, but more complicated for general DFA s. NFA: Each transition is labeled with a symbol or ε. Reads zero or one symbols. Takes matching transition, if any. Generalized non-deterministic finite automata (GNFA): Each transition is labeled with a regular expression. Reads zero or more symbols. Takes matching regular expression, if any. Example (board). GNFAs are natural generalization of NFAs. Iftach Haitner (TAU) Computational Models, Lecture 2 October 30, 2017 54 / 69

GNFA Formal Definition Let R(Σ) be the set of regular expressions over Σ. Definition 26 A generalized deterministic finite automaton (GNFA) is (Q, Σ, δ, q s, q a ) Q is a finite set of states Σ is the alphabet δ : (Q \ {q a }) (Q \ {q s }) R(Σ) is the transition function. q s Q is the start state q a Q is the unique accept state It is a special type of GNFA, but still it is easy to transform any DFA/NFA into this form. Iftach Haitner (TAU) Computational Models, Lecture 2 October 30, 2017 55 / 69

GNFA Model of Computation Definition 27 A GNFA G = (Q, Σ, δ, q s, q a ) accepts a string w Σ, if there exists parsing w = a 1 a 2 a k (Σ ) k, and r 0,..., r k Q, such that r 0 = q s r k = q a a i L(δ(r i 1, r i )), for every 0 < i k. Iftach Haitner (TAU) Computational Models, Lecture 2 October 30, 2017 56 / 69

The Transformation: DFA Regular Expression Strategy sequence of equivalent transformations Given a k-state DFA Transform into (k + 2)-state GNFA (how?) While GNFA has more than 2 states, transform it into equivalent GNFA with one fewer state Eventually reach 2-state GNFA (states are just start and accept). Label of single transition is the desired regular expression. Iftach Haitner (TAU) Computational Models, Lecture 2 October 30, 2017 57 / 69

Converting strategy ( =) 3-state DFA 5-state GNFA 4-state GNFA 3-state GNFA 2-state GNFA regular expression Iftach Haitner (TAU) Computational Models, Lecture 2 October 30, 2017 58 / 69

Removing a state We remove one state q r, and then repair the machine by altering regular expression of other transitions. R 4 q i q j R 1 R 3 q r R 2 q i R 1 R* 2 R 3 UR 4 q j Iftach Haitner (TAU) Computational Models, Lecture 2 October 30, 2017 59 / 69

Conversion - Example Iftach Haitner (TAU) Computational Models, Lecture 2 October 30, 2017 60 / 69

Conversion - Example Iftach Haitner (TAU) Computational Models, Lecture 2 October 30, 2017 61 / 69

Conversion - Example Iftach Haitner (TAU) Computational Models, Lecture 2 October 30, 2017 62 / 69

Conversion - Example Iftach Haitner (TAU) Computational Models, Lecture 2 October 30, 2017 63 / 69

The StateReduce and Convert algorithms Algorithm 28 (StateReduce) Input: a (k > 2)-state GNFA G = (Q, Σ, δ, q s, q a ). Select any state q r Q \ {q s, q a }. Let Q = Q \ {q r }. For any q i Q \ {q a } and q j Q \ {q s }, let R 1 = δ(q i, q r ), R 2 = δ(q r, q r ), R 3 = δ(q r, q j ) and R 4 = δ(q i, q j ). Define δ (q i, q j ) = (R 1 )(R 2 ) (R 3 ) (R 4 ). Return the resulting (k 1)-state GNFA G = (Q, Σ, δ, q s, q a ). Algorithm 29 (Convert) Input: a (k 2)-state GNFA G. If k = 2, return the regular expression labeling the only arrow of G. Otherwise, return Convert(StateReduce(G)). Iftach Haitner (TAU) Computational Models, Lecture 2 October 30, 2017 64 / 69

Correctness proof Claim 30 G and Convert(G) accept the same language. Proof: By induction on k the number of states of G. Basis. k = 2: Immediate by the definition of GFNA. Induction step: Assume claim for (k 1)-state GNFA, where k > 2, prove for k-state GNFA. Let G = StateReduce(G) (note that G has k 1 states), and let q r be the removed state. We prove (in a very high level) that L(G) = L(G ) (i.e., G and G accept the same language). We show w L(G) = w L(G ) w L(G ) = w L(G) Iftach Haitner (TAU) Computational Models, Lecture 2 October 30, 2017 65 / 69

w L(G) = w L(G ) Let w L(G) and let p = q s,..., q a be (a possible) path of states traversed by G on w. If q r / p, then G accepts w (the new regular expression on each edge of G contains the old regular expression in the union part.) If p = q S,..., q i, q r, q j,..., q a, the regular expression (R i,r )(R r,r ) (R r,j ) linking q i and q j in G, causes G to accept w. Hence, w L(G ). Iftach Haitner (TAU) Computational Models, Lecture 2 October 30, 2017 66 / 69

w L(G ) = w L(G) Let q s = q j0, q j1,..., q jn = q a be be (a possible) path of states traversed by G on w. parsing w = w 1,..., w n s.t. w i L(δ G (q ji 1, q ji )). For every i [n]: δ G (q ji 1, q Ji ) = ( δ G (q ji 1, q r )δ G (q r, q r ) δ G (q r, q ji ) ) (δ G (q ji 1, q Ji ). Hence for all i [n], the word w i corresponds to possible traverse from q i 1 to q i in G. Hence, w L(G ) = w L(G). Iftach Haitner (TAU) Computational Models, Lecture 2 October 30, 2017 67 / 69

Summing it up We proved L(G) = L(G ). Hence, G and (the regular expression) Convert(G) accept the same language. Thus, we proved: Every regular language can be described by a regular expression. Iftach Haitner (TAU) Computational Models, Lecture 2 October 30, 2017 68 / 69

Summary Non-Deterministic Automata (with ε-moves) Equivalence to DFA Closure properties union concatenation star Regular expressions. Equivalence to DFA Iftach Haitner (TAU) Computational Models, Lecture 2 October 30, 2017 69 / 69