1 formal languages, automata and computability 15-453 (for next time: read chapter 1.3 of the book)

56
1 FORMAL LANGUAGES, AUTOMATA AND COMPUTABILITY 15-453 (For next time: Read Chapter 1.3 of the book

Post on 19-Dec-2015

216 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: 1 FORMAL LANGUAGES, AUTOMATA AND COMPUTABILITY 15-453 (For next time: Read Chapter 1.3 of the book)

1

FORMAL LANGUAGES, AUTOMATA AND COMPUTABILITY

15-453

(For next time: Read Chapter 1.3 of the book)

Page 2: 1 FORMAL LANGUAGES, AUTOMATA AND COMPUTABILITY 15-453 (For next time: Read Chapter 1.3 of the book)

2

1

0

0

q1

q2

q3

q4

ε

Page 3: 1 FORMAL LANGUAGES, AUTOMATA AND COMPUTABILITY 15-453 (For next time: Read Chapter 1.3 of the book)

3

Q is the set of states

Σ is the alphabet

: Q Σε → 2Q is the transition function

Q0 Q is the set of start states

F Q is the set of accept states

A non-deterministic finite automaton (NFA) is a 5-tuple N = (Q, Σ, , Q0, F)

2Q is the set of subsets of Q and Σε = Σ {ε}

Page 4: 1 FORMAL LANGUAGES, AUTOMATA AND COMPUTABILITY 15-453 (For next time: Read Chapter 1.3 of the book)

4

1

0

0

q1

q2

q3

q4

N = (Q, Σ, , Q0, F)

Q = {q1, q2, q3, q4}

Σ = {0,1}

Q0 = {q1, q2}

F = {q4} Q

ε

0 1 ε

q1

q2

q3

q4

Page 5: 1 FORMAL LANGUAGES, AUTOMATA AND COMPUTABILITY 15-453 (For next time: Read Chapter 1.3 of the book)

5

1

0

0

q1

q2

q3

q4

N = (Q, Σ, , Q0, F)

Q = {q1, q2, q3, q4}

Σ = {0,1}

Q0 = {q1, q2}

F = {q4} Q

0 1 ε

q1 {q3}

q2 {q4}

q3 {q4} {q2}

q4

ε

Page 6: 1 FORMAL LANGUAGES, AUTOMATA AND COMPUTABILITY 15-453 (For next time: Read Chapter 1.3 of the book)

6

1

0

0

q1

q2

q3

q4

N = (Q, Σ, , Q0, F)

Q = {q1, q2, q3, q4}

Σ = {0,1}

Q0 = {q1, q2}

F = {q4} Q

ε, 0

0 1 ε

q1 {q3}

q2 {q4}

q3 {q2,q4} {q2}

q4

00 L(N)?

01 L(N)?

Page 7: 1 FORMAL LANGUAGES, AUTOMATA AND COMPUTABILITY 15-453 (For next time: Read Chapter 1.3 of the book)

7

Let w Σ* and suppose w can be written as w1... wn where wi Σε (ε is viewed as representing

the empty string)

Then N accepts w if there are r0, r1, ..., rn Q such that

1. r0 Q0

2. ri+1 (ri, wi+1 ) for i = 0, ..., n-1, and 3. rn F

A language L is recognized by an NFA Nif L = L (N).

L(N) = the language of machine N= set of all strings machine N accepts

Page 8: 1 FORMAL LANGUAGES, AUTOMATA AND COMPUTABILITY 15-453 (For next time: Read Chapter 1.3 of the book)

8

1

0

0

q1

q2

q3

q4

N = (Q, Σ, , Q0, F)

Q = {q1, q2, q3, q4}

Σ = {0,1}

Q0 = {q1, q2}

F = {q4} Q

ε, 0

0 1 ε

q1 {q3}

q2 {q4}

q3 {q2,q4} {q2}

q4

00 L(N)?

01 L(N)?

Page 9: 1 FORMAL LANGUAGES, AUTOMATA AND COMPUTABILITY 15-453 (For next time: Read Chapter 1.3 of the book)

9

Q = 2Q

: Q Σ → Q(R,) = ε( (r,) )rR

q0 = ε(Q0)

F = { R Q | f R for some f F }

FROM NFA TO DFAInput: N = (Q, Σ, , Q0, F)

Output: M = (Q, Σ, , q0, F)

*

For R Q, the ε-closure of R, ε(R) = {q that can be reached from some r R by traveling along zero or more ε arrows},

*

Page 10: 1 FORMAL LANGUAGES, AUTOMATA AND COMPUTABILITY 15-453 (For next time: Read Chapter 1.3 of the book)

10

a

a , b

a

2 3

1

b

ε

Given: NFA N = ( {1,2,3}, {a.b}, , {1}, {1} )

Construct: equivalent DFA M

ε({1}) = {1,3}

N

Page 11: 1 FORMAL LANGUAGES, AUTOMATA AND COMPUTABILITY 15-453 (For next time: Read Chapter 1.3 of the book)

11

a

a, b

a

2 3

1

Given: NFA N = ( {1,2,3}, {a,b}, , {1}, {1} )

Construct: equivalent DFA M

ε({1}) = {1,3}

N

N = ( Q, Σ, , Q0, F )

= (Q, Σ, , q0, F)

a b

{1} {2}

{2} {2,3} {3}

{3} {1,3} {1,2} {2,3} {2,3}

{1,3} {1,3} {2}

{2,3} {1,2,3} {3}{1,2,3} {1,2,3} {2,3}

q0 =

Page 12: 1 FORMAL LANGUAGES, AUTOMATA AND COMPUTABILITY 15-453 (For next time: Read Chapter 1.3 of the book)

12

a

a, b

a

2 3

1

Given: NFA N = ( {1,2,3}, {a,b}, , {1}, {1} )

Construct: equivalent DFA M

ε({1}) = {1,3}

N

N = ( Q, Σ, , Q0, F )

= (Q, Σ, , q0, F)

a b

{1} {2}

{2} {2,3} {3}

{3} {1,3} {1,2} {2,3} {2,3}

{1,3} {1,3} {2}

{2,3} {1,2,3} {3}{1,2,3} {1,2,3} {2,3}

q0 =

Page 13: 1 FORMAL LANGUAGES, AUTOMATA AND COMPUTABILITY 15-453 (For next time: Read Chapter 1.3 of the book)

13

REGULAR LANGUAGES CLOSED UNDER STAR

Let L be a regular language and M be a DFA for L

We construct an NFA N that recognizes L*

00,1

00

1

1

1

ε

ε

ε

Page 14: 1 FORMAL LANGUAGES, AUTOMATA AND COMPUTABILITY 15-453 (For next time: Read Chapter 1.3 of the book)

14

Formally:

Input: M = (Q, Σ, , q1, F)

Output: N = (Q, Σ, , {q0}, F)

Q = Q {q0}

F = F {q0}

(q,a) =

{(q,a)}

{q1}

{q1}

if q Q and a ≠ ε

if q F and a = ε

if q = q0 and a = ε

if q = q0 and a ≠ ε

else

DFA

NFA

Page 15: 1 FORMAL LANGUAGES, AUTOMATA AND COMPUTABILITY 15-453 (For next time: Read Chapter 1.3 of the book)

15

Show: L(N) = L*

1. L(N) L*

2. L(N) L*

Page 16: 1 FORMAL LANGUAGES, AUTOMATA AND COMPUTABILITY 15-453 (For next time: Read Chapter 1.3 of the book)

16

1. L(N) L*

Assume w = w1…wk is in L*, where w1,…,wk L

We show N accepts w by induction on k

Base Cases:k = 0k = 1

Inductive Step:

Assume N accepts all strings v = v1…vk L*, vi Land let u = u1…ukuk+1 L* , vj L

Since N accepts u1…uk (by induction) and M accepts uk+1, N must accept u

(w = ε)

(w L)

Page 17: 1 FORMAL LANGUAGES, AUTOMATA AND COMPUTABILITY 15-453 (For next time: Read Chapter 1.3 of the book)

17

Assume w is accepted by N, we show w L*If w = ε, then w L*

If w ≠ ε

accept

ε

ε

L*

L*

2. L(N) L*

By induction

By induction

Page 18: 1 FORMAL LANGUAGES, AUTOMATA AND COMPUTABILITY 15-453 (For next time: Read Chapter 1.3 of the book)

18

REGULAR LANGUAGES ARE CLOSED UNDER REGULAR OPERATIONS

Union: A B = { w | w A or w B }

Intersection: A B = { w | w A and w B }

Negation: A = { w Σ* | w A }

Reverse: AR = { w1 …wk | wk …w1 A }

Concatenation: A B = { vw | v A and w B }

Star: A* = { w1 …wk | k ≥ 0 and each wi A }

Page 19: 1 FORMAL LANGUAGES, AUTOMATA AND COMPUTABILITY 15-453 (For next time: Read Chapter 1.3 of the book)

19

The PUMPING LEMMA and REGULAR EXPRESSIONS

Page 20: 1 FORMAL LANGUAGES, AUTOMATA AND COMPUTABILITY 15-453 (For next time: Read Chapter 1.3 of the book)

20

SOME LANGUAGES ARE NOT REGULAR

B = {0n1n | n ≥ 0} is NOT regular!

Page 21: 1 FORMAL LANGUAGES, AUTOMATA AND COMPUTABILITY 15-453 (For next time: Read Chapter 1.3 of the book)

21

WHICH OF THESE ARE REGULAR

C = { w | w has equal number of 1s and 0s}

D = { w | w has equal number of occurrences of 01 and 10}

NOT REGULAR

REGULAR!!!

Page 22: 1 FORMAL LANGUAGES, AUTOMATA AND COMPUTABILITY 15-453 (For next time: Read Chapter 1.3 of the book)

22

THE PUMPING LEMMA

Let L be a regular language with |L| =

Then there exists a positive integer P such that

1. |y| > 0

2. |xy| ≤ P

3. xyiz L for any i ≥ 0

if w L and |w| ≥ Pthen w = xyz, where:

Page 23: 1 FORMAL LANGUAGES, AUTOMATA AND COMPUTABILITY 15-453 (For next time: Read Chapter 1.3 of the book)

23

Let P be the number of states in M

Assume w L is such that |w| ≥ P

q0 qi qj q|w|

There must be j > i such that qi = qj

Let M be a DFA that recognizes L

1. |y| > 0

2. |xy| ≤ P

3. xyiz L for any i ≥ 0

We show w = xyz

x

Page 24: 1 FORMAL LANGUAGES, AUTOMATA AND COMPUTABILITY 15-453 (For next time: Read Chapter 1.3 of the book)

24

USING THE PUMPING LEMMA

Use the pumping lemma to prove that B = {0n1n | n ≥ 0} is not regular

Hint: Assume B is regular Let B = L(M), for DFA M,and let P be larger than the number of states in M

Try pumping s = 0P1P

Page 25: 1 FORMAL LANGUAGES, AUTOMATA AND COMPUTABILITY 15-453 (For next time: Read Chapter 1.3 of the book)

26

Use the pumping lemma to prove that C = { w | w has an equal number of 0s and 1s} is not regular

Hint: Try pumping s = 0P1P

If C is regular, s can be split into s = xyz, where for any i ≥ 0, xyiz is also in C and |xy| ≤ P

Page 26: 1 FORMAL LANGUAGES, AUTOMATA AND COMPUTABILITY 15-453 (For next time: Read Chapter 1.3 of the book)

27

WHAT DOES D LOOK LIKE?

(0(01)*0) (1(01)*1) 1 0 ε

D = { w | w has equal number of occurrences of 01 and 10}

= { w | w = 1, w = 0, w = ε or w starts with a 0 and ends with a 0 orw starts with a 1 and ends with a 1 }

Page 27: 1 FORMAL LANGUAGES, AUTOMATA AND COMPUTABILITY 15-453 (For next time: Read Chapter 1.3 of the book)

28

REGULAR EXPRESSIONS

is a regular expression representing {}

ε is a regular expression representing {ε}

is a regular expression representing

If R1 and R2 are regular expressions representing L1 and L2 then:

(R1R2) represents L1L2

(R1 R2) represents L1 L2

(R1)* represents L1*

Page 28: 1 FORMAL LANGUAGES, AUTOMATA AND COMPUTABILITY 15-453 (For next time: Read Chapter 1.3 of the book)

29

PRECEDENCE

Tightest Star (“*”) Concatenation (“.”, “”)Loosest Union (“”, “+”, “|”)

Page 29: 1 FORMAL LANGUAGES, AUTOMATA AND COMPUTABILITY 15-453 (For next time: Read Chapter 1.3 of the book)

30

R2R1*(

EXAMPLE

R1*R2 R3 = ( ) ) R3

Page 30: 1 FORMAL LANGUAGES, AUTOMATA AND COMPUTABILITY 15-453 (For next time: Read Chapter 1.3 of the book)

31

{ w | w has exactly a single 1 }

0*10*

Page 31: 1 FORMAL LANGUAGES, AUTOMATA AND COMPUTABILITY 15-453 (For next time: Read Chapter 1.3 of the book)

32

What language does * represent?

{ε}

Page 32: 1 FORMAL LANGUAGES, AUTOMATA AND COMPUTABILITY 15-453 (For next time: Read Chapter 1.3 of the book)

33

{ w | w has length ≥ 3 and its 3rd symbol is 0 }

000(01)* 010(01)* 100(01)* 110(01)*

= (01)(01)0(01)*

Page 33: 1 FORMAL LANGUAGES, AUTOMATA AND COMPUTABILITY 15-453 (For next time: Read Chapter 1.3 of the book)

34

{ w | every odd position of w is a 1 }

1((01)1)*(01ε) ε

Also

(1(01))*(1ε)

Page 34: 1 FORMAL LANGUAGES, AUTOMATA AND COMPUTABILITY 15-453 (For next time: Read Chapter 1.3 of the book)

35

EQUIVALENCEL can be represented by a regexp

L is a regular language

Page 35: 1 FORMAL LANGUAGES, AUTOMATA AND COMPUTABILITY 15-453 (For next time: Read Chapter 1.3 of the book)

36

L can be represented by a regexp

L is a regular language

Given regular expression R, we show there exists NFA N such that R represents L(N)

Induction on the length of R:

Page 36: 1 FORMAL LANGUAGES, AUTOMATA AND COMPUTABILITY 15-453 (For next time: Read Chapter 1.3 of the book)

37

Given regular expression R, we show there exists NFA N such that R represents L(N)

Induction on the length of R:

Base Cases (R has length 1):

R = (matches a single symbol)

R = ε(matches the empty string)

R = (matches nothing)

Page 37: 1 FORMAL LANGUAGES, AUTOMATA AND COMPUTABILITY 15-453 (For next time: Read Chapter 1.3 of the book)

38

Inductive Step:

Assume R has length k > 1 and that any regular expression of length < k represents a language that can be recognized by an NFA

Three possibilities for R:

R = R1 R2

R = R1 R2

R = (R1)*

(Union Theorem!)

Page 38: 1 FORMAL LANGUAGES, AUTOMATA AND COMPUTABILITY 15-453 (For next time: Read Chapter 1.3 of the book)

39

L can be represented by a regexp

L is a regular language

Have Shown

Page 39: 1 FORMAL LANGUAGES, AUTOMATA AND COMPUTABILITY 15-453 (For next time: Read Chapter 1.3 of the book)

40

Transform (1(0 1))* to an NFA

1ε 1,0

ε

Page 40: 1 FORMAL LANGUAGES, AUTOMATA AND COMPUTABILITY 15-453 (For next time: Read Chapter 1.3 of the book)

41

L can be represented by a regexp

L is a regular language

Page 41: 1 FORMAL LANGUAGES, AUTOMATA AND COMPUTABILITY 15-453 (For next time: Read Chapter 1.3 of the book)

42

L can be represented by a regexp

L is a regular language

Proof idea: Transform an NFA for L into a regular expression by removing states and re-labeling the arrows with regular expressions

Page 42: 1 FORMAL LANGUAGES, AUTOMATA AND COMPUTABILITY 15-453 (For next time: Read Chapter 1.3 of the book)

43

NFAε

ε

ε

ε

ε

Add unique and distinct start and accept statesWhile machine has more than 2 states:

Pick an internal state, rip it out and re-label the arrows with regexps, to account for the missing state

0

1

0

Page 43: 1 FORMAL LANGUAGES, AUTOMATA AND COMPUTABILITY 15-453 (For next time: Read Chapter 1.3 of the book)

44

NFAε

ε

ε

ε

ε

While machine has more than 2 states:

Pick an internal state, rip it out and re-label the arrows with regexps, to account for the missing state

01*0

Page 44: 1 FORMAL LANGUAGES, AUTOMATA AND COMPUTABILITY 15-453 (For next time: Read Chapter 1.3 of the book)

45

q1

b

a

εq2

a,b

εq0 q3

R(q0,q3) = (a*b)(ab)*

Page 45: 1 FORMAL LANGUAGES, AUTOMATA AND COMPUTABILITY 15-453 (For next time: Read Chapter 1.3 of the book)

46

q2

a,b

εa*bq0 q3

R(q0,q3) = (a*b)(ab)*

Page 46: 1 FORMAL LANGUAGES, AUTOMATA AND COMPUTABILITY 15-453 (For next time: Read Chapter 1.3 of the book)

47

(a*b)(ab)*q0 q3

R(q0,q3) = (a*b)(ab)*

Page 47: 1 FORMAL LANGUAGES, AUTOMATA AND COMPUTABILITY 15-453 (For next time: Read Chapter 1.3 of the book)

48

q3

q2

b

a

b

q1

b

a

a

ε

ε

ε

bb

Page 48: 1 FORMAL LANGUAGES, AUTOMATA AND COMPUTABILITY 15-453 (For next time: Read Chapter 1.3 of the book)

49

q2

b

a

b

q1

a

a

ε

ε

ε

bb

a ba

b

Page 49: 1 FORMAL LANGUAGES, AUTOMATA AND COMPUTABILITY 15-453 (For next time: Read Chapter 1.3 of the book)

50

bb (a ba)b*a

a baq2

b

q1

a

ε

ε

bb

bb (a ba)b*

= R(q1,q1)

(bb (a ba)b*a)*(b (a ba)b*)

Page 50: 1 FORMAL LANGUAGES, AUTOMATA AND COMPUTABILITY 15-453 (For next time: Read Chapter 1.3 of the book)

51

Convert the NFA to a regular expression

q3

q2

b

bq1

a

a, b

ε

ε

εb

(a b)b*b

bb*b

(a b)b*b(bb*b)*

(a b)b*b(bb*b)*a

Page 51: 1 FORMAL LANGUAGES, AUTOMATA AND COMPUTABILITY 15-453 (For next time: Read Chapter 1.3 of the book)

52

((a b)b*b(bb*b)*a)* ((a b)b*b(bb*b)*a)*(a b)b*b(bb*b)*

Page 52: 1 FORMAL LANGUAGES, AUTOMATA AND COMPUTABILITY 15-453 (For next time: Read Chapter 1.3 of the book)

53

Formally:

Run CONVERT(G): (return regexp)

If #states = 2

return the expression on the arrow going from qstart to qaccept

Add qstart and qaccept to create G

If #states > 2

Page 53: 1 FORMAL LANGUAGES, AUTOMATA AND COMPUTABILITY 15-453 (For next time: Read Chapter 1.3 of the book)

54

Formally: Add qstart and qaccept to create G

If #states > 2

select qripQ different from qstart and qaccept

define Q = Q – {qrip}

define R as:

R(qi,qj) = R(qi,qrip)R(qrip,qrip)*R(qrip,qj) R(qi,qj)

return CONVERT(G)

Run CONVERT(G): (return regexp)

} Defines: G (GNFA)

Page 54: 1 FORMAL LANGUAGES, AUTOMATA AND COMPUTABILITY 15-453 (For next time: Read Chapter 1.3 of the book)

55

CONVERT(G) is “equivalent” to G

Proof by induction on k (number of states in G)

Base Case:

k = 2

Inductive Step:

Assume claim is true for k-1 states

We first note that G and G are “equivalent”

But, by the induction hypothesis, G is “equivalent” to CONVERT(G)

And CONVERT(G) is equivalent to CONVERT(G )

QED

Page 55: 1 FORMAL LANGUAGES, AUTOMATA AND COMPUTABILITY 15-453 (For next time: Read Chapter 1.3 of the book)

57

DFA NFA

RegularLanguage

RegularExpression

DEF

Page 56: 1 FORMAL LANGUAGES, AUTOMATA AND COMPUTABILITY 15-453 (For next time: Read Chapter 1.3 of the book)

58

Read Chapter 1.3 of the book for next time