# Finite Automata CPSC 388 Ellen Walker Hiram College.

## Presentation on theme: "Finite Automata CPSC 388 Ellen Walker Hiram College."— Presentation transcript:

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 (  ) A finite set of states (S) A deterministic set of transitions from one state to the next (T : S x  S) One start state (s 0  S) One or more accepting states (A  S)

Deterministic Transition Table

Testing some strings 1001reject 0101reject 10101accept 0reject 1accept 010101011accept

Results

Data for DFA in C++ Class State{ Public: string name; int index; // unique integer bool isFinal; //true if final state } State start; State next [MAXSTATES][MAXCHARS];

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(); S=next[S.index][c]); //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. Non-Deterministic 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  -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 2 n 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 row

Example: DFA for (a|b)*ab(a|b)* Initial NFA table Copy start state, create transition for {s1,s2}

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 enough Prove by induction: –Base cases: , , a (element of  ) -- build an NFA for each. –Step cases: r | s, rs, r* -- build resulting NFA from NFA’s from r and/or s