GitHub同步更新(已分类):Data_Structure_And_Algorithm-Review
公众号:URLeisure 的复习仓库
公众号二维码见文末
以下是本篇文章正文内容,下面案例可供参考。
广度优先搜索
和深度优先搜索
。广度优先搜索(Breadth First Search,BFS)
,又称宽度优先搜索,是最常见的图搜索方法之一。在上图中,设 A 为源点。
根据这个规律,我们可以用队列来实现广度优先遍历。
创建 vis[ ],来标记已被访问的顶点。
广度优先遍历经过的顶点及边,称为广度优先生成树
,简称 BFS树
。
如果是非连通图,则每一个连通分量会产生一棵 BFS树
,合在一起称为 BFS森林
。
算法步骤:
上图中示例:
(这是邻接矩阵的结果,如果是邻接表的话,结果是ACBED。因为邻接表的添加节点类似于头插法,后输入的在前面。)
初始化见完整代码。
c++代码如下(示例):
void BFS_AM(AMGraph G, VexType v) {
queue<VexType> Q;
int i = LocateVex(G, v);
vis[i] = true;
Q.push(v);
while (!Q.empty()) {
VexType u = Q.front();
cout << u << "\t";//可以把 u 存起来最后输出,也可以直接在这里输出
i = LocateVex(G, u);
Q.pop();
for (int j = 0; j < G.vexnum; j++) {
if (G.Edge[i][j] && !vis[j]) {
vis[j] = true;
Q.push(G.Vex[j]);
}
}
}
}
java代码如下(示例):
private static void BFS_AM(AMGraph g, String v) {
LinkedList<String> queue = new LinkedList<>();
int i = locateVex(g, v);
vis[i] = true;
queue.add(v);
while (!queue.isEmpty()) {
String u = queue.pop();
System.out.print(u + "\t");//可以把 u 存起来最后输出,也可以直接在这里输出
i = locateVex(g, u);
for (int j = 0; j < g.vexnum; j++) {
if (g.edge[i][j] != 0 && !vis[j]) {
vis[j] = true;
queue.add(g.vex[j]);
}
}
}
}
c++代码如下(示例):
void BFS_AL(ALGraph G, VexType v) {
queue<VexType> Q;
int i = LocateVex(G, v);
vis[i] = true;
Q.push(v);
while (!Q.empty()) {
VexType u = Q.front();
cout << u << "\t";//可以把 u 存起来最后输出,也可以直接在这里输出
Q.pop();
i = LocateVex(G, u);
AdjNode *p = G.Vex[i].first;
while (p) {
if (!vis[p->v]) {
Q.push(G.Vex[p->v].data);
vis[p->v] = true;
}
p = p->next;
}
}
}
java代码如下(示例):
private static void BFS_AL(ALGraph g, String v) {
LinkedList<String> queue = new LinkedList<>();
int i = locateVex(g, v);
vis[i] = true;
queue.add(v);
while (!queue.isEmpty()) {
String u = queue.pop();
System.out.print(u + "\t");//可以把 u 存起来最后输出,也可以直接在这里输出
i = locateVex(g, u);
AdjNode p = g.vex[i].first;
while (p != null) {
if (!vis[p.v]) {
vis[p.v] = true;
queue.add(g.vex[p.v].data);
}
p = p.next;
}
}
}
c++代码如下(示例):
void BFS(int x) {
queue<int> q;
flag[x] = true;
q.push(x);
while (!q.empty()) {
int u = q.front();
q.pop();
cout << u << "\t";//可以把 u 存起来最后输出,也可以直接在这里输出
for (int i = head[u]; ~i; i = e[i].next) {
int v = e[i].to;
if (!flag[v]) {
flag[v] = true;
q.push(v);
}
}
}
}
java代码如下(示例):
public static void bfs(int x) {
LinkedList<Integer> queue = new LinkedList<>();
flag[x] = true;
queue.add(x);
while (!queue.isEmpty()) {
int u = queue.pop();
System.out.print(u + "\t");
for (int i = head[u]; i != -1; i = e[i].next) {
int v = e[i].to;
if (!flag[v]) {
flag[v] = true;
queue.add(v);
}
}
}
}
c++代码如下(示例):
#include "iostream"
#include "cstring"
#include "queue"
using namespace std;
typedef char VexType;
typedef int EdgeType;
#define MAXN 100//注意,开太大的话二维数组受不了
struct AMGraph {
VexType Vex[MAXN];
EdgeType Edge[MAXN][MAXN];
int vexnum, edgenum;
};
bool vis[MAXN];
void Init() {
memset(vis, false, sizeof vis);
}
int LocateVex(AMGraph G, VexType x) {
for (int i = 0; i < G.vexnum; i++) {
if (G.Vex[i] == x) {
return i;
}
}
return -1;
}
void CreateAMGraph(AMGraph &G) {
cin >> G.vexnum >> G.edgenum;
for (int i = 0; i < G.vexnum; i++) {
cin >> G.Vex[i];
}
for (int i = 0; i < G.vexnum; i++) {
for (int j = 0; j < G.vexnum; j++) {
G.Edge[i][j] = 0;
}
}
VexType u, v;
while (G.edgenum--) {
cin >> u >> v;
int i = LocateVex(G, u);
int j = LocateVex(G, v);
if (i != -1 && j != -1) {
G.Edge[i][j] = 1;
} else {
cout << "输入了不存在的节点,请重新输入" << endl;
G.edgenum++;
}
}
}
void BFS_AM(AMGraph G, VexType v) {
queue<VexType> Q;
int i = LocateVex(G, v);
vis[i] = true;
Q.push(v);
while (!Q.empty()) {
VexType u = Q.front();
cout << u << "\t";
i = LocateVex(G, u);
Q.pop();
for (int j = 0; j < G.vexnum; j++) {
if (G.Edge[i][j] && !vis[j]) {
vis[j] = true;
Q.push(G.Vex[j]);
}
}
}
}
int main() {
Init();
AMGraph G;
CreateAMGraph(G);
VexType v;
cin >> v;
BFS_AM(G, v);
return 0;
}
/*
5 7
A B C D E
A B
A C
B D
B E
C B
C E
E D
A
*/
java代码如下(示例):
import java.util.Arrays;
import java.util.LinkedList;
import java.util.Scanner;
public class A {
private static Scanner sc = new Scanner(System.in);
private static final int MAX = 100;
private static class AMGraph {
String vex[] = new String[MAX];
int edge[][] = new int[MAX][MAX];
int edgenum, vexnum;
}
private static boolean vis[] = new boolean[MAX];
private static void init() {
Arrays.fill(vis, false);
}
private static int locateVex(AMGraph g, String x) {
for (int i = 0; i < g.vexnum; i++) {
if (g.vex[i].equals(x)) {
return i;
}
}
return -1;
}
private static void createAMGraph(AMGraph g) {
g.vexnum = sc.nextInt();
g.edgenum = sc.nextInt();
for (int i = 0; i < g.vexnum; i++) {
g.vex[i] = sc.next();
}
for (int i = 0; i < g.vexnum; i++) {
for (int j = 0; j < g.vexnum; j++) {
g.edge[i][j] = 0;
}
}
String u, v;
while (g.edgenum-- > 0) {
u = sc.next();
v = sc.next();
int i = locateVex(g, u);
int j = locateVex(g, v);
if (i != -1 && j != -1) {
g.edge[i][j] = 1;
} else {
System.out.println("输入了不存在的节点,请重新输入");
g.edgenum++;
}
}
}
private static void BFS_AM(AMGraph g, String v) {
LinkedList<String> queue = new LinkedList<>();
int i = locateVex(g, v);
vis[i] = true;
queue.add(v);
while (!queue.isEmpty()) {
String u = queue.pop();
System.out.print(u + "\t");
i = locateVex(g, u);
for (int j = 0; j < g.vexnum; j++) {
if (g.edge[i][j] != 0 && !vis[j]) {
vis[j] = true;
queue.add(g.vex[j]);
}
}
}
}
public static void main(String[] args) {
AMGraph g = new AMGraph();
init();
createAMGraph(g);
String v = sc.next();
BFS_AM(g, v);
}
}
/*
5 7
A B C D E
A B
A C
B D
B E
C B
C E
E D
A
*/
c++代码如下(示例):
#include "iostream"
#include "cstring"
#include "queue"
using namespace std;
typedef char VexType;
#define MAX 100
struct AdjNode {
int v;
AdjNode *next;
};
struct VexNode {
VexType data;
AdjNode *first;
};
struct ALGraph {
VexNode Vex[MAX];
int vexnum, edgenum;
};
bool vis[MAX];
void Init() {
memset(vis, false, sizeof vis);
}
void InsertEdge(ALGraph &G, int i, int j) {
AdjNode *s;
s = new AdjNode;
s->v = j;
s->next = G.Vex[i].first;
G.Vex[i].first = s;
}
int LocateVex(ALGraph G, VexType x) {
for (int i = 0; i < G.vexnum; i++) {
if (G.Vex[i].data == x) {
return i;
}
}
return -1;
}
void CreateALGraph(ALGraph &G) {
cin >> G.vexnum >> G.edgenum;
for (int i = 0; i < G.vexnum; i++) {
cin >> G.Vex[i].data;
G.Vex[i].first = nullptr;
}
VexType u, v;
while (G.edgenum--) {
cin >> u >> v;
int i = LocateVex(G, u);
int j = LocateVex(G, v);
if (i != -1 && j != -1) {
InsertEdge(G, i, j);
} else {
cout << "输入了不存在的节点,请重新输入" << endl;
G.edgenum++;
}
}
}
void BFS_AL(ALGraph G, VexType v) {
queue<VexType> Q;
int i = LocateVex(G, v);
vis[i] = true;
Q.push(v);
while (!Q.empty()) {
VexType u = Q.front();
cout << u << "\t";
Q.pop();
i = LocateVex(G, u);
AdjNode *p = G.Vex[i].first;
while (p) {
if (!vis[p->v]) {
Q.push(G.Vex[p->v].data);
vis[p->v] = true;
}
p = p->next;
}
}
}
int main() {
ALGraph G;
Init();
CreateALGraph(G);
VexType v;
cin >> v;
BFS_AL(G, v);
return 0;
}
/*
5 7
A B C D E
A B
A C
B D
B E
C B
C E
E D
A
*/
java代码如下(示例):
import java.util.Arrays;
import java.util.LinkedList;
import java.util.Scanner;
public class A {
private static Scanner sc = new Scanner(System.in);
private static final int MAX = 100;
private static class AdjNode {
int v;
AdjNode next;
}
private static class VexNode {
String data;
AdjNode first;
}
private static class ALGraph {
VexNode vex[] = new VexNode[MAX];
int vexnum, edgenum;
}
private static boolean vis[] = new boolean[MAX];
private static void init() {
Arrays.fill(vis, false);
}
private static void insertEdge(ALGraph g, int i, int j) {
AdjNode p = new AdjNode();
p.v = j;
p.next = g.vex[i].first;
g.vex[i].first = p;
}
private static int locateVex(ALGraph g, String x) {
for (int i = 0; i < g.vexnum; i++) {
if (g.vex[i].data.equals(x)) {
return i;
}
}
return -1;
}
private static void createALGraph(ALGraph g) {
g.vexnum = sc.nextInt();
g.edgenum = sc.nextInt();
for (int i = 0; i < g.vexnum; i++) {
g.vex[i] = new VexNode();
g.vex[i].data = sc.next();
g.vex[i].first = null;
}
String u, v;
while (g.edgenum-- > 0) {
u = sc.next();
v = sc.next();
int i = locateVex(g, u);
int j = locateVex(g, v);
if (i != -1 && j != -1) {
insertEdge(g, i, j);
} else {
System.out.println("输入了不存在的节点,请重新输入");
g.edgenum++;
}
}
}
private static void BFS_AL(ALGraph g, String v) {
LinkedList<String> queue = new LinkedList<>();
int i = locateVex(g, v);
vis[i] = true;
queue.add(v);
while (!queue.isEmpty()) {
String u = queue.pop();
System.out.print(u + "\t");
i = locateVex(g, u);
AdjNode p = g.vex[i].first;
while (p != null) {
if (!vis[p.v]) {
vis[p.v] = true;
queue.add(g.vex[p.v].data);
}
p = p.next;
}
}
}
public static void main(String[] args) {
ALGraph g = new ALGraph();
init();
createALGraph(g);
String v = sc.next();
BFS_AL(g, v);
}
}
/*
5 7
A B C D E
A B
A C
B D
B E
C B
C E
E D
A
*/
c++代码如下(示例):
#include
#include
#include
using namespace std;
const int N = 1e3;
struct Edge {
int to;
int next;
} e[N];
int head[N], cnt;
bool flag[N];
void Init() {
memset(head, -1, sizeof head);
memset(flag, false, sizeof flag);
cnt = 0;
}
void add(int u, int v) {
e[cnt].to = v;
e[cnt].next = head[u];
head[u] = cnt++;
}
void BFS(int x) {
queue<int> q;
flag[x] = true;
q.push(x);
while (!q.empty()) {
int u = q.front();
q.pop();
cout << u << "\t";//可以把 u 存起来最后输出,也可以直接在这里输出
for (int i = head[u]; ~i; i = e[i].next) {
int v = e[i].to;
if (!flag[v]) {
flag[v] = true;
q.push(v);
}
}
}
}
int main() {
Init();
int n;
cin >> n;
int u, v;
for (int i = 0; i < n; i++) {
cin >> u >> v;
add(u, v);
}
cin >> v;
BFS(v);
cout << endl;
return 0;
}
/*
7
1 2
1 3
2 4
2 5
3 2
3 5
5 4
1
*/
java代码如下(示例):
import java.util.Arrays;
import java.util.LinkedList;
import java.util.Scanner;
public class A {
public static Scanner sc = new Scanner(System.in);
public static final int N = 1000;
public static class Edge {
int to;
int next;
}
public static Edge e[] = new Edge[N];
public static int head[] = new int[N];
public static boolean flag[] = new boolean[N];
public static int cnt;
public static void init() {
Arrays.fill(head, -1);
Arrays.fill(flag, false);
cnt = 0;
}
public static void add(int u, int v) {
e[cnt] = new Edge();
e[cnt].to = v;
e[cnt].next = head[u];
head[u] = cnt++;
}
public static void bfs(int x) {
LinkedList<Integer> queue = new LinkedList<>();
flag[x] = true;
queue.add(x);
while (!queue.isEmpty()) {
int u = queue.pop();
System.out.print(u + "\t");
for (int i = head[u]; i != -1; i = e[i].next) {
int v = e[i].to;
if (!flag[v]) {
flag[v] = true;
queue.add(v);
}
}
}
}
public static void main(String[] args) {
init();
int n = sc.nextInt();
int v, u;
for (int i = 0; i < n; i++) {
u = sc.nextInt();
v = sc.nextInt();
add(u, v);
}
v = sc.nextInt();
bfs(v);
}
}
/*
7
1 2
1 3
2 4
2 5
3 2
3 5
5 4
1
*/
广度优先搜索的过程实质上是对每个顶点搜索其邻接点的过程,图的存储方式不同,其算法复杂度也不同。
时间复杂度:
时间复杂度为
O ( n 2 ) O(n^2) O(n2) 。空间复杂度:
空间复杂度为
O ( n ) O(n) O(n) 。时间复杂度:
时间复杂度为
O ( n + e ) O(n+e) O(n+e)。空间复杂度:
空间复杂度为
O ( n ) O(n) O(n)。图的邻接矩阵是唯一的,因此
基于邻接矩阵的 BFS 或 DFS 遍历序列也是唯一的
。
图的邻接表不是唯一的,边的输入顺序不同,正序或逆序建表都会影响邻接表的邻接点顺序,因此
基于邻接表的 BFS 或 DFS 遍历序列不是唯一的
。
下期预告:图的遍历_深度优先遍历