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
6 #include <cassert>
7 #include <cstddef>
8 #include <cstdlib>
9 #include <iostream>
10 #include <boost/graph/adjacency_list.hpp>
11 #include <boost/graph/graph_traits.hpp>
12 #include <boost/graph/one_bit_color_map.hpp>
13 #include <boost/graph/stoer_wagner_min_cut.hpp>
14 #include <boost/property_map/property_map.hpp>
15 #include <boost/typeof/typeof.hpp>
16
17 struct edge_t
18 {
19 unsigned long first;
20 unsigned long second;
21 };
22
23 // 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>
main()24 int main()
25 {
26 using namespace std;
27
28 typedef boost::adjacency_list<boost::vecS, boost::vecS, boost::undirectedS,
29 boost::no_property, boost::property<boost::edge_weight_t, int> > undirected_graph;
30 typedef boost::property_map<undirected_graph, boost::edge_weight_t>::type weight_map_type;
31 typedef boost::property_traits<weight_map_type>::value_type weight_type;
32
33 // define the 16 edges of the graph. {3, 4} means an undirected edge between vertices 3 and 4.
34 edge_t edges[] = {{3, 4}, {3, 6}, {3, 5}, {0, 4}, {0, 1}, {0, 6}, {0, 7},
35 {0, 5}, {0, 2}, {4, 1}, {1, 6}, {1, 5}, {6, 7}, {7, 5}, {5, 2}, {3, 4}};
36
37 // for each of the 16 edges, define the associated edge weight. ws[i] is the weight for the edge
38 // that is described by edges[i].
39 weight_type ws[] = {0, 3, 1, 3, 1, 2, 6, 1, 8, 1, 1, 80, 2, 1, 1, 4};
40
41 // construct the graph object. 8 is the number of vertices, which are numbered from 0
42 // through 7, and 16 is the number of edges.
43 undirected_graph g(edges, edges + 16, ws, 8, 16);
44
45 // define a property map, `parities`, that will store a boolean value for each vertex.
46 // Vertices that have the same parity after `stoer_wagner_min_cut` runs are on the same side of the min-cut.
47 BOOST_AUTO(parities, boost::make_one_bit_color_map(num_vertices(g), get(boost::vertex_index, g)));
48
49 // run the Stoer-Wagner algorithm to obtain the min-cut weight. `parities` is also filled in.
50 int w = boost::stoer_wagner_min_cut(g, get(boost::edge_weight, g), boost::parity_map(parities));
51
52 cout << "The min-cut weight of G is " << w << ".\n" << endl;
53 assert(w == 7);
54
55 cout << "One set of vertices consists of:" << endl;
56 size_t i;
57 for (i = 0; i < num_vertices(g); ++i) {
58 if (get(parities, i))
59 cout << i << endl;
60 }
61 cout << endl;
62
63 cout << "The other set of vertices consists of:" << endl;
64 for (i = 0; i < num_vertices(g); ++i) {
65 if (!get(parities, i))
66 cout << i << endl;
67 }
68 cout << endl;
69
70 return EXIT_SUCCESS;
71 }
72