edge_connectivity.hpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. //=======================================================================
  2. // Copyright 2000 University of Notre Dame.
  3. // Authors: Jeremy G. Siek, Andrew Lumsdaine, Lie-Quan Lee
  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_EDGE_CONNECTIVITY
  10. #define BOOST_EDGE_CONNECTIVITY
  11. // WARNING: not-yet fully tested!
  12. #include <boost/config.hpp>
  13. #include <vector>
  14. #include <set>
  15. #include <algorithm>
  16. #include <boost/graph/adjacency_list.hpp>
  17. #include <boost/graph/edmonds_karp_max_flow.hpp>
  18. namespace boost
  19. {
  20. namespace detail
  21. {
  22. template < class Graph >
  23. inline std::pair< typename graph_traits< Graph >::vertex_descriptor,
  24. typename graph_traits< Graph >::degree_size_type >
  25. min_degree_vertex(Graph& g)
  26. {
  27. typedef graph_traits< Graph > Traits;
  28. typename Traits::vertex_descriptor p;
  29. typedef typename Traits::degree_size_type size_type;
  30. size_type delta = (std::numeric_limits< size_type >::max)();
  31. typename Traits::vertex_iterator i, iend;
  32. for (boost::tie(i, iend) = vertices(g); i != iend; ++i)
  33. if (degree(*i, g) < delta)
  34. {
  35. delta = degree(*i, g);
  36. p = *i;
  37. }
  38. return std::make_pair(p, delta);
  39. }
  40. template < class Graph, class OutputIterator >
  41. void neighbors(const Graph& g,
  42. typename graph_traits< Graph >::vertex_descriptor u,
  43. OutputIterator result)
  44. {
  45. typename graph_traits< Graph >::adjacency_iterator ai, aend;
  46. for (boost::tie(ai, aend) = adjacent_vertices(u, g); ai != aend; ++ai)
  47. *result++ = *ai;
  48. }
  49. template < class Graph, class VertexIterator, class OutputIterator >
  50. void neighbors(const Graph& g, VertexIterator first, VertexIterator last,
  51. OutputIterator result)
  52. {
  53. for (; first != last; ++first)
  54. neighbors(g, *first, result);
  55. }
  56. } // namespace detail
  57. // O(m n)
  58. template < class VertexListGraph, class OutputIterator >
  59. typename graph_traits< VertexListGraph >::degree_size_type edge_connectivity(
  60. VertexListGraph& g, OutputIterator disconnecting_set)
  61. {
  62. //-------------------------------------------------------------------------
  63. // Type Definitions
  64. typedef graph_traits< VertexListGraph > Traits;
  65. typedef typename Traits::vertex_iterator vertex_iterator;
  66. typedef typename Traits::edge_iterator edge_iterator;
  67. typedef typename Traits::out_edge_iterator out_edge_iterator;
  68. typedef typename Traits::vertex_descriptor vertex_descriptor;
  69. typedef typename Traits::degree_size_type degree_size_type;
  70. typedef color_traits< default_color_type > Color;
  71. typedef adjacency_list_traits< vecS, vecS, directedS > Tr;
  72. typedef typename Tr::edge_descriptor Tr_edge_desc;
  73. typedef adjacency_list< vecS, vecS, directedS, no_property,
  74. property< edge_capacity_t, degree_size_type,
  75. property< edge_residual_capacity_t, degree_size_type,
  76. property< edge_reverse_t, Tr_edge_desc > > > >
  77. FlowGraph;
  78. typedef typename graph_traits< FlowGraph >::edge_descriptor edge_descriptor;
  79. //-------------------------------------------------------------------------
  80. // Variable Declarations
  81. vertex_descriptor u, v, p, k;
  82. edge_descriptor e1, e2;
  83. bool inserted;
  84. vertex_iterator vi, vi_end;
  85. edge_iterator ei, ei_end;
  86. degree_size_type delta, alpha_star, alpha_S_k;
  87. std::set< vertex_descriptor > S, neighbor_S;
  88. std::vector< vertex_descriptor > S_star, non_neighbor_S;
  89. std::vector< default_color_type > color(num_vertices(g));
  90. std::vector< edge_descriptor > pred(num_vertices(g));
  91. //-------------------------------------------------------------------------
  92. // Create a network flow graph out of the undirected graph
  93. FlowGraph flow_g(num_vertices(g));
  94. typename property_map< FlowGraph, edge_capacity_t >::type cap
  95. = get(edge_capacity, flow_g);
  96. typename property_map< FlowGraph, edge_residual_capacity_t >::type res_cap
  97. = get(edge_residual_capacity, flow_g);
  98. typename property_map< FlowGraph, edge_reverse_t >::type rev_edge
  99. = get(edge_reverse, flow_g);
  100. for (boost::tie(ei, ei_end) = edges(g); ei != ei_end; ++ei)
  101. {
  102. u = source(*ei, g), v = target(*ei, g);
  103. boost::tie(e1, inserted) = add_edge(u, v, flow_g);
  104. cap[e1] = 1;
  105. boost::tie(e2, inserted) = add_edge(v, u, flow_g);
  106. cap[e2] = 1; // not sure about this
  107. rev_edge[e1] = e2;
  108. rev_edge[e2] = e1;
  109. }
  110. //-------------------------------------------------------------------------
  111. // The Algorithm
  112. boost::tie(p, delta) = detail::min_degree_vertex(g);
  113. S_star.push_back(p);
  114. alpha_star = delta;
  115. S.insert(p);
  116. neighbor_S.insert(p);
  117. detail::neighbors(
  118. g, S.begin(), S.end(), std::inserter(neighbor_S, neighbor_S.begin()));
  119. boost::tie(vi, vi_end) = vertices(g);
  120. std::set_difference(vi, vi_end, neighbor_S.begin(), neighbor_S.end(),
  121. std::back_inserter(non_neighbor_S));
  122. while (!non_neighbor_S.empty())
  123. { // at most n - 1 times
  124. k = non_neighbor_S.front();
  125. alpha_S_k = edmonds_karp_max_flow(
  126. flow_g, p, k, cap, res_cap, rev_edge, &color[0], &pred[0]);
  127. if (alpha_S_k < alpha_star)
  128. {
  129. alpha_star = alpha_S_k;
  130. S_star.clear();
  131. for (boost::tie(vi, vi_end) = vertices(flow_g); vi != vi_end; ++vi)
  132. if (color[*vi] != Color::white())
  133. S_star.push_back(*vi);
  134. }
  135. S.insert(k);
  136. neighbor_S.insert(k);
  137. detail::neighbors(g, k, std::inserter(neighbor_S, neighbor_S.begin()));
  138. non_neighbor_S.clear();
  139. boost::tie(vi, vi_end) = vertices(g);
  140. std::set_difference(vi, vi_end, neighbor_S.begin(), neighbor_S.end(),
  141. std::back_inserter(non_neighbor_S));
  142. }
  143. //-------------------------------------------------------------------------
  144. // Compute edges of the cut [S*, ~S*]
  145. std::vector< bool > in_S_star(num_vertices(g), false);
  146. typename std::vector< vertex_descriptor >::iterator si;
  147. for (si = S_star.begin(); si != S_star.end(); ++si)
  148. in_S_star[*si] = true;
  149. degree_size_type c = 0;
  150. for (si = S_star.begin(); si != S_star.end(); ++si)
  151. {
  152. out_edge_iterator ei, ei_end;
  153. for (boost::tie(ei, ei_end) = out_edges(*si, g); ei != ei_end; ++ei)
  154. if (!in_S_star[target(*ei, g)])
  155. {
  156. *disconnecting_set++ = *ei;
  157. ++c;
  158. }
  159. }
  160. return c;
  161. }
  162. } // namespace boost
  163. #endif // BOOST_EDGE_CONNECTIVITY