public DFAMap createDFA() { DFAMap DFA = new DFAMap(); DFA.Alphabet = NFA.Alphabet; //use alphabet from NFA DFA.startingState = NFA.startingState; //use alphabet from NFA //Create first state in DFA DFAElement first = new DFAElement(); DFAMove move = new DFAMove(); first.states.Add(DFA.startingState); //Add each possible move foreach (var c in DFA.Alphabet) { move = new DFAMove(); move.letter = c; first.possibleMoves.Add(move); } //Determine where the moves can end up foreach (var DFAelement in first.possibleMoves) { foreach (var NFAelement in NFA.AllStates) { if (NFAelement.state == first.states[0]) { foreach (var posMove in NFAelement.possibleMoves) { if (posMove.letter == DFAelement.letter) { DFAelement.endingStates.Add(posMove.endingState); } } } } } DFA.AllStates.Add(first); //Add the first state into the DFA states //Check if any new elements should be made, and then make them CheckAndCreateDFAElement(DFA); //Update the total number of states foreach (var item in DFA.AllStates) { DFA.totalNumStates += 1; } //Add the accepted states DFA.acceptedStates = NFA.acceptedStates; return(DFA); }
public void CheckAndCreateDFAElement(DFAMap DFA) { //Look for elements to add bool foundNewElemenet = false; DFAElement elementToAdd = new DFAElement(); DFAElement dummy = new DFAElement(); //Checks if the DFA element exists ...Sorry it's confusing foreach (var DFAelement in DFA.AllStates) { foreach (var moveOption in DFAelement.possibleMoves) { bool check = false; foreach (var dfaElement in DFA.AllStates) { if (dfaElement.states.SequenceEqual(moveOption.endingStates)) { check = true; continue; } } if (check == false) { foundNewElemenet = true; elementToAdd.states = moveOption.endingStates; break; } check = false; } if (foundNewElemenet) { break; } } //end the recursion when there are no more elements left to find if (!foundNewElemenet) { return; } //Element to add was found, so add it //Add each possible move DFAMove move; foreach (var c in DFA.Alphabet) { move = new DFAMove(); move.letter = c; elementToAdd.possibleMoves.Add(move); } //Create new element foreach (var DFAelement in elementToAdd.possibleMoves) //each move possibilty from first state { foreach (var NFAelement in NFA.AllStates) { foreach (var state in elementToAdd.states) { if (NFAelement.state == state) { foreach (var posMove in NFAelement.possibleMoves) { if (posMove.letter == DFAelement.letter) { DFAelement.endingStates.Add(posMove.endingState); } } } } } } DFA.AllStates.Add(elementToAdd); //Add new element to the DFA list //Recursively check if there are more states to add and then add them CheckAndCreateDFAElement(DFA); }