stoer_wagner.cpp 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. // Copyright Daniel Trebbien 2010.
  2. // Distributed under the Boost Software License, Version 1.0.
  3. // (See accompanying file LICENSE_1_0.txt or the copy at
  4. // http://www.boost.org/LICENSE_1_0.txt)
  5. #include <cassert>
  6. #include <cstddef>
  7. #include <cstdlib>
  8. #include <iostream>
  9. #include <boost/graph/adjacency_list.hpp>
  10. #include <boost/graph/graph_traits.hpp>
  11. #include <boost/graph/one_bit_color_map.hpp>
  12. #include <boost/graph/stoer_wagner_min_cut.hpp>
  13. #include <boost/property_map/property_map.hpp>
  14. #include <boost/typeof/typeof.hpp>
  15. struct edge_t
  16. {
  17. unsigned long first;
  18. unsigned long second;
  19. };
  20. // A graphic of the min-cut is available at <http://www.boost.org/doc/libs/release/libs/graph/doc/stoer_wagner_imgs/stoer_wagner.cpp.gif>
  21. int main()
  22. {
  23. using namespace std;
  24. typedef boost::adjacency_list<boost::vecS, boost::vecS, boost::undirectedS,
  25. boost::no_property, boost::property<boost::edge_weight_t, int> > undirected_graph;
  26. typedef boost::property_map<undirected_graph, boost::edge_weight_t>::type weight_map_type;
  27. typedef boost::property_traits<weight_map_type>::value_type weight_type;
  28. // define the 16 edges of the graph. {3, 4} means an undirected edge between vertices 3 and 4.
  29. edge_t edges[] = {{3, 4}, {3, 6}, {3, 5}, {0, 4}, {0, 1}, {0, 6}, {0, 7},
  30. {0, 5}, {0, 2}, {4, 1}, {1, 6}, {1, 5}, {6, 7}, {7, 5}, {5, 2}, {3, 4}};
  31. // for each of the 16 edges, define the associated edge weight. ws[i] is the weight for the edge
  32. // that is described by edges[i].
  33. weight_type ws[] = {0, 3, 1, 3, 1, 2, 6, 1, 8, 1, 1, 80, 2, 1, 1, 4};
  34. // construct the graph object. 8 is the number of vertices, which are numbered from 0
  35. // through 7, and 16 is the number of edges.
  36. undirected_graph g(edges, edges + 16, ws, 8, 16);
  37. // define a property map, `parities`, that will store a boolean value for each vertex.
  38. // Vertices that have the same parity after `stoer_wagner_min_cut` runs are on the same side of the min-cut.
  39. BOOST_AUTO(parities, boost::make_one_bit_color_map(num_vertices(g), get(boost::vertex_index, g)));
  40. // run the Stoer-Wagner algorithm to obtain the min-cut weight. `parities` is also filled in.
  41. int w = boost::stoer_wagner_min_cut(g, get(boost::edge_weight, g), boost::parity_map(parities));
  42. cout << "The min-cut weight of G is " << w << ".\n" << endl;
  43. assert(w == 7);
  44. cout << "One set of vertices consists of:" << endl;
  45. size_t i;
  46. for (i = 0; i < num_vertices(g); ++i) {
  47. if (get(parities, i))
  48. cout << i << endl;
  49. }
  50. cout << endl;
  51. cout << "The other set of vertices consists of:" << endl;
  52. for (i = 0; i < num_vertices(g); ++i) {
  53. if (!get(parities, i))
  54. cout << i << endl;
  55. }
  56. cout << endl;
  57. return EXIT_SUCCESS;
  58. }