图(三)之Adjecancy List的广度优先遍历
2015-06-04 09:51
211 查看
图的遍历的另外一种就是广度优先遍历。图的广度优先搜索遍历类似于书的按层次遍历的过程。但是另外图的广搜还需要附设一个队列用来存储相应的节点。下面把图和相应的代码分享出来:
图依旧是那幅图:
遍历结果:
相应的算法(严蔚敏教材提供的):
下面我把我的代码分享一下:
Queue.h
当然了大家也可以去我的github clone 一下
https://github.com/cremyos/Graph_Visit_BFS
两篇关于遍历的算法可以看出,两者的时间复杂度是由如何存储图的结构决定的。两者的不同仅在对于顶点的访问的顺序不同。
图依旧是那幅图:
遍历结果:
相应的算法(严蔚敏教材提供的):
下面我把我的代码分享一下:
Queue.h
/* *Structure of Queue */ #ifndef __QUEUE_H__ #define __QUEUE_H__ #include <string.h> #define MAX_QUEUE_SIZE 100 typedef struct queue { int data[MAX_QUEUE_SIZE]; int front,rear,length; }queue,*Queue_Ptr; Queue_Ptr InitQueue(Queue_Ptr p); Queue_Ptr EnQueue(Queue_Ptr p,int e); int DeQueue(Queue_Ptr p,int e); int EmptyQueue(Queue_Ptr p); #endifqueue.c
/* ************************************************ *Name : queue.c * *Date : 2015-06-04 * *Author : sniper * *Aim : Queue basic operation. * ************************************************ */ #include <stdio.h> #include <stdlib.h> #include "Queue.h" /* *Init the queue */ Queue_Ptr InitQueue(Queue_Ptr p) { p=(Queue_Ptr)malloc(sizeof(queue)); memset(p->data,0,sizeof(p->data)); p->front = p->rear=0; p->length=0; return p; } /* *Enter the queue */ Queue_Ptr EnQueue(Queue_Ptr p,int e) { p->data[p->rear]=e; p->rear=(p->rear+1)%MAX_QUEUE_SIZE; p->length++; return p; } /* *Delete the queue */ int DeQueue(Queue_Ptr p,int e) { e=p->data[p->front]; p->front=(p->front+1)%MAX_QUEUE_SIZE; p->length--; return e; } /* *Judge the queue null or not */ int EmptyQueue(Queue_Ptr p) { if(p->front==p->rear) return 0; else return 1; }Adj_list.h
/* *Adjacency list structure */ #ifndef __ADJ_LIST_H__ #define __ADJ_LIST_H__ #define MAX_VERTEX_NUM 20 typedef struct ArcNode { char adjvex; struct ArcNode *nextarc; int *info; }ArcNode; typedef struct VNode { char data; ArcNode *firstarc; }VNode,AdjList[MAX_VERTEX_NUM]; typedef struct ALGraph { AdjList vertices; int vexnum,arcnum; int kind; }ALGraph; #endif /* __ADJ_LIST_H__*/graph_visit_BFS.c
/* ************************************************ *Name : graph_visit_BFS.c * *Date : 2015-06-03 * *Author : sniper * *Aim : The Adjacency list storage the graph and* * visit it using the BFS. * ************************************************ */ #include <stdio.h> #include <stdlib.h> #include "Adj_list.h" #include "Queue.h" int visit[MAX_VERTEX_NUM]={0}; /* *Create the Adjacency list */ ALGraph* create(ALGraph *G) { int i,j; int node_pair1,node_pair2; ArcNode *arc; node_pair1=0; node_pair2=0; i=0; j=0; printf("please input the number of node and edge: "); scanf("%d %d",&G->vexnum,&G->arcnum); for(i=0;i<G->vexnum;i++) { G->vertices[i].data = 'A'+i; G->vertices[i].firstarc=NULL; /* *prepare for visiting */ visit[i]=0; } printf("finish the Node!\n"); for(j=0;j<G->arcnum;j++) { printf("please input the node pair: "); scanf("%d %d",&node_pair1,&node_pair2); node_pair1-=1; node_pair2-=1; /* *Node pair get match with each other *and storage into the adjacency list. */ arc = (ArcNode *)malloc(sizeof(ArcNode)); arc->adjvex = node_pair2+'A'; arc->nextarc=G->vertices[node_pair1].firstarc; G->vertices[node_pair1].firstarc=arc; } printf("finish the Adjacency List\n"); return G; } /* *BFS visit *We need a queue to help */ void BFSTraverse(ALGraph *G) { int i; for(i=0;i<G->vexnum;i++) BFS(G,i); } void BFS(ALGraph *G,int i) { int j=0,k=0; ArcNode *arc; Queue_Ptr p; int u=0; p=InitQueue(p); if(!visit[i]) { printf("%c -> ",G->vertices[i].data); visit[i]=1; } p=EnQueue(p,i); while(EmptyQueue(p)) { j=DeQueue(p,j); arc=G->vertices[j].firstarc; while(arc) { if(visit[arc->adjvex-'A']==0) { visit[arc->adjvex-'A']=1; printf("%c -> ",G->vertices[arc->adjvex-'A'].data); p=EnQueue(p,arc->adjvex-'A'); } arc=arc->nextarc; } } } int main() { ALGraph *G; int i; i=0; G = (ALGraph *)malloc(sizeof(ALGraph)); G = create(G); printf("print the BFS\n"); printf("****************************\n"); BFSTraverse(G); printf("\n"); return 0; }
当然了大家也可以去我的github clone 一下
https://github.com/cremyos/Graph_Visit_BFS
两篇关于遍历的算法可以看出,两者的时间复杂度是由如何存储图的结构决定的。两者的不同仅在对于顶点的访问的顺序不同。
相关文章推荐
- 常用的android弹出对话框
- .NET开发者必备的工具箱
- 云计算技术
- Assisx快捷键大全
- UITableViewCell分割线(样式,宽度·高度调整,颜色等的设置)
- 浅谈利用JavaScript进行的DDoS攻击原理与防御
- Hive一点一滴系列之Eclipse调用hive执行操作
- ContentProvider
- SQL模糊查询的写法
- 回形取数
- from weibo:机器学习算法之旅
- from weibo : 开始工作后的十个不要
- 【我们都爱Paul Hegarty】斯坦福IOS8公开课个人笔记28 ScrollView 滑动视图
- 设计模式--缺醒适配器模式
- Openssl-1.0.0c编译报错 libcrypto.so: undefined reference to `ENGINE_load_aep'的问题
- Apache+Tomcat+JK集群配置
- 我应该做的事情
- mysql中间件研究(Atlas,cobar,TDDL)
- cocos2dx 纹理优化
- HLS直播技术方案及踩过的坑