广度优先搜索
有点像是树的层次遍历,都需要使用到队列!
邻接矩阵存储结构无向图——广度优先搜索
#include <iostream>
#include<cmath>
#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
#define MAXSIZE 100
//定义无穷大
#define MAXInt 32767
typedef int Status;
typedef char TElemType;
using namespace std;
//定义顶点数据类型
typedef char VerTexType;
//定义边的数据类型
typedef int ArcType;
//定义图的数据结构
typedef struct {
//顶点表
VerTexType vexs[MAXSIZE];
//邻接矩阵
ArcType arcs[MAXSIZE][MAXSIZE];
//当前结点数和当期边数
int vexnum, arcnum;
}AMGraph; //Adjacency(邻接) Matrix Graph
//函数输入一个顶点的值,返回该顶点在顶点表中的下标,不存在则返回-1
int LocateVex(VerTexType x, AMGraph& G) {
for (int i = 0; i < G.vexnum; i++)
{
if (x == G.vexs[i])
{
return i;
}
}
return -1;
}
void createUDN(AMGraph& G) {
//先输入图的顶点数和边数
cout << "请输入要创建的无向图的顶点数和边数:" << "\n";
scanf_s("%d%d", &G.vexnum, &G.arcnum);
//依次输入各个顶点的值
cout << "依次输入各个顶点的值" << "\n";
for (int i = 0; i < G.vexnum; i++) {
cin >> G.vexs[i];
}
//对邻接矩阵进行初始化
for (int i = 0; i < G.vexnum; i++) {
for (int j = 0; j < G.vexnum; j++) {
G.arcs[i][j] = 0;
}
}
cout << "请输入每条边的顶点和权值:输入方式:顶点1 顶点2" << "\n";
//输入依次输入每一条边的顶点和权值
for (int i = 0; i < G.arcnum; i++)
{
VerTexType a, b;
cin >> a >> b;
//找到输入的顶点a,b在顶点表中的下标
int x = LocateVex(a, G);
int y = LocateVex(b, G);
if (x != -1 && y != -1) {
G.arcs[x][y] = 1;
G.arcs[y][x] = 1;
}
}
}
//输出顶点表以及邻接矩阵
void outPut(AMGraph G) {
cout << "顶点表如下:" << "\n";
for (int i = 0; i < G.vexnum; i++) {
printf("%c ", G.vexs[i]);
}
printf("\n输出邻接矩阵如下\n");
for (int i = 0; i < G.vexnum; i++)
{
for (int j = 0; j < G.vexnum; j++)
{
printf("%6d", G.arcs[i][j]);
}
printf("\n");
}
}
//定义队列
typedef int QElem;
typedef struct SqQueue {
QElem* base;
int front;
int rear;
};
Status InitQ(SqQueue& q) {
q.base = new QElem[MAXSIZE];
if (!q.base)
{
return FALSE;
}
q.front = q.rear = 0;
return TRUE;
}
//入队和出队
Status EnQ(SqQueue& q, QElem e) {
if ((q.rear+1)%MAXSIZE!=q.front)
{
q.base[q.rear] = e;
q.rear = (q.rear + 1) % MAXSIZE;
return OK;
}
return ERROR;
}
Status DeQ(SqQueue& q, QElem& e) {
if (q.rear==q.front)
{
return ERROR;
}
e = q.base[q.front];
q.front = (q.front + 1) % MAXSIZE;
return OK;
}
bool IsEmpty(SqQueue q) {
if (q.front==q.rear)
{
return true;
}
return false;
}
//广度优先遍历,从下标为v的结点开始进行广度优先搜索
int visited[MAXSIZE];
void BFS(AMGraph G,int v) {
SqQueue q;
InitQ(q);
EnQ(q, v);
visited[v]=1;
cout << "\n图G的广度搜索为:\n" << "";
while (!IsEmpty(q))
{
int w;
DeQ(q, w);
cout << G.vexs[w] << " ";
//寻找与w下标结点相邻的且未被访问过的结点
for (int i = 0; i < G.vexnum; i++)
{
//这个结点没有被访问过而且和v结点是相邻的
if (visited[i]==0&&(G.arcs[w][i]!=0))
{
visited[i] = 1;
EnQ(q, i);
}
}
}
}
int main() {
AMGraph G;
createUDN(G);
outPut(G);
for (int i = 0; i < G.vexnum; i++)
{
visited[i] = 0;
}
cout << "\n请输入广度优先搜索开始的结点的值:\n" << "";
VerTexType x;
cin >> x;
cout << "\n广度优先搜索遍历的顺序为:" << "\n";
BFS(G, LocateVex(x, G));
return 0;
}
邻接表存储结构无向图——广度优先搜索
#include<iostream>
using namespace std;
#define OK 1
#define ERROR 0
#define FALSE 0
#define TRUE 1
#define MAXNum 100
//定义无穷大
#define MAXInt 32767
typedef int Status;
//定义顶点数据类型
typedef char VerTexType;
//定义节点权重的类型
typedef int OtherInfo;
//定义边的数据类型
typedef struct ArcNode {
int adjvex;//该边指向的顶点的位置
ArcNode* nextarc;//指向下一条边的指针
OtherInfo info;//与边相关的信息
}ArcNode;
//定义表结点的类型
typedef struct VNode {
VerTexType data;//顶点信息
ArcNode* firstarc;//指向第一条依附该顶点的边的指针
}VNode, AdjList[MAXNum];//AdjList是邻接表类型
//图结构的类型
typedef struct {
AdjList vertices;//邻接表
int vexnum, arcnum;//图当前顶点数和弧数
}ALGraph;//图的定义
//该函数查找顶点x在图的顶点表中的下标
int LocateVex(ALGraph G, VerTexType x) {
for (int i = 0; i < G.vexnum; i++) {
if (G.vertices[i].data == x) {
return i;
}
}
return -1;
}
//邻接表的创建——无向图
Status createUDN(ALGraph& G) {
cout << "请输入无向图的总结点的数目和总的边数" << "\n";
cin >> G.vexnum >> G.arcnum;
cout << "请输入无向图中的各个顶点" << "\n";
for (int i = 0; i < G.vexnum; i++)
{
cin >> G.vertices[i].data;
//初始化表头结点的指针域
G.vertices[i].firstarc = NULL;
}
cout << "请输入每条边的信息:(顶点1 顶点2)" << "\n";
for (int i = 0; i < G.arcnum; i++) {
VerTexType x, y;
cin >> x >> y;
//查找两个顶点的下标
int xIndex = LocateVex(G, x);
int yIndex = LocateVex(G, y);
//找到这两个顶点的下标之后就可以往firstarc域插入表结点了
ArcNode* xArcNode = new ArcNode;
ArcNode* yArcNode = new ArcNode;
if (xIndex != -1 && yIndex != -1) {
//在x结点后面插入邻接y结点的信息
xArcNode->info = 1;
xArcNode->adjvex = yIndex;
xArcNode->nextarc = G.vertices[xIndex].firstarc;
G.vertices[xIndex].firstarc = xArcNode;
//在y结点后面插入邻接x结点的信息
yArcNode->info = 1;
yArcNode->adjvex = xIndex;
yArcNode->nextarc = G.vertices[yIndex].firstarc;
G.vertices[yIndex].firstarc = yArcNode;
}
else
{
return ERROR;
}
}
return OK;
}
void outPut(ALGraph G) {
cout << "输出顶点表如下:" << "\n";
for (int i = 0; i < G.vexnum; i++)
{
cout << G.vertices[i].data << " ";
}
cout << "\n输入邻接表如下:" << "\n";
for (int i = 0; i < G.vexnum; i++)
{
printf("%c顶点相邻接的顶点为:\n", G.vertices[i].data);
ArcNode* p = G.vertices[i].firstarc;
while (p) {
printf("%c ", G.vertices[p->adjvex].data);
p = p->nextarc;
}
printf("\n");
}
}
//定义队列
typedef int QElem;
typedef struct SqQueue {
QElem* base;
int front;
int rear;
};
Status InitQ(SqQueue& q) {
q.base = new QElem[MAXNum];
if (!q.base)
{
return FALSE;
}
q.front = q.rear = 0;
return TRUE;
}
//入队和出队
Status EnQ(SqQueue& q, QElem e) {
if ((q.rear+1)%MAXNum!=q.front)
{
q.base[q.rear] = e;
q.rear = (q.rear + 1) % MAXNum;
return OK;
}
return ERROR;
}
Status DeQ(SqQueue& q, QElem& e) {
if (q.rear==q.front)
{
return ERROR;
}
e = q.base[q.front];
q.front = (q.front + 1) % MAXNum;
return OK;
}
bool IsEmpty(SqQueue q) {
if (q.front==q.rear)
{
return true;
}
return false;
}
//广度优先遍历,从下标为v的结点开始进行广度优先搜索
int visited[MAXNum];
void BFS(ALGraph G,int v) {
SqQueue q;
InitQ(q);
EnQ(q, v);
visited[v]=1;
cout << "\n图G的广度搜索为:\n" << "";
while (!IsEmpty(q))
{
int w;
DeQ(q, w);
cout << G.vertices[w].data << " ";
ArcNode* p = G.vertices[w].firstarc;//获取第一个与w下标结点相邻的结点
//寻找与w下标结点相邻的且未被访问过的结点
while (p)
{
if (visited[p->adjvex] == 0) {
EnQ(q, p->adjvex);
visited[p->adjvex] = 1;//设置p->adjvex下标对应的结点已经被访问
}
p = p->nextarc;//查找下一个结点
}
}
}
int main() {
ALGraph G;
createUDN(G);
outPut(G);
for (int i = 0; i < G.vexnum; i++)
{
visited[i] = 0;
}
cout << "\n请输入广度优先搜索开始的结点的值:\n" << "";
VerTexType x;
cin >> x;
cout << "\n广度优先搜索遍历的顺序为:" << "\n";
BFS(G, LocateVex(G,x));
return 0;
}
测试样例:
邻接矩阵存储方式:
邻接表存储结构:
BFS算法效率:
- 如果使用邻接矩阵,则BFS对于每一个被访问到的顶点,都要循环检测矩阵中的整整一行(n个元素),总的时间代价为O(n2)
- 如果使用邻接表,虽然有2e个表结点,但自需要扫描e个结点即可完成遍历,加上访问n个头结点的时间,时间复杂度为O(n+e)
DFS和BFS算法比较:
- 空间复杂度:都是O(n)(借助了堆栈/队列)
- 时间复杂度只和存储结构(邻接矩阵/邻接表)有关,与搜索路径无关