zoukankan      html  css  js  c++  java
  • 【算法导论】--C++实现随机生成100个顶点的无向图和有向图

    一、题目

    算法实验一(主要是为之后的图算法做前期准备工作)

    ->生成100个顶点的图,随机生成顶点

    ->无向图大约1000条边

    ->有向图大约2000条边

    ->计算每个顶点的度

    ->首先默认每条边的权重为1,随之后的实验内容再进行修改

    二、实现

    使用邻接矩阵的方式存储该图。

    三、代码(具体解释都在注释中)

      1 /*****************************************************************************
      2 *算法实验一;
      3 *->生成100个顶点的图,随机生成顶点
      4 *->无向图大约1000条边
      5 *->有向图大约2000条边
      6 *->计算每个顶点的度
      7 *****************************************************************************/
      8 #include<iostream>
      9 #include<cmath>
     10 #include<ctime>
     11 using namespace std;
     12 
     13 #define maxvertexnum 100 /*最大顶点数设为100*/
     14 #define infinity 2147483647 /*当两点不连通时,则它们之间距离为最大值*/
     15 typedef int vertextype;/*顶点的类型为整型*/
     16 typedef int edgetype;/*边的权重为整形*/
     17 
     18 enum GraphType{DG,UG};/*有向图,无向图*/
     19 struct MGraph//邻接矩阵表示法的结构和类型声明
     20 {
     21     vertextype V[maxvertexnum];//顶点集
     22     edgetype E[maxvertexnum][maxvertexnum];//边集
     23     int n,e;//顶点数,边数
     24     enum GraphType GType;//设定图的类型
     25 };
     26 
     27 int randomnum()//返回一个0-99之间的随机数
     28 {
     29     int a;
     30     //srand(time(NULL));//设置随机数种子,使每次运行获取的随机数不同
     31     a=rand()%100;
     32 
     33     return a;
     34 }
     35 int* countedge(MGraph *G)//函数返回一个数组
     36 {
     37     int i,j,countnum=0;
     38     int *sum=new int[100];
     39     for(i=0;i<G->n;i++)
     40     {
     41         countnum=0;//到达下一个顶点时置0
     42         for(j=0;j<G->n;j++)
     43         {
     44             if(G->E[i][j]==1)
     45             {
     46                 countnum+=1;
     47                 sum[i]=countnum;//计算每一个顶点的度
     48             }
     49         }
     50     }
     51     return sum;
     53 }
     54 
     55 void CreateMGraphUG(MGraph *G)//生成无向图
     56 {
     57     int i,j,k,w;
     58     G->GType=UG;//无向图
     59     G->n=100;
     60     G->e=1000;
     61     for(i=0;i<G->n;i++)
     62     {
     63         G->V[i]=i;//为每个顶点都编号从0-99
     64     }
     65 
     66     for(i=0;i<G->n;i++)
     67     {
     68         for(j=0;j<G->n;j++)
     69         {
     70             G->E[i][j]=0;//初始化邻接矩阵
     71         }
     72     }
     73 
     74     for(k=0;k<G->e;k++)
     75     {
     76         i=randomnum();//随机生成任意两顶点
     77         j=randomnum();
     78         //cout<<i<<" "<<j<<"	";
     79         G->E[i][j]=1;//建立邻接矩阵,边的权重都为1
     80         G->E[j][i]=1;//因为是无向图,所以要对称
     81     }
     82 
     83 }
     84 
     85 void CreateMGraphDG(MGraph *G)//生成有向图
     86 {
     87     int i,j,k,w;
     88     G->GType=DG;//有向图
     89     G->n=100;
     90     G->e=2000;
     91     for(i=0;i<G->n;i++)
     92     {
     93         G->V[i]=i;//为每个顶点都编号从0-99
     94     }
     95 
     96     for(i=0;i<G->n;i++)
     97     {
     98         for(j=0;j<G->n;j++)
     99         {
    100             G->E[i][j]=infinity;//初始化邻接矩阵
    101         }
    102     }
    103 
    104     for(k=0;k<G->e;k++)
    105     {
    106         i=randomnum();//随机生成任意两顶点
    107         j=randomnum();
    108         G->E[i][j]=1;//建立邻接矩阵,边的权重都为1
    109     }
    110 
    111 }
    112 
    113 int main()
    114 {
    115     MGraph *UGG=new MGraph;//无向图
    116     MGraph *DGG=new MGraph;//有向图
    117     int *ug=new int[100];
    118     int *dg=new int[100];
    119 
    120     cout<<"在无向图中"<<endl;
    121     CreateMGraphUG(UGG);
    122     ug=countedge(UGG);
    123     for(int i=0;i<100;i++)
    124     {
    125         cout<<"顶点 【"<<UGG->V[i]<<"】的度为"<<ug[i]<<endl;
    126     }
    127 
    128     cout<<"在有向图中"<<endl;
    129     CreateMGraphUG(DGG);
    130     dg=countedge(DGG);
    131     for(int i=0;i<100;i++)
    132     {
    133         cout<<"顶点 【"<<DGG->V[i]<<"】的度为"<<dg[i]<<endl;
    134     }
    135 
    136     return 0;
    137 }

    上述代码还有很多需要改进之处,期待指正和探讨。

    于2017年4月3日编辑完成。

  • 相关阅读:
    搬家通知
    URL tailing slash
    HowTo: Linux Server Change OR Setup The Timezone
    [引] Security tips for web developers
    [转] Finding the Best Programmer's Font
    Recovering deleted Records
    How to stop uwsgi when no pidfile in config?
    [uwsgi] no request plugin is loaded, you will not be able to manage requests.
    Debian Environment Variables
    Memcached vs. MongoDB vs. Redis Comparison
  • 原文地址:https://www.cnblogs.com/mowangshiyiyi316/p/6661541.html
Copyright © 2011-2022 走看看