#Reduction of Incompletely Specified Automata

class FSA:
    def __init__(self, states, alphabet, transitions, start_state, accepting_states):
        self.states = states
        self.alphabet = alphabet
        self.transitions = transitions
        self.start_state = start_state
        self.accepting_states = accepting_states

    def reduce(self):
        # Initialize a new set of states
        new_states = set()
        new_accepting_states = set()

        for state in self.states:
            for symbol in self.alphabet:
                if (state, symbol) not in self.transitions:
                    # If a transition is missing, add a new state
                    new_state = f'q{len(new_states)}'
                    new_states.add(new_state)
                    self.transitions[(state, symbol)] = new_state

        # Update the accepting states
        for state in self.accepting_states:
            new_accepting_states.add(state)
            if state not in new_states:
                new_state = f'q{len(new_states)}'
                new_states.add(new_state)
                self.transitions[(state, '')] = new_state
                new_accepting_states.add(new_state)

        self.states = new_states
        self.accepting_states = new_accepting_states

    def is_equivalent(self, other_fsa):
        return self.states == other_fsa.states and \
               self.alphabet == other_fsa.alphabet and \
               self.transitions == other_fsa.transitions and \
               self.start_state == other_fsa.start_state and \
               self.accepting_states == other_fsa.accepting_states


# Example usage
# Create the FSA
fsa_states = {'q0', 'q1', 'q2'}
fsa_alphabet = {'0', '1'}
fsa_transitions = {('q0', '0'): 'q1', ('q1', '1'): 'q2'}
fsa_start_state = 'q0'
fsa_accepting_states = {'q2'}

fsa = FSA(fsa_states, fsa_alphabet, fsa_transitions, fsa_start_state, fsa_accepting_states)

# Reduce the FSA
fsa.reduce()

# Check if the reduced FSA is equivalent to the original FSA
is_equivalent = fsa.is_equivalent(fsa)
print(f"Is the reduced FSA equivalent to the original FSA? {is_equivalent}")
