sl@0: // Copyright (C) 2001 Vladimir Prus sl@0: // Copyright (C) 2001 Jeremy Siek sl@0: // Distributed under the Boost Software License, Version 1.0. (See sl@0: // accompanying file LICENSE_1_0.txt or copy at sl@0: // http://www.boost.org/LICENSE_1_0.txt) sl@0: sl@0: // NOTE: this final is generated by libs/graph/doc/transitive_closure.w sl@0: sl@0: #ifndef BOOST_GRAPH_TRANSITIVE_CLOSURE_HPP sl@0: #define BOOST_GRAPH_TRANSITIVE_CLOSURE_HPP sl@0: sl@0: #include sl@0: #include // for std::min and std::max sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: sl@0: namespace boost sl@0: { sl@0: sl@0: namespace detail sl@0: { sl@0: inline void sl@0: union_successor_sets(const std::vector < std::size_t > &s1, sl@0: const std::vector < std::size_t > &s2, sl@0: std::vector < std::size_t > &s3) sl@0: { sl@0: BOOST_USING_STD_MIN(); sl@0: for (std::size_t k = 0; k < s1.size(); ++k) sl@0: s3[k] = min BOOST_PREVENT_MACRO_SUBSTITUTION(s1[k], s2[k]); sl@0: } sl@0: } // namespace detail sl@0: sl@0: namespace detail sl@0: { sl@0: template < typename Container, typename ST = std::size_t, sl@0: typename VT = typename Container::value_type > sl@0: struct subscript_t:public std::unary_function < ST, VT > sl@0: { sl@0: typedef VT& result_type; sl@0: sl@0: subscript_t(Container & c):container(&c) sl@0: { sl@0: } sl@0: VT & operator() (const ST & i) const sl@0: { sl@0: return (*container)[i]; sl@0: } sl@0: protected: sl@0: Container * container; sl@0: }; sl@0: template < typename Container > sl@0: subscript_t < Container > subscript(Container & c) { sl@0: return subscript_t < Container > (c); sl@0: } sl@0: } // namespace detail sl@0: sl@0: template < typename Graph, typename GraphTC, sl@0: typename G_to_TC_VertexMap, sl@0: typename VertexIndexMap > sl@0: void transitive_closure(const Graph & g, GraphTC & tc, sl@0: G_to_TC_VertexMap g_to_tc_map, sl@0: VertexIndexMap index_map) sl@0: { sl@0: if (num_vertices(g) == 0) sl@0: return; sl@0: typedef typename graph_traits < Graph >::vertex_descriptor vertex; sl@0: typedef typename graph_traits < Graph >::edge_descriptor edge; sl@0: typedef typename graph_traits < Graph >::vertex_iterator vertex_iterator; sl@0: typedef typename property_traits < VertexIndexMap >::value_type size_type; sl@0: typedef typename graph_traits < sl@0: Graph >::adjacency_iterator adjacency_iterator; sl@0: sl@0: function_requires < VertexListGraphConcept < Graph > >(); sl@0: function_requires < AdjacencyGraphConcept < Graph > >(); sl@0: function_requires < VertexMutableGraphConcept < GraphTC > >(); sl@0: function_requires < EdgeMutableGraphConcept < GraphTC > >(); sl@0: function_requires < ReadablePropertyMapConcept < VertexIndexMap, sl@0: vertex > >(); sl@0: sl@0: typedef size_type cg_vertex; sl@0: std::vector < cg_vertex > component_number_vec(num_vertices(g)); sl@0: iterator_property_map < cg_vertex *, VertexIndexMap, cg_vertex, cg_vertex& > sl@0: component_number(&component_number_vec[0], index_map); sl@0: sl@0: int num_scc = strong_components(g, component_number, sl@0: vertex_index_map(index_map)); sl@0: sl@0: std::vector < std::vector < vertex > >components; sl@0: build_component_lists(g, num_scc, component_number, components); sl@0: sl@0: typedef std::vector > CG_t; sl@0: CG_t CG(num_scc); sl@0: for (cg_vertex s = 0; s < components.size(); ++s) { sl@0: std::vector < cg_vertex > adj; sl@0: for (size_type i = 0; i < components[s].size(); ++i) { sl@0: vertex u = components[s][i]; sl@0: adjacency_iterator v, v_end; sl@0: for (tie(v, v_end) = adjacent_vertices(u, g); v != v_end; ++v) { sl@0: cg_vertex t = component_number[*v]; sl@0: if (s != t) // Avoid loops in the condensation graph sl@0: adj.push_back(t); sl@0: } sl@0: } sl@0: std::sort(adj.begin(), adj.end()); sl@0: typename std::vector::iterator di = sl@0: std::unique(adj.begin(), adj.end()); sl@0: if (di != adj.end()) sl@0: adj.erase(di, adj.end()); sl@0: CG[s] = adj; sl@0: } sl@0: sl@0: std::vector topo_order; sl@0: std::vector topo_number(num_vertices(CG)); sl@0: topological_sort(CG, std::back_inserter(topo_order), sl@0: vertex_index_map(identity_property_map())); sl@0: std::reverse(topo_order.begin(), topo_order.end()); sl@0: size_type n = 0; sl@0: for (typename std::vector::iterator iter = topo_order.begin(); sl@0: iter != topo_order.end(); ++iter) sl@0: topo_number[*iter] = n++; sl@0: sl@0: for (size_type i = 0; i < num_vertices(CG); ++i) sl@0: std::sort(CG[i].begin(), CG[i].end(), sl@0: boost::bind(std::less(), sl@0: boost::bind(detail::subscript(topo_number), _1), sl@0: boost::bind(detail::subscript(topo_number), _2))); sl@0: sl@0: std::vector > chains; sl@0: { sl@0: std::vector in_a_chain(num_vertices(CG)); sl@0: for (typename std::vector::iterator i = topo_order.begin(); sl@0: i != topo_order.end(); ++i) { sl@0: cg_vertex v = *i; sl@0: if (!in_a_chain[v]) { sl@0: chains.resize(chains.size() + 1); sl@0: std::vector& chain = chains.back(); sl@0: for (;;) { sl@0: chain.push_back(v); sl@0: in_a_chain[v] = true; sl@0: typename graph_traits::adjacency_iterator adj_first, adj_last; sl@0: tie(adj_first, adj_last) = adjacent_vertices(v, CG); sl@0: typename graph_traits::adjacency_iterator next sl@0: = std::find_if(adj_first, adj_last, sl@0: std::not1(detail::subscript(in_a_chain))); sl@0: if (next != adj_last) sl@0: v = *next; sl@0: else sl@0: break; // end of chain, dead-end sl@0: sl@0: } sl@0: } sl@0: } sl@0: } sl@0: std::vector chain_number(num_vertices(CG)); sl@0: std::vector pos_in_chain(num_vertices(CG)); sl@0: for (size_type i = 0; i < chains.size(); ++i) sl@0: for (size_type j = 0; j < chains[i].size(); ++j) { sl@0: cg_vertex v = chains[i][j]; sl@0: chain_number[v] = i; sl@0: pos_in_chain[v] = j; sl@0: } sl@0: sl@0: cg_vertex inf = (std::numeric_limits< cg_vertex >::max)(); sl@0: std::vector > successors(num_vertices(CG), sl@0: std::vector sl@0: (chains.size(), inf)); sl@0: for (typename std::vector::reverse_iterator sl@0: i = topo_order.rbegin(); i != topo_order.rend(); ++i) { sl@0: cg_vertex u = *i; sl@0: typename graph_traits::adjacency_iterator adj, adj_last; sl@0: for (tie(adj, adj_last) = adjacent_vertices(u, CG); sl@0: adj != adj_last; ++adj) { sl@0: cg_vertex v = *adj; sl@0: if (topo_number[v] < successors[u][chain_number[v]]) { sl@0: // Succ(u) = Succ(u) U Succ(v) sl@0: detail::union_successor_sets(successors[u], successors[v], sl@0: successors[u]); sl@0: // Succ(u) = Succ(u) U {v} sl@0: successors[u][chain_number[v]] = topo_number[v]; sl@0: } sl@0: } sl@0: } sl@0: sl@0: for (size_type i = 0; i < CG.size(); ++i) sl@0: CG[i].clear(); sl@0: for (size_type i = 0; i < CG.size(); ++i) sl@0: for (size_type j = 0; j < chains.size(); ++j) { sl@0: size_type topo_num = successors[i][j]; sl@0: if (topo_num < inf) { sl@0: cg_vertex v = topo_order[topo_num]; sl@0: for (size_type k = pos_in_chain[v]; k < chains[j].size(); ++k) sl@0: CG[i].push_back(chains[j][k]); sl@0: } sl@0: } sl@0: sl@0: sl@0: // Add vertices to the transitive closure graph sl@0: typedef typename graph_traits < GraphTC >::vertex_descriptor tc_vertex; sl@0: { sl@0: vertex_iterator i, i_end; sl@0: for (tie(i, i_end) = vertices(g); i != i_end; ++i) sl@0: g_to_tc_map[*i] = add_vertex(tc); sl@0: } sl@0: // Add edges between all the vertices in two adjacent SCCs sl@0: typename graph_traits::vertex_iterator si, si_end; sl@0: for (tie(si, si_end) = vertices(CG); si != si_end; ++si) { sl@0: cg_vertex s = *si; sl@0: typename graph_traits::adjacency_iterator i, i_end; sl@0: for (tie(i, i_end) = adjacent_vertices(s, CG); i != i_end; ++i) { sl@0: cg_vertex t = *i; sl@0: for (size_type k = 0; k < components[s].size(); ++k) sl@0: for (size_type l = 0; l < components[t].size(); ++l) sl@0: add_edge(g_to_tc_map[components[s][k]], sl@0: g_to_tc_map[components[t][l]], tc); sl@0: } sl@0: } sl@0: // Add edges connecting all vertices in a SCC sl@0: for (size_type i = 0; i < components.size(); ++i) sl@0: if (components[i].size() > 1) sl@0: for (size_type k = 0; k < components[i].size(); ++k) sl@0: for (size_type l = 0; l < components[i].size(); ++l) { sl@0: vertex u = components[i][k], v = components[i][l]; sl@0: add_edge(g_to_tc_map[u], g_to_tc_map[v], tc); sl@0: } sl@0: sl@0: // Find loopbacks in the original graph. sl@0: // Need to add it to transitive closure. sl@0: { sl@0: vertex_iterator i, i_end; sl@0: for (tie(i, i_end) = vertices(g); i != i_end; ++i) sl@0: { sl@0: adjacency_iterator ab, ae; sl@0: for (boost::tie(ab, ae) = adjacent_vertices(*i, g); ab != ae; ++ab) sl@0: { sl@0: if (*ab == *i) sl@0: if (components[component_number[*i]].size() == 1) sl@0: add_edge(g_to_tc_map[*i], g_to_tc_map[*i], tc); sl@0: } sl@0: } sl@0: } sl@0: } sl@0: sl@0: template sl@0: void transitive_closure(const Graph & g, GraphTC & tc) sl@0: { sl@0: if (num_vertices(g) == 0) sl@0: return; sl@0: typedef typename property_map::const_type sl@0: VertexIndexMap; sl@0: VertexIndexMap index_map = get(vertex_index, g); sl@0: sl@0: typedef typename graph_traits::vertex_descriptor tc_vertex; sl@0: std::vector to_tc_vec(num_vertices(g)); sl@0: iterator_property_map < tc_vertex *, VertexIndexMap, tc_vertex, tc_vertex&> sl@0: g_to_tc_map(&to_tc_vec[0], index_map); sl@0: sl@0: transitive_closure(g, tc, g_to_tc_map, index_map); sl@0: } sl@0: sl@0: namespace detail sl@0: { sl@0: template < typename Graph, typename GraphTC, typename G_to_TC_VertexMap, sl@0: typename VertexIndexMap> sl@0: void transitive_closure_dispatch sl@0: (const Graph & g, GraphTC & tc, sl@0: G_to_TC_VertexMap g_to_tc_map, VertexIndexMap index_map) sl@0: { sl@0: typedef typename graph_traits < GraphTC >::vertex_descriptor tc_vertex; sl@0: typename std::vector < tc_vertex >::size_type sl@0: n = is_default_param(g_to_tc_map) ? num_vertices(g) : 1; sl@0: std::vector < tc_vertex > to_tc_vec(n); sl@0: sl@0: transitive_closure sl@0: (g, tc, sl@0: choose_param(g_to_tc_map, make_iterator_property_map sl@0: (to_tc_vec.begin(), index_map, to_tc_vec[0])), sl@0: index_map); sl@0: } sl@0: } // namespace detail sl@0: sl@0: template < typename Graph, typename GraphTC, sl@0: typename P, typename T, typename R > sl@0: void transitive_closure(const Graph & g, GraphTC & tc, sl@0: const bgl_named_params < P, T, R > ¶ms) sl@0: { sl@0: if (num_vertices(g) == 0) sl@0: return; sl@0: detail::transitive_closure_dispatch sl@0: (g, tc, get_param(params, orig_to_copy_t()), sl@0: choose_const_pmap(get_param(params, vertex_index), g, vertex_index) ); sl@0: } sl@0: sl@0: sl@0: template < typename G > void warshall_transitive_closure(G & g) sl@0: { sl@0: typedef typename graph_traits < G >::vertex_descriptor vertex; sl@0: typedef typename graph_traits < G >::vertex_iterator vertex_iterator; sl@0: sl@0: function_requires < AdjacencyMatrixConcept < G > >(); sl@0: function_requires < EdgeMutableGraphConcept < G > >(); sl@0: sl@0: // Matrix form: sl@0: // for k sl@0: // for i sl@0: // if A[i,k] sl@0: // for j sl@0: // A[i,j] = A[i,j] | A[k,j] sl@0: vertex_iterator ki, ke, ii, ie, ji, je; sl@0: for (tie(ki, ke) = vertices(g); ki != ke; ++ki) sl@0: for (tie(ii, ie) = vertices(g); ii != ie; ++ii) sl@0: if (edge(*ii, *ki, g).second) sl@0: for (tie(ji, je) = vertices(g); ji != je; ++ji) sl@0: if (!edge(*ii, *ji, g).second && edge(*ki, *ji, g).second) { sl@0: add_edge(*ii, *ji, g); sl@0: } sl@0: } sl@0: sl@0: sl@0: template < typename G > void warren_transitive_closure(G & g) sl@0: { sl@0: using namespace boost; sl@0: typedef typename graph_traits < G >::vertex_descriptor vertex; sl@0: typedef typename graph_traits < G >::vertex_iterator vertex_iterator; sl@0: sl@0: function_requires < AdjacencyMatrixConcept < G > >(); sl@0: function_requires < EdgeMutableGraphConcept < G > >(); sl@0: sl@0: // Make sure second loop will work sl@0: if (num_vertices(g) == 0) sl@0: return; sl@0: sl@0: // for i = 2 to n sl@0: // for k = 1 to i - 1 sl@0: // if A[i,k] sl@0: // for j = 1 to n sl@0: // A[i,j] = A[i,j] | A[k,j] sl@0: sl@0: vertex_iterator ic, ie, jc, je, kc, ke; sl@0: for (tie(ic, ie) = vertices(g), ++ic; ic != ie; ++ic) sl@0: for (tie(kc, ke) = vertices(g); *kc != *ic; ++kc) sl@0: if (edge(*ic, *kc, g).second) sl@0: for (tie(jc, je) = vertices(g); jc != je; ++jc) sl@0: if (!edge(*ic, *jc, g).second && edge(*kc, *jc, g).second) { sl@0: add_edge(*ic, *jc, g); sl@0: } sl@0: // for i = 1 to n - 1 sl@0: // for k = i + 1 to n sl@0: // if A[i,k] sl@0: // for j = 1 to n sl@0: // A[i,j] = A[i,j] | A[k,j] sl@0: sl@0: for (tie(ic, ie) = vertices(g), --ie; ic != ie; ++ic) sl@0: for (kc = ic, ke = ie, ++kc; kc != ke; ++kc) sl@0: if (edge(*ic, *kc, g).second) sl@0: for (tie(jc, je) = vertices(g); jc != je; ++jc) sl@0: if (!edge(*ic, *jc, g).second && edge(*kc, *jc, g).second) { sl@0: add_edge(*ic, *jc, g); sl@0: } sl@0: } sl@0: sl@0: sl@0: } // namespace boost sl@0: sl@0: #endif // BOOST_GRAPH_TRANSITIVE_CLOSURE_HPP