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)
7 // NOTE: this final is generated by libs/graph/doc/transitive_closure.w
9 #ifndef BOOST_GRAPH_TRANSITIVE_CLOSURE_HPP
10 #define BOOST_GRAPH_TRANSITIVE_CLOSURE_HPP
13 #include <algorithm> // for std::min and std::max
15 #include <boost/config.hpp>
16 #include <boost/bind.hpp>
17 #include <boost/graph/strong_components.hpp>
18 #include <boost/graph/topological_sort.hpp>
19 #include <boost/graph/graph_concepts.hpp>
20 #include <boost/graph/named_function_params.hpp>
21 #include <boost/graph/adjacency_list.hpp>
22 #include <boost/concept/assert.hpp>
30 union_successor_sets(const std::vector < std::size_t > &s1,
31 const std::vector < std::size_t > &s2,
32 std::vector < std::size_t > &s3)
34 BOOST_USING_STD_MIN();
35 for (std::size_t k = 0; k < s1.size(); ++k)
36 s3[k] = min BOOST_PREVENT_MACRO_SUBSTITUTION(s1[k], s2[k]);
42 template < typename TheContainer, typename ST = std::size_t,
43 typename VT = typename TheContainer::value_type >
44 struct subscript_t:public std::unary_function < ST, VT >
46 typedef VT& result_type;
48 subscript_t(TheContainer & c):container(&c)
51 VT & operator() (const ST & i) const
53 return (*container)[i];
56 TheContainer * container;
58 template < typename TheContainer >
59 subscript_t < TheContainer > subscript(TheContainer & c) {
60 return subscript_t < TheContainer > (c);
64 template < typename Graph, typename GraphTC,
65 typename G_to_TC_VertexMap,
66 typename VertexIndexMap >
67 void transitive_closure(const Graph & g, GraphTC & tc,
68 G_to_TC_VertexMap g_to_tc_map,
69 VertexIndexMap index_map)
71 if (num_vertices(g) == 0)
73 typedef typename graph_traits < Graph >::vertex_descriptor vertex;
74 typedef typename graph_traits < Graph >::vertex_iterator vertex_iterator;
75 typedef typename property_traits < VertexIndexMap >::value_type size_type;
76 typedef typename graph_traits <
77 Graph >::adjacency_iterator adjacency_iterator;
79 BOOST_CONCEPT_ASSERT(( VertexListGraphConcept < Graph > ));
80 BOOST_CONCEPT_ASSERT(( AdjacencyGraphConcept < Graph > ));
81 BOOST_CONCEPT_ASSERT(( VertexMutableGraphConcept < GraphTC > ));
82 BOOST_CONCEPT_ASSERT(( EdgeMutableGraphConcept < GraphTC > ));
83 BOOST_CONCEPT_ASSERT(( ReadablePropertyMapConcept < VertexIndexMap,
86 typedef size_type cg_vertex;
87 std::vector < cg_vertex > component_number_vec(num_vertices(g));
88 iterator_property_map < cg_vertex *, VertexIndexMap, cg_vertex, cg_vertex& >
89 component_number(&component_number_vec[0], index_map);
91 int num_scc = strong_components(g, component_number,
92 vertex_index_map(index_map));
94 std::vector < std::vector < vertex > >components;
95 build_component_lists(g, num_scc, component_number, components);
97 typedef boost::adjacency_list<boost::vecS, boost::vecS, boost::directedS> CG_t;
99 for (cg_vertex s = 0; s < components.size(); ++s) {
100 std::vector < cg_vertex > adj;
101 for (size_type i = 0; i < components[s].size(); ++i) {
102 vertex u = components[s][i];
103 adjacency_iterator v, v_end;
104 for (boost::tie(v, v_end) = adjacent_vertices(u, g); v != v_end; ++v) {
105 cg_vertex t = component_number[*v];
106 if (s != t) // Avoid loops in the condensation graph
110 std::sort(adj.begin(), adj.end());
111 typename std::vector<cg_vertex>::iterator di =
112 std::unique(adj.begin(), adj.end());
114 adj.erase(di, adj.end());
115 for (typename std::vector<cg_vertex>::const_iterator i = adj.begin();
116 i != adj.end(); ++i) {
121 std::vector<cg_vertex> topo_order;
122 std::vector<cg_vertex> topo_number(num_vertices(CG));
123 topological_sort(CG, std::back_inserter(topo_order),
124 vertex_index_map(identity_property_map()));
125 std::reverse(topo_order.begin(), topo_order.end());
127 for (typename std::vector<cg_vertex>::iterator iter = topo_order.begin();
128 iter != topo_order.end(); ++iter)
129 topo_number[*iter] = n++;
131 std::vector<std::vector<cg_vertex> > CG_vec(num_vertices(CG));
132 for (size_type i = 0; i < num_vertices(CG); ++i) {
133 typedef typename boost::graph_traits<CG_t>::adjacency_iterator cg_adj_iter;
134 std::pair<cg_adj_iter, cg_adj_iter> pr = adjacent_vertices(i, CG);
135 CG_vec[i].assign(pr.first, pr.second);
136 std::sort(CG_vec[i].begin(), CG_vec[i].end(),
137 boost::bind(std::less<cg_vertex>(),
138 boost::bind(detail::subscript(topo_number), _1),
139 boost::bind(detail::subscript(topo_number), _2)));
142 std::vector<std::vector<cg_vertex> > chains;
144 std::vector<cg_vertex> in_a_chain(CG_vec.size());
145 for (typename std::vector<cg_vertex>::iterator i = topo_order.begin();
146 i != topo_order.end(); ++i) {
148 if (!in_a_chain[v]) {
149 chains.resize(chains.size() + 1);
150 std::vector<cg_vertex>& chain = chains.back();
153 in_a_chain[v] = true;
154 typename std::vector<cg_vertex>::const_iterator next
155 = std::find_if(CG_vec[v].begin(), CG_vec[v].end(),
156 std::not1(detail::subscript(in_a_chain)));
157 if (next != CG_vec[v].end())
160 break; // end of chain, dead-end
166 std::vector<size_type> chain_number(CG_vec.size());
167 std::vector<size_type> pos_in_chain(CG_vec.size());
168 for (size_type i = 0; i < chains.size(); ++i)
169 for (size_type j = 0; j < chains[i].size(); ++j) {
170 cg_vertex v = chains[i][j];
175 cg_vertex inf = (std::numeric_limits< cg_vertex >::max)();
176 std::vector<std::vector<cg_vertex> > successors(CG_vec.size(),
177 std::vector<cg_vertex>
178 (chains.size(), inf));
179 for (typename std::vector<cg_vertex>::reverse_iterator
180 i = topo_order.rbegin(); i != topo_order.rend(); ++i) {
182 typename std::vector<cg_vertex>::const_iterator adj, adj_last;
183 for (adj = CG_vec[u].begin(), adj_last = CG_vec[u].end();
184 adj != adj_last; ++adj) {
186 if (topo_number[v] < successors[u][chain_number[v]]) {
187 // Succ(u) = Succ(u) U Succ(v)
188 detail::union_successor_sets(successors[u], successors[v],
190 // Succ(u) = Succ(u) U {v}
191 successors[u][chain_number[v]] = topo_number[v];
196 for (size_type i = 0; i < CG_vec.size(); ++i)
198 for (size_type i = 0; i < CG_vec.size(); ++i)
199 for (size_type j = 0; j < chains.size(); ++j) {
200 size_type topo_num = successors[i][j];
201 if (topo_num < inf) {
202 cg_vertex v = topo_order[topo_num];
203 for (size_type k = pos_in_chain[v]; k < chains[j].size(); ++k)
204 CG_vec[i].push_back(chains[j][k]);
209 // Add vertices to the transitive closure graph
211 vertex_iterator i, i_end;
212 for (boost::tie(i, i_end) = vertices(g); i != i_end; ++i)
213 g_to_tc_map[*i] = add_vertex(tc);
215 // Add edges between all the vertices in two adjacent SCCs
216 typename std::vector<std::vector<cg_vertex> >::const_iterator si, si_end;
217 for (si = CG_vec.begin(), si_end = CG_vec.end(); si != si_end; ++si) {
218 cg_vertex s = si - CG_vec.begin();
219 typename std::vector<cg_vertex>::const_iterator i, i_end;
220 for (i = CG_vec[s].begin(), i_end = CG_vec[s].end(); i != i_end; ++i) {
222 for (size_type k = 0; k < components[s].size(); ++k)
223 for (size_type l = 0; l < components[t].size(); ++l)
224 add_edge(g_to_tc_map[components[s][k]],
225 g_to_tc_map[components[t][l]], tc);
228 // Add edges connecting all vertices in a SCC
229 for (size_type i = 0; i < components.size(); ++i)
230 if (components[i].size() > 1)
231 for (size_type k = 0; k < components[i].size(); ++k)
232 for (size_type l = 0; l < components[i].size(); ++l) {
233 vertex u = components[i][k], v = components[i][l];
234 add_edge(g_to_tc_map[u], g_to_tc_map[v], tc);
237 // Find loopbacks in the original graph.
238 // Need to add it to transitive closure.
240 vertex_iterator i, i_end;
241 for (boost::tie(i, i_end) = vertices(g); i != i_end; ++i)
243 adjacency_iterator ab, ae;
244 for (boost::tie(ab, ae) = adjacent_vertices(*i, g); ab != ae; ++ab)
247 if (components[component_number[*i]].size() == 1)
248 add_edge(g_to_tc_map[*i], g_to_tc_map[*i], tc);
254 template <typename Graph, typename GraphTC>
255 void transitive_closure(const Graph & g, GraphTC & tc)
257 if (num_vertices(g) == 0)
259 typedef typename property_map<Graph, vertex_index_t>::const_type
261 VertexIndexMap index_map = get(vertex_index, g);
263 typedef typename graph_traits<GraphTC>::vertex_descriptor tc_vertex;
264 std::vector<tc_vertex> to_tc_vec(num_vertices(g));
265 iterator_property_map < tc_vertex *, VertexIndexMap, tc_vertex, tc_vertex&>
266 g_to_tc_map(&to_tc_vec[0], index_map);
268 transitive_closure(g, tc, g_to_tc_map, index_map);
273 template < typename Graph, typename GraphTC, typename G_to_TC_VertexMap,
274 typename VertexIndexMap>
275 void transitive_closure_dispatch
276 (const Graph & g, GraphTC & tc,
277 G_to_TC_VertexMap g_to_tc_map, VertexIndexMap index_map)
279 typedef typename graph_traits < GraphTC >::vertex_descriptor tc_vertex;
280 typename std::vector < tc_vertex >::size_type
281 n = is_default_param(g_to_tc_map) ? num_vertices(g) : 1;
282 std::vector < tc_vertex > to_tc_vec(n);
286 choose_param(g_to_tc_map, make_iterator_property_map
287 (to_tc_vec.begin(), index_map, to_tc_vec[0])),
290 } // namespace detail
292 template < typename Graph, typename GraphTC,
293 typename P, typename T, typename R >
294 void transitive_closure(const Graph & g, GraphTC & tc,
295 const bgl_named_params < P, T, R > ¶ms)
297 if (num_vertices(g) == 0)
299 detail::transitive_closure_dispatch
300 (g, tc, get_param(params, orig_to_copy_t()),
301 choose_const_pmap(get_param(params, vertex_index), g, vertex_index) );
305 template < typename G > void warshall_transitive_closure(G & g)
307 typedef typename graph_traits < G >::vertex_iterator vertex_iterator;
309 BOOST_CONCEPT_ASSERT(( AdjacencyMatrixConcept < G > ));
310 BOOST_CONCEPT_ASSERT(( EdgeMutableGraphConcept < G > ));
317 // A[i,j] = A[i,j] | A[k,j]
318 vertex_iterator ki, ke, ii, ie, ji, je;
319 for (boost::tie(ki, ke) = vertices(g); ki != ke; ++ki)
320 for (boost::tie(ii, ie) = vertices(g); ii != ie; ++ii)
321 if (edge(*ii, *ki, g).second)
322 for (boost::tie(ji, je) = vertices(g); ji != je; ++ji)
323 if (!edge(*ii, *ji, g).second && edge(*ki, *ji, g).second) {
324 add_edge(*ii, *ji, g);
329 template < typename G > void warren_transitive_closure(G & g)
331 using namespace boost;
332 typedef typename graph_traits < G >::vertex_iterator vertex_iterator;
334 BOOST_CONCEPT_ASSERT(( AdjacencyMatrixConcept < G > ));
335 BOOST_CONCEPT_ASSERT(( EdgeMutableGraphConcept < G > ));
337 // Make sure second loop will work
338 if (num_vertices(g) == 0)
342 // for k = 1 to i - 1
345 // A[i,j] = A[i,j] | A[k,j]
347 vertex_iterator ic, ie, jc, je, kc, ke;
348 for (boost::tie(ic, ie) = vertices(g), ++ic; ic != ie; ++ic)
349 for (boost::tie(kc, ke) = vertices(g); *kc != *ic; ++kc)
350 if (edge(*ic, *kc, g).second)
351 for (boost::tie(jc, je) = vertices(g); jc != je; ++jc)
352 if (!edge(*ic, *jc, g).second && edge(*kc, *jc, g).second) {
353 add_edge(*ic, *jc, g);
355 // for i = 1 to n - 1
356 // for k = i + 1 to n
359 // A[i,j] = A[i,j] | A[k,j]
361 for (boost::tie(ic, ie) = vertices(g), --ie; ic != ie; ++ic)
362 for (kc = ic, ke = ie, ++kc; kc != ke; ++kc)
363 if (edge(*ic, *kc, g).second)
364 for (boost::tie(jc, je) = vertices(g); jc != je; ++jc)
365 if (!edge(*ic, *jc, g).second && edge(*kc, *jc, g).second) {
366 add_edge(*ic, *jc, g);
373 #endif // BOOST_GRAPH_TRANSITIVE_CLOSURE_HPP