-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbfs.cpp
More file actions
53 lines (43 loc) · 948 Bytes
/
bfs.cpp
File metadata and controls
53 lines (43 loc) · 948 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
49
50
51
52
53
#include<bits/stdc++.h>
using namespace std;
#define N 100
int visited[N];
void bfs(vector<int> graph[], int node) {
queue<int> q;
visited[node]=1;
q.push(node);
while(q.empty()==false) {
node=q.front();
cout<<node<<" ";
q.pop();
for(int i=0;i<graph[node].size();i++) {
if(visited[graph[node][i]]==0) {
visited[graph[node][i]]=1;
q.push(graph[node][i]);
}
}
}
}
void initialize() {
for(int i=1;i<=5;i++)
visited[i]=0;
}
int main() {
// Making a graph(number of nodes = 5) using adjacency list
vector<int> graph[7];
graph[1].push_back(2);
graph[1].push_back(4);
graph[2].push_back(3);
graph[2].push_back(5);
graph[3].push_back(2);
graph[3].push_back(6);
graph[4].push_back(1);
graph[5].push_back(2);
graph[5].push_back(6);
graph[6].push_back(3);
graph[6].push_back(5);
// Initially no node is visited
initialize();
bfs(graph,1);
return 0;
}