# Forward Injective Finite Automata: Exact and Random Generation of Nonisomorphic NFAs

Miguel Ferreira, Nelma Moreira, Rogério Reis

► To cite this version:

Miguel Ferreira, Nelma Moreira, Rogério Reis. Forward Injective Finite Automata: Exact and Random Generation of Nonisomorphic NFAs. 20th International Conference on Descriptional Complexity of Formal Systems (DCFS), Jul 2018, Halifax, NS, Canada. pp.88-100, 10.1007/978-3-319-94631-3_8 . hal-01905623

HAL Id: hal-01905623

https://inria.hal.science/hal-01905623v1

Submitted on 26 Oct 2018

**HAL** is a multi-disciplinary open access
archive for the deposit and dissemination of sci-
entific research documents, whether they are pub-
lished or not. The documents may come from
teaching and research institutions in France or
abroad, or from public or private research centers.

L'archive ouverte pluridisciplinaire HAL, est
destinée au dépôt et à la diffusion de documents
scientifiques de niveau recherche, publiés ou non,
émanant des établissements d'enseignement et de
recherche français ou étrangers, des laboratoires
publics ou privés.

<figure><img src="image_1.png" /></figure>

Distributed under a Creative Commons CC BY 4.0 - Attribution - International License
# Forward Injective Finite Automata: Exact and Random Generation of Nonisomorphic NFAs

Miguel Ferreira, Nelma Moreira, and Rogério Reis*

CMUP and DCC, Faculdade de Ciências da Universidade do Porto,
Rua do Campo Alegre, 1021, 4169-007 Porto, Portugal
miguelferreira108@gmail.com, {nam,rvr}@dcc.fc.up.pt

**Abstract** We define the class of forward injective finite automata (FIFA) and study some of their properties. Each FIFA has a unique canonical representation up to isomorphism. Using this representation an enumeration is given and an efficient uniform random generator is presented. We provide a conversion algorithm from a nondeterministic finite automaton or regular expression into an equivalent FIFA. Finally, we present some experimental results comparing the size of FIFA with other automata.

## 1 Introduction

The study of the average-case complexity of determinisation of nondeterministic finite automata (NFAs) is an important research topic. In most algorithms that use NFAs, it is needed, in a way or another, to convert them to equivalent deterministic finite automata (DFAs), and that leads to an exponential blow up, in the worst-case. In practice, the feasibility of algorithms and representations, dealing with regular languages and related systems, depends primarily on their complexity on the average case, rather than the worst-case scenario which may rarely occur. The average-case analysis of operations in automata is, in general, a difficult task. One approach to this problem is to consider uniformly distributed random representations and to perform statistically significant experiments requiring most of the times nonisomorphic sampled automata. There are several uniform random generators available for nonisomorphic DFAs [4,1,3,10]. For all these generators, DFAs are considered initially connected and it is possible to order their states in a canonical way to ensure that no two different isomorphic automata are generated. However for NFAs, the problem seems unfeasible in general as for n-state NFAs the size of the automorphism group can be n!, and this is polynomially equivalent to testing if two NFAs are isomorphic. Recently, Héam and Joly [6] presented uniform random generators for some classes of NFAs up to isomorphism, using Monte Carlo Markov-Chains modified by the Metropolis-Hastings algorithm, to deal with isomorphic objects [8]. This involves the computation of the sizes of automorphism groups of NFAs. However,

<footnote>* Authors partially funded by CMUP (UID/MAT/00144/2013), which is funded by FCT (Portugal) with national (MCTES) and European structural funds through the programs FEDER, under the partnership agreement PT2020.</footnote>
the performance of these algorithms does not seem to allow the generation of large sets of objects of acceptable size. Considering classes of NFAs for which this latter problem is polynomial, the authors obtain (randomised/heuristic) uniform random generators in polynomial time. These classes include trim NFAs with a single initial state and with states with a fixed maximal output degree.

In this paper, we follow a different path and study a class of NFAs for which it is possible the exact and uniform random generation of nonisomorphic elements. More precisely, we consider a class of initially-connected NFAs (with a single initial state) for which it is possible to define a canonical order over the set of states. Starting with the initial state, whenever new states are reached from an already visited state, we ensure that the set of transition labels are pairwise distinct. Based on a natural order of the power set of the alphabet, an ordering for the set of states is obtained. This induces a unique canonical representation for the nonisomorphic NFAs for which it is possible to obtain such an order, and which are named *forward injective finite automata* (FIFA). We also developed an algorithm that converts an NFA into an equivalent FIFA and performed some experimental tests in order to compare their relative sizes. The class of FIFA represents all regular languages, FIFA sampling is proven to be efficient to implement, and experimental results suggest that, on average, for each NFA one can find a FIFA which is not much larger. These properties convinced the authors that FIFA is a class which deserves to be studied. In particular, FIFA based sampling is a possible alternative for average complexity analysis of algorithms that have NFAs as input. The paper is organised as follows. In the next section, some notation is given and we review the canonical string representation for initially-connected DFAs. In Section 3, we present the new class of automata (FIFA), a canonical representation that is unique for nonisomorphic automata of this class and give an enumeration of FIFAs by states and alphabetic size. A uniform random generator for FIFA is given in Section 4. In section 5 we adapted the determinisation to the conversion from NFAs to FIFAs, together with some experimental results in Section 6. Some future work is discussed in Section 7.

## 2 Preliminaries

Given two integers, $m$ and $n$, let $[m, n]$ be the set $\{ i \in \mathbb{Z} \mid m \le i \land i \le n \}$, and let one use the additional interval notation with its standard meaning.

An NFA $A$ is a tuple $(Q, \Sigma, \delta, q_0, F)$ where $Q$ is a finite set of states, $\Sigma$ the alphabet, i.e., a nonempty finite set of symbols, $\delta: Q \times \Sigma \to 2^Q$ is the transition function, $q_0$ the initial state and $F \subseteq Q$ the set of final states. As usual, let $\delta(q) = \{ p \mid \exists \sigma \in \Sigma, p \in \delta(q, \sigma) \}$. We also consider $\ell: Q \times Q \to 2^\Sigma$, such that $\ell(p, q) = \{ \sigma \mid q \in \delta(p, \sigma) \land \sigma \in \Sigma \}$, for any $p, q \in Q$, and for $\emptyset \ne S \subseteq \Sigma$, let $\ell_p^{-1}(S) = \{ q \mid \ell(p, q) = S \}$. The function $\ell$ allows us to consider the amalgamation of all transitions with the same start and end states, and the value of $\ell$ as label. In the rest of the paper, we will consider either $\delta$ or $\ell$ to refer to the transition function of a given NFA. The size of an NFA is its number of states, $|Q| = n$. An NFA is *initially connected* (or *accessible*) if
for each state $q \in Q$ there exists a sequence $(q'_i)_{i \in [0,j]}$ of states and a sequence $(\sigma_i)_{i \in [0,j-1]}$ of symbols, for some $j < |Q|$, such that $q'_{m+1} \in \delta(q'_m, \sigma_m)$ for $m \in [0, j[$, $q'_0 = q_0$ and $q'_j = q$. The transition function $\delta: Q \times \Sigma \to 2^Q$ extends naturally to $\Sigma^*$ and to sets of states. The language accepted by $A$ is $\mathcal{L}(A) = \{w \in \Sigma^* | \delta(q_0, w) \cap F \neq \emptyset\}$. Two automata are equivalent if they accept the same language. Two NFAs $\langle Q, \Sigma, \delta, q_0, F \rangle$ and $\langle Q', \Sigma, \delta', q'_0, F' \rangle$ are isomorphic if there is a bijection $\iota: Q \to Q'$ such that $\iota(q_0) = q'_0$, for all $\sigma \in \Sigma$ and $q \in Q$, $\iota(\delta(q, \sigma)) = \delta'(\iota(q), \sigma)$, and $\iota(F) = F'$, where we naturally extend $\iota$ to sets $S \subseteq Q$. In the following, we will identify an alphabet $\Sigma$ of size $k$ with $[0, k[$ and the power set $2^\Sigma$ with $[0, 2^k[$ using a natural order for the subsets of $\Sigma$, with $0$ corresponding to the emptyset and $j \in [0, 2^k[$ corresponds to a set $J \subseteq [0, k[$ if and only if $i \in J$ then the $i$-th bit of the binary representation of $j$ is 1. If two automata $A$ and $B$ are isomorphic, we write $A \simeq B$. A semiautomaton $(Q, \Sigma, \delta, q_0)$ denotes an NFA without its final state information and is referred to as an NFA$_\emptyset$. Each semiautomaton, if $|Q| = n$, will be shared by $2^n$ NFAs. An NFA is deterministic (DFA) if $|\delta(q, \sigma)| \le 1$, for all $(q, \sigma) \in Q \times \Sigma$ and is complete if the equality always holds. An initially-connected (complete) DFA is denoted by ICDFA and the corresponding semiautomaton by ICDFA$_\emptyset$.

We review here the canonical string representation for nonisomorphic ICD-FAs presented by Almeida et al. [1]. Given an ICDFA $A = \langle Q, [0, k[, \delta, q_0, F \rangle$, a canonical order for $Q$ can be obtained by a breath-first traversal of $A$, starting with the initial state $q_0$, and in each state considering the transitions in the natural order of $\Sigma$. Let $\varphi: Q \to [0, n[$ be the renaming of $Q$ elements induced by that order. If we, by abuse of language, identify $q$ and $\varphi(q)$, for $q \in Q$, a unique representation of the semiautomaton of $A$ is $S(A) = \langle \delta(|i|/k|, i \bmod k)_{i \in [0, kn[} \rangle$.

A canonical representation for $A$ is obtained by adding a sequence $(b_i)_{i \in [0,n[}$ of $n$-bits such that $b_i = 1$ if state $i$ is final, and $b_i = 0$ otherwise.

**Example 1.** Consider the ICDFA, $A = \langle \{A, B, C, D, E\}, \{a, b\}, \delta, E, \{B\} \rangle$, $a < b$, and $\varphi$ the states renaming according to the induced order.

<figure><img src="image_2.png" /></figure>

The canonical string $S(A)$ for the corresponding ICDFA$_\emptyset$ is $\underbrace{12}_{E} \underbrace{31}_{A} \underbrace{32}_{D} \underbrace{40}_{B} \underbrace{14}_{C}$.

Adding the information pertaining to the final states as a sequence of bits, the canonical string for $A$ is 123132401400010.

The characterisation of the canonical strings allows the enumeration, and the exact and random generation, as well as an optimal coding for ICDFAs, as defined by Lothaire [9]. Inspired by this model, we defined FIFA as NFAs having a unique breadth-first traversal order property according to the natural order of $2^\Sigma$.
## 3 Forward Injective Finite Automata

Even if one only considers initially-connected NFAs, with a single initial state, it is not possible to extend the previous representation to NFAs because it is not possible to induce an order for the set of states from the transition function. Starting with the initial state, whenever unvisited states are reached from an already visited state, we must ensure that the set of transition labels from the current state to the newly reached states are pairwise distinct. We denote NFAs with this property as forward injective finite automata (FIFA).

**Definition 1** Let $A = (Q, \Sigma, \delta, q_0, F)$ be an initially-connected NFA and $\Pi$ the bijection from $2^\Sigma$ to $[0, 2^k[, \text{induced by the order on } \Sigma$. Consider the breadth-first search traversal of $A$, induced by the natural order on $2^\Sigma$, that starts in the initial state $q_0$. For each state $s \in Q$, let $S(s)$ be the set of states that are image of a transition starting from a state already visited by the BFS. The labels for the transitions departing from $s$ to any state in $\delta(s) \setminus S(s)$ need to be unique. The automaton $A$ is a forward injective finite automaton if it holds that:

$$ (\forall p, q \in \delta(s) \setminus S(s))(p \neq q \Rightarrow \ell(s, p) \neq \ell(s, q)). \qquad (1) $$

This class of automata is expressive enough to recognise all regular languages, because deterministic automata trivially satisfy (1). However, not all (initially-connected) NFAs are FIFAs. Some experimental results suggest that for alphabets, of size at least 2, one can find a FIFA equivalent to an NFA that is not much larger than the NFA. These facts and the existence of a unique canonical representation show the interest in studying this class of NFAs. In particular, because of the existence of uniform random generators, one can use this model to obtain estimates of average performance of algorithms that manipulate NFAs.

### 3.1 A Canonical State Order for FIFAs

Given a FIFA it is possible to obtain a canonical state order $\varphi$ through a breadth first traversal starting in the initial state and ordering the newly reached states according to the total order defined in $2^\Sigma$. For that, one can disregard the set of final states and consider the semiautomaton $FIFA_\emptyset$. The canonical state order for a $FIFA_\emptyset$ can be computed through Algorithm 1, where $\Pi$ is the bijection from $2^\Sigma$ to $[0, 2^k[, \text{sorted is a function that sorts integers in increasing order, and } \varphi: Q \to [0, n[$ is the computed bijection. Note that (at line 7) $\ell_{\varphi^{-1}(s)}^{-1}(\Pi^{-1}(j))$ is a single value by the injectivity of $\ell$ restricted to the newly seen states in a FIFA.

**Proposition 1** Let $A = (Q, \Sigma, \delta, q_0)$ be a $FIFA_\emptyset$ with $n$ states and $k = |\Sigma|$, there is a bijection $\varphi: Q \to [0, n[$ that defines an isomorphism between $A$ and $\langle[0, n[, \Sigma, \delta', 0\rangle$ with $\delta'(i, \sigma) = \{ \varphi(s) \mid s \in \delta(\varphi^{-1}(i), \sigma) \}$, for $i \in [0, n[$ and $\sigma \in \Sigma$.

Throughout the paper we will now consider $FIFA_\emptyset$ to have its states in their canonical order: $A = \langle[0, n[, \Sigma, \delta, 0\rangle$.
<caption>**Algorithm 1** FIFA state order algorithm</caption>

1: **procedure** STATEORDER(FIFA<sub>0</sub> ⟨Q, Σ, δ, q<sub>0</sub>⟩)
2: &nbsp;&nbsp;&nbsp;&nbsp;φ(q<sub>0</sub>) ← 0
3: &nbsp;&nbsp;&nbsp;&nbsp;i ← 0; s ← 0
4: &nbsp;&nbsp;&nbsp;&nbsp;**do**
5: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;M ← sorted{ Π(S) | ∅ ≠ S = ℓ(φ<sup>-1</sup>(s), q) ∧ q ∈ Q \ φ<sup>-1</sup>([0, i]) }
6: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;**for** j ∈ M **do**
7: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;φ(ℓ<sup>-1</sup><sub>φ<sup>-1</sup>(s)</sub>(Π<sup>-1</sup>(j)) ← i + 1
8: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;**if** i ← i + 1
9: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;s ← s + 1
10: &nbsp;&nbsp;&nbsp;&nbsp;**while** s < i
11: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;**return** φ

**3.2 Canonical String Representation**

Let $A = \langle [0, n[, \Sigma, \delta, 0, F \rangle$ be a FIFA such that $\langle [0, n[, \Sigma, \delta, 0 \rangle$ is a FIFA<sub>0</sub>. We can represent $A$ by the canonical representation of its FIFA<sub>0</sub> concatenated with the bitmap of the state finalities. The canonical representation of a FIFA<sub>0</sub> is defined as follows.

**Definition 2** Given a FIFA<sub>0</sub> ⟨[0, n[, Σ, δ, 0⟩ with |Σ| = k, its canonical representation is a sequence (r<sub>i</sub>)<sub>i∈[0,n[</sub> such that for each state i,

$$
r_i = s_{i,1}s_{i,2}\dots s_{i,m_i}u_{i,1}\dots u_{i,\overline{m_i}},
$$

and where $m_i$ is the number of previously seen states, $\overline{m_i}$ is the number of newly seen states, $s_{i,j} = \Pi(\ell(i, j-1))$ for $j \in [1, m_i]$, and $u_{i,j} = \Pi(\ell(i, m_i + j - 1))$ for $j \in [1, \overline{m_i}]$. This means that, for each state $i$, $s_{i,j}$ correspond to the sets of transitions to states already seen (back transitions) and $u_{i,j}$ correspond to the sets of transitions to newly seen states from state $i$ (forward transitions).

*Example 2.* Consider the following FIFA on the left.

<figure><img src="image_3.png" /></figure>

Let Π(∅) = 0, Π({a}) = 1, Π({b}) = 2 and Π({a, b}) = 3. The state renaming according to the induced order on the states is given above. The canonical string (r<sub>i</sub>)<sub>i∈[0,5]</sub> for the corresponding FIFA<sub>0</sub> is

<figure><img src="image_4.png" /></figure>

where the transitions for each state are as indicated. The FIFA can be represented by its semiautomaton canonical string with the state finalities appended. Thus, this FIFA canonical string is

[1][1, 2][2, 2, 0][2][0, 0, 2, 1][0, 0, 0, 0][1, 3][0, 1, 0, 0, 2, 0][0, 0, 0, 0, 2, 0][0, 0, 0, 1, 0, 0].
**Lemma 3.** Let $A = \langle[0, n[, \Sigma, \delta, 0) \rangle$ be a $FIFA_\emptyset$ with $k = |\Sigma|$. Let $(r_i)_{i \in [0, n]}$ with $r_i = s_{i,1}s_{i,2} \cdots s_{i,m_i}u_{i,1}u_{i,2} \cdots u_{i,\overline{m_i}}$ be the canonical representation for $A$ as given above. Then the following rules are satisfied:

$$s_{i,j} \in [0, 2^k[, \qquad \forall i \in [0, n[, \forall j \in [1, m_i], \qquad (F1)$$

$$u_{i,j} \in [1, 2^k[, \qquad \forall i \in [0, n[, \forall j \in [1, \overline{m_i}], \qquad (F2)$$

$$j < l \Rightarrow u_{i,j} < u_{i,l}, \qquad \forall i \in [0, n[, \forall j, l \in [1, \overline{m_i}], \qquad (F3)$$

$$m_0 = 1, \qquad (F4)$$

$$m_i = m_{i-1} + \overline{m}_{i-1}, \qquad \forall i \in [1, n[, \qquad (F5)$$

$$i < m_i \le n, \qquad \forall i \in [1, n[, \qquad (F6)$$

$$\overline{m}_{n-1} = 0. \qquad (F7)$$

*Proof.* The rule (F1) describes how transitions to previously seen states are represented, i.e. $s_{i,j} = \Pi(\ell(i, j - 1))$, possibly with $s_{i,j} = 0$. The rule (F2) considers the transitions from state $i$ to states $t_{i,j} = m_i + j - 1$ visited for the first time in $i$, which implies that $\ell(i, t_{i,j}) \neq \emptyset$. Consequently, $u_{i,j} \in [1, 2^k[$. For representation purposes, rule (F3) states that the set of states visited for the first time is represented with its transitions sorted in ascending order. This is a representation choice that ensures that all $u_{i,j}$ are distinct. Rule (F4) is obvious as one starts at state 0 and thus 0 is the only seen state. Rule (F5) is a direct consequence of the definition of $m_i$ in (2), and implies that $m_i = 1 + \sum_{j=0}^{i-1} \overline{m}_j$ for $i \in [1, n[$. Rules (F6) and (F7) ensures that all states are seen, and that the $FIFA_\emptyset$ is initially connected. It is a consequence of the definition of $\varphi$ and also ensures that a state must be seen before its representation is given. Rule (F6) implies that $m_{n-1} = n$. By contradiction, suppose that there is $i$ such that $m_i \le i$, for $i \in [1, n[$. Then, there exist $1 \le j \le i$ such that $j$ is not accessible from 0 in paths that use states only in $[0, i[$. But that contradicts $\varphi$ definition. $\square$

**Lemma 4.** Every string $(r_i)_{i \in [0, n]}$ satisfying rules (F1)-(F7) represents a $FIFA_\emptyset$ with states $[0, n[$ over an alphabet of $k$ symbols.

*Proof.* There is at least one transition reaching each state in $[1, n[$ and there is at least one transition from the state 0 to state 1. The transition function is defined by $\ell(i, j - 1) = \Pi^{-1}(s_{i,j})$ for $i \in [0, n[$ and $j \in [1, m_i]$, and $\ell(i, m_i + j - 1) = \Pi^{-1}(u_{i,j})$ for $i \in [0, n[$ and $j \in [1, \overline{m_i}]$. The proof that the $FIFA_\emptyset$ is initially connected is analogous to the one in Lemma 3. $\square$

From these lemmas the following theorem holds.

**Theorem 5.** For each $n > 0$ and $k > 0$, there is a one-to-one mapping from sequences $(r_i)_{i \in [0, n]}$ satisfying rules (F1)-(F7) and nonisomorphic $FIFA_\emptyset$s with $n$ states over an alphabet of $k$ symbols.

The canonical form for $FIFA_\emptyset$ is not a simple extension of the one for $ICDFA_\emptyset$s reviewed in Section 2 for several reasons. One needs to consider instead of the
alphabet its power set, there are no restrictions for transitions to already seen states, and transitions to newly seen states must have different labels. However, the first occurrences of each state satisfy exactly the same rules (over an alphabet of $2^k$ symbols) observed in the canonical representation of ICDFA$_{\emptyset}$s. This will be made evident in the next section.

## 3.3 Counting FIFAs

Our aim is to enumerate (exactly generate) and count all the nonisomorphic FIFA$_{\emptyset}$ with $n$ states and $k$ symbols. This will also allow us to obtain a uniform random generator for the class of FIFAs. Let $\Psi: [0, n] \times [1, 2^k] \rightarrow [0, n(2^k - 1)]$, be defined by $\Psi(i, j) = i(2^k - 1) + j - 1$. The mapping $\Psi$ is a bijection with $\Psi^{-1}(p) = (\lfloor p/(2^k - 1) \rfloor, (p \bmod (2^k - 1)) + 1)$. Let $(r_i)_{i \in [0, n]}$ be a sequence satisfying rules (F1)–(F7), thus, representing a FIFA$_{\emptyset}$. Let us denote by flag of a state $t \in [1, n]$ the pair $(i, u_{i,j})$, occurring in state $i$, such that $t = m_i + j - 1$ (and $\ell(i, t) = \Pi^{-1}(u_{i,j})$). According to (F3), if in a state $i$ two different flags $(i, u_{i,j})$ and $(i, u_{i,l})$ occur, we know that $j < l \Rightarrow u_{i,j} < u_{i,l}$. For the sake of readability, given $t \in [1, n]$, we denote by $(i_t, u_t)$ its flag, and let $\Psi(i_t, u_t) = f_t$. Then, by (F3), one has

$$ (\forall t \in [2, n]) (i_t = i_{t-1} \Rightarrow u_t > u_{t-1}) \lor (i_t > i_{t-1}), $$

which implies

$$ (\forall t \in [2, n]) (f_t > f_{t-1}), \qquad (G1) $$

$$ (\forall t \in [1, n]) (f_t < t(2^k - 1)). \qquad (G2) $$

Rules (G1)–(G2) are satisfied by the positions of the first occurrence of a state in the canonical strings for ICDFA$_{\emptyset}$s, considering $k$ instead of $2^k - 1$ in rule (G2). The following theorem computes the number of allowed sequences of flags.

**Proposition 2 (Theorem 6 of [1])** Given $k > 0$ and $n > 0$, the number of sequences $(f_t)_{t \in [1, n]}$, $F_{2^k-1,n}$, is given by:

$$ F_{2^k-1,n} = \sum_{f_1=0}^{2^k-1-1} \sum_{f_2=f_1+1}^{2(2^k-1)-1} \dots \sum_{f_{n-1}=f_{n-2}+1}^{(2^k-1)(n-1)-1} 1 = C_n^{(2^k-1)}, $$

where $C_n^{(2^k-1)} = \binom{n(2^k-1)}{n} \frac{1}{(2^k-2)n+1}$ are the generalized Fuss-Catalan numbers.

**Example 6.** For the FIFA of Example 2, ((0, 1), (0, 2), (1, 2), (3, 1), (3, 3)) is the sequence of flags and $(f_t)_{t \in [1,5]} = (0, 1, 4, 9, 11)$.

Given a sequence of flags $(f_t)_{t \in [1, n]}$, the set of possible canonical strings that represent FIFA$_{\emptyset}$s can be easily enumerated: each state $i$ has unconstrained transitions for states already seen $(m_i)$ and has the transitions to new states given by the flags occurring in its description (forward transitions).
The number of canonical strings with a given sequence of flags is given by

$$ \prod_{i \in [0,n]} (2^k)^{m_i}. \qquad (2) $$

**Theorem 7.** The total number of $FIFA_\emptyset$s with $n$ states over a $k$-ary alphabet is

$$ b_{k,n} = \sum_{f_1=0}^{2^k-1} \sum_{f_2=f_1+1}^{2(2^k-1)-1} \dots \sum_{f_{n-1}=f_{n-2}+1}^{(2^k-1)(n-1)-1} \prod_{i \in [0,n]} (2^k)^{m_i}, $$

where $m_i = 1 + \sum_{j=0}^{i-1} \bar{m}_j$ and $\bar{m}_j = |\{f_t | i_t = j\}|$ for $i \in [0, n]$ and $j \in [1, n]$.

This can be adapted for the exact generation/enumeration of all canonical representations. Each $FIFA_\emptyset$ corresponds to a number between 1 and $b_{k,n}$. In Table 1 we present the values of $b_{k,n}$ for $n \in [2, 7]$ and $k \in [2, 3]$. An equivalent recursive definition for $b_{k,n}$ is given in the next section for uniform random generation.

<table><caption>**Table 1.** Values of $b_{k,n}$</caption><thead><tr><th>n | k = 2</th><th>k = 3</th></tr></thead><tbody><tr><td>2 | 192</td><td>3584</td></tr><tr><td>3 | 86016</td><td>56885248</td></tr><tr><td>4 | 321912832</td><td>32236950781952</td></tr><tr><td>5 | 10382009696256</td><td>738091318939425439744</td></tr><tr><td>6 | 3073719939819896832</td><td>733871593861464877408622477312</td></tr><tr><td>7 | 8715818304405159932854272</td><td>32686722749179979231494144786993701191680</td></tr></tbody></table>

**Corollary 8.** The number of nonisomorphic FIFAs with $n$ states and $k$ alphabetic symbols is $B_{k,n} = b_{k,n}2^n$.

# 4 Uniform Random Generation

The canonical representation for FIFAs allows an easy uniform random generation for this class of automata. Given the number of flags occurring in a prefix of a canonical string we count the number of valid suffixes. To count the number of automata with a given prefix a recursive counting formula for $FIFA_\emptyset$ is needed. With these partial values, we can reconstruct any $FIFA_\emptyset$ by knowing its number, which varies from 1 to $b_{k,n}$. The process of uniform randomly generating a FIFA consists, thus, in four steps: creation of a table with partial counts for each prefix; uniformly sample a number between 1 and $b_{k,n}$; construct the $FIFA_\emptyset$ representation using the table; random generation of values from 0 to $2^n - 1$ for the state finalities and return the FIFA.

Let $m$ be the number of already seen states for the state $i$ of a canonical string of a $FIFA_\emptyset$. We count the number $N_{m,i}$ of $FIFA_\emptyset$s for each value of $m$ and $i$. This gives us the following recursive formula for fixed $n$ and $k$:

$$ N_{m,i} = (2^k)^m \sum_{j=0}^{n-m} \binom{2^k-1}{j} N_{m+j,i+1}, \quad m \in [1, n], i \in [0, m], $$

$$ N_{m,i} = 0, \quad m \in [1, n], i \notin [0, m], $$

$$ N_{n,n} = 1. $$
**Proposition 3** $b_{k,n} = N_{1,0}$, for all $k \ge 1$ and $n \ge 1$.

*Proof.* Immediate consequence of the canonical representation and Theorem 7. □

<caption>**Algorithm 2 Random FIFA<sub>θ</sub> algorithm.**</caption>

1: **procedure** RANDOMFIFA(n, k)
2:   $r \leftarrow Random(0, N_{1,0} - 1)$ ▷ number of the FIFA<sub>θ</sub>
3:   $m_0 \leftarrow 1$
4:   **for** $q \in [0, n[$ **do**
5:     $\overline{m}_q \leftarrow 0$
6:     **while** $N_{m_q+\overline{m}_q,q} \le r$ **do**
7:       $\overline{m}_q \leftarrow \overline{m}_q + 1$
8:       $m_{q+1} \leftarrow m_q + \overline{m}_q$
9:       $b \leftarrow r \bmod (2^k) \sum_{i=0}^{n-1} m_i$ ▷ number representing back transitions
10:      $f \leftarrow r/(2^k) \sum_{i=0}^{n-1} m_i$ ▷ number representing forward transitions
11:     **for** $q \in [0, n[$ **do**
12:       **for** $p \in [1, m_q]$ **do**
13:         $s_{q,p} \leftarrow b \bmod 2^k$
14:         $b \leftarrow b/2^k$
15:         **if** $\overline{m}_q \ne 0$ **then**
16:           $c \leftarrow \binom{2^k-1}{\overline{m}_q}$
17:           $t \leftarrow f \bmod c$
18:           $f \leftarrow f/c$
19:         **for** $p \in [1, \overline{m}_q]$ **do**
20:           $u_{q,p} \leftarrow t \bmod (2^k - 1)$
21:           $t \leftarrow \lfloor t/(2^k - 1) \rfloor$
22:         **return** $(s_{i,1}, s_{i,2}, \dots, s_{i,m_i}, u_{i,1}, u_{i,2}, \dots, u_{i,m_i})_{i \in [0,n]}$ ▷ reconstruct back transitions

**Proposition 4** *Algorithm 2 presents a uniform random generator for a FIFA<sub>θ</sub> with n states and k symbols.*

*Proof.* (Sketch) Given an arbitrary integer $r$ representing a FIFA<sub>θ</sub> we determine the values $\overline{m}_i$ for each $i \in [0, n[$, using the precalculated table $N_{m,i}$. For each state $i$ and $m$ previously seen states, $\overline{m}_i$ is the first value such that $N_{m+\overline{m}_i,i} > r$, that is, $N_{m+\overline{m}_{i-1},i} \le r < N_{m+\overline{m}_i,i}$ (lines 4–8). Then, we count the number of available back transitions in each state $i$ using $m_i$ which can be determined by $\overline{m}_i$ (by (F5)). The total number of possible back transitions is $\sum_{i=0}^{n-1} m_i$. With this number (lines 9 and 10) one obtains the integers that represent all the $s_{i,j}$ and the $u_{i,j}$, respectively. For a given $i \in [0, n[$, in lines 12–14 we calculate $s_{i,j}$ for $j \in [1, m_i]$. If state $i$ has forward transitions, their values are computed in lines 16–21. □
To obtain a random FIFA from the $FIFA_\emptyset$ we can generate a random number from $[0, 2^n[$ and reconstruct the state finalities according to the corresponding choice. Using dynamic programming techniques it is possible to generate a table indexed by the values of $m$ and $i$ ($N_{m,i}$) with time complexity $O(n^3 \log((2^k)^{n^2})) = O(n^5 k)$. The amount of memory used is $O(n^4 k)$, and this is a limiting factor for the dimension of the $FIFA_\emptyset$ being generated. This is justified by the huge number of $FIFA_\emptyset$s for a given $n$ and $k$. For example, $b_{2,100}$ is greater than $10^{11531}$. In Table 2 we present the execution times for the generation of 10000 FIFAs for $n \in \{1, 20, 30, 50, 75, 100\}$ and $k \in \{1, 2, 3, 4\}$, using Python 2.7 interpreted by Pypy, with an Intel Xeon CPU X5550 at 2.67GHz. Comparing with some experiments presented by Héam and Joly [6, Table 1], these times correspond, approximately, to the generation of a single NFA.

<table><caption>**Table 2.** Execution times for the generation of 10000 random FIFA.</caption><thead><tr><th>Times</th><th>k = 1</th><th>k = 2</th><th>k = 3</th><th>k = 4</th></tr></thead><tbody><tr><td>n = 10</td><td>0.77s</td><td>1.05s</td><td>0.95s</td><td>8.59s</td></tr><tr><td>n = 20</td><td>1.06s</td><td>2.33s</td><td>3.13s</td><td>3.96s</td></tr><tr><td>n = 30</td><td>1.15s</td><td>5.01s</td><td>7.38s</td><td>9.52s</td></tr><tr><td>n = 50</td><td>2.84s</td><td>16.86s</td><td>26.64s</td><td>40.43s</td></tr><tr><td>n = 75</td><td>7.11s</td><td>47.62s</td><td>71.92s</td><td>91.70s</td></tr><tr><td>n = 100</td><td>15.86s</td><td>100.25s</td><td>156.24s</td><td>202.41s</td></tr></tbody></table>

# 5 Converting an NFA into a FIFA

In this section we discuss a process of converting an arbitrary NFA to a FIFA and the asymptotic time complexity bounds of such a procedure. The algorithm has an NFA as input and outputs an equivalent FIFA. It is based on the subset construction for NFA determinisation, with addition of an heuristic that attempts to create back transitions whenever possible. This gives us a FIFA that is not only forward injective but also forward deterministic. It may be also possible to add an heuristic for nondeterministic forward injective transitions or to have other procedures that are not based on the subset construction. However this one had a good performance in our experiments.

**Proposition 5** *The procedure NFATOFIFA in Algorithm 3 computes a FIFA equivalent to a given NFA.*

This algorithm has time complexity $O(|\Sigma|2^{|Q|})$, which is justified by $|Q'|$ having space complexity $O(2^{|Q|})$ due to the determinisation based algorithm. It is an open problem whether these bounds are tight for this algorithm.

# 6 Experimental Results

The algorithm defined in the previous section was implemented within the FAdo package [5]. We performed some experiments to compare the sizes of the input
<caption>**Algorithm 3** An NFA to FIFA algorithm</caption>

1: **procedure** NFAToFIFA(NFA $\langle Q, \Sigma, \delta, I, F \rangle$)
2:     $Q' \leftarrow \{I\}$
3:     $w \leftarrow \{I\}$
4:     ▷ states to be processed
5:     **while** $w \neq \emptyset$ **do**
6:         $S \leftarrow \text{POP}(w)$
7:         ▷ popping $S$ from $w$
8:         **for** $\sigma \in \Sigma$ **do**
9:             $P \leftarrow \emptyset$
10:             $\delta'(S, \sigma) \leftarrow \emptyset$
11:             **for** $s \in S$ **do**
12:                 $P \leftarrow P \cup \delta(s, \sigma)$
13:                 **for** $R \in \text{SORTEDBYSIZEDESC}(Q')$ **do**
14:                     **if** $R \subseteq P$ **then**
15:                         $\delta'(S, \sigma) \leftarrow \delta'(S, \sigma) \cup \{R\}$
16:                         $P \leftarrow P \setminus R$
17:                     **if** $P \neq \emptyset$ **then**
18:                         $Q' \leftarrow Q' \cup \{P\}$
19:                         $w \leftarrow w \cup \{P\}$
20:                         $\delta'(S, \sigma) \leftarrow \delta'(S, \sigma) \cup \{P\}$
21:             **return** FIFA $\langle Q', \Sigma, \delta', I, \{S \cap F \neq \emptyset, S \in Q'\} \rangle$

and output automata. The input NFAs were obtained from uniform random generated regular expressions, for a fixed (standard) grammar, of a given syntactic tree size *m* over an alphabet of *k* symbols. The conversion method used was the partial derivative automata [2]. For each *m* and *k*, 10000 random regular expressions were generated to ensure a 95% confidence level within a 1% error margin [7, pp. 38–41]. For each sample, we calculated the minimal, the average and the maximum sizes of the obtained automata. For each partial derivative automaton (PD) we applied the algorithm NFAToFIFA and obtained a FIFA (FIFA). We also computed the DFA obtained by determinisation of PD, by the usual subset construction, (DT), and the minimal DFA (MIN). Results for $m \in \{50, 100, 250, 500\}$ and $k \in \{2, 3, 10\}$ are presented in Table 3. In general the FIFA computed is not much larger than the PD, although the determinised automata can be significantly larger.

# 7 Conclusions

We presented a class of initially-connected NFAs with a single initial state for which it is possible to test isomorphism in polynomial time. The definition of these automata (FIFA) is based on the possibility to order the set of states in a breath-first search traversal from the initial state. The uniform random generator can efficiently sample datasets to test the performance of algorithms dealing with NFAs. Moreover one can extend it with a parameter for the density of transitions in order to avoid the high frequency of NFAs recognising the universal language. The class of FIFAs can be further studied in its own. One can obtain asymptotic
<table><caption>**Table 3.** State complexities of automata where *m*, syntactic size of RE; *k*, size of alphabet; PD, size of partial derivative NFA; DT size of DFA from PD; MIN, size of minimal DFA; FIFA, size of FIFA from PD.</caption><thead><tr><th>m, k</th><th>Type</th><th>min</th><th>avg</th><th>max</th></tr></thead><tbody><tr><td rowspan="5">50, 2</td><td>FIFA</td><td>3</td><td>10.1684</td><td>25</td></tr><tr><td>DT</td><td>3</td><td>10.1338</td><td>62</td></tr><tr><td>MIN</td><td>1</td><td>5.0762</td><td>51</td></tr><tr><td>PD</td><td>3</td><td>10.6904</td><td>19</td></tr><tr><td>100, 2</td><td>FIFA</td><td>3</td><td>19.2113</td><td>46</td></tr><tr><td rowspan="5">250, 2</td><td>DT</td><td>3</td><td>19.7193</td><td>158</td></tr><tr><td>MIN</td><td>1</td><td>6.2814</td><td>116</td></tr><tr><td>PD</td><td>9</td><td>20.0239</td><td>30</td></tr><tr><td>FIFA</td><td>9</td><td>48.3731</td><td>107</td></tr><tr><td>DT</td><td>12</td><td>185.6424</td><td>1120</td></tr><tr><td rowspan="5">500, 2</td><td>MIN</td><td>1</td><td>7.0256</td><td>630</td></tr><tr><td>PD</td><td>34</td><td>47.998</td><td>66</td></tr><tr><td>FIFA</td><td>35</td><td>99.8889</td><td>189</td></tr><tr><td>DT</td><td>13</td><td>186.1518</td><td>6451</td></tr><tr><td>MIN</td><td>1</td><td>6.8369</td><td>745</td></tr><tr><td rowspan="5">9</td><td>PD</td><td>72</td><td>94.6422</td><td>124</td></tr></tbody></table><table><thead><tr><th>m, k</th><th>Type</th><th>min</th><th>avg</th><th>max</th></tr></thead><tbody><tr><td rowspan="5">50, 3</td><td>FIFA</td><td>3</td><td>12.4948</td><td>25</td></tr><tr><td>DT</td><td>3</td><td>13.9339</td><td>56</td></tr><tr><td>MIN</td><td>1</td><td>9.1225</td><td>41</td></tr><tr><td>PD</td><td>4</td><td>11.6522</td><td>19</td></tr><tr><td>100, 3</td><td>FIFA</td><td>5</td><td>24.3173</td><td>45</td></tr><tr><td rowspan="5">250, 3</td><td>DT</td><td>5</td><td>34.289</td><td>166</td></tr><tr><td>MIN</td><td>1</td><td>18.2094</td><td>148</td></tr><tr><td>PD</td><td>11</td><td>21.8518</td><td>32</td></tr><tr><td>FIFA</td><td>23</td><td>60.7792</td><td>110</td></tr><tr><td>DT</td><td>12</td><td>185.6424</td><td>1586</td></tr><tr><td rowspan="5">500, 3</td><td>MIN</td><td>1</td><td>59.0185</td><td>988</td></tr><tr><td>PD</td><td>34</td><td>52.5888</td><td>70</td></tr><tr><td>FIFA</td><td>42</td><td>122.2247</td><td>198</td></tr><tr><td>DT</td><td>37</td><td>1143.2134</td><td>11687</td></tr><tr><td>MIN</td><td>1</td><td>92.8985</td><td>2343</td></tr><tr><td rowspan="5">9</td><td>PD</td><td>79</td><td>103.6871</td><td>126</td></tr></tbody></table><table><thead><tr><th>m, k</th><th>Type</th><th>min</th><th>avg</th><th>max</th></tr></thead><tbody><tr><td rowspan="5">50, 10</td><td>FIFA</td><td>6</td><td>14.1252</td><td>24</td></tr><tr><td>DT</td><td>7</td><td>15.3764</td><td>33</td></tr><tr><td>MIN</td><td>1</td><td>13.8138</td><td>30</td></tr><tr><td>PD</td><td>6</td><td>13.3556</td><td>21</td></tr><tr><td>100, 10</td><td>FIFA</td><td>14</td><td>27.4189</td><td>43</td></tr><tr><td rowspan="5">250, 10</td><td>DT</td><td>15</td><td>32.5608</td><td>66</td></tr><tr><td>MIN</td><td>1</td><td>28.7841</td><td>58</td></tr><tr><td>PD</td><td>13</td><td>25.229</td><td>36</td></tr><tr><td>FIFA</td><td>44</td><td>67.8454</td><td>99</td></tr><tr><td>DT</td><td>55</td><td>102.6683</td><td>329</td></tr><tr><td rowspan="5">500, 10</td><td>MIN</td><td>1</td><td>88.8932</td><td>253</td></tr><tr><td>PD</td><td>41</td><td>60.8428</td><td>78</td></tr><tr><td>FIFA</td><td>102</td><td>135.1439</td><td>170</td></tr><tr><td>DT</td><td>128</td><td>277.5053</td><td>1122</td></tr><tr><td>MIN</td><td>1</td><td>237.4012</td><td>869</td></tr><tr><td rowspan="5">9</td><td>PD</td><td>94</td><td>120.0465</td><td>150</td></tr></tbody></table>

bounds for the number of FIFAs of given *n* and *k*. Upper bounds for the size of the minimal FIFAs for a given language will be of major interest. Operational state complexity with respect to this class of automata is also an open problem.

## References

1. Almeida, M., Moreira, N., Reis, R.: Enumeration and generation with a string automata representation. Theoret. Comput. Sci. 387(2), 93–102 (2007)

2. Antimirov, V.M.: Partial derivatives of regular expressions and finite automaton constructions. Theoret. Comput. Sci. 155(2), 291–319 (1996)

3. Bassino, F., Nicaud, C.: Enumeration and random generation of accessible automata. Theoret. Comput. Sci. 381(1-3), 86–104 (2007)

4. Champarnaud, J.M., Paranthoën, T.: Random generation of DFAs. Theoret. Comput. Sci. 330(2), 221–235 (2005)

5. FAdo, P.: FAdo: tools for formal languages manipulation. http://fado.dcc.up.pt (Access date:132018)

6. Héam, P., Joly, J.: On the uniform random generation of non deterministic automata up to isomorphism. In: Drewes, F. (ed.) Proc. 20th CIAA. LNCS, vol. 9223, pp. 140–152. Springer (2015)

7. Knuth, D.E.: The Art of Computer Programming. Seminumerical Algorithms., vol. 2. 2nd edn. (1981)

8. Levin, D.A., Peres, Y., Wilmer, E.L.: Markov Chain and Mixing Times. American Mathematical Society (2008), http://pages.uoregon.edu/dlevin/MARKOV/markovmixing.pdf

9. Lothaire, M.: Applied Combinatorics on Words. No. 105 in Encyclopedia of Mathematics and its Applications (2005)

10. Nicaud, C.: Random deterministic automata. In: Csuhaj-Varjú, E., Dietzfelbinger, M., Ésik, Z. (eds.) MFCS. LNCS, vol. 8634, pp. 5–23. Springer (2014)