transitive_reduction.hpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. // (C) Copyright 2009 Eric Bose-Wolf
  2. //
  3. // Use, modification and distribution are subject to the
  4. // Boost Software License, Version 1.0 (See accompanying file
  5. // LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt)
  6. #ifndef BOOST_GRAPH_TRANSITIVE_REDUCTION_HPP
  7. #define BOOST_GRAPH_TRANSITIVE_REDUCTION_HPP
  8. #include <vector>
  9. #include <algorithm> //std::find
  10. #include <boost/concept/requires.hpp>
  11. #include <boost/concept_check.hpp>
  12. #include <boost/graph/graph_traits.hpp>
  13. #include <boost/graph/topological_sort.hpp>
  14. // also I didn't got all of the concepts thin. Am I suppose to check
  15. // for all concepts, which are needed for functions I call? (As if I
  16. // wouldn't do that, the users would see the functions called by
  17. // complaining about missings concepts, which would be clearly an error
  18. // message revealing internal implementation and should therefore be avoided?)
  19. // the pseudocode which I followed implementing this algorithmn was taken
  20. // from the german book Algorithmische Graphentheorie by Volker Turau
  21. // it is proposed to be of O(n + nm_red ) where n is the number
  22. // of vertices and m_red is the number of edges in the transitive
  23. // reduction, but I think my implementation spoiled this up at some point
  24. // indicated below.
  25. namespace boost {
  26. template <
  27. typename Graph, typename GraphTR, typename G_to_TR_VertexMap,
  28. typename VertexIndexMap
  29. >
  30. BOOST_CONCEPT_REQUIRES(
  31. ((VertexListGraphConcept< Graph >))
  32. ((IncidenceGraphConcept< Graph >))
  33. ((MutableGraphConcept< GraphTR >))
  34. ((ReadablePropertyMapConcept< VertexIndexMap,
  35. typename graph_traits<Graph>::vertex_descriptor >))
  36. ((Integer< typename
  37. property_traits< VertexIndexMap >::value_type >))
  38. ((LvaluePropertyMapConcept< G_to_TR_VertexMap,
  39. typename graph_traits<Graph>::vertex_descriptor >)),
  40. (void))
  41. transitive_reduction(const Graph& g, GraphTR& tr,
  42. G_to_TR_VertexMap g_to_tr_map,
  43. VertexIndexMap g_index_map )
  44. {
  45. typedef typename graph_traits<Graph>::vertex_descriptor Vertex;
  46. typedef typename graph_traits<Graph>::vertex_iterator VertexIterator;
  47. typedef typename std::vector<Vertex>::size_type size_type;
  48. std::vector<Vertex> topo_order;
  49. topological_sort(g, std::back_inserter(topo_order));
  50. std::vector<size_type> topo_number_storage(num_vertices(g));
  51. iterator_property_map<size_type*, VertexIndexMap,
  52. size_type, size_type&> topo_number( &topo_number_storage[0], g_index_map );
  53. {
  54. typename std::vector<Vertex>::reverse_iterator it = topo_order.rbegin();
  55. size_type n = 0;
  56. for(; it != topo_order.rend(); ++it,++n ) {
  57. topo_number[ *it ] = n;
  58. }
  59. }
  60. std::vector< std::vector< bool > > edge_in_closure(num_vertices(g),
  61. std::vector<bool>( num_vertices(g), false));
  62. {
  63. typename std::vector<Vertex>::reverse_iterator it = topo_order.rbegin();
  64. for( ; it != topo_order.rend(); ++it ) {
  65. g_to_tr_map[*it] = add_vertex(tr);
  66. }
  67. }
  68. typename std::vector<Vertex>::iterator
  69. it = topo_order.begin(),
  70. end = topo_order.end();
  71. for( ; it != end; ++it ) {
  72. size_type i = topo_number[ *it ];
  73. edge_in_closure[i][i] = true;
  74. std::vector<Vertex> neighbors;
  75. //I have to collect the successors of *it and traverse them in
  76. //ascending topological order. I didn't know a better way, how to
  77. //do that. So what I'm doint is, collection the successors of *it here
  78. {
  79. typename Graph::out_edge_iterator oi,oi_end;
  80. for( boost::tie(oi, oi_end) = out_edges( *it, g ); oi != oi_end; ++oi ) {
  81. neighbors.push_back( target( *oi, g ) );
  82. }
  83. }
  84. {
  85. //and run through all vertices in topological order
  86. typename std::vector<Vertex>::reverse_iterator
  87. rit = topo_order.rbegin(),
  88. rend = topo_order.rend();
  89. for(; rit != rend; ++rit ) {
  90. //looking if they are successors of *it
  91. if( std::find( neighbors.begin(), neighbors.end(), *rit) != neighbors.end() ) {
  92. size_type j = topo_number[ *rit ];
  93. if( not edge_in_closure[i][j] ) {
  94. for(size_type k = j; k < num_vertices(g); ++k) {
  95. if( not edge_in_closure[i][k] ) {
  96. //here we need edge_in_closure to be in topological order,
  97. edge_in_closure[i][k] = edge_in_closure[j][k];
  98. }
  99. }
  100. //therefore we only access edge_in_closure only through
  101. //topo_number property_map
  102. add_edge(g_to_tr_map[*it], g_to_tr_map[*rit], tr);
  103. } //if ( not edge_in_
  104. } //if (find (
  105. } //for( typename vector<Vertex>::reverse_iterator
  106. } // {
  107. } //for( typename vector<Vertex>::iterator
  108. } //void transitive_reduction
  109. } // namespace boost
  110. #endif