形式语言与自动机 03 Finite Automata

Finite Automata

What is a Finite Automata?

  • A formal system
  • Remembers only a finite amount of information
  • Information represented by its state
  • State changes in response to inputs
  • Rules that tell how the state changes in response to inputs are called transitions

Tennis

形式语言与自动机 03 Finite Automata_第1张图片

Acceptance of Inputs

  • Given a sequence of inputs, start in the start state and follow the transition from each symbol in turn
  • Input is accepted if you wind up in a final state after all inputs have been read

Language of an Automaton

  • The set of strings accepted by an automaton A is the language of A.
  • Denoted L(A).
  • Different sets of final states -> different languages.
  • Example: As designed, L(Tennis) = strings that determine the winner.

Deterministic Finite Automata

  • Alphabets, Strings, and Languages
  • Transition Graphs and Tables
  • Some Proof Techniques

Alphabets

  • An alphabet is any finite set of symbols

Strings

  • A string over an alphabet $\sum $ is a list, each element of which is a member of $\sum $
  • $\sum ^* = $ set of all strings over alphabet ∑ \sum
  • The length of a string is its number of positions
  • ϵ \epsilon ϵ stands for the empty string (string of length 0).

Languages

  • A language is a subset of $\sum ^* $ for some alphabet ∑ \sum

Deterministic Finite Automata

  • A formalism for defining languages, consisting of :
    1. A finite set of states ( Q , t y p i c a l l y Q,typically Q,typically)
    2. An input alphabet ( ∑ , t y p i c a l l y \sum,typically ,typically)
    3. A transition function ( δ , t y p i c a l l y \delta , typically δ,typically)
    4. A start state ( q 0 , i n   Q , t y p i c a l l y q_0,in \:Q,typically q0,inQ,typically)
    5. A set of final states ( F ⊆ Q , t y p i c a l l y F \subseteq Q,typically FQ,typically)

The Transition Function

  • Takes two arguments: a state and an input symbol
  • δ ( q , a )   = \delta(q,a) \: = δ(q,a)= the state that the DFA goes to when it is in state q q q and input a a a ,is received.
  • Note: δ \delta δ is a total function: always a next state - add a dead state if no transition (Example on next slide).

Graph Representation of DFA’ s

  • Nodes = states
  • Arc represents transition function
    • Arc from state p to state q labeled by all those input symbols that have transitions from p to q
  • Arrow labeled “Start” to the start state.
  • Final states indicated by double circles.

Example: Recognizing Strings Ending in “ing”

Alternative Representation: Transition Table

Convention: Strings and Symbols

  • … w,x,y,z are strings.
  • a,b,c,… are single input symbols

Extended Transition Function

  • We describe the effect of a string of inputs on a DFA by extending δ \delta δ to a state and a string.
  • Intuition: Extended δ \delta δ is computed for state q and inputs a 1 a 2 . . . a n a_1a_2...a_n a1a2...an by following a path in the transition graph, starting at q and selecting the arcs with labels a 1 , a 2 , . . . , a n a_1,a_2,...,a_n a1,a2,...,an in turn.

Inductive Definition of Extended $ \delta $

  • Induction on length of string.
  • Basis: δ ( q , ϵ )   = q \delta(q,\epsilon) \: = q δ(q,ϵ)=q
  • Induction: δ ( q , w a ) = δ ( δ ( q , w ) , a ) \delta(q,wa) = \delta(\delta(q,w),a) δ(q,wa)=δ(δ(q,w),a)
    • Remember: w is a string; a is an input symbol, by convention.

Delta-hat

  • We don’t distinguish between the given delta and the extended delta or delta-hat.
  • The reason:
  • δ ( q , a ) = δ ( δ ( q , ϵ ) , a ) = δ ( q , a ) \delta(q,a) = \delta(\delta(q,\epsilon),a) = \delta(q,a) δ(q,a)=δ(δ(q,ϵ),a)=δ(q,a)

Language of a DFA

  • Automata of all kinds define languages.
  • If A is an automaton, L(A) is its language.
  • For a DFA A, L(A) is the set of strings labeling paths from the start state to a final state.
  • Formally: L(A) = the set of strings w such that δ ( q 0 , w ) \delta(q_0,w) δ(q0,w) is in F.

Proofs of Set Equivalence

  • Often, we need to prove that two descriptions of sets are in fact the same set.

  • Here, one set is “the language of this DFA,” and the other is “the set of strings of 0’ s and 1’ s with no consecutive 1’ s.”

  • In general, to prove S = T, we need to prove two parts: S ⊆ T S \subseteq T ST and T ⊆ S T \subseteq S TS. That is:

    1. If w is in S, then w is in T.
    2. If w is in T, then w is in S.
  • Here, S = the language of our running DFA, and T = “no consecutive 1’ s.”

Part 1: S ⊆ T S\subseteq T ST

  • To prove: if w is accepted by then w has no consecutive 1’ s.
  • Proof is an induction on length of w.
  • Important trick: Expand the inductive hypothesis to be more detailed than the statement you are trying to prove.

The Inductive Hypothesis

  • If δ ( A , w ) = A δ(A, w) = A δ(A,w)=A, then w has no consecutive 1’ s and does not end in 1.
  • If δ ( A , w ) = B δ(A, w) = B δ(A,w)=B, then w has no consecutive 1’ s and ends in a single 1.
  • Basis: |w| = 0; i. e. , w = ϵ \epsilon ϵ.
    1. holds since ε has no 1’ s at all.
    2. holds vacuously, since δ(A, ε) is not B. //if 不成立,then 自然为真

Inductive Step

  • Assume (1) and (2) are true for strings shorter than w, where |w| is at least 1

  • Because w is not empty, we can write w = xa, where a is the last symbol of w, and x is the string that precedes

  • IH is true for x

  • Need to prove (1) and (2) for w = xa

  • (1) for w is: If δ ( A , w ) = A \delta(A,w) = A δ(A,w)=A,then w has no consecutive 1’ s and does not end in 1

  • Since δ ( A , w ) = A \delta(A,w) = A δ(A,w)=A δ ( A , w ) \delta(A,w) δ(A,w) must be A or B, and a must be 0

  • By the IH, x has no 11 's

  • Thus, w has no 11’ s and does not end in 1

  • Now, prove (2) for w xa: If δ ( A , w ) = B \delta(A,w) = B δ(A,w)=B, then w has no 11’ s and ends in 1

  • Since δ ( A , w ) = B \delta(A,w) =B δ(A,w)=B, δ ( A , x ) \delta(A,x) δ(A,x) must be A, and a must be 1

  • By the IH, x has no 11’ s and does not end in 1

  • Thus, w has no 11’ s and ends in 1

Part 2: T ⊆ S T\subseteq S TS

  • Now, we must prove: if w has no 11’ s, then w is accepted by that example
  • Contrapositive: If w is not accepted by that, then w has 11

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-qy4cM4aa-1668870017714)(https://s2.loli.net/2022/09/28/ak7rSsdQ4tmyjiP.png)]

Using the Contrapositive

The only way w is not accepted is if it gets to C

  • The only way to get to C is if w = x 1 y, x gets to B and y is the tail of w
  • If δ ( A , x ) = B \delta(A,x) = B δ(A,x)=B,then surely x = z 1 for some z
  • Thus, w = z 11 y and has 11

Regular Languages

  • Language L is regular is it is the language accepted by some DFA
    • Note: the DFA must accept only the strings in L, no others
  • Some languages are not regular
    • Intuitively, regular languages “cannot count” to arbitrarily high integers

Example: A Nonregular Language

L 1 = { 0 n 1 n ∣ n ≥ 1 } L_1 = \{0^n 1^n | n \ge 1\} L1={0n1nn1}

  • Note: a i a^i ai is conventional for i a’ s

  • Rea: “The set of strings consisting of n 0’ s followed by n 1’ s, such that n is at least 1”

  • Thus, L 1 = { 01 , 0011 , 000111 , . . . } L_1 = \{ 01,0011,000111,...\} L1={01,0011,000111,...}

  • Proof ?

  • Suppose there is a DFA with m states

  • $S_0 0^m 1^m \rightarrow … \rightarrow S_1 0^{m-1} 1^m \rightarrow … \rightarrow S_{2m-1} \rightarrow S_2m $

  • For the first m moves, there are m+1 states

  • PHP ! At least one state happen more than once

  • Suppose the state is q

  • S i = S j = q S_i = S_j = q Si=Sj=q

  • S 0 0 m 1 m → q 0 m − i 1 m → . . . → q 0 m − j 1 m → . . . → S 2 m S_0 0^m 1^m \rightarrow q0^{m-i}1^m \rightarrow ... \rightarrow q0^{m-j}1^m \rightarrow ... \rightarrow S_{2m} S00m1mq0mi1m...q0mj1m...S2m

  • How about S 0 0 m − j + i 1 m S_0 0^{m-j+i} 1^m S00mj+i1m

Example: A Regular Language

L 3 = { w ∣ w i n { 0 , 1 } ∗ a n d    w , v i e w e d   a s   a   b i n a r y   i n t e g e r   i s   d i v i s i b l e   b y   23 } L_3 = \{ w | w in \{0,1 \}^* and\; w, viewed \, as \, a\, binary \, integer \, is \, divisible \, by \, 23\} L3={wwin{0,1}andw,viewedasabinaryintegerisdivisibleby23}

  • The DFA:
    • 23 states, named 0, 1,…, 22
    • Correspond to the 23 remainders of an integer divided by 23
    • Start and only final state is 0

Transitions of the DFA for L 3 L_3 L3

  • If string w represents integer i, then assume δ ( 0 , w ) = i % 23 \delta (0,w) = i \% 23 δ(0,w)=i%23
  • Then w0 represents integer 2i, so we want δ ( i % 23 , 0 ) = ( 2 i ) % 23 \delta(i \% 23,0) = (2i) \% 23 δ(i%23,0)=(2i)%23
  • Similarly: w1 represents 2i+1, so we want δ ( i % 23 , 1 ) = ( 2 i + 1 ) % 23 \delta(i\% 23,1) = (2i + 1)\% 23 δ(i%23,1)=(2i+1)%23
  • Example: δ ( 15 , 0 ) = 30 % 23 = 7 ; δ ( 11 , 1 ) = 23 % 23 = 0 \delta(15,0) = 30 \% 23 = 7; \delta(11,1) = 23\% 23 = 0 δ(15,0)=30%23=7;δ(11,1)=23%23=0

Another Example

L 4 = { w ∣ w    i n { 0 , 1 } } L_4 = \{ w | w \; in \{ 0,1\}\} L4={wwin{0,1}} and w, viewed as the reverse of a binary integer is divisible by 23

  • Example: 01110100 is in L 4 L_4 L4 , because 46/23 == 2
  • Hard to construct the DFA
  • But there is a theorem that says the reverse of regular is also regular

Nondeterministic Finite Automata

非确定性有穷自动机

Nondeterminism

  • A nondeterministic finite automaton has the ability to be in several at once

  • Transitions from a state on an input symbol can be to any set of states

  • Start in one start state

  • Accept if any sequence of choices leads to a final state

  • Intuitively: the NFA always “guesses right”

Example: Moves on a Chessboard

  • States = squares
  • Inputs = r(move to an adjacent red square) and b (move to an adjacent black square)
  • Start state, final state are in opposite corners

形式语言与自动机 03 Finite Automata_第2张图片

Formal NFA

  • A finite set of states, typically Q
  • An input alphabet, typically Σ \Sigma Σ
  • A transition function, typically δ \delta δ
  • A start state in Q, typically q 0 q_0 q0
  • A set of final states F ⊆ Q F \subseteq Q FQ

Transition Function of NFA

  • $\delta(q,a) $ is a set of states
  • Extend to strings as follows
  • Basis: δ ( q , ϵ ) = { q } \delta(q,\epsilon) = \{ q\} δ(q,ϵ)={q}
  • Induction: $\delta(q,wa) $ = the union over all states p in δ ( q , w )   o f    δ ( p , a ) \delta(q,w) \, of \, \,\delta(p,a) δ(q,w)ofδ(p,a)

Language of an NFA

  • A string w is accepted by an NFA if δ ( q 0 , w ) \delta{(q_0,w)} δ(q0,w) contains at least one final state
  • The language of the NFA is the set of strings it accepts

Example: Language of an NFA

  • For our chessboard NFA we saw rbb is accepted
  • If the input consists of only b’ s, the set of accessible states alternates between {5} and {1,3,7,9}, so only even-length, nonempty strings of b’ s are accepted
  • What about strings with at least one r?

形式语言与自动机 03 Finite Automata_第3张图片

Equivalence of DFA’ s, NFA’ s

Part 1

  • A DFA can be turned into an DFA that accepts the same language
  • If δ D ( q , a ) \delta_D (q,a) δD(q,a) = p, let the NFA have δ N ( q , a ) \delta_N (q,a) δN(q,a) = {p}
  • Then the NFA is always in a set containing exactly one state - the state the DFA is in after reading the same input

Part 2

  • Surprisingly, for any NFA there is a DFA that accepts the same language
  • Proof is the subset construction
  • The number of states of the DFA can be exponential in the number of states of the NFA
  • Thus, NFA’ s accept exactly the regular languages

Subset Construction

  • Given an NFA with states Q, inputs Σ \Sigma Σ , transition function δ N \delta _{N} δN, start state q 0 q_0 q0, and final states F, construct equivalent DFA with:
    • States 2 Q 2^Q 2Q (Set of subsets of Q)
    • Inputs Σ \Sigma Σ
    • Start state { q 0 } \{ q_0 \} {q0}
    • Final states = all those with a member of F

Critical Point

  • The DFA states have names that are sets of NFA states

  • But as a DFA state, an expression like { p , q } \{ p,q\} {p,q} must be understood to be a single symbol, not as a set

  • Analogy: a class of object whose values are sets of objects of another class

  • The transition function $\delta _D $ is defined by:

    δ D ( { q 1 , . . . , q k } , a ) \delta_D (\{ q_1, ...,q_k\} , a) δD({q1,...,qk},a) is the union over all i = 1 , . . . , k i = 1,...,k i=1,...,k of δ D ( q i , a ) \delta _D (q_i,a) δD(qi,a)

Example

形式语言与自动机 03 Finite Automata_第4张图片

Proof of Equivalence

Basic

  • The proof is almost a pun
  • Show by induction on |w| that δ D ( q 0 , w ) = δ D ( { q 0 } , w ) \delta_D (q_0, w) = \delta_D(\{q_0\},w) δD(q0,w)=δD({q0},w)
  • Basic: w = ϵ : δ N ( q 0 , ϵ ) \epsilon : \delta_N (q_0, \epsilon ) ϵ:δN(q0,ϵ) = δ D ( { q 0 } , ϵ ) = { q 0 } \delta_D (\{q_0\},\epsilon) = \{ q_0\} δD({q0},ϵ)={q0}

Induction

  • Assume IH for strings shorter than w
  • Let w = xa; IH holds for x
  • Let δ N ( q 0 , x ) = δ ( { q 0 } , x ) \delta _N (q_0,x) = \delta(\{q_0\},x) δN(q0,x)=δ({q0},x) = S
  • Let T = the union over all states p in S of δ N ( p , a ) \delta_N(p,a) δN(p,a)
  • Then δ N ( q 0 , w ) = δ D ( { q 0 } , w ) \delta_N(q_0,w) = \delta_D(\{q_0\},w) δN(q0,w)=δD({q0},w) = T

But

Sub-Construction may lead to Bad case (指数增长)

NFA’ s With $ \epsilon $ - Transitions

  • We can allow state-to-state transitions on ϵ \epsilon ϵ input
  • These transitions are done spontaneously, without looking at the input string
  • A convenience at times, but still only regular languages are accepted

$ \epsilon $ - NFA

形式语言与自动机 03 Finite Automata_第5张图片

Closure of States

  • CL(q) = set states you can reach from state q following only arcs labeled $ \epsilon $

  • CL(A) = {A}

  • CL(E) = {B,C,D,E}

  • Closure of a set of states = union of the closure of each state

Extended Delta

  • Intuition: δ ^ ( q , w ) \hat{\delta} (q,w) δ^(q,w) is the set of states you can reach from q following a path labled w
  • Basic: δ ^ ( q , ϵ ) = C L ( q ) \hat{\delta} (q,\epsilon) = CL(q) δ^(q,ϵ)=CL(q)
  • Induction: δ ^ ( q , x a ) \hat{\delta}(q,xa) δ^(q,xa) is computed by:
    • Start with $\hat\delta(q,x) $ = S
    • Take the union of CL( δ ( p , a ) \delta(p,a) δ(p,a)) for all p in S

Equivalence of NFA, $ \epsilon $ - NFA

  • Every NFA is an ϵ \epsilon ϵ - NFA

    • It just has no transitions on ϵ \epsilon ϵ
  • Converse requires us to take an ϵ \epsilon ϵ - NFA and construct an NFA that accepts the same language

  • We do so by combining ϵ \epsilon ϵ - transitions with the next transition on a real input

  • Start with an ϵ \epsilon ϵ - NFA with states Q, inputs Σ \Sigma Σ , start state q 0 q_0 q0 , final states F, and transition function δ E \delta_E δE

  • Construct an “ordinary” NFA with sates Q, inputs Σ \Sigma Σ, start state q 0 q_0 q0, final states F’ , and transition function δ N \delta_N δN

  • Compute $\delta_N(q,a) $ as follows:

    1. Let S =CL(q)
    2. δ N ( q , a ) \delta_N(q,a) δN(q,a) is the union over all p in S of δ E ( p , a ) \delta_E(p,a) δE(p,a)
  • F’ = the set of states q such that CL(q) contains a state of F

  • Prove by induction on |w| that CL( δ N ( q 0 , w ) \delta_N(q_0,w) δN(q0,w)) = δ E ^ ( q 0 , w ) \hat{\delta_E} (q_0,w) δE^(q0,w)

  • Thus, the ϵ \epsilon ϵ - NFA accepts w if and only if the “ordinary” NFA does

形式语言与自动机 03 Finite Automata_第6张图片

你可能感兴趣的:(形式语言与自动机,算法)