coms 3261, lecture 2

46
COMS 3261, Lecture 2 Strings, Languages, Automata September 6, 2001

Upload: yamin

Post on 09-Feb-2016

38 views

Category:

Documents


3 download

DESCRIPTION

COMS 3261, Lecture 2. Strings, Languages, Automata September 6, 2001. Agenda. Today Strings Languages Deterministic Finite Automata For next time: Read up to 1.2 Check out ABCEZ in lecture 2-4 directory. Due 9/13: HW 1. Vending Machine Example. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: COMS 3261, Lecture 2

COMS 3261, Lecture 2Strings, Languages, Automata

September 6, 2001

Page 2: COMS 3261, Lecture 2

AgendaToday Strings Languages Deterministic Finite AutomataFor next time: Read up to 1.2 Check out ABCEZ in lecture 2-4 directory.Due 9/13: HW 1

Page 3: COMS 3261, Lecture 2

Vending Machine ExampleVending machine dispenses soda for $0.45Accepts only dimes and quarters Eats your money if you don’t have correct change

Model this by Java-method pseudocode:

Page 4: COMS 3261, Lecture 2

Vending Machine ExampleSoda vend(){int total = 0, coin;while (total != 45){receive(coin);if ((coin==10 && total==40) ||(coin==25 && total>=25))reject(coin);elsetotal += coin;}return new Soda();

}

Page 5: COMS 3261, Lecture 2

Vending Machine ExampleWhy was this over-kill?

Page 6: COMS 3261, Lecture 2

Vending Machine ExampleWhy was this over-kill?1) Vending machines have been around long

before computers or Java!2) Don’t really need int’s. Each int

introduces 232 possibilities multiplicatively!!!

3) Don’t need to know how to add integers to model venting machine (total += coin)

4) if/else, Java grammar, all really artifices that just complicate the essence

Page 7: COMS 3261, Lecture 2

Vending Machine Example

Page 8: COMS 3261, Lecture 2

Vending Machine Example

Input: DQQDAbout to put in a dime

Page 9: COMS 3261, Lecture 2

Vending Machine Example

Input: DQQDAbout to put in a quarter

Page 10: COMS 3261, Lecture 2

Vending Machine Example

Input: DQQDAbout to put in a quarter

Page 11: COMS 3261, Lecture 2

Vending Machine Example

Input: DQQDAbout to put in a dime

Page 12: COMS 3261, Lecture 2

Vending Machine Example

Input: DQQDDONE! MONEY ACCEPTED.

Page 13: COMS 3261, Lecture 2

Vending Machine ExampleWhat made this example simpler

than the Java pseudocode?

Page 14: COMS 3261, Lecture 2

Vending Machine Example1. Only needed two coin types “D” and

“Q” – symbols/letters in alphabet2. Only needed 7 possible current total

amounts – states/nodes/vertices3. Much cleaner and aesthetically

pleasing than Java lingoNow generalize and abstractify…

Page 15: COMS 3261, Lecture 2

Alphabets, Strings, Languages

DEF: An alphabet is a set of symbols (characters, letters). A string (or word) over is a sequence of symbols. The empty string is the string containing no symbols at all, and is denoted by

Q1: What is in our vending machine example?

Q2: What are some good/bad strings in our example?

Q3: What does signify in our example?

Page 16: COMS 3261, Lecture 2

Alphabets, Strings, Languages

A1: D, Q A2:

Good: QDD, DQD, DDQ, QQQQDD, etc. Bad: Q, D, DD, etc. Ugly: DDD …now you’re screwed! A3: signifies trying to get something

for nothing (putting no money in at all).

Page 17: COMS 3261, Lecture 2

Alphabets, Strings, Languages

DEF: The length of a string is the number of symbols that it contains (repetitions allowed).

Absolute values are used to denote length.

EG: Lengths of the above good (QDD, DQD, DDQ, QQQQDD) are: 3, 3, 3, 6

Q: What’s the length of ?

Page 18: COMS 3261, Lecture 2

Alphabets, Strings, Languages

A:

Page 19: COMS 3261, Lecture 2

Alphabets, Strings, Languages

DEF: The concatenation of two strings is the string resulting from putting them together from left to right. Given strings u and v, denote the concatenation by u v, or just uv.

EG: ire land = ireland, QQ DD = QQDD, DDD u is still bad, no-matter what u is!

Q1: Why the last claim?Q2: What’s the Java equivalent of

concatenation?Q3: Find a formula for u v

Page 20: COMS 3261, Lecture 2

Alphabets, Strings, Languages

A1: You are still screwed no matter what combination of coins you put in.

A2: The + operator on strings.A3: u v u +v

Page 21: COMS 3261, Lecture 2

Alphabets, Strings, Languages

DEF: The reversal of a string u is denoted by u R.

EG: (banana)R = ananabDEF: If is an alphabet, denotes

the set of all strings over A language over is a subset of , i.e. a set of strings each consisting of sequences of letters in

Page 22: COMS 3261, Lecture 2

Alphabets, Strings, Languages

EG: D, Q ,

D, Q, DD, DQ, QD, QQ,DDD, DDQ, DQD, DQQ, QDD, QDQ, QQD, QQQ,DDDD, DDDQ, …

Define L = u| u successfully vends Classroom exercise: What are all the strings

in L of length 1? Of length 2? 3? 4? 5?

Page 23: COMS 3261, Lecture 2

Finite Deterministic Automata

0

1

0

1

1 1 0 0 1

More computer-like example:

01

sourcelessarrow denotes start

doublecircledenotesaccept

input puton taperead leftto right

Page 24: COMS 3261, Lecture 2

Finite Deterministic Automata

0

1

0

1

1 1 0 0 1

01

Page 25: COMS 3261, Lecture 2

Finite Deterministic Automata

0

1

0

1

1 1 0 0 1

01

Page 26: COMS 3261, Lecture 2

Finite Deterministic Automata

0

1

0

1

1 1 0 0 1

01

Page 27: COMS 3261, Lecture 2

Finite Deterministic Automata

0

1

0

1

1 1 0 0 1

01

Page 28: COMS 3261, Lecture 2

Finite Deterministic Automata

0

1

0

1

1 1 0 0 1

01

Page 29: COMS 3261, Lecture 2

Finite Deterministic Automata

0

1

0

1

1 1 0 0 1

01

REJECT!

Page 30: COMS 3261, Lecture 2

Finite Deterministic Automata

0

1

0

101

Q: What kinds of bitstrings are accepted?

Page 31: COMS 3261, Lecture 2

Finite Deterministic Automata

0

1

0

101

A: Bitstrings that represent binary even numbers.

Page 32: COMS 3261, Lecture 2

Finite Deterministic Automata

Exercise: Design with a friend a machine that tells us when a base-10 number is divisible by 3.

What should your alphabet be?How can you tell when a number is

divisible by 3?

Page 33: COMS 3261, Lecture 2

Finite Deterministic Automata

Solution: (except for

0 mod 3

1 mod 3

2 mod 30,3,6,9

0,3,6,9

0,3,6,9

1,4,7

1,4,7

1,4,7

2,5,8 2,5,82,5,8

Page 34: COMS 3261, Lecture 2

Formal Definition of FA DEF: A (deterministic ) finite

automaton (FA) consists of a set of states Q, an alphabet , labeled transitions between states , a start state q0 Q, and a set of accept states F. This is all encapsulated by the “5-tuple”

M = (Q, , , q0, F )

Page 35: COMS 3261, Lecture 2

Formal Definition of FANotice that the input string, as well

as the tape containing the input string, are implicit in the definition of an FA. I.e., definition only deals with static view. Further explaining needed for understanding how FA’s interact with their input.

Page 36: COMS 3261, Lecture 2

Why Deterministic?Deterministic means that there is

enough information to always determine which state the Automaton goes into next, when reading a particular symbol. Our Vending Machine Example was actually not deterministic because after $.45 have been deposited, the effects of an additional coin deposit are undefined.

Page 37: COMS 3261, Lecture 2

0 mod 3 1 mod 3

2 mod 3

0,3,6,9

0,3,6,9

0,3,6,9

1,4,7

1,4,71,4,7

2,5,8 2,5,8

2,5,8

Exercise: Find theformal description of this automaton.

Page 38: COMS 3261, Lecture 2

Definition of FA, exampleQ = { 0 mod 3, 1 mod 3, 2 mod 3 }

( rename: {q0, q1, q2} ) = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }q0 = 0 mod 3 F = { 0 mod 3 } -- further explanation required

Page 39: COMS 3261, Lecture 2

The labeling function tells us which state to go to, if

machine reads a given symbol. I.e., given a source state in Q and a letter in , defines a unique target state in Q. In other words, is a function from the Cartesian product Q x to Q : QQ :δ

Page 40: COMS 3261, Lecture 2

The labeling function

?),(δ:Question

.)5,(δ,)3,(δ ,)7,(δ ,)2,(δ,)9,(δ ,)2,(δ

122221

010020

jq

qqqqqqqqqqqq

QQ

i

Page 41: COMS 3261, Lecture 2

The labeling function

3 mod )(),(δ jii qjq Usually don’t have such neat andSimple formulas.

Page 42: COMS 3261, Lecture 2

Formal Definition of an FA:Dynamic

How does an FA operate on strings? Implicitly, there is some notion of an auxiliary tape containing the string. The FA reads the tape from left to right with each new character causing the FA to go into another state. When the string is completely read, the string is accepted depending on whether the FA’s final state was an accept state.

Page 43: COMS 3261, Lecture 2

Formal Definition of an FA:Dynamic

DEF: A string u is accepted by an automaton iff (IF and only iF ) the path starting at q0 which is labeled by u ends in an accept state.

Note: To really define what it means for string to label a path, you need to break u up into its sequence of characters and apply repeatedly, keeping track of states. See Sipser for further details.

Page 44: COMS 3261, Lecture 2

Language Accepted by an FA

DEF: The language accepted by an FA M is the set of all strings which are accepted by M and is denoted by L (M ).

Intuitively, think of all the possible ways of getting from the start state to any accept state. Then think of all the possible ways of labeling those paths (if there are multiple labels on some edges).

Page 45: COMS 3261, Lecture 2

Regular LanguagesWe will eventually see that not all

languages can be described as the accepted language of some FA. Languages which do belong to some FA, exhibit a high degree of regularity and fit the pattern defined by the FA. In fact,

DEF: A language L is called a regular language if some FA M exists such that L = L (M ).

Page 46: COMS 3261, Lecture 2

Blackboard exercises