File indexing completed on 2025-01-18 09:37:38
0001
0002
0003
0004
0005
0006
0007 #ifndef BOOST_GRAPH_TRANSITIVE_REDUCTION_HPP
0008 #define BOOST_GRAPH_TRANSITIVE_REDUCTION_HPP
0009
0010 #include <vector>
0011 #include <algorithm> //std::find
0012 #include <boost/concept/requires.hpp>
0013 #include <boost/concept_check.hpp>
0014
0015 #include <boost/graph/graph_traits.hpp>
0016 #include <boost/graph/topological_sort.hpp>
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031 namespace boost
0032 {
0033
0034 template < typename Graph, typename GraphTR, typename G_to_TR_VertexMap,
0035 typename VertexIndexMap >
0036 BOOST_CONCEPT_REQUIRES(
0037 ((VertexListGraphConcept< Graph >))((IncidenceGraphConcept< Graph >))(
0038 (MutableGraphConcept< GraphTR >))(
0039 (ReadablePropertyMapConcept< VertexIndexMap,
0040 typename graph_traits< Graph >::vertex_descriptor >))(
0041 (Integer< typename property_traits< VertexIndexMap >::value_type >))(
0042 (LvaluePropertyMapConcept< G_to_TR_VertexMap,
0043 typename graph_traits< Graph >::vertex_descriptor >)),
0044 (void))
0045 transitive_reduction(const Graph& g, GraphTR& tr, G_to_TR_VertexMap g_to_tr_map,
0046 VertexIndexMap g_index_map)
0047 {
0048 typedef typename graph_traits< Graph >::vertex_descriptor Vertex;
0049 typedef typename graph_traits< Graph >::vertex_iterator VertexIterator;
0050 typedef typename std::vector< Vertex >::size_type size_type;
0051
0052 std::vector< Vertex > topo_order;
0053 topological_sort(g, std::back_inserter(topo_order));
0054
0055 std::vector< size_type > topo_number_storage(num_vertices(g));
0056
0057 iterator_property_map< size_type*, VertexIndexMap, size_type, size_type& >
0058 topo_number(&topo_number_storage[0], g_index_map);
0059
0060 {
0061 typename std::vector< Vertex >::reverse_iterator it
0062 = topo_order.rbegin();
0063 size_type n = 0;
0064 for (; it != topo_order.rend(); ++it, ++n)
0065 {
0066 topo_number[*it] = n;
0067 }
0068 }
0069
0070 std::vector< std::vector< bool > > edge_in_closure(
0071 num_vertices(g), std::vector< bool >(num_vertices(g), false));
0072 {
0073 typename std::vector< Vertex >::reverse_iterator it
0074 = topo_order.rbegin();
0075 for (; it != topo_order.rend(); ++it)
0076 {
0077 g_to_tr_map[*it] = add_vertex(tr);
0078 }
0079 }
0080
0081 typename std::vector< Vertex >::iterator it = topo_order.begin(),
0082 end = topo_order.end();
0083 for (; it != end; ++it)
0084 {
0085 size_type i = topo_number[*it];
0086 edge_in_closure[i][i] = true;
0087 std::vector< Vertex > neighbors;
0088
0089
0090
0091
0092 {
0093 typename Graph::out_edge_iterator oi, oi_end;
0094 for (boost::tie(oi, oi_end) = out_edges(*it, g); oi != oi_end; ++oi)
0095 {
0096 neighbors.push_back(target(*oi, g));
0097 }
0098 }
0099
0100 {
0101
0102 typename std::vector< Vertex >::reverse_iterator rit
0103 = topo_order.rbegin(),
0104 rend = topo_order.rend();
0105 for (; rit != rend; ++rit)
0106 {
0107
0108 if (std::find(neighbors.begin(), neighbors.end(), *rit)
0109 != neighbors.end())
0110 {
0111 size_type j = topo_number[*rit];
0112 if (not edge_in_closure[i][j])
0113 {
0114 for (size_type k = j; k < num_vertices(g); ++k)
0115 {
0116 if (not edge_in_closure[i][k])
0117 {
0118
0119
0120 edge_in_closure[i][k] = edge_in_closure[j][k];
0121 }
0122 }
0123
0124
0125 add_edge(g_to_tr_map[*it], g_to_tr_map[*rit], tr);
0126 }
0127 }
0128 }
0129 }
0130
0131 }
0132
0133 }
0134
0135 }
0136
0137 #endif