create_condensation_graph.hpp 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. template <typename Graph, typename ComponentLists,
  15. typename ComponentNumberMap,
  16. typename CondensationGraph, typename EdgeMultiplicityMap>
  17. void create_condensation_graph(const Graph& g,
  18. const ComponentLists& components,
  19. ComponentNumberMap component_number,
  20. CondensationGraph& cg,
  21. EdgeMultiplicityMap edge_mult_map)
  22. {
  23. typedef typename graph_traits<Graph>::vertex_descriptor vertex;
  24. typedef typename graph_traits<Graph>::vertices_size_type size_type;
  25. typedef typename graph_traits<CondensationGraph>::vertex_descriptor
  26. cg_vertex;
  27. std::vector<cg_vertex> to_cg_vertex(components.size());
  28. for (size_type s = 0; s < components.size(); ++s)
  29. to_cg_vertex[s] = add_vertex(cg);
  30. for (size_type si = 0; si < components.size(); ++si) {
  31. cg_vertex s = to_cg_vertex[si];
  32. std::vector<cg_vertex> adj;
  33. for (size_type i = 0; i < components[si].size(); ++i) {
  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; ++v) {
  37. cg_vertex t = to_cg_vertex[component_number[*v]];
  38. if (s != t) // Avoid loops in the condensation graph
  39. adj.push_back(t);
  40. }
  41. }
  42. std::sort(adj.begin(), adj.end());
  43. if (! adj.empty()) {
  44. size_type i = 0;
  45. cg_vertex t = adj[i];
  46. typename graph_traits<CondensationGraph>::edge_descriptor e;
  47. bool inserted;
  48. boost::tie(e, inserted) = add_edge(s, t, cg);
  49. put(edge_mult_map, e, 1);
  50. ++i;
  51. while (i < adj.size()) {
  52. if (adj[i] == t)
  53. put(edge_mult_map, e, get(edge_mult_map, e) + 1);
  54. else {
  55. t = adj[i];
  56. boost::tie(e, inserted) = add_edge(s, t, cg);
  57. put(edge_mult_map, e, 1);
  58. }
  59. ++i;
  60. }
  61. }
  62. }
  63. }
  64. template <typename Graph, typename ComponentLists,
  65. typename ComponentNumberMap, typename CondensationGraph>
  66. void create_condensation_graph(const Graph& g,
  67. const ComponentLists& components,
  68. ComponentNumberMap component_number,
  69. CondensationGraph& cg)
  70. {
  71. create_condensation_graph(g, components, component_number, cg,
  72. dummy_property_map());
  73. }
  74. } // namespace boost
  75. #endif // BOOST_CREATE_CONDENSATION_GRAPH_HPP