广度优先搜索算法(Breadth First Search),又称为”宽度优先搜索”或”横向优先搜索”,简称BFS。
一、算法思路
算法思想:从图中某顶点v出发,在访问了v之后依次访问v的各个未曾访问过的邻接点,然后再分别从这些邻接点出发依次访问它们的邻接点(邻接节点的邻接节点)。
换句话说,广度优先搜索遍历图的过程是以v为起点,由近至远,依次访问和v有路径相通且路径长度为1,2…的顶点。
1. 无向图的广度优先搜索
下面以”无向图”为例,来对广度优先搜索进行演示。
第1步:访问A(起始节点)。
第2步:依次访问C,D,F(都是A的邻接点)。
在访问了A之后,接下来访问A的邻接点。为了形象的描述,我们假设该无向图用邻接矩阵表示(如下),A所在的行中为1的部分表示与节点A有直接联系,是A的邻接点。那么就可以按照访问一维数组一样,从左到右依次A所在的行,通过标识1来判断是否为A的邻接点。由此,便产生了一个访问了顺序,所以在A的邻接点中,访问顺序是C-D-F。
第3步:依次访问B,G。
在第2步访问完C,D,F之后,再依次访问它们的邻接点。首先访问C的邻接点B,再访问F的邻接点G(程序中可以依次遍历C所在的行和F所在的行)。
第4步:访问E。
在第3步访问完B,G之后,再依次访问它们的邻接点。只有G有邻接点E,因此访问G的邻接点E。
因此访问顺序是:A -> C -> D -> F -> B -> G -> E
2. 有向图的广度优先搜索
下面以”有向图”为例,来对广度优先搜索进行演示。
第1步:访问A(起始点)。
第2步:访问B(A的邻接点)。
第3步:依次访问C,E,F(B的邻接点)。
在访问了B之后,接下来访问B的出边的另一个顶点,即C,E,F。判断过程跟上述无向图的一样,就是通过访问邻接矩阵中的B行。
第4步:依次访问D,G。
在访问完C,E,F之后,再依次访问它们的出边的另一个顶点。还是按照C,E,F的顺序访问,C的已经全部访问过了,那么就只剩下E,F;先访问E的邻接点D,再访问F的邻接点G。
因此访问顺序是:A -> B -> C -> E -> F -> D -> G
二、应用举例
1. 图:最短路径
求任意两个节点之间的最短距离。
bfs.h
#ifndef _BFS_H_
#define _BFS_H_
struct node
{
int iOrder; //节点序号
int iDisten; //节点与起始节点的距离
};
// 广度优先搜索
class BFS
{
public:
BFS(int(*arr)[100] = nullptr, int nod = 0);
void handle(int beg,int tar); // beg: 起始点 tar: 目的点
private:
int (*array_t)[100]; //邻接矩阵
int node_n; //节点个数
};
#endif
bfs.cpp
#include "bfs.h"
#include "string.h"
#include <iostream>
using namespace std;
BFS::BFS(int (*arr)[100]/* = nullptr */, int nod /* = 0 */){
array_t = arr;
node_n = nod;
}
void BFS::handle(int beg, int tar)
{
struct node * que = new struct node[node_n+1]; //
int head = 1;
int tail = 1;
que[tail].iOrder = beg; // 起始点
que[tail].iDisten = 0;
tail++;
bool * visited = new bool[node_n+1]; // 记录节点是否访问过。
memset(visited, 0, node_n+1);
visited[beg] = 1; // 初始化起始节点已经访问过。
int cur; //当前结点
bool flag = 0;
while ( head < tail )
{
cur = que[head].iOrder;
for (int i = 1; i <= node_n;i++) // 依次询问 cur 节点所能访问的节点
{
if (array_t[cur][i] != -1 && visited[i] == 0)
{
que[tail].iDisten = que[head].iDisten + 1;
que[tail].iOrder = i;
tail++;
visited[i] = 1;
}
if (que[tail-1].iOrder == tar) // 判断是否已经到达目的点
{
flag = true;
break;
}
}
if (flag)
{
break;
}
head++;
}
cout << que[tail - 1].iDisten << endl;
delete[] que;
delete[] visited;
}
测试代码:
#include <iostream> // for cout
#include <string.h> // for memset
#include "bfs.h"
#define N 100
using namespace std;
int city_n; //城市的数量
int road_n; //道路的数量
int minDis = -1; //最短路程 -1 表示此数值无效
int visited[N]; //已经路过的城市
int edge[N][N]; //邻接矩阵 : -1 表示此路不通
int city_tar; //目标城市
int main()
{
cout << "输入城市数量 和 路的总数【eg :5 8 总共5个城市 8条单向通道】" << endl;
cin >> city_n >> road_n;
// 初始化邻接矩阵
memset(*edge, -1, 4 * N*(city_n+1));
for (int i = 0; i <= city_n; i++)
{
edge[i][i] = 0; // 自己到自己的距离是0
}
cout << "输入城市之间的道路情况【eg : 1 2 8 城市1到2的路程为8】" << endl;
int city_s, city_d; //路的起始城市,和 目的城市
for (int i = 0; i < road_n; i++)
{
cin >> city_s >> city_d ;
edge[city_s][city_d] = 1;
edge[city_d][city_s] = 1;
}
/* 输入出发点 和 目标点 */
int city_cur;
cout << "输入出发点 和 目标点【eg : 1 5 从城市1出发到城市5】" << endl;
BFS bfs(edge,city_n);
while (true)
{
cin >> city_cur >> city_tar;
if (city_tar == 0 || city_cur == 0 )
break;
if (city_cur > city_n || city_tar > city_n)
{
cout << "wrong input" << endl;
continue;
}
bfs.handle(city_cur, city_tar);
}
return 0;
}
测试数据
5 7 //5 个节点 7条路
1 2
1 3
2 3
2 4
3 4
3 5
4 5
测试结果:
2. 迷宫
以下是一个迷宫的示意图,标有1的空格表示为障碍物,要求从起始位置到目的位置的最短步数。注意:每次只能在当前位置向上下左右四个方向移动一格,要求只能在迷宫内移动,并且遇到障碍物只能绕道而行。
代码如下:
#include <iostream>
#include <algorithm>
#include <iterator>
using namespace std;
struct info
{
int x; //位置
int y;
int dis; //从起始位置到(x,y)需要的步数
};
int direct[4][2] = {{ 0, 1 }, { 1, 0 }, { 0, -1 }, {-1,0}};
info que[2501];
int map[51][51];
int visited[51][51];
int row;
int col;
int tarx,tary;
void mazebfs(int x,int y) //起始位置(x,y)
{
int head = 1;
int tail = 1;
que[tail].x = x;
que[tail].y = y;
que[tail].dis = 0;
tail++;
int tx;
int ty;
int flag = 0;
while(1)
{
info cur = que[head];
for(int k = 0; k < 4; k++)
{
tx = cur.x + direct[k][0];
ty = cur.y + direct[k][1];
if(tx < 1 || tx > row || ty < 1 || ty > col)
continue;
if( map[tx][ty] == 0 && visited[tx][ty] == 0)
{
visited[tx][ty] = 1;
que[tail].x = tx;
que[tail].y = ty;
que[tail].dis = cur.dis + 1;
tail++;
if(tx == tarx && ty == tary)
{
flag = 1;
break;
}
}
}
if(flag == 1)
{
break;
}
head++;
}
cout << que[tail-1].dis <<endl;
return ;
}
int main()
{
cout << "input row & col "<< endl;
cin >> row >> col;
cout << "input map info "<<endl;
for(int i = 1; i <= row ; i++)
{
for(int j = 1; j <= col; j++)
{
cin >> map[i][j];
}
}
cout << "input start pos(x,y) & target pos(x,y) "<<endl;
int sx,sy;
cin >> sx >> sy >> tarx >> tary;
mazebfs(sx,sy);
return 0;
}
测试数据:
5 4
0 0 1 0
0 0 0 0
0 0 1 0
0 1 0 0
0 0 0 1
1 1 4 3
测试结果: