Random generation of graphs and the judging of Euler's (back) road The discrete experiment of South Mail

Source: Internet
Author: User
Tags rand

Enter m nodes, n edges, randomly generate graphs, and determine whether it is Eulerian or half Eulerian.


#include <iostream> #include <ctime> #include <cstdlib> #include <cstring> #include <iomanip        > #define MAX 1000 int A[max][max];     Matrix int B[max] of graphs;    The degree bool Visit[max],ff=true of each node is recorded;     Whether the record has been accessed int euler[max],ans[max];    Euler Road int m,n;    M is the number of nodes, N is the number of bars int C[max][max];   Determine whether the side has traversed void randcreat (); Randomly generating graphs, calculating the degrees of each node, and outputting the Matrix void DFS (int x);       Depth-First search determines whether the connected graph bool Judge_connect ();     To determine whether Unicom int judge_euler ();     Determine if the Euler (back) path void Find_euler (int x,int y);
    Start with X to find if there is Euler (back) path void Clearmap () {ff=true;
    memset (b,0,max*sizeof (int));
    memset (euler,-1,max*sizeof (int));
    memset (c,0,max*sizeof (int));
        for (int i=0;i<m;i++) {visit[i]=false;
            for (int j=0;j<m;j++) {a[i][j]=0;
        c[i][j]=0;

using namespace Std;
    int main () {memset (euler,-1,max*sizeof (int));
        while (1) {cout<< "Please enter the number of nodes and the number of edges in order" <<endl; Cin>>m>>n; if (n> (m* (m-1)/2)) {cout<<] The number of input edges is too large.
            "<<endl;
        Continue
        } randcreat ();
        if (Judge_connect ()) cout<< "is connected graph" <<endl;
            else {cout<< "Not connected graph" <<endl;
            Clearmap ();
        Continue
        for (int i=0;i<m;i++) cout<< "" <<b[i]<< ";
        cout<<endl;
        Ff=true;
        Judge_euler ();
        /* for (int j=0;j<=n;j++) COUT&LT;&LT;SETW (3) <<euler[j];
        cout<<endl;
        * * CLEARMAP ();
    cout<< "***********************" <<endl<<endl;
return 0;  } void Randcreat () {int ra,rb;    Two random number int count=0;
    Only the M-bar, count to record the number of Srand (time (0)) currently generated;
        while (count<n) {Ra=rand ()%m;
        Rb=rand ()%m;
        while (RA==RB) Rb=rand ()%m;
  if (!a[ra][rb]) {          A[ra][rb]=a[rb][ra]=1;
            count++;
            b[ra]++;
        b[rb]++; (int i=0;i<m;i++) {for (int j=0;j<m;j++) cout<< "" <<a[i][j]<&
        lt; "";
    cout<<endl;
    } void DFS (int x) {visit[x]=true;
for (int i=0;i<m;i++) if (!visit[i]&&a[x][i]) DFS (i);
    BOOL Judge_connect () {DFS (0);
    for (int i=0;i<m;i++) if (!visit[i]) return false;
return true;  int Judge_euler () {int first=0;  Record the recursive starting point int num=0;
            Number of odd degrees nodes for (int i=0;i<m;i++) if (b[i]%2) {i;
        num++; } if (num==1| | NUM&GT;2) cout<< "not Eulerian or half Eulerian."
    "<<endl;         else {euler[0]=first;
        Euler[] Record node Find_euler (first,1);
        if (num==0) cout<< "Eulerian" <<endl;
        else cout<< "Semi Eulerian" <<endl; For(int k=0;k<=n;k++)
        {COUT&LT;&LT;SETW (3) <<ans[k];
    } cout<<endl<<endl; } void Find_euler (int x,int y) {if (euler[n]!=-1) {if (ff) {for (int k=0;k<=n;k++
            ) Ans[k]=euler[k];
        Ff=false;
    } return;
            for (int i=0;i<m;i++) {if (A[x][i]&&!c[x][i]) {euler[y]=i;
            C[x][i]=c[i][x]=1;
            Find_euler (i,y+1);
            if (!FF)//ff to mark the need to unmark if a Oralu is found to return directly;
        else c[x][i]=c[i][x]=0;
 }
    }
}

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.