当前位置:网站首页>图的邻接矩阵存储 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;
}
运行结果,如图:

边栏推荐
- On the role of database tables
- Mysql表数据比较大情况下怎么修改添加字段
- 公司电脑强制休眠的3种解决方案
- Auto.js学习笔记16:按项目保存到手机上,不用每次都保存单个js文件,方便调试和打包
- The broadcast module code runs normally in autojs4.1.1, but an error is reported in pro7.0 (not resolved)
- Tp6 framework integrates JWT for token authentication
- 可视化HTA窗体设计器-HtaMaker 界面介绍及使用方法,下载 | HTA VBS可视化脚本编写
- 广播模块代码在autojs4.1.1版本运行正常,但在pro7.0版本上运行报错(未解决)
- How to switch ipykernel to a different CONDA virtual environment in jupyterlab?
- C # basic learning (XIII) | breakpoint debugging
猜你喜欢

简单自定义mvc

LeetCode 3. Longest substring without duplicate characters

IDEA 远程调试 Remote JVM Debug

Mysql表数据比较大情况下怎么修改添加字段

DC/DC变换器轻载时三种工作模式的原理及优缺点

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的使用

中断操作:AbortController学习笔记
![[live broadcast notes 0629] Concurrent Programming II: lock](/img/5c/42f5c9a9969b4d2bb950a7caac5555.png)
[live broadcast notes 0629] Concurrent Programming II: lock

Distributed file system fastdfs
随机推荐
Auto. JS learning notes 16: save to the mobile phone by project, instead of saving a single JS file every time, which is convenient for debugging and packaging
WPF Initialized事件在.cs中绑定不被触发的原因
Three solutions to forced hibernation of corporate computers
GTK interface programming (II): key components
How do I enable assembly binding logging- How can I enable Assembly binding logging?
链接乱码转义符
Cross domain, CORS, jsonp
Cmake tutorial series -04- compiling related functions
简单自定义mvc
Quick sort, cluster index, find the k-largest value in the data
Auto.js学习笔记15:autojs的UI界面基础篇2
编译一个无导入表的DLL
Visual HTA form designer htamaker interface introduction and usage, Download | HTA VBS visual script writing
华为面试题: 高矮个子排队
How to use redis to realize the like function
Jvxetable sub table record loading completion event
Some technology sharing
【实战技能】如何撰写敏捷开发文档
oracle怎么设置密码复杂度及超时退出的功能
发现mariadb数据库时间晚了12个小时