Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/boost_1_33_1/libs/graph/test/subgraph.cpp @ 12

Last change on this file since 12 was 12, checked in by landauf, 17 years ago

added boost

File size: 3.8 KB
Line 
1//  (C) Copyright Jeremy Siek 2004
2//  Distributed under the Boost Software License, Version 1.0. (See
3//  accompanying file LICENSE_1_0.txt or copy at
4//  http://www.boost.org/LICENSE_1_0.txt)
5
6#include <set>
7
8#include <boost/test/minimal.hpp>
9
10#include <boost/graph/subgraph.hpp>
11#include <boost/graph/adjacency_list.hpp>
12#include <boost/graph/random.hpp>
13#include <boost/graph/graph_test.hpp>
14#include <boost/graph/iteration_macros.hpp>
15
16#include <boost/random/mersenne_twister.hpp>
17
18// UNDER CONSTRUCTION
19
20int test_main(int argc, char* argv[])
21{
22  using namespace boost;
23  typedef adjacency_list<vecS, vecS, bidirectionalS,
24    property<vertex_color_t, int>,
25    property<edge_index_t, std::size_t, property<edge_weight_t, int> >
26  > graph_t;
27  typedef subgraph<graph_t> subgraph_t;
28  typedef graph_traits<subgraph_t>::vertex_descriptor vertex_t;
29  typedef graph_traits<subgraph_t>::edge_descriptor edge_t;
30
31  mt19937 gen;
32  for (int t = 0; t < 100; t += 5) {
33    subgraph_t g;
34    int N = t + 2;
35    std::vector<vertex_t> vertex_set;
36    std::vector< std::pair<vertex_t, vertex_t> > edge_set;
37    generate_random_graph(g, N, N * 2, gen,
38                          std::back_inserter(vertex_set),
39                          std::back_inserter(edge_set));
40
41    graph_test< subgraph_t > gt;
42
43    gt.test_incidence_graph(vertex_set, edge_set, g);
44    gt.test_bidirectional_graph(vertex_set, edge_set, g);
45    gt.test_adjacency_graph(vertex_set, edge_set, g);
46    gt.test_vertex_list_graph(vertex_set, g);
47    gt.test_edge_list_graph(vertex_set, edge_set, g);
48    gt.test_adjacency_matrix(vertex_set, edge_set, g);
49
50    std::vector<vertex_t> sub_vertex_set;
51    std::vector<vertex_t> sub_global_map;
52    std::vector<vertex_t> global_sub_map(num_vertices(g));
53    std::vector< std::pair<vertex_t, vertex_t> > sub_edge_set;
54
55    subgraph_t& g_s = g.create_subgraph();
56
57    const std::set<vertex_t>::size_type Nsub = N/2;
58
59    // Collect a set of random vertices to put in the subgraph
60    std::set<vertex_t> verts;
61    while (verts.size() < Nsub)
62      verts.insert(random_vertex(g, gen));
63
64    for (std::set<vertex_t>::iterator it = verts.begin();
65        it != verts.end(); ++it) {
66      vertex_t v_global = *it;
67      vertex_t v = add_vertex(v_global, g_s);
68      sub_vertex_set.push_back(v);
69      sub_global_map.push_back(v_global);
70      global_sub_map[v_global] = v;
71    }
72
73    // compute induced edges
74    BGL_FORALL_EDGES(e, g, subgraph_t)
75      if (contains(sub_global_map, source(e, g))
76          && contains(sub_global_map, target(e, g)))
77        sub_edge_set.push_back(std::make_pair(global_sub_map[source(e, g)],
78                                              global_sub_map[target(e, g)]));
79
80    gt.test_incidence_graph(sub_vertex_set, sub_edge_set, g_s);
81    gt.test_bidirectional_graph(sub_vertex_set, sub_edge_set, g_s);
82    gt.test_adjacency_graph(sub_vertex_set, sub_edge_set, g_s);
83    gt.test_vertex_list_graph(sub_vertex_set, g_s);
84    gt.test_edge_list_graph(sub_vertex_set, sub_edge_set, g_s);
85    gt.test_adjacency_matrix(sub_vertex_set, sub_edge_set, g_s);
86
87    if (num_vertices(g_s) == 0)
88      return 0;
89    std::vector<int> weights;
90    for (unsigned i = 0; i < num_vertices(g_s); ++i)
91    weights.push_back(i*2);
92    gt.test_vertex_property_graph(weights, vertex_color_t(), g_s);
93
94    // A regression test: the copy constructor of subgraph did not
95    // copy one of the members, so local_edge->global_edge mapping
96    // was broken.
97    {
98        subgraph_t g;
99        graph_t::vertex_descriptor v1, v2;
100        v1 = add_vertex(g);
101        v2 = add_vertex(g);
102        add_edge(v1, v2, g);
103
104        subgraph_t sub = g.create_subgraph(vertices(g).first, vertices(g).second);
105
106        graph_t::edge_iterator ei, ee;
107        for (tie(ei, ee) = edges(sub); ei != ee; ++ei) {
108            // This used to segfault.
109            get(edge_weight, sub, *ei);
110        }
111    }
112
113  }
114  return 0;
115}
Note: See TracBrowser for help on using the repository browser.