theory of computation (fall 2013): closure properties of regular languages; dfa & nfa...

36
Theory of Computation Closure Properties of Regular Languages, DFA and NFA Implementation Vladimir Kulyukin http://www.vkedco.blogspot.com/

Upload: vladimir-kulyukin

Post on 06-May-2015

677 views

Category:

Technology


1 download

TRANSCRIPT

Page 1: Theory of Computation (Fall 2013): Closure Properties of Regular Languages; DFA & NFA Implementation

Theory of Computation

Closure Properties of Regular

Languages, DFA and NFA

Implementation

Vladimir Kulyukin

http://www.vkedco.blogspot.com/

Page 2: Theory of Computation (Fall 2013): Closure Properties of Regular Languages; DFA & NFA Implementation

Outline

• Closure Properties of Regular Languages

• DFA Implementation

• NFA Implementation

Page 3: Theory of Computation (Fall 2013): Closure Properties of Regular Languages; DFA & NFA Implementation

Closure Properties of Regular

Languages

Page 4: Theory of Computation (Fall 2013): Closure Properties of Regular Languages; DFA & NFA Implementation

Definition: Language Complement

.|

is of complement the

,over language a is If

* LxxL

L

L

Page 5: Theory of Computation (Fall 2013): Closure Properties of Regular Languages; DFA & NFA Implementation

Example

q0

q1

q2

0

0,1

0,1 L is the language over {0, 1} that

starts with 0.

1

q0

q1

q2

0,1

0,1

1

0

The complement of L is the

language of strings over {0, 1} that

start with 1 and the empty string.

Page 6: Theory of Computation (Fall 2013): Closure Properties of Regular Languages; DFA & NFA Implementation

Closure under Complement

regular. also is then language,regular a is If :Theorem LL

. ifonly and if ,any for Then,

.,,,,

DFA new a definecan We

.such that ,,,,

DFA a is thereregular, a is If :Proof

*

0

0

MLx

MLxx

FQqQM

MLLFqQM

L

Page 7: Theory of Computation (Fall 2013): Closure Properties of Regular Languages; DFA & NFA Implementation

Definition: Language Intersection

. & |

then languages, are and If

21

*

21

21

LxLxxLL

LL

Page 8: Theory of Computation (Fall 2013): Closure Properties of Regular Languages; DFA & NFA Implementation

Closure under Intersection

regular. also is

then languages,regular are and If :Theorem 2121 LLLL

.,,

,,,

, and ,

,any For .,,

where,,,,,,

where,,,,,,

DFA new aconstruct We.

such that ,,,, and

such that ,,,, DFAs two

are thereregular, are and If :Proof

212100

*

3

210

*

20

*

1

20

*

210

*

1

21

*

213

210033

22

202211

1011

21

LLxFFxrq

FFxrxq

FxrFxq

LLxxaRrQq

araqarq

FFrqRQM

MLL

FrRMMLL

FqQM

LL

Page 9: Theory of Computation (Fall 2013): Closure Properties of Regular Languages; DFA & NFA Implementation

Definition: Language Union

.or |

then languages, are and If

21

*

21

21

LxLxxLL

LL

Page 10: Theory of Computation (Fall 2013): Closure Properties of Regular Languages; DFA & NFA Implementation

Closure under Union

regular. also is

then languages,regular are and If :Theorem 2121 LLLL

regular. is

on,intersecti and complementunder closed are

languagesregular .areSince

then regular, are and If :1 Proof

21

2121

21

LL

LLLL

LL

Page 11: Theory of Computation (Fall 2013): Closure Properties of Regular Languages; DFA & NFA Implementation

Closure under Union

regular. also is

then languages,regular are and If :Theorem 2121 LLLL

. are states final

theunion, for the DFA theof states final for the

except on,intersectiunder closure of proof in the

ason constructi same theusecan We:2 Proof

21 FQRF

Page 12: Theory of Computation (Fall 2013): Closure Properties of Regular Languages; DFA & NFA Implementation

DFA Implementation

Page 13: Theory of Computation (Fall 2013): Closure Properties of Regular Languages; DFA & NFA Implementation

Problem

Show that the language of binary

representations of numbers divisible by 3 is

regular.

Page 14: Theory of Computation (Fall 2013): Closure Properties of Regular Languages; DFA & NFA Implementation

DFA

q0 q1 q2

0

1

0

Q3 is the sink state where the automaton stays as soon as a symbol

different from 0 or 1 is seen.

1

0

1

q3 0, 1

not 0 or 1

not 0 or 1

not 0 or 1

Page 15: Theory of Computation (Fall 2013): Closure Properties of Regular Languages; DFA & NFA Implementation

DFA

q0 q1 q2

0

1

0

This DFA does the job but does not handle symbols outside of {0, 1}. We

need to add an error state where the DFA goes and stays as soon as it

sees a symbol other than 0 or 1.

1

0

1

Page 16: Theory of Computation (Fall 2013): Closure Properties of Regular Languages; DFA & NFA Implementation

Two Implementation Approaches

• There are two approaches used in implementing

both DFAs and NFAs: implement delta as a

function and implement delta as a table

• Implementing delta as a function is more

understandable and sometimes more efficient if

the FA is a weakly connected graph

• Implementing delta as a table is more common

because it tables can be constructed offline

read in at run time

Page 17: Theory of Computation (Fall 2013): Closure Properties of Regular Languages; DFA & NFA Implementation

DELTA AS FUNCTION

source code is here

Page 18: Theory of Computation (Fall 2013): Closure Properties of Regular Languages; DFA & NFA Implementation

Defining States

public class Mod3DFA {

static final int Q0 = 0;

static final int Q1 = 1;

static final int Q2 = 2;

static final int Q3 = 3;

private static int mCurrState;

}

Page 19: Theory of Computation (Fall 2013): Closure Properties of Regular Languages; DFA & NFA Implementation

Defining Delta private static int delta(int state, char c) {

switch ( state ) {

case Q0: switch ( c ) {

case '0': return Q0;

case '1': return Q1;

default: return Q3;

}

case Q1: switch ( c ) {

case '0': return Q2;

case '1': return Q0;

default: return Q3;

}

case Q2: switch ( c ) {

case '0': return Q1;

case '1': return Q2;

default: return Q3;

}

default: return Q3;

}

}

Page 20: Theory of Computation (Fall 2013): Closure Properties of Regular Languages; DFA & NFA Implementation

Resetting, Processing, Accepting

static void reset() {

mCurrState = Q0;

}

static void process(String input) {

for(int i = 0; i < input.length(); i++) {

mCurrState = delta(mCurrState, input.charAt(i));

}

}

static boolean isAccepted() {

return mCurrState == Q0;

}

Page 21: Theory of Computation (Fall 2013): Closure Properties of Regular Languages; DFA & NFA Implementation

Defining String Filter

static void mod3StringFilter() {

try {

BufferedReader input = new BufferedReader(new InputStreamReader(System.in));

String inputStr = input.readLine();

while ( inputStr != null ) {

Mod3DFA.reset();

Mod3DFA.process(inputStr);

if ( Mod3DFA.isAccepted() )

System.out.println("YES");

else

System.out.println("NO");

inputStr = input.readLine();

}

}

catch ( IOException ex ) {

System.err.println(ex.toString());

}

}

Page 22: Theory of Computation (Fall 2013): Closure Properties of Regular Languages; DFA & NFA Implementation

Using String Filter

public static void main(String[] argv) {

Mod3DFA.mod3StringFilter();

}

Page 23: Theory of Computation (Fall 2013): Closure Properties of Regular Languages; DFA & NFA Implementation

DELTA AS TABLE

source code is here

Page 24: Theory of Computation (Fall 2013): Closure Properties of Regular Languages; DFA & NFA Implementation

Defining States

public class Mod3DFA {

static final int Q0 = 0;

static final int Q1 = 1;

static final int Q2 = 2;

static final int Q3 = 3;

private static int mCurrState;

}

Page 25: Theory of Computation (Fall 2013): Closure Properties of Regular Languages; DFA & NFA Implementation

Defining Delta

static int[][] deltaTable =

{

{Q0, Q1}, // state Q0: delta('0', Q0) = Q0; delta('1', Q1)

{Q2, Q0}, // state Q1: delta('0', Q1) = Q2; detal('1', Q0)

{Q1, Q2}, // state Q2: delta('0', Q2) = Q1; delta('1', Q2)

{Q3, Q3} // state Q3: delta('0', Q3) = Q3; delta('1', Q3)

};

Page 26: Theory of Computation (Fall 2013): Closure Properties of Regular Languages; DFA & NFA Implementation

Resetting, Processing, Accepting

static void reset() { mCurrState = Q0; }

static void process(String input) {

for(int i = 0; i < input.length(); i++) {

char c = input.charAt(i);

try { // Table lookup is based on the following Java trick:

// System.out.println('1'-'0'); // this outputs integer 1

// System.out.println('1'-'1'); // this outputs integer 0

mCurrState = deltaTable[mCurrState][c-'0'];

}

catch ( ArrayIndexOutOfBoundsException ex ) {

mCurrState = Q3;

} } }

static boolean isAccepted() { return mCurrState == Q0; }

Page 27: Theory of Computation (Fall 2013): Closure Properties of Regular Languages; DFA & NFA Implementation

Defining String Filter

// same as in function implement

static void mod3StringFilter() {

try {

BufferedReader input = new BufferedReader(new InputStreamReader(System.in));

String inputStr = input.readLine();

while ( inputStr != null ) {

Mod3DFA.reset();

Mod3DFA.process(inputStr);

if ( Mod3DFA.isAccepted() )

System.out.println("YES");

else

System.out.println("NO");

inputStr = input.readLine();

}

}

catch ( IOException ex ) {

System.err.println(ex.toString());

}

}

Page 28: Theory of Computation (Fall 2013): Closure Properties of Regular Languages; DFA & NFA Implementation

Using String Filter

// same as in function implementation

public static void main(String[] argv) {

Mod3DFA.mod3StringFilter();

}

Page 29: Theory of Computation (Fall 2013): Closure Properties of Regular Languages; DFA & NFA Implementation

Sketchy NFA Implementation

Page 30: Theory of Computation (Fall 2013): Closure Properties of Regular Languages; DFA & NFA Implementation

Defining Symbols

public class Symbol {

public String name; // C-style structure

public Symbol() { name = ""; }

public Symbol(String n) { name = n; }

@Override

public String toString() {

return name;

}

}

Page 31: Theory of Computation (Fall 2013): Closure Properties of Regular Languages; DFA & NFA Implementation

Defining Symbols

public class EpsilonSymbol {

public static final Symbol epsilon = new Symbol("epsilon");

public EpsilonSymbol() {}

}

Page 32: Theory of Computation (Fall 2013): Closure Properties of Regular Languages; DFA & NFA Implementation

Defining States

public class FSAState {

public Integer id;

public FSAState(int id) {

this.id = new Integer(id);

}

public FSAState(Integer id) {

this.id = new Integer(id.intValue());

}

@Override

public String toString() {

return new String("q" + id.toString());

}

}

Page 33: Theory of Computation (Fall 2013): Closure Properties of Regular Languages; DFA & NFA Implementation

Defining Alphabet

• Sigma is a map that maps Symbol.name

(String) to Symbol objects

import java.util.TreeMap;

TreeMap<String, Symbol> sigma;

Page 34: Theory of Computation (Fall 2013): Closure Properties of Regular Languages; DFA & NFA Implementation

Defining State Sets

import java.util.ArrayList;

private ArrayList<FSAState> states;

private ArrayList<FSAState> finalStates;

private FSAState startState;

Page 35: Theory of Computation (Fall 2013): Closure Properties of Regular Languages; DFA & NFA Implementation

Defining State Sets

• Suppose an NFA in state 1 transitions to

states {2, 3} on a and to states {4, 5} on b

• Here is a way to implement this:

TreeMap<Integer, TreeMap<String, ArrayList<FSAState>> deltaTable;

Page 36: Theory of Computation (Fall 2013): Closure Properties of Regular Languages; DFA & NFA Implementation

References

• A. Brooks Weber. Formal Language: A

Practical Introduction. Franklin, Beedle &

Associates.

• M. Davis, R. Sigal, E. Weyuker.

Computability, Complexity, & Languages:

Fundamentals of Theorectical Computer

Science. Academic Press.

http://www.vkedco.blogspot.com/