If I have a graph with 1 node and no edges. the the number of connected components = 1,right?
If I have a graph with 2 node and no edges. the the number of connected components = 2,right?
If I have a graph with 2 node and 1 edges. the the number of connected components = 1,right?
I have this peice of code :
typedef adjacency_list <vecS, vecS, undirectedS> Graph;
typedef graph_traits < Graph >::edge_descriptor Edge;
/* typedef graph_traits<Graph>::vertex_descriptor Vertex;
typedef property_map<Graph, vertex_index_t>::type IndexMap;
typedef std::pair<int,int> E;
typedef graph_traits<Graph>::vertex_iterator vertex_iter;
typedef graph_traits<Graph>::out_edge_iterator edge_iter;
typedef property_map<Graph, vertex_index_t>::type VertexIndexMap;
typedef property_map<Graph, edge_weight_t>::type EdgeWeightMap;*/
int main(int,char*[])
{
int num_nodes,num_edges;
cin >> num_nodes >> num_edges;
Graph g(num_nodes);
for(int i = 0;i < num_edges; i++) // i/p edges into graph g
{
int e1,e2;
cin >> e1 >> e2;
Edge e;
bool success;
tie(e,success) = add_edge(e1, e2, g);
}
vector <int> components(num_nodes);
int num = connected_components(g, &components[0]);
cout<<"moooooo"<<num<<endl;
for these inputs :
1 0
and
2 0
and
2 1
I get respectively
1
and
2
and
2
why? shoudn't it be 1 now? Have I misunderstood connected components?
My program gives num=2 for all inputs,my inputs are the following:
4 3
1 2
2 3
3 4
4 4
1 2
2 3
3 4
4 1
5 5
1 2
2 3
3 4
4 1
3 5
6 6
1 2
2 3
3 4
4 1
3 5
5 6
8 9
1 2
2 3
3 4
4 1
3 5
5 6
6 7
7 8
8 5
8 10
1 2
2 3
3 4
4 1
3 5
5 6
6 7
7 8
8 5
4 6
what shpudl I do?
The problem here was that I used node starting from 1.... and boost considers nodes from 0,hence I always got 1 connected component more than normal.
The trick :: is to use
vertice_num_ -1
instead of the vertice number itself.