1 | // Copyright (C) 2001 Vladimir Prus <ghost@cs.msu.su> |
---|
2 | // Copyright (C) 2001 Jeremy Siek <jsiek@cs.indiana.edu> |
---|
3 | // Distributed under the Boost Software License, Version 1.0. (See |
---|
4 | // accompanying file LICENSE_1_0.txt or copy at |
---|
5 | // http://www.boost.org/LICENSE_1_0.txt) |
---|
6 | |
---|
7 | |
---|
8 | #include <boost/graph/vector_as_graph.hpp> |
---|
9 | #include <boost/graph/transitive_closure.hpp> |
---|
10 | |
---|
11 | #include <iostream> |
---|
12 | |
---|
13 | #include <boost/graph/vector_as_graph.hpp> |
---|
14 | #include <boost/graph/adjacency_list.hpp> |
---|
15 | #include <boost/graph/adjacency_list_io.hpp> |
---|
16 | #include <boost/graph/graph_utility.hpp> |
---|
17 | |
---|
18 | #include <cstdlib> |
---|
19 | #include <ctime> |
---|
20 | #include <boost/progress.hpp> |
---|
21 | using namespace std; |
---|
22 | using namespace boost; |
---|
23 | |
---|
24 | void generate_graph(int n, double p, vector< vector<int> >& r1) |
---|
25 | { |
---|
26 | static class { |
---|
27 | public: |
---|
28 | double operator()() { |
---|
29 | return double(rand())/RAND_MAX; |
---|
30 | } |
---|
31 | } gen; |
---|
32 | r1.clear(); |
---|
33 | r1.resize(n); |
---|
34 | for (int i = 0; i < n; ++i) |
---|
35 | for (int j = 0; j < n; ++j) |
---|
36 | if (gen() < p) |
---|
37 | r1[i].push_back(j); |
---|
38 | } |
---|
39 | |
---|
40 | template <class Graph> |
---|
41 | typename graph_traits<Graph>::degree_size_type |
---|
42 | num_incident(typename graph_traits<Graph>::vertex_descriptor u, |
---|
43 | typename graph_traits<Graph>::vertex_descriptor v, |
---|
44 | const Graph& g) |
---|
45 | { |
---|
46 | typename graph_traits<Graph>::degree_size_type d = 0; |
---|
47 | typename graph_traits<Graph>::out_edge_iterator i, i_end; |
---|
48 | for (tie(i, i_end) = out_edges(u, g); i != i_end; ++i) |
---|
49 | if (target(*i, g) == v) |
---|
50 | ++d; |
---|
51 | return d; |
---|
52 | } |
---|
53 | |
---|
54 | |
---|
55 | // (i,j) is in E' iff j is reachable from i |
---|
56 | // Hmm, is_reachable does not detect when there is a non-trivial path |
---|
57 | // from i to i. It always returns true for is_reachable(i,i). |
---|
58 | // This needs to be fixed/worked around. |
---|
59 | template <typename Graph, typename GraphTC> |
---|
60 | bool check_transitive_closure(Graph& g, GraphTC& tc) |
---|
61 | { |
---|
62 | typename graph_traits<Graph>::vertex_iterator i, i_end; |
---|
63 | for (tie(i, i_end) = vertices(g); i != i_end; ++i) { |
---|
64 | typename graph_traits<Graph>::vertex_iterator j, j_end; |
---|
65 | for (tie(j, j_end) = vertices(g); j != j_end; ++j) { |
---|
66 | bool g_has_edge; |
---|
67 | typename graph_traits<Graph>::edge_descriptor e_g; |
---|
68 | typename graph_traits<Graph>::degree_size_type num_tc; |
---|
69 | tie (e_g, g_has_edge) = edge(*i, *j, g); |
---|
70 | num_tc = num_incident(*i, *j, tc); |
---|
71 | if (*i == *j) { |
---|
72 | if (g_has_edge) { |
---|
73 | if (num_tc != 1) |
---|
74 | return false; |
---|
75 | } else { |
---|
76 | bool can_reach = false; |
---|
77 | typename graph_traits<Graph>::adjacency_iterator k, k_end; |
---|
78 | for (tie(k, k_end) = adjacent_vertices(*i, g); k != k_end; ++k) { |
---|
79 | std::vector<default_color_type> color_map_vec(num_vertices(g)); |
---|
80 | if (is_reachable(*k, *i, g, &color_map_vec[0])) { |
---|
81 | can_reach = true; |
---|
82 | break; |
---|
83 | } |
---|
84 | } |
---|
85 | if (can_reach) { |
---|
86 | if (num_tc != 1) { |
---|
87 | std::cout << "1. " << *i << std::endl; |
---|
88 | return false; |
---|
89 | } |
---|
90 | } else { |
---|
91 | if (num_tc != 0) { |
---|
92 | std::cout << "2. " << *i << std::endl; |
---|
93 | return false; |
---|
94 | } |
---|
95 | } |
---|
96 | } |
---|
97 | } else { |
---|
98 | std::vector<default_color_type> color_map_vec(num_vertices(g)); |
---|
99 | if (is_reachable(*i, *j, g, &color_map_vec[0])) { |
---|
100 | if (num_tc != 1) |
---|
101 | return false; |
---|
102 | } else { |
---|
103 | if (num_tc != 0) |
---|
104 | return false; |
---|
105 | } |
---|
106 | } |
---|
107 | } |
---|
108 | } |
---|
109 | return true; |
---|
110 | } |
---|
111 | |
---|
112 | bool test(int n, double p) |
---|
113 | { |
---|
114 | vector< vector<int> > g1, g1_tc; |
---|
115 | generate_graph(n, p, g1); |
---|
116 | cout << "Created graph with " << n << " vertices.\n"; |
---|
117 | |
---|
118 | vector< vector<int> > g1_c(g1); |
---|
119 | |
---|
120 | { |
---|
121 | progress_timer t; |
---|
122 | cout << "transitive_closure" << endl; |
---|
123 | transitive_closure(g1, g1_tc, vertex_index_map(identity_property_map())); |
---|
124 | } |
---|
125 | |
---|
126 | if(check_transitive_closure(g1, g1_tc)) |
---|
127 | return true; |
---|
128 | else { |
---|
129 | cout << "Original graph was "; |
---|
130 | print_graph(g1, identity_property_map()); |
---|
131 | cout << "Result is "; |
---|
132 | print_graph(g1_tc, identity_property_map()); |
---|
133 | return false; |
---|
134 | } |
---|
135 | } |
---|
136 | |
---|
137 | |
---|
138 | int main() |
---|
139 | { |
---|
140 | srand(time(0)); |
---|
141 | static class { |
---|
142 | public: |
---|
143 | double operator()() { |
---|
144 | return double(rand())/RAND_MAX; |
---|
145 | } |
---|
146 | } gen; |
---|
147 | |
---|
148 | |
---|
149 | for (size_t i = 0; i < 100; ++i) { |
---|
150 | int n = 0 + int(20*gen()); |
---|
151 | double p = gen(); |
---|
152 | if (!test(n, p)) { |
---|
153 | cout << "Failed." << endl; |
---|
154 | return 1; |
---|
155 | } |
---|
156 | } |
---|
157 | cout << "Passed." << endl; |
---|
158 | } |
---|
159 | |
---|