-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathBFS_graph.cpp
More file actions
45 lines (40 loc) · 1.12 KB
/
BFS_graph.cpp
File metadata and controls
45 lines (40 loc) · 1.12 KB
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
#include <iostream>
#include <queue>
using namespace std;
// Khai báo m?t d? th? b?ng m?ng k?
const int V = 5;
int edges[V][V] = {{0, 1, 1, 0, 0},
{1, 0, 1, 1, 0},
{1, 1, 0, 1, 1},
{0, 1, 1, 0, 1},
{0, 0, 1, 1, 0}};
// Phuong th?c d? th?c hi?n BFS
void bfs1(int start) {
// Mang bool danh dau nhung dinh da duoc tham
bool visited[V] = {false};
// queue luu cac dinh da duoc duyet
queue<int> q;
// Ðua dinh bat dau vao hang doi va danh dau la da tham
q.push(start);
visited[start] = true;
while (!q.empty()) {
// lay dinh dau hang doi
int currentNode = q.front();
q.pop();
cout << currentNode << " "; // xu ly o day
// Duyet cac dinh ke cua dinh hien tai va dua vao hang doi
for (int i = 0; i < V; i++) {
if (edges[currentNode][i] && !visited[i]) {
q.push(i);
visited[i] = true;
}
}
}
}
int main() {
// In ra ket qua bfs
cout << "BFS starting from vertex 3: ";
bfs1(3);
// cout << edges[3][2];
return 0;
}