Exemplo n.º 1
0
        /// <summary>
        /// Пример работы с автоматами
        /// </summary>
        static void AutomataSample()
        {
            DeterminedFiniteAutomaton dfa = new DeterminedFiniteAutomaton("q0");

            dfa.AddSymbol("a");
            dfa.AddToFinalStates("q0");
            dfa.AddTransition("q0", "q0", "a");
            dfa.AddSymbol("b");
            dfa.AddState("q1");
            dfa.AddState("q2");
            dfa.AddTransition("q0", "q2", "b");
            dfa.AddTransition("q2", "q1", "b");
            dfa.AddToFinalStates("q1");
            string fromQ0ByB = dfa.GetTransitionDestination("q0", "b");

            Console.WriteLine("Переход по символу b из состояния q0 приведёт в состояние: {0}", fromQ0ByB);
        }
Exemplo n.º 2
0
        public static DeterminedFiniteAutomaton ConvertNFAToDFA(NondeterminedFiniteAutomaton nfa)
        {
            //DeterminedFiniteAutomaton dfa
            nfa = nfa.GetEquivalentDeletedEpsilons();
            HashSet <string>         currentStates, accessibleStates;
            List <HashSet <string> > allSetsOfStates = new List <HashSet <string> >();

            allSetsOfStates.Add(new HashSet <string>(nfa.GetStartStates()));
            DeterminedFiniteAutomaton dfa = new DeterminedFiniteAutomaton("q0");

            List <string> names = new List <string>();

            dfa.AddSymbols(nfa.GetAlphabet());
            var    finalStates = nfa.GetFinalStates();
            string from, to;
            bool   f = true;

            for (int allSetsCounter = 0, currentSetNumber = 0, pos; currentSetNumber <= allSetsCounter; currentSetNumber++)
            {
                currentStates = allSetsOfStates[currentSetNumber];
                if (currentStates.Any(state => nfa.GetFinalStates().Contains(state)))
                {
                    dfa.AddToFinalStates(string.Format("q{0}", currentSetNumber));
                }
                foreach (var symbol in nfa.GetAlphabet())
                {
                    accessibleStates = GetAccessibleStates(nfa, currentStates, symbol);
                    if (accessibleStates.Count != 0)
                    {
                        pos  = CheckIsListContains(allSetsOfStates, accessibleStates);
                        from = string.Format("q{0}", currentSetNumber);
                        to   = string.Format("q{0}", pos);
                        if (pos == -1)
                        {
                            allSetsCounter++;
                            to = string.Format("q{0}", allSetsCounter);
                            dfa.AddState(to);
                            allSetsOfStates.Add(new HashSet <string>(accessibleStates));
                        }
                        dfa.AddTransition(from, to, symbol);
                    }
                }
            }
            return(dfa);
        }