forked from Amisha-here/Data-Structures
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbreadth_first_search.cpp
51 lines (49 loc) · 977 Bytes
/
breadth_first_search.cpp
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
#include <bits/stdc++.h>
using namespace std;
int n=5;
bool visited[5];
void bfs(vector<int> graph[],int src){
queue<int> q;
q.push(src);
visited[src]=true;
while(!q.empty()){
src=q.front();
cout<<src<<" ";
q.pop();
for(int i=0;i<graph[src].size();i++){
if(!visited[graph[src][i]]){
visited[graph[src][i]]=true;
q.push(graph[src][i]);
}
}
}
}
void bfsUtil(vector<int> graph[]){
for(int i=0;i<n;i++){
visited[i]=false;
}
for(int i=0;i<n;i++){
if(!visited[i]){
bfs(graph,i);
}
}
}
int main() {
vector<int> graph[n];
graph[0].push_back(1);
graph[0].push_back(4);
graph[1].push_back(0);
graph[1].push_back(2);
graph[1].push_back(3);
graph[1].push_back(4);
graph[2].push_back(1);
graph[2].push_back(3);
graph[3].push_back(1);
graph[3].push_back(2);
graph[3].push_back(4);
graph[4].push_back(0);
graph[4].push_back(1);
graph[4].push_back(3);
bfsUtil(graph);
return 0;
}