CS 208: Computing Theory

73
CS 208: Computing Theory Assoc. Prof. Dr. Brahim Hnich Faculty of Computer Sciences Izmir University of Economics

description

CS 208: Computing Theory. Assoc. Prof. Dr. Brahim Hnich Faculty of Computer Sciences Izmir University of Economics. Automata Theory. Finite Sate Machines. Motivation. Question : What is a computer? Too complex to allow a precise mathematical model - PowerPoint PPT Presentation

Transcript of CS 208: Computing Theory

Page 1: CS 208: Computing Theory

CS 208: Computing Theory

Assoc. Prof. Dr. Brahim HnichFaculty of Computer SciencesIzmir University of Economics

Page 2: CS 208: Computing Theory

Automata Theory

Finite Sate Machines

Page 3: CS 208: Computing Theory

Motivation

Question: What is a computer? Too complex to allow a precise mathematical

model We introduce an abstract idealized computer Computational model

One such a model is called finite automaton

Page 4: CS 208: Computing Theory

Key ideas

Formal definition of a finite automaton

Deterministic vs. nondeterministic finite automaton

Regular languages Operations on regular

languages Regular expressions Pumping Lemma

Page 5: CS 208: Computing Theory

Automata Theory

Deterministic Finite Automaton

Page 6: CS 208: Computing Theory

Example: Automatic door

Frontpad

Rearpad

door

Opens when someone approachesHolds open until person clearsDoesn’t open when someone is standing behind the door

Page 7: CS 208: Computing Theory

Automatic door Sensors

The door has 4 sensors FRONT: someone on rear pad BEHIND: someone on the rear pad BOTH: someone on both pads NEITHER: no one on either pad

Page 8: CS 208: Computing Theory

Automatic door states

The door has two states OPEN CLOSE

Page 9: CS 208: Computing Theory

Automatic door: state diagram

CLOSED OPEN

Page 10: CS 208: Computing Theory

Automatic door: state diagram

CLOSED OPEN

FRONTREARBOTH

Page 11: CS 208: Computing Theory

Automatic door: state diagram

CLOSED OPEN

REARBOTHNEITHER

FRONTREARBOTH

Page 12: CS 208: Computing Theory

Automatic door: state diagram

CLOSED OPEN

FRONTREARBOTHNEITHER

FRONTREARBOTH

Page 13: CS 208: Computing Theory

Automatic door: state diagram

CLOSED OPEN

FRONT

NEITHER

REARBOTHNEITHER

FRONTREARBOTH

Page 14: CS 208: Computing Theory

Automatic door: state transition table

CLOSED OPEN

FRONT

NEITHER

REARBOTHNEITHER

FRONTREARBOTH

NEITHER FRONT REAR BOTH

CLOSEDOPEN

CLOSED OPEN CLOSED CLOSEDCLOSED OPEN OPEN OPEN

Page 15: CS 208: Computing Theory

Informal definition

The machine M has States: q1,q2, and q3 Start state: q1 (incoming arrow from nowhere) Accept state: q2 (thick circle) State transitions: arrows

q1 q2 q3

0 1 1 0

0,1

Page 16: CS 208: Computing Theory

Informal definition

The machine M on input string begins in start state q1 after reading each symbol, 1 takes transition with matching symbol

q1 q2 q3

0 1 1 0

0,1

Page 17: CS 208: Computing Theory

Informal definition

After processing all the input string machine M produces output accept if M is in an accepting state reject otherwise

q1 q2 q3

0 1 1 0

0,1

Page 18: CS 208: Computing Theory

Informal definition

What happens on input string? 1101 0010 01100

q1 q2 q3

0 1 1 0

0,1

Page 19: CS 208: Computing Theory

Informal definition

What happens on input string? 1101

q1 q2 q3

0 1 1 0

0,1

Page 20: CS 208: Computing Theory

Informal definition

What happens on input string? 1101

q1 q2 q3

0 1 1 0

0,1

Page 21: CS 208: Computing Theory

Informal definition

What happens on input string? 1101

q1 q2 q3

0 1 1 0

0,1

Page 22: CS 208: Computing Theory

Informal definition

What happens on input string? 1101 Accept

q1 q2 q3

0 1 1 0

0,1

Page 23: CS 208: Computing Theory

Informal definition

What happens on input string? 0010

q1 q2 q3

0 1 1 0

0,1

Page 24: CS 208: Computing Theory

Informal definition

What happens on input string? 0010

q1 q2 q3

0 1 1 0

0,1

Page 25: CS 208: Computing Theory

Informal definition

What happens on input string? 0010

q1 q2 q3

0 1 1 0

0,1

Page 26: CS 208: Computing Theory

Informal definition

What happens on input string? 0010 Reject

q1 q2 q3

0 1 1 0

0,1

Page 27: CS 208: Computing Theory

Informal definition

Can you describe the language consisting of all string that M actually accepts?

q1 q2 q3

0 1 1 0

0,1

Page 28: CS 208: Computing Theory

Informal definition

M actually accepts any string that ends with a 1

1,01,11,01010101010101,… any string the ends with an even number of 0’s after

the last 1 100, 101010000,…

q1 q2 q3

0 1 1 0

0,1

Page 29: CS 208: Computing Theory

Formal Definitions

A finite automaton is a 5-tuple (Q,∑,ξ,q0,F), where Q is a finite set of states ∑ is a finite set of symbols called the alphabet ξ : Q x ∑ Q is the transition function q0 Є Q is the start state F ⊆ Q is the set of accept states or final states

Page 30: CS 208: Computing Theory

Back to M

M is a 5-tuple (Q,∑,ξ,q1,F), where Q is a finite set of states {q1,q2,q3} ∑ is {0,1} ξ : Q x ∑ Q is the transition function q1 Є Q is the start state F ⊆ Q is {q2}

q1 q2 q3

0 1 1 0

0,1

0 1

q1q2q3

q1 q2q3 q2q2 q2

ξ

Page 31: CS 208: Computing Theory

Another example: M1

M1 is a 5-tuple (Q,∑,ξ,q1,F), where Q is a finite set of states {q1,q2} ∑ is {0,1} ξ : Q x ∑ Q is the transition function q1 Є Q is the start state F ⊆ Q is {q2}

q1 q2

0 1 1

0 1

q1q2

q1 q2q1 q2

ξ

0

Page 32: CS 208: Computing Theory

Another example: M1

What language does M1 accept?

q1 q2

0 1 1

0

Page 33: CS 208: Computing Theory

Another example:M1

What language does M1 accept?

M1 accepts all the strings that end with a 1

q1 q2

0 1 1

0

Page 34: CS 208: Computing Theory

Another example:M2

q1 r1

a b

q2 r2

s

a

ab

b a

a b

b

Page 35: CS 208: Computing Theory

Another example:M2

What language does M2 accept?

q1 r1

a b

q2 r2

s

a

ab

b a

a b

b

Page 36: CS 208: Computing Theory

Another example:M2

What language does M2 accept?

M1 accepts all the strings that start and end with the same symbol!

q1 r1

a b

q2 r2

s

a

ab

b a

a b

b

Page 37: CS 208: Computing Theory

Languages

The language of machine M is the set of strings A that M accepts L(M) = A

Note that M can accept many strings … but M accepts only one language

We say that M recognizes language A if A= {w | M accepts w}

Page 38: CS 208: Computing Theory

Regular language

A language is called a regular language if some finite automaton recognizes it

Page 39: CS 208: Computing Theory

Formal model of computation

Let M= (Q,∑,ξ,q0,F) be a Deterministic Finite Automaton (DFA)

Let w=w1w2…wn be a string over ∑

M accepts w if a sequence of states r0,…,rn exist such that:

1. r0=q0

2. ξ(ri,wi+1)=ri+1, i<n

3. rn Є F

Page 40: CS 208: Computing Theory

Formal model of computation

Let M= (Q,∑,ξ,q0,F) be a Deterministic Finite Automaton (DFA)

Let w=w1w2…wn be a string over ∑

M accepts w if a sequence of states r0,…,rn exist such that:

1. r0=q0 : the machine starts in the start state

2. ξ(ri,wi+1)=ri+1, i<n: the machines goes from state to state according to the transition function

3. rn Є F: machine accepts its input if it ends up in an accept state

Page 41: CS 208: Computing Theory

The regular operations

Union operation

A U B = {x | x Є A or x Є B}

It takes all string and A and B and puts them together into one language

Page 42: CS 208: Computing Theory

The regular operations

Union operation

A U B = {x | x Є A or x Є B}

It takes all string in A and B and puts them together into one language

Let A={good, bad} and B ={boy, girl} A U B ={good, bad, boy, girl}

Page 43: CS 208: Computing Theory

The regular operations

Concatenation operation

A o B = {xy| x Є A and y Є B}

It attaches a string from B to the end of a string from A in the new language

Page 44: CS 208: Computing Theory

The regular operations

Concatenation operation

A o B = {xy| x Є A and y Є B}

It attaches a string from B to the end of a string from A in the new language

Let A={good, bad} and B ={boy, girl} A o B = {goodboy, goodgirl, badboy, badgirl}

Page 45: CS 208: Computing Theory

The regular operations

Star operation

A*={x1x2…xk| k is positive and each xi Є A}

It applies to a single language

Attaches any number of string from A to get a string in the new language

Page 46: CS 208: Computing Theory

The regular operations

Star operation

A*={x1x2…xk| k is positive and each xi Є A}

It applies to a single language Attaches any number of string from A to get a string in the new

language

Let A={good, bad} A* = {Ø, good,bad, goodgood, goodbad, badbad, badgood,

gooodgooodgood, goodgoodbad, goodbadgood,…}

Page 47: CS 208: Computing Theory

“Closed under”

The set of natural number N={1,2,3,….} is closed under multiplication For any a and b in N, a x b is also in N

3 x 5 = 15: 3, 5, and 15 are all naturals

In general, a collection of objects is closed under some operation if applying that operation to members of the collection returns and object still in the collection

Note; the set of natural numbers is not closed under division 3 / 5 is not a natural number

Page 48: CS 208: Computing Theory

Regular languages closed under union

Theorem: The class of regular languages is closed under the union operation

If A1 and A2 are regular languages, so is A1 U A2

Page 49: CS 208: Computing Theory

Proof idea

How to prove such a theorem?

Remember that the only result we have seen so far is

“A language is called a regular language if some finite automaton recognizes it”

Page 50: CS 208: Computing Theory

Proof idea

“A language is called a regular language if some finite automaton recognizes it”

So, let us look at our theorem from this perspective: If A1 and A2 are regular languages, so is A1 U A2

M1

Page 51: CS 208: Computing Theory

Proof idea

“A language is called a regular language if some finite automaton recognizes it”

So, let us look at our theorem from this perspective: If A1 and A2 are regular languages, so is A1 U A2

M1 M2

Page 52: CS 208: Computing Theory

Proof idea

“A language is called a regular language if some finite automaton recognizes it”

So, let us look at our theorem from this perspective: If A1 and A2 are regular languages, so is A1 U A2

M1 M2 MCan we construct?

Page 53: CS 208: Computing Theory

Proof: approach

Approach: proof by construction Some M1 accepts A1 Some M2 accepts A2 Construct M that accepts A1 U A2

Page 54: CS 208: Computing Theory

Proof: first attempt

Given M1 and M2 First simulate M1 If M1 doesn’t accept then simulate M2

Page 55: CS 208: Computing Theory

Proof: first attempt

Given M1 and M2 First simulate M1 If M1 doesn’t accept then simulate M2

What is wrong with this approach? Once the symbols are read by M1, they are lost! We cannot simulate M2! We need another way

Page 56: CS 208: Computing Theory

Proof: a second attempt

We need another way: Simulate both machines simultaneously! That way only one pass thru the input is necessary

But, can we keep of both simulations with finite memory?

Page 57: CS 208: Computing Theory

Proof: a second attempt

But, can we keep of both simulations with finite memory?

Answer: All you need to remember is the sate that each machine would be in if it had read up to this point in the input You need to remember a pair of states!

Page 58: CS 208: Computing Theory

Proof: a second attempt

q1 q2 r1 r2

q1,r1

M1 M2

M

Page 59: CS 208: Computing Theory

Proof: a second attempt

q1 q2 r1 r2

q1,r1 q1,r2

M1 M2

M

Page 60: CS 208: Computing Theory

Proof: a second attempt

q1 q2 r1 r2

q1,r1 q1,r2

q2,r1

M1 M2

M

Page 61: CS 208: Computing Theory

Proof: a second attempt

q1 q2 r1 r2

q1,r1 q1,r2

q2,r1 q2,r2

M1 M2

M

Page 62: CS 208: Computing Theory

Proof: a second attempt

q1 q2 r1 r2

q1,r1 q1,r2

q2,r1 q2,r2

M1 M2

M

Page 63: CS 208: Computing Theory

Proof: a second attempt: Accept sates

q1 q2 r1 r2

q1,r1 q1,r2

q2,r1 q2,r2

M1 M2

M

Page 64: CS 208: Computing Theory

Proof: a second attempt: Accept sates

q1 q2 r1 r2

q1,r1 q1,r2

q2,r1 q2,r2

M1 M2

M

Page 65: CS 208: Computing Theory

Proof: formal details

Let M1 = (Q1,∑,ξ1,q1,F1) accept A1

Let M2 = (Q2,∑,ξ2,q2,F2) accept A2 Define M = (Q,∑,ξ,q,F) as follows

1. Q = {(r1,r2) | r1 Є Q1 and r2 Є Q2}

This set is the Cartesian product of sets Q1 and Q2

Denoted by Q1x Q2

Page 66: CS 208: Computing Theory

Proof: formal details

Let M1 = (Q1,∑,ξ1,q1,F1) accept A1

Let M2 = (Q2,∑,ξ2,q2,F2) accept A2 Define M = (Q,∑,ξ,q,F) as follows

1. …

2. ∑ is the same

Page 67: CS 208: Computing Theory

Proof: formal details

Let M1 = (Q1,∑,ξ1,q1,F1) accept A1

Let M2 = (Q2,∑,ξ2,q2,F2) accept A2 Define M = (Q,∑,ξ,q,F) as follows

1. …

2. …

3. ξ, the transition function is defined as follows

ξ((r1,r2),a) = (ξ1(r1,a), ξ2(r2,a))

Page 68: CS 208: Computing Theory

Proof: formal details

Let M1 = (Q1,∑,ξ1,q1,F1) accept A1

Let M2 = (Q2,∑,ξ2,q2,F2) accept A2 Define M = (Q,∑,ξ,q,F) as follows

1. …

2. …

3. …

4. q is the pair (q1,q2)

Page 69: CS 208: Computing Theory

Proof: formal details

Let M1 = (Q1,∑,ξ1,q1,F1) accept A1 Let M2 = (Q2,∑,ξ2,q2,F2) accept A2 Define M = (Q,∑,ξ,q,F) as follows

1. …2. …3. …4. …5. F is the set of pairs in which either member is an accept state of

M1 or M2

F = {(r1,r2) | r1 Є F1 or r2 Є F2}

Page 70: CS 208: Computing Theory

Proof: formal details

Let M1 = (Q1,∑,ξ1,q1,F1) accept A1

Let M2 = (Q2,∑,ξ2,q2,F2) accept A2 Define M = (Q,∑,ξ,q,F) as follows

1. …

2. …

3. …

4. …

5. Why not choose F as follows

F = {(r1,r2) | r1 Є F1 and r2 Є F2}=F1 x F2 ?

Page 71: CS 208: Computing Theory

What about concatenation?

Theorem: The class of regular languages is closed under the concatenation operation

If A1 and A2 are regular languages, so is A1 o A2

Page 72: CS 208: Computing Theory

Proof Idea

Simulate M1 for a while, then switch to M2

BUT: when to switch?

… leads us into nondeterminism

Page 73: CS 208: Computing Theory

Conclusions

Deterministic Finite State Machines

Regular Languages

Regular OperationsUnionConcatenationStar

Next time: Nondeterministic FA