第12周项目2-操作用邻接表存储的图

来源:互联网 发布:印度人左手知乎 编辑:程序博客网 时间:2024/05/17 08:14

问题:

[cpp] view plain copy
  1. /* 
  2. * Copyright (c)2016,烟台大学计算机与控制工程学院 
  3. * All rights reserved. 
  4. * 文件名称:项目2.cbp 
  5. * 作    者:程德泉 
  6. * 完成日期:2016年11月16日 
  7. * 版 本 号:v1.0 
  8.  
  9.  
  10. * 问题描述:假设图G采用邻接表存储,分别设计实现以下要求的算法: 
  11.  
  12.  
  13. * 输入描述:无 
  14. * 程序输出:测试数据 
  15. */  


头文件及功能函数详见【图算法库】


利用下图作为测试用图,输出结果。

这里写图片描述

(1)输出出图G中每个顶点的出度

代码:

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

运行结果:


(2)求出图G中出度最大的一个顶点,输出该顶点编号

代码:

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

(3)计算图G中出度为0的顶点数;
代码:
[cpp] view plain copy
  1. #include "graph.h"  
  2.   
  3. //返回图G中编号为v的顶点的出度  
  4. int OutDegree(ALGraph *G,int v)  
  5. {  
  6.     ArcNode *p;  
  7.     int n=0;  
  8.     p=G->adjlist[v].firstarc;  
  9.     while (p!=NULL)  
  10.     {  
  11.         n++;  
  12.         p=p->nextarc;  
  13.     }  
  14.     return n;  
  15. }  
  16.   
  17. //输出图G中每个顶点的出度  
  18. void OutDs(ALGraph *G)  
  19. {  
  20.     int i;  
  21.     for (i=0; i<G->n; i++)  
  22.         printf("  顶点%d:%d\n",i,OutDegree(G,i));  
  23. }  
  24.   
  25. //输出图G中出度为0的顶点数  
  26. void ZeroDs(ALGraph *G)  
  27. {  
  28.     int i,x;  
  29.     for (i=0; i<G->n; i++)  
  30.     {  
  31.         x=OutDegree(G,i);  
  32.         if (x==0)  
  33.             printf("%2d",i);  
  34.     }  
  35.     printf("\n");  
  36. }  
  37.   
  38. int main()  
  39. {  
  40.     ALGraph *G;  
  41.     int A[7][7]=  
  42.     {  
  43.         {0,1,1,1,0,0,0},  
  44.         {0,0,0,0,1,0,0},  
  45.         {0,0,0,0,1,1,0},  
  46.         {0,0,0,0,0,0,1},  
  47.         {0,0,0,0,0,0,0},  
  48.         {0,0,0,1,1,0,1},  
  49.         {0,1,0,0,0,0,0}  
  50.     };  
  51.     ArrayToList(A[0], 7, G);  
  52.   
  53.     printf("出度为0的顶点:");  
  54.     ZeroDs(G);  
  55.   
  56.     return 0;  
  57. }  
运行结果:

(4)判断图G中是否存在边<i,j>
[cpp] view plain copy
  1. #include "graph.h"  
  2.   
  3. //返回图G中编号为v的顶点的出度  
  4. int OutDegree(ALGraph *G,int v)  
  5. {  
  6.     ArcNode *p;  
  7.     int n=0;  
  8.     p=G->adjlist[v].firstarc;  
  9.     while (p!=NULL)  
  10.     {  
  11.         n++;  
  12.         p=p->nextarc;  
  13.     }  
  14.     return n;  
  15. }  
  16.   
  17. //输出图G中每个顶点的出度  
  18. void OutDs(ALGraph *G)  
  19. {  
  20.     int i;  
  21.     for (i=0; i<G->n; i++)  
  22.         printf("  顶点%d:%d\n",i,OutDegree(G,i));  
  23. }  
  24.   
  25. //输出图G中出度最大的一个顶点  
  26. void OutMaxDs(ALGraph *G)  
  27. {  
  28.     int maxv=0,maxds=0,i,x;  
  29.     for (i=0; i<G->n; i++)  
  30.     {  
  31.         x=OutDegree(G,i);  
  32.         if (x>maxds)  
  33.         {  
  34.             maxds=x;  
  35.             maxv=i;  
  36.         }  
  37.     }  
  38.     printf("顶点%d,出度=%d\n",maxv,maxds);  
  39. }  
  40. //输出图G中出度为0的顶点数  
  41. void ZeroDs(ALGraph *G)  
  42. {  
  43.     int i,x;  
  44.     for (i=0; i<G->n; i++)  
  45.     {  
  46.         x=OutDegree(G,i);  
  47.         if (x==0)  
  48.             printf("%2d",i);  
  49.     }  
  50.     printf("\n");  
  51. }  
  52.   
  53. //返回图G中是否存在边<i,j>  
  54. bool Arc(ALGraph *G, int i,int j)  
  55. {  
  56.     ArcNode *p;  
  57.     bool found = false;  
  58.     p=G->adjlist[i].firstarc;  
  59.     while (p!=NULL)  
  60.     {  
  61.         if(p->adjvex==j)  
  62.         {  
  63.             found = true;  
  64.             break;  
  65.         }  
  66.         p=p->nextarc;  
  67.     }  
  68.     return found;  
  69. }  
  70.   
  71. int main()  
  72. {  
  73.     ALGraph *G;  
  74.     int A[7][7]=  
  75.     {  
  76.         {0,1,1,1,0,0,0},  
  77.         {0,0,0,0,1,0,0},  
  78.         {0,0,0,0,1,1,0},  
  79.         {0,0,0,0,0,0,1},  
  80.         {0,0,0,0,0,0,0},  
  81.         {0,0,0,1,1,0,1},  
  82.         {0,1,0,0,0,0,0}  
  83.     };  
  84.     ArrayToList(A[0], 7, G);  
  85.   
  86.     printf("边<2,6>存在吗?");  
  87.     if(Arc(G,2,6))  
  88.         printf("是\n");  
  89.     else  
  90.         printf("否\n");  
  91.     printf("\n");  
  92.       
  93.     return 0;  
  94. }  
运行结果:

知识点总结:
图算法库的应用。
学习心得:
过程很多,实现不容易,但实现第一个后后面的就有规律可循了。

0 0
原创粉丝点击