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

来源:互联网 发布:羽毛笔哈利波特 淘宝 编辑:程序博客网 时间:2024/05/22 01:53

问题:

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

头文件及功能函数详见【图算法库】


  • 深度优先遍历——DFS

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


运行结果:


  • 广度优先遍历——BFS
[cpp] view plain copy
  1. #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. }  
测试用图:

运行结果:



知识点总结:
图算法库的应用。
学习心得:
这两种算法可以通过画图的方法理解。

0 0
原创粉丝点击