edge_connectivity.hpp 6.6 KB

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