第十一周项目二(2)

/*
Copyright (c++) 2017,烟台大学计算机与控制工程学院
文件名称:jcy
作 者:贾存钰
完成日期:2017年11月18日
问题描述:假设图G采用邻接表存储,分别设计实现以下要求的算法: 
(3)计算图G中出度为0的顶点数; 

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

ps:需用图的算法库
利用下图作为测试用图,输出结果。

*/

[cpp]  view plain  copy
  1. //输出图G中出度为0的顶点数  
  2. #include <stdio.h>  
  3. #include <malloc.h>  
  4. #include "graph.h"  
  5.   
  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. void ZeroDs(ALGraph *G)  
  19. {  
  20.     int i,x;  
  21.     for (i=0; i<G->n; i++)  
  22.     {  
  23.         x=OutDegree(G,i);  
  24.         if (x==0)  
  25.             printf("%2d",i);  
  26.     }  
  27.     printf("\n");  
  28. }  
  29. int main()  
  30. {  
  31.     ALGraph *G;  
  32.     int A[7][7]=  
  33.     {  
  34.         {0,1,1,1,0,0,0},  
  35.         {0,0,0,0,1,0,0},  
  36.         {0,0,0,0,1,1,0},  
  37.         {0,0,0,0,0,0,1},  
  38.         {0,0,0,0,0,0,0},  
  39.         {0,0,0,1,1,0,1},  
  40.         {0,1,0,0,0,0,0}  
  41.     };  
  42.     ArrayToList(A[0], 7, G);  
  43.     printf("(3)出度为0的顶点:");  
  44.     ZeroDs(G);  
  45.     return 0;  
  46.   
  47. }  



[cpp]  view plain  copy
  1. //返回图G中是否存在边<i,j>  
  2. #include <stdio.h>  
  3. #include <malloc.h>  
  4. #include "graph.h"  
  5.   
  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.   
  19. bool Arc(ALGraph *G, int i,int j)  
  20. {  
  21.     ArcNode *p;  
  22.     bool found = false;  
  23.     p=G->adjlist[i].firstarc;  
  24.     while (p!=NULL)  
  25.     {  
  26.         if(p->adjvex==j)  
  27.         {  
  28.             found = true;  
  29.             break;  
  30.         }  
  31.         p=p->nextarc;  
  32.     }  
  33.     return found;  
  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("(4)边<2,6>存在吗?");  
  49.     if(Arc(G,2,6))  
  50.         printf("是\n");  
  51.     else  
  52.         printf("否\n");  
  53.     printf("\n");  
  54.     return 0;  
  55.   
  56. }  

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值