You are on page 1of 8

CS 476 1 - Finite Automata

Course topics:
1. Finite automata / regular expressions 2. Context-free grammars 3. Turing Machines 4. Decidability / undecidability 5. NP-Completeness

Finite Automata

Automata: Plural of automaton (= machine) A Finite State Machine is dened with: Finite set of states Finite set of inputs Transition function A special start state Examples: TV:
on

start

off

switch off

switch on

turn off turn on

switch off

standby

PC:

standby
switch off switch on stand by switch off

start

off

switch on

on
hibernate switch on

switch off

hibernate

Finite Automaton is a Finite State Machine with some accept state. M (q1 is the accept state): 1
q0

0 0
q1

start

1 input string w = 0100 is accepted by M . M also accepts 0, 10, 110, 010, . . ., i.e. all inputs that ends with 0.

2.1

Background Review

Some notations for sets A and B : A B : the cartesian product of A and B. eg. A = {a, b}, B = {x, y }, then A B = {(a, x), (a, y ), (b, x), (b, y )} Ak for integer k : A A . . . A (k times) 2A : the power set of A. eg. A = {a, b}; 2A = {, {a}, {b}, {a, b}} |A|: number of elements in A (cardinality of A). Some denitions: alphabet: a nite set of symbols. eg. 1 = {0, 1}, 2 = {a, b, c, . . . , z } string: a nite sequence of symbols; a.k.a. word. eg. w1 = 0100, w2 = abcbd language: a set of strings over an alphabet. eg. L1 = {01, 10, 001}, L2 = {w {0, 1} : w ends with 0}

2.2

Deterministic Finite Automata

Denition: A deterministic nite automaton (DFA) is a 5-tuple (Q, , , q0 , F ) where: Q is a nite set of states. is a nite alphabet. : Q Q is the transition function. q0 Q is the start state. F Q is the set of accept states.

Transition diagram representation:

1
q0

0 0
q1

start

1 Transition table representation: Q/ q0 q1 0 q1 q1 1 q0 q0

Denition: A DFA M is said to accept an input string if its computation ends at an accept state. eg. M accepts 0100. Denition: The language of a DFA M , L(M ), is the set of all input strings accepted by M . eg. L(M ) = {w|w ends with 0} Examples: L : {w|w begins with 0}

q1

0,1

0
start q0

1
q2

0,1

L : {w|w begins with 0 and ends with 1}

0 1
q1 q3

0
start q0

0 1
q2

0,1

L : {w|w begins and ends with 0}

0 1
q1 q3

0
start q0

0 1
q2

0,1

L : {w|w has even number of 1s}

0
q0

0 1
q1

start

1 L : {w|w has odd number of 1s}

0
q0

0 1
q1

start

1 L : {w|w ends with 1 or w = }

1
q0

0 0
q1

start

1 L : {w|w contains 01 as a substring }

1
q0

0 0
q1

start

q2

0,1

L : {w|w contains 010 as a substring }

1
q0

0 0
q1

start

q2

q3

0,1

1 L : {w|n0 (w) is even and n1 (w) is even} 0


n0 even, n1 even

start

n0 odd, n1 even

0 1 1 0 1 1

n0 even, n1 odd

n0 odd, n1 odd

0 L : {w|n0 (w) n1 (w) mod 2} Same as above, but n0 odd, n1 odd is also an accept state. L : {w|n0 (w) = n1 (w)} Cannot be accepted by a DFA.

Denition: A language is regular if it is accepted by some DFA. eg. L1 = {w|w ends with 0} L2 = {w|w contains an even number of 0s} ... Example: Theorem 2.1 The language of the DFA M below is L = {w|w {0, 1} and w does not have two consecutive 1s}. M: 0 1
B

0,1 1

start

0 5

Proof We have two sets S and T : S = the language of M T = the set of strings of 0s and 1s with no consecutive 1s To prove S = T , we need to prove both S T and T S . That is: if w S , then w T . if w T , then w S . Inductive hypothesis for Part 1 (S T ): 1. If (A, w) = A, then w has no consecutive 1s and does not end in 1. 2. If (A, w) = B , then w has no consecutive 1s and ends in a single 1. Basis: |w| = 0; i.e. w = (1) holds since has no 1s at all. (2) holds vacuously, since (A, ) is not B . Induction: Assume (1) and (2) are true for strings shorter than w, where |w| 1. Because w = , we can write w = xa, where a is the last symbol of w, and x is the string that precedes. IH holds for x. Need to prove (1) and (2) for w = xa. (1) for w is: If (A, w) = A, then w has no consecutive 1s and does not end in 1. Since (A, w) = A, (A, x) must be A or B , and a must be 0 (look at the DFA). By the IH, x has no 11s. Thus, w has no 11s and does not end in 1. Now, prove (2) for w = xa: If (A, w) = B , then w has no 11s and ends in 1. Since (A, w) = B , (A, x) must be A, and a must be 1 (look at the DFA). By the IH, x has no 11s and does not end in 1. Thus, w has no 11s and ends in 1. Inductive hypothesis for Part 2 (T S ): if w has no 11s, then w is accepted by M . Contrapositive: If w is not accepted by M , then w has 11. Using the contrapositive: Because there is a unique transition from every state on every input symbol, each w gets the DFA to exactly one state. The only way w is not accepted is if it gets to C . The only way to get to C (formally: (A, w) = C ) is if w = x1y , x gets to B , and y is the tail of w that follows what gets to C for the rst time. If (A, x) = B then surely x = z 1 for some z . Thus, w = z 11y and has 11. 6

2.3

Nonregular Languages

Some languages are nonregular. L1 = {0n 1n } is nonregular since DFAs cannot count. L2 = {w|w {(, )} and w is balanced.}. Balanced parentheses are those sequences of parentheses that can appear in an arithmetic expression. E.g.: (), ()(), (()), (()()) . . . You can use CFGs to represent L1 and L2 above.

2.4

Regular Languages

They appear in many contexts and have many useful properties. Examples: L3 = {w|w {0, 1} and w, viewed as a binary integer is divisible by 23} DFA M3 to recognize L3 : 23 states, named 0, 1, . . . , 22 that correspond to the 23 remainders of an integer divided by 23. Start and only nal state is 0. If string w represents integer i, then assume (0, w) = i mod 23. Then w0 represents integer 2i, so we want (i mod 23, 0) = (2i) mod 23. Similarly: w1 represents 2i + 1, so we want (i mod 23, 1) = (2i + 1) mod 23. Example: (15, 0) = 30 mod 23 = 7; (11, 1) = 23 mod 23 = 0. Example 2: L4 = {w|w {0, 1} and w, viewed as the reverse of a binary integer is divisible by 23} 01110100 is in L4 , because its reverse, 00101110 is 46 in binary. Hard to construct the DFA. But there is a theorem that says the reverse of a regular language is also regular.

2.5

Implementation of DFA
enum STATES {A, B , C} s t a t e ; int i ; char w [ ] ; / / f r o m t h e u s e r s t a t e = A; / / A i s s t a r t s t a t e f o r ( i = 0 ; i < s t r l e n (w ) ; i ++) { switch ( s t a t e ){ case A: i f (w[ i ]== 0 ) s t a t e =A ; e l s e s t a t e =B ; break ; case B: i f (w[ i ]== 0 ) s t a t e =A ; e l s e s t a t e =C ; break ; case C: s t a t e =C ; break ; } } i f ( s t a t e == A | | s t a t e == B) { p r i n t f ( accepted .\ n ) ; } else{ p r i n t f ( r e j e c t e d .\ n ) ; }

0 1
start A B

0,1 1

Recall L3 = {w|w {0, 1} and w, viewed as a binary integer is divisible by 23} if (w)2 = (i)10 ; then: (0, w) = i mod 23. Thus Q = {0, 1, . . . , 22}, q0 = {0} and F = {0}. Note that (w0)2 = (2i)10 and (w1)2 = (2i + 1)10 . (0, w) = i mod 23. (0, w0) = 2i mod 23. (0, w0) = ( (0, w), 0) = (i mod 23, 0) = 2i mod 23. Similarly : (0, w1) = (2i + 1) mod 23. (0, w1) = ( (0, w), 1) = (i mod 23, 0) = (2i + 1) mod 23. DFA (partial):
0 1 1 0 0

start

3
1

22

For implementation, see http://www.cs.bilkent.edu.tr/calkan/teaching/cs476/dfaMod.c 8

You might also like