Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 8e2d004

Browse files
Added more stuff
1 parent 622f4de commit 8e2d004

File tree

4 files changed

+205
-2
lines changed

4 files changed

+205
-2
lines changed

‎Graphs1/Allconnectedcomponents.cpp‎

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/*
2+
3+
Name: Mehul Chaturvedi
4+
IIT-Guwahati
5+
6+
*/
7+
8+
9+
#include <iostream>
10+
11+
12+
#include <bits/stdc++.h>
13+
using namespace std;
14+
void connected(bool** edges, int n, int sv, int *visited)
15+
{
16+
int init=sv;
17+
18+
while(init<n){
19+
if(visited[init]!=0){
20+
init++;
21+
continue;
22+
}
23+
visited[init] = 1;
24+
queue<int> q;
25+
q.push(init);
26+
init++;
27+
vector<int> v1;
28+
while(!q.empty())
29+
{
30+
int x = q.front();
31+
v1.push_back(x);
32+
33+
q.pop();
34+
35+
for(int i=0;i<n;i++)
36+
{
37+
if(edges[x][i] && !visited[i] )
38+
{
39+
q.push(i);
40+
visited[i] = 1;
41+
}
42+
}
43+
}
44+
sort(v1.begin(), v1.end());
45+
for(int i=0;i<v1.size();i++){
46+
cout<<v1[i]<<" ";
47+
}
48+
cout<<endl;
49+
}
50+
51+
return;
52+
53+
}
54+
int main()
55+
{
56+
int V, E;
57+
cin >> V >> E;
58+
59+
bool** edges = new bool*[V];
60+
for(int i=0; i<V; i++){
61+
edges[i]=new bool[V];
62+
for(int j=0;j<V;j++){
63+
edges[i][j]=0;
64+
}
65+
}
66+
67+
for(int i=0;i<E;i++){
68+
int f,s;
69+
cin>>f;
70+
cin>>s;
71+
72+
edges[f][s]=1;
73+
edges[s][f]=1;
74+
75+
76+
}
77+
78+
int *visited = new int[V];
79+
for(int i=0;i<V;i++)
80+
visited[i] = 0;
81+
82+
connected(edges, V, 0 , visited);
83+
84+
return 0;
85+
}

‎Graphs1/Test.txt‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
5 3
1+
5 5
22
0 1
33
0 2
4+
1 3
5+
1 4
46
3 4
5-
0 1
7+
0 2

‎Graphs1/a.out‎

0 Bytes
Binary file not shown.

‎Graphs1/isconnected.cpp‎

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
/*
2+
3+
Name: Mehul Chaturvedi
4+
IIT-Guwahati
5+
6+
*/
7+
/*
8+
Given an undirected graph G(V,E), check if the graph G is connected graph or not.
9+
V is the number of vertices present in graph G and vertices are numbered from 0 to V-1.
10+
E is the number of edges present in graph G.
11+
Input Format :
12+
Line 1: Two Integers V and E (separated by space)
13+
Next 'E' lines, each have two space-separated integers, 'a' and 'b', denoting that there exists an edge between Vertex 'a' and Vertex 'b'.
14+
Output Format :
15+
"true" or "false"
16+
Constraints :
17+
2 <= V <= 1000
18+
1 <= E <= 1000
19+
Sample Input 1:
20+
4 4
21+
0 1
22+
0 3
23+
1 2
24+
2 3
25+
Sample Output 1:
26+
true
27+
Sample Input 2:
28+
4 3
29+
0 1
30+
1 3
31+
0 3
32+
Sample Output 2:
33+
false
34+
Sample Output 2 Explanation
35+
The graph is not connected, even though vertices 0,1 and 3 are connected to each other but there isn’t any path from vertices 0,1,3 to vertex 2.
36+
*/
37+
38+
#include <bits/stdc++.h>
39+
40+
using namespace std;
41+
42+
void marker(int** edges, int n, int* visited, int sv){
43+
if (n==1)
44+
{
45+
visited[sv] = 1;
46+
return;
47+
}
48+
49+
visited[sv] = 1;
50+
51+
for (int i = 0; i < n; ++i)
52+
{
53+
if (edges[sv][i]==1 && visited[i]==0)
54+
{
55+
marker(edges, n, visited, i);
56+
}
57+
}
58+
return;
59+
60+
}
61+
62+
int main( int argc , char ** argv )
63+
{
64+
ios_base::sync_with_stdio(false) ;
65+
cin.tie(NULL) ;
66+
67+
int n, e;
68+
cin>>n>>e;
69+
70+
int** edges = new int*[n];
71+
72+
for (int i = 0; i < n; ++i)
73+
{
74+
edges[i] = new int[n];
75+
for (int j = 0; j < n; ++j)
76+
{
77+
edges[i][j] = 0;
78+
}
79+
80+
}
81+
82+
int* visited = new int[n];
83+
for (int i = 0; i < n; ++i)
84+
{
85+
visited[i] = 0;
86+
}
87+
88+
for (int i = 0; i < e; ++i)
89+
{
90+
int a, b;
91+
cin>>a>>b;
92+
93+
edges[a][b] = 1;
94+
edges[b][a] = 1;
95+
}
96+
97+
98+
marker(edges, n, visited, 0);
99+
100+
for (int i = 0; i < n; ++i)
101+
{
102+
if (visited[i] == 0)
103+
{
104+
cout << "false" << '\n';
105+
return 0;
106+
}
107+
108+
}
109+
cout << "true" << '\n';
110+
111+
112+
return 0 ;
113+
114+
115+
116+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /