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