当前位置:网站首页>图的邻接矩阵存储 C语言实现BFS
图的邻接矩阵存储 C语言实现BFS
2022-06-30 03:02:00 【黄昏和星空】
图的邻接矩阵存储,以及在此基础上实现的广度优先遍历(BFS)
/图的邻接矩阵+广度优先遍历/
#include<stdio.h>
#include<stdlib.h>
#define INFINITE 65535
#define MaxSize 100
typedef char VertexType;
typedef int EdgeType;
typedef int ElemType;
typedef struct{
ElemType data[MaxSize];
int front, rear;
}SqQueue;
typedef struct Graph{
VertexType vexs[MaxSize]; //顶点表
EdgeType arc[MaxSize][MaxSize]; //邻接矩阵
int vexNum, arcNum;
}Graph;
void InitQueue(SqQueue *Q); //初始化循环队列
int IsEmpty(SqQueue Q); //判队空
int EnQueue(SqQueue *Q, ElemType e); //入队
int DeQueue(SqQueue *Q, ElemType *e); //出队
/-----------------------------------------------------------------------------/
int visited[MaxSize];
SqQueue Q;
void CreateGraph(Graph *G); //创建图
void OutputGraph(Graph G); //输出图中所有顶点
int GetVexIndex(Graph G,VertexType vex); //根据顶点信息得出其对应的顶点序号
int FirstNeighbor(Graph G,int v); //输入一个顶点信息,输出与其相连的一个点的序号,若无则返回-1
int NextNeighbor(Graph G, int v, int w); //检测v除w以外的邻接点
void Visit(Graph G,int v); //访问顶点号为v的顶点
void BFS(Graph G,int v);
void BFSTraverse(Graph G);
void main(){
Graph G;
CreateGraph(&G);
OutputGraph(G);
BFSTraverse(G);
}
void Visit(Graph G, int v){
printf("%c ", G.vexs[v]);
}
void BFS(Graph G, int v){
Visit(G,v);
visited[v] = 1;
EnQueue(&Q, v);
while (!IsEmpty(Q)){
DeQueue(&Q, &v);
for (int w = FirstNeighbor(G, v); w >= 0; w = NextNeighbor(G, v, w)){
if (!visited[w]){
Visit(G, w);
visited[w] = 1;
EnQueue(&Q, w);
}
}
}
}
void BFSTraverse(Graph G){
printf(“\n广度优先遍历xulie:\n”);
int i;
for (i = 0; i < G.vexNum; i++)
visited[i] = 0;
InitQueue(&Q);
for (i = 0; i < G.vexNum; i++){
if (!visited[i])
BFS(G, i);
}
}
int FirstNeighbor(Graph G, int v){
int i;
for (i = 0; i < G.vexNum; i++){
if (G.arc[v][i] == 1){
return i;
}
}
return -1;
}
int NextNeighbor(Graph G, int v, int w){
int i;
for (i = w+1; i < G.vexNum; i++){
if (G.arc[v][i] == 1)
return i;
}
return -1;
}
void CreateGraph(Graph *G){
int i, j;
VertexType vs, ve;
printf(“请输入顶点数,边数:”);
scanf(“%d %d”, &G->vexNum, &G->arcNum);
for (i = 0; i < G->vexNum; i++){ //图的初始化
for (j = 0; j < G->vexNum; j++){
if (i == j)
G->arc[i][j] = 0;
else
G->arc[i][j] = INFINITE;
}
}
fflush(stdin);
for (i = 0; i < G->vexNum; i++){
printf(“请输入顶点%d:”, i+1);
scanf(“%c”, &G->vexs[i]);
getchar();
}
for (i = 0; i < G->arcNum; i++){
printf(“请输入起点,终点:”);
scanf(“%c %c”, &vs, &ve);
getchar();
int vs_index=0, ve_index=0;
for (j = 0; j < G->vexNum; j++){ //找到起始点、终点下标
if (vs == G->vexs[j])
vs_index = j;
if (ve == G->vexs[j])
ve_index = j;
}
G->arc[vs_index][ve_index] = 1; //无向图,上下三角都要保存
G->arc[ve_index][vs_index] = 1;
}
}
void OutputGraph(Graph G){
int i, j;
printf(“图中顶点:\n”);
for (i = 0; i < G.vexNum; i++){
printf(“%c “, G.vexs[i]);
}
printf(”\n图中的弧:\n”);
for (i = 0; i < G.vexNum; i++){
for (j = 0; j < G.vexNum; j++){
if (G.arc[i][j] == 1){
printf(“%c------>%c\n”, G.vexs[i], G.vexs[j]);
}
}
}
}
int GetVexIndex(Graph G, VertexType vex){
int i;
for (i = 0; i < G.vexNum; i++){
if (G.vexs[i] == vex)
return i;
}
return -1;
}
/队列基本操作实现/
void InitQueue(SqQueue *Q){
Q->rear = Q->front = 0;
}
int IsEmpty(SqQueue Q){
if (Q.rear == Q.front)
return 1;
else
return 0;
}
int EnQueue(SqQueue *Q, ElemType e){
if ((Q->rear + 1) % MaxSize == Q->front) //队满
return 0;
Q->data[Q->rear] = e;
Q->rear = (Q->rear + 1) % MaxSize;
return 1;
}
int DeQueue(SqQueue *Q, ElemType *e){
if (Q->front == Q->rear) //队空
return 0;
*e = Q->data[Q->front];
Q->front = (Q->front + 1) % MaxSize;
return 1;
}
运行结果,如图:

边栏推荐
- 2022 underground coal mine electrical test and underground coal mine electrical simulation test
- Idea remote debugging remote JVM debug
- HTA入门基础教程 | VBS脚本的GUI界面 HTA简明教程 ,附带完整历程及界面美化
- Summary of interview and Employment Questions
- How to modify and add fields when MySQL table data is large
- Azure 开发者新闻快讯丨开发者6月大事记一览
- Prompt learning a blood case caused by a space
- Link garbled escape character
- 中断操作:AbortController学习笔记
- Formal and actual parameters, value passing and address passing
猜你喜欢

Distributed file storage system fastdfs hands on how to do it

General paging (2)

【微信小程序】条件渲染 列表渲染 原来这样用?

HOOK Native API

Three solutions to forced hibernation of corporate computers

The MariaDB database was found 12 hours late

Call collections Sort() method, compare two person objects (by age ratio first, and by name ratio for the same age), and pass lambda expression as a parameter.

简单自定义mvc

Use of Arthas

Compile a DLL without import table
随机推荐
JS cross reference
问题记录:fel_lib.c:26:10: fatal error: libusb.h: 没有那个文件或目录
What is the concept of string in PHP
【直播笔记0629】 并发编程二:锁
oracle怎么设置密码复杂度及超时退出的功能
原生JS怎么生成九宫格
Redis+AOP怎么自定义注解实现限流
Jvxetable sub table record loading completion event
自定义MVC的使用
What kind of foreign exchange trading platform is regulated and safe?
Servlet面试题
个人PC安装软件
Comparable和Comparator的区别
HTA introductory basic tutorial | GUI interface of vbs script HTA concise tutorial, with complete course and interface beautification
How do I enable assembly binding logging- How can I enable Assembly binding logging?
Auto.js学习笔记16:按项目保存到手机上,不用每次都保存单个js文件,方便调试和打包
Heavy attack -- ue5's open source digital twin solution
Simple custom MVC
(graph theory) connected component (template) + strongly connected component (template)
Use of Arthas