Repository navigation
Expand file tree
/
Copy pathDISPLAY ALL CONNECTED COMPONENTS.cpp
More file actions
74 lines (66 loc) · 1.8 KB
/
Copy pathDISPLAY ALL CONNECTED COMPONENTS.cpp
File metadata and controls
74 lines (66 loc) · 1.8 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include<bits/stdc++.h>
using namespace std;
#define ll long long
void printbfs(int** edges,int n,int sv,bool* visited)
{
vector<int > v;
queue<int > pendingvertices;//QUEUE IS USED SO THAT FOR EVERY VERTEX FIRST ITS CHILD ARE TO BE PRINTED IN ASCENDING ORDER
pendingvertices.push(sv);//INITIALLY MARKEIT AS VISITED
visited[sv]=true;
while(!pendingvertices.empty())//IF QUEUE IS NOT EMPTY
{
int currentvertex=pendingvertices.front();//TAIKING OUT THE FIRST EDGE
pendingvertices.pop();//DELETEING THAT EDGE FROM THE QUEUE
v.push_back(currentvertex);
for(int i=0;i<n;i++)//NOW PRINTING ALL ITS ADJACENT ELEMENTS IF NOT VISITED
{
if(edges[currentvertex][i]==1 && !visited[i])
{
pendingvertices.push(i);
visited[i]=true;
}
}
}
sort(v.begin(),v.end());//PRINTING WAS TO BE DONE BY STORING THE PATH AND SORTING IT
for(auto i:v)
cout<<i<<" ";
}
int main()
{
int n,e;
cin>>n>>e;
int** edges=new int*[n+2];
for(int i=0;i<=n;i++)
{
edges[i]=new int[n+2];
for(int j=0;j<=n;j++)
{
edges[i][j]=0;
}
}
for(int i=0;i<e;i++)
{
int f,s;
cin>>f>>s;
edges[f][s]=1;
edges[s][f]=1;
}
bool* visited=new bool[n+2];
for(int i=0;i<=n;i++)
{
visited[i]=false;
}
int p=0;
for(int i=0;i<n;i++)//LOGIC FOR PRINTING ALL DISCONNECTED GRAPHS
{
if(visited[i]==false)
printbfs(edges,n,i,visited);
cout<<endl;
}
delete [] visited;
for(int i=0;i<=n;i++)
{
delete [] edges[i];
}
delete []edges;
}