图的深度优先遍历(DFS)与广度优先遍历(BFS)的c语言实现

头文件

#pragma warning( disable : 4996)
#pragma once
#ifndef _GRAPH_H_
#define _GRAPH_H_

#define MAX_VERTEX_NUM 20
typedef int Graphkind;//有向图该值为1、无向图该值为0
typedef int InfoType;
typedef char VertexType;
//弧节点表示
typedef struct ArcNode {
	int adjvex;//弧的下一个顶点的位置
	struct ArcNode* nextarc;//指向下一条弧的位置
	InfoType info;//弧的相关信息
};
//表头结点
typedef struct VNode{
	VertexType data;//顶点的信息
	ArcNode* firstarc;//指向顶点的第一条弧的位置
}VNode,AdjList[MAX_VERTEX_NUM];
typedef struct {
	AdjList vertices;
	int vexnum, arcnum;//图的顶点数目、弧数目
	Graphkind kind;
}Graph;
extern int visisted[MAX_VERTEX_NUM];//标记节点还未访问
Graph* creat_graph(Graph* g);//初始化图否被访问,0代表节点未被访问,1代表节点已被被访问
void DFS(Graph* g, int i);//单个节点开始进行深度优先遍历
void Graph_DFS(Graph* g);//整个图的深度优先遍历
void BFS(Graph* g, int i);//单个节点开始进行广度优先遍历
void Graph_BFS(Graph* g);//整个图的广度优先遍历
#endif

头文件中相关函数的定义文件

#include<stdio.h>
#include<stdlib.h>
#include"Graph.h"
int visisted[MAX_VERTEX_NUM];//标记节点还未访问
Graph* creat_graph(Graph* g) {//初始化图
	g = (Graph*)malloc(sizeof(Graph));
	if (g == NULL) {
		printf("内存分配不成功!\n");
		exit(-1);
	}
	printf("请输入图的顶点数和弧数:\n");
	printf("顶点数:");
	scanf("%d", &g->vexnum);
	printf("弧数:");
	scanf("%d", &g->arcnum);
	printf("请选择该图的种类:1为有向图;0为无向图\n");
	scanf("%d", &g->kind);
	printf("\n");
	printf("输入顶点的信息:\n");
	//初始化表头结点
	//fflush(stdin);
	getchar();
	char c;
	for (int i = 0; i < g->vexnum; i++) {
		scanf("%c", &c);
		g->vertices[i].data = c;
		g->vertices[i].firstarc = NULL;
	}
	printf("初始化弧,并建立图:\n");
	//初始化弧,并建立图
	int i1, j1;
	ArcNode* temp;
	//fflush(stdin);
	getchar();
	for (int j = 0; j < g->arcnum; j++) {
		printf("请输入弧(用(i1,j1)表示):");
		scanf("%d %d", &i1, &j1);
		temp = (ArcNode*)malloc(sizeof(ArcNode));
		if (temp == NULL) {
			printf("内存分配不成功!\n");
			exit(-1);
		}
		temp->adjvex = j1;
		temp->nextarc = g->vertices[i1].firstarc;
		printf("该弧的权值:");
		fflush(stdin);
		scanf("%d", &temp->info);
		g->vertices[i1].firstarc = temp;
		if (g->kind == 0) {//图为无向图
		    temp = (ArcNode*)malloc(sizeof(ArcNode));
			if (temp == NULL) {
				printf("内存分配不成功!\n");
				exit(-1);
			}
			temp->adjvex = i1;
			temp->nextarc = g->vertices[j1].firstarc;
			g->vertices[j1].firstarc = temp;
		}
	}
	return g;
}
//图的深度优先遍历
void DFS(Graph* g, int i) {//单个节点开始进行深度优先遍历
	ArcNode* t;
	printf("%c", g->vertices[i].data);
	visisted[i] = 1;
	t = g->vertices[i].firstarc;
	while (t) {
		if (!visisted[t->adjvex])
			DFS(g, t->adjvex);
		t = t->nextarc;
	}
}
void Graph_DFS(Graph* g) {//整个图的深度优先遍历
	int i;
	for (i = 0; i < g->vexnum; i++)//将所有结点定义为未访问
		visisted[i] = 0;
	for (i = 0; i < g->vexnum; i++) {
		if (!visisted[i])//如果节点还未访问,则从该节点进行深度访问
			DFS(g, i);
	}
}
void BFS(Graph* g, int i) {//单个节点开始进行广度优先遍历
	int q[MAX_VERTEX_NUM];//用于模拟访问过结点的队列
	int front, rear;//front为队列的头部,rear为队列的尾部
	int temp;//该变量用于接受出队的元素
	front = 0;
	rear = 0;
	printf("%c", g->vertices[i].data);
	visisted[i] = 1;
	q[rear++] = i;//模拟入队
	ArcNode* p;
	while (rear > front) {
		temp = q[front++];//模拟出队
		p = g->vertices[temp].firstarc;
		while (p) {
			if (!visisted[p->adjvex]) {
				printf("%c", g->vertices[p->adjvex].data);
				q[rear++] = p->adjvex;
				visisted[p->adjvex] = 1;
			}
			p = p->nextarc;
   		}
	}
}
void Graph_BFS(Graph* g) {//整个图的广度优先遍历
	int i;
	for (i = 0; i < g->vexnum; i++)
		visisted[i] = 0;
	for (i = 0; i < g->vexnum; i++)
		if (!visisted[i])
			BFS(g, i);
}

主程序文件

#include<stdio.h>
#include"Graph.h"
int main() {
	Graph* G=NULL;
	G = creat_graph(G);
	printf("此图的深度优先遍历(DFS):\n");
	Graph_DFS(G);
	printf("\n");
	printf("此图的广度优先遍历(BFS):\n");
	Graph_BFS(G);
	return 0;
}

由于测试的图如下所示:
在这里插入图片描述
程序的运行结果如下所示:
在这里插入图片描述
参考文献:
(1)《数据结构》——严蔚敏 吴伟民 编著
(2)参考的博文链接:[https://blog.csdn.net/Nicht_sehen/article/details/84306286
]

  • 6
    点赞
  • 51
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值