十二周项目三 图遍历算法实现

问题及代码:

[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. /*     
  2. * Copyright (c)2016,烟台大学计算机与控制工程学院     
  3. * All rights reserved.     
  4. * 文件名称:项目3.cpp     
  5. * 作    者:泮春宇   
  6. * 完成日期:2016年12月6日     
  7. * 版 本 号:v1.0      
  8. *问题描述:实现图遍历算法,分别输出如下图结构的深度优先(DFS)遍历序列和广度优先遍历(BFS)序列。  
  9. *输入描述:无     
  10. *程序输出:测试数据     
  11. */     


 

测试用图如下:

 

1、深度优先遍历---DFS

头文件graph.h和源文件graph.cpp代码详见第十二周项目1--图基本算法

 

主函数main.cpp代码:

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


 

运算结果:

 

2、广度优先遍历---BFS

头文件graph.h和源文件graph.cpp代码详见第十二周项目1--图基本算法库

 

主函数main.cpp代码:

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


 

运算结果:

 

知识点总结:

图的两种遍历

学习心得:

两种遍历比较容易掌握,相对来说要理解好深度优先遍历的内容,并且要避免出现回路。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值