图(三)之Adjecancy List的广度优先遍历

图的遍历的另外一种就是广度优先遍历。图的广度优先搜索遍历类似于书的按层次遍历的过程。但是另外图的广搜还需要附设一个队列用来存储相应的节点。下面把图和相应的代码分享出来:

图依旧是那幅图:


遍历结果:


相应的算法(严蔚敏教材提供的):


下面我把我的代码分享一下:

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;
}


当然了大家也可以去我的github clone 一下

https://github.com/cremyos/Graph_Visit_BFS

两篇关于遍历的算法可以看出,两者的时间复杂度是由如何存储图的结构决定的。两者的不同仅在对于顶点的访问的顺序不同。

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值