static Set <int> AcceptStates( ICollection <Set <int> > states, NfaToDfaRenamer renamer, Set <int> acceptStates) { Set <int> result = new Set <int>(); foreach (Set <int> dfaSubsetState in states) { if (acceptStates.Any(finalState => dfaSubsetState.Contains(finalState))) { result.Add(renamer.ToDfaStateIndex(dfaSubsetState)); } } return(result); }
// Using a renamer (a Map from Set of int to int), replace // composite (Set of int) states with simple (int) states in // the transition relation trans, which is assumed to be a Map // from Set of int to Map from string to Set of int. The // result is a Map from int to Map from string to int. // Given a Map from Set of int to Map from string to Set of // int, use the result of MkRenamer to replace all Sets of ints // by ints. static IDictionary <int, Dictionary <TAlphabet, int> > Rename( NfaToDfaRenamer renamer, IDictionary <Set <int>, Dictionary <TAlphabet, Set <int> > > dfaTrans) { var newDfaTrans = new SortedDictionary <int, Dictionary <TAlphabet, int> >(); // keys/states are sorted foreach (KeyValuePair <Set <int>, Dictionary <TAlphabet, Set <int> > > entry in dfaTrans) { Set <int> dfaState = entry.Key; var newDfaTransRow = new Dictionary <TAlphabet, int>(); foreach (KeyValuePair <TAlphabet, Set <int> > tr in entry.Value) { newDfaTransRow.Add(tr.Key, renamer.ToDfaStateIndex(tr.Value)); } newDfaTrans.Add(renamer.ToDfaStateIndex(dfaState), newDfaTransRow); } return(newDfaTrans); }
/// <summary> /// McNaughton-Yamada-Thompson algorithm (aka Thompson's construction) /// </summary> public Dfa <TAlphabet> ToDfa(bool skipRenaming = false) { IDictionary <Set <int>, Dictionary <TAlphabet, Set <int> > > cDfaTrans = CompositeDfaTrans(Start, Trans); Set <int> cDfaStart = EpsilonClose(new Set <int>(new [] { Start }), Trans); ICollection <Set <int> > cDfaStates = cDfaTrans.Keys; var renamer = new NfaToDfaRenamer(cDfaStates, skipRenaming, _predicate); // DFA-transitions (delta) IDictionary <int, Dictionary <TAlphabet, int> > dfaTrans = Rename(renamer, cDfaTrans); // The singleton start state = q_0 int dfaStartStateIndex = renamer.ToDfaStateIndex(cDfaStart); // The subset of accepting states = F Set <int> dfaAcceptingStateIndices = AcceptStates(cDfaStates, renamer, AcceptingStates); return(new Dfa <TAlphabet>(dfaStartStateIndex, dfaAcceptingStateIndices, dfaTrans, renamer)); }