snick snack cpsc 121: models of computation 2010 winter term 2 dfas in depth benjamin israel...

37
snick snack CPSC 121: Models of Computation 2010 Winter Term 2 DFAs in Depth Benjamin Israel [email protected] Notes heavily borrowed from Steve Wolfman’s, whose slides were based on notes by Patrice Belleville and others

Post on 21-Dec-2015

214 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Snick  snack CPSC 121: Models of Computation 2010 Winter Term 2 DFAs in Depth Benjamin Israel bisrael@cs.ubc.ca Notes heavily borrowed from Steve Wolfman’s,

snick

snack

CPSC 121: Models of Computation2010 Winter Term 2

DFAs in DepthBenjamin Israel

[email protected] heavily borrowed from Steve

Wolfman’s, whose slides were based on notes by Patrice Belleville and

others

Page 2: Snick  snack CPSC 121: Models of Computation 2010 Winter Term 2 DFAs in Depth Benjamin Israel bisrael@cs.ubc.ca Notes heavily borrowed from Steve Wolfman’s,

Outline

• Learning Goals

• Formally Specifying DFAs

• Designing DFAs

• Analyzing DFAs: Can DFAs Count?

• Non-Deterministic Finite Automata: Are They More Powerful than DFAs?

2

Page 3: Snick  snack CPSC 121: Models of Computation 2010 Winter Term 2 DFAs in Depth Benjamin Israel bisrael@cs.ubc.ca Notes heavily borrowed from Steve Wolfman’s,

Learning Goals: In-Class

By the end of this unit, you should be able to for the exam:– Build a DFA to recognize a particular

language.– Identify the language recognized by a

particular DFA.

Page 4: Snick  snack CPSC 121: Models of Computation 2010 Winter Term 2 DFAs in Depth Benjamin Israel bisrael@cs.ubc.ca Notes heavily borrowed from Steve Wolfman’s,

Learning Goals: In-Class

By the end of this unit, you should be able to for yourself:– Use the formalisms that we’ve learned so far

(especially power sets and set union) to illustrate that nondeterministic finite automata are no more powerful than deterministic finite automata.

– Demonstrate through a simple contradiction argument that there are interesting properties of programs that cannot be computed in general.

– Describe how the theoretical story 121 has been telling connects to the branch of CS theory called automata theory.

Page 5: Snick  snack CPSC 121: Models of Computation 2010 Winter Term 2 DFAs in Depth Benjamin Israel bisrael@cs.ubc.ca Notes heavily borrowed from Steve Wolfman’s,

Outline

• Learning Goals

• Formally Specifying DFAs

• Designing DFAs

• Analyzing DFAs: Can DFAs Count?

• Non-Deterministic Finite Automata: Are They More Powerful than DFAs?

5

Page 6: Snick  snack CPSC 121: Models of Computation 2010 Winter Term 2 DFAs in Depth Benjamin Israel bisrael@cs.ubc.ca Notes heavily borrowed from Steve Wolfman’s,

Reminder: Formal DFA Specification

I the (finite) set of letters in the input language.

S the (finite) set of states.

s0 the start state; s0 SF the set of accepting

states; F SN : S I S is the

transition function.

a

b

b

a,b

a,ba

Page 7: Snick  snack CPSC 121: Models of Computation 2010 Winter Term 2 DFAs in Depth Benjamin Israel bisrael@cs.ubc.ca Notes heavily borrowed from Steve Wolfman’s,

One Way to FormalizeDFA Operation

The algorithm “RunDFA” runs a given DFA on an input, resulting in “Yes”, “No”, or (if the input is invalid) “Huh?”

RunDFA((I, S, s0, F, N), in)• If in is empty, then:

if s0 F, return “Yes”, else return “No”.• Otherwise, let in0 be the first element of in and inrest be the rest of in.

• If in0 I, return “Huh?”• Otherwise, let s' = N(s0, in0).• Return RunDFA((I, S, s', F, N), inrest).

ab

b

a,b

a,ba

Page 8: Snick  snack CPSC 121: Models of Computation 2010 Winter Term 2 DFAs in Depth Benjamin Israel bisrael@cs.ubc.ca Notes heavily borrowed from Steve Wolfman’s,

Formal DFA Example (Fixed)

A DFA is a five-tuple: (I, S, s0, F, N)

a b

b

a,b

What is I? S? s0? F? N?

a,b

a

Page 9: Snick  snack CPSC 121: Models of Computation 2010 Winter Term 2 DFAs in Depth Benjamin Israel bisrael@cs.ubc.ca Notes heavily borrowed from Steve Wolfman’s,

Running the DFA

How does the DFA runner work?

a b

b

a,b

a,b

a

Input: abbabOnly partly on the exam.

(You should be able to simulate a DFA, but needn’t use RunDFA.)

Page 10: Snick  snack CPSC 121: Models of Computation 2010 Winter Term 2 DFAs in Depth Benjamin Israel bisrael@cs.ubc.ca Notes heavily borrowed from Steve Wolfman’s,

Outline

• Prereqs, Learning Goals, and !Quiz Notes

• Formally Specifying DFAs

• Designing DFAs

• Analyzing DFAs: Can DFAs Count?

• Non-Deterministic Finite Automata: Are They More Powerful than DFAs?

10

Page 11: Snick  snack CPSC 121: Models of Computation 2010 Winter Term 2 DFAs in Depth Benjamin Israel bisrael@cs.ubc.ca Notes heavily borrowed from Steve Wolfman’s,

Strategy for Designing a DFA

To design a DFA for some language, try:1. Should the empty string be accepted? If so, draw

an accepting start state; else, draw a rejecting one.

2. From that state, consider what each letter would lead to. Group letters that lead to the same outcome. (Some may lead back to the start state if they’re no different from “starting over”.)

3. Decide for each new state whether it’s accepting.4. Repeat steps 2 and 3 for new states as long as

you have unfinished states, always trying to reuse existing states!

As you go, give as descriptive names as you can to your states, to figure out when to reuse them!

Page 12: Snick  snack CPSC 121: Models of Computation 2010 Winter Term 2 DFAs in Depth Benjamin Israel bisrael@cs.ubc.ca Notes heavily borrowed from Steve Wolfman’s,

Problem: Design a DFA to Recognize “Decimal Numbers”

Rules?

Page 13: Snick  snack CPSC 121: Models of Computation 2010 Winter Term 2 DFAs in Depth Benjamin Israel bisrael@cs.ubc.ca Notes heavily borrowed from Steve Wolfman’s,

Problem: Design a DFA to Recognize “Decimal Numbers”

•Can have a negative sign

•Can have a decimal

•If decimal, needs at least one digit before and one digit after the decimal.

Page 14: Snick  snack CPSC 121: Models of Computation 2010 Winter Term 2 DFAs in Depth Benjamin Israel bisrael@cs.ubc.ca Notes heavily borrowed from Steve Wolfman’s,

Is this a decimal number?

Let’s build a DFA to check whether an input is a decimal number. Which of the following is a decimal number?

3.5 2.011 -6 5. -3.1415926536

.25 --3 3-5 5.2.5 .

Rules for whether something is a decimal number?

Page 15: Snick  snack CPSC 121: Models of Computation 2010 Winter Term 2 DFAs in Depth Benjamin Israel bisrael@cs.ubc.ca Notes heavily borrowed from Steve Wolfman’s,

Regular Expressions

• Syntax for defining which words to accept and which to reject

• Can be converted back and forth between DFAs

• Don’t worry too much about them now, they will be further introduced in lab

Page 16: Snick  snack CPSC 121: Models of Computation 2010 Winter Term 2 DFAs in Depth Benjamin Israel bisrael@cs.ubc.ca Notes heavily borrowed from Steve Wolfman’s,

Regular Expressions

Something followed by ? is optional.

Anything followed by a + can be repeated.

[0-9] is a digit, an element of the set{0, 1, …, 9}.

– is literally –. \. means.

() are used to group elements together.

Rules for a decimal number:

-?[0-9]+(\.[0-9]+)?

Page 17: Snick  snack CPSC 121: Models of Computation 2010 Winter Term 2 DFAs in Depth Benjamin Israel bisrael@cs.ubc.ca Notes heavily borrowed from Steve Wolfman’s,

Problem: Design a DFA to Recognize Telephone NumbersProblem: Design a DFA to recognize

telephone numbers

Step 1: Design a Regular Expression

Page 18: Snick  snack CPSC 121: Models of Computation 2010 Winter Term 2 DFAs in Depth Benjamin Israel bisrael@cs.ubc.ca Notes heavily borrowed from Steve Wolfman’s,

Problem: Design a DFA to Recognize Telephone NumbersProblem: Design a DFA to recognize

telephone numbers, defined using the regular expression:((1\-)?[2-9][0-9][0-9]-)?

[2-9][0-9][0-9]\-

[0-9][0-9][0-9][0-9]

Page 19: Snick  snack CPSC 121: Models of Computation 2010 Winter Term 2 DFAs in Depth Benjamin Israel bisrael@cs.ubc.ca Notes heavily borrowed from Steve Wolfman’s,

More DFA Design Problems

Design a DFA that recognizes words that have two or more contiguous sequences of at least two vowels. (This is a rough stab at “multisyllabic” words.)

Accepted: Sweetie, Bearee

Not accepted: Queue

Page 20: Snick  snack CPSC 121: Models of Computation 2010 Winter Term 2 DFAs in Depth Benjamin Israel bisrael@cs.ubc.ca Notes heavily borrowed from Steve Wolfman’s,

More DFA Design Problems

Design a DFA that recognizes base 4 numbers with at least one digit in which the digits are in sorted order.

Page 21: Snick  snack CPSC 121: Models of Computation 2010 Winter Term 2 DFAs in Depth Benjamin Israel bisrael@cs.ubc.ca Notes heavily borrowed from Steve Wolfman’s,

More DFA Design Problems

Design a DFA that recognizes strings containing the letters a, b, and c in which all the as come before the first c and no two bs neighbour each other.

Page 22: Snick  snack CPSC 121: Models of Computation 2010 Winter Term 2 DFAs in Depth Benjamin Israel bisrael@cs.ubc.ca Notes heavily borrowed from Steve Wolfman’s,

Outline

• Prereqs, Learning Goals, and !Quiz Notes

• Formally Specifying DFAs

• Designing DFAs

• Analyzing DFAs: Can DFAs Count?

• Non-Deterministic Finite Automata: Are They More Powerful than DFAs?

22

Page 23: Snick  snack CPSC 121: Models of Computation 2010 Winter Term 2 DFAs in Depth Benjamin Israel bisrael@cs.ubc.ca Notes heavily borrowed from Steve Wolfman’s,

Can a DFA Count?

Problem: Design a DFA to recognize anbn: the language that includes all strings that start with some number of as and end with the same number of bs.

Page 24: Snick  snack CPSC 121: Models of Computation 2010 Winter Term 2 DFAs in Depth Benjamin Israel bisrael@cs.ubc.ca Notes heavily borrowed from Steve Wolfman’s,

DFAs Can’t Count

Let’s prove it. The heart of the proof is the insight that a DFA can only have a finite number of states and so can only “count up” a finite number of as.

We proceed by contradiction. Assume some DFA recognizes the language anbn.

Page 25: Snick  snack CPSC 121: Models of Computation 2010 Winter Term 2 DFAs in Depth Benjamin Israel bisrael@cs.ubc.ca Notes heavily borrowed from Steve Wolfman’s,

DFAs Can’t Count

Assume DFA counter recognizes anbn.

counter must have some finite number of states k = |Scounter|.

Consider the input akbk. counter must repeat (at least) one state as it processes the k as in that string. (Because it starts in s0 and transitions k times; if it didn’t repeat a state, that would be k+1 states, which is greater than |Scounter|.)

(In fact, it repeats each state after the first one it repeats up to the b.)

Page 26: Snick  snack CPSC 121: Models of Computation 2010 Winter Term 2 DFAs in Depth Benjamin Israel bisrael@cs.ubc.ca Notes heavily borrowed from Steve Wolfman’s,

DFAs Can’t Count

Consider the input akbk. counter must repeat (at least) one state as it processes the k as in that string.

counter accepts akbk, meaning it ends in an accepting state.

...

...

Doesn’t necessarily look like this, but similar.

a

aa a

aab

b

Page 27: Snick  snack CPSC 121: Models of Computation 2010 Winter Term 2 DFAs in Depth Benjamin Israel bisrael@cs.ubc.ca Notes heavily borrowed from Steve Wolfman’s,

DFAs Can’t Count

Now, consider the number of as that are processed between the first and second time visiting the repeated state. Call it r.

Give counter a(k-r)bk instead.

(Note: r > 0.)

...

...

a

aa a

aab

b

Page 28: Snick  snack CPSC 121: Models of Computation 2010 Winter Term 2 DFAs in Depth Benjamin Israel bisrael@cs.ubc.ca Notes heavily borrowed from Steve Wolfman’s,

DFAs Can’t Count

Give counter a(k-r)bk instead.

counter must accept a(k-r)bk, which is not in the language anbn . Contradiction!

Thus, no DFA recognizes anbn

...

...

a

aa a

aab

b

Page 29: Snick  snack CPSC 121: Models of Computation 2010 Winter Term 2 DFAs in Depth Benjamin Israel bisrael@cs.ubc.ca Notes heavily borrowed from Steve Wolfman’s,

Outline

• Prereqs, Learning Goals, and !Quiz Notes

• Formally Specifying DFAs

• Designing DFAs

• Analyzing DFAs: DFAs Can’t Count

• Non-Deterministic Finite Automata: Are They More Powerful than DFAs?

29

Page 30: Snick  snack CPSC 121: Models of Computation 2010 Winter Term 2 DFAs in Depth Benjamin Israel bisrael@cs.ubc.ca Notes heavily borrowed from Steve Wolfman’s,

Let’s Try Something More Powerful (?): NFAs

An NFA is like a DFA except:• Any number (zero or more) of arcs can lead from

each state for each letter of the alphabet• There may be many start states

When we run a DFA, we put a finger on the current state and then read one letter of the input at a time, transitioning from state to state.

When we run an NFA, we may need many fingers to keep track of many current states. When we transition, we go to all the possible next states.

Page 31: Snick  snack CPSC 121: Models of Computation 2010 Winter Term 2 DFAs in Depth Benjamin Israel bisrael@cs.ubc.ca Notes heavily borrowed from Steve Wolfman’s,

NFAs Continued

An NFA is like a DFA except:• Any number (zero or more) of arcs can lead

from each state for each letter of the alphabet• There may be many start states

A DFA accepts when we end in an accepting state.

An NFA accepts when at least one of the states we end in is accepting.

Page 32: Snick  snack CPSC 121: Models of Computation 2010 Winter Term 2 DFAs in Depth Benjamin Israel bisrael@cs.ubc.ca Notes heavily borrowed from Steve Wolfman’s,

Another way to think of NFAs

An NFA is a non-deterministic finite automaton.

Instead of doing one predictable thing at each state given an input, it may have choices of what to do.

If one of the choices leads to accepting the input, the NFA will make that choice. (It “magically” knows which choice to make.)

Page 33: Snick  snack CPSC 121: Models of Computation 2010 Winter Term 2 DFAs in Depth Benjamin Israel bisrael@cs.ubc.ca Notes heavily borrowed from Steve Wolfman’s,

Decimal Number NFA... Is Easy!

s1 s2

-?[0-9]+(\.[0-9]+)?

int dot-

0-9

0-9 .

real

0-9

0-9

Page 34: Snick  snack CPSC 121: Models of Computation 2010 Winter Term 2 DFAs in Depth Benjamin Israel bisrael@cs.ubc.ca Notes heavily borrowed from Steve Wolfman’s,

Are NFAs More Powerful than DFAs?

Problem: Prove that every language an NFA can recognize can be recognized by a DFA as well.

Strategy: Given an NFA, show how to build a DFA (i.e., I, S, s0, F, and N) that accepts/rejects the same strings.

Hint: Formally, what is the “group of states” an NFA can be in at any given time? Build the parts of the DFA based on that (i.e., S is the set of all such things).

Page 35: Snick  snack CPSC 121: Models of Computation 2010 Winter Term 2 DFAs in Depth Benjamin Israel bisrael@cs.ubc.ca Notes heavily borrowed from Steve Wolfman’s,

Worked Problem: Are NFAs More Powerful than DFAs?

Problem: Prove that every language an NFA can recognize can be recognized by a DFA as well.

WLOG, consider an arbitrary NFA “C”. We’ll build a DFA “D” that does the same thing.

We now build each element of the five-tuple defining D. We do so in such a way that D faithfully simulates N’s operation.

Page 36: Snick  snack CPSC 121: Models of Computation 2010 Winter Term 2 DFAs in Depth Benjamin Israel bisrael@cs.ubc.ca Notes heavily borrowed from Steve Wolfman’s,

Worked Problem: Are NFAs More Powerful than DFAs?

C has an alphabet IC. Let ID = IC. (They operate on the same strings.)

C’s states are SC. Let SD = P(SC). (Each state in D represents being in a subset of the states in C.)

C starts in some subset S0C of SC. Let s0D = S0C. (D’s start state represents being in the set of start states in C.)

C has accepting states FC. Let FD = {S SC | S FC }. (A state in D is accepting if it represents a set of states in C that contains at least one accepting state.)

C has zero or more arcs leading out of each state in SC for each letter in IC. ND(sD, i) operates on a state sD that actually represents a set of states from SC. So:

DC ssCDD siisN

from labeled arcsby toconnected states ofset the),(

Page 37: Snick  snack CPSC 121: Models of Computation 2010 Winter Term 2 DFAs in Depth Benjamin Israel bisrael@cs.ubc.ca Notes heavily borrowed from Steve Wolfman’s,

Decimal Number NFA... as a DFA

-

0-9

0-9 .

0-9

0-9

{s1, s2} {s2} {s2, int} {dot}

{real}{ }

0-9

.else else

else

else

LOTS of states left off that are unreachable from the start state.