CS 322 D: Formal languages and automata theory

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

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

CSC236 Week 11. Larry Zhang

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

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

CS 455/555: Finite automata

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

Deterministic Finite Automaton (DFA)

CMSC 330: Organization of Programming Languages

Extended transition function of a DFA

CMSC 330: Organization of Programming Languages. Theory of Regular Expressions Finite Automata

(Refer Slide Time: 0:21)

Nondeterministic Finite Automata

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

COM364 Automata Theory Lecture Note 2 - Nondeterminism

Nondeterministic Finite Automata

cse303 ELEMENTS OF THE THEORY OF COMPUTATION Professor Anita Wasilewska

CSE 105 Theory of Computation Professor Jeanne Ferrante

CS 154 Formal Languages and Computability Assignment #2 Solutions

Chapter Five: Nondeterministic Finite Automata

Closure under the Regular Operations

Deterministic Finite Automata (DFAs)

Equivalence of DFAs and NFAs

Lecture 3: Nondeterministic Finite Automata

CS 154, Lecture 3: DFA NFA, Regular Expressions

Chapter 6: NFA Applications

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

FORMAL LANGUAGES, AUTOMATA AND COMPUTABILITY

Nondeterministic Finite Automata. Nondeterminism Subset Construction

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

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

CS 482 Problem Set 2 Solutions Dan FitzGerald, dpf7

Deterministic Finite Automata (DFAs)

CS 154. Finite Automata, Nondeterminism, Regular Expressions

Chap. 1.2 NonDeterministic Finite Automata (NFA)

Figure 1: NFA N. Figure 2: Equivalent DFA N obtained through function nfa2dfa

Non-deterministic Finite Automata (NFAs)

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

Sri vidya college of engineering and technology

PS2 - Comments. University of Virginia - cs3102: Theory of Computation Spring 2010

Automata: a short introduction

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

September 7, Formal Definition of a Nondeterministic Finite Automaton

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

Automata and Computability. Solutions to Exercises

Decision, Computation and Language

Nondeterminism and Epsilon Transitions

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

Theory of Computation (I) Yijia Chen Fudan University

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

Automata and Computability. Solutions to Exercises

Deterministic Finite Automata (DFAs)

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

FINITE STATE MACHINES (AUTOMATA)

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

TDDD65 Introduction to the Theory of Computation

Intro to Theory of Computation

Nondeterminism. September 7, Nondeterminism

Finite-State Machines (Automata) lecture 12

False. They are the same language.

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

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

Nondeterministic Finite Automata

CONCATENATION AND KLEENE STAR ON DETERMINISTIC FINITE AUTOMATA

CS21 Decidability and Tractability

Automata and Languages

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

Automata and Formal Languages - CM0081 Non-Deterministic Finite Automata

Regular Expressions and Language Properties

Computational Models #1

CS21 Decidability and Tractability

Pushdown automata. Twan van Laarhoven. Institute for Computing and Information Sciences Intelligent Systems Radboud University Nijmegen

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

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

Finite Automata. Finite Automata

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

Uses of finite automata

NFA and regex. the Boolean algebra of languages. regular expressions. Informatics 1 School of Informatics, University of Edinburgh

UNIT-III REGULAR LANGUAGES

Finite Automata and Formal Languages

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

Lecture 17: Language Recognition

Equivalence of Regular Expressions and FSMs

Deterministic Finite Automata (DFAs)

Theory of Computation

Fooling Sets and. Lecture 5

Theory of Computation

Homework 02 Solution updated

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

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

Lecture 1: Finite State Automaton

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,

Nondeterministic finite automata

Theory of Computation

CSE 105 THEORY OF COMPUTATION

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

Nondeterministic Finite Automata and Regular Expressions

Name: Student ID: Instructions:

Pushdown Automata (2015/11/23)

Compiler Design. Spring Lexical Analysis. Sample Exercises and Solutions. Prof. Pedro C. Diniz

Transcription:

CS 322 D: Formal languages and automata theory Tutorial NFA DFA Regular Expression T. Najla Arfawi 2 nd Term - 26

Finite Automata Finite Automata. Q - States 2. S - Alphabets 3. d - Transitions 4. q - Initial state 5. F - Final state(s) / Accepting state(s) 2 T. Najla Arfawi - 2nd Term 26

Deterministic Finite Automata There must be a transition corresponding to each alphabet at each state 3 T. Najla Arfawi - 2nd Term 26

Nondeterministic Finite Automata Can be at multiple states at the same time Can go to multiple states on one alphabet May have no transitions on an alphabet (die) Every NFA can be converted into a DFA for the same language. 4 T. Najla Arfawi - 2nd Term 26

NFA DFA in two easy steps ➊ Eliminate e-transitions ➋ Convert simplified NFA We do this first 5 T. Najla Arfawi - 2nd Term 26

NFA DFA: states, NFA: q q q 2 DFA: {q } {q, q } {q } {q, q 2 } {q, q, q 2 } {q 2 } {q, q 2 } DFA has a state for every subset of NFA states 6 T. Najla Arfawi - 2nd Term 26

NFA DFA: transitions, NFA: q q q 2 DFA:, {q } {q } {q, q } {q, q 2 } {q, q, q 2 } {q 2 } {q, q 2 } 7 T. Najla Arfawi - 2nd Term 26

NFA DFA: accepting states, NFA: q q q 2 DFA:, {q } {q } {q, q } {q, q 2 } {q, q, q 2 } {q 2 } {q, q 2 } NFA accepts if it contains a DFA final state 8 T. Najla Arfawi - 2nd Term 26

NFA DFA: dead state elimination, NFA: q q q 2 DFA:, {q } {q } {q, q } {q, q 2 } {q, q, q 2 } {q 2 } {q, q 2 } At the end, you can eliminate the unreachable states 9 T. Najla Arfawi - 2nd Term 26

General method NFA DFA states q, q,, q n q }, {q }, {q,q },, {q,,q n } initial state q q } one for each subset of states in the NFA transitions d d ({q i,,q ik }, a) = accepting states d(q i, a) d(q ik, a) F Q F = {S: S contains some state in F} T. Najla Arfawi - 2nd Term 26

NFA DFA in two easy steps ➊ Eliminate e-transitions ➋ Convert simplified NFA T. Najla Arfawi - 2nd Term 26

Eliminating e-transitions NFA: e, q q q 2 e NFA without es: q q q 2 {q, q, q 2 } {q, q, q 2 } {q, q 2 } Accepting states: q 2, q, q 2 T. Najla Arfawi - 2nd Term 26

Eliminating e-transitions NFA: e, q q q 2 e new NFA: q q q 2 {q, q, q 2 } {q, q, q 2 } {q, q 2 } 3 T. Najla Arfawi - 2nd Term 26, q q q 2,

Eliminating e-transitions Paths with es are replaced by a single transition q e 5 q a q e 2 q e q 3 q 3 e q 4 a e q5 q a 5 q 3 States that can reach final state by e are all accepting q 3 a 4 T. Najla Arfawi - 2nd Term 26 q 9 q 7 e q 3 e q 2 e

Regular Expression The symbols and e are regular expressions Every a in S is a regular expression If R and S are regular expressions, so are RUS, RS and R* Remember for R*, * could be 5 T. Najla Arfawi - 2nd Term 26

Regular Languages Languages that can be represented by a DFA / NFA / RE DFA = NFA = RE (in term of equivalence) When asked if a language L is regular, represent L using a DFA / NFA / RE 6 T. Najla Arfawi - 2nd Term 26

Checklist. Always pay attention to S 2. For DFA, out degree of each state must be equal S e.g. S = {,, 2}, L = {w: w begins with } 3. For NFA, pay attention to e-transitions 4. Remember to specify the initial state 5. Make sure you have considered the input e e.g. L = {w: w contains even number of } 7 T. Najla Arfawi - 2nd Term 26

Exercise S = {, } L = {w: w is non-empty and the sum of the digits in w is divisible by 5} 8 T. Najla Arfawi - 2nd Term 26

Idea Need to keep track of the sum of digits Arrive at the state q k when sum of digits = k q q q 2 q 3 q 4 q 5 9 T. Najla Arfawi - 2nd Term 26

Idea Need to keep track of the sum of digits Arrive at the state q k when sum of digits = k k divisible by 5 iff k mod 5 = Keep track of the remainder instead q q q 2 q 3 q 4 2 T. Najla Arfawi - 2nd Term 26

Answer Handle the input e q s q q q 2 q 3 q 4 2 T. Najla Arfawi - 2nd Term 26

Converting an NFA to a DFA Eliminate e q q e q 2 for each state i for each alphabet x for each state j q q q 2 if i can reach j using one x and e(s) add an edge from i to j with label x e q q q 2 q q q 2 22 e q q q 2 T. Najla Arfawi - 2nd Term 26 e q 3 q q q 2 q 3

Converting an NFA to a DFA Eliminate e Remember to consider the case when the initial state can reach accepting states on e(s) e q q q 2 q q q 2 23 T. Najla Arfawi - 2nd Term 26

Converting an NFA to a DFA Every possible subsets of Q is a state in the DFA Going to multiple states at the same time in the NFA = going to a subset of Q, which is now a state in the DFA q q q 2 {q } {q, q 2 } 24 T. Najla Arfawi - 2nd Term 26

Exercise q e q, q 2 25 T. Najla Arfawi - 2nd Term 26

Eliminating e-transitions q can reach q on (q -> q -> q ) q can reach q on (q -> q ) q can reach q on (q -> q ) q can reach q on (q -> q ) The rest of the transitions remain unchanged q e q q q, q q 2, q q, q q 2 q q, q 2 26 q 2 T. Najla Arfawi - 2nd Term 26

Eliminating e-transitions q is the initial state q can reach an accepting state on e (i.e. q ) therefore q is also an accepting state q q, q q 2 q q, q q e q q q q 2 q q, q 2,, 27 T. Najla Arfawi - 2nd Term 26 q 2 q 2

Converting NFA to DFA {q } {q, q } {q 2 } {q } {q, q } {q 2 } {q } {q, q 2 } q q, q 2 28 T. Najla Arfawi - 2nd Term 26

Converting NFA to DFA Starting from initial state {q } {q } {q, q } {q 2 } {q } {q, q } {q 2 } {q } {q, q 2 } {q } {q, q } {q 2 } 29 T. Najla Arfawi - 2nd Term 26

Converting NFA to DFA Check if every state has S outgoing transitions No Fix {q 2 } and {q, q } {q, q } goes to {q, q } {q, q } = {q, q } on {q, q } goes to {q 2 } = {q 2 } on {q } {q, q } {q } {q, q } {q 2 } {q } {q, q } {q 2 } {q } {q, q 2 } {q } {q 2 } 3 T. Najla Arfawi - 2nd Term 26 {q, q 2 }

Converting NFA to DFA Check if every state has S outgoing transitions No Fix {q } and {q, q 2 } {q, q 2 } goes to {q, q } {q } = {q, q } on {q, q 2 } goes to {q, q 2 } = {q, q 2 } on {q } {q, q } {q 2 } {q } {q, q } {q 2 } {q } {q, q 2 } {q } {q, q } {q 2 } {q } 3 T. Najla Arfawi - 2nd Term 26 {q, q 2 }

Converting NFA to DFA Check if every state has S outgoing transitions No Fix = die {q } {q, q } {q } {q, q } {q 2 } {q } {q, q } {q } {q 2 } {q } {q, q 2 } {q, 2 } 32 T. Najla Arfawi - 2nd Term 26 {q, q 2 }

Converting NFA to DFA Check if every state has S outgoing transitions Yes Accepting states = states that contain q or q q q, q 2 {q } {q, q } {q 2 } {q } {q, q } {q 2 } {q } {q, q 2 } {q } {q, q } {q 2 } {q }, 33 T. Najla Arfawi - 2nd Term 26 {q, q 2 }

Converting NFA to DFA Done q e q {q } {q, q }, q 2 {q 2 } {q }, {q, q 2 } 34 T. Najla Arfawi - 2nd Term 26

Regular expressions 35 T. Najla Arfawi - 2nd Term 26

String concatenation s = abb t = bab st st = abbbab abbbab ts ts = bababb bababb ss ss = abbabb abbabb sst sst = abbabbbab abbabbbab s = x x n t = y y m st = x x n y y m 36 T. Najla Arfawi - 2nd Term 26

Operations on languages The concatenation of languages L and L 2 is The n-th power of L n is The union of L and L 2 is L L 2 = {st: s L, t L 2 } L n = {s s 2...s n : s, s 2,..., s n L} L L 2 = {s: s L or s L 2 } 37 T. Najla Arfawi - 2nd Term 26

Operations on languages The star of L are all strings made up of zero or more chunks from L: L * = L L L 2 Example: L = {, }, L 2 = {e,,,, }. What is L * and L 2*? 38 T. Najla Arfawi - 2nd Term 26

Example L = {, } L * : is in L * is not in L * L = {e} L = {, } L 2 = {,,, } L 3 = {,,,,,,, } is not in L * L * are all strings that start with and do not contain consecutive s (plus the empty string) 39 T. Najla Arfawi - 2nd Term 26

Example L 2 = {e,,,, } any number of s L 2 = {e} L 2 * = L 2 L 2 L 2 2 L 2 = L 2 L 2 2 = L 2 L 2 n = L 2 (n ) = {e} L 2 L 2 2 = L 2 L 2 * = L 2 4 T. Najla Arfawi - 2nd Term 26

Combining languages We can construct languages by starting with simple ones, like {}, {} and combining them {}({} {})* ( + )* all strings that start with ({}{}*) ({}{}*) * + * followed by any number of s, or followed by any number of s 4 T. Najla Arfawi - 2nd Term 26

Regular expressions A regular expression over S is an expression formed using the following rules: The symbols and e are regular expressions Every a in S is a regular expression If R and S are regular expressions, so are R+S, RS and R*. + is union operator U A language is regular if it is represented by a regular expression 42 T. Najla Arfawi - 2nd Term 26

Analyzing regular expressions S = {, } * = (*) = {,,,, } followed by any number of s (*)() = {,,,, } followed by any number of s and then 43 T. Najla Arfawi - 2nd Term 26

Analyzing regular expressions + = {, } strings of length (+)* = {e,,,,,,, } any string (+)* any string that ends in (+)*(+)* any string that contatins the pattern 44 T. Najla Arfawi - 2nd Term 26

Analyzing regular expressions ((+)(+))*+((+)(+)(+))* all strings whose length is even or a mutliple of 3 = strings of length, 2, 3, 4, 6, 8, 9,, 2,... ((+)(+))* strings of even length (+)(+) strings of length 2 ((+)(+)(+))* strings of length a multiple of 3 (+)(+)(+) strings of length 3 45 T. Najla Arfawi - 2nd Term 26

Analyzing regular expressions ((+)(+)+(+)(+)(+))* strings that can be broken in blocks, where each block has length 2 or 3 (+)(+)+(+)(+)(+) strings of length 2 or 3 (+)(+) (+)(+)(+) strings of length 2 strings of length 3 46 T. Najla Arfawi - 2nd Term 26

Analyzing regular expressions ((+)(+)+(+)(+)(+))* strings that can be broken in blocks, where each block has length 2 or 3 e this includes all strings except those of length ((+)(+)+(+)(+)(+))* = all strings except and 47 T. Najla Arfawi - 2nd Term 26

Analyzing regular expressions (++)*(e++) ends in at most two s there can be at most two s between consecutive s there are never three consecutive s Guess: (++)*(e++) = {x: x does not contain } e 48 T. Najla Arfawi - 2nd Term 26

Writing regular expressions Write a regular expression for all strings with two consecutive s. S = {, } (anything) (anything else) (+)*(+)* 49 T. Najla Arfawi - 2nd Term 26

Writing regular expressions Write a regular expression for all strings that do not contain two consecutive s. S = {, } some s at every maybe a at the end the beginning followed by one or more s... every middle followed by one or more s... and at most one in the last block (*) (e + ) *(*)*(e + ) 5 T. Najla Arfawi - 2nd Term 26

Writing regular expressions Write a regular expression for all strings with an even number of s. S = {, } even number of zeros = (two zeros)* two zeros = *** (***)* 5 T. Najla Arfawi - 2nd Term 26