第十二周项目3-图遍历算法实现

/*   

* 烟台大学计算机与控制工程学院   

* 作者:王雪松 

* 完成日期:2016年11月18日   

*   

* 问题描述:

* 输入描述:

* 程序输出:

 */ 

代码:

1、深度优先遍历——DFS(程序中graph.h是图存储结构的“算法库”中的头文件,详情请单击链接…

[csharp]  view plain  copy
  1. #include <stdio.h>  
  2. #include <malloc.h>  
  3. #include "graph.h"  
  4. int visited[MAXV];  
  5. void DFS(ALGraph *G, int v)  
  6. {  
  7.     ArcNode *p;  
  8.     int w;  
  9.     visited[v]=1;  
  10.     printf("%d ", v);  
  11.     p=G->adjlist[v].firstarc;  
  12.     while (p!=NULL)  
  13.     {  
  14.         w=p->adjvex;  
  15.         if (visited[w]==0)  
  16.             DFS(G,w);  
  17.         p=p->nextarc;  
  18.     }  
  19. }  
  20.   
  21. int main()  
  22. {  
  23.     int i;  
  24.     ALGraph *G;  
  25.     int A[5][5]=  
  26.     {  
  27.         {0,1,0,1,0},  
  28.         {1,0,1,0,0},  
  29.         {0,1,0,1,1},  
  30.         {1,0,1,0,1},  
  31.         {0,0,1,1,0}  
  32.     };  
  33.     ArrayToList(A[0], 5, G);  
  34.   
  35.     for(i=0; i<MAXV; i++) visited[i]=0;  
  36.     printf(" 由2开始深度遍历:");  
  37.     DFS(G, 2);  
  38.     printf("\n");  
  39.   
  40.     for(i=0; i<MAXV; i++) visited[i]=0;  
  41.     printf(" 由0开始深度遍历:");  
  42.     DFS(G, 0);  
  43.     printf("\n");  
  44.     return 0;  
  45. }  


运行结果:

2、广度优先遍历——BFS(程序中graph.h是图存储结构的“算法库”中的头文件,详情请单击链接…

[csharp]  view plain  copy
  1. #include <stdio.h>  
  2. #include <malloc.h>  
  3. #include "graph.h"  
  4.   
  5. void BFS(ALGraph *G, int v)  
  6. {  
  7.     ArcNode *p;  
  8.     int w,i;  
  9.     int queue[MAXV],front=0,rear=0; //定义循环队列  
  10.     int visited[MAXV];     //定义存放节点的访问标志的数组  
  11.     for (i=0; i<G->n; i++) visited[i]=0; //访问标志数组初始化  
  12.     printf("%2d",v);            //输出被访问顶点的编号  
  13.     visited[v]=1;                       //置已访问标记  
  14.     rear=(rear+1)%MAXV;  
  15.     queue[rear]=v;              //v进队  
  16.     while (front!=rear)         //若队列不空时循环  
  17.     {  
  18.         front=(front+1)%MAXV;  
  19.         w=queue[front];             //出队并赋给w  
  20.         p=G->adjlist[w].firstarc;   //找w的第一个的邻接点  
  21.         while (p!=NULL)  
  22.         {  
  23.             if (visited[p->adjvex]==0)  
  24.             {  
  25.                 printf("%2d",p->adjvex); //访问之  
  26.                 visited[p->adjvex]=1;  
  27.                 rear=(rear+1)%MAXV; //该顶点进队  
  28.                 queue[rear]=p->adjvex;  
  29.             }  
  30.             p=p->nextarc;       //找下一个邻接顶点  
  31.         }  
  32.     }  
  33.     printf("\n");  
  34. }  
  35.   
  36.   
  37. int main()  
  38. {  
  39.     ALGraph *G;  
  40.     int A[5][5]=  
  41.     {  
  42.         {0,1,0,1,0},  
  43.         {1,0,1,0,0},  
  44.         {0,1,0,1,1},  
  45.         {1,0,1,0,1},  
  46.         {0,0,1,1,0}  
  47.     };  
  48.     ArrayToList(A[0], 5, G);  
  49.   
  50.     printf(" 由2开始广度遍历:");  
  51.     BFS(G, 2);  
  52.   
  53.     printf(" 由0开始广度遍历:");  
  54.     BFS(G, 0);  
  55.     return 0;  
  56. }  


运行结果:

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值