bu cs 332 theory of computation · 2020. 1. 26. · bu cs 332 –theory of computation lecture 2:...

40
BU CS 332 – Theory of Computation Lecture 2: Deterministic Finite Automata Regular Operations Non-deterministic FAs Reading: Sipser Ch 1.1-1.2 Mark Bun January 27, 2020

Upload: others

Post on 12-Sep-2020

4 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: BU CS 332 Theory of Computation · 2020. 1. 26. · BU CS 332 –Theory of Computation Lecture 2: •Deterministic Finite Automata •Regular Operations •Non-deterministic FAs Reading:

BU CS 332 – Theory of Computation

Lecture 2:

• Deterministic Finite Automata

• Regular Operations

• Non-deterministic FAs

Reading:

Sipser Ch 1.1-1.2

Mark Bun

January 27, 2020

Page 2: BU CS 332 Theory of Computation · 2020. 1. 26. · BU CS 332 –Theory of Computation Lecture 2: •Deterministic Finite Automata •Regular Operations •Non-deterministic FAs Reading:

Deterministic Finite Automata

1/26/2020 CS332 - Theory of Computation 2

Page 3: BU CS 332 Theory of Computation · 2020. 1. 26. · BU CS 332 –Theory of Computation Lecture 2: •Deterministic Finite Automata •Regular Operations •Non-deterministic FAs Reading:

A (Real-Life?) Example

• Example: Car stereo

• 𝑃 = Power button (ON/OFF)

• 𝑆 = Source button (cycles through Radio/Bluetooth/USB)Only works when stereo is ON, but source remembered when stereo is OFF

• Starts OFF in Radio mode

• A computational problem: Does a sequence of button presses in {𝑃, 𝑆}∗ leave the stereo ON in USB mode?

1/26/2020 CS332 - Theory of Computation 3

Page 4: BU CS 332 Theory of Computation · 2020. 1. 26. · BU CS 332 –Theory of Computation Lecture 2: •Deterministic Finite Automata •Regular Operations •Non-deterministic FAs Reading:

Machine Models

• Finite Automata (FAs): Machine with a finite amount of unstructured memory

1/26/2020 CS332 - Theory of Computation 4

Input 𝑃 𝑆 𝑃 𝑆

Finite control

Control scans left-to-right

Page 5: BU CS 332 Theory of Computation · 2020. 1. 26. · BU CS 332 –Theory of Computation Lecture 2: •Deterministic Finite Automata •Regular Operations •Non-deterministic FAs Reading:

A DFA for the car stereo problem

1/26/2020 CS332 - Theory of Computation 5

Page 6: BU CS 332 Theory of Computation · 2020. 1. 26. · BU CS 332 –Theory of Computation Lecture 2: •Deterministic Finite Automata •Regular Operations •Non-deterministic FAs Reading:

A DFA for Parity

Parity: Given a string consisting of 𝑎’s and 𝑏’s, does it contain an even number of 𝑎’s?

Ʃ = {𝑎, 𝑏} 𝐿 = {𝑤 | 𝑤 contains an even number of 𝑎’s}

1/26/2020 CS332 - Theory of Computation 6

Page 7: BU CS 332 Theory of Computation · 2020. 1. 26. · BU CS 332 –Theory of Computation Lecture 2: •Deterministic Finite Automata •Regular Operations •Non-deterministic FAs Reading:

Anatomy of a DFA

1/26/2020 CS332 - Theory of Computation 7

𝒒𝟐

00,1

00

1

1

1

𝒒𝟎

𝒒𝟏

𝒒𝟑

Page 8: BU CS 332 Theory of Computation · 2020. 1. 26. · BU CS 332 –Theory of Computation Lecture 2: •Deterministic Finite Automata •Regular Operations •Non-deterministic FAs Reading:

Formal Definition of a DFA

1/26/2020 CS332 - Theory of Computation 8

𝑄 is the set of states

Σ is the alphabet

∶ 𝑄 × Σ → 𝑄 is the transition function

𝑞0 𝑄 is the start state

𝐹 ⊆ 𝑄 is the set of accept states

A finite automaton is a 5-tuple 𝑀 = (𝑄, Σ, , 𝑞0, 𝐹)

Page 9: BU CS 332 Theory of Computation · 2020. 1. 26. · BU CS 332 –Theory of Computation Lecture 2: •Deterministic Finite Automata •Regular Operations •Non-deterministic FAs Reading:

A DFA for Parity

Parity: Given a string consisting of 𝑎’s and 𝑏’s, does it contain an even number of 𝑎’s?

Ʃ = {𝑎, 𝑏} 𝐿 = {𝑤 | 𝑤 contains an even number of 𝑎’s}

1/26/2020 CS332 - Theory of Computation 9

𝑞0 𝑞1

𝑏 𝑏

𝑎

𝑎

State set 𝑄 =Alphabet Ʃ =Transition function 𝛿

Start state 𝑞0Set of accept states 𝐹 =

𝛿 𝑎 𝑏

𝑞0

𝑞1

Page 10: BU CS 332 Theory of Computation · 2020. 1. 26. · BU CS 332 –Theory of Computation Lecture 2: •Deterministic Finite Automata •Regular Operations •Non-deterministic FAs Reading:

Formal Definition of DFA Computation

1/26/2020 CS332 - Theory of Computation 10

𝐿(𝑀) = the language of machine 𝑀= set of all (finite) strings machine 𝑀 accepts

𝑀 recognizes the language 𝐿(𝑀)

A DFA 𝑀 = (𝑄, Σ, , 𝑞0, 𝐹) accepts a string 𝑤 = 𝑤1𝑤2 · · · 𝑤𝑛 ∈ Σ∗ (where each 𝑤𝑖 ∈ Σ) if there exist 𝑟0, . . . , 𝑟𝑛 ∈ 𝑄 such that

1. 𝑟0 = 𝑞02. 𝛿(𝑟𝑖 , 𝑤𝑖+1) = 𝑟𝑖+1 for each 𝑖 = 0, . . . , 𝑛 − 1, and3. 𝑟𝑛 ∈ 𝐹

Page 11: BU CS 332 Theory of Computation · 2020. 1. 26. · BU CS 332 –Theory of Computation Lecture 2: •Deterministic Finite Automata •Regular Operations •Non-deterministic FAs Reading:

Example: Computing with the Parity DFA

1/26/2020 CS332 - Theory of Computation 11

𝑞0 𝑞1

𝑏 𝑏

𝑎

𝑎

A DFA 𝑀 = (𝑄, Σ, , 𝑞0, 𝐹) accepts a string 𝑤 = 𝑤1𝑤2 · · · 𝑤𝑛 ∈ Σ∗ (where each 𝑤𝑖 ∈ Σ) if there exist 𝑟0, . . . , 𝑟𝑛 ∈ 𝑄 such that

1. 𝑟0 = 𝑞02. 𝛿(𝑟𝑖 , 𝑤𝑖+1) = 𝑟𝑖+1 for each 𝑖 = 0, . . . , 𝑛 − 1, and3. 𝑟𝑛 ∈ 𝐹

Let 𝑤 = 𝑎𝑏𝑏𝑎Does 𝑀 accept 𝑤?

Page 12: BU CS 332 Theory of Computation · 2020. 1. 26. · BU CS 332 –Theory of Computation Lecture 2: •Deterministic Finite Automata •Regular Operations •Non-deterministic FAs Reading:

Automata Tutor

1/26/2020 CS332 - Theory of Computation 12

http://automatatutor.com/

Page 13: BU CS 332 Theory of Computation · 2020. 1. 26. · BU CS 332 –Theory of Computation Lecture 2: •Deterministic Finite Automata •Regular Operations •Non-deterministic FAs Reading:

Regular Languages

1/26/2020 CS332 - Theory of Computation 13

Definition: A language is regular if it is recognized by a DFA

𝑳 = { 𝒘 ∈ 𝟎, 𝟏 ∗| 𝒘 contains 𝟎𝟎𝟏 } is regular

𝑳 = { 𝒘 ∈ 𝒂, 𝒃 ∗ | 𝒘 has an even number of 𝒂’s } is regular

Many interesting programs recognize regular languages

NETWORK PROTOCOLS

COMPILERS

GENETIC TESTING

ARITHMETIC

Page 14: BU CS 332 Theory of Computation · 2020. 1. 26. · BU CS 332 –Theory of Computation Lecture 2: •Deterministic Finite Automata •Regular Operations •Non-deterministic FAs Reading:

Internet Transmission Control Protocol

1/26/2020 CS332 - Theory of Computation 14

Let TCPS = { 𝑤 | 𝑤 is a complete TCP Session}Theorem. TCPS is regular

Page 15: BU CS 332 Theory of Computation · 2020. 1. 26. · BU CS 332 –Theory of Computation Lecture 2: •Deterministic Finite Automata •Regular Operations •Non-deterministic FAs Reading:

Compilers

1/26/2020 CS332 - Theory of Computation 15

Comments :

Are delimited by /* */

Cannot have nested /* */

Must be closed by */

*/ is illegal outside a comment

COMMENTS = {strings over {0,1, /, *} with legal comments}

Theorem. COMMENTS is regular.

Page 16: BU CS 332 Theory of Computation · 2020. 1. 26. · BU CS 332 –Theory of Computation Lecture 2: •Deterministic Finite Automata •Regular Operations •Non-deterministic FAs Reading:

Genetic Testing

1/26/2020 CS332 - Theory of Computation 16

DNA sequences are strings over the alphabet {𝑨, 𝑪, 𝑮, 𝑻}.

A gene 𝒈 is a special substring over this alphabet.

A genetic test searches a DNA sequence for a gene.

GENETICTEST𝒈 = {strings over {𝑨, 𝑪, 𝑮, 𝑻} containing 𝒈 as a substring}

Theorem. GENETICTEST𝒈 is regular for every gene 𝒈.

Page 17: BU CS 332 Theory of Computation · 2020. 1. 26. · BU CS 332 –Theory of Computation Lecture 2: •Deterministic Finite Automata •Regular Operations •Non-deterministic FAs Reading:

Arithmetic

1/26/2020 CS332 - Theory of Computation 17

LET 3 =

• A string over 3 has three ROWS (ROW1, ROW2, ROW3)

• Each ROW 𝒃𝟎𝒃𝟏𝒃𝟐…𝒃𝑵 represents the integer

𝒃𝟎 + 𝟐𝒃𝟏 + … + 𝟐𝑵𝒃𝑵.

• Let ADD = {𝑺 ∈ 𝟑∗ | ROW1 + ROW2 = ROW3 }

Theorem. ADD is regular.

{ [ ],[ ],[ ],[ ],[ ],[ ],[ ],[ ]}

000

001

010

011

100

101

110

111

Page 18: BU CS 332 Theory of Computation · 2020. 1. 26. · BU CS 332 –Theory of Computation Lecture 2: •Deterministic Finite Automata •Regular Operations •Non-deterministic FAs Reading:

Regular Operations

1/26/2020 CS332 - Theory of Computation 18

Page 19: BU CS 332 Theory of Computation · 2020. 1. 26. · BU CS 332 –Theory of Computation Lecture 2: •Deterministic Finite Automata •Regular Operations •Non-deterministic FAs Reading:

An Analogy

In algebra, we try to identify operations which are common to many different mathematical structures

1/26/2020 CS332 - Theory of Computation 19

Example: The integers ℤ = {…− 2,−1, 0, 1, 2, … } are closed under• Addition: 𝑥 + 𝑦• Multiplication: 𝑥 × 𝑦• Negation: −𝑥• …but NOT Division: 𝑥 / 𝑦

We’d like to investigate similar closure properties of the class of regular languages

Page 20: BU CS 332 Theory of Computation · 2020. 1. 26. · BU CS 332 –Theory of Computation Lecture 2: •Deterministic Finite Automata •Regular Operations •Non-deterministic FAs Reading:

Regular operations on languagesLet 𝐴, 𝐵 ⊆ Σ∗ be languages. Define

Union: 𝐴 ∪ 𝐵 =

Concatenation: 𝐴 ∘ 𝐵 =

Star: 𝐴∗ =

1/26/2020 CS332 - Theory of Computation 20

Page 21: BU CS 332 Theory of Computation · 2020. 1. 26. · BU CS 332 –Theory of Computation Lecture 2: •Deterministic Finite Automata •Regular Operations •Non-deterministic FAs Reading:

Other operationsLet 𝐴, 𝐵 ⊆ Σ∗ be languages. Define

Complement: ҧ𝐴 =

Intersection: 𝐴 ∩ 𝐵 =

Reverse: 𝐴𝑅 =

1/26/2020 CS332 - Theory of Computation 21

Page 22: BU CS 332 Theory of Computation · 2020. 1. 26. · BU CS 332 –Theory of Computation Lecture 2: •Deterministic Finite Automata •Regular Operations •Non-deterministic FAs Reading:

Closure properties of the regular languages

Theorem: The class of regular languages is closed under all three regular operations (union, concatenation, star), as well as under complement, intersection, and reverse.

i.e., if 𝐴 and 𝐵 are regular, applying any of these operations yields a regular language

1/26/2020 CS332 - Theory of Computation 22

Page 23: BU CS 332 Theory of Computation · 2020. 1. 26. · BU CS 332 –Theory of Computation Lecture 2: •Deterministic Finite Automata •Regular Operations •Non-deterministic FAs Reading:

Proving Closure Properties

1/26/2020 CS332 - Theory of Computation 23

Page 24: BU CS 332 Theory of Computation · 2020. 1. 26. · BU CS 332 –Theory of Computation Lecture 2: •Deterministic Finite Automata •Regular Operations •Non-deterministic FAs Reading:

Complement

Complement: ҧ𝐴 = 𝑤 𝑤 ∉ 𝐴}

Theorem: If 𝐴 is regular, then ҧ𝐴 is also regular

Proof idea:

1/26/2020 CS332 - Theory of Computation 24

Page 25: BU CS 332 Theory of Computation · 2020. 1. 26. · BU CS 332 –Theory of Computation Lecture 2: •Deterministic Finite Automata •Regular Operations •Non-deterministic FAs Reading:

Union

1/26/2020 CS332 - Theory of Computation 25

Union: 𝐴 ∪ 𝐵 = 𝑤 𝑤 ∈ 𝐴 or 𝑤 ∈ 𝐵}

Theorem: If 𝐴 and 𝐵 are regular, then so is 𝐴 ∪ 𝐵

Proof:

Let 𝑀𝐴 = (𝑄𝐴, Σ, 𝐴, 𝑞0𝐴, 𝐹𝐴) be a DFA recognizing 𝐴 and

𝑀𝐵 = (𝑄𝐵, Σ, 𝐵, 𝑞0𝐵 , 𝐹𝐵) be a DFA recognizing 𝐵

Goal: Construct a DFA 𝑀 = 𝑄, Σ, , 𝑞0, 𝐹

that recognizes 𝐴 ∪ 𝐵

Page 26: BU CS 332 Theory of Computation · 2020. 1. 26. · BU CS 332 –Theory of Computation Lecture 2: •Deterministic Finite Automata •Regular Operations •Non-deterministic FAs Reading:

Example

1/26/2020 CS332 - Theory of Computation 26

𝑞0𝐴 𝑞1

𝐴

00

1

1

𝑞0𝐵 𝑞1

𝐵

11

0

0

𝑴𝑨

𝑴𝑩

𝑴 = ?

Page 27: BU CS 332 Theory of Computation · 2020. 1. 26. · BU CS 332 –Theory of Computation Lecture 2: •Deterministic Finite Automata •Regular Operations •Non-deterministic FAs Reading:

Closure under union proof (cont’d)

Idea: Run both 𝑀𝐴 and 𝑀𝐵 at the same time

“Cross-product construction”

𝑄 = 𝑄𝐴 × 𝑄𝐵

= {(𝑞𝐴, 𝑞𝐵) |𝑞𝐴 ∈ 𝐴 and 𝑞𝐵 ∈ 𝐵}

( (𝑞𝐴, 𝑞𝐵),) = (𝐴(𝑞𝐴,), 𝐵(𝑞𝐵,))

𝑞0 = (𝑞0𝐴, 𝑞0

𝐵)

𝐹 = {(𝑞𝐴, 𝑞𝐵) |𝑞𝐴 ∈ 𝐹𝐴 or 𝑞𝐵 ∈ 𝐹𝐵}

= 𝐹𝐴 × 𝑄𝐵 ∪ 𝑄𝐴 × 𝐹𝐵1/26/2020 CS332 - Theory of Computation 27

Page 28: BU CS 332 Theory of Computation · 2020. 1. 26. · BU CS 332 –Theory of Computation Lecture 2: •Deterministic Finite Automata •Regular Operations •Non-deterministic FAs Reading:

Example (cont’d)

1/26/2020 CS332 - Theory of Computation 28

𝑞0𝐴 𝑞1

𝐴

00

1

1

𝑞0𝐵 𝑞1

𝐵

1 1

0

0

𝑴𝑨𝑴

𝑴𝑩

Page 29: BU CS 332 Theory of Computation · 2020. 1. 26. · BU CS 332 –Theory of Computation Lecture 2: •Deterministic Finite Automata •Regular Operations •Non-deterministic FAs Reading:

Intersection

1/26/2020 CS332 - Theory of Computation 29

Intersection: 𝐴 ∩ 𝐵 = 𝑤 𝑤 ∈ 𝐴 and 𝑤 ∈ 𝐵}

Theorem: If 𝐴 and 𝐵 are regular, then so is 𝐴 ∩ 𝐵

Proof:

Let 𝑀𝐴 = (𝑄𝐴, Σ, 𝐴, 𝑞0𝐴, 𝐹𝐴) be a DFA recognizing 𝐴 and

𝑀𝐵 = (𝑄𝐵, Σ, 𝐵, 𝑞0𝐵 , 𝐹𝐵) be a DFA recognizing 𝐵

Goal: Construct a DFA 𝑀 = 𝑄, Σ, , 𝑞0, 𝐹

that recognizes 𝐴 ∩ 𝐵

Page 30: BU CS 332 Theory of Computation · 2020. 1. 26. · BU CS 332 –Theory of Computation Lecture 2: •Deterministic Finite Automata •Regular Operations •Non-deterministic FAs Reading:

Intersection

1/26/2020 CS332 - Theory of Computation 30

Intersection: 𝐴 ∩ 𝐵 = 𝑤 𝑤 ∈ 𝐴 and 𝑤 ∈ 𝐵}

Theorem: If 𝐴 and 𝐵 are regular, then so is 𝐴 ∩ 𝐵

Another Proof:

𝐴 ∩ 𝐵 = ҧ𝐴 ∪ ത𝐵

Page 31: BU CS 332 Theory of Computation · 2020. 1. 26. · BU CS 332 –Theory of Computation Lecture 2: •Deterministic Finite Automata •Regular Operations •Non-deterministic FAs Reading:

Reverse

1/26/2020 CS332 - Theory of Computation 31

Reverse: 𝐴𝑅 = 𝑤1𝑤2 · · · 𝑤𝑛 𝑤𝑛 · · · 𝑤1 ∈ 𝐴}

Theorem: If 𝐴 is regular, then 𝐴𝑅 is also regular

Proof idea:

Let 𝑀 = (𝑄, Σ, , 𝑞0, 𝐹) be a DFA recognizing 𝐴

Goal: Construct a DFA 𝑀′ = 𝑄′, Σ, ′, 𝑞0′ , 𝐹′

that recognizes 𝐴𝑅

Define 𝑀′ as 𝑀 but

• With the arrows reversed

• With start and accept states swapped

Page 32: BU CS 332 Theory of Computation · 2020. 1. 26. · BU CS 332 –Theory of Computation Lecture 2: •Deterministic Finite Automata •Regular Operations •Non-deterministic FAs Reading:

Example (Reverse)

1/26/2020 CS332 - Theory of Computation 32

1 0

1

0 1

0,1

0𝑴

𝑴′

Page 33: BU CS 332 Theory of Computation · 2020. 1. 26. · BU CS 332 –Theory of Computation Lecture 2: •Deterministic Finite Automata •Regular Operations •Non-deterministic FAs Reading:

Closure under reverse

𝑀’ is not always a DFA!

• It might have many start states

• Some states may have too many outgoing edges, or none at all

1/26/2020 CS332 - Theory of Computation 33

Page 34: BU CS 332 Theory of Computation · 2020. 1. 26. · BU CS 332 –Theory of Computation Lecture 2: •Deterministic Finite Automata •Regular Operations •Non-deterministic FAs Reading:

Nondeterminism

1/26/2020 CS332 - Theory of Computation 34

1 0

1

0 1

0,1

0

A Nondeterministic Finite Automaton (NFA) accepts if

there is a way to make it reach an accept state.

Page 35: BU CS 332 Theory of Computation · 2020. 1. 26. · BU CS 332 –Theory of Computation Lecture 2: •Deterministic Finite Automata •Regular Operations •Non-deterministic FAs Reading:

Example

1/26/2020 CS332 - Theory of Computation 35

1

0

0

𝑳(𝑴) =

𝜺

𝜺

Page 36: BU CS 332 Theory of Computation · 2020. 1. 26. · BU CS 332 –Theory of Computation Lecture 2: •Deterministic Finite Automata •Regular Operations •Non-deterministic FAs Reading:

Example

1/26/2020 CS332 - Theory of Computation 36

0,1

0,𝜺 1

0,1

1

𝑳(𝑴) =

Page 37: BU CS 332 Theory of Computation · 2020. 1. 26. · BU CS 332 –Theory of Computation Lecture 2: •Deterministic Finite Automata •Regular Operations •Non-deterministic FAs Reading:

Formal Definition of a NFA

1/26/2020 CS332 - Theory of Computation 37

𝑄 is the set of states

Σ is the alphabet

∶ 𝑄 × Σ𝜀 → 𝑃(𝑄) is the transition function

𝑞0 𝑄 is the start state

𝐹 ⊆ 𝑄 is the set of accept states

An NFA is a 5-tuple 𝑀 = (𝑄, Σ, , 𝑞0, 𝐹)

𝑀 accepts a string 𝑤 if there exists a path from 𝑞0 to an accept state that can be followed by reading 𝑤.

Page 38: BU CS 332 Theory of Computation · 2020. 1. 26. · BU CS 332 –Theory of Computation Lecture 2: •Deterministic Finite Automata •Regular Operations •Non-deterministic FAs Reading:

Example

1/26/2020 CS332 - Theory of Computation 38

1

0

0𝜺

𝜺

(𝒒𝟑, 𝟏) =

𝑴 = (𝑸, 𝚺, , 𝑸𝟎, 𝑭)

𝑸 = {𝒒𝟎,𝒒𝟏, 𝒒𝟐, 𝒒𝟑, 𝒒𝟒}

𝚺 = {𝟎, 𝟏}

𝑭 = {𝒒𝟒}

(𝒒𝟐, 𝟏) =

𝒒𝟏

𝒒𝟐

𝒒𝟑

𝒒𝟒

𝒒𝟎

Page 39: BU CS 332 Theory of Computation · 2020. 1. 26. · BU CS 332 –Theory of Computation Lecture 2: •Deterministic Finite Automata •Regular Operations •Non-deterministic FAs Reading:

Example

1/26/2020 CS332 - Theory of Computation 39

0,1

0,𝜺 1

0,1

1𝒒𝟏 𝒒𝟐 𝒒𝟑𝒒𝟎

𝑵 = (𝑸, 𝚺, , 𝒒𝟎, 𝑭)

𝑸 = {𝒒𝟎,𝒒𝟏, 𝒒𝟐, 𝒒𝟑}

𝚺 = {𝟎, 𝟏}

𝑭 = {𝒒𝟑}

(𝒒𝟎, 𝟏) =

(𝒒𝟐, 𝟎) =

(𝒒𝟎, 𝟎) =

(𝒒𝟏, 𝜺) =

Page 40: BU CS 332 Theory of Computation · 2020. 1. 26. · BU CS 332 –Theory of Computation Lecture 2: •Deterministic Finite Automata •Regular Operations •Non-deterministic FAs Reading:

Nondeterminism

1/26/2020 CS332 - Theory of Computation 40

Ways to think about nondeterminism

• (restricted) parallel computation

• tree of possible computations

• guessing and verifying the “right” choice

Deterministic

ComputationNondeterministic

Computation

accept or reject accept

reject