|
| 1 | +from itertools import product |
| 2 | +from typing import Set |
| 3 | +from ..schemas.fsa import FSA |
| 4 | + |
| 5 | +def is_valid_fsa(fsa: FSA) -> bool: |
| 6 | + states = set(fsa.states) |
| 7 | + alphabet = set(fsa.alphabet) |
| 8 | + |
| 9 | + if fsa.initial_state not in states: |
| 10 | + return False |
| 11 | + |
| 12 | + if not set(fsa.accept_states).issubset(states): |
| 13 | + return False |
| 14 | + |
| 15 | + for t in fsa.transitions: |
| 16 | + if t.from_state not in states: |
| 17 | + return False |
| 18 | + if t.to_state not in states: |
| 19 | + return False |
| 20 | + if t.symbol not in alphabet: |
| 21 | + return False |
| 22 | + |
| 23 | + return True |
| 24 | + |
| 25 | +def is_deterministic(fsa: FSA) -> bool: |
| 26 | + if not is_valid_fsa(fsa): |
| 27 | + return False |
| 28 | + |
| 29 | + seen = set() |
| 30 | + |
| 31 | + for t in fsa.transitions: |
| 32 | + key = (t.from_state, t.symbol) |
| 33 | + if key in seen: |
| 34 | + return False |
| 35 | + seen.add(key) |
| 36 | + |
| 37 | + return True |
| 38 | + |
| 39 | +def is_complete(fsa: FSA) -> bool: |
| 40 | + if not is_deterministic(fsa): |
| 41 | + return False |
| 42 | + |
| 43 | + states = set(fsa.states) |
| 44 | + alphabet = set(fsa.alphabet) |
| 45 | + |
| 46 | + seen = {(t.from_state, t.symbol) for t in fsa.transitions} |
| 47 | + |
| 48 | + for state in states: |
| 49 | + for symbol in alphabet: |
| 50 | + if (state, symbol) not in seen: |
| 51 | + return False |
| 52 | + |
| 53 | + return True |
| 54 | + |
| 55 | +def classify_fsa(fsa: FSA) -> dict: |
| 56 | + return { |
| 57 | + "valid": is_valid_fsa(fsa), |
| 58 | + "deterministic": is_deterministic(fsa), |
| 59 | + "complete": is_complete(fsa), |
| 60 | + } |
| 61 | + |
| 62 | +# simple bfs |
| 63 | +def accepts_string(fsa: FSA, string: str) -> bool: |
| 64 | + """ |
| 65 | + Simulate the FSA on a given string. |
| 66 | + Returns True if the string is accepted, False otherwise. |
| 67 | + """ |
| 68 | + current_states: Set[str] = {fsa.initial_state} |
| 69 | + |
| 70 | + for symbol in string: |
| 71 | + next_states = set() |
| 72 | + for state in current_states: |
| 73 | + for t in fsa.transitions: |
| 74 | + if t.from_state == state and t.symbol == symbol: |
| 75 | + next_states.add(t.to_state) |
| 76 | + current_states = next_states |
| 77 | + if not current_states: |
| 78 | + return False |
| 79 | + |
| 80 | + return any(state in fsa.accept_states for state in current_states) |
| 81 | + |
| 82 | + |
| 83 | +def fsas_accept_same_string(fsa1: FSA, fsa2: FSA, string: str) -> bool: |
| 84 | + """ |
| 85 | + Check if two FSAs accept the same given string. |
| 86 | + """ |
| 87 | + return accepts_string(fsa1, string) and accepts_string(fsa2, string) |
| 88 | + |
| 89 | +def fsas_accept_same_language(fsa1: FSA, fsa2: FSA, max_length: int = 5) -> bool: |
| 90 | + """ |
| 91 | + Approximate check if two FSAs accept the same language. |
| 92 | + Checks all strings over the alphabet up to length `max_length`. |
| 93 | + Warning: exponential in alphabet size * max_length. |
| 94 | + """ |
| 95 | + alphabet = fsa1.alphabet |
| 96 | + if set(fsa1.alphabet) != set(fsa2.alphabet): |
| 97 | + return False |
| 98 | + |
| 99 | + for length in range(max_length + 1): |
| 100 | + for s in product(alphabet, repeat=length): |
| 101 | + string = ''.join(s) |
| 102 | + if accepts_string(fsa1, string) != accepts_string(fsa2, string): |
| 103 | + return False |
| 104 | + return True |
| 105 | +# Note: This is practical for small alphabets and short strings. |
| 106 | +# For full correctness on infinite languages, you need minimized DFA equivalence. |
| 107 | + |
| 108 | +# is_nfa() |
| 109 | +# make_complete() |
| 110 | +# add_sink_state() |
| 111 | + |
0 commit comments