-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbfs.cpp
More file actions
48 lines (42 loc) · 757 Bytes
/
bfs.cpp
File metadata and controls
48 lines (42 loc) · 757 Bytes
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
#include "bits/stdc++.h"
using namespace std;
vector<vector<int>> v(100);
vector<bool> visited(100);
deque<int> p;
struct bfs{
void add(int a, int b) {
v[a].push_back(b);
v[b].push_back(a);
}
void printing(int a) {
if (!visited[a]) {
visited[a] = 1;
cout << a << endl;
}
for (auto x : v[a]) {
if (!visited[x]) {
cout << x << endl;
p.push_back(x);
visited[x] = 1;
}
}
while(!p.empty()){
int x = p.front();
p.pop_front();
// if (!visited[x]) {
// visited[x] = 1;
printing(x);
// }
}
}
};
int main() {
bfs b;
b.add(1,4);
b.add(1,2);
b.add(2,5);
b.add(2,3);
b.add(3,6);
b.add(5,6);
b.printing(1);
}