create_condensation_graph.hpp 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. //=======================================================================
  2. // Copyright 2002 Indiana University.
  3. // Authors: Andrew Lumsdaine, Lie-Quan Lee, Jeremy G. Siek
  4. //
  5. // Distributed under the Boost Software License, Version 1.0. (See
  6. // accompanying file LICENSE_1_0.txt or copy at
  7. // http://www.boost.org/LICENSE_1_0.txt)
  8. //=======================================================================
  9. #ifndef BOOST_CREATE_CONDENSATION_GRAPH_HPP
  10. #define BOOST_CREATE_CONDENSATION_GRAPH_HPP
  11. #include <boost/graph/graph_traits.hpp>
  12. #include <boost/property_map/property_map.hpp>
  13. namespace boost
  14. {
  15. template < typename Graph, typename ComponentLists, typename ComponentNumberMap,
  16. typename CondensationGraph, typename EdgeMultiplicityMap >
  17. void create_condensation_graph(const Graph& g, const ComponentLists& components,
  18. ComponentNumberMap component_number, CondensationGraph& cg,
  19. EdgeMultiplicityMap edge_mult_map)
  20. {
  21. typedef typename graph_traits< Graph >::vertex_descriptor vertex;
  22. typedef typename graph_traits< Graph >::vertices_size_type size_type;
  23. typedef
  24. typename graph_traits< CondensationGraph >::vertex_descriptor cg_vertex;
  25. std::vector< cg_vertex > to_cg_vertex(components.size());
  26. for (size_type s = 0; s < components.size(); ++s)
  27. to_cg_vertex[s] = add_vertex(cg);
  28. for (size_type si = 0; si < components.size(); ++si)
  29. {
  30. cg_vertex s = to_cg_vertex[si];
  31. std::vector< cg_vertex > adj;
  32. for (size_type i = 0; i < components[si].size(); ++i)
  33. {
  34. vertex u = components[s][i];
  35. typename graph_traits< Graph >::adjacency_iterator v, v_end;
  36. for (boost::tie(v, v_end) = adjacent_vertices(u, g); v != v_end;
  37. ++v)
  38. {
  39. cg_vertex t = to_cg_vertex[component_number[*v]];
  40. if (s != t) // Avoid loops in the condensation graph
  41. adj.push_back(t);
  42. }
  43. }
  44. std::sort(adj.begin(), adj.end());
  45. if (!adj.empty())
  46. {
  47. size_type i = 0;
  48. cg_vertex t = adj[i];
  49. typename graph_traits< CondensationGraph >::edge_descriptor e;
  50. bool inserted;
  51. boost::tie(e, inserted) = add_edge(s, t, cg);
  52. put(edge_mult_map, e, 1);
  53. ++i;
  54. while (i < adj.size())
  55. {
  56. if (adj[i] == t)
  57. put(edge_mult_map, e, get(edge_mult_map, e) + 1);
  58. else
  59. {
  60. t = adj[i];
  61. boost::tie(e, inserted) = add_edge(s, t, cg);
  62. put(edge_mult_map, e, 1);
  63. }
  64. ++i;
  65. }
  66. }
  67. }
  68. }
  69. template < typename Graph, typename ComponentLists, typename ComponentNumberMap,
  70. typename CondensationGraph >
  71. void create_condensation_graph(const Graph& g, const ComponentLists& components,
  72. ComponentNumberMap component_number, CondensationGraph& cg)
  73. {
  74. create_condensation_graph(
  75. g, components, component_number, cg, dummy_property_map());
  76. }
  77. } // namespace boost
  78. #endif // BOOST_CREATE_CONDENSATION_GRAPH_HPP