CS 301 Lecture 08 Regular languages recap Stephen Checkoway - - PowerPoint PPT Presentation

cs 301
SMART_READER_LITE
LIVE PREVIEW

CS 301 Lecture 08 Regular languages recap Stephen Checkoway - - PowerPoint PPT Presentation

CS 301 Lecture 08 Regular languages recap Stephen Checkoway February 12, 2018 1 / 39 Symbols, alphabets, strings, and languages 1 Alphabets are sets of symbols 2 Strings over an alphabet are sequences of symbols from the alphabet 3


slide-1
SLIDE 1

CS 301

Lecture 08 – Regular languages recap Stephen Checkoway February 12, 2018

1 / 39

slide-2
SLIDE 2

Symbols, alphabets, strings, and languages

1 Alphabets are sets of symbols 2 Strings over an alphabet are sequences of symbols from the alphabet 3 Languages over an alphabet are sets strings over the alphabet

2 / 39

slide-3
SLIDE 3

Question 1

Can an alphabet contain zero symbols?

3 / 39

slide-4
SLIDE 4

Question 1

Can an alphabet contain zero symbols?

  • No. Alphabets must have at least one symbol

3 / 39

slide-5
SLIDE 5

Question 2

Can an alphabet contain infinitely many symbols?

4 / 39

slide-6
SLIDE 6

Question 2

Can an alphabet contain infinitely many symbols?

  • No. Alphabets must be finite

4 / 39

slide-7
SLIDE 7

Question 3

Can a string contain zero symbols?

5 / 39

slide-8
SLIDE 8

Question 3

Can a string contain zero symbols?

  • Yes. ε is a perfectly reasonable string

5 / 39

slide-9
SLIDE 9

Question 4

Can a string contain infinitely many symbols?

6 / 39

slide-10
SLIDE 10

Question 4

Can a string contain infinitely many symbols?

  • No. Strings must have finite length

6 / 39

slide-11
SLIDE 11

Question 5

Can a language contain zero strings?

7 / 39

slide-12
SLIDE 12

Question 5

Can a language contain zero strings?

  • Yes. ∅ is the empty language

7 / 39

slide-13
SLIDE 13

Question 6

Can a language contain infinitely many strings?

8 / 39

slide-14
SLIDE 14

Question 6

Can a language contain infinitely many strings?

  • Yes. Most languages contain infinitely many strings.

(For a given alphabet, there are countably-many finite languages but uncountably-many nonfinite languages)

8 / 39

slide-15
SLIDE 15

Deterministic finite automata

DFAs are five-tuples M = (Q, Σ, δ, q0, F) where

  • Q is the set of states
  • Σ is the alphabet
  • δ is the transition function
  • q0 is the start state
  • F is the set of accepting states

9 / 39

slide-16
SLIDE 16

Question 7

Can Q be the empty set?

10 / 39

slide-17
SLIDE 17

Question 7

Can Q be the empty set?

  • No. Every DFA contains at least a start state q0

10 / 39

slide-18
SLIDE 18

Question 8

Can Q contain infinitely many states?

11 / 39

slide-19
SLIDE 19

Question 8

Can Q contain infinitely many states?

  • No. These are finite automata

11 / 39

slide-20
SLIDE 20

Question 9

Can F be the empty set?

12 / 39

slide-21
SLIDE 21

Question 9

Can F be the empty set?

  • Yes. A DFA without any accepting states rejects every string

12 / 39

slide-22
SLIDE 22

Question 10

Can F be all of Q?

13 / 39

slide-23
SLIDE 23

Question 10

Can F be all of Q?

  • Yes. A DFA where every state is an accepting state accepts every string

13 / 39

slide-24
SLIDE 24

Question 11

Can M have multiple start states?

14 / 39

slide-25
SLIDE 25

Question 11

Can M have multiple start states?

  • No. DFAs have a single start state

14 / 39

slide-26
SLIDE 26

Question 12

Can a DFA have a state that’s not reachable from any other state? q0 q1 q2 1 1 1

15 / 39

slide-27
SLIDE 27

Question 12

Can a DFA have a state that’s not reachable from any other state? q0 q1 q2 1 1 1

  • Yes. Nothing in the mathematical definition of a DFA forbids that and it simplifies

conversions to DFA from other machines

15 / 39

slide-28
SLIDE 28

Question 13

Can a DFA have a state without any transitions from it?

16 / 39

slide-29
SLIDE 29

Question 13

Can a DFA have a state without any transitions from it?

  • No. The transition function δ ∶ Q × Σ → Q requires every state have a transition for

every symbol in the alphabet

16 / 39

slide-30
SLIDE 30

Recognition and acceptance

  • A DFA accepts a string when the sequence of states it goes through when it runs
  • n the string ends in an accepting state
  • A DFA recognizes a language when it accepts every string in the language and,

crucially, rejects every string not in the language

17 / 39

slide-31
SLIDE 31

Question 14

Does this DFA recognize the string 1101? q0 q1 q2 1 1 1

18 / 39

slide-32
SLIDE 32

Question 14

Does this DFA recognize the string 1101? q0 q1 q2 1 1 1

  • No. The question doesn’t even make sense. DFAs recognize languages, not strings

18 / 39

slide-33
SLIDE 33

Question 15

Consider the language A = {w ∣ w ∈ {0, 1}∗ ends in 11}. The following DFA accepts every string in A. Does the DFA recognize A? q0 q1 q2 1 1 1

19 / 39

slide-34
SLIDE 34

Question 15

Consider the language A = {w ∣ w ∈ {0, 1}∗ ends in 11}. The following DFA accepts every string in A. Does the DFA recognize A? q0 q1 q2 1 1 1

  • No. The DFA accepts string 1 which is not in A

19 / 39

slide-35
SLIDE 35

Two methods of proving that a DFA recognizes a language

If we want to show that DFA M recognizes some language L, we have two options

1 Show that M accepts every string in L and rejects every string not in L 2 Show that M accepts every string in L and every string accepted by the DFA is in

L

20 / 39

slide-36
SLIDE 36

Nondeterministic finite automata

NFAs are five-tuples N = (Q, Σ, δ, q0, F) where

  • Q is the set of states
  • Σ is the alphabet
  • δ is the transition function
  • q0 is the start state
  • F is the set of accepting states

21 / 39

slide-37
SLIDE 37

Question 16

NFAs add two capabilities to DFAs

1 The ability to transition on an input symbol to zero or more states 2 The ability to transition on no input at all (ε-transitions)

For an NFA N = (Q, Σ, δ, q0, F), is ε ∈ Σ?

22 / 39

slide-38
SLIDE 38

Question 16

NFAs add two capabilities to DFAs

1 The ability to transition on an input symbol to zero or more states 2 The ability to transition on no input at all (ε-transitions)

For an NFA N = (Q, Σ, δ, q0, F), is ε ∈ Σ?

  • No. Remember, the transition function is δ ∶ Q × Σε → P(Q) where Σε = Σ ∪ {ε}

22 / 39

slide-39
SLIDE 39

Question 17

Can an NFA have multiple start states?

23 / 39

slide-40
SLIDE 40

Question 17

Can an NFA have multiple start states?

  • No. Still just the one

23 / 39

slide-41
SLIDE 41

Question 18

Consider a new type of finite automaton called a multinondeterministic finite automaton (I just made this name up) which is a five tuple M = (Q, Σ, δ, I, F) where I is a set of initial states but is otherwise similar to an NFA. Are MNFAs more powerful (meaning, can the class of MNFAs recognize more languages) than NFAs?

24 / 39

slide-42
SLIDE 42

Question 18

Consider a new type of finite automaton called a multinondeterministic finite automaton (I just made this name up) which is a five tuple M = (Q, Σ, δ, I, F) where I is a set of initial states but is otherwise similar to an NFA. Are MNFAs more powerful (meaning, can the class of MNFAs recognize more languages) than NFAs?

  • No. We can build an equivalent NFA by adding a new state which is the only start

state and adding ε-transitions to the states in I.

24 / 39

slide-43
SLIDE 43

Regular expressions

Regular expressions are defined recursively with three base cases

  • ∅ generates the empty language ∅
  • ε generates the language {ε}
  • t for some t ∈ Σ generates the language {t}

and three recursive cases

  • R1R2 generates L(R1) ◦ L(R2)
  • R1 ∣ R2 generates L(R1) ∪ L(R2)
  • R∗ generates L(R)∗

25 / 39

slide-44
SLIDE 44

Regularity

Four equivalent statements about a language A

1 A is regular 2 Some DFA recognizes A 3 Some NFA recognizes A 4 Some regular expression generates A

26 / 39

slide-45
SLIDE 45

Converting between DFA, NFA, regex

DFA M = (Q1, Σ, δ1, q1, F1) NFA N = (Q2, Σ, δ2, q2, F2) Regular Expression

δ2(q, t) = {δ1(q, t)} Construct GNFA and remove states Q1 = P(Q2) Construct GNFA and remove states Construct NFAs for base cases and combine

27 / 39

slide-46
SLIDE 46

Converting from a regular expression to an NFA

Construct it step by step

1 Start with the base cases 2 Then construct NFAs for increasingly larger expressions by combining NFAs for

smaller expressions

28 / 39

slide-47
SLIDE 47

Example

Construct an NFA corresponding to the regular expression (aba ∣ aa)∗

29 / 39

slide-48
SLIDE 48

Converting from an NFA to a DFA

Given an NFA N = (Q, Σ, δ, q0, F), we can construct an equivalent DFA M = (Q′, Σ, δ′, q′

0, F ′) 1 Each state of M represents a set of states of N 2 Each transition of M from state S ⊆ Q on input t is to the state representing all

  • f the states of N reachable from some state in S by following t and then 0 or

more ε-transitions

3 The start state of M is the state that represents all of the states of N reachable

from q0 by following 0 or more ε-transitions

4 The set of accepting states of M are those representing a set of states of N that

contains at least one accepting state of N Formally,

1 Q′ = P(Q) 2 δ′(S, t) = ⋃q∈S E(δ(q, t)) 3 q′ 0 = E({q0}) 4 F ′ = {S ∣ S ⊆ Q and S ∩ F ≠ ∅}

The function E(⋅) is the epsilon closure.

30 / 39

slide-49
SLIDE 49

Example

Let’s simplify our NFA for the language (aba ∣ aa)∗.

31 / 39

slide-50
SLIDE 50

Example

Let’s simplify our NFA for the language (aba ∣ aa)∗. 1 2 3 a b,ε a Now let’s convert it to a DFA

31 / 39

slide-51
SLIDE 51

Converting from a DFA or an NFA to a regular expression

1 Create a GNFA by adding a start state and an accepting state 2 Add ε-transition from the new start state to the old start sate 3 Add ε-transitions from the old accepting states to the new accepting state 4 Convert each transition to a regex (i.e., transitions labeled a, b become a ∣ b) 5 Remove each state, updating transitions from

q r s R1 R4 R2 R3 q r R1 R4 R3 R2 to q s R1R∗

2R3 ∣ R4

q R1R∗

2R3 ∣ R4

32 / 39

slide-52
SLIDE 52

Example

Let’s convert our DFA to a regular expression {1} {2, 3} ∅ {3} a b a b a b a,b

33 / 39

slide-53
SLIDE 53

Cartesian product construction

We can use DFAs directly to show that the class of regular languages is closed under union and intersection Let M1 = (Q1, Σ, δ1, q1, F1) M2 = (Q2, Σ, δ2, q2, F2) and build M = (Q, Σ, δ, q0, F) Q = Q1 × Q2 δ((q, r), t) = (δ1(q, t), δ2(r, t)) q0 = (q1, q2) For union, let F = (F1 × Q2) ∪ (Q1 × F2) For intersection, let F = F1 × F2

34 / 39

slide-54
SLIDE 54

Pumping lemma

Theorem

Pumping lemma for regular languages For every regular language A, there exists an integer p > 0 called the pumping length such that for every w ∈ A there exist strings x, y, and z with w = xyz such that

1 xyiz ∈ A for all i ≥ 0 2 ∣y∣ > 0 3 ∣xy∣ ≤ p.

35 / 39

slide-55
SLIDE 55

A two-player game

Player One (∃) Player Two (∀) Claims A is pumpable with p.l. p A, p − − − − − − − − − − − − − − − → Picks w ∈ A s.t. ∣w∣ ≥ p w ← − − − − − − − − − − − − − − − Picks x, y, z s.t. w = xyz x, y, z − − − − − − − − − − − − − − − → Checks 3 conditions Player One “wins” if

1 xyiz ∈ A for all i ≥ 0 2 ∣y∣ > 0 3 ∣xy∣ ≤ p

Can play as either Player One or Two

  • To show that A is pumpable, play as Player One

You must consider all possible w and pick x, y, and z

  • To show that A is not pumpable, play as Player Two

You must pick w and consider all possible x, y, and z

36 / 39

slide-56
SLIDE 56

Proving that a language isn’t regular

Three options

1 Assume that it is regular and show that it violates the pumping lemma 2 Assume that it is regular and apply operations on languages that preserve

regularity, arrive at a contradiction because the result isn’t regular

3 First apply some operations on languages, then use the pumping lemma

37 / 39

slide-57
SLIDE 57

Closure properties of regular languages

The class of regular languages is closed under

  • Union
  • Concatenation
  • Kleene star
  • Intersection
  • Complement
  • Reversal
  • Difference (we haven’t proved this)
  • Prefix
  • Suffix (we haven’t proved this)
  • Left quotient by a string
  • Right quotient by a string (we haven’t proved this)
  • Left/right quotient by a language (we haven’t proved this)
  • . . .

38 / 39

slide-58
SLIDE 58

Closure properties of nonregular languages

The class of nonregular languages is closed under

  • Complement
  • Reversal

The class of nonregular languages is not closed under

  • Union
  • Concatenation (we haven’t proved this)
  • Kleene star (we haven’t proved this)
  • Intersection
  • Prefix (we haven’t proved this)
  • Suffix (we haven’t proved this)
  • Left/right quotient by a string/language (we haven’t proved this)

39 / 39