// Write a program to convert NFA into an equivalent DFA

#include <iostream>
#include <iomanip>
#include <vector>
#include <unordered_map>
#include <unordered_set>
#include <queue>
using namespace std;

class NFA
{
public:
  vector<char> states;
  vector<char> inputSymbols;
  unordered_map<char, unordered_map<char, vector<char>>> transitionFn;
  char startState;
  vector<char> finalStates;

  NFA(vector<char> &states, vector<char> &inputSymbols, unordered_map<char, unordered_map<char, vector<char>>> &transitionFn, char &startState, vector<char> &finalStates)
  {
    this->states = states;
    this->inputSymbols = inputSymbols;
    this->transitionFn = transitionFn;
    this->startState = startState;
    this->finalStates = finalStates;
  }
};

class DFA
{
public:
  vector<int> states;
  vector<char> inputSymbols;
  unordered_map<int, unordered_map<char, int>> transitionFn;
  int startState;
  vector<int> finalStates;
};

unordered_map<char, unordered_set<char>> findeClosure(NFA &nfa)
{
  unordered_map<char, unordered_set<char>> eClosure;
  for (char c : nfa.states)
  {
    queue<char> q;
    unordered_set<char> visited;
    q.push(c);
    visited.insert(c);
    while (!q.empty())
    {
      char srcState = q.front();
      q.pop();
      vector<char> destStates = nfa.transitionFn[srcState]['$'];
      for (char destState : destStates)
      {
        if (visited.find(destState) == visited.end())
        {
          visited.insert(destState);
          q.push(destState);
        }
      }
    }
    for (auto eReachableState : visited)
    {
      eClosure[c].insert(eReachableState);
    }
  }

  return eClosure;
}

void printeClosure(NFA &nfa, unordered_map<char, unordered_set<char>> &eClosure)
{
  cout << "Epsilon Closures are:";
  for (char state : nfa.states)
  {
    cout << "\nState " << state << ": ";
    for (char c : eClosure[state])
    {
      cout << c << " ";
    }
  }
}

void printMappings(unordered_map<int, unordered_set<char>> &mappedStates)
{
  cout << "\n\nMapped states are:\n";
  cout << "DFA == NFA\n";
  int n = mappedStates.size();
  for (int i = 0; i < n; i++)
  {
    cout << i << " == ";
    for (auto c : mappedStates[i])
    {
      cout << c << ", ";
    }
    cout << "\b\b \n";
  }
}

DFA convertNFAtoDFA(NFA &nfa)
{
  unordered_map<char, unordered_set<char>> eClosure = findeClosure(nfa);
  printeClosure(nfa, eClosure);

  DFA dfa;
  dfa.inputSymbols = nfa.inputSymbols;
  dfa.startState = 0;
  int noOfDFAStates = 1;
  unordered_map<int, unordered_set<char>> mappedStates;
  queue<int> q;
  q.push(0);
  mappedStates[0] = eClosure[nfa.startState];

  while (!q.empty())
  {
    int dfaState = q.front();
    q.pop();
    for (char symbol : nfa.inputSymbols)
    {
      unordered_set<char> reach;
      unordered_set<char> nfaStates = mappedStates[dfaState];
      for (char srcState : nfaStates)
      {
        vector<char> destStates = nfa.transitionFn[srcState][symbol];
        for (char destState : destStates)
        {
          unordered_set<char> s = eClosure[destState];
          reach.insert(s.begin(), s.end());
        }
      }

      int found = -1;
      for (int i = 0; i < noOfDFAStates; i++)
      {
        if (mappedStates[i] == reach)
        {
          found = i;
          break;
        }
      }
      if (found != -1)
      {
        dfa.transitionFn[dfaState][symbol] = found;
      }
      else
      {
        dfa.transitionFn[dfaState][symbol] = noOfDFAStates;
        mappedStates[noOfDFAStates] = reach;
        q.push(noOfDFAStates);
        noOfDFAStates++;
      }
    }
  }

  printMappings(mappedStates);
  for (int i = 0; i < noOfDFAStates; i++)
  {
    dfa.states.push_back(i);
  }

  auto &states1 = nfa.finalStates;
  for (int i = 0; i < noOfDFAStates; i++)
  {
    auto &states2 = mappedStates[i];
    for (auto c1 : states1)
    {
      if (states2.find(c1) != states2.end())
      {
        dfa.finalStates.push_back(i);
        break;
      }
    }
  }

  return dfa;
}

void printNFA(NFA &nfa)
{
  int width = 6;
  cout << "Given NFA:\n";
  cout << "States: ";
  for (char c : nfa.states)
  {
    cout << c << ", ";
  }

  cout << "\b\b \nInput symbols: ";
  for (char c : nfa.inputSymbols)
  {
    cout << c << ", ";
  }

  cout << "\b\b \nTransition Function:\n";
  cout << setw(width) << "";
  for (char c : nfa.inputSymbols)
  {
    cout << setw(width) << c;
  }
  cout << setw(width) << "$" << endl;
  for (char srcState : nfa.states)
  {
    cout << setw(width) << srcState;
    for (char symbol : nfa.inputSymbols)
    {
      vector<char> destStates = nfa.transitionFn[srcState][symbol];
      string str = "";
      for (char state : destStates)
      {
        str += state;
      }
      cout << setw(width) << str;
    }
    vector<char> destStates = nfa.transitionFn[srcState]['$'];
    string str = "";
    for (char state : destStates)
    {
      str += state;
    }
    cout << setw(width) << str << endl;
  }

  cout << "Start State: " << nfa.startState;
  cout << "\nFinal States: ";
  for (char c : nfa.finalStates)
  {
    cout << c << ", ";
  }
  cout << "\b\b \n\n";
}

void printDFA(DFA &dfa)
{
  int width = 6;
  cout << "\n\nThe converted DFA is:\n";
  cout << "States: ";
  for (int i : dfa.states)
  {
    cout << i << ", ";
  }

  cout << "\b\b \nInput symbols: ";
  for (char c : dfa.inputSymbols)
  {
    cout << c << ", ";
  }

  cout << "\b\b \nTransition Function:\n";
  cout << setw(width) << "";
  for (char c : dfa.inputSymbols)
  {
    cout << setw(width) << c;
  }
  for (int srcState : dfa.states)
  {
    cout << endl
         << setw(width) << srcState;
    for (char symbol : dfa.inputSymbols)
    {
      int destState = dfa.transitionFn[srcState][symbol];
      cout << setw(width) << destState;
    }
  }

  cout << "\nStart State: " << dfa.startState;
  cout << "\nFinal States: ";
  for (int c : dfa.finalStates)
  {
    cout << c << ", ";
  }
  cout << "\b\b \n\n";
}

int main()
{
  cout << "---Conversion of epsilon-NFA to DFA---\n";
  cout << "---Note: Epsilon has been denoted by $ in this program---\n\n";

  vector<char> states = {'A', 'B', 'C'};
  vector<char> inputSymbols = {'0', '1'};
  unordered_map<char, unordered_map<char, vector<char>>> transitionFn = {
      {'A', {{'0', {'B', 'C'}}, {'1', {'A'}}, {'$', {'B'}}}},
      {'B', {{'0', {}}, {'1', {'B'}}, {'$', {'C'}}}},
      {'C', {{'0', {'C'}}, {'1', {'C'}}, {'$', {}}}},
  };
  char startState = 'A';
  vector<char> finalStates = {'C'};

  NFA nfa(states, inputSymbols, transitionFn, startState, finalStates);
  printNFA(nfa);
  DFA dfa = convertNFAtoDFA(nfa);
  printDFA(dfa);
}

// Example-1
// vector<char> states = {'A', 'B', 'C'};
// vector<char> inputSymbols = {'0', '1'};
// unordered_map<char, unordered_map<char, vector<char>>> transitionFn = {
// 	{'A', {{'0', {'B', 'C'}}, {'1', {'A'}}, {'$', {'B'}}}},
// 	{'B', {{'0', {}}, {'1', {'B'}}, {'$', {'C'}}}},
// 	{'C', {{'0', {'C'}}, {'1', {'C'}}, {'$', {}}}},
// };
// char startState = 'A';
// vector<char> finalStates = {'C'};

// Example-2
// vector<char> states = {'0', '1', '2'};
// vector<char> inputSymbols = {'a', 'b'};
// unordered_map<char, unordered_map<char, vector<char>>> transitionFn = {
// 	{'0', {{'a', {'0'}}, {'b', {'0','1'}}}},
// 	{'1', {{'a', {}}, {'b', {'2'}}}},
// 	{'2', {{'a', {}}, {'b', {}}}},
// };
// char startState = '0';
// vector<char> finalStates = {'2'};

// Example-3
// vector<char> states = {'0', '1', '2', '3'};
// vector<char> inputSymbols = {'a', 'b'};
// unordered_map<char, unordered_map<char, vector<char>>> transitionFn = {
// 	{'0', {{'a', {'0','1'}}, {'b', {'0'}}}},
// 	{'1', {{'a', {'2'}}, {'b', {'1'}}}},
// 	{'2', {{'a', {'3'}}, {'b', {'3'}}}},
// 	{'3', {{'a', {}}, {'b', {'2'}}}}
// };
// char startState = '0';
// vector<char> finalStates = {'2','3'}; 

C++ Online Compiler

Write, Run & Share C++ code online using OneCompiler's C++ online compiler for free. It's one of the robust, feature-rich online compilers for C++ language, running on the latest version 17. Getting started with the OneCompiler's C++ compiler is simple and pretty fast. The editor shows sample boilerplate code when you choose language as C++ and start coding!

Read inputs from stdin

OneCompiler's C++ online compiler supports stdin and users can give inputs to programs using the STDIN textbox under the I/O tab. Following is a sample program which takes name as input and print your name with hello.

#include <iostream>
#include <string>
using namespace std;

int main() 
{
    string name;
    cout << "Enter name:";
    getline (cin, name);
    cout << "Hello " << name;
    return 0;
}

About C++

C++ is a widely used middle-level programming language.

  • Supports different platforms like Windows, various Linux flavours, MacOS etc
  • C++ supports OOPS concepts like Inheritance, Polymorphism, Encapsulation and Abstraction.
  • Case-sensitive
  • C++ is a compiler based language
  • C++ supports structured programming language
  • C++ provides alot of inbuilt functions and also supports dynamic memory allocation.
  • Like C, C++ also allows you to play with memory using Pointers.

Syntax help

Loops

1. If-Else:

When ever you want to perform a set of operations based on a condition If-Else is used.

if(conditional-expression) {
   //code
}
else {
   //code
}

You can also use if-else for nested Ifs and If-Else-If ladder when multiple conditions are to be performed on a single variable.

2. Switch:

Switch is an alternative to If-Else-If ladder.

switch(conditional-expression){    
case value1:    
 // code    
 break;  // optional  
case value2:    
 // code    
 break;  // optional  
......    
    
default:     
 code to be executed when all the above cases are not matched;    
} 

3. For:

For loop is used to iterate a set of statements based on a condition.

for(Initialization; Condition; Increment/decrement){  
  //code  
} 

4. While:

While is also used to iterate a set of statements based on a condition. Usually while is preferred when number of iterations are not known in advance.

while (condition) {  
// code 
}  

5. Do-While:

Do-while is also used to iterate a set of statements based on a condition. It is mostly used when you need to execute the statements atleast once.

do {  
 // code 
} while (condition); 

Functions

Function is a sub-routine which contains set of statements. Usually functions are written when multiple calls are required to same set of statements which increases re-usuability and modularity. Function gets run only when it is called.

How to declare a Function:

return_type function_name(parameters);

How to call a Function:

function_name (parameters)

How to define a Function:

return_type function_name(parameters) {  
 // code
}