1 | // Copyright 2004 The Trustees of Indiana University. |
---|
2 | |
---|
3 | // Use, modification and distribution is subject to the Boost Software |
---|
4 | // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at |
---|
5 | // http://www.boost.org/LICENSE_1_0.txt) |
---|
6 | |
---|
7 | // Authors: Douglas Gregor |
---|
8 | // Andrew Lumsdaine |
---|
9 | #include <boost/graph/biconnected_components.hpp> |
---|
10 | #include <boost/graph/adjacency_list.hpp> |
---|
11 | #include <boost/test/minimal.hpp> |
---|
12 | #include <boost/lexical_cast.hpp> |
---|
13 | #include <vector> |
---|
14 | #include <iterator> |
---|
15 | #include <iostream> |
---|
16 | #include <algorithm> |
---|
17 | #include <boost/graph/connected_components.hpp> |
---|
18 | #include <boost/graph/random.hpp> |
---|
19 | #include <boost/random/linear_congruential.hpp> |
---|
20 | #include <fstream> |
---|
21 | |
---|
22 | using namespace boost; |
---|
23 | |
---|
24 | struct EdgeProperty |
---|
25 | { |
---|
26 | std::size_t component; |
---|
27 | }; |
---|
28 | |
---|
29 | static bool any_errors = false; |
---|
30 | |
---|
31 | template<typename Graph, typename Vertex> |
---|
32 | void |
---|
33 | check_articulation_points(const Graph& g, std::vector<Vertex> art_points) |
---|
34 | { |
---|
35 | std::vector<int> components(num_vertices(g)); |
---|
36 | int basic_comps = |
---|
37 | connected_components(g, |
---|
38 | make_iterator_property_map(components.begin(), |
---|
39 | get(vertex_index, g), |
---|
40 | int())); |
---|
41 | |
---|
42 | std::vector<Vertex> art_points_check; |
---|
43 | |
---|
44 | typename graph_traits<Graph>::vertex_iterator vi, vi_end; |
---|
45 | for (tie(vi, vi_end) = vertices(g); vi != vi_end; ++vi) { |
---|
46 | Graph g_copy(g); |
---|
47 | Vertex victim = vertex(get(vertex_index, g, *vi), g_copy); |
---|
48 | clear_vertex(victim, g_copy); |
---|
49 | remove_vertex(victim, g_copy); |
---|
50 | |
---|
51 | int copy_comps = |
---|
52 | connected_components |
---|
53 | (g_copy, |
---|
54 | make_iterator_property_map(components.begin(), |
---|
55 | get(vertex_index, g_copy), |
---|
56 | int())); |
---|
57 | |
---|
58 | if (copy_comps > basic_comps) |
---|
59 | art_points_check.push_back(*vi); |
---|
60 | } |
---|
61 | |
---|
62 | std::sort(art_points.begin(), art_points.end()); |
---|
63 | std::sort(art_points_check.begin(), art_points_check.end()); |
---|
64 | |
---|
65 | BOOST_CHECK(art_points == art_points_check); |
---|
66 | if (art_points != art_points_check) { |
---|
67 | std::cerr << "ERROR!" << std::endl; |
---|
68 | std::cerr << "\tComputed: "; |
---|
69 | std::size_t i; |
---|
70 | for (i = 0; i < art_points.size(); ++i) |
---|
71 | std::cout << art_points[i] << ' '; |
---|
72 | std::cout << std::endl << "\tExpected: "; |
---|
73 | for (i = 0; i < art_points_check.size(); ++i) |
---|
74 | std::cout << art_points_check[i] << ' '; |
---|
75 | std::cout << std::endl; |
---|
76 | any_errors = true; |
---|
77 | } else std::cout << "OK." << std::endl; |
---|
78 | } |
---|
79 | |
---|
80 | int test_main(int argc, char* argv[]) |
---|
81 | { |
---|
82 | std::size_t n = 100; |
---|
83 | std::size_t m = 500; |
---|
84 | std::size_t seed = 1; |
---|
85 | |
---|
86 | if (argc > 1) n = lexical_cast<std::size_t>(argv[1]); |
---|
87 | if (argc > 2) m = lexical_cast<std::size_t>(argv[2]); |
---|
88 | if (argc > 3) seed = lexical_cast<std::size_t>(argv[3]); |
---|
89 | |
---|
90 | typedef adjacency_list<listS, vecS, undirectedS, |
---|
91 | no_property, EdgeProperty> Graph; |
---|
92 | typedef graph_traits<Graph>::vertex_descriptor Vertex; |
---|
93 | |
---|
94 | Graph g(n); |
---|
95 | minstd_rand gen(seed); |
---|
96 | generate_random_graph(g, n, m, gen); |
---|
97 | std::vector<Vertex> art_points; |
---|
98 | |
---|
99 | std::cout << "Computing biconnected components & articulation points... "; |
---|
100 | std::cout.flush(); |
---|
101 | |
---|
102 | std::size_t num_comps = |
---|
103 | biconnected_components(g, |
---|
104 | get(&EdgeProperty::component, g), |
---|
105 | std::back_inserter(art_points)).first; |
---|
106 | |
---|
107 | std::cout << "done.\n\t" << num_comps << " biconnected components.\n" |
---|
108 | << "\t" << art_points.size() << " articulation points.\n" |
---|
109 | << "\tTesting articulation points..."; |
---|
110 | std::cout.flush(); |
---|
111 | |
---|
112 | check_articulation_points(g, art_points); |
---|
113 | |
---|
114 | if (any_errors) { |
---|
115 | std::ofstream out("biconnected_components_test_failed.dot"); |
---|
116 | |
---|
117 | out << "graph A {\n" << " node[shape=\"circle\"]\n"; |
---|
118 | |
---|
119 | for (std::size_t i = 0; i < art_points.size(); ++i) { |
---|
120 | out << art_points[i] << " [ style=\"filled\" ];" << std::endl; |
---|
121 | } |
---|
122 | |
---|
123 | graph_traits<Graph>::edge_iterator ei, ei_end; |
---|
124 | for (tie(ei, ei_end) = edges(g); ei != ei_end; ++ei) |
---|
125 | out << source(*ei, g) << " -- " << target(*ei, g) |
---|
126 | << "[label=\"" << g[*ei].component << "\"]\n"; |
---|
127 | out << "}\n"; |
---|
128 | } |
---|
129 | |
---|
130 | return 0; |
---|
131 | } |
---|