Kleene's algorithm

From HandWiki

In theoretical computer science, in particular in formal language theory, Kleene's algorithm transforms a given nondeterministic finite automaton (NFA) into a regular expression. Together with other conversion algorithms, it establishes the equivalence of several description formats for regular languages. Alternative presentations of the same method include the "elimination method" attributed to Brzozowski and McCluskey, the algorithm of McNaughton and Yamada,[1] and the use of Arden's lemma.

Algorithm description

According to Gross and Yellen (2004),[2] the algorithm can be traced back to Kleene (1956).[3] A presentation of the algorithm in the case of deterministic finite automata (DFAs) is given in Hopcroft and Ullman (1979).[4] The presentation of the algorithm for NFAs below follows Gross and Yellen (2004).[2]

Given a nondeterministic finite automaton M = (Q, Σ, δ, q0, F), with Q = { q0,...,qn } its set of states, the algorithm computes

the sets Rkij of all strings that take M from state qi to qj without going through any state numbered higher than k.

Here, "going through a state" means entering and leaving it, so both i and j may be higher than k, but no intermediate state may. Each set Rkij is represented by a regular expression; the algorithm computes them step by step for k = -1, 0, ..., n. Since there is no state numbered higher than n, the regular expression Rn0j represents the set of all strings that take M from its start state q0 to qj. If F = { q1,...,qf } is the set of accept states, the regular expression Rn01 | ... | Rn0f represents the language accepted by M.

The initial regular expressions, for k = -1, are computed as follows for ij:

R−1ij = a1 | ... | am       where qj ∈ δ(qi,a1), ..., qj ∈ δ(qi,am)

and as follows for i=j:

R−1ii = a1 | ... | am | ε       where qi ∈ δ(qi,a1), ..., qi ∈ δ(qi,am)

In other words, R−1ij mentions all letters that label a transition from i to j, and we also include ε in the case where i=j.

After that, in each step the expressions Rkij are computed from the previous ones by

Rkij = Rk-1ik (Rk-1kk)* Rk-1kj | Rk-1ij

Another way to understand the operation of the algorithm is as an "elimination method", where the states from 0 to n are successively removed: when state k is removed, the regular expression Rk-1ij, which describes the words that label a path from state i>k to state j>k, is rewritten into Rkij so as to take into account the possibility of going via the "eliminated" state k.

By induction on k, it can be shown that the length[5] of each expression Rkij is at most 1/3(4k+1(6s+7) - 4) symbols, where s denotes the number of characters in Σ. Therefore, the length of the regular expression representing the language accepted by M is at most 1/3(4n+1(6s+7)f - f - 3) symbols, where f denotes the number of final states. This exponential blowup is inevitable, because there exist families of DFAs for which any equivalent regular expression must be of exponential size.[6]

In practice, the size of the regular expression obtained by running the algorithm can be very different depending on the order in which the states are considered by the procedure, i.e., the order in which they are numbered from 0 to n.

Example

Example DFA given to Kleene's algorithm

The automaton shown in the picture can be described as M = (Q, Σ, δ, q0, F) with

  • the set of states Q = { q0, q1, q2 },
  • the input alphabet Σ = { a, b },
  • the transition function δ with δ(q0,a)=q0,   δ(q0,b)=q1,   δ(q1,a)=q2,   δ(q1,b)=q1,   δ(q2,a)=q1, and δ(q2,b)=q1,
  • the start state q0, and
  • set of accept states F = { q1 }.

Kleene's algorithm computes the initial regular expressions as

R−100     = a | ε
R−101 = b
R−102 = ∅
R−110 = ∅
R−111 = b | ε
R−112 = a
R−120 = ∅
R−121 = a | b
R−122 = ε

After that, the Rkij are computed from the Rk-1ij step by step for k = 0, 1, 2. Kleene algebra equalities are used to simplify the regular expressions as much as possible.

Step 0
R000     = R−100 (R−100)* R−100 | R−100     = (a | ε) (a | ε)* (a | ε) | a | ε     = a*
R001 = R−100 (R−100)* R−101 | R−101 = (a | ε) (a | ε)* b | b = a* b
R002 = R−100 (R−100)* R−102 | R−102 = (a | ε) (a | ε)* | ∅ = ∅
R010 = R−110 (R−100)* R−100 | R−110 = ∅ (a | ε)* (a | ε) | ∅ = ∅
R011 = R−110 (R−100)* R−101 | R−111 = ∅ (a | ε)* b | b | ε = b | ε
R012 = R−110 (R−100)* R−102 | R−112 = ∅ (a | ε)* | a = a
R020 = R−120 (R−100)* R−100 | R−120 = ∅ (a | ε)* (a | ε) | ∅ = ∅
R021 = R−120 (R−100)* R−101 | R−121 = ∅ (a | ε)* b | a | b = a | b
R022 = R−120 (R−100)* R−102 | R−122 = ∅ (a | ε)* | ε = ε
Step 1
R100     = R001 (R011)* R010 | R000     = a*b (b | ε)* | a*         = a*
R101 = R001 (R011)* R011 | R001 = a*b (b | ε)* (b | ε) | a* b = a* b* b
R102 = R001 (R011)* R012 | R002 = a*b (b | ε)* a | ∅ = a* b* ba
R110 = R011 (R011)* R010 | R010 = (b | ε) (b | ε)* | ∅ = ∅
R111 = R011 (R011)* R011 | R011 = (b | ε) (b | ε)* (b | ε) | b | ε = b*
R112 = R011 (R011)* R012 | R012 = (b | ε) (b | ε)* a | a = b* a
R120 = R021 (R011)* R010 | R020 = (a | b) (b | ε)* | ∅ = ∅
R121 = R021 (R011)* R011 | R021 = (a | b) (b | ε)* (b | ε) | a | b = (a | b) b*
R122 = R021 (R011)* R012 | R022 = (a | b) (b | ε)* a | ε = (a | b) b* a | ε
Step 2
R200     = R102 (R122)* R120 | R100     = a*b*ba ((a|b)b*a | ε)* | a* = a*
R201 = R102 (R122)* R121 | R101 = a*b*ba ((a|b)b*a | ε)* (a|b)b* | a* b* b = a* b (a (a | b) | b)*
R202 = R102 (R122)* R122 | R102 = a*b*ba ((a|b)b*a | ε)* ((a|b)b*a | ε) | a* b* ba = a* b* b (a (a | b) b*)* a
R210 = R112 (R122)* R120 | R110 = b* a ((a|b)b*a | ε)* | ∅ = ∅
R211 = R112 (R122)* R121 | R111 = b* a ((a|b)b*a | ε)* (a|b)b* | b* = (a (a | b) | b)*
R212 = R112 (R122)* R122 | R112 = b* a ((a|b)b*a | ε)* ((a|b)b*a | ε) | b* a = (a (a | b) | b)* a
R220 = R122 (R122)* R120 | R120 = ((a|b)b*a | ε) ((a|b)b*a | ε)* | ∅ = ∅
R221 = R122 (R122)* R121 | R121 = ((a|b)b*a | ε) ((a|b)b*a | ε)* (a|b)b* | (a | b) b* = (a | b) (a (a | b) | b)*
R222 = R122 (R122)* R122 | R122 = ((a|b)b*a | ε) ((a|b)b*a | ε)* ((a|b)b*a | ε) | (a | b) b* a | ε     = ((a | b) b* a)*

Since q0 is the start state and q1 is the only accept state, the regular expression R201 denotes the set of all strings accepted by the automaton.

See also

  • Floyd–Warshall algorithm — an algorithm on weighted graphs that can be implemented by Kleene's algorithm using a particular Kleene algebra
  • Star height problem — what is the minimum stars' nesting depth of all regular expressions corresponding to a given DFA?
  • Generalized star height problem — if a complement operator is allowed additionally in regular expressions, can the stars' nesting depth of Kleene's algorithm's output be limited to a fixed bound?
  • Thompson's construction algorithm — transforms a regular expression to a finite automaton

References

  1. McNaughton, R.; Yamada, H. (March 1960). "Regular Expressions and State Graphs for Automata". IRE Transactions on Electronic Computers EC-9 (1): 39–47. doi:10.1109/TEC.1960.5221603. ISSN 0367-9950. 
  2. 2.0 2.1 Jonathan L. Gross and Jay Yellen, ed (2004). Handbook of Graph Theory. Discrete Mathematics and it Applications. CRC Press. ISBN 1-58488-090-2.  Here: sect.2.1, remark R13 on p.65
  3. Kleene, Stephen C. (1956). "Representation of Events in Nerve Nets and Finite Automata". Automata Studies, Annals of Math. Studies (Princeton Univ. Press) 34. http://www.dlsi.ua.es/~mlf/nnafmc/papers/kleene56representation.pdf.  Here: sect.9, p.37-40
  4. John E. Hopcroft, Jeffrey D. Ullman (1979). Introduction to Automata Theory, Languages, and Computation. Addison-Wesley. ISBN 0-201-02988-X. https://archive.org/details/introductiontoau00hopc.  Here: Section 3.2.1 pages 91-96
  5. More precisely, the number of regular-expression symbols, "ai", "ε", "|", "*", "·"; not counting parentheses.
  6. Gruber, Hermann; Holzer, Markus (2008). "Finite Automata, Digraph Connectivity, and Regular Expression Size". in Aceto, Luca; Damgård, Ivan; Goldberg, Leslie Ann et al.. Automata, Languages and Programming. Lecture Notes in Computer Science. 5126. Springer Berlin Heidelberg. pp. 39–50. doi:10.1007/978-3-540-70583-3_4. ISBN 9783540705833. . Theorem 16.