35
CSE2243 Theory of Computation and Compilers Lecture 1: Regular Languages Finite Automata

Lecture1 Finite Automata

Embed Size (px)

Citation preview

Page 1: Lecture1 Finite Automata

CSE2243 Theory of Computation and Compilers

Lecture 1:Regular Languages

Finite Automata

Page 2: Lecture1 Finite Automata

CSE2243 ­ Lecture 1 2

Areas in the Theory of Computation

● What are the fundamental capabilities and limitations of computers?

● Areas:● Automata● Computability● Complexity

Page 3: Lecture1 Finite Automata

CSE2243 ­ Lecture 1 3

Complexity

● How computationally hard a problem is

● Problem 1: Arranging a list of numbers in ascending order● Even a small computer can sort a million numbers rather quickly

● Problem 2: Finding a schedule of classes for the entire university to satisfy some reasonable constraints like no two classes can be scheduled in the same room at the same time● Much harder! If there are thousand classes, finding the best schedule may take 

centuries, even on a supercomputer

● What makes some problems computationally hard and others easy?

● Classify problems as easy or hard

Cypher
Highlight
Cypher
Highlight
Page 4: Lecture1 Finite Automata

CSE2243 ­ Lecture 1 4

Computability

● Certain basic problems cannot be solved by computers● Example: determining whether a mathematical 

statement is true or false – no computer algorithm can perform this task!

● Classify problems as those which can be solved and those which cannot

Cypher
Highlight
Cypher
Highlight
Page 5: Lecture1 Finite Automata

CSE2243 ­ Lecture 1 5

Automata

● Deals with the definitions and properties of mathematical models of computation

● Examples of models:● Finite automaton – used in text processing, 

compilers and hardware design● Context­free grammar – used in programming 

languages and artificial intelligence

● Practice with formal definitions of computation

Cypher
Highlight
Page 6: Lecture1 Finite Automata

CSE2243 ­ Lecture 1 6

Regular Languages

● What is a computer?● Use an idealized computer called a 

computational model● We will use several different computational 

models depending on the features we want to focus on

● We begin with the simplest model: finite state machine or finite automaton

Page 7: Lecture1 Finite Automata

CSE2243 ­ Lecture 1 7

Finite Automata

● Models computers with an extremely limited amount of memory (finite memory)

● Example: Controller for an automatic door● Pads on each side of the door● Door opens when someone steps on the front pad, 

if no one is standing on the rear pad

Cypher
Highlight
Page 8: Lecture1 Finite Automata

CSE2243 ­ Lecture 1 8

Automatic Door Example

● Two states of the controller representing the condition of the door:● OPEN● CLOSED

● Four possible input conditions● FRONT – someone is standing on the front pad● REAR – someone is standing on the rear pad● BOTH – people are standing on both pads● NEITHER – no one is standing on either pad

Page 9: Lecture1 Finite Automata

CSE2243 ­ Lecture 1 9

Automatic Door – State Diagram● State Diagram:

● State Transition Table:

● This controller is a computer that has just a single bit of memory, capable of recording which of the two states the controller is in.

● Other examples of computers with limited memories: elevators, dishwashers, electronic thermostats, parts of digital watches and calculators

Page 10: Lecture1 Finite Automata

CSE2243 ­ Lecture 1 10

DFA M1 (1)

● State Diagram:

● 3 States: q1, q

2 and q

3

● Start state: q1 (indicated by an arrow pointing at it from nowhere)

● Accept state: q2 (indicated by a double circle)

● Transitions: the arrows going from one state to another labelled by inputs

Page 11: Lecture1 Finite Automata

CSE2243 ­ Lecture 1 11

DFA M1 (2)

● How does the FA compute?● It is given and input string● It processes the string● It produces an output

● Processing:● Start in the start state● Read the input string, left to right

– After reading a symbol, M1 moves from current state to a new state by 

following the transition labeled with the input symbol

● After it processes the last symbol, M1 produces an output

– Accept if M1 is now in an accept state, otherwise reject

Page 12: Lecture1 Finite Automata

CSE2243 ­ Lecture 1 12

DFA M1 (3)

● How does M1 compute?

● On 1101?– Answer: It visits (in order) q

1, q

2, q

2, q

3, q

2    Accept

● On 01?

● On 101000?

● Does M1 accept or reject: 0? 1? 100? 01? 10? Any string 

that ends in 1?

Page 13: Lecture1 Finite Automata

CSE2243 ­ Lecture 1 13

Formal Definition of DFA (1)

● Definition of a DFA by a state diagram (Previous slide)

● Formal Definition of DFA● Why formal definition

● It is precise● No ambiguity● It provides notation

Cypher
Highlight
Cypher
Highlight
Cypher
Highlight
Cypher
Highlight
Page 14: Lecture1 Finite Automata

CSE2243 ­ Lecture 1 14

Formal Definition of DFA (2)

● A DFA is a 5­tuple (Q, ∑, δ, q0, F), where

● Q is a finite set called the states● ∑ is a finite set called the alphabet● δ: Q x ∑  Q is the → transition function

– Q x ∑ is the set of pairs (a, b) where a ∈ Q and b ∈ ∑● q

0 ∈ Q is the start state

● F ⊆ Q is the set of accept states (sometimes called the final states)

Cypher
Highlight
Cypher
Highlight
Cypher
Highlight
Cypher
Highlight
Cypher
Highlight
Cypher
Highlight
Cypher
Highlight
Cypher
Highlight
Cypher
Highlight
Cypher
Highlight
Cypher
Highlight
Cypher
Highlight
Cypher
Highlight
Cypher
Highlight
Cypher
Highlight
Cypher
Highlight
Cypher
Highlight
Page 15: Lecture1 Finite Automata

CSE2243 ­ Lecture 1 15

Formal Definition of M1

● M1 = (Q, ∑, δ, q

1, F )

● Q = {q1, q

2, q

3}

● ∑ = {0, 1}● δ is described by the table

● q1 is the start state

● F = {q2}

Cypher
Highlight
Page 16: Lecture1 Finite Automata

CSE2243 ­ Lecture 1 16

Language of a Machine M

● If A is the set of all strings that machine M accepts, then we say:● A is the language of machine M● L(M) = A● M recognizes A ● M accepts A

● A machine may accept several strings, but it always recognizes only one language

● If M accepts no strings, it recognizes one language, namely the empty language, ⌀

Cypher
Highlight
Page 17: Lecture1 Finite Automata

CSE2243 ­ Lecture 1 17

Language of M1

● What is the language of M1?

●  Answer: L(M1) = A, where

● A = {w | w contains at least one 1 and an even number of 0s follows the last 1}

● M1  recognises A

Page 18: Lecture1 Finite Automata

CSE2243 ­ Lecture 1 18

Example M2

● What is the formal description of M2?

● What is the language of M2?

Page 19: Lecture1 Finite Automata

CSE2243 ­ Lecture 1 19

Example M3

● What is the language of M3?

Page 20: Lecture1 Finite Automata

CSE2243 ­ Lecture 1 20

Example M4

● Formal Description of M4:

● M4 = ({s,q

1, q

2,r

1, r

2},{a,b}, δ, s, {q

1, r

1})

where   is given byδ

● What is the language of M4?

a b

s q1 r1

q1 q1 q2

q2 q1 q2

r1 r2 r1

r2 r2 r1

Page 21: Lecture1 Finite Automata

CSE2243 ­ Lecture 1 21

Formal Definition of Computation

● Let M = (Q, ∑, δ, q0, F) be a DFA

● Let w = w1 w

2 … w

n be a string over the alphabet ∑

● Then M accepts w if a sequence of states r0, r

1, … 

rn exists in Q with the following three conditions:

● r0 = q

0

● δ(ri,w

i+1) = r

i+1

● rn ∈ F

Page 22: Lecture1 Finite Automata

CSE2243 ­ Lecture 1 22

Designing DFAs (1)

● How to design a DFA to do a specific task?● Put yourself in the place of the machine you are designing● Ask: What states are needed?  What do I need to remember?● Then do the transitions and the rest of the state diagram

● Exercise● Suppose the alphabet is {0,1} and the language is the set of all 

strings with an odd number of 1s.● Design a DFA to recognize this language

Page 23: Lecture1 Finite Automata

CSE2243 ­ Lecture 1 23

Designing DFAs (2)

● Two states:

● qeven

: even number of 1s

● qodd

: odd number of 1s

● Note the counting idea in this example

Page 24: Lecture1 Finite Automata

CSE2243 ­ Lecture 1 24

Designing DFAs (3)● Exercise

● Suppose the alphabet is {0,1} and the language is the set of all strings that contain 001 as the substring– Eg: 0010, 1001, 001, 1111100111110 are in the language– Eg: 11, 1101, 0000 are not in the language

● Design a DFA to recognize this language– How many states?

● Note the substring idea in this example 

Page 25: Lecture1 Finite Automata

CSE2243 ­ Lecture 1 25

Regular Languages and Regular Operations

● Definition:● Language L is regular if L = L(M) for some DFA M

● One way to show a language is regular:● Give a DFA for it

● A tool for showing a language is regular: regular operations● If you have two regular languages● And you combine them by a regular operation,● Then the result is another regular language

Page 26: Lecture1 Finite Automata

CSE2243 ­ Lecture 1 26

Regular Operations ­ Context

● In arithmetic:● Basic objects are numbers● Basic tools (addition, multiplication, etc.) operate on 

numbers

● In theory of computation:● Basic objects are languages● Basic tools (regular operations) operate on 

languages

Page 27: Lecture1 Finite Automata

CSE2243 ­ Lecture 1 27

Regular Operations ­ Definition

● Let A and B be languages● We define regular operations as follows:

● Union: A ∪ B = {x | x ∈ A or x ∈ B}● Concatenation: A ° B = {xy | x ∈ A and y ∈ B}

● Star: A* = {x1x

2...x

k  | k ≥ 0 and each x

i ∈ A}

Page 28: Lecture1 Finite Automata

CSE2243 ­ Lecture 1 28

Regular Operations ­ Example

● Let the alphabet ∑ be {a, b, … z}● If A = {good, bad} and B = {boy, girl}, then

● A ∪ B = {good, bad, boy, girl}● A ° B = {goodboy, goodgirl, badboy, badgirl}● A* = {ε, good, bad, goodgood, goodbad, badgood, 

badbad, goodgoodgood, goodgoodbad, goodbadgood,...}

Page 29: Lecture1 Finite Automata

CSE2243 ­ Lecture 1 29

Regular Operations ­ Closure

● A collection of objects is closed under operation X if applying operation X to members of the collection returns an object still in the collection

● The natural numbers are closed under multiplication (Meaning for any natural numbers x and y, the product x x y is also a natural number)

● The class of regular languages is closed under each of the regular operations

Page 30: Lecture1 Finite Automata

CSE2243 ­ Lecture 1 30

Proving the Closure Theorem

● Theorem: the class of regular languages is closed under ● Union● Concatenation● Star

● The 3 proofs are by construction

Page 31: Lecture1 Finite Automata

CSE2243 ­ Lecture 1 31

Proof v/s Theorem

● A  proof of a mathematical statement is an argument that convinces the reader that the statement is true.● Different readers imply different proofs

● A theorem is a mathematical statement that has been proved true.● If we need the statement to prove another statement of greater 

interest, we call the first statement a lemma● If the theorem lets us easily conclude other, related statements, 

we call these other statements corollaries of the theorem

Page 32: Lecture1 Finite Automata

CSE2243 ­ Lecture 1 32

Types of Proofs

● Proof by contradiction● Assume that the statement is false● Show that this leads to an obviously false consequence, 

called a contradiction

● Proof by construction● If the theorem says an object of a certain type exists, 

demonstrate how to construct the object

● Proof by induction● Advanced method to show that all elements of an infinite 

set have a specified property

Page 33: Lecture1 Finite Automata

CSE2243 ­ Lecture 1 33

Theorem: The class of regular languages is closed under union

● That is:● If A

1 and A

2 are regular languages, then so is A

1 ∪ A

2

● Proof idea– We know that A

1 and A

2 are regular

● So, there is an M1  which recognizes A

1  and an M

2 which recognizes A

2

– We construct an M from M1  and M

2 that recognizes A

1 ∪ A

2

– M simulates both M1  and M

2 and accepts if either accepts

Page 34: Lecture1 Finite Automata

CSE2243 ­ Lecture 1 34

If A1 and A

2 are regular languages, 

then so is A1 ∪ A

2● M simulates both M

1  and M

2 and accepts if either accepts

● M does not know (in advance) whether M1 or M

2 will accept

● So, it must remember the state of each, i.e., a pair of states

● So, a state in M is a pair (x,y), where x is the state M1  would be in 

and y is the state M2 would be in

● Transitions in M go from pair to pair● The accept states in M are pairs (x,y) where either x is an accept 

state of M1 or y is an accept state of M

2  

Page 35: Lecture1 Finite Automata

CSE2243 ­ Lecture 1 35

If A1 and A

2 are regular languages, 

then so is A1 ∪ A

2● Proof

● Let M1  recognize A

1  where M

1  = (Q

1, ∑, δ

1, q

1, F

1)

● Let M2 recognize A

2  where M

2  = (Q

2, ∑, δ

2, q

2, F

2)

● We construct M that recognizes A1 ∪ A

2 where M = (Q, ∑, δ, q, F) where

– Q = {(r1,r

2)| r

1 ∈ Q

1 and r

2 ∈ Q

2}

● ∑ is the same as the languages for M1 and M

2

● δ is defined as follows: For each (r1,r

2) ∈ Q  and each a in ∑, let 

         δ((r1,r

2),a) = (δ

1(r

1,a),δ

2(r

2,a))

● q = (q1,q

2)

● F = {(r1,r

2)| r

1 ∈ F

1 or r

2 ∈ F

2}

● It is evident from the construction of M that M recognizes A1 ∪ A

2