-
Notifications
You must be signed in to change notification settings - Fork 0
/
bfsconnected.cpp
62 lines (57 loc) · 1015 Bytes
/
bfsconnected.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
52
53
54
55
56
57
58
59
60
61
62
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
vector<int> adj[7];
queue<int> q;
void addEdge(vector<int> adj1[], int u, int v)
{
adj1[u].push_back(v);
adj1[v].push_back(u);
}
void bfs(bool vis[7],int v)
{
vis[v] = true;
q.push(v);
while(q.size()>0)
{
int z = q.front();
q.pop();
cout<<z;
for (int x=0;x<adj[z].size();x++)
{
if(vis[adj[z][x]]==false)
{
vis[adj[z][x]] = true;
q.push(adj[z][x]);
}
}
}
}
int main()
{
int count =0;
addEdge(adj, 0, 1);
addEdge(adj, 0, 4);
addEdge(adj, 1, 2);
addEdge(adj, 1, 3);
addEdge(adj, 1, 4);
addEdge(adj, 2, 3);
addEdge(adj, 3, 4);
addEdge(adj, 5, 6);
bool vis[7];
for(int i =0;i<7;i++)
{
vis[i] = false;
}
for(int i=0;i<7;i++)
{
if(vis[i]==false)
{
bfs(vis,i);
cout<<endl;
count++;
}
}
cout<<"NO Of connected componenets: "<<count;
return 0;
}