os/ossrv/ossrv_pub/boost_apis/boost/graph/transitive_closure.hpp
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
sl@0
     1
// Copyright (C) 2001 Vladimir Prus <ghost@cs.msu.su>
sl@0
     2
// Copyright (C) 2001 Jeremy Siek <jsiek@cs.indiana.edu>
sl@0
     3
// Distributed under the Boost Software License, Version 1.0. (See
sl@0
     4
// accompanying file LICENSE_1_0.txt or copy at
sl@0
     5
// http://www.boost.org/LICENSE_1_0.txt)
sl@0
     6
sl@0
     7
// NOTE: this final is generated by libs/graph/doc/transitive_closure.w
sl@0
     8
sl@0
     9
#ifndef BOOST_GRAPH_TRANSITIVE_CLOSURE_HPP
sl@0
    10
#define BOOST_GRAPH_TRANSITIVE_CLOSURE_HPP
sl@0
    11
sl@0
    12
#include <vector>
sl@0
    13
#include <algorithm> // for std::min and std::max
sl@0
    14
#include <functional>
sl@0
    15
#include <boost/config.hpp>
sl@0
    16
#include <boost/bind.hpp>
sl@0
    17
#include <boost/graph/vector_as_graph.hpp>
sl@0
    18
#include <boost/graph/strong_components.hpp>
sl@0
    19
#include <boost/graph/topological_sort.hpp>
sl@0
    20
#include <boost/graph/graph_concepts.hpp>
sl@0
    21
#include <boost/graph/named_function_params.hpp>
sl@0
    22
sl@0
    23
namespace boost
sl@0
    24
{
sl@0
    25
sl@0
    26
  namespace detail
sl@0
    27
  {
sl@0
    28
    inline void
sl@0
    29
      union_successor_sets(const std::vector < std::size_t > &s1,
sl@0
    30
                           const std::vector < std::size_t > &s2,
sl@0
    31
                           std::vector < std::size_t > &s3)
sl@0
    32
    {
sl@0
    33
      BOOST_USING_STD_MIN();
sl@0
    34
      for (std::size_t k = 0; k < s1.size(); ++k)
sl@0
    35
        s3[k] = min BOOST_PREVENT_MACRO_SUBSTITUTION(s1[k], s2[k]);
sl@0
    36
    }
sl@0
    37
  }                             // namespace detail
sl@0
    38
sl@0
    39
  namespace detail
sl@0
    40
  {
sl@0
    41
    template < typename Container, typename ST = std::size_t,
sl@0
    42
      typename VT = typename Container::value_type >
sl@0
    43
      struct subscript_t:public std::unary_function < ST, VT >
sl@0
    44
    {
sl@0
    45
      typedef VT& result_type;
sl@0
    46
sl@0
    47
      subscript_t(Container & c):container(&c)
sl@0
    48
      {
sl@0
    49
      }
sl@0
    50
      VT & operator() (const ST & i) const
sl@0
    51
      {
sl@0
    52
        return (*container)[i];
sl@0
    53
      }
sl@0
    54
    protected:
sl@0
    55
        Container * container;
sl@0
    56
    };
sl@0
    57
    template < typename Container >
sl@0
    58
      subscript_t < Container > subscript(Container & c) {
sl@0
    59
      return subscript_t < Container > (c);
sl@0
    60
    }
sl@0
    61
  }                             // namespace detail
sl@0
    62
sl@0
    63
  template < typename Graph, typename GraphTC,
sl@0
    64
    typename G_to_TC_VertexMap,
sl@0
    65
    typename VertexIndexMap >
sl@0
    66
    void transitive_closure(const Graph & g, GraphTC & tc,
sl@0
    67
                            G_to_TC_VertexMap g_to_tc_map,
sl@0
    68
                            VertexIndexMap index_map)
sl@0
    69
  {
sl@0
    70
    if (num_vertices(g) == 0)
sl@0
    71
      return;
sl@0
    72
    typedef typename graph_traits < Graph >::vertex_descriptor vertex;
sl@0
    73
    typedef typename graph_traits < Graph >::edge_descriptor edge;
sl@0
    74
    typedef typename graph_traits < Graph >::vertex_iterator vertex_iterator;
sl@0
    75
    typedef typename property_traits < VertexIndexMap >::value_type size_type;
sl@0
    76
    typedef typename graph_traits <
sl@0
    77
      Graph >::adjacency_iterator adjacency_iterator;
sl@0
    78
sl@0
    79
    function_requires < VertexListGraphConcept < Graph > >();
sl@0
    80
    function_requires < AdjacencyGraphConcept < Graph > >();
sl@0
    81
    function_requires < VertexMutableGraphConcept < GraphTC > >();
sl@0
    82
    function_requires < EdgeMutableGraphConcept < GraphTC > >();
sl@0
    83
    function_requires < ReadablePropertyMapConcept < VertexIndexMap,
sl@0
    84
      vertex > >();
sl@0
    85
sl@0
    86
    typedef size_type cg_vertex;
sl@0
    87
    std::vector < cg_vertex > component_number_vec(num_vertices(g));
sl@0
    88
    iterator_property_map < cg_vertex *, VertexIndexMap, cg_vertex, cg_vertex& >
sl@0
    89
      component_number(&component_number_vec[0], index_map);
sl@0
    90
sl@0
    91
    int num_scc = strong_components(g, component_number,
sl@0
    92
                                    vertex_index_map(index_map));
sl@0
    93
sl@0
    94
    std::vector < std::vector < vertex > >components;
sl@0
    95
    build_component_lists(g, num_scc, component_number, components);
sl@0
    96
sl@0
    97
    typedef std::vector<std::vector<cg_vertex> > CG_t;
sl@0
    98
    CG_t CG(num_scc);
sl@0
    99
    for (cg_vertex s = 0; s < components.size(); ++s) {
sl@0
   100
      std::vector < cg_vertex > adj;
sl@0
   101
      for (size_type i = 0; i < components[s].size(); ++i) {
sl@0
   102
        vertex u = components[s][i];
sl@0
   103
        adjacency_iterator v, v_end;
sl@0
   104
        for (tie(v, v_end) = adjacent_vertices(u, g); v != v_end; ++v) {
sl@0
   105
          cg_vertex t = component_number[*v];
sl@0
   106
          if (s != t)           // Avoid loops in the condensation graph
sl@0
   107
            adj.push_back(t);
sl@0
   108
        }
sl@0
   109
      }
sl@0
   110
      std::sort(adj.begin(), adj.end());
sl@0
   111
      typename std::vector<cg_vertex>::iterator di =
sl@0
   112
        std::unique(adj.begin(), adj.end());
sl@0
   113
      if (di != adj.end())
sl@0
   114
        adj.erase(di, adj.end());
sl@0
   115
      CG[s] = adj;
sl@0
   116
    }
sl@0
   117
sl@0
   118
    std::vector<cg_vertex> topo_order;
sl@0
   119
    std::vector<cg_vertex> topo_number(num_vertices(CG));
sl@0
   120
    topological_sort(CG, std::back_inserter(topo_order),
sl@0
   121
                     vertex_index_map(identity_property_map()));
sl@0
   122
    std::reverse(topo_order.begin(), topo_order.end());
sl@0
   123
    size_type n = 0;
sl@0
   124
    for (typename std::vector<cg_vertex>::iterator iter = topo_order.begin();
sl@0
   125
         iter != topo_order.end(); ++iter)
sl@0
   126
      topo_number[*iter] = n++;
sl@0
   127
sl@0
   128
    for (size_type i = 0; i < num_vertices(CG); ++i)
sl@0
   129
      std::sort(CG[i].begin(), CG[i].end(),
sl@0
   130
                boost::bind(std::less<cg_vertex>(),
sl@0
   131
                            boost::bind(detail::subscript(topo_number), _1),
sl@0
   132
                            boost::bind(detail::subscript(topo_number), _2)));
sl@0
   133
sl@0
   134
    std::vector<std::vector<cg_vertex> > chains;
sl@0
   135
    {
sl@0
   136
      std::vector<cg_vertex> in_a_chain(num_vertices(CG));
sl@0
   137
      for (typename std::vector<cg_vertex>::iterator i = topo_order.begin();
sl@0
   138
           i != topo_order.end(); ++i) {
sl@0
   139
        cg_vertex v = *i;
sl@0
   140
        if (!in_a_chain[v]) {
sl@0
   141
          chains.resize(chains.size() + 1);
sl@0
   142
          std::vector<cg_vertex>& chain = chains.back();
sl@0
   143
          for (;;) {
sl@0
   144
            chain.push_back(v);
sl@0
   145
            in_a_chain[v] = true;
sl@0
   146
            typename graph_traits<CG_t>::adjacency_iterator adj_first, adj_last;
sl@0
   147
            tie(adj_first, adj_last) = adjacent_vertices(v, CG);
sl@0
   148
            typename graph_traits<CG_t>::adjacency_iterator next
sl@0
   149
              = std::find_if(adj_first, adj_last,
sl@0
   150
                             std::not1(detail::subscript(in_a_chain)));
sl@0
   151
            if (next != adj_last)
sl@0
   152
              v = *next;
sl@0
   153
            else
sl@0
   154
              break;            // end of chain, dead-end
sl@0
   155
sl@0
   156
          }
sl@0
   157
        }
sl@0
   158
      }
sl@0
   159
    }
sl@0
   160
    std::vector<size_type> chain_number(num_vertices(CG));
sl@0
   161
    std::vector<size_type> pos_in_chain(num_vertices(CG));
sl@0
   162
    for (size_type i = 0; i < chains.size(); ++i)
sl@0
   163
      for (size_type j = 0; j < chains[i].size(); ++j) {
sl@0
   164
        cg_vertex v = chains[i][j];
sl@0
   165
        chain_number[v] = i;
sl@0
   166
        pos_in_chain[v] = j;
sl@0
   167
      }
sl@0
   168
sl@0
   169
    cg_vertex inf = (std::numeric_limits< cg_vertex >::max)();
sl@0
   170
    std::vector<std::vector<cg_vertex> > successors(num_vertices(CG),
sl@0
   171
                                                    std::vector<cg_vertex>
sl@0
   172
                                                    (chains.size(), inf));
sl@0
   173
    for (typename std::vector<cg_vertex>::reverse_iterator
sl@0
   174
           i = topo_order.rbegin(); i != topo_order.rend(); ++i) {
sl@0
   175
      cg_vertex u = *i;
sl@0
   176
      typename graph_traits<CG_t>::adjacency_iterator adj, adj_last;
sl@0
   177
      for (tie(adj, adj_last) = adjacent_vertices(u, CG);
sl@0
   178
           adj != adj_last; ++adj) {
sl@0
   179
        cg_vertex v = *adj;
sl@0
   180
        if (topo_number[v] < successors[u][chain_number[v]]) {
sl@0
   181
          // Succ(u) = Succ(u) U Succ(v)
sl@0
   182
          detail::union_successor_sets(successors[u], successors[v],
sl@0
   183
                                       successors[u]);
sl@0
   184
          // Succ(u) = Succ(u) U {v}
sl@0
   185
          successors[u][chain_number[v]] = topo_number[v];
sl@0
   186
        }
sl@0
   187
      }
sl@0
   188
    }
sl@0
   189
sl@0
   190
    for (size_type i = 0; i < CG.size(); ++i)
sl@0
   191
      CG[i].clear();
sl@0
   192
    for (size_type i = 0; i < CG.size(); ++i)
sl@0
   193
      for (size_type j = 0; j < chains.size(); ++j) {
sl@0
   194
        size_type topo_num = successors[i][j];
sl@0
   195
        if (topo_num < inf) {
sl@0
   196
          cg_vertex v = topo_order[topo_num];
sl@0
   197
          for (size_type k = pos_in_chain[v]; k < chains[j].size(); ++k)
sl@0
   198
            CG[i].push_back(chains[j][k]);
sl@0
   199
        }
sl@0
   200
      }
sl@0
   201
sl@0
   202
sl@0
   203
    // Add vertices to the transitive closure graph
sl@0
   204
    typedef typename graph_traits < GraphTC >::vertex_descriptor tc_vertex;
sl@0
   205
    {
sl@0
   206
      vertex_iterator i, i_end;
sl@0
   207
      for (tie(i, i_end) = vertices(g); i != i_end; ++i)
sl@0
   208
        g_to_tc_map[*i] = add_vertex(tc);
sl@0
   209
    }
sl@0
   210
    // Add edges between all the vertices in two adjacent SCCs
sl@0
   211
    typename graph_traits<CG_t>::vertex_iterator si, si_end;
sl@0
   212
    for (tie(si, si_end) = vertices(CG); si != si_end; ++si) {
sl@0
   213
      cg_vertex s = *si;
sl@0
   214
      typename graph_traits<CG_t>::adjacency_iterator i, i_end;
sl@0
   215
      for (tie(i, i_end) = adjacent_vertices(s, CG); i != i_end; ++i) {
sl@0
   216
        cg_vertex t = *i;
sl@0
   217
        for (size_type k = 0; k < components[s].size(); ++k)
sl@0
   218
          for (size_type l = 0; l < components[t].size(); ++l)
sl@0
   219
            add_edge(g_to_tc_map[components[s][k]],
sl@0
   220
                     g_to_tc_map[components[t][l]], tc);
sl@0
   221
      }
sl@0
   222
    }
sl@0
   223
    // Add edges connecting all vertices in a SCC
sl@0
   224
    for (size_type i = 0; i < components.size(); ++i)
sl@0
   225
      if (components[i].size() > 1)
sl@0
   226
        for (size_type k = 0; k < components[i].size(); ++k)
sl@0
   227
          for (size_type l = 0; l < components[i].size(); ++l) {
sl@0
   228
            vertex u = components[i][k], v = components[i][l];
sl@0
   229
            add_edge(g_to_tc_map[u], g_to_tc_map[v], tc);
sl@0
   230
          }
sl@0
   231
sl@0
   232
    // Find loopbacks in the original graph.
sl@0
   233
    // Need to add it to transitive closure.
sl@0
   234
    {
sl@0
   235
      vertex_iterator i, i_end;
sl@0
   236
      for (tie(i, i_end) = vertices(g); i != i_end; ++i)
sl@0
   237
        {
sl@0
   238
          adjacency_iterator ab, ae;
sl@0
   239
          for (boost::tie(ab, ae) = adjacent_vertices(*i, g); ab != ae; ++ab)
sl@0
   240
            {
sl@0
   241
              if (*ab == *i)
sl@0
   242
                if (components[component_number[*i]].size() == 1)
sl@0
   243
                  add_edge(g_to_tc_map[*i], g_to_tc_map[*i], tc);
sl@0
   244
            }
sl@0
   245
        }
sl@0
   246
    }
sl@0
   247
  }
sl@0
   248
sl@0
   249
  template <typename Graph, typename GraphTC>
sl@0
   250
  void transitive_closure(const Graph & g, GraphTC & tc)
sl@0
   251
  {
sl@0
   252
    if (num_vertices(g) == 0)
sl@0
   253
      return;
sl@0
   254
    typedef typename property_map<Graph, vertex_index_t>::const_type
sl@0
   255
      VertexIndexMap;
sl@0
   256
    VertexIndexMap index_map = get(vertex_index, g);
sl@0
   257
sl@0
   258
    typedef typename graph_traits<GraphTC>::vertex_descriptor tc_vertex;
sl@0
   259
    std::vector<tc_vertex> to_tc_vec(num_vertices(g));
sl@0
   260
    iterator_property_map < tc_vertex *, VertexIndexMap, tc_vertex, tc_vertex&>
sl@0
   261
      g_to_tc_map(&to_tc_vec[0], index_map);
sl@0
   262
sl@0
   263
    transitive_closure(g, tc, g_to_tc_map, index_map);
sl@0
   264
  }
sl@0
   265
sl@0
   266
  namespace detail
sl@0
   267
  {
sl@0
   268
    template < typename Graph, typename GraphTC, typename G_to_TC_VertexMap,
sl@0
   269
      typename VertexIndexMap>
sl@0
   270
    void transitive_closure_dispatch
sl@0
   271
      (const Graph & g, GraphTC & tc,
sl@0
   272
       G_to_TC_VertexMap g_to_tc_map, VertexIndexMap index_map)
sl@0
   273
    {
sl@0
   274
      typedef typename graph_traits < GraphTC >::vertex_descriptor tc_vertex;
sl@0
   275
      typename std::vector < tc_vertex >::size_type
sl@0
   276
        n = is_default_param(g_to_tc_map) ? num_vertices(g) : 1;
sl@0
   277
      std::vector < tc_vertex > to_tc_vec(n);
sl@0
   278
sl@0
   279
      transitive_closure
sl@0
   280
        (g, tc,
sl@0
   281
         choose_param(g_to_tc_map, make_iterator_property_map
sl@0
   282
                      (to_tc_vec.begin(), index_map, to_tc_vec[0])),
sl@0
   283
         index_map);
sl@0
   284
    }
sl@0
   285
  }                             // namespace detail
sl@0
   286
sl@0
   287
  template < typename Graph, typename GraphTC,
sl@0
   288
    typename P, typename T, typename R >
sl@0
   289
    void transitive_closure(const Graph & g, GraphTC & tc,
sl@0
   290
                            const bgl_named_params < P, T, R > &params)
sl@0
   291
  {
sl@0
   292
    if (num_vertices(g) == 0)
sl@0
   293
      return;
sl@0
   294
    detail::transitive_closure_dispatch
sl@0
   295
      (g, tc, get_param(params, orig_to_copy_t()),
sl@0
   296
       choose_const_pmap(get_param(params, vertex_index), g, vertex_index) );
sl@0
   297
  }
sl@0
   298
sl@0
   299
sl@0
   300
  template < typename G > void warshall_transitive_closure(G & g)
sl@0
   301
  {
sl@0
   302
    typedef typename graph_traits < G >::vertex_descriptor vertex;
sl@0
   303
    typedef typename graph_traits < G >::vertex_iterator vertex_iterator;
sl@0
   304
sl@0
   305
    function_requires < AdjacencyMatrixConcept < G > >();
sl@0
   306
    function_requires < EdgeMutableGraphConcept < G > >();
sl@0
   307
sl@0
   308
    // Matrix form:
sl@0
   309
    // for k
sl@0
   310
    //  for i
sl@0
   311
    //    if A[i,k]
sl@0
   312
    //      for j
sl@0
   313
    //        A[i,j] = A[i,j] | A[k,j]
sl@0
   314
    vertex_iterator ki, ke, ii, ie, ji, je;
sl@0
   315
    for (tie(ki, ke) = vertices(g); ki != ke; ++ki)
sl@0
   316
      for (tie(ii, ie) = vertices(g); ii != ie; ++ii)
sl@0
   317
        if (edge(*ii, *ki, g).second)
sl@0
   318
          for (tie(ji, je) = vertices(g); ji != je; ++ji)
sl@0
   319
            if (!edge(*ii, *ji, g).second && edge(*ki, *ji, g).second) {
sl@0
   320
              add_edge(*ii, *ji, g);
sl@0
   321
            }
sl@0
   322
  }
sl@0
   323
sl@0
   324
sl@0
   325
  template < typename G > void warren_transitive_closure(G & g)
sl@0
   326
  {
sl@0
   327
    using namespace boost;
sl@0
   328
    typedef typename graph_traits < G >::vertex_descriptor vertex;
sl@0
   329
    typedef typename graph_traits < G >::vertex_iterator vertex_iterator;
sl@0
   330
sl@0
   331
    function_requires < AdjacencyMatrixConcept < G > >();
sl@0
   332
    function_requires < EdgeMutableGraphConcept < G > >();
sl@0
   333
sl@0
   334
    // Make sure second loop will work
sl@0
   335
    if (num_vertices(g) == 0)
sl@0
   336
      return;
sl@0
   337
sl@0
   338
    // for i = 2 to n
sl@0
   339
    //    for k = 1 to i - 1
sl@0
   340
    //      if A[i,k]
sl@0
   341
    //        for j = 1 to n
sl@0
   342
    //          A[i,j] = A[i,j] | A[k,j]
sl@0
   343
sl@0
   344
    vertex_iterator ic, ie, jc, je, kc, ke;
sl@0
   345
    for (tie(ic, ie) = vertices(g), ++ic; ic != ie; ++ic)
sl@0
   346
      for (tie(kc, ke) = vertices(g); *kc != *ic; ++kc)
sl@0
   347
        if (edge(*ic, *kc, g).second)
sl@0
   348
          for (tie(jc, je) = vertices(g); jc != je; ++jc)
sl@0
   349
            if (!edge(*ic, *jc, g).second && edge(*kc, *jc, g).second) {
sl@0
   350
              add_edge(*ic, *jc, g);
sl@0
   351
            }
sl@0
   352
    //  for i = 1 to n - 1
sl@0
   353
    //    for k = i + 1 to n
sl@0
   354
    //      if A[i,k]
sl@0
   355
    //        for j = 1 to n
sl@0
   356
    //          A[i,j] = A[i,j] | A[k,j]
sl@0
   357
sl@0
   358
    for (tie(ic, ie) = vertices(g), --ie; ic != ie; ++ic)
sl@0
   359
      for (kc = ic, ke = ie, ++kc; kc != ke; ++kc)
sl@0
   360
        if (edge(*ic, *kc, g).second)
sl@0
   361
          for (tie(jc, je) = vertices(g); jc != je; ++jc)
sl@0
   362
            if (!edge(*ic, *jc, g).second && edge(*kc, *jc, g).second) {
sl@0
   363
              add_edge(*ic, *jc, g);
sl@0
   364
            }
sl@0
   365
  }
sl@0
   366
sl@0
   367
sl@0
   368
}                               // namespace boost
sl@0
   369
sl@0
   370
#endif // BOOST_GRAPH_TRANSITIVE_CLOSURE_HPP