广度优先搜索BFS代码实现

main.cpp

#include<stdio.h>
#include<stdlib.h>
#include"BFS.h"
#include"Queue.h"

void main()
{
	printf("无向图\n");
	MGraph Graph;
	Graph = BuildGraph();
	BFS(Graph,1);
}

Queue.h

#ifndef QUEUE
#include"BFS.h"

#define MAX_QUEUE_INIT 100

typedef struct {
	int* base;
	int front;
	int rear;
}SqQueue;

void InitQueue(SqQueue* Q);
void getQueueLength(SqQueue Q);
void EnQueue(SqQueue* Q, int e);
void DeQueue(SqQueue* Q, Vertex* V);
bool IsEmpty(SqQueue Q);

#endif // !QUEUE
#pragma once

Queue.cpp

#include"Queue.h"
#include"stdlib.h"
#include"stdio.h"

bool IsEmpty(SqQueue Q)
{
	return Q.front == Q.rear ? true : false;
}



void InitQueue(SqQueue* Q)
{
	Q->base = (int*)malloc(sizeof(int) * MAX_QUEUE_INIT);
	if (!Q)
	{
		printf("error");
	}
	else {
		Q->front = Q->rear = 0;
	}
}

void getQueueLength(SqQueue Q)
{
	printf("\n length = %d \n", (Q.rear - Q.front + MAX_QUEUE_INIT) % MAX_QUEUE_INIT);
}

void EnQueue(SqQueue* Q, int e)
{
	if ((Q->rear + 1) % MAX_QUEUE_INIT == Q->front)
	{
		printf("full");
	}
	else
	{
		Q->base[Q->rear] = e;
		Q->rear = (Q->rear + 1) % MAX_QUEUE_INIT;
	}
}

void DeQueue(SqQueue* Q, Vertex* V)
{
	if (Q->rear == Q->front)
	{
		printf("no element");
	}
	else
	{
		*V = Q->base[Q->front];
		Q->front = (Q->front + 1) % MAX_QUEUE_INIT;
	}
	
}

BFS.h

#ifndef BFS

#define MaxVertexNum 100 //图的最大节点
#define INFINITY 0xFFFF

typedef int Vertex; //顶点下标
typedef int WeigthType; //边的权值
typedef int DataType; //顶点的数据类型

//边的定义
typedef struct ENode {
	Vertex v1, v2;
	WeigthType weigth;
}*PtrToENode;
typedef PtrToENode Edge;

//图的定义
typedef struct GNode {
	int Nv; //顶点数量
	int Ne; //边的数量
	WeigthType G[MaxVertexNum][MaxVertexNum];//邻接矩阵,存边的权重
	DataType Data[MaxVertexNum];//顶点的数据
}*PtrToGNode;
typedef PtrToGNode MGraph;


void InsertEdge(MGraph Graph, Edge E);
MGraph BuildGraph();
void BFS(MGraph Graph, Vertex S);


#endif // !BFS
#pragma once

BFS.cpp

#include"BFS.h"
#include"stdlib.h"
#include"stdio.h"
#include"Queue.h"

bool Visited[MaxVertexNum] = {false};
//创建一个没有边的图
MGraph CreatGraph(int VertexNum)
{
	Vertex V, W;
	MGraph Graph;

	Graph = (MGraph)malloc(sizeof(struct GNode));
	if (!Graph)
	{
		printf("Graph生成失败");
		exit(-1);
		return NULL;
	}
	Graph->Nv = VertexNum;
	Graph->Ne = 0;

	for (V = 0; V < Graph->Nv; V++)
	{
		for (W = 0; W < Graph->Nv; W++)
		{
			Graph->G[V][W] = INFINITY;
		}
	}

	return Graph;
}

//构建一个图
MGraph BuildGraph()
{
	int Nv, i;
	Vertex V;
	Edge E;
	MGraph Graph;

	printf_s("请输入插入节点的个数\n");
	scanf_s("%d", &Nv);

	Graph = CreatGraph(Nv);

	printf_s("请输入插入边的个数\n");
	scanf_s("%d", &(Graph->Ne));
	if (Graph->Ne != 0)
	{
		E = (Edge)malloc(sizeof(struct ENode));
		if (!E)
		{
			printf("E生成失败");
			exit(-1);
			return NULL;
		}
		for (i = 0; i < Graph->Ne; i++)
		{
			printf("请输入第%d条边:  V1,V2,权重\n", i);
			scanf_s("%d %d %d", &(E->v1), &(E->v2), &(E->weigth));
			InsertEdge(Graph, E);
		}
	}

	for (V = 0; V < Graph->Nv; V++)
	{
		printf("请输入第%d个顶点的数据\n", V);
		scanf_s("%d", &(Graph->Data[V]));
	}

	return Graph;
}



void Visit(Vertex V)
{
	printf("正在访问顶点%d\n", V);
}

bool IsEdge(MGraph Graph, Vertex V, Vertex W)
{
	return Graph->G[V][W] < INFINITY ? true : false;
}


/* IsEdge(Graph, V, W)检查<V, W>是否图Graph中的一条边,即W是否V的邻接点。  */
/* 此函数根据图的不同类型要做不同的实现,关键取决于对不存在的边的表示方法。*/
/* 例如对有权图, 如果不存在的边被初始化为INFINITY, 则函数实现如下:         */


/* Visited[]为全局变量,已经初始化为false */
void BFS(MGraph Graph, Vertex S)
{   /* 以S为出发点对邻接矩阵存储的图Graph进行BFS搜索 */
	SqQueue Q;
	Vertex V, W;

	InitQueue(& Q); /* 创建空队列, MaxSize为外部定义的常数 */
	/* 访问顶点S:此处可根据具体访问需要改写 */
	Visit(S);
	Visited[S] = true; /* 标记S已访问 */
	EnQueue(& Q, S); /* S入队列 */

	while (!IsEmpty(Q)) {
	    DeQueue(&Q,&V);  /* 弹出V */
		for (W = 0; W < Graph->Nv; W++) /* 对图中的每个顶点W */
			/* 若W是V的邻接点并且未访问过 */
			if (!Visited[W] && IsEdge(Graph, V, W)) {
				/* 访问顶点W */
				Visit(W);
				Visited[W] = true; /* 标记W已访问 */
				EnQueue(&Q, W); /* W入队列 */
			}
	} 
}



//插入边
void InsertEdge(MGraph Graph, Edge E)
{
	Graph->G[E->v1][E->v2] = E->weigth;
	Graph->G[E->v2][E->v1] = E->weigth;
}





  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

鄢广杰

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值