File: stoer_wagner_test.cpp

package info (click to toggle)
boost1.88 1.88.0-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 576,932 kB
  • sloc: cpp: 4,149,234; xml: 136,789; ansic: 35,092; python: 33,910; asm: 5,698; sh: 4,604; ada: 1,681; makefile: 1,633; pascal: 1,139; perl: 1,124; sql: 640; yacc: 478; ruby: 271; java: 77; lisp: 24; csh: 6
file content (327 lines) | stat: -rw-r--r-- 12,343 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
//            Copyright Daniel Trebbien 2010.
// Distributed under the Boost Software License, Version 1.0.
//   (See accompanying file LICENSE_1_0.txt or the copy at
//         http://www.boost.org/LICENSE_1_0.txt)

#if defined(_MSC_VER) && !defined(_CRT_SECURE_NO_WARNINGS)
#define _CRT_SECURE_NO_WARNINGS
#endif

#include <fstream>
#include <iostream>
#include <map>
#include <vector>
#include <string>
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/connected_components.hpp>
#include <boost/graph/exception.hpp>
#include <boost/graph/graph_traits.hpp>
#include <boost/graph/read_dimacs.hpp>
#include <boost/graph/stoer_wagner_min_cut.hpp>
#include <boost/graph/property_maps/constant_property_map.hpp>
#include <boost/property_map/property_map.hpp>
#include <boost/core/lightweight_test.hpp>
#include <boost/tuple/tuple.hpp>

typedef boost::adjacency_list< boost::vecS, boost::vecS, boost::undirectedS,
    boost::no_property, boost::property< boost::edge_weight_t, int > >
    undirected_graph;
typedef boost::property_map< undirected_graph, boost::edge_weight_t >::type
    weight_map_type;
typedef boost::property_traits< weight_map_type >::value_type weight_type;

typedef boost::adjacency_list< boost::vecS, boost::vecS, boost::undirectedS >
    undirected_unweighted_graph;

std::string test_dir;

struct edge_t
{
    unsigned long first;
    unsigned long second;
};

// the example from Stoer & Wagner (1997)
void test0()
{
    edge_t edges[] = { { 0, 1 }, { 1, 2 }, { 2, 3 }, { 0, 4 }, { 1, 4 },
        { 1, 5 }, { 2, 6 }, { 3, 6 }, { 3, 7 }, { 4, 5 }, { 5, 6 }, { 6, 7 } };
    weight_type ws[] = { 2, 3, 4, 3, 2, 2, 2, 2, 2, 3, 1, 3 };
    undirected_graph g(edges, edges + 12, ws, 8, 12);

    weight_map_type weights = get(boost::edge_weight, g);
    std::map< int, bool > parity;
    boost::associative_property_map< std::map< int, bool > > parities(parity);
    int w
        = boost::stoer_wagner_min_cut(g, weights, boost::parity_map(parities));
    BOOST_TEST_EQ(w, 4);
    const bool parity0 = get(parities, 0);
    BOOST_TEST_EQ(parity0, get(parities, 1));
    BOOST_TEST_EQ(parity0, get(parities, 4));
    BOOST_TEST_EQ(parity0, get(parities, 5));
    const bool parity2 = get(parities, 2);
    BOOST_TEST_NE(parity0, parity2);
    BOOST_TEST_EQ(parity2, get(parities, 3));
    BOOST_TEST_EQ(parity2, get(parities, 6));
    BOOST_TEST_EQ(parity2, get(parities, 7));
}

void test1()
{
    { // if only one vertex, can't run `boost::stoer_wagner_min_cut`
        undirected_graph g;
        add_vertex(g);

        BOOST_TEST_THROWS(
            boost::stoer_wagner_min_cut(g, get(boost::edge_weight, g)),
            boost::bad_graph);
    }
    { // three vertices with one multi-edge
        typedef boost::graph_traits< undirected_graph >::vertex_descriptor
            vertex_descriptor;

        edge_t edges[] = { { 0, 1 }, { 1, 2 }, { 1, 2 }, { 2, 0 } };
        weight_type ws[] = { 3, 1, 1, 1 };
        undirected_graph g(edges, edges + 4, ws, 3, 4);

        weight_map_type weights = get(boost::edge_weight, g);
        std::map< int, bool > parity;
        boost::associative_property_map< std::map< int, bool > > parities(
            parity);
        std::map< vertex_descriptor, vertex_descriptor > assignment;
        boost::associative_property_map<
            std::map< vertex_descriptor, vertex_descriptor > >
            assignments(assignment);
        int w = boost::stoer_wagner_min_cut(g, weights,
            boost::parity_map(parities).vertex_assignment_map(assignments));
        BOOST_TEST_EQ(w, 3);
        const bool parity2 = get(parities, 2), parity0 = get(parities, 0);
        BOOST_TEST_NE(parity2, parity0);
        BOOST_TEST_EQ(parity0, get(parities, 1));
    }
}

// example by Daniel Trebbien
void test2()
{
    edge_t edges[] = { { 5, 2 }, { 0, 6 }, { 5, 6 }, { 3, 1 }, { 0, 1 },
        { 6, 3 }, { 4, 6 }, { 2, 4 }, { 5, 3 } };
    weight_type ws[] = { 1, 3, 4, 6, 4, 1, 2, 5, 2 };
    undirected_graph g(edges, edges + 9, ws, 7, 9);

    std::map< int, bool > parity;
    boost::associative_property_map< std::map< int, bool > > parities(parity);
    int w = boost::stoer_wagner_min_cut(
        g, get(boost::edge_weight, g), boost::parity_map(parities));
    BOOST_TEST_EQ(w, 3);
    const bool parity2 = get(parities, 2);
    BOOST_TEST_EQ(parity2, get(parities, 4));
    const bool parity5 = get(parities, 5);
    BOOST_TEST_NE(parity2, parity5);
    BOOST_TEST_EQ(parity5, get(parities, 3));
    BOOST_TEST_EQ(parity5, get(parities, 6));
    BOOST_TEST_EQ(parity5, get(parities, 1));
    BOOST_TEST_EQ(parity5, get(parities, 0));
}

// example by Daniel Trebbien
void test3()
{
    edge_t edges[] = { { 3, 4 }, { 3, 6 }, { 3, 5 }, { 0, 4 }, { 0, 1 },
        { 0, 6 }, { 0, 7 }, { 0, 5 }, { 0, 2 }, { 4, 1 }, { 1, 6 }, { 1, 5 },
        { 6, 7 }, { 7, 5 }, { 5, 2 }, { 3, 4 } };
    weight_type ws[] = { 0, 3, 1, 3, 1, 2, 6, 1, 8, 1, 1, 80, 2, 1, 1, 4 };
    undirected_graph g(edges, edges + 16, ws, 8, 16);

    weight_map_type weights = get(boost::edge_weight, g);
    std::map< int, bool > parity;
    boost::associative_property_map< std::map< int, bool > > parities(parity);
    int w
        = boost::stoer_wagner_min_cut(g, weights, boost::parity_map(parities));
    BOOST_TEST_EQ(w, 7);
    const bool parity1 = get(parities, 1);
    BOOST_TEST_EQ(parity1, get(parities, 5));
    const bool parity0 = get(parities, 0);
    BOOST_TEST_NE(parity1, parity0);
    BOOST_TEST_EQ(parity0, get(parities, 2));
    BOOST_TEST_EQ(parity0, get(parities, 3));
    BOOST_TEST_EQ(parity0, get(parities, 4));
    BOOST_TEST_EQ(parity0, get(parities, 6));
    BOOST_TEST_EQ(parity0, get(parities, 7));
}

void test4()
{
    typedef boost::graph_traits<
        undirected_unweighted_graph >::vertex_descriptor vertex_descriptor;
    typedef boost::graph_traits< undirected_unweighted_graph >::edge_descriptor
        edge_descriptor;

    edge_t edges[] = { { 0, 1 }, { 1, 2 }, { 2, 3 }, { 0, 4 }, { 1, 4 },
        { 1, 5 }, { 2, 6 }, { 3, 6 }, { 3, 7 }, { 4, 5 }, { 5, 6 }, { 6, 7 },
        { 0, 4 }, { 6, 7 } };
    undirected_unweighted_graph g(edges, edges + 14, 8);

    std::map< vertex_descriptor, bool > parity;
    boost::associative_property_map< std::map< vertex_descriptor, bool > >
        parities(parity);
    std::map< vertex_descriptor, vertex_descriptor > assignment;
    boost::associative_property_map<
        std::map< vertex_descriptor, vertex_descriptor > >
        assignments(assignment);
    int w = boost::stoer_wagner_min_cut(g,
        boost::make_constant_property< edge_descriptor >(weight_type(1)),
        boost::vertex_assignment_map(assignments).parity_map(parities));
    BOOST_TEST_EQ(w, 2);
    const bool parity0 = get(parities, 0);
    BOOST_TEST_EQ(parity0, get(parities, 1));
    BOOST_TEST_EQ(parity0, get(parities, 4));
    BOOST_TEST_EQ(parity0, get(parities, 5));
    const bool parity2 = get(parities, 2);
    BOOST_TEST_NE(parity0, parity2);
    BOOST_TEST_EQ(parity2, get(parities, 3));
    BOOST_TEST_EQ(parity2, get(parities, 6));
    BOOST_TEST_EQ(parity2, get(parities, 7));
}

// Non regression test for github.com/boostorg/graph/issues/286
void test5()
{
    edge_t edges[] = { { 0, 1 }, { 0, 2 }, { 0, 3 }, { 1, 2 }, { 1, 3 },
        { 2, 3 }, { 4, 5 }, { 4, 6 }, { 4, 7 }, { 5, 6 }, { 5, 7 }, { 6, 7 },
        { 0, 4 } };
    weight_type ws[] = { 3, 3, 3, 2, 2, 2, 3, 3, 3, 2, 2, 2, 6 };
    undirected_graph g(edges, edges + 13, ws, 8, 13);

    weight_map_type weights = get(boost::edge_weight, g);
    std::map< int, bool > parity;
    boost::associative_property_map< std::map< int, bool > > parities(parity);
    int w
        = boost::stoer_wagner_min_cut(g, weights, boost::parity_map(parities));
    BOOST_TEST_EQ(w, 6);
    const bool parity0 = get(parities, 0);
    BOOST_TEST_EQ(parity0, get(parities, 1));
    BOOST_TEST_EQ(parity0, get(parities, 2));
    BOOST_TEST_EQ(parity0, get(parities, 3));
    const bool parity4 = get(parities, 4);
    BOOST_TEST_NE(parity0, parity4);
    BOOST_TEST_EQ(parity4, get(parities, 5));
    BOOST_TEST_EQ(parity4, get(parities, 6));
    BOOST_TEST_EQ(parity4, get(parities, 7));
}

// The input for the `test_prgen` family of tests comes from a program, named
// `prgen`, that comes with a package of min-cut solvers by Chandra Chekuri,
// Andrew Goldberg, David Karger, Matthew Levine, and Cliff Stein. `prgen` was
// used to generate input graphs and the solvers were used to verify the return
// value of `boost::stoer_wagner_min_cut` on the input graphs.
//
// http://www.columbia.edu/~cs2035/code.html
//
// Note that it is somewhat more difficult to verify the parities because
// "`prgen` graphs" often have several min-cuts. This is why only the cut
// weight of the min-cut is verified.

// 3 min-cuts
void test_prgen_20_70_2()
{
    typedef boost::graph_traits< undirected_graph >::vertex_descriptor
        vertex_descriptor;

    std::ifstream ifs(
        (test_dir + "/prgen_input_graphs/prgen_20_70_2.net").c_str());
    undirected_graph g;
    boost::read_dimacs_min_cut(
        g, get(boost::edge_weight, g), boost::dummy_property_map(), ifs);

    std::map< vertex_descriptor, std::size_t > component;
    boost::associative_property_map<
        std::map< vertex_descriptor, std::size_t > >
        components(component);
    BOOST_TEST_EQ(boost::connected_components(g, components),
        1U); // verify the connectedness assumption

    typedef boost::shared_array_property_map< weight_type,
        boost::property_map< undirected_graph,
            boost::vertex_index_t >::const_type >
        distances_type;
    distances_type distances = boost::make_shared_array_property_map(
        num_vertices(g), weight_type(0), get(boost::vertex_index, g));
    typedef std::vector< vertex_descriptor >::size_type index_in_heap_type;
    typedef boost::shared_array_property_map< index_in_heap_type,
        boost::property_map< undirected_graph,
            boost::vertex_index_t >::const_type >
        indicesInHeap_type;
    indicesInHeap_type indicesInHeap = boost::make_shared_array_property_map(
        num_vertices(g), index_in_heap_type(-1), get(boost::vertex_index, g));
    boost::d_ary_heap_indirect< vertex_descriptor, 22, indicesInHeap_type,
        distances_type, std::greater< weight_type > >
        pq(distances, indicesInHeap);

    int w = boost::stoer_wagner_min_cut(
        g, get(boost::edge_weight, g), boost::max_priority_queue(pq));
    BOOST_TEST_EQ(w, 3407);
}

// 7 min-cuts
void test_prgen_50_40_2()
{
    typedef boost::graph_traits< undirected_graph >::vertex_descriptor
        vertex_descriptor;

    std::ifstream ifs(
        (test_dir + "/prgen_input_graphs/prgen_50_40_2.net").c_str());
    undirected_graph g;
    boost::read_dimacs_min_cut(
        g, get(boost::edge_weight, g), boost::dummy_property_map(), ifs);

    std::map< vertex_descriptor, std::size_t > component;
    boost::associative_property_map<
        std::map< vertex_descriptor, std::size_t > >
        components(component);
    BOOST_TEST_EQ(boost::connected_components(g, components),
        1U); // verify the connectedness assumption

    int w = boost::stoer_wagner_min_cut(g, get(boost::edge_weight, g));
    BOOST_TEST_EQ(w, 10056);
}

// 6 min-cuts
void test_prgen_50_70_2()
{
    typedef boost::graph_traits< undirected_graph >::vertex_descriptor
        vertex_descriptor;

    std::ifstream ifs(
        (test_dir + "/prgen_input_graphs/prgen_50_70_2.net").c_str());
    undirected_graph g;
    boost::read_dimacs_min_cut(
        g, get(boost::edge_weight, g), boost::dummy_property_map(), ifs);

    std::map< vertex_descriptor, std::size_t > component;
    boost::associative_property_map<
        std::map< vertex_descriptor, std::size_t > >
        components(component);
    BOOST_TEST_EQ(boost::connected_components(g, components),
        1U); // verify the connectedness assumption

    int w = boost::stoer_wagner_min_cut(g, get(boost::edge_weight, g));
    BOOST_TEST_EQ(w, 21755);
}

int main(int argc, char* argv[])
{
    if (BOOST_TEST(argc == 2))
    {
        test_dir = argv[1];
        test0();
        test1();
        test2();
        test3();
        test4();
        test5();
        test_prgen_20_70_2();
        test_prgen_50_70_2();
    }
    return boost::report_errors();
}