Docsity

Theory of Computation: Finite Automata - Prof. Mahesh Viswanathan, Study notes of Computer Science

Partial preview of the text

Download Theory of Computation: Finite Automata - Prof. Mahesh Viswanathan and more Study notes Computer Science in PDF only on Docsity!

CS 373: Theory of Computation

Manoj Prabhakaran Mahesh Viswanathan

Fall 2008

1 Introduction

1.1 Problems and Computation

Decision Problems

Decision Problems Given input, decide “yes” or “no”

  • Examples: Is x an even number? Is x prime? Is there a path from s to t in graph G?
  • i.e., Compute a boolean function of input

General Computational Problem In contrast, typically a problem requires computing some non-boolean function, or carrying out interactive/reactive computation in a distributed environment

  • Examples: Find the factors of x. Find the balance in account number x.
  • In this course, we will study decision problems because aspects of computability are captured by this special class of problems

What Does a Computation Look Like?

  • Some code (a.k.a control): the same for all instances
  • The input (a.k.a problem instance)
  • As the program starts executing, some memory (a.k.a state)
    • Includes the values of variables (and the “program counter”)
    • State evolves throughout the computation
    • Often, takes more memory for larger problem instances
  • But some programs do not need larger state for larger instances!

1.2 Finite Automata: Informal Overview

Finite State Computation

  • Finite state: A fixed upper bound on the size of the state, independent of the size of the input
    • If t-bit state, at most 2t^ possible states

q 0 q 1

Figure 3: Transition Diagram of automaton

Example: Computation

  • On input 1001, the computation is
    1. Start in state q 0. Read 1 and goto q 1.
    2. Read 0 and goto q 1.
    3. Read 0 and goto q 1.
    4. Read 1 and goto q 0. Since q 0 is not a final state 1001 is rejected.
  • On input 010, the computation is
    1. Start in state q 0. Read 0 and goto q 0.
    2. Read 1 and goto q 1.
    3. Read 0 and goto q 1. Since q 1 is a final state 010 is accepted.

q 0 q 1

1.3 Examples

Example I

q 0

Figure 4: Automaton accepts all strings of 0s and 1s

Example II

q 0 q 1

Figure 5: Automaton accepts strings ending in 1

Example III

q 0 q 1

Figure 6: Automaton accepts strings having an odd number of 1s

Example IV

q 0 q 1

q 3 q 2

Figure 7: Automaton accepts strings having an odd number of 1s and odd number of 0s

1.4 Applications

Finite Automata in Practice

  • grep

Set Notation

We will often define languages using the set builder notation. Thus, L = {w ∈ Σ∗^ | p(w)} is the collection of all strings w over Σ that satisfy the property p.

Example 5. • L = {w ∈ { 0 , 1 }∗^ | |w| is even} is the set of all even length strings over { 0 , 1 }.

  • L = {w ∈ { 0 , 1 }∗^ | there is a u such that wu = 10001} is the set of all prefixes of 10001.

2.2 Deterministic Finite Automaton

Defining an Automaton

To describe an automaton, we to need to specify

  • What the alphabet is,
  • What the states are,
  • What the initial state is,
  • What states are accepting/final, and
  • What the transition from each state and input symbol is.

Thus, the above 5 things are part of the formal definition.

Deterministic Finite Automata Formal Definition

Definition 6. A deterministic finite automaton (DFA) is M = (Q, Σ, δ, q 0 , F ), where

  • Q is the finite set of states
  • Σ is the finite alphabet
  • δ : Q × Σ → Q “Next-state” transition function
  • q 0 ∈ Q initial state
  • F ⊆ Q final/accepting states

Given a state and a symbol, the next state is “determined”.

Computation

Definition 7. For a DFA M = (Q, Σ, δ, q 0 , F ), string w = w 1 w 2 · · · wk, where for each i wi ∈ Σ, and states q 1 , q 2 ∈ Q, we say q 1 w −→M q 2 if there is a sequence of states r 0 , r 1 ,... rk such that

  • r 0 = q 1 ,
  • for each i, δ(ri, wi+1) = ri+1, and
  • rk = q 2.

Definition 8. For a DFA M = (Q, Σ, δ, q 0 , F ) and string w ∈ Σ∗, we say M accepts w iff q 0 w −→M q for some q ∈ F.

Acceptance/Recognition and Regular Languages

Definition 9. The language accepted or recognized by a DFA M over alphabet Σ is L(M ) = {w ∈ Σ∗^ | M accepts w}. A language L is said to be accepted/recognized by M if L = L(M ).

Definition 10. A language L is regular if there is some DFA M such that L = L(M ).

Simple Observations about DFAs

Proposition 11. For a DFA M , string w, and state q 1 , there is exactly one state q 2 such that q 1 −→w M q 2.

Proof. By induction on |w|.

Proposition 12. For DFA M , strings u and v, and states q 1 and q 3 , q 1 −→uvM q 3 if and only if there is a state q 2 such that q 1 u −→M q 2 and q 2 v −→M q 3.

2.3 Formal Example of DFA

Formal Example of DFA

Example 13.

q 0 q 1

Figure 8: Transition Diagram of DFA

Formally the automaton is Modd = ({q 0 , q 1 }, { 0 , 1 }, δ, q 0 , {q 1 }) where

δ(q 0 , 0) = q 0 δ(q 0 , 1) = q 1 δ(q 1 , 0) = q 1 δ(q 1 , 1) = q 0

  • Induction Step w = u 1 : w has an odd number of 1s iff u has an even number of 1s iff q 0 −→u M q 0 (ind. hyp.) iff q 0 −→wM q 1 (since q 0 −→^1 M q 1 and q 1 −→^1 M q 0 ).

Proving Correctness of a DFA

Proof Template Given a DFA M having n states {q 0 , q 1 ,... qn− 1 } with initial state q 0 , to prove that L(M ) = L, we do the following.

  1. Come up with languages L 0 , L 1 ,... Ln− 1 such that ∪n i=0−^1 Li = Σ∗^ and are pairwise disjoint, i.e., Li ∩ Lj = ∅ for any i 6 = j
  2. Prove by induction on |w|, q 0 w −→M qi if and only if w ∈ Li
  3. Show that ∪qi∈F Li = L; the collection of all strings that reach a final state is exactly L.

3 Designing DFAs

3.1 General Method

Typical Problem

Problem Given a language L, design a DFA M that accepts L, i.e., L(M ) = L. How does one go about it?

Methodology

  • Imagine yourself in the place of the machine, reading symbols of the input, and trying to determine if it should be accepted.
  • Remember at any point you have only seen a part of the input, and you don’t know when it ends.
  • Figure out what to keep in memory. It cannot be all the symbols seen so far: it must fit into a finite number of bits.

3.2 Examples

Strings containing 0

Problem Design an automaton that accepts all strings over { 0 , 1 } that contain at least one 0.

Solution What do you need to remember? Whether you have seen a 0 so far or not!

qnoz qzer

1 0 ,^1

Figure 10: Automaton accepting strings with at least one 0.

Even length strings

Problem Design an automaton that accepts all strings over { 0 , 1 } that have an even length.

Solution What do you need to remember? Whether you have seen an odd or an even number of symbols.

qe qo

Figure 11: Automaton accepting strings of even length.

Pattern Recognition

Problem Design an automaton that accepts all strings over { 0 , 1 } that have 001 as a substring, where u is a substring of w if there are w 1 and w 2 such that w = w 1 uw 2.

Solution What do you need to remember? Whether you

  • haven’t seen any symbols of the pattern
  • Is L regular no matter what s is?
  • If yes, can M be built “efficiently”?

Knuth-Morris-Pratt (1977): Yes to both the above questions.

Multiples

Problem Design an automaton that accepts all strings w over { 0 , 1 } such that w is the binary representation of a number that is a multiple of 5.

Solution What do you need to remember? The remainder when divided by 5. How do you compute remainders?

  • If w is the number n then w0 is 2n and w1 is 2n + 1.
  • (a.b + c) mod 5 = (a.(b mod 5) + c) mod 5
  • e.g. 1011 = 11 (decimal) ≡ 1 mod 5 10110 = 22 (decimal) ≡ 2 mod 5 10111 = 23 (decimal) ≡ 3 mod 5

Automaton for recognizing Multiples

q 0

q 1

q 4

q 2

q 3

Figure 13: Automaton recognizing strings encoding binary number that are multiples of 5.

A One k-positions from end

Problem

Design an automaton for the language Lk = {w | kth character from end of w is 1}

Solution What do you need to remember? The last k characters seen so far! Formally, Mk = (Q, { 0 , 1 }, δ, q 0 , F )

  • States = Q = {〈w〉 | w ∈ { 0 , 1 }∗^ and |w| ≤ k}
  • δ(〈w〉, b) =

〈wb〉 if |w| < k 〈w 2 w 3... wkb〉 if w = w 1 w 2... wk

  • q 0 = 〈〉
  • F = {〈 1 w 2 w 3... wk〉 | wi ∈ { 0 , 1 }}

Lower Bound on DFA size

Proposition 15. Any DFA recognizing Lk has at least 2 k^ states.

Proof Let M , with initial state q 0 , recognize Lk and assume (for contradiction) that M has < 2 k^ states.

  • Number of strings of length k = 2k
  • There must be two distinct string w 0 and w 1 of length k such that for some state q, q 0 w 0 −→M q and q 0 −→w^1 M q.

Proof (contd)

Proof Let i be the first position where w 0 and w 1 differ. Without loss of generality assume that w 0 has 0 in the ith position and w 1 has 1.

w 00 i−^1 =...

k ︷ ︸︸ ︷ 0... 0 i−^1 w 10 i−^1 = (^) ︸︷︷︸... i− 1

k−i

0 i−^1

w 00 i−^1 6 ∈ Lk and w 10 i−^1 ∈ Lk. Thus, M cannot accept both w 00 i−^1 and w 10 i−^1.

Proof (contd)

... Almost there

Proof