incremental_components, component_index for listS, listS adjacency_list
Hi All, I couldn't figure out how to use the component_index facility of the incremental components algorithm when operating on an listS, listS adjecency_list so I rolled my own as per below. Any suggestion on how to use the provided interface are appreciated. thanks Sean using namespace boost; typedef graph_traits<BlockDependencyGraph>::vertices_size_type size_type; typedef BlockDependencyGraph::vertex_descriptor vertex_descriptor; typedef BlockDependencyGraph::vertex_iterator vertex_iterator; BlockDependencyGraph& graph = _blk_dep_view->get_graph(); // Disjoint sets requires Rank to map vertex_descriptors onto integers typedef std::map<vertex_descriptor, int> rank_storage_t; rank_storage_t rank_map; typedef associative_property_map<rank_storage_t> rank_t; // Disjoint sets requires Parent to map vertex_descriptors onto vertex_descriptors typedef std::map<vertex_descriptor, vertex_descriptor> parent_storage_t; parent_storage_t parent_map; typedef associative_property_map<parent_storage_t> parent_t; disjoint_sets<rank_t, parent_t> component_set(make_assoc_property_map(rank_map), make_assoc_property_map(parent_map)); initialize_incremental_components(graph, component_set); incremental_components(graph, component_set); // Not at all clear how to extract components using component_index interface // when graph is listS, listS adjacency_list... so we roll our own. // // Index vertices by representative component in a multimap typedef std::map<vertex_descriptor, std::vector<vertex_descriptor> > components_t; components_t components; vertex_iterator curr(0), end(0); for(tie(curr, end) = vertices(graph); curr != end; ++curr) { components[component_set.find_set(*curr)].push_back(*curr); } // Print all components size_t n_c(0); for(components_t::const_iterator c = components.begin(); c != components.end(); ++c, ++n_c) { std::cout << "component " << n_c << std::endl; for(std::vector<vertex_descriptor>::const_iterator element = c->second.begin(); element != c->second.end(); ++element) { std::cout << graph[*element]->get_name() << std::endl; } }
Hi All,
I couldn't figure out how to use the component_index facility of the incremental components algorithm when operating on an listS, listS adjecency_list so I rolled my own as per below. Any suggestion on how to use the provided interface are appreciated. Hi Kelly, A couple of weeks ago I saw a good example of "iterator_property_map" usage in the similar situation but for connected_components() algorithm (you need to add vertex_index property). Probably this can help you,
Sean Kelly wrote: mainly to improve perfomance. Please, take a look on this example. /////////////////////////////////////////////////////////////////////// #include <boost/config.hpp> #include <vector> #include <map> #include <iostream> // for std::cout #include <algorithm> // for std::for_each #include <boost/utility.hpp> // for boost::tie #include <boost/graph/graph_traits.hpp> // for boost::graph_traits #include <boost/graph/adjacency_list.hpp> #include <boost/graph/connected_components.hpp> using namespace boost; struct Node { int index; //****color property****************************** boost::default_color_type m_algo_color; //****color property****************************** }; struct Boundary { double something; }; typedef boost::adjacency_list< boost::setS, boost::listS, boost::undirectedS, Node, Boundary, boost::setS> Graph; int main(int argc, char*argv[]) { Graph g(5); boost::graph_traits<Graph>::vertex_iterator vi, viend; std::vector< int > c( num_vertices(g) ); int num; // manually intialize the vertex index, // because we have listS as vertex list type int n = 0; for (tie(vi, viend) = vertices(g); vi != viend; ++vi, ++n) { g[*vi].index = n; } //****modified call****************************** num = connected_components(g, make_iterator_property_map(c.begin(), get(&Node::index, g)), boost::color_map( get(&Node::m_algo_color, g) )); //****modified call****************************** return 0; }
participants (2)
-
Dmitry Bufistov
-
Sean Kelly