Module: Finite Automata (fa)¶
Finite automata manipulation.
Deterministic and non-deterministic automata manipulation, conversion and evaluation. .. Authors: Rogério Reis & Nelma Moreira .. This is part of FAdo project https://fado.dcc.fc.up.pt.
Classes¶
FA¶
- class FA[source]¶
- Base class for Finite Automata.
This is just an abstract class. Not to be used directly!!
- Variables
- addFinal(stateindex)[source]¶
A new state is added to the already defined set of final states.
- Parameters
stateindex (int) – index of the new final state.
- addSigma(sym)[source]¶
Adds a new symbol to the alphabet.
- Parameters
sym (str) – symbol to be added
- Raises
DFAepsilonRedefinition – if sym is Epsilon
Note
There is no problem with duplicate symbols because sigma is a Set.
No symbol Epsilon can be added.
- addState(name=None) int[source]¶
Adds a new state to an FA. If no name is given a new name is created.
- Parameters
name (
Object, optional) – Name of the state to be added.- Returns
Current number of states (the new state index).
- Return type
- Raises
DuplicateName – if a state with that name already exists
- countTransitions()[source]¶
Evaluates the size of FA transitionwise
- Returns
the number of transitions
- Return type
Changed in version 1.0.
- delFinal(st)[source]¶
Deletes a state from the final states list
- Parameters
st (int) – state to be marked as not final.
- deleteState(sti: int)[source]¶
Remove the given state and the transitions related with that state.
- Parameters
sti (int) – index of the state to be removed
- Raises
DFAstateUnknown – if state index does not exist
- dotFormat(size='20,20', filename=None, direction='LR', strict=False, maxlblsz=6, sep='\n') str[source]¶
A dot representation
- Parameters
- Returns
the dot representation
- Return type
New in version 0.9.6.
Changed in version 1.2.1.
- eliminateDeadName()[source]¶
Eliminates dead state name (common.DeadName) renaming the state
- Returns
self
- Return type
Attention
works inplace
New in version 1.2.
- initialP(state: int) bool[source]¶
Tests if a state is initial
- Parameters
state – state index
- Returns
is the state initial?
- Return type
- inputS(i)[source]¶
Input labels coming out of state i
- Parameters
i (int) – state
- Returns
set of input labels
- Return type
set of str
New in version 1.0.
- noBlankNames()[source]¶
Eliminates blank names
- Returns
self
- Return type
Attention
in place transformation
- renameState(st, name)[source]¶
Rename a given state.
Note
Deals gracefully both with int and str names in the case of name collision.
Attention
the object is modified in place
- renameStates(name_list=None)[source]¶
Renames all states using a new list of names.
- Parameters
name_list (list) – list of new names.
- Returns
self.
- Return type
- Raises
DFAerror – if provided list is too short.
Note
If no list of names is given, state indexes are used.
Attention
the object is modified in place
- reversal()[source]¶
Returns a NFA that recognizes the reversal of the language
- Returns
NFA recognizing reversal language
- Return type
- setFinal(statelist)[source]¶
Sets the final states of the FA
Caution
Erases any previous definition of the final state set.
- setInitial(stateindex)[source]¶
Sets the initial state of a FA
- Parameters
stateindex (int) – index of the initial state.
- stateIndex(name, auto_create=False)[source]¶
Index of given state name.
- Parameters
- Returns
state index
- Return type
- Raises
DFAstateUnknown – if the state name is unknown and autoCreate==False
Note
Replaces stateName
Note
If the state name is not known and flag is set creates it on the fly
New in version 1.0.
- stateName(name, auto_create=False)[source]¶
Index of given state name.
- Parameters
- Returns
state index
- Return type
- Raises
DFAstateUnknown – if the state name is unknown and autoCreate==False
Deprecated since version 1.0: Use:
stateIndex()insteadDeprecated since version 1.0: Use the stateIndex() function instead
- class SemiDFA[source]¶
Class of automata without initial or final states
- Variables
- static dotDrawTransition(st1: str, lbl1: str, st2, sep='\n') str[source]¶
Draw a transition in dot format
OFA¶
- class OFA[source]¶
Base class for one-way automata
- Variables

- acyclicP(strict=True)[source]¶
Checks if the FA is acyclic
- Parameters
strict (bool) – if not True loops are allowed
- Returns: True if the FA is acyclic
bool: True if the FA is acyclic
- abstract deleteStates(del_states)[source]¶
To be implemented below
- Parameters
del_states (list) – states to be deleted
- dump()[source]¶
Returns a python representation of the object
- Returns
the python representation (Tags,States,sigma,delta,Initial,Final)
- Return type
- eliminateStout(st)[source]¶
Eliminate all transitions outgoing from a given state
- Parameters
st (int) – the state index to loose all outgoing transitions
Attention
performs in place alteration of the automata
New in version 0.9.6.
- minimalBrzozowski()[source]¶
Constructs the equivalent minimal DFA using Brzozowski’s algorithm
- Returns
equivalent minimal DFA
- Return type
- topoSort()[source]¶
Topological order for the FA
- Returns
List of state indexes
- Return type
Note
self loops are taken in consideration
DFA¶
- class DFA[source]¶
Class for Deterministic Finite Automata.
- Variables

- HKeqP(other, strict=True)[source]¶
Tests the DFA’s equivalence using Hopcroft and Karp’s state equivalence algorithm
- Parameters
other –
strict –
- Returns
bool
See also
J. E. Hopcroft and r. M. Karp.A Linear Algorithm for Testing Equivalence of Finite Automata.TR 71–114. U. California. 1971
Attention
The automaton must be complete.
- MyhillNerodePartition()[source]¶
Myhill-Nerode partition, Moore’s way
New in version 1.3.5.
Attention
No state should be named with DeadName. This states is removed from the obtained partition.
See also
F.Bassino, J.David and C.Nicaud, On the Average Complexity of Moores’s State Minimization Algorihm, Symposium on Theoretical Aspects of Computer Science
- aEquiv()[source]¶
Computes almost equivalence, used by hyperMinimial
- Returns
partition of states
- Return type
Note
may be optimized to avoid dupped
- addTransition(sti1, sym, sti2)[source]¶
Adds a new transition from
sti1tosti2consuming symbolsym.
- complete(dead='DeaD')[source]¶
Transforms the automata into a complete one. If sigma is empty nothing is done.
Note
Adds a dead state (if necessary) so that any word can be processed with the automata. The new state is named
dead, so this name should never be used for other purposes.Attention
The object is modified in place.
Changed in version 1.0.
- completeMinimal()[source]¶
Completes a DFA assuming it is a minimal and avoiding de destruction of its minimality If the automaton is not complete, all the non final states are checked to see if tey are not already a dead state. Only in the negative case a new (dead) state is added to the automaton.
- Return type
Attention
The object is modified in place. If the alphabet is empty nothing is done
- computeKernel()[source]¶
The Kernel of a ICDFA is the set of states that accept a non finite language.
- Returns
triple (comp, center , mark) where comp are the strongly connected components, center the set of center states and mark the kernel states
- Return type
- concat(fa2, strict=False)[source]¶
Concatenation of two DFAs. If DFAs are not complete, they are completed.
- concatI(fa2, strict=False)[source]¶
Concatenation of two DFAs.
- Parameters
- Returns
the result of the concatenation
- Return type
- Raises
DFAdifferentSigma – if alphabet are not equal
New in version 0.9.5.
Note
this is to be used with non complete DFAs
- delTransition(sti1, sym, sti2, _no_check=False)[source]¶
Remove a transition if existing and perform cleanup on the transition function’s internal data structure.
- Parameters
Note
Unused alphabet symbols will be discarded from sigma.
- deleteStates(del_states)[source]¶
Delete given iterable collection of states from the automaton.
- Parameters
del_states – collection of int representing states
Note
in-place action
Note
delta function will always be rebuilt, regardless of whether the states list to remove is a suffix, or a sublist, of the automaton’s states list.
- dist()[source]¶
Evaluate the distinguishability language for a DFA
- Return type
See also
Cezar Câmpeanu, Nelma Moreira, Rogério Reis: The distinguishability operation on regular languages. NCMA 2014: 85-100
New in version 0.9.8.
- distMin()[source]¶
Evaluates the list of minimal words that distinguish each pair of states
- Returns
set of minimal distinguishing words
- Return type
New in version 0.9.8.
Attention
If the DFA is not minimal, the method loops forever
- distR()[source]¶
Evaluate the right distinguishability language for a DFA
- Return type
- ..seealso:: Cezar Câmpeanu, Nelma Moreira, Rogério Reis:
The distinguishability operation on regular languages. NCMA 2014: 85-100
- distRMin()[source]¶
Compute distRMin for DFA
:rtype FL
- ..seealso:: Cezar Câmpeanu, Nelma Moreira, Rogério Reis:
The distinguishability operation on regular languages. NCMA 2014: 85-100
- distTS()[source]¶
Evaluate the two-sided distinguishability language for a DFA
- Return type
- ..seealso:: Cezar Câmpeanu, Nelma Moreira, Rogério Reis:
The distinguishability operation on regular languages. NCMA 2014: 85-100
- enumDFA(n=None)[source]¶
returns the set of words of words of length up to n accepted by self :param int n: highest length or all words if finite
- Return type
list of strings or None
- equal(other)[source]¶
Verify if the two automata are equivalent. Both are verified to be minimum and complete, and then one is matched against the other… Doesn’t destroy either dfa…
- evalSymbolI(init, sym)[source]¶
Returns the state reached from a given state.
- Parameters
init (init) – current state
sym (str) – symbol to be consumed
- Returns
reached state or -1
- Return type
set of int
- Raises
DFAsymbolUnknown – if symbol not in alphabet
New in version 0.9.5.
Note
this is to be used with non complete DFAs
- evalSymbolL(ls, sym)[source]¶
Returns the set of states reached from a given set of states through a given symbol
- Parameters
ls (set of int) – set of states indexes
sym (str) – symbol to be read
- Returns
set of reached states
- Return type
set of int
- evalSymbolLI(ls, sym)[source]¶
Returns the set of states reached from a given set of states through a given symbol
- Parameters
ls (set of int) – set of current states
sym (str) – symbol to be consumed
- Returns
set of reached states
- Return type
set of int
New in version 0.9.5.
Note
this is to be used with non complete DFAs
- finalCompP(s)[source]¶
Verifies if there is a final state in strongly connected component containing
s.- Parameters
s (int) – state
- Returns
1 if yes, 0 if no
- hasTrapStateP()[source]¶
Tests if the automaton has a dead trap state
- Return type
New in version 1.1.
- hyperMinimal(strict=False)[source]¶
Hyperminization of a minimal DFA
- Parameters
strict (bool) – if strict=True it first minimizes the DFA
- Returns
an hyperminimal DFA
- Return type
See also
M. Holzer and A. Maletti, An nlogn Algorithm for Hyper-Minimizing a (Minimized) Deterministic Automata, TCS 411(38-39): 3404-3413 (2010)
Note
if strict=False minimality is assumed
- initialComp()[source]¶
Evaluates the connected component starting at the initial state.
- Returns
list of state indexes in the component
- Return type
list of int
- initialSet()[source]¶
The set of initial states
- Returns
the set of the initial states
- Return type
set of States
- joinStates(lst)[source]¶
Merge a list of states.
- Parameters
lst (iterable of state indexes.) – set of equivalent states
- makeReversible()[source]¶
Make a DFA reversible (if possible)
See also
M.Holzer, s. Jakobi, M. Kutrib ‘Minimal Reversible Deterministic Finite Automata’
- Return type
- markNonEquivalent(s1, s2, data)[source]¶
Mark states with indexes s1 and s2 in given map as non equivalent states. If any back-effects exist, apply them.
- mergeStates(f, t)[source]¶
Merge the first given state into the second. If the first state is an initial state the second becomes the initial state.
Attention
It is up to the caller to remove the disconnected state. This can be achieved with
`trim().
- minimal(method='minimalHopcroft', complete=True)[source]¶
Evaluates the equivalent minimal complete DFA
- minimalHopcroft()[source]¶
Evaluates the equivalent minimal complete DFA using Hopcroft algorithm
- Returns
equivalent minimal DFA
- Return type
See also
John Hopcroft,An n log{n} algorithm for minimizing states in a finite automaton.The Theory of Machines and Computations.AP. 1971
- minimalIncremental(minimal_test=False)[source]¶
Minimizes the DFA with an incremental method using the Union-Find algorithm and memoized non-equivalence intermediate results
- Parameters
minimal_test (bool) – starts by verifying that the automaton is not minimal?
- Returns
equivalent minimal DFA
- Return type
See also
M. Almeida and N. Moreira and and r. Reis.Incremental DFA minimisation. CIAA 2010. LNCS 6482. pp 39-48. 2010
- minimalMoore()[source]¶
Evaluates the equivalent minimal automata with Moore’s algorithm
See also
John E. Hopcroft and Jeffrey D. Ullman, Introduction to Automata Theory, Languages, and Computation, AW, 1979
- Returns
minimal complete DFA
- Return type
- minimalMooreSq()[source]¶
Evaluates the equivalent minimal complete DFA using Moore’s (quadratic) algorithm
See also
John E. Hopcroft and Jeffrey D. Ullman, Introduction to Automata Theory, Languages, and Computation, AW, 1979
- Returns
equivalent minimal DFA
- Return type
- minimalMooreSqP()[source]¶
Tests if a DFA is minimal using the quadratic version of Moore’s algorithm
- Return type
- minimalNCompleteP()[source]¶
Tests if a non necessarely complete DFA is minimal, i.e., if the DFA is non complete, if the minimal complete has only one more state.
- Returns
True if not minimal
- Return type
Attention
obsolete: use minimalP
- minimalNotEquivP()[source]¶
Tests if the DFA is minimal by computing the set of distinguishable (not equivalent) pairs of states
- Return type
- minimalP(method='minimalMooreSq')[source]¶
Tests if the DFA is minimal
- Parameters
method – the minimization algorithm to be used
- Return type
..note: if DFA non complete test if complete minimal has one more state
- minimalWatson(test_only=False)[source]¶
Evaluates the equivalent minimal complete DFA using Waton’s incremental algorithm
- Parameters
test_only (bool) – is it only to test minimality
- Returns
equivalent minimal DFA
- Return type
- Raises
DFAnotComplete – if automaton is not complete
- ..attention::
automaton must be complete
- minimalWatsonP()[source]¶
Tests if a DFA is minimal using Watson’s incremental algorithm
- Return type
- orderedStrConnComponents()[source]¶
Topological ordered list of strong components
New in version 1.3.3.
- Return type
- pairGraph()[source]¶
Returns pair graph
- Return type
DiGraphVM
See also
A graph theoretic approach to automata minimality. Antonio Restivo and Roberto Vaglica. Theoretical Computer Science, 429 (2012) 282-291. doi:10.1016/j.tcs.2011.12.049 Theoretical Computer Science, 2012 vol. 429 (C) pp. 282-291. http://dx.doi.org/10.1016/j.tcs.2011.12.049
- print_data(data)[source]¶
Prints table of compatibility (in the context of the minimalization algorithm).
- Parameters
data – data to print
- product(other)[source]¶
Returns a DFA resulting of the simultaneous execution of two DFA. No final states set.
Note
this is a fast version of the method. The resulting state names are not meaningfull.
- Parameters
other – the other DFA
- Return type
- productSlow(other, complete=True)[source]¶
Returns a DFA resulting of the simultaneous execution of two DFA. No final states set.
Note
this is a slow implementation for those that need meaningfull state names
New in version 1.3.3.
- reorder(dicti)[source]¶
Reorders states according to given dictionary. Given a dictionary (not necessarily complete)… reorders states accordingly.
- Parameters
dicti (dict) – reorder dictionary
- reverseTransitions(rev)[source]¶
Evaluate reverse transition function.
- Parameters
rev (DFA) – DFA in which the reverse function will be stored
- sSemigroup()[source]¶
Evaluation of the syntactic semigroup of a DFA
- Returns
the semigroup
- Return type
- shuffle(other, strict=False)[source]¶
CShuffle of two languages: L1 W L2
- Parameters
- Return type
See also
C. Câmpeanu, K. Salomaa and s. Yu, Tight lower bound for the state complexity of CShuffle of regular languages. J. Autom. Lang. Comb. 7 (2002) 303–310.
- sop(other)[source]¶
Strange operation
See also
Nelma Moreira, Giovanni Pighizzini, and Rogério Reis. Universal disjunctive concatenation
and star. In Jeffrey Shallit and Alexander Okhotin, editors, Proceedings of the 17th Int. Workshop on Descriptional Complexity of Formal Systems (DCFS15), number 9118 in LNCS, pages 197–208. Springer, 2015.
New in version 1.2b2.
- star(flag=False)[source]¶
Star of a DFA. If the DFA is not complete, it is completed.
..versionchanged: 0.9.6
- stronglyConnectedComponents()[source]¶
Dummy method that uses the NFA conterpart
New in version 1.3.3.
- Return type
- subword()[source]¶
Returns a dfa that recognizes subword(L(self))
- Return type
dfa
New in version 1.1.
- succintTransitions()[source]¶
Collects the transition information in a compact way suitable for graphical representation. :rtype: list of tupples
New in version 0.9.8.
- syncPower()[source]¶
Evaluates the Power automata for the action of each symbol
- Returns
The Power automata being the set of all states the initial state and all singleton states final.
- Return type
- toADFA()[source]¶
Try to convert DFA to ADFA
- Returns
the same automaton as a ADFA
- Return type
- Raises
notAcyclic – if this is not an acyclic DFA
New in version 1.2.
Changed in version 1.2.1.
- uniqueRepr()[source]¶
Normalise unique string for the string icdfa’s representation. .. seealso:: TCS 387(2):93-102, 2007 https://www.dcc.fc.up.pt/~nam/publica/tcsamr06.pdf
- Returns
normalised representation
- Return type
- Raises
DFAnotComplete – if DFA is not complete
- unmark()[source]¶
Unmarked NFA that corresponds to a marked DFA: in which each alfabetic symbol is a tuple (symbol, index)
- Returns
a NFA
- Return type
- usefulStates(initial_states=None)[source]¶
Set of states reacheable from the given initial state(s) that have a path to a final state.
- Parameters
initial_states (iterable of int) – starting states
- Returns
set of state indexes
- Return type
set of int
- static vDescription()[source]¶
Generation of Verso interface description
New in version 0.9.5.
- Returns
the interface list
- witnessDiff(other)[source]¶
Returns a witness for the difference of two DFAs and:
0
if the witness belongs to the other language
1
if the witness belongs to the self language
- Parameters
other (DFA) – the other DFA
- Returns
a witness word
- Return type
list of symbols
- Raises
DFAequivalent – if automata are equivalent
NFA¶
- class NFA[source]¶
Class for Non-deterministic Finite Automata (CEpsilon-transitions allowed).
- Variables

- HKeqP(other, strict=True)[source]¶
Test NFA equivalence with extended Hopcroft-Karp method
See also
J. E. Hopcroft and r. M. Karp. A Linear Algorithm for Testing Equivalence of Finite Automata.TR 71–114. U. California. 1971
- Parameters
other – NFA
strict – if True checks for same alphabets
- Returns
Boolean
- addEpsilonLoops()[source]¶
Add epsilon loops to every state :return: self
Attention
in-place modification
New in version 1.0.
- addInitial(stateindex)[source]¶
Add a new state to the set of initial states.
- Parameters
stateindex (int) – index of new initial state
- addTransition(sti1, sym, sti2)[source]¶
Adds a new transition. Transition is from
sti1tosti2consuming symbolsym.sti2is a unique state, not a set of them.
- addTransitionQ(srci, dest, symb, qfuture, qpast)[source]¶
Add transition to the new transducer instance.
- Parameters
New in version 1.0.
- autobisimulation()[source]¶
Largest right invariant equivalence between states of the NFA
- Returns
Incomplete equivalence relation (transitivity, and reflexivity not calculated) as a set of unordered pairs of states
- Return type
Set of frozensets
See also
Ilie&Yu, 2003
- autobisimulation2()[source]¶
Alternative space-efficient definition of NFA.autobisimulation.
- Returns
Incomplete equivalence relation (reflexivity, symmetry, and transitivity not calculated) as a set of pairs of states
- Return type
list of tuples
- closeEpsilon(st)[source]¶
Add all non CEpsilon transitions from the states in the CEpsilon closure of given state to given state.
- Parameters
st (int) – state index
- delTransition(sti1, sym, sti2, _no_check=False)[source]¶
Remove a transition if existing and perform cleanup on the transition function’s internal data structure.
- Parameters
Note
unused alphabet symbols will be discarded from sigma.
- deleteStates(del_states)[source]¶
Delete given iterable collection of states from the automaton.
Note
delta function will always be rebuilt, regardless of whether the states list to remove is a suffix, or a sublist, of the automaton’s states list.
- detSet(generic=False)[source]¶
Computes the determination uppon a followFromPosition result
- Return type
- dotFormat(size='20,20', filename=None, direction='LR', strict=False, maxlblsz=6, sep='\n') str[source]¶
A dot representation
- Parameters
- Returns
the dot representation
- Return type
New in version 0.9.6.
Changed in version 1.2.1.
- elimEpsilon()[source]¶
Eliminate CEpsilon-transitions from this automaton.
:rtype : NFA
Attention
performs in place modification of automaton
Changed in version 1.1.1.
- eliminateEpsilonTransitions()[source]¶
Eliminates all epslilon-transitions with no state addition
Attention
in-place modification
- eliminateTSymbol(symbol)[source]¶
Delete all trasitions through a given symbol
- Parameters
symbol (str) – the symbol to be excluded from delta
Attention
in place alteration of the automata
New in version 0.9.6.
- enumNFA(n=None)[source]¶
returns the set of words of words of length up to n accepted by self :param int n: highest lenght or all words if finite
- Return type
list of strings or None
- epsilonClosure(st)[source]¶
Returns the set of states CEpsilon-connected to from given state or set of states.
- Parameters
- Returns
the list of state indexes CEpsilon connected to
st- Return type
set of int
Attention
stmust exist.
- epsilonPaths(start, end)[source]¶
All states in all paths (DFS) through empty words from a given starting state to a given ending state.
- equivReduced(equiv_classes)[source]¶
Equivalent NFA reduced according to given equivalence classes.
- Parameters
equiv_classes (UnionFind) – Equivalence classes
- Returns
Equivalent NFA
- Return type
- evalSymbol(stil, sym)[source]¶
Set of states reacheable from given states through given symbol and CEpsilon closure.
- finalCompP(s)[source]¶
Verify whether there is a final state in strongly connected component containing given state.
- Parameters
s (int) – state index
- Returns
:: bool
- hasTransitionP(state, symbol=None, target=None)[source]¶
Whether there’s a transition from given state, optionally through given symbol, and optionally to a specific target.
- homogeneousFinalityP()[source]¶
Tests if states have incoming transitions froms states with different finalities
- Return type
- homogenousP(x)[source]¶
Whether this NFA is homogenous; that is, for all states, whether all incoming transitions to that state are through the same symbol.
- Parameters
x – dummy parameter to agree with the method in DFAr
- Return type
- initialComp()[source]¶
Evaluate the connected component starting at the initial state.
- Returns
list of state indexes in the component
- Return type
list of int
- lEquivNFA()[source]¶
Equivalent NFA obtained from merging equivalent states from autobisimulation of this NFA’s reversal.
- Return type
Note
returns copy of self if autobisimulation renders no equivalent states.
- lrEquivNFA()[source]¶
Equivalent NFA obtained from merging equivalent states from autobisimulation of this NFA, and from autobisimulation of its reversal; i.e., merges all states that are equivalent w.r.t. the largest right invariant and largest left invariant equivalence relations.
- Return type
Note
returns copy of self if autobisimulations render no equivalent states.
- minimalDFA()[source]¶
Evaluates the equivalent minimal complete DFA
- Returns
equivalent minimal DFA
- Return type
- product(other)[source]¶
Returns a NFA (skeletom) resulting of the simultaneous execution of two DFA.
Note
No final states are set.
Attention
the name
EmptySetis used in a unique special state namethe method uses 3 internal functions for simplicity of code (really!)
- rEquivNFA()[source]¶
Equivalent NFA obtained from merging equivalent states from autobisimulation of this NFA.
- Return type
Note
returns copy of self if autobisimulation renders no equivalent states.
- renameStatesFromPosition()[source]¶
Rename states of a Glushkov automaton using the positions of the marked RE
- Return type
- reorder(dicti)[source]¶
Reorder states indexes according to given dictionary.
- Parameters
dicti (dict) – state name reorder
Note
dictionary does not have to be complete
- reversal()[source]¶
Returns a NFA that recognizes the reversal of the language
- Returns
NFA recognizing reversal language
- Return type
- reverseTransitions(rev)[source]¶
Evaluate reverse transition function.
- Parameters
rev (NFA) – NFA in which the reverse function will be stored
- shuffle(other)[source]¶
Shuffle of a NFA
- Parameters
other (FA) – an FA
- Returns: the resulting NFA
NFA: the resulting NFA
- succintTransitions()[source]¶
Collects the transition information in a compact way suitable for graphical representation. :rtype: list
- toDFA()[source]¶
Construct a DFA equivalent to this NFA, by the subset construction method.
- Return type
Note
valid to CEpsilon-NFA
- toNFAr()[source]¶
NFA with the reverse mapping of the delta function.
- Returns
shallow copy with reverse delta function added
- Return type
- usefulStates(initial_states=None)[source]¶
Set of states reacheable from the given initial state(s) that have a path to a final state.
- Parameters
initial_states (set of int or list of int) – set of initial states
- Returns
set of state indexes
- Return type
set of int
NFAr¶
- class NFAr[source]¶
- Class for Non-deterministic Finite Automata with reverse delta function added by construction.
Includes efficient methods for merging states.

- addTransition(sti1, sym, sti2)[source]¶
Adds a new transition. Transition is from
sti1tosti2consuming symbolsym.sti2is a unique state, not a set of them. Reversed transition function is also computed
- delTransition(sti1, sym, sti2, _no_check=False)[source]¶
Remove a transition if existing and perform cleanup on the transition function’s internal data structure and in the reversal transition function
- deleteStates(del_states)[source]¶
Delete given iterable collection of states from the automaton. Performe deletion in the transition function and its reversal.
- Parameters
del_states (set or list of int) – collection of int representing states
- elimEpsilonO()[source]¶
Eliminate CEpsilon-transitions from this automaton, with reduction of states through elimination of CEpsilon-cycles, and single CEpsilon-transition cases.
- Returns
itself
- Return type
Attention
performs inplace modification of automaton
- homogenousP(inplace=False)[source]¶
Checks is the automaton is homogenous, i.e.the transitions that reaches a state have all the same label.
- mergeStates(f, t)[source]¶
Merge the first given state into the second. If first state is an initial or final state, the second becomes respectively an initial or final state.
Attention
It is up to the caller to remove the disconnected state. This can be achieved with
`trim().
- mergeStatesSet(tomerge, target=None)[source]¶
Merge a set of states with a target merge state. If the states in the set have transitions among them, those transitions will be directly merged into the target state.
- Parameters
tomerge (Set of int) – set of states to merge with target
target (int) – optional target state
Note
if target state is not given, the minimal index with be considered.
Attention
The states of the list will become unreacheable, but won’t be removed. It is up to the caller to remove them. That can be achieved with
trim().
- toNFA()[source]¶
Turn into an instance of NFA, and remove the reverse mapping of the delta function.
- Returns
shallow copy without reverse delta function
- Return type
- unlinkSoleIncoming(state)[source]¶
If given state has only one incoming transition (indegree is one), and it’s through CEpsilon, then remove such transition and return the source state.
Note
if conditions aren’t met, returned source state is None, and automaton remains unmodified.
SSemiGroup¶
- class SSemiGroup[source]¶
Class support for the Syntactic SemiGroup.
- Variables
elements – list of tuples representing the transformations
words – a list of pairs (index of the prefix transformation, index of the suffix char)
gen – a list of the max index of each generation
sigma – set of symbols
EnumL¶
- class EnumL(aut, store=False)[source]¶
- Class for enumerate FA languages
See: Efficient enumeration of words in regular languages, M. Ackerman and J. Shallit, Theor. Comput. Sci. 410, 37, pp 3461-3470. 2009. http://dx.doi.org/10.1016/j.tcs.2009.03.018
- Variables
New in version 0.9.8.
- enum(m)[source]¶
Enumerates the first m words of L(A) according to the lexicographic order if there are at least m words. Otherwise, enumerates all words accepted by A.
- Parameters
m (int) – max number of words
- enumCrossSection(n)[source]¶
Enumerates the nth cross-section of L(A)
- Parameters
n (int) – nonnegative integer