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
#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
#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;
}
void BFS(MGraph Graph, Vertex S)
{
SqQueue Q;
Vertex V, W;
InitQueue(& Q);
Visit(S);
Visited[S] = true;
EnQueue(& Q, S);
while (!IsEmpty(Q)) {
DeQueue(&Q,&V);
for (W = 0; W < Graph->Nv; W++)
if (!Visited[W] && IsEdge(Graph, V, W)) {
Visit(W);
Visited[W] = true;
EnQueue(&Q, W);
}
}
}
void InsertEdge(MGraph Graph, Edge E)
{
Graph->G[E->v1][E->v2] = E->weigth;
Graph->G[E->v2][E->v1] = E->weigth;
}