connected_components.hpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. //=======================================================================
  2. // Copyright 1997, 1998, 1999, 2000 University of Notre Dame.
  3. // Authors: Andrew Lumsdaine, Lie-Quan Lee, Jeremy G. Siek
  4. //
  5. // Distributed under the Boost Software License, Version 1.0. (See
  6. // accompanying file LICENSE_1_0.txt or copy at
  7. // http://www.boost.org/LICENSE_1_0.txt)
  8. //=======================================================================
  9. #ifndef BOOST_GRAPH_DETAIL_CONNECTED_COMPONENTS_HPP
  10. #define BOOST_GRAPH_DETAIL_CONNECTED_COMPONENTS_HPP
  11. #if defined(__sgi) && !defined(__GNUC__)
  12. #pragma set woff 1234
  13. #endif
  14. #include <boost/operators.hpp>
  15. namespace boost {
  16. namespace detail {
  17. //=========================================================================
  18. // Implementation details of connected_components
  19. // This is used both in the connected_components algorithm and in
  20. // the kosaraju strong components algorithm during the second DFS
  21. // traversal.
  22. template <class ComponentsPA, class DFSVisitor>
  23. class components_recorder : public DFSVisitor
  24. {
  25. typedef typename property_traits<ComponentsPA>::value_type comp_type;
  26. public:
  27. components_recorder(ComponentsPA c,
  28. comp_type& c_count,
  29. DFSVisitor v)
  30. : DFSVisitor(v), m_component(c), m_count(c_count) {}
  31. template <class Vertex, class Graph>
  32. void start_vertex(Vertex u, Graph& g) {
  33. ++m_count;
  34. DFSVisitor::start_vertex(u, g);
  35. }
  36. template <class Vertex, class Graph>
  37. void discover_vertex(Vertex u, Graph& g) {
  38. put(m_component, u, m_count);
  39. DFSVisitor::discover_vertex(u, g);
  40. }
  41. protected:
  42. ComponentsPA m_component;
  43. comp_type& m_count;
  44. };
  45. template <class DiscoverTimeMap, class FinishTimeMap, class TimeT,
  46. class DFSVisitor>
  47. class time_recorder : public DFSVisitor
  48. {
  49. public:
  50. time_recorder(DiscoverTimeMap d, FinishTimeMap f, TimeT& t, DFSVisitor v)
  51. : DFSVisitor(v), m_discover_time(d), m_finish_time(f), m_t(t) {}
  52. template <class Vertex, class Graph>
  53. void discover_vertex(Vertex u, Graph& g) {
  54. put(m_discover_time, u, ++m_t);
  55. DFSVisitor::discover_vertex(u, g);
  56. }
  57. template <class Vertex, class Graph>
  58. void finish_vertex(Vertex u, Graph& g) {
  59. put(m_finish_time, u, ++m_t);
  60. DFSVisitor::discover_vertex(u, g);
  61. }
  62. protected:
  63. DiscoverTimeMap m_discover_time;
  64. FinishTimeMap m_finish_time;
  65. TimeT m_t;
  66. };
  67. template <class DiscoverTimeMap, class FinishTimeMap, class TimeT,
  68. class DFSVisitor>
  69. time_recorder<DiscoverTimeMap, FinishTimeMap, TimeT, DFSVisitor>
  70. record_times(DiscoverTimeMap d, FinishTimeMap f, TimeT& t, DFSVisitor vis)
  71. {
  72. return time_recorder<DiscoverTimeMap, FinishTimeMap, TimeT, DFSVisitor>
  73. (d, f, t, vis);
  74. }
  75. //=========================================================================
  76. // Implementation detail of dynamic_components
  77. //-------------------------------------------------------------------------
  78. // Helper functions for the component_index class
  79. // Record the representative vertices in the header array.
  80. // Representative vertices now point to the component number.
  81. template <class Parent, class OutputIterator, class Integer>
  82. inline void
  83. build_components_header(Parent p,
  84. OutputIterator header,
  85. Integer num_nodes)
  86. {
  87. Parent component = p;
  88. Integer component_num = 0;
  89. for (Integer v = 0; v != num_nodes; ++v)
  90. if (p[v] == v) {
  91. *header++ = v;
  92. component[v] = component_num++;
  93. }
  94. }
  95. // Pushes x onto the front of the list. The list is represented in
  96. // an array.
  97. template <class Next, class T, class V>
  98. inline void push_front(Next next, T& head, V x)
  99. {
  100. T tmp = head;
  101. head = x;
  102. next[x] = tmp;
  103. }
  104. // Create a linked list of the vertices in each component
  105. // by reusing the representative array.
  106. template <class Parent1, class Parent2,
  107. class Integer>
  108. void
  109. link_components(Parent1 component, Parent2 header,
  110. Integer num_nodes, Integer num_components)
  111. {
  112. // Make the non-representative vertices point to their component
  113. Parent1 representative = component;
  114. for (Integer v = 0; v != num_nodes; ++v)
  115. if (component[v] >= num_components || header[component[v]] != v)
  116. component[v] = component[representative[v]];
  117. // initialize the "head" of the lists to "NULL"
  118. std::fill_n(header, num_components, num_nodes);
  119. // Add each vertex to the linked list for its component
  120. Parent1 next = component;
  121. for (Integer k = 0; k != num_nodes; ++k)
  122. push_front(next, header[component[k]], k);
  123. }
  124. template <class IndexContainer, class HeaderContainer>
  125. void
  126. construct_component_index(IndexContainer& index, HeaderContainer& header)
  127. {
  128. build_components_header(index.begin(),
  129. std::back_inserter(header),
  130. index.end() - index.begin());
  131. link_components(index.begin(), header.begin(),
  132. index.end() - index.begin(),
  133. header.end() - header.begin());
  134. }
  135. template <class IndexIterator, class Integer, class Distance>
  136. class component_iterator
  137. : boost::forward_iterator_helper<
  138. component_iterator<IndexIterator,Integer,Distance>,
  139. Integer, Distance,Integer*, Integer&>
  140. {
  141. public:
  142. typedef component_iterator self;
  143. IndexIterator next;
  144. Integer node;
  145. typedef std::forward_iterator_tag iterator_category;
  146. typedef Integer value_type;
  147. typedef Integer& reference;
  148. typedef Integer* pointer;
  149. typedef Distance difference_type;
  150. component_iterator() {}
  151. component_iterator(IndexIterator x, Integer i)
  152. : next(x), node(i) {}
  153. Integer operator*() const {
  154. return node;
  155. }
  156. self& operator++() {
  157. node = next[node];
  158. return *this;
  159. }
  160. };
  161. template <class IndexIterator, class Integer, class Distance>
  162. inline bool
  163. operator==(const component_iterator<IndexIterator, Integer, Distance>& x,
  164. const component_iterator<IndexIterator, Integer, Distance>& y)
  165. {
  166. return x.node == y.node;
  167. }
  168. } // namespace detail
  169. } // namespace detail
  170. #if defined(__sgi) && !defined(__GNUC__)
  171. #pragma reset woff 1234
  172. #endif
  173. #endif