1 //=======================================================================
2 // Copyright 2007 Aaron Windsor
3 //
4 // Distributed under the Boost Software License, Version 1.0. (See
5 // accompanying file LICENSE_1_0.txt or copy at
6 // http://www.boost.org/LICENSE_1_0.txt)
7 //=======================================================================
8 #ifndef __MAKE_CONNECTED_HPP__
9 #define __MAKE_CONNECTED_HPP__
10 
11 #include <boost/config.hpp>
12 #include <boost/next_prior.hpp>
13 #include <boost/tuple/tuple.hpp> //for tie
14 #include <boost/graph/connected_components.hpp>
15 #include <boost/property_map/property_map.hpp>
16 #include <vector>
17 
18 #include <boost/graph/planar_detail/add_edge_visitors.hpp>
19 #include <boost/graph/planar_detail/bucket_sort.hpp>
20 
21 namespace boost
22 {
23 
24 template < typename Graph, typename VertexIndexMap, typename AddEdgeVisitor >
make_connected(Graph & g,VertexIndexMap vm,AddEdgeVisitor & vis)25 void make_connected(Graph& g, VertexIndexMap vm, AddEdgeVisitor& vis)
26 {
27     typedef typename graph_traits< Graph >::vertex_iterator vertex_iterator_t;
28     typedef typename graph_traits< Graph >::vertex_descriptor vertex_t;
29     typedef typename graph_traits< Graph >::vertices_size_type v_size_t;
30     typedef iterator_property_map< typename std::vector< v_size_t >::iterator,
31         VertexIndexMap >
32         vertex_to_v_size_map_t;
33 
34     std::vector< v_size_t > component_vector(num_vertices(g));
35     vertex_to_v_size_map_t component(component_vector.begin(), vm);
36     std::vector< vertex_t > vertices_by_component(num_vertices(g));
37 
38     v_size_t num_components = connected_components(g, component);
39 
40     if (num_components < 2)
41         return;
42 
43     vertex_iterator_t vi, vi_end;
44     boost::tie(vi, vi_end) = vertices(g);
45     std::copy(vi, vi_end, vertices_by_component.begin());
46 
47     bucket_sort(vertices_by_component.begin(), vertices_by_component.end(),
48         component, num_components);
49 
50     typedef typename std::vector< vertex_t >::iterator vec_of_vertices_itr_t;
51 
52     vec_of_vertices_itr_t ci_end = vertices_by_component.end();
53     vec_of_vertices_itr_t ci_prev = vertices_by_component.begin();
54     if (ci_prev == ci_end)
55         return;
56 
57     for (vec_of_vertices_itr_t ci = boost::next(ci_prev); ci != ci_end;
58          ci_prev = ci, ++ci)
59     {
60         if (component[*ci_prev] != component[*ci])
61             vis.visit_vertex_pair(*ci_prev, *ci, g);
62     }
63 }
64 
65 template < typename Graph, typename VertexIndexMap >
make_connected(Graph & g,VertexIndexMap vm)66 inline void make_connected(Graph& g, VertexIndexMap vm)
67 {
68     default_add_edge_visitor vis;
69     make_connected(g, vm, vis);
70 }
71 
make_connected(Graph & g)72 template < typename Graph > inline void make_connected(Graph& g)
73 {
74     make_connected(g, get(vertex_index, g));
75 }
76 
77 } // namespace boost
78 
79 #endif //__MAKE_CONNECTED_HPP__
80