Docsity

Conversion of Deterministic Finite Automata to Regular Expressions - Prof. Mahesh Viswanat, Study notes of Computer Science

Partial preview of the text

Download Conversion of Deterministic Finite Automata to Regular Expressions - Prof. Mahesh Viswanat and more Study notes Computer Science in PDF only on Docsity!

CS 373: Theory of Computation

Manoj Prabhakaran Mahesh Viswanathan

Fall 2008

Regular Expressions and Regular Languages Why do they have such similar names?

Theorem 1. L is a regular language if and only if there is a regular expression R such that L(R) = L

i.e., Regular expressions have the same “expressive power” as finite automata.

Proof. • Given regular expression R, can construct NFA N such that L(N ) = L(R)

  • Given DFA M , will construct regular expression R such that L(M ) = L(R)

1 Converting a DFA to an Equivalent Regular Expression

DFA to Regular Expression

  • Given DFA M , will construct regular expression R such that L(M ) = L(R). In two steps:
    • Construct a “Generalized NFA” (GNFA) G from the DFA M
    • And then convert G to a regex R

1.1 Generalized NFA

Generalized NFA

  • A GNFA is similar to an NFA, but:
    • There is a single accept state.
    • The start state has no incoming transitions, and the accept state has no outgoing tran- sitions. ∗ These are “cosmetic changes”: Any NFA can be converted to an equivalent NFA of this kind.
    • The transitions are labeled not by characters in the alphabet, but by regular expressions. ∗ For every pair of states (q 1 , q 2 ), the transition from q 1 to q 2 is labeled by a regular expression ρ(q 1 , q 2 ).
    • “Generalized NFA” because a normal NFA has transitions labeled by , elements in Σ (a union of elements, if multiple edges between a pair of states) and ∅ (missing edges).

Generalized NFA

  • w = x 1 x 2 x 3 · · · xt
  • r 0 = q 0 and rt = qF
  • for each i ∈ [1, t], xi ∈ L(ρ(ri− 1 , ri)),

1.2 Converting DFA to GNFA

Converting DFA to GNFA A DFA M = (Q, Σ, δ, q 0 , F ) can be easily converted to an equivalent GNFA G = (Q′, Σ, q 0 ′, q′ F , ρ):

  • Q′^ = Q ∪ {q 0 ′, q′ F }
  • ρ(q 1 , q 2 ) =

, if q 1 = q′ 0 and q 2 = q 0 , if q 1 ∈ F and q 2 = q′ F ⋃ {a|δ(q 1 ,a)=q 2 } a^ otherwise

q′ 0 q 0 q′ F

q 1

q 2

Prove: L(G) = L(M ).

1.3 Converting GNFA to Regular Expression

GNFA to Regex

  • Suppose G is a GNFA with only two states, q 0 and qF.
  • Then L(R) = L(G) where R = ρ(q 0 , qF ).
  • How about G with three states?

q 0

q 1

qF

R 4

R 1 R 3

R 2

q 0 qF

(R 4 ) ∪ (R 1 R 2 ∗R 3 )

  • Plan: Reduce any GNFA G with k > 2 states to an equivalent GFA with k − 1 states.

GNFA to Regex: From k states to k − 1 states

Definition 4 (Deleting a GNFA State). Given GNFA G = (Q, Σ, q 0 , qF , ρ) with |Q| > 2, and any state q∗^ ∈ Q \ {q 0 , qF }, define GNFA rip(G, q∗) = (Q′, Σ, q 0 , qF , ρ′) as follows:

  • Q′^ = Q \ {q∗}.
  • For any (q 1 , q 2 ) ∈ Q′^ \ {qF } × Q′^ \ {q 0 } (possibly q 1 = q 2 ), let

ρ′(q 1 , q 2 ) = (R 1 R∗ 2 R 3 ) ∪ R 4 ,

where R 1 = ρ(q 1 , q∗), R 2 = ρ(q∗, q∗), R 3 = ρ(q∗, q 2 ) and R 4 = ρ(q 1 , q 2 ).

Claim. For any q∗^ ∈ Q \ {q 0 , qF }, G and rip(G, q∗) are equivalent.

GNFA to Regex: From k states to k − 1 states w ∈ L(G) =⇒ w ∈ L(G′)

Proof. • w ∈ L(G) =⇒ w = x 1 x 2 x 3 · · · xt, and a sequence of states q 0 = r 0 , r 1 ,... , rt = qF s.t. xi ∈ L(ρ(ri− 1 , ri)).

  • Let (q 0 = s 0 ,... , sd = qF ) be the subsequence of states obtained by deleting all occurrences of q∗.
  • For any run of q∗^ — i.e., an interval [a, b] s.t. ra− 1 6 = q∗^ = ra =... = rb− 1 6 = rb — let x[a,b] = xa · · · xb.
  • If sj− 1 = ra− 1 and sj = rb, then x[a,b] ∈ L(ρ′(sj− 1 , sj ))
  • σ(j) = σ(j − 1) + 1 (i.e., rσ(j−1)+1 6 = q∗). Then yj = xi and sj− 1 = ri− 1 and sj = ri, where i = σ(j). yj = xi ∈ L(ρ(ri− 1 , ri)) ⊆ L(ρ′(ri− 1 , ri)) = L(ρ′(sj− 1 , sj )).
  • σ(j) > σ(j − 1) + 1 (i.e., rσ(j−1)+1 = q∗). Then yj = x· · · xi and sj− 1 = r− 1 and sj = ri, where ` = σ(j − 1) + 1 and i = σ(j).

yj = x· · · xi ∈ L(ρ(r− 1 , r)ρ(r, r+1) · · · ρ(ri− 1 , ri)ρ(ri, ri+1)) = L(ρ(r− 1 , q∗)ρ(q∗, q∗)i−ρ(q∗, ri)) ⊆ L(ρ(r− 1 , r`)ρ(q∗, q∗)∗ρ(q∗, ri)) ⊆ L(ρ(sj− 1 , q∗)ρ(q∗, q∗)∗ρ(q∗, sj )) ⊆ L(ρ′(sj− 1 , sj ))

Thus w ∈ L(G′) as we set out to prove. Next we need to show that w ∈ L(G′) =⇒ w ∈ L(G). w ∈ L(G′) =⇒ ∃q 0 = s 0 , s 1 ,... , sd = qF and y 1 ,... , yd ∈ Σ∗^ such that w = y 1 y 2 y 3 · · · yd and for each j, yj ∈ L(ρ′(sj− 1 , sj )) = L ((ρ(sj− 1 , q∗)ρ(q∗, q∗)∗ρ(q∗, ri)) ∪ ρ(sj− 1 , sj )) Define σ as follows, for j = 0,... , d:

σ(j) =

0 if j = 0, σ(j − 1) + 1 if yj ∈ L(ρ(sj− 1 , sj )) σ(j − 1) + u + 2 otherwise, where u = minv (yj ∈ L(ρ(sj− 1 , q∗)ρ(q∗, q∗)vρ(q∗, sj )))

Let t = σ(d). For i = 0,... , t define ri as follows:

r(i) =

sj if there exists j such thati = σ(j), q∗^ otherwise.

Finally, define xi (i = 1,... , t) as follows: if i = σ(j) and i − 1 = σ(j − 1), then let xi = yj. For other i (σ(j − 1) < i − 1 < i ≤ σ(j) for some j), we have yj ∈ L(ρ(sj− 1 , q∗)ρ(q∗, q∗)uρ(q∗, sj )) where u = σ(j) − σ(j − 1) − 2. Therefore we can write yj = x· · · xσ(j), where = σ(j − 1) + 1, such that x∈ L(ρ(sj− 1 , q∗)), xσ(j) ∈ L(ρ(q∗, sj ) and x+1,... , xσ(j)− 1 ∈ L(ρ(q∗, q∗)). Verify that all xi (i = 1,... , t) are well-defined by this. With these definitions it can be easily verified that x 0 · · · xt = y 0 · · · yd = w and xi ∈ L(ρ(ri− 1 , ri)).

DFA to Regex: Summary

Lemma 5. For every DFA M , there is a regular expression R such that L(M ) = L(R).

  • Any DFA can be converted into an equivalent GNFA. So let G be a GNFA s.t. L(M ) = L(G).
  • For any GNFA G = (Q, Σ, q 0 , qF , ρ) with |Q| > 2, for any q∗^ ∈ Q \ {q 0 , qF }, G and rip(G, q∗) are equivalent. rip(G, q∗) has one fewer state than G.
  • So given G, by applying rip repeatedly (choosing q∗^ arbitrarily each time), we can get a GNFA G′^ with two states s.t. L(G) = L(G′). Formally, by induction on the number of states in G.
  • For a 2-state GNFA G′, L(G′) = L(R), where R = ρ(q 0 , qF ).

DFA to Regex: Example

q 1 q 2

Figure 2: Example DFA D

q 1 q 2

q 0 qF

Figure 3: GNFA G equivalent to D, ignoring transitions labelled ∅