-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconnected.cpp
More file actions
44 lines (32 loc) · 820 Bytes
/
connected.cpp
File metadata and controls
44 lines (32 loc) · 820 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
// C++ program to print connected components in
// an undirected graph
#include<iostream>
#include "graph.h"
using namespace std;
// Drive program to test above
int main()
{
// Create a graph given in the above diagram
Graph g(5); // 5 vertices numbered from 0 to 4
g.addEdge(1, 0);
g.addEdge(2, 3);
g.addEdge(3, 4);
cout << "Following are connected components \n";
g.connectedComponents();
for (int i = 0; i < g.getNumberNodes(); i++)
{
cout << i << ", " << g.getClassLabel(i) << endl;
}
Graph g1;
g1.addNode();
g1.addNode();
cout << g1.getNumberNodes() << "---" << endl;
g1.addEdge(0,1);
g1.connectedComponents();
cout << "--------" << endl;
for (int i = 0; i < g1.getNumberNodes(); i++)
{
cout << i << ", " << g1.getClassLabel(i) << endl;
}
return 0;
}