圖的遍歷的另外一種就是廣度優先遍歷。圖的廣度優先搜索遍歷類似於書的按層次遍歷的過程。但是另外圖的廣搜還需要附設一個隊列用來存儲相應的節點。下面把圖和相應的代碼分享出來:
圖依舊是那幅圖:
遍歷結果:
相應的算法(嚴蔚敏教材提供的):
下面我把我的代碼分享一下:
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);
#endif
queue.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;
}
https://github.com/cremyos/Graph_Visit_BFS
兩篇關於遍歷的算法可以看出,兩者的時間複雜度是由如何存儲圖的結構決定的。兩者的不同僅在對於頂點的訪問的順序不同。