Implementation of deterministic code for DFA conversion to NFA DFA

Source: Internet
Author: User
#include <iostream> #include <map> #include <set> #include <queue> #include <cstdlib> #

Include <cstring> #include <cstdio> using namespace std;
const int maxn=100;
int VIS[MAXN]; Vector<set<int> >c;//constructed subset map<set<int>,int> flag;//subset is marked map<int,map<int,map<
char,int> > > NFA;
    struct DFA {int st;
    Char W;
int Ed;
};

Vector<dfa> DFA;
    int n,m;//State number and number of arcs//C-ARC conversion of the state set T set<int> Move (set<int> T,char c) {set<int> U;
        For (Set<int>::iterator I=t.begin (); I!=t.end (); i++) {int u=*i;
        for (int j=0;j<n;j++) {if (nfa[u][j][c]==1) U.insert (j);
}} return U;
    }//E-closure of the state set T set<int> eclosure (set<int> t) {set<int> U;
    Queue<int> Q;
        For (Set<int>::iterator I=t.begin (); I!=t.end (); i++) {U.insert (*i);
    Q.push (*i); //bfs look for an e-arc that can be reached by any of the barsState memset (vis,0,sizeof (VIS)); while (!
        Q.empty ()) {int V=q.front ();
        Q.pop (); for (int i=0;i<n;i++) {if (nfa[v][i][' E ']&&vis[i]==0) {U.insert
                (i);
                Q.push (i);
            Vis[i]=1;
}}} return U;
    } int main () {freopen ("Input.txt", "R", stdin);
    cin>>n>>m;
    n++;
        Constructs a state diagram for (int i=0;i<m;i++) {int u,v;
        Char W;
        cin>>u>>v>>w;
    Nfa[u][v][w]=1;
    } int k0=0;//k0 Set<int> temp;
    Temp.insert (K0);
    Temp=eclosure (temp);
    C.push_back (temp);
        Subset Construction algorithm while (1) {int st=-1;
        Set<int> T;
                Determine if there is a marked subset of T for (int i=0;i<c.size (); i++) {if (!flag[c[i]) {
                st=i;
                T=c[i];
            Break
          }} if (St==-1)  Break
        flag[t]=1;//Mark T set<int> u=eclosure (Move (t, ' a '));
        if (!flag[u]) {c.push_back (U);
                }//lazy to write the conversion function, here to get the conversion result for (int i=0;i<c.size (); i++) {if (U==c[i]) {
                Dfa.push_back (dfa{st, ' A ', i});
            Break
        }} u=eclosure (Move (T, ' B '));
        if (!flag[u]) {c.push_back (U); } for (int i=0;i<c.size (); i++) {if (U==c[i]) {Dfa.push_back (DFA
                {St, ' B ', i});
            Break
    }}}//A subset of output constructs cout<< "<<c.size () <<" subset: "<<endl;
        for (int i=0;i<c.size (); i++) {set<int> t=c[i];
        cout<< "T" <<i<< "={"; For (Set<int>::iterator J=t.begin (); J!=t.end (); j + +) {if (J!=t.begin ()) COUT&LT;&L
            t; ', '; cout<<*j;
    } cout<< "}" <<endl;
    } cout<<endl;
    cout<< "constructs the DFA m as follows:" <<endl<<endl;
    cout<< "s={";
        for (int i=0;i<c.size (); i++) {if (i!=0) cout<< ",";
    cout<< "[T" <<i<< "]";
    } cout<< "}" <<endl<<endl; for (int i=0;i<dfa.size (); i++) {cout<< "D ([T" <<dfa[i].st<< "]," <<dfa[i].w<< ") =[
    T "<<dfa[i].ed<<"] "<<endl;
} return 0;
 }

Enter the test data input.txt format as follows

0 1 E
1 2 e
2 3 a
3 6 e
1 4 e
4 5 B
5 6 e
6 1 E
6 7 E
7 8 a
8 9 b
9 B
0 7 E

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.