From e36a9d82ada58499cc4ac97bf5b50331e4ff7696 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Djild=C3=A9-Mb=C3=A9nard=20Dabo?=
 <djilde-mbenard.dabo@etu.u-bordeaux.fr>
Date: Mon, 22 Jul 2024 10:26:45 +0200
Subject: [PATCH] Optimization in final states computations

---
 pysemigroup/automata.py | 13 ++++++-------
 1 file changed, 6 insertions(+), 7 deletions(-)

diff --git a/pysemigroup/automata.py b/pysemigroup/automata.py
index 82b6c38..97c14de 100644
--- a/pysemigroup/automata.py
+++ b/pysemigroup/automata.py
@@ -791,7 +791,7 @@ class DFA(NFA):
     def minimize(self) -> "DFA":
         def components_of_minimized_automata(
             minimized_states: Iterable[Hashable],
-        ) -> tuple[Iterable[tuple[Hashable, Hashable, Hashable]], Hashable]:
+        ) -> tuple[Iterable[tuple[Hashable, Hashable, Hashable]], Hashable, Iterable[Hashable]]:
             """
             Intermediary function that, from a list containing tuples compounded by sets of equivalent states as well
             as their key, returns another tuple containing the transitions that correspond to the ones in between the
@@ -802,10 +802,13 @@ class DFA(NFA):
 
             init_state = 0
             transitions = set()
+            final_states = set()
             for state in minimized_states:
                 if self.initial_state in state:
                     # Assignment of new initial state
                     init_state = state
+                if self.final_states.intersection(state):
+                    final_states.add(state)
 
                 # In order to compute the new transitions, we take a random state from each set of equivalent states,
                 # we then look for its successor (another state) though each letter of the alphabet (as each state needs
@@ -818,7 +821,7 @@ class DFA(NFA):
                         if successor.issubset(successor_state):
                             transitions.add((state, letter, successor_state))
                             continue
-            return transitions, init_state
+            return transitions, init_state, final_states
 
         def split(classes_eq: Iterable[C]) -> Iterable[C]:
             """
@@ -870,14 +873,10 @@ class DFA(NFA):
         # With subsets considered the equivalent states (states of minimized automata), it is now possible to compute
         # the transitions of these new states, as well as initial state.
 
-        tr, init = components_of_minimized_automata(states_classes)
+        tr, init, final_states = components_of_minimized_automata(states_classes)
 
         # Final states are computable as well
 
-        final_states = set()
-        for f in states_classes:
-            if self.final_states.intersection(f):
-                final_states.add(f)
 
         return DFA(tr, init, final_states)
 
-- 
GitLab