Example #1
0
        public void DfaTest1()
        {
            char[]            alphabet = { 'a', 'b' };
            Automaat <string> dfa      = new Automaat <string>(alphabet);

            dfa.AddTransition(new Transition <string>("0", 'a', "1"));
            dfa.AddTransition(new Transition <string>("0", 'b', "4"));

            dfa.AddTransition(new Transition <string>("1", 'b', "2"));
            dfa.AddTransition(new Transition <string>("1", 'a', "4"));

            dfa.AddTransition(new Transition <string>("2", 'a', "3"));
            dfa.AddTransition(new Transition <string>("2", 'b', "4"));

            dfa.AddTransition(new Transition <string>("3", 'a', "1"));
            dfa.AddTransition(new Transition <string>("3", 'b', "2"));

            dfa.AddTransition(new Transition <string>("4", 'a', "4"));
            dfa.AddTransition(new Transition <string>("4", 'b', "4"));

            dfa.DefineAsStartState("0");
            dfa.DefineAsFinalState("0");
            dfa.DefineAsFinalState("2");

            Assert.IsTrue(dfa.IsDfa());
        }
Example #2
0
        public void AcceptTestInCorrectFinalState()
        {
            char[]            alphabet = { 'a', 'b' };
            Automaat <string> dfa      = new Automaat <string>(alphabet);

            dfa.AddTransition(new Transition <string>("0", 'a', "1"));
            dfa.AddTransition(new Transition <string>("0", 'b', "4"));

            dfa.AddTransition(new Transition <string>("1", 'b', "2"));
            dfa.AddTransition(new Transition <string>("1", 'a', "4"));

            dfa.AddTransition(new Transition <string>("2", 'a', "3"));
            dfa.AddTransition(new Transition <string>("2", 'b', "4"));

            dfa.AddTransition(new Transition <string>("3", 'a', "1"));
            dfa.AddTransition(new Transition <string>("3", 'b', "2"));

            dfa.AddTransition(new Transition <string>("4", 'a', "4"));
            dfa.AddTransition(new Transition <string>("4", 'b', "4"));

            dfa.DefineAsStartState("0");
            dfa.DefineAsFinalState("0");
            dfa.DefineAsFinalState("2");

            Assert.IsFalse(dfa.Accept("aba"));
        }
Example #3
0
        public static Automaat <string> GetExampleSlide8Lesson2()
        {
            char[]            alphabet = { 'a', 'b' };
            Automaat <string> m        = new Automaat <string>(alphabet);

            m.AddTransition(new Transition <string>("q0", 'a', "q1"));
            m.AddTransition(new Transition <string>("q0", 'b', "q4"));

            m.AddTransition(new Transition <string>("q1", 'a', "q4"));
            m.AddTransition(new Transition <string>("q1", 'b', "q2"));

            m.AddTransition(new Transition <string>("q2", 'a', "q3"));
            m.AddTransition(new Transition <string>("q2", 'b', "q4"));

            m.AddTransition(new Transition <string>("q3", 'a', "q1"));
            m.AddTransition(new Transition <string>("q3", 'b', "q2"));

            // the error state, loops for a and b:
            m.AddTransition(new Transition <string>("q4", 'a'));
            m.AddTransition(new Transition <string>("q4", 'b'));

            // only on start state in a dfa:
            m.DefineAsStartState("q0");

            // two final states:
            m.DefineAsFinalState("q2");
            m.DefineAsFinalState("q3");

            return(m);
        }
Example #4
0
        public static Automaat <string> GetExampleSlide14Lesson2()
        {
            char[]            alphabet = { 'a', 'b' };
            Automaat <string> m        = new Automaat <string>(alphabet);

            m.AddTransition(new Transition <string>("A", 'a', "C"));
            m.AddTransition(new Transition <string>("A", 'b', "B"));
            m.AddTransition(new Transition <string>("A", 'b', "C"));

            m.AddTransition(new Transition <string>("B", 'b', "C"));
            m.AddTransition(new Transition <string>("B", "C"));

            m.AddTransition(new Transition <string>("C", 'a', "D"));
            m.AddTransition(new Transition <string>("C", 'a', "E"));
            m.AddTransition(new Transition <string>("C", 'b', "D"));

            m.AddTransition(new Transition <string>("D", 'a', "B"));
            m.AddTransition(new Transition <string>("D", 'a', "C"));

            m.AddTransition(new Transition <string>("E", 'a'));
            m.AddTransition(new Transition <string>("E", "D"));

            // only on start state in a dfa:
            m.DefineAsStartState("A");

            // two final states:
            m.DefineAsFinalState("C");
            m.DefineAsFinalState("E");

            return(m);
        }
        public void TestConvertAutomaat2()
        {
            Automaat <string> ndfa = new Automaat <string>(new[] { 'a', 'b' });

            ndfa.AddTransition(new Transition <string>("S", 'a', "A"));
            ndfa.AddTransition(new Transition <string>("S", 'a', "S"));

            ndfa.AddTransition(new Transition <string>("A", 'b', "B"));

            ndfa.DefineAsStartState("S");
            ndfa.DefineAsFinalState("B");

            Automaat <string> dfa = NdfaToDfaConverter.Convert(ndfa);

            Assert.AreEqual("(AS, a)-->AS", dfa.Transitions.ToList()[0].ToString());
            Assert.AreEqual("(AS, b)-->B", dfa.Transitions.ToList()[1].ToString());
            Assert.AreEqual("(B, a)-->F", dfa.Transitions.ToList()[2].ToString());
            Assert.AreEqual("(B, b)-->F", dfa.Transitions.ToList()[3].ToString());
            Assert.AreEqual("(F, a)-->F", dfa.Transitions.ToList()[4].ToString());
            Assert.AreEqual("(F, b)-->F", dfa.Transitions.ToList()[5].ToString());
            Assert.AreEqual("(S, a)-->AS", dfa.Transitions.ToList()[6].ToString());
            Assert.AreEqual("(S, b)-->F", dfa.Transitions.ToList()[7].ToString());

            Assert.AreEqual("B", dfa.FinalStates.ToList()[0]);

            Assert.AreEqual("S", dfa.StartStates.ToList()[0]);
        }
        public void TestReverseStartStop()
        {
            Automaat <string> automaat = new Automaat <string>();

            automaat.DefineAsStartState("0");
            automaat.DefineAsFinalState("1");
            Automaat <string> reverse = NdfaToDfaConverter.Reverse(automaat);

            Assert.AreEqual(automaat.StartStates, reverse.FinalStates);
            Assert.AreEqual(automaat.FinalStates, reverse.StartStates);
        }
        public void TestConvertAutomaat1()
        {
            char[]            alphabet = { 'a', 'b' };
            Automaat <string> ndfa     = new Automaat <string>(alphabet);

            ndfa.AddTransition(new Transition <string>("1", 'a', "2"));
            ndfa.AddTransition(new Transition <string>("1", 'b', "2"));
            ndfa.AddTransition(new Transition <string>("1", 'b', "3"));

            ndfa.AddTransition(new Transition <string>("2", 'a', "4"));
            ndfa.AddTransition(new Transition <string>("2", 'a', "2"));

            ndfa.AddTransition(new Transition <string>("4", 'b', "3"));
            ndfa.AddTransition(new Transition <string>("4", 'b', "1"));

            ndfa.DefineAsStartState("1");
            ndfa.DefineAsFinalState("1");
            ndfa.DefineAsFinalState("4");

            Automaat <string> dfa = NdfaToDfaConverter.Convert(ndfa);

            Assert.AreEqual("(1, a)-->2", dfa.Transitions.ToList()[0].ToString());
            Assert.AreEqual("(1, b)-->23", dfa.Transitions.ToList()[1].ToString());
            Assert.AreEqual("(13, a)-->2", dfa.Transitions.ToList()[2].ToString());
            Assert.AreEqual("(13, b)-->23", dfa.Transitions.ToList()[3].ToString());
            Assert.AreEqual("(2, a)-->24", dfa.Transitions.ToList()[4].ToString());
            Assert.AreEqual("(2, b)-->F", dfa.Transitions.ToList()[5].ToString());
            Assert.AreEqual("(23, a)-->24", dfa.Transitions.ToList()[6].ToString());
            Assert.AreEqual("(23, b)-->F", dfa.Transitions.ToList()[7].ToString());
            Assert.AreEqual("(24, a)-->24", dfa.Transitions.ToList()[8].ToString());
            Assert.AreEqual("(24, b)-->13", dfa.Transitions.ToList()[9].ToString());
            Assert.AreEqual("(F, a)-->F", dfa.Transitions.ToList()[10].ToString());
            Assert.AreEqual("(F, b)-->F", dfa.Transitions.ToList()[11].ToString());

            Assert.AreEqual("1", dfa.FinalStates.ToList()[0]);
            Assert.AreEqual("13", dfa.FinalStates.ToList()[1]);
            Assert.AreEqual("24", dfa.FinalStates.ToList()[2]);

            Assert.AreEqual("1", dfa.StartStates.ToList()[0]);
        }
Example #8
0
        public void GeefTaalTotLengte()
        {
            Automaat <string> automaat = new Automaat <string>(new[] { 'a', 'b' });

            automaat.AddTransition(new Transition <string>("0", 'a', "1"));
            automaat.AddTransition(new Transition <string>("0", 'b', "0"));

            automaat.AddTransition(new Transition <string>("1", 'a', "1"));
            automaat.AddTransition(new Transition <string>("1", 'b', "0"));

            automaat.DefineAsStartState("0");
            automaat.DefineAsFinalState("1");

            Assert.AreEqual(4, automaat.GeefTaalTotLengte(2).Count);
        }
Example #9
0
        public void AcceptEpsilonTest()
        {
            char[]            alphabet    = { 'a', 'b' };
            Automaat <string> epsilonNdfa = new Automaat <string>(alphabet);

            epsilonNdfa.AddTransition(new Transition <string>("0", 'a', "1"));
            epsilonNdfa.AddTransition(new Transition <string>("1", '$', "2"));
            epsilonNdfa.AddTransition(new Transition <string>("2", 'b', "3"));
            epsilonNdfa.AddTransition(new Transition <string>("3", '$', "4"));
            epsilonNdfa.AddTransition(new Transition <string>("4", '$', "5"));
            epsilonNdfa.AddTransition(new Transition <string>("5", 'c', "6"));

            epsilonNdfa.DefineAsStartState("0");
            epsilonNdfa.DefineAsFinalState("6");

            Assert.IsTrue(epsilonNdfa.Accept("abc"));
        }
        public void TestConvertAutomaat3()
        {
            Automaat <string> ndfa = new Automaat <string>(new[] { 'a', 'b' });

            ndfa.AddTransition(new Transition <string>("0", 'a', "0"));
            ndfa.AddTransition(new Transition <string>("0", 'a', "1"));
            ndfa.AddTransition(new Transition <string>("0", 'b', "1"));

            ndfa.AddTransition(new Transition <string>("1", 'a', "2"));
            ndfa.AddTransition(new Transition <string>("1", 'b', "2"));

            ndfa.AddTransition(new Transition <string>("2", 'a', "0"));
            ndfa.AddTransition(new Transition <string>("2", 'a', "2"));

            ndfa.DefineAsStartState("0");
            ndfa.DefineAsFinalState("2");

            Automaat <string> dfa = NdfaToDfaConverter.Convert(ndfa);

            Assert.AreEqual("(0, a)-->01", dfa.Transitions.ToList()[0].ToString());
            Assert.AreEqual("(0, b)-->1", dfa.Transitions.ToList()[1].ToString());
            Assert.AreEqual("(01, a)-->012", dfa.Transitions.ToList()[2].ToString());
            Assert.AreEqual("(01, b)-->12", dfa.Transitions.ToList()[3].ToString());
            Assert.AreEqual("(012, a)-->012", dfa.Transitions.ToList()[4].ToString());
            Assert.AreEqual("(012, b)-->12", dfa.Transitions.ToList()[5].ToString());
            Assert.AreEqual("(02, a)-->012", dfa.Transitions.ToList()[6].ToString());
            Assert.AreEqual("(02, b)-->1", dfa.Transitions.ToList()[7].ToString());
            Assert.AreEqual("(1, a)-->2", dfa.Transitions.ToList()[8].ToString());
            Assert.AreEqual("(1, b)-->2", dfa.Transitions.ToList()[9].ToString());
            Assert.AreEqual("(12, a)-->02", dfa.Transitions.ToList()[10].ToString());
            Assert.AreEqual("(12, b)-->2", dfa.Transitions.ToList()[11].ToString());
            Assert.AreEqual("(2, a)-->02", dfa.Transitions.ToList()[12].ToString());
            Assert.AreEqual("(2, b)-->F", dfa.Transitions.ToList()[13].ToString());
            Assert.AreEqual("(F, a)-->F", dfa.Transitions.ToList()[14].ToString());
            Assert.AreEqual("(F, b)-->F", dfa.Transitions.ToList()[15].ToString());

            Assert.AreEqual("012", dfa.FinalStates.ToList()[0]);
            Assert.AreEqual("02", dfa.FinalStates.ToList()[1]);
            Assert.AreEqual("12", dfa.FinalStates.ToList()[2]);
            Assert.AreEqual("2", dfa.FinalStates.ToList()[3]);

            Assert.AreEqual("0", dfa.StartStates.ToList()[0]);
        }
Example #11
0
        public void HopcroftTest1()
        {
            char[]            alphabet      = { 'a', 'b' };
            Automaat <string> dfaToHopcroft = new Automaat <string>(alphabet);

            dfaToHopcroft.AddTransition(new Transition <string>("1", 'a', "2"));
            dfaToHopcroft.AddTransition(new Transition <string>("1", 'b', "3"));

            dfaToHopcroft.AddTransition(new Transition <string>("2", 'a', "4"));
            dfaToHopcroft.AddTransition(new Transition <string>("2", 'b', "5"));

            dfaToHopcroft.AddTransition(new Transition <string>("3", 'a', "6"));
            dfaToHopcroft.AddTransition(new Transition <string>("3", 'b', "7"));

            dfaToHopcroft.AddTransition(new Transition <string>("4", 'a', "4"));
            dfaToHopcroft.AddTransition(new Transition <string>("4", 'b', "4"));

            dfaToHopcroft.AddTransition(new Transition <string>("5", 'a', "2"));
            dfaToHopcroft.AddTransition(new Transition <string>("5", 'b', "8"));

            dfaToHopcroft.AddTransition(new Transition <string>("6", 'a', "4"));
            dfaToHopcroft.AddTransition(new Transition <string>("6", 'b', "5"));

            dfaToHopcroft.AddTransition(new Transition <string>("7", 'a', "6"));
            dfaToHopcroft.AddTransition(new Transition <string>("7", 'b', "7"));

            dfaToHopcroft.AddTransition(new Transition <string>("8", 'a', "6"));
            dfaToHopcroft.AddTransition(new Transition <string>("8", 'b', "9"));

            dfaToHopcroft.AddTransition(new Transition <string>("9", 'a', "6"));
            dfaToHopcroft.AddTransition(new Transition <string>("9", 'b', "10"));

            dfaToHopcroft.AddTransition(new Transition <string>("10", 'a', "6"));
            dfaToHopcroft.AddTransition(new Transition <string>("10", 'b', "10"));

            dfaToHopcroft.DefineAsStartState("1");

            dfaToHopcroft.DefineAsFinalState("3");
            dfaToHopcroft.DefineAsFinalState("5");
            dfaToHopcroft.DefineAsFinalState("7");
            dfaToHopcroft.DefineAsFinalState("9");
            dfaToHopcroft.DefineAsFinalState("10");

            Automaat <string> hopcroftedDfa = HopcroftAlgorithm.MinimizeDfa(dfaToHopcroft);

            Assert.AreEqual("(A, a)-->B", hopcroftedDfa.Transitions.ToList()[0].ToString());
            Assert.AreEqual("(A, b)-->D", hopcroftedDfa.Transitions.ToList()[1].ToString());
            Assert.AreEqual("(B, a)-->C", hopcroftedDfa.Transitions.ToList()[2].ToString());
            Assert.AreEqual("(B, b)-->E", hopcroftedDfa.Transitions.ToList()[3].ToString());
            Assert.AreEqual("(C, a)-->C", hopcroftedDfa.Transitions.ToList()[4].ToString());
            Assert.AreEqual("(C, b)-->C", hopcroftedDfa.Transitions.ToList()[5].ToString());
            Assert.AreEqual("(D, a)-->B", hopcroftedDfa.Transitions.ToList()[6].ToString());
            Assert.AreEqual("(D, b)-->D", hopcroftedDfa.Transitions.ToList()[7].ToString());
            Assert.AreEqual("(E, a)-->B", hopcroftedDfa.Transitions.ToList()[8].ToString());
            Assert.AreEqual("(E, b)-->A", hopcroftedDfa.Transitions.ToList()[9].ToString());

            Assert.AreEqual("D", hopcroftedDfa.FinalStates.ToList()[0]);
            Assert.AreEqual("E", hopcroftedDfa.FinalStates.ToList()[1]);

            Assert.AreEqual("A", hopcroftedDfa.StartStates.ToList()[0]);
        }