java ii finite automata i - uni-saarland.de ii finite automata i bernd kiefer [email protected]...

29
Java II Finite Automata I Bernd Kiefer [email protected] Deutsches Forschungszentrum f¨ ur k ¨ unstliche Intelligenz Finite Automata I – p.1/13

Upload: doanliem

Post on 26-May-2018

214 views

Category:

Documents


0 download

TRANSCRIPT

Java IIFinite Automata I

Bernd Kiefer

[email protected]

Deutsches Forschungszentrum fur kunstliche Intelligenz

Finite Automata I – p.1/13

Processing Regular Expressions

We already learned about Java’s regular expressionfunctionality

Now we get to know the machinery behindPattern andMatcher classes

Compiling a regular expression into a Pattern objectproduces a Finite Automaton

This automaton is then used to perform the matching tasks

We will see how to construct a finite automaton thatrecognizes an input string, i.e., tries to find a full match

Finite Automata I – p.2/13

Definition: Finite Automaton

A finite automaton (FA) is a tuple A =< Q,Σ, δ, q0, F >

Q a finite non-empty set of statesΣ a finite alphabet of input lettersδ a (total) transition function Q × Σ −→ Q

q0 ∈ Q the initial stateF ⊆ Q the set of final (accepting) states

Transition graphs (diagrams):

q0 q1 q2 q3

d o g

transitionstatesfinal state

initial state

Finite Automata I – p.3/13

Finite Automata: Matching

A finite automaton accepts a given input string s if there isa sequence of states p1, p2, . . . , p|s| ∈ Q such that

1. p1 = q0, the start state2. δ(pi, si) = pi+1, where si is the i-th character in s

3. p|s| ∈ F , i.e., a final state

A string is successfully matched if we have found theappropriate sequence of states

Imagine the string on an input tape with a pointer that isadvanced when using a δ transition

The set of strings accepted by an automaton is theaccepted language, analogous to regular expressions

Finite Automata I – p.4/13

(Non)deterministic Automata

in the definition of automata, δ was a total function ⇒

given an input string, the path through the automaton isuniquely determined

those automata are therefore called deterministic

for nondeterministic FA, δ is a transition relation

δ : Q × Σ ∪ {ǫ} −→ P(Q), where P(Q) is the powerset of Q

allows transitions from one state into several states withthe same input symbol

need not be total

can have transitions labeled ǫ (not in Σ), which representsthe empty string

Finite Automata I – p.5/13

RegExps −→ Automata

Construct nondeterminstic automata from regular expressions

(αβ) q0α . . .qfα

q0β. . . qfβ

(α | β) q0α . . . qfα q0β . . . qfβq0 qf

ǫ

ǫ

ǫ

ǫ

(α)∗ q0α . . . qfαq0 qf

ǫǫ

ǫ

ǫ

Finite Automata I – p.6/13

NFA vs. DFA

Traversing a DFA is easy given the input string: the path isuniquely determined

In contrast, traversing an NFA requires keeping track of aset of (current) states, starting with the set {qo}

Processing the next input symbol means taking allpossible outgoing transitions from this set and collectingthe new set

From every NFA, an equivalent DFA (one which doesaccept the same language), can be computed

Basic Idea: track the subsets that can be reached for everypossible input

Finite Automata I – p.7/13

Traversing an NFA

0 1

2 3

4 5

6 7 8 9ǫ

ǫ

ǫ

a

b

ǫ

ǫ

ǫ a b

ǫ

ǫ

abab

Finite Automata I – p.8/13

Traversing an NFA

0 1

2 3

4 5

6 7 8 9ǫ

ǫ

ǫ

a

b

ǫ

ǫ

ǫ a b

ǫ

ǫ

0 1

2

4

7

abab

Finite Automata I – p.8/13

Traversing an NFA

0 1

2 3

4 5

6 7 8 9ǫ

ǫ

ǫ

a

b

ǫ

ǫ

ǫ a b

ǫ

ǫ

1

2 3

4

6 7 8

abab

Finite Automata I – p.8/13

Traversing an NFA

0 1

2 3

4 5

6 7 8 9ǫ

ǫ

ǫ

a

b

ǫ

ǫ

ǫ a b

ǫ

ǫ

1

2

4 5

6 7 9

abab

Finite Automata I – p.8/13

Traversing an NFA

0 1

2 3

4 5

6 7 8 9ǫ

ǫ

ǫ

a

b

ǫ

ǫ

ǫ a b

ǫ

ǫ

1

2 3

4

6 7 8

abab

Finite Automata I – p.8/13

Traversing an NFA

0 1

2 3

4 5

6 7 8 9ǫ

ǫ

ǫ

a

b

ǫ

ǫ

ǫ a b

ǫ

ǫ

1

2

4 5

6 7 9

abab

Finite Automata I – p.8/13

NFA −→ DFA: Subset Construction

Simulate “in parallel” all possible moves the automatoncan make

The states of the resulting DFA will represent sets of statesof the NFA, i.e., elements of P(Q)

We use two operations on states/state-sets of the NFA

ǫ-closure(T )Set of states reachable from any state s in T onon ǫ-transitions

move(T, a)Set of states to which there is a transition fromone state in T on input symbol a

The final states of the DFA are those where thecorresponding NFA subset contains a final state

Finite Automata I – p.9/13

Algorithm: Subset Construction

proc SubsetConstruction(s0) ≡DFAStates = ǫ-closure({s0})while there is an unmarked state T in DFAStates do

mark T

for each input symbol a doU := ǫ-closure(move(T, a))DFADelta[T, a] := U

if U 6∈ DFAStates then add U as unmarked state to DFAStates

proc ǫ-closure(T ) ≡ǫ-closure := T ; to check := T

while to check not empty doget some state t from to checkfor each state u with edge labeled ǫ from t to u

if u 6∈ ǫ-closure then add u to ǫ-closure and to check

Finite Automata I – p.10/13

Example: Subset Construction

0 1

2 3

4 5

6 7 8 9ǫ

ǫ

ǫ

a

b

ǫ

ǫ

ǫ a b

ǫ

ǫ

Finite Automata I – p.11/13

Example: Subset Construction

0 1

2 3

4 5

6 7 8 9ǫ

ǫ

ǫ

a

b

ǫ

ǫ

ǫ a b

ǫ

ǫ

0,1,2,4,7

0 1

2

4

7

0,1,2,4,7

Finite Automata I – p.11/13

Example: Subset Construction

0 1

2 3

4 5

6 7 8 9ǫ

ǫ

ǫ

a

b

ǫ

ǫ

ǫ a b

ǫ

ǫ

0,1,2,4,7

1,2,34,6,7,8a

1

2 3

4

6 7 8

1,2,34,6,7,8

Finite Automata I – p.11/13

Example: Subset Construction

0 1

2 3

4 5

6 7 8 9ǫ

ǫ

ǫ

a

b

ǫ

ǫ

ǫ a b

ǫ

ǫ

0,1,2,4,7

1,2,34,6,7,8a

1,2,45,6,7

b

1

2

4 5

6 7

1,2,45,6,7

Finite Automata I – p.11/13

Example: Subset Construction

0 1

2 3

4 5

6 7 8 9ǫ

ǫ

ǫ

a

b

ǫ

ǫ

ǫ a b

ǫ

ǫ

0,1,2,4,7

1,2,34,6,7,8a

1,2,45,6,7

ba

1

2 3

4

6 7 8

1,2,34,6,7,8

Finite Automata I – p.11/13

Example: Subset Construction

0 1

2 3

4 5

6 7 8 9ǫ

ǫ

ǫ

a

b

ǫ

ǫ

ǫ a b

ǫ

ǫ

0,1,2,4,7

1,2,34,6,7,8a

1,2,45,6,7

ba

b

1

2

4 5

6 7

1,2,45,6,7

Finite Automata I – p.11/13

Example: Subset Construction

0 1

2 3

4 5

6 7 8 9ǫ

ǫ

ǫ

a

b

ǫ

ǫ

ǫ a b

ǫ

ǫ

0,1,2,4,7

1,2,34,6,7,8a

1,2,45,6,7

ba

b

a

1

2 3

4

6 7 8

1,2,34,6,7,8

Finite Automata I – p.11/13

Example: Subset Construction

0 1

2 3

4 5

6 7 8 9ǫ

ǫ

ǫ

a

b

ǫ

ǫ

ǫ a b

ǫ

ǫ

0,1,2,4,7

1,2,34,6,7,8a

1,2,45,6,7

ba

b

a1,2,45,6,7,91,2,45,6,7,9

b

1

2 3

4

6 7 8 9

Finite Automata I – p.11/13

Example: Subset Construction

0 1

2 3

4 5

6 7 8 9ǫ

ǫ

ǫ

a

b

ǫ

ǫ

ǫ a b

ǫ

ǫ

0,1,2,4,7

1,2,34,6,7,8a

1,2,45,6,7

ba

b

a1,2,45,6,7,91,2,45,6,7,9

b

1

2 3

4

6 7 8 9

a

b

Finite Automata I – p.11/13

Time/Space Considerations

DFA traversal is linear to the length of input string x

NFA needs O(n) space (states+transitions), where n is thelength of the regular expression

NFA traversal may need time n × |x|, so why use NFAs?

Finite Automata I – p.12/13

Time/Space Considerations

DFA traversal is linear to the length of input string x

NFA needs O(n) space (states+transitions), where n is thelength of the regular expression

NFA traversal may need time n × |x|, so why use NFAs?

There are DFA that have at least 2n states!

Finite Automata I – p.12/13

Time/Space Considerations

DFA traversal is linear to the length of input string x

NFA needs O(n) space (states+transitions), where n is thelength of the regular expression

NFA traversal may need time n × |x|, so why use NFAs?

There are DFA that have at least 2n states!

Solution 1: “Lazy” construction of the DFA: construct DFAstates on the fly up to a certain amount and cache them

Finite Automata I – p.12/13

Time/Space Considerations

DFA traversal is linear to the length of input string x

NFA needs O(n) space (states+transitions), where n is thelength of the regular expression

NFA traversal may need time n × |x|, so why use NFAs?

There are DFA that have at least 2n states!

Solution 1: “Lazy” construction of the DFA: construct DFAstates on the fly up to a certain amount and cache them

Solution 2: Try to minimize the DFA:There is a unique (modulo state names) minimalautomaton for a regular language!

Finite Automata I – p.12/13

Minimization Algorithm by Hopcroft

proc Minimize() ≡B1 = F ; B2 = Q F

E = {B1, B2}k = 3for a ∈ Σ do

a(i) = {s ∈ Q|s ∈ Bi ∧ ∃t : δ(t, a) = s}L = the smaller of the a(i)while L 6= ∅ do

take some i ∈ L and delete itfor j < k s.th. ∃t ∈ Bj

Finite Automata I – p.13/13