图的深度优先遍历和广度优先遍历算法

(1)ALGraph.h

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define MAX_VERTEX_NUM 100
#define MAX_LENGTH 100
typedef int VertexType;
//弧结点数据结构
typedef struct ArcNode {
	int adjvex;//邻接域
	struct ArcNode* nextArc;
	//int weight;//权值
}ArcNode;
//顶点数据结构
typedef struct VNode {
	VertexType data;
	ArcNode* firstarc;
}VNode, AdjList[MAX_VERTEX_NUM];
//图的数据结构
typedef struct {
	AdjList vertices;
	int vexNum, arcNum;
	int kind;
}ALGraph;
//建图
void createALGraph(ALGraph& G, VertexType vList[], int vListLength, int arcList[][2], int arcListLength, int kind);
//深度优先遍历
void DFSTraverse(ALGraph G);
//广度优先遍历算法
void BFSTraverse(ALGraph G);

//链队相关数据结构
typedef VertexType QElemType;
typedef struct LinkNode {
	QElemType data;
	struct LinkNode* next;
}LinkNode, * LinkList;
typedef struct LinkQueue {
	LinkNode* front, * rear;
}LinkQueue;
//初始化队列
void InitQueue(LinkQueue& Q);
//判断队列是否为空
int IsEmpty(LinkQueue Q);
//入队
void EnQueue(LinkQueue& Q, QElemType x);
//出队
void DeQueue(LinkQueue& Q, QElemType& x);

(2)createALGraph.cpp

#include"ALGraph.h"
void createALGraph(ALGraph& G, VertexType vList[], int vListLength, int arcList[][2], int arcListLength, int kind) {
	for (int i = 1; i <= vListLength; i++) {
		G.vertices[i].data = vList[i];
		G.vertices[i].firstarc = NULL;
	}
	for (int i = 0; i < arcListLength; i++) {
		int v = arcList[i][0];
		int w = arcList[i][1];
		ArcNode* pnode = (ArcNode*)malloc(sizeof(ArcNode));
		pnode->adjvex = w;
		pnode->nextArc = G.vertices[v].firstarc;
		G.vertices[v].firstarc = pnode;
		if (kind == 0) {//无向图
			pnode = (ArcNode*)malloc(sizeof(ArcNode));
			pnode->adjvex = v;
			pnode->nextArc = G.vertices[w].firstarc;
			G.vertices[w].firstarc = pnode;
		}
	}
	G.arcNum = arcListLength;
	G.vexNum = vListLength;
	G.kind = kind;
}

(3)queue.cpp

#include"ALGraph.h"
void InitQueue(LinkQueue& Q) {
	Q.front = Q.rear = (LinkNode*)malloc(sizeof(LinkNode));
	Q.front->next = NULL;
}
int IsEmpty(LinkQueue Q) {
	if (Q.front == Q.rear) {
		return 1;
	}
	else {
		return 0;
	}
}
void EnQueue(LinkQueue& Q, QElemType x) {
	LinkNode* pnode = (LinkNode*)malloc(sizeof(LinkNode));
	pnode->data = x;
	pnode->next = Q.rear->next;
	Q.rear->next = pnode;
	Q.rear = pnode;
}
void DeQueue(LinkQueue& Q, QElemType& x) {
	LinkNode* pnode = (LinkNode*)malloc(sizeof(LinkNode));
	pnode = Q.front->next;
	x = pnode->data;
	Q.front->next = pnode->next;
	if (Q.rear == pnode) {
		Q.rear = Q.front;
	}
	free(pnode);
}

(4)DFS.cpp

#include"ALGraph.h"
//深度优先遍历(以下默认顶点为数字编号,链号、顶点信息、邻接域索引三者等价)
/*算法思想:初始状态下,G中所有顶点都未被访问过。从G中未访问的顶点v出发,访问该结点,然后依次从该顶点未访问的邻接点出发进行DFS,
直到图中所有与v路径相连同的顶点都被访问过;再从图中未访问的顶点w出发,重复上述步骤,直到G中所有顶点都访问完为止*/
int visited[MAX_VERTEX_NUM] = { 0 };
void DFS(ALGraph G, VertexType v) {//该算法思想与树的先跟遍历算法思想类似
	visited[v] = 1;//访问该结点
	printf("%d ", v);
	ArcNode* pnode = G.vertices[v].firstarc;//链中第一个弧节点
	while (pnode != NULL) {//从前往后访问每个弧结点
		VertexType adjVertex = pnode->adjvex;//获得v的邻接点
		if (!visited[adjVertex]) {//邻接点未被访问过
			DFS(G, adjVertex);//从邻接点进行DFS
		}
		pnode = pnode->nextArc;//指向下一个表结点
	}
}
void DFSTraverse(ALGraph G) {
	for (int iVertex = 1; iVertex <= G.vexNum; iVertex++) {
		if (!visited[iVertex]) {//从G中未访问的顶点出发
			DFS(G,iVertex);
		}
	}
}

(5)BFS.cpp

#include"ALGraph.h"
//广度优先遍历算法类似树的层次遍历算法
int visited1[MAX_VERTEX_NUM] = { 0 };
LinkQueue Q;
void BFS(ALGraph G, VertexType v) {
	printf("%d ", v);//访问结点
	visited1[v] = 1;//修改标记位
	EnQueue(Q, v);//入队
	while (!IsEmpty(Q)) {//如果队列不为空
		DeQueue(Q, v);//对头元素出队
		ArcNode* pnode = G.vertices[v].firstarc;//获得链中第一个弧结点
		while (pnode != NULL) {//依次访问链中每一个未被访问的弧结点
			VertexType w = pnode->adjvex;
			if (!visited1[w]) {
				printf("%d ", w);
				visited1[w] = 1;
				EnQueue(Q, w);
			}
			pnode = pnode->nextArc;
		}
	}
}
void BFSTraverse(ALGraph G) {
	InitQueue(Q);
	for (int iVertex = 1; iVertex <= G.vexNum; iVertex++) {
		if (!visited1[iVertex]) {
			BFS(G, iVertex);
		}
	}
}

(6)main.cpp

#include"ALGraph.h"
int main() {
	ALGraph G;//邻接表
	int vListLength;
	VertexType vList[MAX_LENGTH];
	int arcListLength;
	int arcList[MAX_LENGTH][2];
	int kind;
	scanf("%d", &vListLength);
	for (int i = 1; i <= vListLength; i++) {
		scanf("%d ", &vList[i]);
	}
	scanf("%d", &arcListLength);
	for (int i = 0; i < arcListLength; i++) {
		for (int j = 0; j < 2; j++) {
			scanf("%d", &arcList[i][j]);
		}
	}
	scanf("%d", &kind);
	createALGraph(G, vList, vListLength, arcList, arcListLength, kind);建图
	DFSTraverse(G);
	printf("\n");
	BFSTraverse(G);
	return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值