transitive_reduction.hpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. {
  27. template < typename Graph, typename GraphTR, typename G_to_TR_VertexMap,
  28. typename VertexIndexMap >
  29. BOOST_CONCEPT_REQUIRES(
  30. ((VertexListGraphConcept< Graph >))((IncidenceGraphConcept< Graph >))(
  31. (MutableGraphConcept< GraphTR >))(
  32. (ReadablePropertyMapConcept< VertexIndexMap,
  33. typename graph_traits< Graph >::vertex_descriptor >))(
  34. (Integer< typename property_traits< VertexIndexMap >::value_type >))(
  35. (LvaluePropertyMapConcept< G_to_TR_VertexMap,
  36. typename graph_traits< Graph >::vertex_descriptor >)),
  37. (void))
  38. transitive_reduction(const Graph& g, GraphTR& tr, G_to_TR_VertexMap g_to_tr_map,
  39. VertexIndexMap g_index_map)
  40. {
  41. typedef typename graph_traits< Graph >::vertex_descriptor Vertex;
  42. typedef typename graph_traits< Graph >::vertex_iterator VertexIterator;
  43. typedef typename std::vector< Vertex >::size_type size_type;
  44. std::vector< Vertex > topo_order;
  45. topological_sort(g, std::back_inserter(topo_order));
  46. std::vector< size_type > topo_number_storage(num_vertices(g));
  47. iterator_property_map< size_type*, VertexIndexMap, size_type, size_type& >
  48. topo_number(&topo_number_storage[0], g_index_map);
  49. {
  50. typename std::vector< Vertex >::reverse_iterator it
  51. = topo_order.rbegin();
  52. size_type n = 0;
  53. for (; it != topo_order.rend(); ++it, ++n)
  54. {
  55. topo_number[*it] = n;
  56. }
  57. }
  58. std::vector< std::vector< bool > > edge_in_closure(
  59. num_vertices(g), std::vector< bool >(num_vertices(g), false));
  60. {
  61. typename std::vector< Vertex >::reverse_iterator it
  62. = topo_order.rbegin();
  63. for (; it != topo_order.rend(); ++it)
  64. {
  65. g_to_tr_map[*it] = add_vertex(tr);
  66. }
  67. }
  68. typename std::vector< Vertex >::iterator it = topo_order.begin(),
  69. end = topo_order.end();
  70. for (; it != end; ++it)
  71. {
  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. {
  82. neighbors.push_back(target(*oi, g));
  83. }
  84. }
  85. {
  86. // and run through all vertices in topological order
  87. typename std::vector< Vertex >::reverse_iterator rit
  88. = topo_order.rbegin(),
  89. rend = topo_order.rend();
  90. for (; rit != rend; ++rit)
  91. {
  92. // looking if they are successors of *it
  93. if (std::find(neighbors.begin(), neighbors.end(), *rit)
  94. != neighbors.end())
  95. {
  96. size_type j = topo_number[*rit];
  97. if (not edge_in_closure[i][j])
  98. {
  99. for (size_type k = j; k < num_vertices(g); ++k)
  100. {
  101. if (not edge_in_closure[i][k])
  102. {
  103. // here we need edge_in_closure to be in
  104. // topological order,
  105. edge_in_closure[i][k] = edge_in_closure[j][k];
  106. }
  107. }
  108. // therefore we only access edge_in_closure only through
  109. // topo_number property_map
  110. add_edge(g_to_tr_map[*it], g_to_tr_map[*rit], tr);
  111. } // if ( not edge_in_
  112. } // if (find (
  113. } // for( typename vector<Vertex>::reverse_iterator
  114. } // {
  115. } // for( typename vector<Vertex>::iterator
  116. } // void transitive_reduction
  117. } // namespace boost
  118. #endif