15-251 Great Theoretical Ideas in Computer Science Lecture 4: - - PowerPoint PPT Presentation

15 251 great theoretical ideas in computer science
SMART_READER_LITE
LIVE PREVIEW

15-251 Great Theoretical Ideas in Computer Science Lecture 4: - - PowerPoint PPT Presentation

15-251 Great Theoretical Ideas in Computer Science Lecture 4: Deterministic Finite Automaton (DFA), Part 2 January 26th, 2017 Formal definition: DFA A deterministic finite automaton (DFA) is a 5-tuple M M = ( Q, , , q 0 , F ) Q


slide-1
SLIDE 1

January 26th, 2017

15-251 Great Theoretical Ideas in Computer Science

Lecture 4: Deterministic Finite Automaton (DFA), Part 2

slide-2
SLIDE 2

Formal definition: DFA

A deterministic finite automaton (DFA) is a 5-tuple M M = (Q, Σ, δ, q0, F) Q = {q0, q1, q2, q3} Σ = {0, 1} δ : Q × Σ → Q F = {q1, q2} q0 is the start state q0 q1 q2 q3 1 δ q0 q0 q1 q2 q2 q2 q2 q3

slide-3
SLIDE 3

Formal definition: DFA accepting a string

Let be a DFA. M = (Q, Σ, δ, q0, F) For , q ∈ Q, w ∈ Σ∗ δ(q, w) = state we end up in when we start at and read . w q Otherwise rejects . M w accepts if M w δ(q0, w) ∈ F.

slide-4
SLIDE 4

Definition: Regular languages

Definition: A language is called regular if for some DFA . L = L(M) M L Let be a DFA. We let denote the set of strings that accepts. M L(M) M

slide-5
SLIDE 5

Non-regular languages

Theorem: The language is not regular. L = {0n1n : n ∈ N} Theorem: The language is not regular. {a2n : n ∈ N} L =

slide-6
SLIDE 6

The big picture

. . . Regular languages All languages P(Σ∗)

{0n1n : n ∈ N}

. . .

{a2n : n ∈ N}

slide-7
SLIDE 7

Regular languages

Questions:

  • 1. Are all languages regular?

(Are all decision problems computable by a DFA?)

  • 2. Are there other ways to tell if a language is regular?
slide-8
SLIDE 8

Closure properties of regular languages

slide-9
SLIDE 9

Closed under complementation

Proposition: Let be some finite alphabet. Σ If is regular, then so is . L ⊆ Σ∗ L = Σ∗\L Proof: If is regular, then there is a DFA L M = (Q, Σ, δ, q0, F) recognizing . L Then M 0 = (Q, Σ, δ, q0, Q\F) recognizes . L So is regular. L

slide-10
SLIDE 10

Closed under complementation

By contrapositive: Proof: Corollary: If is non-regular, then so is . L ⊆ Σ∗ L If is regular, then by the previous Proposition L is regular. L = L Examples: {0, 1}∗\{0n1n : n ∈ N} {a}∗\{a2n : n ∈ N} are non-regular. Closure properties can be used to show languages are not regular.

slide-11
SLIDE 11

Closed under union

Theorem: Let be some finite alphabet. Σ If and are regular, then so is . L1 ⊆ Σ∗ L2 ⊆ Σ∗ L1 ∪ L2 Proof: and be a DFA deciding . Let be a DFA deciding M = (Q, Σ, δ, q0, F) M 0 = (Q0, Σ, δ0, q0

0, F 0)

L1 L2 We construct a DFA M 00 = (Q00, Σ, δ00, q00

0, F 00)

that decides , as follows: L1 ∪ L2 . . .

slide-12
SLIDE 12

The mindset Imagine yourself as a DFA. Rules: 1) Can only scan the input once, from left to right. 2) Can only remember “constant” amount of information.

should not change based on input length

slide-13
SLIDE 13

Step 1: Imagining ourselves as a DFA

slide-14
SLIDE 14

Closed under union

Example L1 = L2 = strings with even number of 1’s strings with length divisible by 3. 0, 1 0, 1 0, 1 p0 p1 p2 M2 qeven qodd 1 1 M1

slide-15
SLIDE 15

Closed under union

0, 1 0, 1 0, 1 p0 p1 p2 qeven qodd 1 1 Input: 101001 M1 M2

slide-16
SLIDE 16

Closed under union

0, 1 0, 1 0, 1 p0 p1 p2 qeven qodd 1 1 Input: 101001 M1 M2

slide-17
SLIDE 17

Closed under union

0, 1 0, 1 0, 1 p0 p1 p2 qeven qodd 1 1 Input: 101001 M1 M2

slide-18
SLIDE 18

Closed under union

0, 1 0, 1 0, 1 p0 p1 p2 qeven qodd 1 1 Input: 101001 M1 M2

slide-19
SLIDE 19

Closed under union

0, 1 0, 1 0, 1 p0 p1 p2 qeven qodd 1 1 Input: 101001 M1 M2

slide-20
SLIDE 20

Closed under union

0, 1 0, 1 0, 1 p0 p1 p2 qeven qodd 1 1 Input: 101001 M1 M2

slide-21
SLIDE 21

Closed under union

0, 1 0, 1 0, 1 p0 p1 p2 qeven qodd 1 1 Input: 101001 M1 M2

slide-22
SLIDE 22

Closed under union

0, 1 0, 1 0, 1 p0 p1 p2 qeven qodd 1 1 Input: 101001 M1 M2

slide-23
SLIDE 23

Closed under union

0, 1 0, 1 0, 1 p0 p1 p2 qeven qodd 1 1 Input: 101001 M1 M2 Accept

slide-24
SLIDE 24

Closed under union

0, 1 0, 1 0, 1 p0 p1 p2 qeven qodd 1 1 M1 M2 Main idea: Construct a DFA that keeps track of both at once.

slide-25
SLIDE 25

Closed under union

Main idea: Construct a DFA that keeps track of both at once. p0 p2 qeven qodd qeven qeven p1 qodd qodd p0 p2 p1

slide-26
SLIDE 26

Closed under union

? Main idea: Construct a DFA that keeps track of both at once. p0 p2 qeven qodd qeven qeven p1 qodd qodd p0 p2 p1

slide-27
SLIDE 27

Closed under union

p0 p2 qeven qodd qeven qeven p1 qodd qodd p0 p2 p1 Main idea: Construct a DFA that keeps track of both at once.

slide-28
SLIDE 28

Closed under union

p0 p2 qeven qodd qeven qeven p1 qodd qodd p0 p2 p1 ? 1 Main idea: Construct a DFA that keeps track of both at once.

slide-29
SLIDE 29

Closed under union

p0 p2 qeven qodd qeven qeven p1 qodd qodd p0 p2 p1 1 Main idea: Construct a DFA that keeps track of both at once.

slide-30
SLIDE 30

Closed under union

p0 p2 qeven qodd qeven qeven p1 qodd qodd p0 p2 p1 1 ? Main idea: Construct a DFA that keeps track of both at once.

slide-31
SLIDE 31

Closed under union

p0 p2 qeven qodd qeven qeven p1 qodd qodd p0 p2 p1 1 Main idea: Construct a DFA that keeps track of both at once.

slide-32
SLIDE 32

Closed under union

p0 p2 qeven qodd qeven qeven p1 qodd qodd p0 p2 p1 1 ? 1 Main idea: Construct a DFA that keeps track of both at once.

slide-33
SLIDE 33

Closed under union

p0 p2 qeven qodd qeven qeven p1 qodd qodd p0 p2 p1 1 1 Main idea: Construct a DFA that keeps track of both at once.

slide-34
SLIDE 34

Closed under union

p0 p2 qeven qodd qeven qeven p1 qodd qodd p0 p2 p1 1 1 Main idea: Construct a DFA that keeps track of both at once. 1 1

slide-35
SLIDE 35

Closed under union

p0 p2 qeven qodd qeven qeven p1 qodd qodd p0 p2 p1 1 1 1 1 Input: 101001

slide-36
SLIDE 36

Closed under union

p0 p2 qeven qodd qeven qeven p1 qodd qodd p0 p2 p1 1 1 Input: 101001 1 1

slide-37
SLIDE 37

Closed under union

p0 p2 qeven qodd qeven qeven p1 qodd qodd p0 p2 p1 1 1 Input: 101001 1 1

slide-38
SLIDE 38

Closed under union

p0 p2 qeven qodd qeven qeven p1 qodd qodd p0 p2 p1 1 1 1 1 Input: 101001

slide-39
SLIDE 39

Closed under union

p0 p2 qeven qodd qeven qeven p1 qodd qodd p0 p2 p1 1 1 Input: 101001 1 1

slide-40
SLIDE 40

Closed under union

p0 p2 qeven qodd qeven qeven p1 qodd qodd p0 p2 p1 1 1 1 1 Input: 101001

slide-41
SLIDE 41

Closed under union

p0 p2 qeven qodd qeven qeven p1 qodd qodd p0 p2 p1 1 1 Input: 101001 1 1

slide-42
SLIDE 42

Closed under union

p0 p2 qeven qodd qeven qeven p1 qodd qodd p0 p2 p1 1 1 1 1 Input: 101001

slide-43
SLIDE 43

Closed under union

p0 p2 qeven qodd qeven qeven p1 qodd qodd p0 p2 p1 1 1 Input: 101001 1 1

slide-44
SLIDE 44

Closed under union

p0 p2 qeven qodd qeven qeven p1 qodd qodd p0 p2 p1 1 1 1 1 Input: 101001

slide-45
SLIDE 45

Closed under union

p0 p2 qeven qodd qeven qeven p1 qodd qodd p0 p2 p1 1 1 Input: 101001 1 1

slide-46
SLIDE 46

Closed under union

p0 p2 qeven qodd qeven qeven p1 qodd qodd p0 p2 p1 1 1 1 1 Input: 101001

slide-47
SLIDE 47

Closed under union

p0 p2 qeven qodd qeven qeven p1 qodd qodd p0 p2 p1 1 1 Input: 101001 1 1

slide-48
SLIDE 48

Closed under union

p0 p2 qeven qodd qeven qeven p1 qodd qodd p0 p2 p1 1 1 Input: 101001 1 1 Decision: Accept

slide-49
SLIDE 49

Step 2: Formally defining the DFA

slide-50
SLIDE 50

Closed under union

Proof: L1 and be a DFA deciding . Let be a DFA deciding M = (Q, Σ, δ, q0, F) M 0 = (Q0, Σ, δ0, q0

0, F 0)

L2 We construct a DFA M 00 = (Q00, Σ, δ00, q00

0, F 00)

that decides , as follows: L1 ∪ L2

  • Q00 = Q × Q0 = {(q, q0) : q ∈ Q, q0 ∈ Q0}
  • δ00((q, q0), a) = (δ(q, a), δ0(q0, a))
  • q00

0 = (q0, q0 0)

  • F 00 = {(q, q0) : q ∈ F or q0 ∈ F 0}

It remains to show that . L(M 00) = L1 ∪ L2 L(M 00) ⊆ L1 ∪ L2 : . . . L1 ∪ L2 ⊆ L(M 00) : . . .

slide-51
SLIDE 51

Closed under intersection

Corollary: Let be some finite alphabet. Σ If and are regular, then so is . L1 ⊆ Σ∗ L2 ⊆ Σ∗ L1 ∩ L2 Proof: Follows from:

  • L1 ∩ L2 = L1 ∪ L2
  • regular languages are closed under complementation
  • regular languages are closed under union
slide-52
SLIDE 52

Closed under intersection

Closure properties can be used to show languages are not regular. Example: Let be the language consisting of all words with an equal number of 0’s and 1’s. L ⊆ {0, 1}∗ We claim is not regular. L {0n1m : n, m ∈ N} {0n1n : n ∈ N} ∩ L = Suppose it was regular. regular regular regular

contradiction

slide-53
SLIDE 53

More closure properties

regular regular. = ⇒ L∗ L∗ = {x1x2 · · · xk : k ≥ 0, ∀i xi ∈ L} L Closed under star: regular regular. L1, L2 = ⇒ L1 · L2 Closed under concatenation: L1 · L2 = {xy : x ∈ L1, y ∈ L2} regular regular. L1, L2 = ⇒ L1 ∪ L2 Closed under union: L1 ∪ L2 = {x ∈ Σ∗ : x ∈ L1 or x ∈ L2}

slide-54
SLIDE 54

awesome vs regular

What is the relationship between awesome and regular ? awesome regular ⊆ awesome regular = In fact:

slide-55
SLIDE 55

awesome = regular

Theorem: Can define regular languages recursively as follows: ∅

  • is regular.
  • For every , is regular.

{a} a ∈ Σ

  • regular regular.

L1, L2 = ⇒ L1 ∪ L2

  • regular regular.

L1, L2 = ⇒ L1 · L2

  • regular regular.

= ⇒ L∗ L

slide-56
SLIDE 56

Regular expressions

Definition: A regular expression is defined recursively as follows: ∅

  • is a regular expression.
  • For every , is a regular expression.

a ∈ Σ a

  • is a regular expression.

  • regular expr. regular expr.

= ⇒ (R1 ∪ R2) R1, R2

  • regular expr. regular expr.

= ⇒ (R1R2) R1, R2

  • regular expr. regular expr.

R = ⇒ (R∗)

slide-57
SLIDE 57

Regular expressions

Examples: (((0 ∪ 1)∗1)(0 ∪ 1)∗) 0∗10∗ 0Σ∗0 ∪ 1Σ∗1 ∪ 0 ∪ 1 {w ∈ {0, 1}∗ : w starts and ends with same symbol} {w ∈ {0, 1}∗ : w has at least one 1} {w ∈ {0, 1}∗ : w has exactly one 1} Σ∗1Σ∗ =

slide-58
SLIDE 58

Closed under concatenation

Theorem: Let be some finite alphabet. Σ If and are regular, then so is . L1 ⊆ Σ∗ L2 ⊆ Σ∗ L1L2

slide-59
SLIDE 59

The mindset Imagine yourself as a DFA. Rules: 1) Can only scan the input once, from left to right. 2) Can only remember “constant” amount of information.

should not change based on input length

slide-60
SLIDE 60

Step 1: Imagining ourselves as a DFA

slide-61
SLIDE 61

Given , we need to decide if w ∈ Σ∗ w = uv for u ∈ L1, v ∈ L2. Problem: don’t know where ends, begins. u v When do you stop simulating and start simulating ? M1 M2 M1

1 1 1 1 1 q0 q1 q2 q3 q4 1 0 1 1

M2

q0

2

q0

1

q0

slide-62
SLIDE 62

w1 w2 w3 w4 w5 w6 w7 w8 w9

1 1 1

w10

1

q0 q1 q1 q0 q0

2

q0

2

q0

2

q0

1

q0

2

q0

2

q0

1

Suppose God tells you ends at . u w3 M1

1 1 1 1 1 q0 q1 q2 q3 q4 1 0 1 1

M2

q0

2

q0

1

q0

thread: a simulation of and then that corresponds to breaking up into where .

M1 M2 w uv u ∈ L1

q3

slide-63
SLIDE 63

M1

1 1 1 1 1 q0 q1 q2 q3 q4 1 0 1 1

w1 w2 w3 w4 w5 w6 w7 w8 w9

1 1 1 M2

q0

2

q0

1

q0

w10

w11

1 1

q0 q1 q1 q0 q2 q0

2

q1 q0

2

q0

1

q1 q0

2

q0

1

q0

1

q1 q0

2

q0

2

q0

2

q2 q0 q0 q0 q0

2

thread1

q0

thread2

q0

thread3

q0

thread4

automatic teleportation

q3 q4 q0

2

q3 q0

1

q0 q3 q0

1

q0

1

q0

1

slide-64
SLIDE 64

w1 w2 w3 w4 w5 w6 w7 w8 w9

1 1 1

w10

w11

1 1

q0 q1 q1 q3 q2 q4 q1 q3 q1 q1 q3 q2 q0 q0

2

q0

2

q0

2

q0

1

q0

2

q0

2

q0

1

q0 q0 q0

1

q0 q0

1

q0 q0

2

q0 q0

1

q0

2

q0

1

q0 q0

2

q0

1

q0

thread1 thread2 thread3 thread4

M1

1 1 1 1 1 q0 q1 q2 q3 q4 1 0 1 1

M2

q0

2

q0

1

q0

automatic teleportation

slide-65
SLIDE 65

w1 w2 w3 w4 w5 w6 w7 w8 w9

1 1 1

w10

w11

1 1

q0 q1 q1 q3 q2 q4 q1 q3 q1 q1 q3 q2 q0 q0

2

q0

2

q0

2

q0

1

q0

2

q0

2

q0

1

q0 q0 q0

1

q0 q0

1

q0 q0

2

q0 q0

1

q0

2

q0

1

q0 q0

2

q0

1

q0 ⊆ ⊆ ⊆ ⊆ ⊆ ⊆ ⊆ ⊆ ⊆ ⊆ ⊆ Q0 Q0 Q0 Q0 Q0 Q0 Q0 Q0 Q0 Q0 Q0 At any point, need to remember:

  • an element of Q
  • a subset of Q0

constant amount of information This keeps track of every possible thread.

slide-66
SLIDE 66

Step 2: Formally defining the DFA

slide-67
SLIDE 67

M1 = (Q, Σ, δ, q0, F) M2 = (Q0, Σ, δ0, q0

0, F 0)

Q×P(Q0) = Q00 δ00 : Q× P(Q0)×Σ → Q× P(Q0) for , , q ∈ Q S ∈ P(Q0) a ∈ Σ (δ(q, a),{δ0(s, a) : s ∈ S}) δ(q, a) 62 F if q00

0 = (q0, ∅)

if q0 62 F q00

0 = (q0,{q0 0}) otherwise

F 00 = {(q, ) : q ∈ Q, S S ∈ P(Q0), S \ F 0 6= ;} S δ00 (q, , a) S (δ(q, a), {δ0(s, a) : s ∈ S} ) ∪{q0

0} otherwise

(q, , a) δ00

slide-68
SLIDE 68

Next Time