图上BFS
将BFS扩展到图,从顶点0开始访问,逐层访问其邻居顶点,持续到访问完毕
最后获得序列:按照访问距离离起始顶点最近的所有顶点
按照该序列,访问到每个顶点后,就找到了达到该顶点的最短路径(适用于权值相等)
对相同距离邻居顶点,访问顺序不重要
同样可以使用队列实现
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include <bits/stdc++.h>
using namespace std;
const int N = 100;
bool vis[N]; // 访问标记数组
typedef char VertexType ;
typedef int EdgeType;
queue<int> q;
struct mat_grph{
VertexType vertex[N];
EdgeType arc[N][N];
int vertex_num;
int edge_num;
};
void create_graph(mat_grph* G){
G->vertex_num = 9;
G->edge_num = 15;
G->vertex[0] = 'A';
G->vertex[1] = 'B';
G->vertex[2] = 'C';
G->vertex[3] = 'D';
G->vertex[4] = 'E';
G->vertex[5] = 'F';
G->vertex[6] = 'G';
G->vertex[7] = 'H';
G->vertex[8] = 'I';
for (int i = 0;i<G->vertex_num;i++){
for (int j = 0;j<G->vertex_num;j++){
G->arc[i][j] = 0;
}
}
G->arc[0][1] = 1;
G->arc[0][5] = 1;
G->arc[1][2] = 1;
G->arc[1][6] = 1;
G->arc[1][8] = 1;
G->arc[2][3] = 1;
G->arc[2][8] = 1;
G->arc[3][4] = 1;
G->arc[3][6] = 1;
G->arc[3][7] = 1;
G->arc[3][8] = 1;
G->arc[4][5] = 1;
G->arc[4][7] = 1;
G->arc[5][6] = 1;
G->arc[6][7] = 1;
// 无向图,把对称的也复制过去
for (int i = 0;i<G->vertex_num;i++){
for (int j = 0;j<G->vertex_num;j++){
G->arc[j][i] = G->arc[i][j];
}
}
}
void bfs(mat_grph &G){
int i = 0;
vis[i]=1;
cout << G.vertex[i]<<endl;
q.push(i);
while(!q.empty()){
i = q.front();
q.pop();
for (int j = 0;j<G.vertex_num;j++){
if (!(vis[j])&&G.arc[i][j]==1){
vis[j] = 1;
cout << G.vertex[j]<<endl;
q.push(j);
}
}
}
}
int main(){
mat_grph G;
create_graph(&G);
memset(vis, 0, sizeof vis);
bfs(G);
return 0;
}图上DFS
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include <bits/stdc++.h>
using namespace std;
const int N = 100;
bool vis[N]; // 访问标记数组
typedef char VertexType ;
typedef int EdgeType;
struct mat_grph{
VertexType vertex[N];
EdgeType arc[N][N];
int vertex_num;
int edge_num;
};
void create_graph(mat_grph* G){
G->vertex_num = 9;
G->edge_num = 15;
G->vertex[0] = 'A';
G->vertex[1] = 'B';
G->vertex[2] = 'C';
G->vertex[3] = 'D';
G->vertex[4] = 'E';
G->vertex[5] = 'F';
G->vertex[6] = 'G';
G->vertex[7] = 'H';
G->vertex[8] = 'I';
for (int i = 0;i<G->vertex_num;i++){
for (int j = 0;j<G->vertex_num;j++){
G->arc[i][j] = 0;
}
}
G->arc[0][1] = 1;
G->arc[0][5] = 1;
G->arc[1][2] = 1;
G->arc[1][6] = 1;
G->arc[1][8] = 1;
G->arc[2][3] = 1;
G->arc[2][8] = 1;
G->arc[3][4] = 1;
G->arc[3][6] = 1;
G->arc[3][7] = 1;
G->arc[3][8] = 1;
G->arc[4][5] = 1;
G->arc[4][7] = 1;
G->arc[5][6] = 1;
G->arc[6][7] = 1;
// 无向图,把对称的也复制过去
for (int i = 0;i<G->vertex_num;i++){
for (int j = 0;j<G->vertex_num;j++){
G->arc[j][i] = G->arc[i][j];
}
}
}
void dfs(mat_grph &G, int i){
vis[i] = 1;
cout << G.vertex[i]<<endl;
for (int j = 0;j<G.vertex_num;j++){ // 寻找邻接矩阵中和第i个顶点间有边的顶点
if ((!vis[j]) && G.arc[i][j]==1){ // 如果没被访问过且存在边,则从其对应的顶点开始搜索与其有边的顶点
dfs(G, j);
}
}
}
int main(){
mat_grph G;
create_graph(&G);
memset(vis, 0, sizeof vis);
dfs(G, 0);
return 0;
}