数据结构上机实践第11周项目3

来源:互联网 发布:网上配眼镜 知乎 编辑:程序博客网 时间:2024/05/21 18:45


图遍历算法实现 本次实践将运用图结构基本算法库,进行遍历操作的实现,本次实践到的算法库点击此处参考。(编译环境:VC++6.0) 1.深度优先遍历——DFS 测试用图: 测试时用的图是这里写图片描述,可以使用其他类型的图代替。
实现源代码:
[cpp] view plain copy
  1. //*Copyright  (c)2017,烟台大学计算机与控制工程学院*                     
  2. //*All rights reservrd.*                     
  3. //*文件名称 :main.cpp*                     
  4. //*作者:侯成健*                  
  5. //*完成时间:2017年11月29日*                      
  6. //*版本号:v1.0*                  
  7. //*问题描述:测试函数*                     
  8. //*输入描述:无*                     
  9. //*程序输出:无*  
  10. #include <stdio.h>  
  11. #include <malloc.h>  
  12. #include "graph.h"  
  13. int visited[MAXV];  
  14. void DFS(ALGraph *G, int v)  
  15. {  
  16.     ArcNode *p;  
  17.     int w;  
  18.     visited[v]=1;  
  19.     printf("%d ", v);  
  20.     p=G->adjlist[v].firstarc;  
  21.     while (p!=NULL)  
  22.     {  
  23.         w=p->adjvex;  
  24.         if (visited[w]==0)  
  25.             DFS(G,w);  
  26.         p=p->nextarc;  
  27.     }  
  28. }  
  29.   
  30. int main()  
  31. {  
  32.     int i;  
  33.     ALGraph *G;  
  34.     int A[5][5]=  
  35.     {  
  36.         {0,1,0,1,0},  
  37.         {1,0,1,0,0},  
  38.         {0,1,0,1,1},  
  39.         {1,0,1,0,1},  
  40.         {0,0,1,1,0}  
  41.     };  
  42.     ArrayToList(A[0], 5, G);  
  43.   
  44.     for(i=0; i<MAXV; i++) visited[i]=0;  
  45.     printf(" 由2开始深度遍历:");  
  46.     DFS(G, 2);  
  47.     printf("\n");  
  48.   
  49.     for(i=0; i<MAXV; i++) visited[i]=0;  
  50.     printf(" 由0开始深度遍历:");  
  51.     DFS(G, 0);  
  52.     printf("\n");  
  53.     return 0;  
  54. }  


运行结果截图: 
2.广度优先遍历——BFS 测试用图: 测试时用的图是这里写图片描述,可以使用其他类型的图代替。
实现源代码:
[cpp] view plain copy
  1. //*Copyright  (c)2017,烟台大学计算机与控制工程学院*                     
  2. //*All rights reservrd.*                     
  3. //*文件名称 :main.cpp*                     
  4. //*作者:侯成健*                  
  5. //*完成时间:2017年11月29日*                      
  6. //*版本号:v1.0*                  
  7. //*问题描述:测试函数*                     
  8. //*输入描述:无*                     
  9. //*程序输出:无*  
  10. #include <stdio.h>  
  11. #include <malloc.h>  
  12. #include "graph.h"  
  13.   
  14. void BFS(ALGraph *G, int v)  
  15. {  
  16.     ArcNode *p;  
  17.     int w,i;  
  18.     int queue[MAXV],front=0,rear=0; //定义循环队列  
  19.     int visited[MAXV];     //定义存放节点的访问标志的数组  
  20.     for (i=0; i<G->n; i++) visited[i]=0; //访问标志数组初始化  
  21.     printf("%2d",v);            //输出被访问顶点的编号  
  22.     visited[v]=1;                       //置已访问标记  
  23.     rear=(rear+1)%MAXV;  
  24.     queue[rear]=v;              //v进队  
  25.     while (front!=rear)         //若队列不空时循环  
  26.     {  
  27.         front=(front+1)%MAXV;  
  28.         w=queue[front];             //出队并赋给w  
  29.         p=G->adjlist[w].firstarc;   //找w的第一个的邻接点  
  30.         while (p!=NULL)  
  31.         {  
  32.             if (visited[p->adjvex]==0)  
  33.             {  
  34.                 printf("%2d",p->adjvex); //访问之  
  35.                 visited[p->adjvex]=1;  
  36.                 rear=(rear+1)%MAXV; //该顶点进队  
  37.                 queue[rear]=p->adjvex;  
  38.             }  
  39.             p=p->nextarc;       //找下一个邻接顶点  
  40.         }  
  41.     }  
  42.     printf("\n");  
  43. }  
  44.   
  45.   
  46. int main()  
  47. {  
  48.     ALGraph *G;  
  49.     int A[5][5]=  
  50.     {  
  51.         {0,1,0,1,0},  
  52.         {1,0,1,0,0},  
  53.         {0,1,0,1,1},  
  54.         {1,0,1,0,1},  
  55.         {0,0,1,1,0}  
  56.     };  
  57.     ArrayToList(A[0], 5, G);  
  58.   
  59.     printf(" 由2开始广度遍历:");  
  60.     BFS(G, 2);  
  61.   
  62.     printf(" 由0开始广度遍历:");  
  63.     BFS(G, 0);  
  64.     return 0;  
  65. }  


运行结果截图: 
原创粉丝点击