Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:37:38

0001 // (C) Copyright 2009 Eric Bose-Wolf
0002 //
0003 // Use, modification and distribution are subject to the
0004 // Boost Software License, Version 1.0 (See accompanying file
0005 // LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt)
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 // also I didn't got all of the concepts thin. Am I suppose to check
0019 // for all concepts, which are needed for functions I call? (As if I
0020 // wouldn't do that, the users would see the functions called by
0021 // complaining about missings concepts, which would be clearly an error
0022 // message revealing internal implementation and should therefore be avoided?)
0023 
0024 // the pseudocode which I followed implementing this algorithmn was taken
0025 // from the german book Algorithmische Graphentheorie by Volker Turau
0026 // it is proposed to be of O(n + nm_red ) where n is the number
0027 // of vertices and m_red is the number of edges in the transitive
0028 // reduction, but I think my implementation spoiled this up at some point
0029 // indicated below.
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         // I have to collect the successors of *it and traverse them in
0090         // ascending topological order. I didn't know a better way, how to
0091         // do that. So what I'm doint is, collection the successors of *it here
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             // and run through all vertices in topological order
0102             typename std::vector< Vertex >::reverse_iterator rit
0103                 = topo_order.rbegin(),
0104                 rend = topo_order.rend();
0105             for (; rit != rend; ++rit)
0106             {
0107                 // looking if they are successors of *it
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                                 // here we need edge_in_closure to be in
0119                                 // topological order,
0120                                 edge_in_closure[i][k] = edge_in_closure[j][k];
0121                             }
0122                         }
0123                         // therefore we only access edge_in_closure only through
0124                         // topo_number property_map
0125                         add_edge(g_to_tr_map[*it], g_to_tr_map[*rit], tr);
0126                     } // if ( not edge_in_
0127                 } // if (find (
0128             } // for( typename vector<Vertex>::reverse_iterator
0129         } // {
0130 
0131     } // for( typename vector<Vertex>::iterator
0132 
0133 } // void transitive_reduction
0134 
0135 } // namespace boost
0136 
0137 #endif