|
Boost : |
From: Kommers, Jefferson (kommers_at_[hidden])
Date: 2004-05-04 14:00:06
I am trying to understand why the order in which I add edges to an
adjacency_list graph has some effect on the connected components I get using
incremental_components. Using the listing below, with Boost release 1.30.0
(also 1.31.0) with g++ 2.95 (cygwin) I get the following:
> g++ -I./boost_1_31_0 -o ok -ftemplate-depth-40
incremental-components-eg-bug.cpp
> ./ok
rank[0] = 0
rank[1] = 0
rank[2] = 0
rank[3] = 1
rank[4] = 0
parent[0] = 3
parent[1] = 3
parent[2] = 3
parent[3] = 3
parent[4] = 3
component 0 contains: 4 3 2 1 0
This is what I expect. If I change the order in which I add edges to the
graph, I get a different answer:
> g++ -I./boost_1_31_0 -DSHOWBUG -o bug -ftemplate-depth-40
incremental-components-eg-bug.cpp
> ./bug
rank[0] = 0
rank[1] = 0
rank[2] = 1
rank[3] = 2
rank[4] = 0
parent[0] = 3
parent[1] = 2
parent[2] = 3
parent[3] = 3
parent[4] = 3
component 0 contains: 4 3 2 0
This is not what I expect. The single connected component has lost a
vertex. But it should be the same graph, the only difference being the
order in which I called add_edge().
Is this a bug or a misunderstanding on my part? Any advice would be
appreciated...
Jeff
#include <iostream>
#include <vector>
#include <algorithm>
#include <utility>
#include <boost/graph/adjacency_list.hpp>
#include <boost/pending/disjoint_sets.hpp>
#include <boost/graph/incremental_components.hpp>
int
main(int, char *[])
{
using namespace boost;
// Create a graph
typedef adjacency_list < vecS, vecS, undirectedS > Graph;
typedef graph_traits < Graph >::vertex_descriptor Vertex;
const int N = 5;
Graph G(N);
add_edge(0, 3, G);
add_edge(0, 4, G);
#ifndef SHOWBUG
add_edge(1, 4, G);
add_edge(1, 2, G);
#else
add_edge(1, 2, G);
add_edge(1, 4, G);
#endif
// create the disjoint-sets object, which requires rank and parent vertex
properties
std::vector < Vertex > rank(num_vertices(G));
std::vector < Vertex > parent(num_vertices(G));
typedef graph_traits<Graph>::vertices_size_type* Rank;
typedef Vertex* Parent;
disjoint_sets < Rank, Parent > ds(&rank[0], &parent[0]);
// determine the connected components, storing the results in the
disjoint-sets object
initialize_incremental_components(G, ds);
incremental_components(G, ds);
for (unsigned int i = 0; i < rank.size(); ++i) {
std::cout << "rank[" << i << "] = " << rank[i] << std::endl;
}
for (unsigned int i = 0; i < parent.size(); ++i) {
std::cout << "parent[" << i << "] = " << parent[i] << std::endl;
}
typedef component_index < unsigned int >Components;
Components components(parent.begin(), parent.end());
for (Components::size_type i = 0; i < components.size(); ++i) {
std::cout << "component " << i << " contains: ";
for (Components::value_type::iterator j = components[i].begin();
j != components[i].end(); ++j)
std::cout << *j << " ";
std::cout << std::endl;
}
return EXIT_SUCCESS;
}
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk