make_connected.hpp 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. #include <boost/config.hpp>
  11. #include <boost/next_prior.hpp>
  12. #include <boost/tuple/tuple.hpp> //for tie
  13. #include <boost/graph/connected_components.hpp>
  14. #include <boost/property_map/property_map.hpp>
  15. #include <vector>
  16. #include <boost/graph/planar_detail/add_edge_visitors.hpp>
  17. #include <boost/graph/planar_detail/bucket_sort.hpp>
  18. namespace boost
  19. {
  20. template < typename Graph, typename VertexIndexMap, typename AddEdgeVisitor >
  21. void make_connected(Graph& g, VertexIndexMap vm, AddEdgeVisitor& vis)
  22. {
  23. typedef typename graph_traits< Graph >::vertex_iterator vertex_iterator_t;
  24. typedef typename graph_traits< Graph >::vertex_descriptor vertex_t;
  25. typedef typename graph_traits< Graph >::vertices_size_type v_size_t;
  26. typedef iterator_property_map< typename std::vector< v_size_t >::iterator,
  27. VertexIndexMap >
  28. vertex_to_v_size_map_t;
  29. std::vector< v_size_t > component_vector(num_vertices(g));
  30. vertex_to_v_size_map_t component(component_vector.begin(), vm);
  31. std::vector< vertex_t > vertices_by_component(num_vertices(g));
  32. v_size_t num_components = connected_components(g, component);
  33. if (num_components < 2)
  34. return;
  35. vertex_iterator_t vi, vi_end;
  36. boost::tie(vi, vi_end) = vertices(g);
  37. std::copy(vi, vi_end, vertices_by_component.begin());
  38. bucket_sort(vertices_by_component.begin(), vertices_by_component.end(),
  39. component, num_components);
  40. typedef typename std::vector< vertex_t >::iterator vec_of_vertices_itr_t;
  41. vec_of_vertices_itr_t ci_end = vertices_by_component.end();
  42. vec_of_vertices_itr_t ci_prev = vertices_by_component.begin();
  43. if (ci_prev == ci_end)
  44. return;
  45. for (vec_of_vertices_itr_t ci = boost::next(ci_prev); ci != ci_end;
  46. ci_prev = ci, ++ci)
  47. {
  48. if (component[*ci_prev] != component[*ci])
  49. vis.visit_vertex_pair(*ci_prev, *ci, g);
  50. }
  51. }
  52. template < typename Graph, typename VertexIndexMap >
  53. inline void make_connected(Graph& g, VertexIndexMap vm)
  54. {
  55. default_add_edge_visitor vis;
  56. make_connected(g, vm, vis);
  57. }
  58. template < typename Graph > inline void make_connected(Graph& g)
  59. {
  60. make_connected(g, get(vertex_index, g));
  61. }
  62. } // namespace boost
  63. #endif //__MAKE_CONNECTED_HPP__