#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#define MaxSize 100
#define INF 1000001
typedef struct EageTable //锟竭憋拷
{
int eageData; //值
struct EageTable *next; //指锟斤拷锟斤拷一锟斤拷锟斤拷
}EageTable;
typedef struct HeadTable //头锟斤拷
{
int headData; //值
EageTable *first; //指锟斤拷头锟节碉拷锟斤拷锟斤拷一锟斤拷锟节碉拷
}HeadTable,HeadList[MaxSize];
typedef struct{
HeadList Graph;
int headNum,arcNum; //头锟节碉拷锟斤拷 锟斤拷锟斤拷
}Graph;
typedef struct LQueue //锟斤拷锟斤拷
{
int Queue[MaxSize];
int front,rear;
}LQueue;
bool vis[MaxSize] = {false};
int dis[MaxSize];
void InitQueue(LQueue *Q);
bool JudgeEmpty(LQueue *Q);
void EnQueue(LQueue *Q,int num);
int DeQueue(LQueue *Q);
int GetFront(LQueue *Q);
void InitGraph(Graph *G);
void CreateGraph(Graph *G,int flag,int a,int b);
void BFS(LQueue *Q,Graph *G,int front);
void TraverBFS(LQueue *Q,Graph *G,int start);
/*锟斤拷始锟斤拷锟斤拷锟斤拷*/
void InitQueue(LQueue *Q)
{
Q->front = Q->rear = 0;
}
/*锟叫断讹拷锟斤拷锟角凤拷为锟斤拷*/
bool JudgeEmpty(LQueue *Q)
{
if(Q->front == Q->rear)return true;
else return false;
}
/*锟斤拷锟斤拷*/
void EnQueue(LQueue *Q,int num)
{
Q->Queue[Q->rear++] = num;
}
/*锟斤拷锟斤拷*/
int DeQueue(LQueue *Q)
{
int temp = Q->Queue[Q->front];
Q->front++;
return temp;
}
int GetFront(LQueue *Q)
{
return Q->Queue[Q->front];
}
/*锟斤拷始锟斤拷锟节接憋拷锟斤拷头*/
void InitGraph(Graph *G)
{
int i;
for(i = 1;i <= G->headNum;i++)
{
G->Graph[i].headData = i;
G->Graph[i].first = NULL;
}
}
/*锟斤拷锟斤拷锟节接憋拷*/
void CreateGraph(Graph *G,int flag,int a,int b)
{
EageTable* eage;
eage = (EageTable*)malloc(sizeof(EageTable));
eage->eageData = b;
//头锟斤拷锟斤拷
eage->next = G->Graph[a].first;
G->Graph[a].first = eage;
//图为锟斤拷锟斤拷图时
if(flag == 2)
{
EageTable* eagee;
eagee = (EageTable*)malloc(sizeof(EageTable));
eagee->eageData = a;
//头锟斤拷锟斤拷
eagee->next = G->Graph[b].first;
G->Graph[b].first = eagee;
}
}
/*锟斤拷锟斤拷*/
void BFS(LQueue *Q,Graph *G,int front)
{
EageTable *eage = G->Graph[front].first;//eage锟斤拷front锟节碉拷锟侥诧拷锟斤拷
while(eage)//锟斤拷锟斤拷
{
if(vis[eage->eageData] == false)//没锟斤拷锟斤拷锟绞的节碉拷锟斤拷栈 锟斤拷 锟斤拷志锟节碉拷锟窖讹拷
{
EnQueue(Q,eage->eageData);
vis[eage->eageData] = true;
dis[eage->eageData] = dis[front] + 1;
}
eage = eage->next;
}
}
void TraverBFS(LQueue *Q,Graph *G,int start)
{
InitQueue(Q);
EnQueue(Q,start);
vis[start] = true;//锟斤拷志锟节碉拷锟窖讹拷
dis[start] = 0;
while(!JudgeEmpty(Q))
{
int front = GetFront(Q);//锟斤拷取锟斤拷锟叫碉拷一锟斤拷锟节碉拷
printf("%-2d",front);
BFS(Q,G,front);
DeQueue(Q);
}
}
int main(int argc, char const *argv[]) {
Graph G;
LQueue Q;
int i,flag,start;
memset(dis,INF,sizeof(int)*MaxSize);
printf("锟斤拷锟斤拷图锟斤拷锟斤拷1锟斤拷锟斤拷锟斤拷图锟斤拷锟斤拷2:");
scanf("%d",&flag);
printf("锟斤拷锟斤拷锟斤拷锟节碉拷锟斤拷:");
scanf("%d",&G.headNum);
InitGraph(&G);
printf("锟斤拷锟斤拷锟诫弧锟斤拷锟斤拷锟斤拷(锟斤拷1锟斤拷始):");
scanf("%d",&G.arcNum);
printf("锟斤拷锟斤拷%d锟斤拷锟斤拷锟斤拷:\n",G.arcNum);
for(i = 0;i < G.arcNum;i++)
{
int a,b;
scanf("%d %d",&a,&b);
CreateGraph(&G,flag,a,b);
}
printf("锟斤拷锟斤拷锟诫开始锟侥节碉拷:");
scanf("%d",&start);
printf("BFS锟斤拷锟斤拷:\n");
TraverBFS(&Q,&G,start);
printf("\n锟斤拷锟姐单源锟斤拷锟斤拷路锟斤拷锟斤拷:\n");
for(i = 1;i <= G.headNum;i++)
printf("%-d%c",dis[i]," \n"[i == G.headNum]);
return 0;
}
王道考研 ++++ BFS单源最短路径
猜你喜欢
转载自blog.csdn.net/WX_1218639030/article/details/99117114
今日推荐
周排行