涉及知识:
关键词:数据结构 图 队列 bfs算法
本文所用函数参考书籍:《数据结构(c语言版 第2版)》严蔚敏 版本
题目:
【问题描述】建立一个有向图的邻接表存储,然后逐个链表输出,
然后从0号顶点出发对该图进行广度优先搜索,按顺序输出所访问的顶点
【输入形式】
5 5
A B C D E
AB AD BE DE DC
【输出形式】
A 3 1
B 4
C
D 2 4
E
ADBCE
代码展示:
#include <iostream>
using namespace std;
bool visited[100];
/*----------------------队列-----------------------------*/
#define ERROR 0
#define OK 1
#define OVERFLOW -2
typedef int Status;
typedef int QElemType; // 假设元素类型为int
#define MAXSIZE 10
typedef struct {
QElemType *base;
int front;
int rear;
} SqQueue;
//初始化
Status InitQueue(SqQueue &Q) {
Q.base = new QElemType[MAXSIZE];
//if (!Q.base) exit(OVERFLOW);
Q.front = Q.rear = 0;
return OK;
}
//入队
Status EnQueue(SqQueue &Q, QElemType e) {
if ((Q.rear + 1) % MAXSIZE == Q.front) return ERROR;
Q.base[Q.rear] = e;
Q.rear = (Q.rear + 1) % MAXSIZE;
return OK;
}
//出队
Status DeQueue(SqQueue &Q, QElemType &e) {
if (Q.rear == Q.front) return ERROR;
e = Q.base[Q.front];
Q.front = (Q.front + 1) % MAXSIZE;
return OK;
}
//判断空队
int QueueEmpty(SqQueue Q) {
if (Q.front == Q.rear) return 1;
else return 0;
}
/*---------------------------图---------------------------*/
//图(邻接表版)
#define MVNum 100
typedef char VerTexType;
typedef int ArcType;
typedef int Status;
typedef struct ArcNode {
int adjvex;
struct ArcNode *nextarc;
} ArcNode;//边结点
typedef struct VNode {
VerTexType data;
ArcNode *firstarc;
} VNode, AdjList[MVNum];//顶点
typedef struct {
AdjList vextices;
int vexnum, arcnum;
} ALGraph;//邻接表
//返回顶点位置
int LocateVex(ALGraph G, VerTexType u) {
int i = 0;
for (i = 0; i < G.vexnum; ++i) {
if (u == G.vextices[i].data)
return i;
}
return -1;
}
//创建有向图(邻接表版)
Status CreateUDG(ALGraph &G) {
int i = 0, j = 0, k = 0;
VerTexType v1, v2;
cin >> G.vexnum >> G.arcnum;
for (i = 0; i < G.vexnum; ++i) {
cin >> G.vextices[i].data;
G.vextices[i].firstarc = NULL;
}
for (k = 0; k < G.arcnum; ++k) {
cin >> v1 >> v2;
i = LocateVex(G, v1); j = LocateVex(G, v2);
ArcNode* p1;
p1 = new ArcNode;
p1->adjvex = j;
p1->nextarc = G.vextices[i].firstarc; G.vextices[i].firstarc = p1;
}
return OK;
}
//返回顶点u的第一个邻接顶点
int FirstAdjVex(ALGraph G, VerTexType u) {
int i = LocateVex(G, u);
if (i == -1) return -1;
if (G.vextices[i].firstarc != NULL) {
return G.vextices[i].firstarc->adjvex;
}
else {
return -1;
}
}
//返回顶点u的下一个邻接顶点
int NextAdjVex(ALGraph G, VerTexType u, int w) {
int i = LocateVex(G, u);
if (i == -1) return -1;
ArcNode *p = G.vextices[i].firstarc;
while (p != NULL && p->adjvex != w) {
p = p->nextarc;
}
if (p == NULL || p->nextarc == NULL) {
return -1;
}
else {
return p->nextarc->adjvex;
}
}
void ShowALG(ALGraph G) {
int i = 0,j = 0;
ArcNode* p1 = new ArcNode;
for (i = 0; i < G.vexnum; i++) {
p1 = G.vextices[i].firstarc;
cout << G.vextices[i].data<<" "; //先输出顶点信息
while (p1) {
cout << p1->adjvex << " "; //再输出邻接点
p1 = p1->nextarc;
}
cout << "\n";
}
}
//bfs
void BFS(ALGraph G, int v) {
SqQueue Q;
cout << G.vextices[v].data;
visited[v] = true;
InitQueue(Q);
EnQueue(Q, v);
QElemType u;
while (!QueueEmpty(Q)) {
DeQueue(Q, u);
for (int w = FirstAdjVex(G, G.vextices[u].data); w >= 0; w = NextAdjVex(G, G.vextices[u].data, w)) {
if (!visited[w]) {
cout << G.vextices[w].data;
visited[w] = true;
EnQueue(Q, w);
}
}
}
}
int main() {
ALGraph G;
CreateUDG(G);
ShowALG(G);
BFS(G, 0);
return 0;
}
注意: CreatUDG( )函数在构造邻接表时,采用了头插法插入新节点,因此输出时看到的节点就是倒过来的。
文章多用于作者自身对所学知识的梳理和记录。
如能帮到你,不胜荣幸。