bipartite_example.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. #include <iostream>
  13. #include <boost/graph/adjacency_list.hpp>
  14. #include <boost/graph/bipartite.hpp>
  15. using namespace boost;
  16. /// Example to test for bipartiteness and print the certificates.
  17. template <typename Graph>
  18. void print_bipartite (const Graph& g)
  19. {
  20. typedef graph_traits <Graph> traits;
  21. typename traits::vertex_iterator vertex_iter, vertex_end;
  22. /// Most simple interface just tests for bipartiteness.
  23. bool bipartite = is_bipartite (g);
  24. if (bipartite)
  25. {
  26. typedef std::vector <default_color_type> partition_t;
  27. typedef typename property_map <Graph, vertex_index_t>::type index_map_t;
  28. typedef iterator_property_map <partition_t::iterator, index_map_t> partition_map_t;
  29. partition_t partition (num_vertices (g));
  30. partition_map_t partition_map (partition.begin (), get (vertex_index, g));
  31. /// A second interface yields a bipartition in a color map, if the graph is bipartite.
  32. is_bipartite (g, get (vertex_index, g), partition_map);
  33. for (boost::tie (vertex_iter, vertex_end) = vertices (g); vertex_iter != vertex_end; ++vertex_iter)
  34. {
  35. std::cout << "Vertex " << *vertex_iter << " has color " << (get (partition_map, *vertex_iter) == color_traits <
  36. default_color_type>::white () ? "white" : "black") << std::endl;
  37. }
  38. }
  39. else
  40. {
  41. typedef std::vector <typename traits::vertex_descriptor> vertex_vector_t;
  42. vertex_vector_t odd_cycle;
  43. /// A third interface yields an odd-cycle if the graph is not bipartite.
  44. find_odd_cycle (g, get (vertex_index, g), std::back_inserter (odd_cycle));
  45. std::cout << "Odd cycle consists of the vertices:";
  46. for (size_t i = 0; i < odd_cycle.size (); ++i)
  47. {
  48. std::cout << " " << odd_cycle[i];
  49. }
  50. std::cout << std::endl;
  51. }
  52. }
  53. int main (int argc, char **argv)
  54. {
  55. typedef adjacency_list <vecS, vecS, undirectedS> vector_graph_t;
  56. typedef std::pair <int, int> E;
  57. /**
  58. * Create the graph drawn below.
  59. *
  60. * 0 - 1 - 2
  61. * | |
  62. * 3 - 4 - 5 - 6
  63. * / \ /
  64. * | 7
  65. * | |
  66. * 8 - 9 - 10
  67. **/
  68. E bipartite_edges[] = { E (0, 1), E (0, 4), E (1, 2), E (2, 6), E (3, 4), E (3, 8), E (4, 5), E (4, 7), E (5, 6), E (
  69. 6, 7), E (7, 10), E (8, 9), E (9, 10) };
  70. vector_graph_t bipartite_vector_graph (&bipartite_edges[0],
  71. &bipartite_edges[0] + sizeof(bipartite_edges) / sizeof(E), 11);
  72. /**
  73. * Create the graph drawn below.
  74. *
  75. * 2 - 1 - 0
  76. * | |
  77. * 3 - 6 - 5 - 4
  78. * / \ /
  79. * | 7
  80. * | /
  81. * 8 ---- 9
  82. *
  83. **/
  84. E non_bipartite_edges[] = { E (0, 1), E (0, 4), E (1, 2), E (2, 6), E (3, 6), E (3, 8), E (4, 5), E (4, 7), E (5, 6),
  85. E (6, 7), E (7, 9), E (8, 9) };
  86. vector_graph_t non_bipartite_vector_graph (&non_bipartite_edges[0], &non_bipartite_edges[0]
  87. + sizeof(non_bipartite_edges) / sizeof(E), 10);
  88. /// Call test routine for a bipartite and a non-bipartite graph.
  89. print_bipartite (bipartite_vector_graph);
  90. print_bipartite (non_bipartite_vector_graph);
  91. return 0;
  92. }