No Slide Title - Hiram College - Department of Computer Science

Document Sample
No Slide Title - Hiram College - Department of Computer Science Powered By Docstoc
					Finite Automata

    CPSC 388
   Ellen Walker
  Hiram College
  Machine Model of Computation

• Strings in the language are accepted
• Strings not in the language are rejected
 Deterministic Finite Automaton

Accepts strings with an odd number of 1’s
 (e.g. 1011, 110111, 10, 01110)
            Parts of a DFA

• An alphabet (S)
• A finite set of states (S)
• A deterministic set of transitions from
  one state to the next (T : S x S ® S)
• One start state (s0 Î S)
• One or more accepting states (A Ì S)
Deterministic Transition Table
        Testing some strings

•   1001 reject
•   0101 reject
•   10101 accept
•   0      reject
•   1      accept
•   010101011 accept
      Data for DFA in C++
Class State{
  string name;
  int index; // unique integer
  bool isFinal; //true if final state
State start;
   Implementing DFA in C++
//Construct all the states, set isFinal
//Create next array from transitions
int machine(istream &sin){
   //sin.get() reads a single character from sin
   for(State S=start; c=sin.get();
  //Action for S would go here...
   return S.isFinal;
What Language is This?
              Draw DFA for:

•   {aa, ab, bb}
•   (ab)*
•   (a+ab+aab)*b
•   All strings with 1 a and 2 b’s
•   All strings containing ab
•   All strings that don’t contain ab
•   All strings that don’t contain abc
         Deterministic vs.
• Deterministic: exactly one new state for
  a given state,character pair
• Non-deterministic: 0 or more new
  states for each state,character pair
  – Assume machine will always make the
    right choice!
  – Add e-transitions (move w/o symbol)
NFA: All strings containing “ab”
     Are NFA’s More Powerful?

• Yes if ...
   – There is at least one language that can be
     accepted by an NFA but no DFA

• No if …
   – For every NFA, we can make a DFA that
     accepts the same language
          From NFA to DFA

• Each DFA state corresponds to a set of
  states in the NFA
  – DFA has max 2n states if NFA has n states
    (still finite)
• Transition from {s1,s2} on a is union of
  transitions from s1 on a and s2 on a
       Conversion Algorithm

• Copy the transitions from the start state.
• Choose one of the “new state” sets.
  Create a new row for that set, and
  construct its transitions (by unioning
  transitions from original table)
• Repeat until every set in the table has a
Example: DFA for (a|b)*ab(a|b)*

• Initial NFA table

• Copy start state, create transition for
Final NFA
           Minimizing DFA

• If 2 states have different isFinal, they
  are different
• If 2 states have transitions on the same
  character to states that are known
  different, they are different
• Otherwise, the states are the same and
  can be merged (repeat until all different)
       Minimization Example

s1 != {s1,s3} s1 != {s1,s2,s3} (final)
s1 != {s1,s2} (transition on b)
{s1,s2} != {s1,s3} {s1,s2} != {s1,s2,s3} (final)
We cannot say {s1,s3} != {s1,s2,s3}, so merge!
            Why minimize?

• Implement machine with smaller table
• Compare two machines
  – If both are minimized, if both compute the
    same language, then the machines will be
    identical except for state names
  DFA and Regular Expression

• The set of languages accepted by
  DFA’s = the set of languages accepted
  by regular expressions
  – For every r.e. we can make a DFA that
    accepts the same language
  – For every DFA we can make a r.e. that
    accepts the same language
  For every r.e. there is a DFA...

• If we can construct an NFA, that’s good
• Prove by induction:
  – Base cases: Æ, e, a (element of S) -- build
    an NFA for each.
  – Step cases: r | s , rs , r* -- build resulting
    NFA from NFA’s from r and/or s

Shared By: