邻接表深度优先和广度优先遍历(DFS和BFS)

用邻接矩阵创建邻接表,然后进行DFS和BFS

<span style="font-family:Microsoft YaHei;font-size:14px;">#include <stdio.h>
#include <stdlib.h>

#define MAXVEX 100
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0

//结点定义
typedef char VertexType;
typedef int EdgeType;
typedef int Status;//Status是函数的类型,其值是函数结果状态代码
typedef int Boolean;//Boolean是布尔类型,其值是TRUE或FALSE

//邻接矩阵结构
typedef struct
{
	VertexType vexs[MAXVEX];//顶点表
	EdgeType arc[MAXVEX][MAXVEX];//邻接矩阵,可看作边表
	int numVertexes,numEdge;//图中当前的顶点数和边数
}MGraph;

//邻接表结构
//边表结点
typedef struct EdgeNode 
{
	int adjvex;//邻接点域,存储该顶点对应的下标
	//EdgeType weight;//用于存储权值,对于非网图可以不需要
	struct EdgeNode *next;//链域,指向下一个邻接点
}EdgeNode;

//顶点表结点
typedef struct VertexNode
{
	VertexType data; //顶点域,存储顶点信息
	EdgeNode *firstedge; //边表头指针
}VertexNode,AdjList[MAXVEX];

typedef struct
{
	AdjList adjList;
	int numVertexes,numEdges;//图中当前顶点数和边数
}graphAdjList,*GraphAdjList;

//***********用到的队列结构与函数***********  

//链队列的结构  
typedef int QElemType;//这里QElemType的类型假设为int  

typedef struct QNode //结点结构  
{  
	QElemType  data;  
	struct QNode *next;  
}QNode,*QueuePtr;  

typedef struct //队列的链表结构  
{  
	QueuePtr front,rear;//队头、队尾指针  
}LinkQueue;  

//初始化队列:  
Status initQueue(LinkQueue *q)  
{  
	q->front = q->rear = (QueuePtr)malloc(sizeof(QNode));  
	if(!q->front)  
		return ERROR;  
	q->front->next = NULL;  

	return OK;  
}  

//入队:插入元素e为q的新的队尾元素  
Status EnQueue(LinkQueue *q,QElemType e)  
{  
	QueuePtr s = (QueuePtr)malloc(sizeof(QNode));  
	if(!s)//存储分配失败  
		return ERROR;  
	s->data = e;  
	s->next = NULL;  
	q->rear->next = s;  
	q->rear = s;  

	return OK;  
}  

//出队  
//若队列不为空,删除q的队头元素,用e返回其值,并返回OK,否则返回ERROR  
Status DeQueue(LinkQueue *q,QElemType *e)  
{  

	QueuePtr p;  

	if(q->front==q->rear)  
		return ERROR;  
	p = q->front->next;  
	*e = p->data;  
	q->front->next = p->next;  

	if(q->rear==p)//若队头是队尾,则删除后将rear指向头结点  
		q->rear = q->front;  
	free(p);  
	return OK;  
}  

//判断是否为空队列,若为空队列则返回1,否则返回0  
Status QueueEmpty(LinkQueue q)  
{  
	if(q.front == q.rear)  
		return 1;  
	return 0;  
}  

//******************************************************  

//建立邻接矩阵
void CreateMGraph(MGraph *G)
{
	int i,j;

	G->numEdge=15;
	G->numVertexes=9;

	//读入顶点信息,建立顶点表
	G->vexs[0]='A';
	G->vexs[1]='B';
	G->vexs[2]='C';
	G->vexs[3]='D';
	G->vexs[4]='E';
	G->vexs[5]='F';
	G->vexs[6]='G';
	G->vexs[7]='H';
	G->vexs[8]='I';

	//初始化图
	for(i=0;i<G->numVertexes;i++)
		for(j=0;j<G->numVertexes;j++)
			G->arc[i][j] = 0;

	G->arc[0][1]=1;
	G->arc[0][5]=1;

	G->arc[1][2]=1;
	G->arc[1][6]=1;
	G->arc[1][8]=1;

	G->arc[2][3]=1;
	G->arc[2][8]=1;

	G->arc[3][4]=1;
	G->arc[3][6]=1;
	G->arc[3][7]=1;
	G->arc[3][8]=1;

	G->arc[4][5]=1;
	G->arc[4][7]=1;

	G->arc[5][6]=1;

	G->arc[6][7]=1;

	//对称矩阵
	for(i=0;i<G->numVertexes;i++)
		for(j=i;j<G->numVertexes;j++)
			G->arc[j][i] = G->arc[i][j];
}

//利用邻接矩阵构建邻接表
void CreateALGraph(MGraph G,GraphAdjList *GL)
{
	int i,j,k;
	EdgeNode *e;

	*GL = (GraphAdjList )malloc(sizeof(graphAdjList));

	(*GL)->numVertexes=G.numVertexes;
	(*GL)->numEdges=G.numEdge;

	//读入顶点信息,建立顶点表
	for(i=0;i<G.numVertexes;i++)
	{
		(*GL)->adjList[i].data=G.vexs[i];
		(*GL)->adjList[i].firstedge=NULL;
	}

	//建立边表
	for(k=0;k<G.numEdge;k++)
	{
		for(j=0;j<G.numVertexes;j++)
		{
			if(G.arc[k][j]==1)
			{
				e=(EdgeNode *)malloc(sizeof(EdgeNode));
				e->adjvex=j;
				e->next=(*GL)->adjList[k].firstedge;
				(*GL)->adjList[k].firstedge=e;
			}
		}
	}
}

Boolean visited[MAXVEX];//访问标记的数组

//邻接表的深度优先递归算法
void DFS(GraphAdjList GL, int i)
{
	EdgeNode *p;
	visited[i]= TRUE;
	printf("%c ",GL->adjList[i].data);
	p=GL->adjList[i].firstedge;
	while(p)
	{
		if(!visited[p->adjvex])
			DFS(GL,p->adjvex);
		p=p->next;
	}
}

//邻接表的深度遍历操作
void DFSTraverse(GraphAdjList GL)
{
	int i;
	for(i=0;i<GL->numVertexes;i++)
		visited[i]=FALSE;//初始化所以顶点状态都是未访问过状态
	for(i=0;i<GL->numVertexes;i++)
		if(!visited[i])
			DFS(GL,i);
}


//邻接表的广度遍历算法
void BFSTraverse(GraphAdjList GL)
{
	int i;
	EdgeNode *p;
	LinkQueue Q;

	for(i=0;i<GL->numVertexes;i++)
		visited[i]=FALSE;
	initQueue(&Q);
	for(i=0;i<GL->numVertexes;i++)
	{
		if(!visited[i])
		{
			visited[i]= TRUE;
			printf("%c ",GL->adjList[i].data);
			EnQueue(&Q,i);
			while(!QueueEmpty(Q))
			{
				DeQueue(&Q,&i);
				p = GL->adjList[i].firstedge;
				while(p)
				{
					if(!visited[p->adjvex])
					{
						visited[p->adjvex]=TRUE;
						printf("%c ",GL->adjList[p->adjvex].data);
						EnQueue(&Q,p->adjvex);
					}
					p=p->next;
				}
			}
		}
	}
}


int main(void)
{    
	MGraph G;
	GraphAdjList GL; 
	CreateMGraph(&G);
	CreateALGraph(G,&GL);

	printf("\n深度遍历:");
	DFSTraverse(GL);
	printf("\n广度遍历:");
	BFSTraverse(GL);

	return 0;
}</span>


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值