本博文源于浙江大学《数据结构》,主要完成了图的广度优先搜索,根据姥姥课上讲解内容,一步步完成,广度优先搜索的测试如下(文末完整代码):
我想对这样一个图进行遍历:
输入用例应该如下:
6 7
0 1 1
0 4 1
0 2 1
1 4 1
2 5 1
3 5 1
3 4 1
遍历结果如下
广度优先搜索基于队列实现,因此广搜本身代码不会很庞大,庞大是整个队列+图,如果大家学过stl,直接借助容器实现,那么给出广搜的代码
#define false 0
#define true 1
int Visited[MaxVertexNum]={false};
void BFS(MGraph Graph,Vertex S)
{
Queue Q;
Vertex V,W;
Q = CreateQueue(MaxVertexNum);
printf("%d->",Graph->Data[S]);
Visited[S]=true;
AddQ(Q,S);
while(!IsEmptyQ(Q)){
V = DeleteQ(Q);
for(W=0;W<Graph->Nv;W++)
{
if(Graph->G[V][W]==1&&!Visited[W]){
printf("%d-> ",Graph->Data[W]);
Visited[W]=true;
AddQ(Q,W);
}
}
}
}
void BFStraverse(MGraph Graph)
{
for(int i=0;i<Graph->Nv;i++){
if(!Visited[i])
BFS(Graph,i);
}
}
完整代码如下:
#include<stdio.h>
#include<stdlib.h>
#define MaxVertexNum 10
#define false 0
#define true 1
#define ERROR -1
#define INFINITY 1
typedef int WeightType;
typedef int DataType;
typedef struct GNode *PtrToGNode;
typedef int bool;
typedef int Position;
typedef int ElementType;
int Visited[MaxVertexNum]={false};
struct GNode {
int Nv; //一张图的顶点数
int Ne; //一张图的边数
WeightType G[MaxVertexNum][MaxVertexNum];
DataType Data[MaxVertexNum];//存顶点的数据
};
typedef PtrToGNode MGraph;//以邻接矩阵存储的图类型
//初始化一个有VertexNum 个顶点但没有边的图
typedef int Vertex;//用顶点下标表示顶点,为整型
MGraph CreateGraph(int VertexNum)
{
Vertex V, W;
MGraph Graph;
Graph = (MGraph)malloc(sizeof(struct GNode));
Graph->Nv = VertexNum;
Graph->Ne = 0;
//注意:这里默认顶点编号从0开始,到(Graph->Nv-1)
for(V=0; V<Graph->Nv;V++)
for(W=0; W<Graph->Nv;W++)
Graph->G[V][W] = 0;
for(int i =0;i<Graph->Nv;i++)
{
Visited[i]=false;
Graph->Data[i]=i;
}
return Graph;
}
typedef struct ENode *PtrToENode;
struct ENode {
Vertex V1,V2;//有向边<V1,V2>
WeightType Weight;//权值
};
typedef PtrToENode Edge;
struct QNode{
ElementType *Data;
int rear;
int front;
int MaxSize;
};
typedef struct QNode *Queue;
Queue CreateQueue(int MaxSize){
Queue Q = (Queue)malloc(sizeof(struct QNode));
Q->Data = (ElementType *) malloc(MaxSize * sizeof(ElementType));
Q->front = Q->rear = 0;
Q->MaxSize = MaxSize;
return Q;
}
int IsFullQ(Queue Q){
return ((Q->rear+1)%Q->MaxSize == Q->front);
}
void AddQ(Queue Q,ElementType item){
if(IsFullQ(Q)){
printf("Queue is full\n");
return ;
}
Q->rear = (Q->rear+1) %Q->MaxSize;
Q->Data[Q->rear] = item;
}
int IsEmptyQ(Queue Q){
return (Q->front == Q->rear);
}
ElementType DeleteQ(Queue Q){
if(IsEmptyQ(Q))
{
printf("Queue is empty\n");
return ERROR;
}else{
Q->front = (Q->front+1)%Q->MaxSize;
return Q->Data[Q->front];
}
}
void InsertEdge(MGraph Graph, Edge E)
{
Graph->G[E->V1][E->V2] = E->Weight;
Graph->G[E->V2][E->V1] = E->Weight;
}
void BFS(MGraph Graph,Vertex S)
{
Queue Q;
Vertex V,W;
Q = CreateQueue(MaxVertexNum);
printf("%d->",Graph->Data[S]);
Visited[S]=true;
AddQ(Q,S);
while(!IsEmptyQ(Q)){
V = DeleteQ(Q);
for(W=0;W<Graph->Nv;W++)
{
if(Graph->G[V][W]==1&&!Visited[W]){
printf("%d-> ",Graph->Data[W]);
Visited[W]=true;
AddQ(Q,W);
}
}
}
}
void BFStraverse(MGraph Graph)
{
for(int i=0;i<Graph->Nv;i++){
if(!Visited[i])
BFS(Graph,i);
}
}
MGraph BuildGraph()
{
MGraph Graph;
Edge E;
Vertex V;
int Nv,i;
scanf("%d",&Nv);
Graph = CreateGraph(Nv);
scanf("%d",&(Graph->Ne));
if(Graph->Ne != 0) {
E = (Edge)malloc(sizeof(struct ENode));
for(i=0;i<Graph->Ne;i++) {
scanf("%d %d %d",&E->V1,&E->V2,&E->Weight);
InsertEdge(Graph, E);
}
}
/*
for(V=0;V<Graph->Nv;V++)
scanf(" %c",&(Graph->Dadta[V]));
*/
return Graph;
}
int main()
{
MGraph Graph = BuildGraph();
int Nv = Graph->Nv;
BFStraverse(Graph);
return 0;
}