0% found this document useful (0 votes)
32 views8 pages

1 Dfa

CS 476

Uploaded by

berkozkanjp
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views8 pages

1 Dfa

CS 476

Uploaded by

berkozkanjp
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

CS 476 – 1 - Finite Automata

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

2 Finite Automata
Automata: Plural of “automaton” (= machine)

A Finite State Machine is dened with:


• Finite set of states
• Finite set of inputs
• Transition function
• A special start state
Examples:

TV:

start off on
switch off

switch on
turn off
turn on

switch off

standby

PC:

standby
switch off
switch on
stand by

switch off
switch on
start off 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 0

q0
0 q1
start

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 denitions:

• 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


Denition: 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.

2
Transition diagram representation:

1 0

q0
0 q1
start

Transition table representation:

Q/Σ 0 1
→ q0 q1 q0
∗q1 q1 q0
Denition: A DFA M is said to accept an input string if its computation ends at an accept state.
eg. M accepts 0100.
Denition: 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 1
0
0
start q0
1

q2 0,1

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

0
1
q1 q3 1
0
0
start q0
1

q2 0,1

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

0 0

q0
1 q1
start

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

0 0

q0
1 q1
start

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

1 0

q0
0 q1
start

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

1 0

q0
0 q1
1 q2
start 0,1

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

1 0

q0
0 q1
1 q2
0 q3
start 0,1

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

start n0 even, n1 even n0 odd, n1 even

1 1 1 1

n0 even, n1 odd n0 odd, n1 odd

• 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.

Denition: 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 0,1
1

1
start A B C

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 6= , 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 = z1 for some z.
• Thus, w = z11y 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.

7
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 ;
0 0,1 case B:
i f (w[ i ]== ’ 0 ’ ) s t a t e =A ;
1 e l s e s t a t e =C ;
1
start A B C break ;
case C:
0
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” ) ;
}

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 0

1 0
start 0 1 2 3 4 5 22

For implementation, see


http://www.cs.bilkent.edu.tr/˜calkan/teaching/cs476/dfaMod.c

You might also like