Finite State Automata (Endliche Automaten) Laura Kallmeyer€¦ · Adeterministic •nite state...

71
Einf¨ uhrung in die Computerlinguistik Finite State Automata (Endliche Automaten) Laura Kallmeyer Heinrich-Heine-Universit¨ at D¨ usseldorf Summer 2020 1 / 26

Transcript of Finite State Automata (Endliche Automaten) Laura Kallmeyer€¦ · Adeterministic •nite state...

Page 1: Finite State Automata (Endliche Automaten) Laura Kallmeyer€¦ · Adeterministic •nite state automaton(DFA) M is a quintuple hQ; ; ;q 0;Fisuch that Q is a •nite set of states.

Einfuhrung in die ComputerlinguistikFinite State Automata (Endliche Automaten)

Laura Kallmeyer

Heinrich-Heine-Universitat Dusseldorf

Summer 2020

1 / 26

Page 2: Finite State Automata (Endliche Automaten) Laura Kallmeyer€¦ · Adeterministic •nite state automaton(DFA) M is a quintuple hQ; ; ;q 0;Fisuch that Q is a •nite set of states.

Automaten (1)

Automaten

haben in der Regel Zustande, darunter einen Startzustand undEndzustande;verarbeiten eine Eingabe;durchlaufen bei Verarbeitung der Eingabe verschiedene Zustande;haben eventuell eine Ausgabe;akzeptieren Eingaben, die zu einem Endzustand fuhren.

2

Page 3: Finite State Automata (Endliche Automaten) Laura Kallmeyer€¦ · Adeterministic •nite state automaton(DFA) M is a quintuple hQ; ; ;q 0;Fisuch that Q is a •nite set of states.

Automaten (2)

Beispiel FSAAutomat, der Worter uber {a, b} akzeptiert, bei denen jedes bnotwendig von einem a gefolgt ist.

q0 q1

ab

a

Zustande q0, q1. q0 ist Start- und Endzustand.Ubergange in q0: falls ein a gelesen wird, bleibt man in q0; fallsein b gelesen wird, wechselt man in q1;Ubergange in q1: man kann als nachstes nur ein a verarbeitenund geht dabei wieder in q0.

3

Page 4: Finite State Automata (Endliche Automaten) Laura Kallmeyer€¦ · Adeterministic •nite state automaton(DFA) M is a quintuple hQ; ; ;q 0;Fisuch that Q is a •nite set of states.

DFA (1)

A deterministic �nite state automaton (DFA) M is a quintuple〈Q,Σ, δ, q0, F〉 such that

Q is a �nite set of states.Σ is a �nite set, the input alphabet.δ : Q × Σ→ Q is a partial function, the transition function.q0 ∈ Q is the initial state.F ⊆ Q is the set of �nal states.

4

Page 5: Finite State Automata (Endliche Automaten) Laura Kallmeyer€¦ · Adeterministic •nite state automaton(DFA) M is a quintuple hQ; ; ;q 0;Fisuch that Q is a •nite set of states.

DFA (1)

A deterministic �nite state automaton (DFA) M is a quintuple〈Q,Σ, δ, q0, F〉 such that

Q is a �nite set of states.

Σ is a �nite set, the input alphabet.δ : Q × Σ→ Q is a partial function, the transition function.q0 ∈ Q is the initial state.F ⊆ Q is the set of �nal states.

4

Page 6: Finite State Automata (Endliche Automaten) Laura Kallmeyer€¦ · Adeterministic •nite state automaton(DFA) M is a quintuple hQ; ; ;q 0;Fisuch that Q is a •nite set of states.

DFA (1)

A deterministic �nite state automaton (DFA) M is a quintuple〈Q,Σ, δ, q0, F〉 such that

Q is a �nite set of states.Σ is a �nite set, the input alphabet.

δ : Q × Σ→ Q is a partial function, the transition function.q0 ∈ Q is the initial state.F ⊆ Q is the set of �nal states.

4

Page 7: Finite State Automata (Endliche Automaten) Laura Kallmeyer€¦ · Adeterministic •nite state automaton(DFA) M is a quintuple hQ; ; ;q 0;Fisuch that Q is a •nite set of states.

DFA (1)

A deterministic �nite state automaton (DFA) M is a quintuple〈Q,Σ, δ, q0, F〉 such that

Q is a �nite set of states.Σ is a �nite set, the input alphabet.δ : Q × Σ→ Q is a partial function, the transition function.

q0 ∈ Q is the initial state.F ⊆ Q is the set of �nal states.

4

Page 8: Finite State Automata (Endliche Automaten) Laura Kallmeyer€¦ · Adeterministic •nite state automaton(DFA) M is a quintuple hQ; ; ;q 0;Fisuch that Q is a •nite set of states.

DFA (1)

A deterministic �nite state automaton (DFA) M is a quintuple〈Q,Σ, δ, q0, F〉 such that

Q is a �nite set of states.Σ is a �nite set, the input alphabet.δ : Q × Σ→ Q is a partial function, the transition function.q0 ∈ Q is the initial state.

F ⊆ Q is the set of �nal states.

4

Page 9: Finite State Automata (Endliche Automaten) Laura Kallmeyer€¦ · Adeterministic •nite state automaton(DFA) M is a quintuple hQ; ; ;q 0;Fisuch that Q is a •nite set of states.

DFA (1)

A deterministic �nite state automaton (DFA) M is a quintuple〈Q,Σ, δ, q0, F〉 such that

Q is a �nite set of states.Σ is a �nite set, the input alphabet.δ : Q × Σ→ Q is a partial function, the transition function.q0 ∈ Q is the initial state.F ⊆ Q is the set of �nal states.

4

Page 10: Finite State Automata (Endliche Automaten) Laura Kallmeyer€¦ · Adeterministic •nite state automaton(DFA) M is a quintuple hQ; ; ;q 0;Fisuch that Q is a •nite set of states.

DFA (2)

Beispiel: DFA als Graph und als Tupel

q0 q1

ab

aDFA 〈Q,Σ, δ, q0, F〉 mit

Q = {q0, q1} (q0 Startzustand),F = {q0},Σ = {a, b} undδ(q0, a) = q0, δ(q0, b) = q1,δ(q1, a) = q0, δ(q1, b) nicht de�niert.

5

Page 11: Finite State Automata (Endliche Automaten) Laura Kallmeyer€¦ · Adeterministic •nite state automaton(DFA) M is a quintuple hQ; ; ;q 0;Fisuch that Q is a •nite set of states.

DFA (3)

Die Hulle der Ubergangsfunktion, δ, beschreibt fur einen Zustand qund eine Eingabe w ∈ Σ∗, in welchen Zustand man, ausgehend von q,nach Verarbeiten von w gelangt.

�e transition closure of δ is its re�exive transitive closure:Let M = 〈Q,Σ, δ, q0, F〉 be a DFA. We de�ne its transition closureδ : Q × Σ∗ → Q as follows:

δ(q, ε) := q for all q ∈ Q.δ(q,wa) := δ(δ(q,w), a) for all a ∈ Σ,w ∈ Σ∗.

6

Page 12: Finite State Automata (Endliche Automaten) Laura Kallmeyer€¦ · Adeterministic •nite state automaton(DFA) M is a quintuple hQ; ; ;q 0;Fisuch that Q is a •nite set of states.

DFA (3)

Die Hulle der Ubergangsfunktion, δ, beschreibt fur einen Zustand qund eine Eingabe w ∈ Σ∗, in welchen Zustand man, ausgehend von q,nach Verarbeiten von w gelangt.

�e transition closure of δ is its re�exive transitive closure:Let M = 〈Q,Σ, δ, q0, F〉 be a DFA. We de�ne its transition closureδ : Q × Σ∗ → Q as follows:

δ(q, ε) := q for all q ∈ Q.δ(q,wa) := δ(δ(q,w), a) for all a ∈ Σ,w ∈ Σ∗.

6

Page 13: Finite State Automata (Endliche Automaten) Laura Kallmeyer€¦ · Adeterministic •nite state automaton(DFA) M is a quintuple hQ; ; ;q 0;Fisuch that Q is a •nite set of states.

DFA (4)

Beispiel: δ

q0 q1

ab

a

δ(q0, aaba) = q0

δ(q0, ε) = q0

δ(q1, aab) = q1

δ(q1, b) ist nicht de�niert.

7

Page 14: Finite State Automata (Endliche Automaten) Laura Kallmeyer€¦ · Adeterministic •nite state automaton(DFA) M is a quintuple hQ; ; ;q 0;Fisuch that Q is a •nite set of states.

DFA (5)

Die akzeptierte Sprache sind alle Worter, die erlauben, von q0 in einenEndzustand zu gelangen.

�e language accepted by a DFA M = 〈Q,Σ, δ, q0, F〉 is

L(M) := {w | δ(q0,w) ∈ F}

8

Page 15: Finite State Automata (Endliche Automaten) Laura Kallmeyer€¦ · Adeterministic •nite state automaton(DFA) M is a quintuple hQ; ; ;q 0;Fisuch that Q is a •nite set of states.

DFA (5)

Die akzeptierte Sprache sind alle Worter, die erlauben, von q0 in einenEndzustand zu gelangen.

�e language accepted by a DFA M = 〈Q,Σ, δ, q0, F〉 is

L(M) := {w | δ(q0,w) ∈ F}

8

Page 16: Finite State Automata (Endliche Automaten) Laura Kallmeyer€¦ · Adeterministic •nite state automaton(DFA) M is a quintuple hQ; ; ;q 0;Fisuch that Q is a •nite set of states.

NFA (1)

Ein endlicher Automat ist nicht-deterministisch, wenn es dieUbergangsfunktion nicht eindeutig ist. D.h., es gibt Zustande, vondenen aus bei Verarbeitung eines Eingabesymbols mehrere Zustandeerreicht werden konnten.

Nicht-deterministischer Automat

q0

q1

q2b

ba c

b

Sprache: a(ba|bc∗ba)∗

9

Page 17: Finite State Automata (Endliche Automaten) Laura Kallmeyer€¦ · Adeterministic •nite state automaton(DFA) M is a quintuple hQ; ; ;q 0;Fisuch that Q is a •nite set of states.

NFA (1)

Ein endlicher Automat ist nicht-deterministisch, wenn es dieUbergangsfunktion nicht eindeutig ist. D.h., es gibt Zustande, vondenen aus bei Verarbeitung eines Eingabesymbols mehrere Zustandeerreicht werden konnten.

Nicht-deterministischer Automat

q0

q1

q2b

ba c

b

Sprache: a(ba|bc∗ba)∗

9

Page 18: Finite State Automata (Endliche Automaten) Laura Kallmeyer€¦ · Adeterministic •nite state automaton(DFA) M is a quintuple hQ; ; ;q 0;Fisuch that Q is a •nite set of states.

NFA (2)

A �nite state automaton is non-deterministic if the transition functionis not deterministic:A non-deterministic �nite state automaton (NFA) M is a quintuple〈Q,Σ, δ, q0, F〉 such that

Q, Σ, q0 and F are like in a DFA.δ : Q × Σ→ P(Q) is the transition function (where P(Q) is thepowerset of Q).

10

Page 19: Finite State Automata (Endliche Automaten) Laura Kallmeyer€¦ · Adeterministic •nite state automaton(DFA) M is a quintuple hQ; ; ;q 0;Fisuch that Q is a •nite set of states.

NFA (2)

A �nite state automaton is non-deterministic if the transition functionis not deterministic:A non-deterministic �nite state automaton (NFA) M is a quintuple〈Q,Σ, δ, q0, F〉 such that

Q, Σ, q0 and F are like in a DFA.

δ : Q × Σ→ P(Q) is the transition function (where P(Q) is thepowerset of Q).

10

Page 20: Finite State Automata (Endliche Automaten) Laura Kallmeyer€¦ · Adeterministic •nite state automaton(DFA) M is a quintuple hQ; ; ;q 0;Fisuch that Q is a •nite set of states.

NFA (2)

A �nite state automaton is non-deterministic if the transition functionis not deterministic:A non-deterministic �nite state automaton (NFA) M is a quintuple〈Q,Σ, δ, q0, F〉 such that

Q, Σ, q0 and F are like in a DFA.δ : Q × Σ→ P(Q) is the transition function (where P(Q) is thepowerset of Q).

10

Page 21: Finite State Automata (Endliche Automaten) Laura Kallmeyer€¦ · Adeterministic •nite state automaton(DFA) M is a quintuple hQ; ; ;q 0;Fisuch that Q is a •nite set of states.

NFA (3)

Nicht-deterministischer Automat

q0

q1

q2b

ba c

b

Ubergangsfunktion:δ(q0, a) = {q1} δ(q2, c) = {q2}δ(q1, b) = {q0, q2} δ(q2, b) = {q0}

In allen anderen Fallen ist der Wert ∅.

11

Page 22: Finite State Automata (Endliche Automaten) Laura Kallmeyer€¦ · Adeterministic •nite state automaton(DFA) M is a quintuple hQ; ; ;q 0;Fisuch that Q is a •nite set of states.

NFA (3)

δ, beschreibt fur einen Zustand q und eine Eingabe w ∈ Σ∗, inwelchen Zustande man, ausgehend von q, nach Verarbeiten von wgelangen kann.

Let M = 〈Q,Σ, δ, q0, F〉 be a NFA. We de�ne its transition closureδ : Q × Σ∗ → P(Q) as follows:

δ(q, ε) := {q} for all q ∈ Q.For all a ∈ Σ,w ∈ Σ∗:δ(q,wa) := {p | there is a r ∈ δ(q,w) such that p ∈ δ(r, a)}.

Beispiel δ bei NFA(Automat s. vorherige Folie):

δ(q0, ab) = {q0, q2},δ(q0, abca) = ∅

12

Page 23: Finite State Automata (Endliche Automaten) Laura Kallmeyer€¦ · Adeterministic •nite state automaton(DFA) M is a quintuple hQ; ; ;q 0;Fisuch that Q is a •nite set of states.

NFA (3)

δ, beschreibt fur einen Zustand q und eine Eingabe w ∈ Σ∗, inwelchen Zustande man, ausgehend von q, nach Verarbeiten von wgelangen kann.

Let M = 〈Q,Σ, δ, q0, F〉 be a NFA. We de�ne its transition closureδ : Q × Σ∗ → P(Q) as follows:

δ(q, ε) := {q} for all q ∈ Q.For all a ∈ Σ,w ∈ Σ∗:δ(q,wa) := {p | there is a r ∈ δ(q,w) such that p ∈ δ(r, a)}.

Beispiel δ bei NFA(Automat s. vorherige Folie):

δ(q0, ab) = {q0, q2},δ(q0, abca) = ∅

12

Page 24: Finite State Automata (Endliche Automaten) Laura Kallmeyer€¦ · Adeterministic •nite state automaton(DFA) M is a quintuple hQ; ; ;q 0;Fisuch that Q is a •nite set of states.

NFA (3)

δ, beschreibt fur einen Zustand q und eine Eingabe w ∈ Σ∗, inwelchen Zustande man, ausgehend von q, nach Verarbeiten von wgelangen kann.

Let M = 〈Q,Σ, δ, q0, F〉 be a NFA. We de�ne its transition closureδ : Q × Σ∗ → P(Q) as follows:

δ(q, ε) := {q} for all q ∈ Q.For all a ∈ Σ,w ∈ Σ∗:δ(q,wa) := {p | there is a r ∈ δ(q,w) such that p ∈ δ(r, a)}.

Beispiel δ bei NFA(Automat s. vorherige Folie):

δ(q0, ab) = {q0, q2},δ(q0, abca) = ∅

12

Page 25: Finite State Automata (Endliche Automaten) Laura Kallmeyer€¦ · Adeterministic •nite state automaton(DFA) M is a quintuple hQ; ; ;q 0;Fisuch that Q is a •nite set of states.

NFA (3)

δ(q, ε) := {q} for all q ∈ Q.

For all a ∈ Σ,w ∈ Σ∗:δ(q,wa) := {p | there is a r ∈ δ(q,w) such that p ∈ δ(r, a)}.

Weiteres Beispiel: δ bei NFA

q0

q1

q2

b

ba

a

δ(q0, ab) =

{p | there is a r ∈ δ(q0, a) such that p ∈ δ(r, b)}δ(q0, a) = {q0, q2}δ(q0, ab) = δ(q0, b) ∪ δ(q2, b) = {q1} ∪ {q2} = {q1, q2}.

13

Page 26: Finite State Automata (Endliche Automaten) Laura Kallmeyer€¦ · Adeterministic •nite state automaton(DFA) M is a quintuple hQ; ; ;q 0;Fisuch that Q is a •nite set of states.

NFA (3)

δ(q, ε) := {q} for all q ∈ Q.

For all a ∈ Σ,w ∈ Σ∗:δ(q,wa) := {p | there is a r ∈ δ(q,w) such that p ∈ δ(r, a)}.

Weiteres Beispiel: δ bei NFA

q0

q1

q2

b

ba

a

δ(q0, ab) = {p | there is a r ∈ δ(q0, a) such that p ∈ δ(r, b)}

δ(q0, a) = {q0, q2}δ(q0, ab) = δ(q0, b) ∪ δ(q2, b) = {q1} ∪ {q2} = {q1, q2}.

13

Page 27: Finite State Automata (Endliche Automaten) Laura Kallmeyer€¦ · Adeterministic •nite state automaton(DFA) M is a quintuple hQ; ; ;q 0;Fisuch that Q is a •nite set of states.

NFA (3)

δ(q, ε) := {q} for all q ∈ Q.

For all a ∈ Σ,w ∈ Σ∗:δ(q,wa) := {p | there is a r ∈ δ(q,w) such that p ∈ δ(r, a)}.

Weiteres Beispiel: δ bei NFA

q0

q1

q2

b

ba

a

δ(q0, ab) = {p | there is a r ∈ δ(q0, a) such that p ∈ δ(r, b)}δ(q0, a) = {q0, q2}

δ(q0, ab) = δ(q0, b) ∪ δ(q2, b) = {q1} ∪ {q2} = {q1, q2}.

13

Page 28: Finite State Automata (Endliche Automaten) Laura Kallmeyer€¦ · Adeterministic •nite state automaton(DFA) M is a quintuple hQ; ; ;q 0;Fisuch that Q is a •nite set of states.

NFA (3)

δ(q, ε) := {q} for all q ∈ Q.

For all a ∈ Σ,w ∈ Σ∗:δ(q,wa) := {p | there is a r ∈ δ(q,w) such that p ∈ δ(r, a)}.

Weiteres Beispiel: δ bei NFA

q0

q1

q2

b

ba

a

δ(q0, ab) = {p | there is a r ∈ δ(q0, a) such that p ∈ δ(r, b)}δ(q0, a) = {q0, q2}δ(q0, ab) = δ(q0, b) ∪ δ(q2, b) = {q1} ∪ {q2} = {q1, q2}.

13

Page 29: Finite State Automata (Endliche Automaten) Laura Kallmeyer€¦ · Adeterministic •nite state automaton(DFA) M is a quintuple hQ; ; ;q 0;Fisuch that Q is a •nite set of states.

NFA (4)

Die Sprache des NFA ist die Menge aller Worter, die erlauben,ausgehend von q0 einen Endzustand zu erreichen.

�e language accepted by a NFA M = 〈Q,Σ, δ, q0, F〉 is

L(M) := {w | δ(q0,w) ∩ F 6= ∅}

14

Page 30: Finite State Automata (Endliche Automaten) Laura Kallmeyer€¦ · Adeterministic •nite state automaton(DFA) M is a quintuple hQ; ; ;q 0;Fisuch that Q is a •nite set of states.

NFA (4)

Die Sprache des NFA ist die Menge aller Worter, die erlauben,ausgehend von q0 einen Endzustand zu erreichen.

�e language accepted by a NFA M = 〈Q,Σ, δ, q0, F〉 is

L(M) := {w | δ(q0,w) ∩ F 6= ∅}

14

Page 31: Finite State Automata (Endliche Automaten) Laura Kallmeyer€¦ · Adeterministic •nite state automaton(DFA) M is a quintuple hQ; ; ;q 0;Fisuch that Q is a •nite set of states.

Equivalence of DFA and NFA (1)

�e following holds:Let L be a language. �ere is a DFA M such that L = L(M) i� there isa NFA M ′ such that L = L(M ′).

�e “⇒” direction of the i� is obvious: Each DFA can be wri�en as aNFA by

(i) replacing δ(q1, a) = q2 with δ(q1, a) = {q2}, and(ii) adding δ(q1, a) = ∅ for all cases where δ(q1, a) is unde�ned in

the original automaton.

15

Page 32: Finite State Automata (Endliche Automaten) Laura Kallmeyer€¦ · Adeterministic •nite state automaton(DFA) M is a quintuple hQ; ; ;q 0;Fisuch that Q is a •nite set of states.

Equivalence of DFA and NFA (1)

�e following holds:Let L be a language. �ere is a DFA M such that L = L(M) i� there isa NFA M ′ such that L = L(M ′).

�e “⇒” direction of the i� is obvious: Each DFA can be wri�en as aNFA by

(i) replacing δ(q1, a) = q2 with δ(q1, a) = {q2}, and(ii) adding δ(q1, a) = ∅ for all cases where δ(q1, a) is unde�ned in

the original automaton.

15

Page 33: Finite State Automata (Endliche Automaten) Laura Kallmeyer€¦ · Adeterministic •nite state automaton(DFA) M is a quintuple hQ; ; ;q 0;Fisuch that Q is a •nite set of states.

Equivalence of DFA and NFA (2)

For the “⇐” direction, we need to show how to construct anequivalent DFA for a given NFA.

For this, we need to do two things:

1 Give the construction algorithm.2 Prove that the resulting DFA accepts the string language of the

original NFA.

Proofs of this type are called construction proofs.

16

Page 34: Finite State Automata (Endliche Automaten) Laura Kallmeyer€¦ · Adeterministic •nite state automaton(DFA) M is a quintuple hQ; ; ;q 0;Fisuch that Q is a •nite set of states.

Equivalence of DFA and NFA (2)

For the “⇐” direction, we need to show how to construct anequivalent DFA for a given NFA.

For this, we need to do two things:

1 Give the construction algorithm.2 Prove that the resulting DFA accepts the string language of the

original NFA.

Proofs of this type are called construction proofs.

16

Page 35: Finite State Automata (Endliche Automaten) Laura Kallmeyer€¦ · Adeterministic •nite state automaton(DFA) M is a quintuple hQ; ; ;q 0;Fisuch that Q is a •nite set of states.

Equivalence of DFA and NFA (2)

For the “⇐” direction, we need to show how to construct anequivalent DFA for a given NFA.

For this, we need to do two things:

1 Give the construction algorithm.2 Prove that the resulting DFA accepts the string language of the

original NFA.

Proofs of this type are called construction proofs.

16

Page 36: Finite State Automata (Endliche Automaten) Laura Kallmeyer€¦ · Adeterministic •nite state automaton(DFA) M is a quintuple hQ; ; ;q 0;Fisuch that Q is a •nite set of states.

Equivalence of DFA and NFA (3)

Idea of the construction: In the new DFA, the states are subsets of thestate set Q of the NFA. A state contains all q ∈ Q that are reachablewith the same input word.

q0

q1

q2

a c

b

b

b

neue Zustande:q0 = {q0}, q1 = {q1},q2 = {q2}, q3 = {q0, q2}

q0 q1

q3q2

a

b

c

ba

c

b

17

Page 37: Finite State Automata (Endliche Automaten) Laura Kallmeyer€¦ · Adeterministic •nite state automaton(DFA) M is a quintuple hQ; ; ;q 0;Fisuch that Q is a •nite set of states.

Equivalence of DFA and NFA (3)

Idea of the construction: In the new DFA, the states are subsets of thestate set Q of the NFA. A state contains all q ∈ Q that are reachablewith the same input word.

q0

q1

q2

a c

b

b

b

neue Zustande:q0 = {q0}, q1 = {q1},q2 = {q2}, q3 = {q0, q2}

q0 q1

q3q2

a

b

c

ba

c

b

17

Page 38: Finite State Automata (Endliche Automaten) Laura Kallmeyer€¦ · Adeterministic •nite state automaton(DFA) M is a quintuple hQ; ; ;q 0;Fisuch that Q is a •nite set of states.

Equivalence of DFA and NFA (4)

Let M = 〈Q,Σ, δ, q0, F〉 be an NFA. We construct an equivalent DFAM ′ = 〈Q′,Σ, δ′, q′0, F ′〉 as follows:

Q′ := P(Q).q′0 := {q0}.δ′(Q1, a) :=

⋃q∈Q1

δ(q, a).F ′ := {Qf ⊆ Q |Qf ∩ F 6= ∅}.

A�er having read the input w, the automaton is in a state Qw ⊆ Qsuch that, in the original NFA, δ(q0,w) = Qw .

18

Page 39: Finite State Automata (Endliche Automaten) Laura Kallmeyer€¦ · Adeterministic •nite state automaton(DFA) M is a quintuple hQ; ; ;q 0;Fisuch that Q is a •nite set of states.

Equivalence of DFA and NFA (4)

Let M = 〈Q,Σ, δ, q0, F〉 be an NFA. We construct an equivalent DFAM ′ = 〈Q′,Σ, δ′, q′0, F ′〉 as follows:

Q′ := P(Q).

q′0 := {q0}.δ′(Q1, a) :=

⋃q∈Q1

δ(q, a).F ′ := {Qf ⊆ Q |Qf ∩ F 6= ∅}.

A�er having read the input w, the automaton is in a state Qw ⊆ Qsuch that, in the original NFA, δ(q0,w) = Qw .

18

Page 40: Finite State Automata (Endliche Automaten) Laura Kallmeyer€¦ · Adeterministic •nite state automaton(DFA) M is a quintuple hQ; ; ;q 0;Fisuch that Q is a •nite set of states.

Equivalence of DFA and NFA (4)

Let M = 〈Q,Σ, δ, q0, F〉 be an NFA. We construct an equivalent DFAM ′ = 〈Q′,Σ, δ′, q′0, F ′〉 as follows:

Q′ := P(Q).q′0 := {q0}.

δ′(Q1, a) :=⋃

q∈Q1δ(q, a).

F ′ := {Qf ⊆ Q |Qf ∩ F 6= ∅}.

A�er having read the input w, the automaton is in a state Qw ⊆ Qsuch that, in the original NFA, δ(q0,w) = Qw .

18

Page 41: Finite State Automata (Endliche Automaten) Laura Kallmeyer€¦ · Adeterministic •nite state automaton(DFA) M is a quintuple hQ; ; ;q 0;Fisuch that Q is a •nite set of states.

Equivalence of DFA and NFA (4)

Let M = 〈Q,Σ, δ, q0, F〉 be an NFA. We construct an equivalent DFAM ′ = 〈Q′,Σ, δ′, q′0, F ′〉 as follows:

Q′ := P(Q).q′0 := {q0}.δ′(Q1, a) :=

⋃q∈Q1

δ(q, a).

F ′ := {Qf ⊆ Q |Qf ∩ F 6= ∅}.

A�er having read the input w, the automaton is in a state Qw ⊆ Qsuch that, in the original NFA, δ(q0,w) = Qw .

18

Page 42: Finite State Automata (Endliche Automaten) Laura Kallmeyer€¦ · Adeterministic •nite state automaton(DFA) M is a quintuple hQ; ; ;q 0;Fisuch that Q is a •nite set of states.

Equivalence of DFA and NFA (4)

Let M = 〈Q,Σ, δ, q0, F〉 be an NFA. We construct an equivalent DFAM ′ = 〈Q′,Σ, δ′, q′0, F ′〉 as follows:

Q′ := P(Q).q′0 := {q0}.δ′(Q1, a) :=

⋃q∈Q1

δ(q, a).F ′ := {Qf ⊆ Q |Qf ∩ F 6= ∅}.

A�er having read the input w, the automaton is in a state Qw ⊆ Qsuch that, in the original NFA, δ(q0,w) = Qw .

18

Page 43: Finite State Automata (Endliche Automaten) Laura Kallmeyer€¦ · Adeterministic •nite state automaton(DFA) M is a quintuple hQ; ; ;q 0;Fisuch that Q is a •nite set of states.

Equivalence of DFA and NFA (4)

Let M = 〈Q,Σ, δ, q0, F〉 be an NFA. We construct an equivalent DFAM ′ = 〈Q′,Σ, δ′, q′0, F ′〉 as follows:

Q′ := P(Q).q′0 := {q0}.δ′(Q1, a) :=

⋃q∈Q1

δ(q, a).F ′ := {Qf ⊆ Q |Qf ∩ F 6= ∅}.

A�er having read the input w, the automaton is in a state Qw ⊆ Qsuch that, in the original NFA, δ(q0,w) = Qw .

18

Page 44: Finite State Automata (Endliche Automaten) Laura Kallmeyer€¦ · Adeterministic •nite state automaton(DFA) M is a quintuple hQ; ; ;q 0;Fisuch that Q is a •nite set of states.

Equivalence of DFA and NFA (5)

From NFA to DFA

q0 q1

q2

a

b

bb

a

a

equivalent DFA:

q0

q01 q02 q012a b a

bb

a a

19

Page 45: Finite State Automata (Endliche Automaten) Laura Kallmeyer€¦ · Adeterministic •nite state automaton(DFA) M is a quintuple hQ; ; ;q 0;Fisuch that Q is a •nite set of states.

Equivalence of DFA and NFA (5)

From NFA to DFA

q0 q1

q2

a

b

bb

a

a

equivalent DFA:

q0 q01

q02 q012

a

b a

bb

a a

19

Page 46: Finite State Automata (Endliche Automaten) Laura Kallmeyer€¦ · Adeterministic •nite state automaton(DFA) M is a quintuple hQ; ; ;q 0;Fisuch that Q is a •nite set of states.

Equivalence of DFA and NFA (5)

From NFA to DFA

q0 q1

q2

a

b

bb

a

a

equivalent DFA:

q0 q01

q02 q012

a

b a

bb

a

a

19

Page 47: Finite State Automata (Endliche Automaten) Laura Kallmeyer€¦ · Adeterministic •nite state automaton(DFA) M is a quintuple hQ; ; ;q 0;Fisuch that Q is a •nite set of states.

Equivalence of DFA and NFA (5)

From NFA to DFA

q0 q1

q2

a

b

bb

a

a

equivalent DFA:

q0 q01 q02

q012

a b

a

bb

a

a

19

Page 48: Finite State Automata (Endliche Automaten) Laura Kallmeyer€¦ · Adeterministic •nite state automaton(DFA) M is a quintuple hQ; ; ;q 0;Fisuch that Q is a •nite set of states.

Equivalence of DFA and NFA (5)

From NFA to DFA

q0 q1

q2

a

b

bb

a

a

equivalent DFA:

q0 q01 q02 q012a b a

bb

a

a

19

Page 49: Finite State Automata (Endliche Automaten) Laura Kallmeyer€¦ · Adeterministic •nite state automaton(DFA) M is a quintuple hQ; ; ;q 0;Fisuch that Q is a •nite set of states.

Equivalence of DFA and NFA (5)

From NFA to DFA

q0 q1

q2

a

b

bb

a

a

equivalent DFA:

q0 q01 q02 q012a b a

b

b

a

a

19

Page 50: Finite State Automata (Endliche Automaten) Laura Kallmeyer€¦ · Adeterministic •nite state automaton(DFA) M is a quintuple hQ; ; ;q 0;Fisuch that Q is a •nite set of states.

Equivalence of DFA and NFA (5)

From NFA to DFA

q0 q1

q2

a

b

bb

a

a

equivalent DFA:

q0 q01 q02 q012a b a

b

b

a a

19

Page 51: Finite State Automata (Endliche Automaten) Laura Kallmeyer€¦ · Adeterministic •nite state automaton(DFA) M is a quintuple hQ; ; ;q 0;Fisuch that Q is a •nite set of states.

Equivalence of DFA and NFA (5)

From NFA to DFA

q0 q1

q2

a

b

bb

a

a

equivalent DFA:

q0 q01 q02 q012a b a

bb

a a

19

Page 52: Finite State Automata (Endliche Automaten) Laura Kallmeyer€¦ · Adeterministic •nite state automaton(DFA) M is a quintuple hQ; ; ;q 0;Fisuch that Q is a •nite set of states.

Minimizing DFAs (1)

Ein DFA kann eindeutig durch folgende Aquivalenzrelation minimiertwerden:p ∼ q genau dann wenn fur alle w gilt: δ(p,w) ∈ F gdw. δ(q,w) ∈ F .Die Aquivalenzklassen [q] bilden dann die Zustande, und fur die neueUbergangsfunktion δ′ gilt δ′([q], a) = [δ(q, a)].

Mit anderen Worten: Zustande, die die gleiche Sprache de�nieren,werden als aquivalent zusammengefasst.

Minimierung: einfaches Beispiel

q0

q1

q2

q3

a

ba

b

ba

q1 ∼ q2

20

Page 53: Finite State Automata (Endliche Automaten) Laura Kallmeyer€¦ · Adeterministic •nite state automaton(DFA) M is a quintuple hQ; ; ;q 0;Fisuch that Q is a •nite set of states.

Minimizing DFAs (1)

Ein DFA kann eindeutig durch folgende Aquivalenzrelation minimiertwerden:p ∼ q genau dann wenn fur alle w gilt: δ(p,w) ∈ F gdw. δ(q,w) ∈ F .Die Aquivalenzklassen [q] bilden dann die Zustande, und fur die neueUbergangsfunktion δ′ gilt δ′([q], a) = [δ(q, a)].

Mit anderen Worten: Zustande, die die gleiche Sprache de�nieren,werden als aquivalent zusammengefasst.

Minimierung: einfaches Beispiel

q0

q1

q2

q3

a

ba

b

ba

q1 ∼ q2

20

Page 54: Finite State Automata (Endliche Automaten) Laura Kallmeyer€¦ · Adeterministic •nite state automaton(DFA) M is a quintuple hQ; ; ;q 0;Fisuch that Q is a •nite set of states.

Minimizing DFAs (2)

Algorithmus fur die Berechnung der Aquivalenzklassen:

1 Entferne alle nutzlosen Zustande (nicht erreichbar oder zukeinem Endzustand fuhrend).

2 Partitioniere Q in Q = {F ,Q \ F}.3 Fur jedes Eingabesymbol a zerlege die Mengen in Q sukzessive

weiter, so dass es anschließend fur alle Q′ ∈ Q ein Q′′ ∈ Q gibtmit δ(q′, a) ∈ Q′′ fur alle q′ ∈ Q′.Anders gesagt: aus einem Q′ ∈ Q bleiben nur die Zustande ineiner Menge, die nach Verarbeitung von a in ein und dieselbeMenge Q′′ ∈ Q fuhren.

4 Wiederhole 3. so lange, bis sich Q nicht mehr verandert.

Der neue Startzustand ist [q0], Endzustande sind alle [qf ] mit qf ∈ F .

21

Page 55: Finite State Automata (Endliche Automaten) Laura Kallmeyer€¦ · Adeterministic •nite state automaton(DFA) M is a quintuple hQ; ; ;q 0;Fisuch that Q is a •nite set of states.

Minimizing DFAs (3)

q0

q1 q2

q3 q4

ab a

bc

c

b a

a

entferne q4 (nutzlos)Start: {q3} {q0, q1, q2}a, b: trennen nichtc: {q3} {q0} {q1, q2}keine weitere Trennung

Ergebnis:q0 q1,2 q3

a,b

a,b

c

22

Page 56: Finite State Automata (Endliche Automaten) Laura Kallmeyer€¦ · Adeterministic •nite state automaton(DFA) M is a quintuple hQ; ; ;q 0;Fisuch that Q is a •nite set of states.

Minimizing DFAs (3)

q0

q1 q2

q3 q4

ab a

bc

c

b a

a

entferne q4 (nutzlos)Start: {q3} {q0, q1, q2}a, b: trennen nichtc: {q3} {q0} {q1, q2}keine weitere Trennung

Ergebnis:q0 q1,2 q3

a,b

a,b

c

22

Page 57: Finite State Automata (Endliche Automaten) Laura Kallmeyer€¦ · Adeterministic •nite state automaton(DFA) M is a quintuple hQ; ; ;q 0;Fisuch that Q is a •nite set of states.

Minimizing DFAs (4)

Einfacher Algorithmus, der berechnet, welche Zustande in einerAquivalenzklasse sind:

1. Entferne nutzlose Zustande.2. Erganze den DFA zu einem vollstandigen DFA durch Hinzufugen

eines Fehlerzustands (trap state).

Example: trap state

q0

q1 q2

q5

q3

a

c

a,b,c

b a

b

cc

b

a

a,b,c

23

Page 58: Finite State Automata (Endliche Automaten) Laura Kallmeyer€¦ · Adeterministic •nite state automaton(DFA) M is a quintuple hQ; ; ;q 0;Fisuch that Q is a •nite set of states.

Minimizing DFAs (4)

Einfacher Algorithmus, der berechnet, welche Zustande in einerAquivalenzklasse sind:

1. Entferne nutzlose Zustande.2. Erganze den DFA zu einem vollstandigen DFA durch Hinzufugen

eines Fehlerzustands (trap state).

Example: trap state

q0

q1 q2

q5

q3

a

c

a,b,c

b a

b

cc

b

a

a,b,c

23

Page 59: Finite State Automata (Endliche Automaten) Laura Kallmeyer€¦ · Adeterministic •nite state automaton(DFA) M is a quintuple hQ; ; ;q 0;Fisuch that Q is a •nite set of states.

Minimizing DFAs (4)

Einfacher Algorithmus, der berechnet, welche Zustande in einerAquivalenzklasse sind:

1. Entferne nutzlose Zustande.2. Erganze den DFA zu einem vollstandigen DFA durch Hinzufugen

eines Fehlerzustands (trap state).

Example: trap state

q0

q1 q2 q5

q3

ac

a,b,c

b a

b

cc

b

a

a,b,c

23

Page 60: Finite State Automata (Endliche Automaten) Laura Kallmeyer€¦ · Adeterministic •nite state automaton(DFA) M is a quintuple hQ; ; ;q 0;Fisuch that Q is a •nite set of states.

Minimizing DFAs (5)

3. Bilde eine (|Q| − 1) × (|Q| − 1) Matrix, deren Spaltenindizesalle Zustande außer dem letzten und deren Zeilenindizes alleZustande außer dem ersten, in umgekehrter Reihenfolge, durch-laufen.

4. Ein X in einem Feld der Matrix bedeutet, dass die beiden Zustande,die die Indizes dieses Feldes bilden, nicht in einer Aquivalenzk-lasse sein konnen. Beginne damit, alle Felder mit genau einemEndzustand mit X zu fullen. (Sie konnen wegen ε nicht in einerKlasse sein.)

24

Page 61: Finite State Automata (Endliche Automaten) Laura Kallmeyer€¦ · Adeterministic •nite state automaton(DFA) M is a quintuple hQ; ; ;q 0;Fisuch that Q is a •nite set of states.

Minimizing DFAs (6)

q0

q1 q2 q5

q3

ac

a,b,c

b a

b

cc

b

a

a,b,c

0 1 2 35

X X X

X3 X X X2

X

1

X

5. Durchlaufe die noch nicht markierten Felder wiederholt, bissich die Matrix nicht mehr andert und fuhre fur jedes Feld mitIndizes 〈p, q〉 folgendes durch:Gibt es ein Eingabesymbol a, so dass das Paar 〈δ(p, a), δ(q, a)〉(oder umgekehrt) markiert ist, so markiere auch das Feld〈p, q〉.

6. Freie Felder sagen uns anschließend, welche Zustande aquiva-lent sind.

25

Page 62: Finite State Automata (Endliche Automaten) Laura Kallmeyer€¦ · Adeterministic •nite state automaton(DFA) M is a quintuple hQ; ; ;q 0;Fisuch that Q is a •nite set of states.

Minimizing DFAs (6)

q0

q1 q2 q5

q3

ac

a,b,c

b a

b

cc

b

a

a,b,c

0 1 2 35

X X X

X3 X X X2

X

1

X

5. Durchlaufe die noch nicht markierten Felder wiederholt, bissich die Matrix nicht mehr andert und fuhre fur jedes Feld mitIndizes 〈p, q〉 folgendes durch:Gibt es ein Eingabesymbol a, so dass das Paar 〈δ(p, a), δ(q, a)〉(oder umgekehrt) markiert ist, so markiere auch das Feld〈p, q〉.

6. Freie Felder sagen uns anschließend, welche Zustande aquiva-lent sind.

25

Page 63: Finite State Automata (Endliche Automaten) Laura Kallmeyer€¦ · Adeterministic •nite state automaton(DFA) M is a quintuple hQ; ; ;q 0;Fisuch that Q is a •nite set of states.

Minimizing DFAs (6)

q0

q1 q2 q5

q3

ac

a,b,c

b a

b

cc

b

a

a,b,c

0 1 2 35

X

X

X

X3 X X X2

X

1

X

5. Durchlaufe die noch nicht markierten Felder wiederholt, bissich die Matrix nicht mehr andert und fuhre fur jedes Feld mitIndizes 〈p, q〉 folgendes durch:Gibt es ein Eingabesymbol a, so dass das Paar 〈δ(p, a), δ(q, a)〉(oder umgekehrt) markiert ist, so markiere auch das Feld〈p, q〉.

6. Freie Felder sagen uns anschließend, welche Zustande aquiva-lent sind.

25

Page 64: Finite State Automata (Endliche Automaten) Laura Kallmeyer€¦ · Adeterministic •nite state automaton(DFA) M is a quintuple hQ; ; ;q 0;Fisuch that Q is a •nite set of states.

Minimizing DFAs (6)

q0

q1 q2 q5

q3

ac

a,b,c

b a

b

cc

b

a

a,b,c

0 1 2 35

X

X X X3 X X X2

X

1

X

5. Durchlaufe die noch nicht markierten Felder wiederholt, bissich die Matrix nicht mehr andert und fuhre fur jedes Feld mitIndizes 〈p, q〉 folgendes durch:Gibt es ein Eingabesymbol a, so dass das Paar 〈δ(p, a), δ(q, a)〉(oder umgekehrt) markiert ist, so markiere auch das Feld〈p, q〉.

6. Freie Felder sagen uns anschließend, welche Zustande aquiva-lent sind.

25

Page 65: Finite State Automata (Endliche Automaten) Laura Kallmeyer€¦ · Adeterministic •nite state automaton(DFA) M is a quintuple hQ; ; ;q 0;Fisuch that Q is a •nite set of states.

Minimizing DFAs (6)

q0

q1 q2 q5

q3

ac

a,b,c

b a

b

cc

b

a

a,b,c

0 1 2 35

X

X X X3 X X X2 X1

X

5. Durchlaufe die noch nicht markierten Felder wiederholt, bissich die Matrix nicht mehr andert und fuhre fur jedes Feld mitIndizes 〈p, q〉 folgendes durch:Gibt es ein Eingabesymbol a, so dass das Paar 〈δ(p, a), δ(q, a)〉(oder umgekehrt) markiert ist, so markiere auch das Feld〈p, q〉.

6. Freie Felder sagen uns anschließend, welche Zustande aquiva-lent sind.

25

Page 66: Finite State Automata (Endliche Automaten) Laura Kallmeyer€¦ · Adeterministic •nite state automaton(DFA) M is a quintuple hQ; ; ;q 0;Fisuch that Q is a •nite set of states.

Minimizing DFAs (6)

q0

q1 q2 q5

q3

ac

a,b,c

b a

b

cc

b

a

a,b,c

0 1 2 35

X

X X X3 X X X2 X1 X

5. Durchlaufe die noch nicht markierten Felder wiederholt, bissich die Matrix nicht mehr andert und fuhre fur jedes Feld mitIndizes 〈p, q〉 folgendes durch:Gibt es ein Eingabesymbol a, so dass das Paar 〈δ(p, a), δ(q, a)〉(oder umgekehrt) markiert ist, so markiere auch das Feld〈p, q〉.

6. Freie Felder sagen uns anschließend, welche Zustande aquiva-lent sind.

25

Page 67: Finite State Automata (Endliche Automaten) Laura Kallmeyer€¦ · Adeterministic •nite state automaton(DFA) M is a quintuple hQ; ; ;q 0;Fisuch that Q is a •nite set of states.

Minimizing DFAs (6)

q0

q1 q2 q5

q3

ac

a,b,c

b a

b

cc

b

a

a,b,c

0 1 2 35 X X X X3 X X X2 X1 X

5. Durchlaufe die noch nicht markierten Felder wiederholt, bissich die Matrix nicht mehr andert und fuhre fur jedes Feld mitIndizes 〈p, q〉 folgendes durch:Gibt es ein Eingabesymbol a, so dass das Paar 〈δ(p, a), δ(q, a)〉(oder umgekehrt) markiert ist, so markiere auch das Feld〈p, q〉.

6. Freie Felder sagen uns anschließend, welche Zustande aquiva-lent sind.

25

Page 68: Finite State Automata (Endliche Automaten) Laura Kallmeyer€¦ · Adeterministic •nite state automaton(DFA) M is a quintuple hQ; ; ;q 0;Fisuch that Q is a •nite set of states.

Minimizing DFAs (6)

q0

q1 q2 q5

q3

ac

a,b,c

b a

b

cc

b

a

a,b,c

0 1 2 35 X X X X3 X X X2 X1 X

5. Durchlaufe die noch nicht markierten Felder wiederholt, bissich die Matrix nicht mehr andert und fuhre fur jedes Feld mitIndizes 〈p, q〉 folgendes durch:Gibt es ein Eingabesymbol a, so dass das Paar 〈δ(p, a), δ(q, a)〉(oder umgekehrt) markiert ist, so markiere auch das Feld〈p, q〉.

6. Freie Felder sagen uns anschließend, welche Zustande aquiva-lent sind.

25

Page 69: Finite State Automata (Endliche Automaten) Laura Kallmeyer€¦ · Adeterministic •nite state automaton(DFA) M is a quintuple hQ; ; ;q 0;Fisuch that Q is a •nite set of states.

Minimizing DFAs (7)

Weiteres Bsp.: Minimierung

q0

q1

q2

q3

aa

b

bbb

a

a FSA vollstandig,kein Trap-State notig

Matrix furMinimierung:

0 1 23 x x2 x x1

Neuer Automat:q01 q23

a a,b

b

26

Page 70: Finite State Automata (Endliche Automaten) Laura Kallmeyer€¦ · Adeterministic •nite state automaton(DFA) M is a quintuple hQ; ; ;q 0;Fisuch that Q is a •nite set of states.

Minimizing DFAs (7)

Weiteres Bsp.: Minimierung

q0

q1

q2

q3

aa

b

bbb

a

a FSA vollstandig,kein Trap-State notig

Matrix furMinimierung:

0 1 23 x x2 x x1

Neuer Automat:q01 q23

a a,b

b

26

Page 71: Finite State Automata (Endliche Automaten) Laura Kallmeyer€¦ · Adeterministic •nite state automaton(DFA) M is a quintuple hQ; ; ;q 0;Fisuch that Q is a •nite set of states.

Minimizing DFAs (7)

Weiteres Bsp.: Minimierung

q0

q1

q2

q3

aa

b

bbb

a

a FSA vollstandig,kein Trap-State notig

Matrix furMinimierung:

0 1 23 x x2 x x1

Neuer Automat:q01 q23

a a,b

b

26