bipartite.hpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. /**
  2. *
  3. * Copyright (c) 2010 Matthias Walter (xammy@xammy.homelinux.net)
  4. *
  5. * Authors: Matthias Walter
  6. *
  7. * Distributed under the Boost Software License, Version 1.0. (See
  8. * accompanying file LICENSE_1_0.txt or copy at
  9. * http://www.boost.org/LICENSE_1_0.txt)
  10. *
  11. */
  12. #ifndef BOOST_GRAPH_BIPARTITE_HPP
  13. #define BOOST_GRAPH_BIPARTITE_HPP
  14. #include <utility>
  15. #include <vector>
  16. #include <exception>
  17. #include <boost/graph/properties.hpp>
  18. #include <boost/graph/adjacency_list.hpp>
  19. #include <boost/graph/depth_first_search.hpp>
  20. #include <boost/graph/one_bit_color_map.hpp>
  21. #include <boost/bind.hpp>
  22. namespace boost
  23. {
  24. namespace detail
  25. {
  26. /**
  27. * The bipartite_visitor_error is thrown if an edge cannot be colored.
  28. * The witnesses are the edges incident vertices.
  29. */
  30. template < typename Vertex >
  31. struct BOOST_SYMBOL_VISIBLE bipartite_visitor_error : std::exception
  32. {
  33. std::pair< Vertex, Vertex > witnesses;
  34. bipartite_visitor_error(Vertex a, Vertex b) : witnesses(a, b) {}
  35. const char* what() const throw() { return "Graph is not bipartite."; }
  36. };
  37. /**
  38. * Functor which colors edges to be non-monochromatic.
  39. */
  40. template < typename PartitionMap > struct bipartition_colorize
  41. {
  42. typedef on_tree_edge event_filter;
  43. bipartition_colorize(PartitionMap partition_map)
  44. : partition_map_(partition_map)
  45. {
  46. }
  47. template < typename Edge, typename Graph >
  48. void operator()(Edge e, const Graph& g)
  49. {
  50. typedef typename graph_traits< Graph >::vertex_descriptor
  51. vertex_descriptor_t;
  52. typedef color_traits<
  53. typename property_traits< PartitionMap >::value_type >
  54. color_traits;
  55. vertex_descriptor_t source_vertex = source(e, g);
  56. vertex_descriptor_t target_vertex = target(e, g);
  57. if (get(partition_map_, source_vertex) == color_traits::white())
  58. put(partition_map_, target_vertex, color_traits::black());
  59. else
  60. put(partition_map_, target_vertex, color_traits::white());
  61. }
  62. private:
  63. PartitionMap partition_map_;
  64. };
  65. /**
  66. * Creates a bipartition_colorize functor which colors edges
  67. * to be non-monochromatic.
  68. *
  69. * @param partition_map Color map for the bipartition
  70. * @return The functor.
  71. */
  72. template < typename PartitionMap >
  73. inline bipartition_colorize< PartitionMap > colorize_bipartition(
  74. PartitionMap partition_map)
  75. {
  76. return bipartition_colorize< PartitionMap >(partition_map);
  77. }
  78. /**
  79. * Functor which tests an edge to be monochromatic.
  80. */
  81. template < typename PartitionMap > struct bipartition_check
  82. {
  83. typedef on_back_edge event_filter;
  84. bipartition_check(PartitionMap partition_map)
  85. : partition_map_(partition_map)
  86. {
  87. }
  88. template < typename Edge, typename Graph >
  89. void operator()(Edge e, const Graph& g)
  90. {
  91. typedef typename graph_traits< Graph >::vertex_descriptor
  92. vertex_descriptor_t;
  93. vertex_descriptor_t source_vertex = source(e, g);
  94. vertex_descriptor_t target_vertex = target(e, g);
  95. if (get(partition_map_, source_vertex)
  96. == get(partition_map_, target_vertex))
  97. throw bipartite_visitor_error< vertex_descriptor_t >(
  98. source_vertex, target_vertex);
  99. }
  100. private:
  101. PartitionMap partition_map_;
  102. };
  103. /**
  104. * Creates a bipartition_check functor which raises an error if a
  105. * monochromatic edge is found.
  106. *
  107. * @param partition_map The map for a bipartition.
  108. * @return The functor.
  109. */
  110. template < typename PartitionMap >
  111. inline bipartition_check< PartitionMap > check_bipartition(
  112. PartitionMap partition_map)
  113. {
  114. return bipartition_check< PartitionMap >(partition_map);
  115. }
  116. /**
  117. * Find the beginning of a common suffix of two sequences
  118. *
  119. * @param sequence1 Pair of bidirectional iterators defining the first
  120. * sequence.
  121. * @param sequence2 Pair of bidirectional iterators defining the second
  122. * sequence.
  123. * @return Pair of iterators pointing to the beginning of the common suffix.
  124. */
  125. template < typename BiDirectionalIterator1,
  126. typename BiDirectionalIterator2 >
  127. inline std::pair< BiDirectionalIterator1, BiDirectionalIterator2 >
  128. reverse_mismatch(
  129. std::pair< BiDirectionalIterator1, BiDirectionalIterator1 > sequence1,
  130. std::pair< BiDirectionalIterator2, BiDirectionalIterator2 > sequence2)
  131. {
  132. if (sequence1.first == sequence1.second
  133. || sequence2.first == sequence2.second)
  134. return std::make_pair(sequence1.first, sequence2.first);
  135. BiDirectionalIterator1 iter1 = sequence1.second;
  136. BiDirectionalIterator2 iter2 = sequence2.second;
  137. while (true)
  138. {
  139. --iter1;
  140. --iter2;
  141. if (*iter1 != *iter2)
  142. {
  143. ++iter1;
  144. ++iter2;
  145. break;
  146. }
  147. if (iter1 == sequence1.first)
  148. break;
  149. if (iter2 == sequence2.first)
  150. break;
  151. }
  152. return std::make_pair(iter1, iter2);
  153. }
  154. }
  155. /**
  156. * Checks a given graph for bipartiteness and fills the given color map with
  157. * white and black according to the bipartition. If the graph is not
  158. * bipartite, the contents of the color map are undefined. Runs in linear
  159. * time in the size of the graph, if access to the property maps is in
  160. * constant time.
  161. *
  162. * @param graph The given graph.
  163. * @param index_map An index map associating vertices with an index.
  164. * @param partition_map A color map to fill with the bipartition.
  165. * @return true if and only if the given graph is bipartite.
  166. */
  167. template < typename Graph, typename IndexMap, typename PartitionMap >
  168. bool is_bipartite(
  169. const Graph& graph, const IndexMap index_map, PartitionMap partition_map)
  170. {
  171. /// General types and variables
  172. typedef
  173. typename property_traits< PartitionMap >::value_type partition_color_t;
  174. typedef
  175. typename graph_traits< Graph >::vertex_descriptor vertex_descriptor_t;
  176. /// Declare dfs visitor
  177. // detail::empty_recorder recorder;
  178. // typedef detail::bipartite_visitor <PartitionMap,
  179. // detail::empty_recorder> dfs_visitor_t; dfs_visitor_t dfs_visitor
  180. // (partition_map, recorder);
  181. /// Call dfs
  182. try
  183. {
  184. depth_first_search(graph,
  185. vertex_index_map(index_map).visitor(make_dfs_visitor(
  186. std::make_pair(detail::colorize_bipartition(partition_map),
  187. std::make_pair(detail::check_bipartition(partition_map),
  188. put_property(partition_map,
  189. color_traits< partition_color_t >::white(),
  190. on_start_vertex()))))));
  191. }
  192. catch (const detail::bipartite_visitor_error< vertex_descriptor_t >&)
  193. {
  194. return false;
  195. }
  196. return true;
  197. }
  198. /**
  199. * Checks a given graph for bipartiteness.
  200. *
  201. * @param graph The given graph.
  202. * @param index_map An index map associating vertices with an index.
  203. * @return true if and only if the given graph is bipartite.
  204. */
  205. template < typename Graph, typename IndexMap >
  206. bool is_bipartite(const Graph& graph, const IndexMap index_map)
  207. {
  208. typedef one_bit_color_map< IndexMap > partition_map_t;
  209. partition_map_t partition_map(num_vertices(graph), index_map);
  210. return is_bipartite(graph, index_map, partition_map);
  211. }
  212. /**
  213. * Checks a given graph for bipartiteness. The graph must
  214. * have an internal vertex_index property. Runs in linear time in the
  215. * size of the graph, if access to the property maps is in constant time.
  216. *
  217. * @param graph The given graph.
  218. * @return true if and only if the given graph is bipartite.
  219. */
  220. template < typename Graph > bool is_bipartite(const Graph& graph)
  221. {
  222. return is_bipartite(graph, get(vertex_index, graph));
  223. }
  224. /**
  225. * Checks a given graph for bipartiteness and fills a given color map with
  226. * white and black according to the bipartition. If the graph is not
  227. * bipartite, a sequence of vertices, producing an odd-cycle, is written to
  228. * the output iterator. The final iterator value is returned. Runs in linear
  229. * time in the size of the graph, if access to the property maps is in
  230. * constant time.
  231. *
  232. * @param graph The given graph.
  233. * @param index_map An index map associating vertices with an index.
  234. * @param partition_map A color map to fill with the bipartition.
  235. * @param result An iterator to write the odd-cycle vertices to.
  236. * @return The final iterator value after writing.
  237. */
  238. template < typename Graph, typename IndexMap, typename PartitionMap,
  239. typename OutputIterator >
  240. OutputIterator find_odd_cycle(const Graph& graph, const IndexMap index_map,
  241. PartitionMap partition_map, OutputIterator result)
  242. {
  243. /// General types and variables
  244. typedef
  245. typename property_traits< PartitionMap >::value_type partition_color_t;
  246. typedef
  247. typename graph_traits< Graph >::vertex_descriptor vertex_descriptor_t;
  248. typedef typename graph_traits< Graph >::vertex_iterator vertex_iterator_t;
  249. vertex_iterator_t vertex_iter, vertex_end;
  250. /// Declare predecessor map
  251. typedef std::vector< vertex_descriptor_t > predecessors_t;
  252. typedef iterator_property_map< typename predecessors_t::iterator, IndexMap,
  253. vertex_descriptor_t, vertex_descriptor_t& >
  254. predecessor_map_t;
  255. predecessors_t predecessors(
  256. num_vertices(graph), graph_traits< Graph >::null_vertex());
  257. predecessor_map_t predecessor_map(predecessors.begin(), index_map);
  258. /// Initialize predecessor map
  259. for (boost::tie(vertex_iter, vertex_end) = vertices(graph);
  260. vertex_iter != vertex_end; ++vertex_iter)
  261. {
  262. put(predecessor_map, *vertex_iter, *vertex_iter);
  263. }
  264. /// Call dfs
  265. try
  266. {
  267. depth_first_search(graph,
  268. vertex_index_map(index_map).visitor(make_dfs_visitor(
  269. std::make_pair(detail::colorize_bipartition(partition_map),
  270. std::make_pair(detail::check_bipartition(partition_map),
  271. std::make_pair(
  272. put_property(partition_map,
  273. color_traits< partition_color_t >::white(),
  274. on_start_vertex()),
  275. record_predecessors(
  276. predecessor_map, on_tree_edge())))))));
  277. }
  278. catch (const detail::bipartite_visitor_error< vertex_descriptor_t >& error)
  279. {
  280. typedef std::vector< vertex_descriptor_t > path_t;
  281. path_t path1, path2;
  282. vertex_descriptor_t next, current;
  283. /// First path
  284. next = error.witnesses.first;
  285. do
  286. {
  287. current = next;
  288. path1.push_back(current);
  289. next = predecessor_map[current];
  290. } while (current != next);
  291. /// Second path
  292. next = error.witnesses.second;
  293. do
  294. {
  295. current = next;
  296. path2.push_back(current);
  297. next = predecessor_map[current];
  298. } while (current != next);
  299. /// Find beginning of common suffix
  300. std::pair< typename path_t::iterator, typename path_t::iterator >
  301. mismatch = detail::reverse_mismatch(
  302. std::make_pair(path1.begin(), path1.end()),
  303. std::make_pair(path2.begin(), path2.end()));
  304. /// Copy the odd-length cycle
  305. result = std::copy(path1.begin(), mismatch.first + 1, result);
  306. return std::reverse_copy(path2.begin(), mismatch.second, result);
  307. }
  308. return result;
  309. }
  310. /**
  311. * Checks a given graph for bipartiteness. If the graph is not bipartite, a
  312. * sequence of vertices, producing an odd-cycle, is written to the output
  313. * iterator. The final iterator value is returned. Runs in linear time in the
  314. * size of the graph, if access to the property maps is in constant time.
  315. *
  316. * @param graph The given graph.
  317. * @param index_map An index map associating vertices with an index.
  318. * @param result An iterator to write the odd-cycle vertices to.
  319. * @return The final iterator value after writing.
  320. */
  321. template < typename Graph, typename IndexMap, typename OutputIterator >
  322. OutputIterator find_odd_cycle(
  323. const Graph& graph, const IndexMap index_map, OutputIterator result)
  324. {
  325. typedef one_bit_color_map< IndexMap > partition_map_t;
  326. partition_map_t partition_map(num_vertices(graph), index_map);
  327. return find_odd_cycle(graph, index_map, partition_map, result);
  328. }
  329. /**
  330. * Checks a given graph for bipartiteness. If the graph is not bipartite, a
  331. * sequence of vertices, producing an odd-cycle, is written to the output
  332. * iterator. The final iterator value is returned. The graph must have an
  333. * internal vertex_index property. Runs in linear time in the size of the
  334. * graph, if access to the property maps is in constant time.
  335. *
  336. * @param graph The given graph.
  337. * @param result An iterator to write the odd-cycle vertices to.
  338. * @return The final iterator value after writing.
  339. */
  340. template < typename Graph, typename OutputIterator >
  341. OutputIterator find_odd_cycle(const Graph& graph, OutputIterator result)
  342. {
  343. return find_odd_cycle(graph, get(vertex_index, graph), result);
  344. }
  345. }
  346. #endif /// BOOST_GRAPH_BIPARTITE_HPP