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

  1. /*  
  2. Copyright (c)2016,烟台大学计算机与控制工程学院  
  3. All rights reserved.  
  4. 文件名称:第十二周项目3 - 图遍历算法实现.cpp  
  5. 作    者:朱建豪 
  6. 完成日期:2016年11月18日  
  7. 版 本 号:v1.0  
  8.   
  9. 问题描述: 实现图遍历算法,分别输出如下图结构的深度优先(DFS)遍历序列和广度优先遍历(BFS)序列。   
  10. 输入描述:若干测试数据。  
  11. 程序输出:DFS,BFS的遍历序列。   
  12. */    


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

代码:

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


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

代码:

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


(3)计算图G中出度为0的顶点数;

代码:

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



(4)判断图G中是否存在边 <i,j>

代码:

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



知识点总结:

算法的应用。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值