| 1 | #include <fstream>
|
|---|
| 2 | #include <map>
|
|---|
| 3 |
|
|---|
| 4 | #include <boost/graph/adjacency_list.hpp>
|
|---|
| 5 | #include <boost/graph/incremental_components.hpp>
|
|---|
| 6 | #include <boost/property_map.hpp>
|
|---|
| 7 | #include <boost/foreach.hpp>
|
|---|
| 8 | using namespace boost;
|
|---|
| 9 | typedef boost::adjacency_list<vecS, vecS, undirectedS> ImplGraph_t;
|
|---|
| 10 | typedef ImplGraph_t::vertex_descriptor vertex_t;
|
|---|
| 11 | typedef ImplGraph_t::edge_descriptor edge_t;
|
|---|
| 12 | typedef graph_traits<ImplGraph_t>::vertices_size_type size_type;
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 | int main(int argc, char** argv)
|
|---|
| 16 | {
|
|---|
| 17 | std::ifstream ifs(argv[1]);
|
|---|
| 18 | std::ofstream logstream("TClog.txt");
|
|---|
| 19 | int sid, did;
|
|---|
| 20 |
|
|---|
| 21 | ImplGraph_t gg;
|
|---|
| 22 | int numNodes = atoi(argv[2]);
|
|---|
| 23 | std::map<int, vertex_t> forwardMap;
|
|---|
| 24 | for(int i = 0; i < numNodes; i++)
|
|---|
| 25 | {
|
|---|
| 26 | vertex_t k = add_vertex(gg);
|
|---|
| 27 | //logstream<<i<<" "<<k<<std::endl;
|
|---|
| 28 | forwardMap[i] = k;
|
|---|
| 29 | }
|
|---|
| 30 | ifs>>sid>>did;
|
|---|
| 31 | while(ifs)
|
|---|
| 32 | {
|
|---|
| 33 | vertex_t src = forwardMap[sid];
|
|---|
| 34 | vertex_t des = forwardMap[did];
|
|---|
| 35 | add_edge(src, des, gg);
|
|---|
| 36 | ifs>>sid>>did;
|
|---|
| 37 | }
|
|---|
| 38 |
|
|---|
| 39 | std::cout<<" "<<num_vertices(gg)<<" "<<num_edges(gg)<<std::endl;
|
|---|
| 40 | std::map<vertex_t, size_type> rank;
|
|---|
| 41 | boost::associative_property_map< std::map<vertex_t, size_type> > rank_map(rank);
|
|---|
| 42 | std::vector<vertex_t> parent(num_vertices(gg));
|
|---|
| 43 | typedef std::vector<vertex_t>::iterator vv_iter_t;
|
|---|
| 44 | iterator_property_map<vv_iter_t, identity_property_map, vertex_t, vertex_t&> ipm(parent.begin());
|
|---|
| 45 | // std::map<vertex_t, vertex_t> parent;
|
|---|
| 46 | // boost::associative_property_map< std::map<vertex_t, vertex_t> > parent_map(parent);
|
|---|
| 47 |
|
|---|
| 48 |
|
|---|
| 49 |
|
|---|
| 50 | disjoint_sets< boost::associative_property_map<std::map<vertex_t, size_type> >, iterator_property_map<vv_iter_t, identity_property_map, vertex_t, vertex_t&> > ds(rank_map, ipm);
|
|---|
| 51 | initialize_incremental_components(gg, ds);
|
|---|
| 52 | incremental_components(gg, ds);
|
|---|
| 53 | edge_t e;
|
|---|
| 54 | vertex_t src;
|
|---|
| 55 | vertex_t des;
|
|---|
| 56 |
|
|---|
| 57 |
|
|---|
| 58 | typedef component_index<unsigned int> Components_t;
|
|---|
| 59 | Components_t components(parent.begin(), parent.end());
|
|---|
| 60 |
|
|---|
| 61 | for (Components_t::size_type c = 0; c < components.size(); ++c) {
|
|---|
| 62 | std::cout << "component " << c << " contains: ";
|
|---|
| 63 | Components_t::value_type::iterator
|
|---|
| 64 | j = components[c].begin(),
|
|---|
| 65 | jend = components[c].end();
|
|---|
| 66 | for ( ; j != jend; ++j)
|
|---|
| 67 | std::cout << *j << " ";
|
|---|
| 68 | std::cout << std::endl;
|
|---|
| 69 | }
|
|---|
| 70 |
|
|---|
| 71 |
|
|---|
| 72 | return 0;
|
|---|
| 73 |
|
|---|
| 74 |
|
|---|
| 75 | }
|
|---|