Graph theory Small System

Source: Internet
Author: User

The knowledge of their own to integrate a bit, made a small graph theory system.

For knowledge, see: http://blog.csdn.net/column/details/tulun.html

#include <iostream> #include <stdio.h> #include <stdlib.h> #include <string.h> #include < algorithm> #include <math.h> #include <queue> #include <stack> #include <windows.h>using Namespace Std;typedef long long ll;const int oo=1e9;/**oo means infinity */const int mm=1111111;/**mm represents the maximum number of edges, remembering that if twice times the original, it is Bidirectional */const int MN=2010;/**MN represents the maximum number of points */int Head[mn];int Ip;int n,m;///n points, M-bar int rd[mn],rdd[mn];///in degrees int cd[mn];///    Out of the degree int dd[999][999],dist[mn];///any two points away from the struct data{int to,w,next;} tu[mm];void init ()///Initialize {ip=0;    memset (head,-1,sizeof (head));    memset (cd,0,sizeof (CD));    memset (rd,0,sizeof (RD));    memset (rdd,0,sizeof (RDD)); for (int i=1, i<=n; i++) for (int j=1; j<=n; j + +) Dd[i][j]=i==j?0:oo;} void Add (int u,int v,int W)///add edge {tu[ip].w=w,tu[ip].to=v,tu[ip].next=head[u],head[u]=ip++;}    void input (int flag)///Graph input {cout<<n<< "dot" <<m<< "sidebar" <<endl; cout<< "Please enter this" <<m<< "Edges and their weights (note that the sequence of n points is 1 to "<<n<<"!)    ) "<<endl;    cout<< "Example input: 1 2 3 (indicates that point 1 and point 2 have a weighted 3 edge)" <<endl;        for (int i=0; i<m; i++) {int a,b,c;        cout<< "section" <<i+1<< "Edge:";        cin>>a>>b>>c; while (a<1| | a>n| | b<1| | B>n) {cout<< "Please re-enter the correct vertex!! (1 to "<<n<<"!)            ) "<<endl;        cin>>a>>b>>c;        } if (flag) Add (A,B,C);        else Add (a,b,c), add (B,A,C);        rd[b]++;        rdd[b]++;    cd[a]++; } cout<< "Build complete!    Jumping to ... "<<endl;". "    Sleep (1000); System ("CLS");}    void Floyd ()///multi-source Shortest Path {System ("CLS");    for (int i=1, i<=n; i++) for (int j=head[i]; j!=-1; j=tu[j].next) DD[I][TU[J].TO]=TU[J].W; for (int k=1, k<=n; k++) for (int. I=1; i<=n; i++) for (int j=1; j<=n; j + +) Dd[i][j]    =min (Dd[i][j],dd[i][k]+dd[k][j]); while (1) {COUT&LT;&Lt; "        Please enter any two points (1 to "<<n<<") (enter two identical numbers to return) "<<endl;        int ss,ee;        cout<< "Please enter the first point";        cin>>ss; while (ss<1| |            Ss>n) {cout<< "Please re-enter the number between 1 and" <<n<< "<<endl;        cin>>ss;        } cout<< "Please enter a second point";        cin>>ee; while (ee<1| |            Ee>n) {cout<< "Please re-enter the number between 1 and" <<n<< "<<endl;        cin>>ee; if (Ss!=ee) {if (Dd[ss][ee]<oo) cout<< "points" <<ss<< "point" <<e            e<< "The shortest distance between is" <<dd[ss][ee]<<endl<<endl;        else cout<< "point" <<ss<< "unreachable point" <<ee<<endl<<endl;    } else break;    }}void SPFA ()///single source shortest circuit {System ("CLS");        while (1) {cout<< "Please enter the desired point (1 to" <<n<< ", 0 means return)" <<endl;        int s;        cin>>s; while (s<0| |        S>n) {    cout<< "Please retype the number between 1 and <<n<<" <<endl;        cin>>s;        } if (s==0) break;        queue<int>q;        for (int i=0; i<=n; i++) Dist[i]=oo;        int vis[mn]= {0};        Q.push (s);        dist[s]=0;            while (!q.empty ()) {int H=q.front ();            Q.pop ();            vis[h]=0;                for (int i=head[h]; i!=-1; i=tu[i].next) {int v=tu[i].to;                int W=TU[I].W;                    if (dist[v]>dist[h]+w) {dist[v]=dist[h]+w;                        if (!vis[v]) {vis[v]=1;                    Q.push (v);                }}}} for (int i=1; i<=n; i++) if (i!=s) {                if (dist[i]>=oo) cout<<s<< "point unreachable" <<i<< "points" <<endl; else Cout<<s<< the shortest distance from point to <<i<< Point is <<dist[i]<<endl; }}}int Dfn[mn], low[mn];///dfn[] indicates the number of steps in deep search, low[u] means that the subtree of U or u can be traced back to the earliest stack of nodes in the order number int sccno[mn];///The array of vertices, representing a point corresponding to the indentation value of int step;    int scc_cnt;///The number of strong connected components vector<int> scc[mn];///out of the contraction point, scc[i] inside the indentation of I this indent specifically what points stack<int> s;void dfs (int u) {    Dfn[u] = low[u] = ++step;    S.push (U);        for (int i = head[u]; i!=-1; i=tu[i].next) {int v = tu[i].to;            if (!dfn[v]) {DFS (v);        Low[u] = min (Low[u], low[v]);    } else if (!sccno[v]) low[u] = min (Low[u], dfn[v]);        } if (low[u] = = Dfn[u]) {scc_cnt + = 1;        Scc[scc_cnt].clear ();            while (1) {int x = S.top ();            S.pop ();            if (sccno[x]! = scc_cnt) scc[scc_cnt].push_back (x);            SCCNO[X] = scc_cnt;        if (x = = u) break;    }}}void Tarjan () {System ("CLS");    for (int i=0; i<=n; i++) scc[i].clear (); memset (sccno, 0, siZeof (SCCNO));    memset (DFN, 0, sizeof (DFN));    Step = scc_cnt = 0;    for (int i = 1; I <=n; i++) if (!dfn[i]) DFS (i);    cout<< "The strong connected component of the graph has a" <<scc_cnt<< ", you can indent the original image into this" <<scc_cnt<< "point <<endl;        for (int i=1; i<=scc_cnt; i++) {int l=scc[i].size ();        for (int j=0; j<l; j + +) cout<<scc[i][j]<< ";    cout<< "This" <<l<< "dots indent" <<i<<endl;    } cout<< "Enter any character back" <<endl;    Step=getchar (); Step=getchar ();}    void Youshow ()///Show Graph function {cout<< "1. Traversal of a graph" <<endl;    cout<< "2. View the topological sort sequence for the graph" <<endl;    cout<< "3. Find out the maximum flow of the graph" <<endl;    cout<< "4. Find the shortest path between all points" <<endl;    cout<< "5. Find the shortest path to a point to another point" <<endl;    cout<< "6. Finding the strong connected component of the graph" <<endl; cout<< "0. Exit and re-create the diagram" &LT;&LT;ENDL; void Bianli ()///Graph traversal {for (int i=1; i<=n; i++) for (int j=head[i]; j>=0; j=tu[j].next) cout< <i<< "---> "<<tu[j].to<<" The weight is "&LT;&LT;TU[J].W&LT;&LT;ENDL;}    void Topu ()///topological sort {queue<int>q;    int flag=0;    for (int i=1; i<=n; i++) if (!rdd[i]) Q.push (i), flag=1;        if (!flag) {cout<< "The graph has formed a ring and cannot be sorted by topology" <<endl;    return;    } cout<< "The topological sort sequence of the graph is:" <<endl;        while (!q.empty ()) {int X=q.front ();        cout<<x<< ";        rdd[x]--;        Q.pop ();            for (int i=head[x]; i>=0; i=tu[i].next) {int to=tu[i].to;            rdd[to]--;            if (!rdd[to]) {Q.push (to);    }}} cout<<endl; for (int i=1; i<=n; i++) rdd[i]=rd[i];} int Node,src,dest,edge;/**node represents the number of nodes, SRC represents the source point, dest represents the meeting point, edge statistics edge Count */int hea[mn],ver[mm],flow[mm],nex[mm];int Work[mn],    dis[mn],q[mn];void Prepare (int _node, int _src,int _dest) {node=_node,src=_src,dest=_dest;    for (int i=0; i<=node; ++i) hea[i]=-1; edge=0;} void Addedge (int u, int v, int c) {ver[edge]=v,flow[edge]=c,nex[edge]=hea[u],hea[u]=edge++; ver[edge]=u,flow[edge]=0,nex[edge]=hea[v],hea[v]=edge++;}    BOOL Dinic_bfs () {int i,u,v,l,r=0;    for (i=0; i<node; ++i) dis[i]=-1;    dis[q[r++]=src]=0;            For (l=0, l<r; ++l) for (I=hea[u=q[l]]; i>=0; i=nex[i]) if (flow[i]&&dis[v=ver[i]]<0)                {dis[q[r++]=v]=dis[u]+1;            if (v==dest) return 1; } return 0;}    int Dinic_dfs (int u, int exp) {if (u==dest) return exp; for (int &i=work[u],v,tmp; i>=0; i=nex[i]) if (flow[i]&&dis[v=ver[i]]==dis[u]+1&& (tmp=dinic_            DFS (V,min (exp,flow[i))) >0) {flow[i]-=tmp;            flow[i^1]+=tmp;        return TMP; } return 0;}    int Dinic_flow () {int i,ret=0,delta;        while (DINIC_BFS ()) {for (i=0; i<node; ++i) work[i]=hea[i];    while ((Delta=dinic_dfs (Src,oo))) Ret+=delta; } return ret;} void Dinic ()/The maximum flow {prepare (n+2,0,n+1) is obtained;        for (int i=1; i<=n; i++) {if (!rd[i]) Addedge (0,I,1E9);    if (!cd[i]) Addedge (I,N+1,1E9);    } for (int i=1, i<=n; i++) for (int j=head[i]; j!=-1; j=tu[j].next) Addedge (I,TU[J].TO,TU[J].W); cout<< "The maximum flow that the graph can pass is" <<dinic_flow () <<endl;}    void Youxiangtu ()///Graph {System ("CLS");    Init ();    Input (1);        while (1) {System ("CLS");        Youshow (); cout<< "Please enter the corresponding number: (0 to 6!)        ): "<<endl;        int tt; while (CIN&GT;&GT;TT) {while (tt<0| | tt>6) {cout<< "Please re-enter the number from 0 to 6!!                "<<endl;            cin>>tt;            } if (!TT) return;            else if (tt==1) Bianli ();            else if (tt==2) Topu ();            else if (tt==3) dinic ();                else if (tt==4) {Floyd ();   Break         } else if (tt==5) {SPFA ();            Break                } else {Tarjan ();            Break    }}}}void Wushow ()///Show Graph function {cout<< "1. Traversal of the graph without direction" <<endl;    cout<< "2. Find the shortest path between all points" <<endl;    cout<< "3. Find the shortest path to a point to another point" <<endl;    cout<< "4. Finding the smallest spanning tree of this graph" <<endl;    cout<< "5. Finding the strong connected component of the graph" <<endl; cout<< "0. Exit and re-create the diagram" &LT;&LT;ENDL;    void Wsbianli ()///depth traversal of the graph {cout<< "the depth traversal sequence of the diagram is:" <<endl;    BOOL vis[mn]= {0};    stack<int>ss;    Ss.push (1);        while (!ss.empty ()) {int x=ss.top ();        cout<<x<< ";        Ss.pop ();        Vis[x]=1;            for (int i=head[x]; i!=-1; i=tu[i].next) {int to=tu[i].to;        if (!vis[to]) Ss.push (to), vis[to]=1; }} Cout<<endl<<endl;} void Wgbianli ()///non-graph breadth Traversal {cout<< "the breadth traversal sequence of the graph is:" <<endl;    BOOL vis[mn]= {0};    queue<int>q;    Q.push (1);        while (!q.empty ()) {int X=q.front ();        cout<<x<< ";        Q.pop ();        Vis[x]=1;            for (int i=head[x]; i!=-1; i=tu[i].next) {int to=tu[i].to;        if (!vis[to]) Q.push (to), vis[to]=1; }} Cout<<endl<<endl;}    void Wbianli ()///non-graph traversal {System ("CLS");        while (1) {cout<< "Please select Traverse Mode (enter 0 to 2):" <<endl;        cout<< "0. Return 1. Depth traversal 2. Breadth Traversal" <<endl;        int tt;        cin>>tt; while (tt>2| | tt<0) {cout<< "please re-enter 0 to 2!!            "<<endl;        cin>>tt;        } if (tt==1) Wsbianli ();        else if (tt==2) Wgbianli ();    else break;    }}struct tree{int a,b,w;    void sett (int x,int y,int z) {a=x,b=y,w=z; }} Ttu[mm];bool cmp (tree A,tree b) {return A.W&LT;B.W;}   int f[mn];int fa (int x) { if (f[x]!=x) return F[x]=fa (F[x]); return f[x];}    void Mintree ()///non-graph minimum spanning tree {System ("CLS");    int bj=0;        for (int i=1; i<=n; i++) {f[i]=i;    for (int j=head[i]; j!=-1; j=tu[j].next) Ttu[bj++].sett (I,TU[J].TO,TU[J].W);    } sort (ttu,ttu+bj,cmp);    cout<< "All sides of the least spanning tree for this graph are:" <<endl;    int sum=0,s=0;        for (int i=0; i<bj; i++) {int X1=fa (TTU[I].A), X2=FA (TTU[I].B); if (x1!=x2) {cout<<ttu[i].a<< "--->" <<ttu[i].b<< "Weight is" <<ttu[i].w<<e            Ndl            F[X1]=FA (f[x2]);            s++;        SUM+=TTU[I].W;    } if (s==n-1) break;    } cout<< "The total weight of the minimum spanning tree is" <<sum<<endl;    cout<< "Enter any character back" <<endl;    S=getchar (); S=getchar ();}    void Wuxiangtu ()///non-graphic {init ();    Input (0);        while (1) {System ("CLS");        Wushow (); cout<< "Please enter the corresponding number: (0 to 5!)        ): "<<endl;        int tt; while (CIN&GT;&GT;TT) {while (tt<0| | tt>5) {cout<< "Please re-enter the number from 0 to 5!!                "<<endl;            cin>>tt;            } if (!TT) return;                else if (tt==1) {Wbianli ();            Break                } else if (tt==2) {Floyd ();            Break                } else if (tt==3) {SPFA ();            Break                } else if (tt==4) {mintree ();            Break                } else {Tarjan ();            Break        }}}}int Main () {while (1) {System ("CLS");        cout<< "----------This is a graph theory small system----------" <<endl;        cout<< "Please enter the number of points to be built (enter 0 to end the program):";        cin>>n;        if (!n) break;        cout<< "Please enter the number of edges to be built:";        cin>>m; cout<< "is jumping ..." "<<end."L        Sleep (800);        System ("CLS");        cout<< "Please enter the corresponding number (return input 0):" <<endl;        cout<< "1. Direction diagram 2. No map" <<endl;        int tt;                while (CIN&GT;&GT;TT) {if (tt==1) {youxiangtu ();                System ("CLS");            Break                } else if (tt==2) {wuxiangtu ();            Break                } else if (tt==0) {System ("CLS");            Break } cout<< "Please enter 0,1,2!        "<<endl; }} return 0;}


Graph theory Small System

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.