第十一周项目二(1)

来源:互联网 发布:库里15 16赛季数据 编辑:程序博客网 时间:2024/06/07 23:41
/*
Copyright (c++) 2017,烟台大学计算机与控制工程学院
文件名称:jcy
作 者:贾存钰
完成日期:2017年11月11日
问题描述:假设图G采用邻接表存储,分别设计实现以下要求的算法: 
(1)输出出图G中每个顶点的出度; 
(2)求出图G中出度最大的一个顶点,输出该顶点编号; 
利用下图作为测试用图,输出结果。 


*/


[cpp] view plain copy
  1. #include <stdio.h>  
  2. #include <malloc.h>  
  3. #include "graph.h"  
  4.   
  5. //返回图G中编号为v的顶点的出度  
  6. int OutDegree(ALGraph *G,int v)  
  7. {  
  8.     ArcNode *p;  
  9.     int n=0;  
  10.     p=G->adjlist[v].firstarc;  
  11.     while (p!=NULL)  
  12.     {  
  13.         n++;  
  14.         p=p->nextarc;  
  15.     }  
  16.     return n;  
  17. }  
  18. //输出图G中每个顶点的出度  
  19. void OutDs(ALGraph *G)  
  20. {  
  21.     int i;  
  22.     for (i=0; i<G->n; i++)  
  23.         printf("  顶点%d:%d\n",i,OutDegree(G,i));  
  24. }  
  25. int main()  
  26. {  
  27.     ALGraph *G;  
  28.     int A[7][7]=  
  29.     {  
  30.         {0,1,1,1,0,0,0},  
  31.         {0,0,0,0,1,0,0},  
  32.         {0,0,0,0,1,1,0},  
  33.         {0,0,0,0,0,0,1},  
  34.         {0,0,0,0,0,0,0},  
  35.         {0,0,0,1,1,0,1},  
  36.         {0,1,0,0,0,0,0}  
  37.     };  
  38.    ArrayToList(A[0], 7, G);  
  39.     printf("(1)各顶点出度:\n");  
  40.     OutDs(G);  
  41.     return 0;  
  42. }  



[cpp] view plain copy
  1. #include <stdio.h>  
  2. #include <malloc.h>  
  3. #include "graph.h"  
  4.   
  5. //返回图G中编号为v的顶点的出度  
  6. int OutDegree(ALGraph *G,int v)  
  7. {  
  8.     ArcNode *p;  
  9.     int n=0;  
  10.     p=G->adjlist[v].firstarc;  
  11.     while (p!=NULL)  
  12.     {  
  13.         n++;  
  14.         p=p->nextarc;  
  15.     }  
  16.     return n;  
  17. }  
  18. //输出图G中出度最大的一个顶点  
  19. void OutMaxDs(ALGraph *G)  
  20. {  
  21.     int maxv=0,maxds=0,i,x;  
  22.     for (i=0; i<G->n; i++)  
  23.     {  
  24.         x=OutDegree(G,i);  
  25.         if (x>maxds)  
  26.         {  
  27.             maxds=x;  
  28.             maxv=i;  
  29.         }  
  30.     }  
  31.     printf("顶点%d,出度=%d\n",maxv,maxds);  
  32. }int main()  
  33. {  
  34.     ALGraph *G;  
  35.     int A[7][7]=  
  36.     {  
  37.         {0,1,1,1,0,0,0},  
  38.         {0,0,0,0,1,0,0},  
  39.         {0,0,0,0,1,1,0},  
  40.         {0,0,0,0,0,0,1},  
  41.         {0,0,0,0,0,0,0},  
  42.         {0,0,0,1,1,0,1},  
  43.         {0,1,0,0,0,0,0}  
  44.     };  
  45.    ArrayToList(A[0], 7, G);  
  46.      printf("(2)最大出度的顶点信息:");  
  47.     OutMaxDs(G);  
  48.     return 0;  
  49. }  

原创粉丝点击