os/ossrv/ossrv_pub/boost_apis/boost/graph/subgraph.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
//=======================================================================
sl@0
     2
// Copyright 2001 University of Notre Dame.
sl@0
     3
// Authors: Jeremy G. Siek and 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_SUBGRAPH_HPP
sl@0
    11
#define BOOST_SUBGRAPH_HPP
sl@0
    12
sl@0
    13
// UNDER CONSTRUCTION
sl@0
    14
sl@0
    15
#include <boost/config.hpp>
sl@0
    16
#include <list>
sl@0
    17
#include <vector>
sl@0
    18
#include <map>
sl@0
    19
#include <cassert>
sl@0
    20
#include <boost/graph/graph_traits.hpp>
sl@0
    21
#include <boost/graph/properties.hpp>
sl@0
    22
#include <boost/iterator/indirect_iterator.hpp>
sl@0
    23
sl@0
    24
#include <boost/static_assert.hpp>
sl@0
    25
#include <boost/type_traits/is_same.hpp>
sl@0
    26
sl@0
    27
namespace boost {
sl@0
    28
sl@0
    29
  struct subgraph_tag { };
sl@0
    30
sl@0
    31
  // Invariants of an induced subgraph:
sl@0
    32
  //   - If vertex u is in subgraph g, then u must be in g.parent().
sl@0
    33
  //   - If edge e is in subgraph g, then e must be in g.parent().
sl@0
    34
  //   - If edge e=(u,v) is in the root graph, then edge e
sl@0
    35
  //     is also in any subgraph that contains both vertex u and v.
sl@0
    36
sl@0
    37
  // The Graph template parameter must have a vertex_index
sl@0
    38
  // and edge_index internal property. It is assumed that
sl@0
    39
  // the vertex indices are assigned automatically by the
sl@0
    40
  // graph during a call to add_vertex(). It is not
sl@0
    41
  // assumed that the edge vertices are assigned automatically,
sl@0
    42
  // they are explicitly assigned here.
sl@0
    43
sl@0
    44
  template <typename Graph>
sl@0
    45
  class subgraph {
sl@0
    46
    typedef graph_traits<Graph> Traits;
sl@0
    47
    typedef std::list<subgraph<Graph>*> ChildrenList;
sl@0
    48
  public:
sl@0
    49
    // Graph requirements
sl@0
    50
    typedef typename Traits::vertex_descriptor         vertex_descriptor;
sl@0
    51
    typedef typename Traits::edge_descriptor           edge_descriptor;
sl@0
    52
    typedef typename Traits::directed_category         directed_category;
sl@0
    53
    typedef typename Traits::edge_parallel_category    edge_parallel_category;
sl@0
    54
    typedef typename Traits::traversal_category        traversal_category;
sl@0
    55
sl@0
    56
    static vertex_descriptor null_vertex()
sl@0
    57
    {
sl@0
    58
      return Traits::null_vertex();
sl@0
    59
    }
sl@0
    60
sl@0
    61
sl@0
    62
    // IncidenceGraph requirements
sl@0
    63
    typedef typename Traits::out_edge_iterator         out_edge_iterator;
sl@0
    64
    typedef typename Traits::degree_size_type          degree_size_type;
sl@0
    65
sl@0
    66
    // AdjacencyGraph requirements
sl@0
    67
    typedef typename Traits::adjacency_iterator        adjacency_iterator;
sl@0
    68
sl@0
    69
    // VertexListGraph requirements
sl@0
    70
    typedef typename Traits::vertex_iterator           vertex_iterator;
sl@0
    71
    typedef typename Traits::vertices_size_type        vertices_size_type;
sl@0
    72
sl@0
    73
    // EdgeListGraph requirements
sl@0
    74
    typedef typename Traits::edge_iterator             edge_iterator;
sl@0
    75
    typedef typename Traits::edges_size_type           edges_size_type;
sl@0
    76
sl@0
    77
    typedef typename Traits::in_edge_iterator          in_edge_iterator;
sl@0
    78
sl@0
    79
    typedef typename Graph::edge_property_type         edge_property_type;
sl@0
    80
    typedef typename Graph::vertex_property_type       vertex_property_type;
sl@0
    81
    typedef subgraph_tag                               graph_tag;
sl@0
    82
    typedef Graph                                      graph_type;
sl@0
    83
    typedef typename Graph::graph_property_type        graph_property_type;
sl@0
    84
sl@0
    85
    // Constructors
sl@0
    86
sl@0
    87
    // Create the main graph, the root of the subgraph tree
sl@0
    88
    subgraph()
sl@0
    89
      : m_parent(0), m_edge_counter(0)
sl@0
    90
    { }
sl@0
    91
    subgraph(const graph_property_type& p)
sl@0
    92
      : m_graph(p), m_parent(0), m_edge_counter(0)
sl@0
    93
    { }
sl@0
    94
    subgraph(vertices_size_type n,
sl@0
    95
             const graph_property_type& p = graph_property_type())
sl@0
    96
      : m_graph(n, p), m_parent(0), m_edge_counter(0), m_global_vertex(n)
sl@0
    97
    {
sl@0
    98
      typename Graph::vertex_iterator v, v_end;
sl@0
    99
      vertices_size_type i = 0;
sl@0
   100
      for (tie(v, v_end) = vertices(m_graph); v != v_end; ++v)
sl@0
   101
        m_global_vertex[i++] = *v;
sl@0
   102
    }
sl@0
   103
sl@0
   104
    // copy constructor
sl@0
   105
    subgraph(const subgraph& x)
sl@0
   106
      : m_graph(x.m_graph), m_parent(x.m_parent),
sl@0
   107
      m_edge_counter(x.m_edge_counter),
sl@0
   108
      m_global_vertex(x.m_global_vertex),
sl@0
   109
      m_global_edge(x.m_global_edge)
sl@0
   110
    {
sl@0
   111
      // Do a deep copy
sl@0
   112
      for (typename ChildrenList::const_iterator i = x.m_children.begin();
sl@0
   113
           i != x.m_children.end(); ++i)
sl@0
   114
        m_children.push_back(new subgraph<Graph>( **i ));
sl@0
   115
    }
sl@0
   116
sl@0
   117
sl@0
   118
    ~subgraph() {
sl@0
   119
      for (typename ChildrenList::iterator i = m_children.begin();
sl@0
   120
           i != m_children.end(); ++i)
sl@0
   121
        delete *i;
sl@0
   122
    }
sl@0
   123
sl@0
   124
sl@0
   125
    // Create a subgraph
sl@0
   126
    subgraph<Graph>& create_subgraph() {
sl@0
   127
      m_children.push_back(new subgraph<Graph>());
sl@0
   128
      m_children.back()->m_parent = this;
sl@0
   129
      return *m_children.back();
sl@0
   130
    }
sl@0
   131
sl@0
   132
    // Create a subgraph with the specified vertex set.
sl@0
   133
    template <typename VertexIterator>
sl@0
   134
    subgraph<Graph>& create_subgraph(VertexIterator first,
sl@0
   135
                                     VertexIterator last)
sl@0
   136
    {
sl@0
   137
      m_children.push_back(new subgraph<Graph>());
sl@0
   138
      m_children.back()->m_parent = this;
sl@0
   139
      for (; first != last; ++first)
sl@0
   140
        add_vertex(*first, *m_children.back());
sl@0
   141
      return *m_children.back();
sl@0
   142
    }
sl@0
   143
sl@0
   144
    // local <-> global descriptor conversion functions
sl@0
   145
    vertex_descriptor local_to_global(vertex_descriptor u_local) const
sl@0
   146
    {
sl@0
   147
      return m_global_vertex[u_local];
sl@0
   148
    }
sl@0
   149
    vertex_descriptor global_to_local(vertex_descriptor u_global) const
sl@0
   150
    {
sl@0
   151
      vertex_descriptor u_local; bool in_subgraph;
sl@0
   152
      tie(u_local, in_subgraph) = this->find_vertex(u_global);
sl@0
   153
      assert(in_subgraph == true);
sl@0
   154
      return u_local;
sl@0
   155
    }
sl@0
   156
    edge_descriptor local_to_global(edge_descriptor e_local) const
sl@0
   157
    {
sl@0
   158
      return m_global_edge[get(get(edge_index, m_graph), e_local)];
sl@0
   159
    }
sl@0
   160
    edge_descriptor global_to_local(edge_descriptor e_global) const
sl@0
   161
    {
sl@0
   162
      return
sl@0
   163
        (*m_local_edge.find(get(get(edge_index, root().m_graph), e_global))).second;
sl@0
   164
    }
sl@0
   165
sl@0
   166
    // Is vertex u (of the root graph) contained in this subgraph?
sl@0
   167
    // If so, return the matching local vertex.
sl@0
   168
    std::pair<vertex_descriptor, bool>
sl@0
   169
    find_vertex(vertex_descriptor u_global) const
sl@0
   170
    {
sl@0
   171
      typename std::map<vertex_descriptor, vertex_descriptor>::const_iterator
sl@0
   172
        i = m_local_vertex.find(u_global);
sl@0
   173
      bool valid = i != m_local_vertex.end();
sl@0
   174
      return std::make_pair((valid ? (*i).second : null_vertex()), valid);
sl@0
   175
    }
sl@0
   176
sl@0
   177
    // Return the parent graph.
sl@0
   178
    subgraph& parent() { return *m_parent; }
sl@0
   179
    const subgraph& parent() const { return *m_parent; }
sl@0
   180
sl@0
   181
    bool is_root() const { return m_parent == 0; }
sl@0
   182
sl@0
   183
    // Return the root graph of the subgraph tree.
sl@0
   184
    subgraph& root() {
sl@0
   185
      if (this->is_root())
sl@0
   186
        return *this;
sl@0
   187
      else
sl@0
   188
        return m_parent->root();
sl@0
   189
    }
sl@0
   190
    const subgraph& root() const {
sl@0
   191
      if (this->is_root())
sl@0
   192
        return *this;
sl@0
   193
      else
sl@0
   194
        return m_parent->root();
sl@0
   195
    }
sl@0
   196
sl@0
   197
    // Return the children subgraphs of this graph/subgraph.
sl@0
   198
    // Use a list of pointers because the VC++ std::list doesn't like
sl@0
   199
    // storing incomplete type.
sl@0
   200
    typedef indirect_iterator<
sl@0
   201
        typename ChildrenList::const_iterator
sl@0
   202
      , subgraph<Graph>
sl@0
   203
      , std::bidirectional_iterator_tag
sl@0
   204
    >
sl@0
   205
    children_iterator;
sl@0
   206
sl@0
   207
    typedef indirect_iterator<
sl@0
   208
        typename ChildrenList::const_iterator
sl@0
   209
      , subgraph<Graph> const
sl@0
   210
      , std::bidirectional_iterator_tag
sl@0
   211
    >
sl@0
   212
    const_children_iterator;
sl@0
   213
sl@0
   214
    std::pair<const_children_iterator, const_children_iterator>
sl@0
   215
    children() const
sl@0
   216
    {
sl@0
   217
      return std::make_pair(const_children_iterator(m_children.begin()),
sl@0
   218
                            const_children_iterator(m_children.end()));
sl@0
   219
    }
sl@0
   220
sl@0
   221
    std::pair<children_iterator, children_iterator>
sl@0
   222
    children()
sl@0
   223
    {
sl@0
   224
      return std::make_pair(children_iterator(m_children.begin()),
sl@0
   225
                            children_iterator(m_children.end()));
sl@0
   226
    }
sl@0
   227
sl@0
   228
    std::size_t num_children() const { return m_children.size(); }
sl@0
   229
sl@0
   230
#ifndef BOOST_GRAPH_NO_BUNDLED_PROPERTIES
sl@0
   231
    // Bundled properties support
sl@0
   232
    template<typename Descriptor>
sl@0
   233
    typename graph::detail::bundled_result<Graph, Descriptor>::type&
sl@0
   234
    operator[](Descriptor x)
sl@0
   235
    { 
sl@0
   236
      if (m_parent == 0) return m_graph[x];
sl@0
   237
      else return root().m_graph[local_to_global(x)];
sl@0
   238
    }
sl@0
   239
sl@0
   240
    template<typename Descriptor>
sl@0
   241
    typename graph::detail::bundled_result<Graph, Descriptor>::type const&
sl@0
   242
    operator[](Descriptor x) const
sl@0
   243
    { 
sl@0
   244
      if (m_parent == 0) return m_graph[x];
sl@0
   245
      else return root().m_graph[local_to_global(x)];
sl@0
   246
    }
sl@0
   247
#endif // BOOST_GRAPH_NO_BUNDLED_PROPERTIES
sl@0
   248
sl@0
   249
    //  private:
sl@0
   250
    typedef typename property_map<Graph, edge_index_t>::type EdgeIndexMap;
sl@0
   251
    typedef typename property_traits<EdgeIndexMap>::value_type edge_index_type;
sl@0
   252
    BOOST_STATIC_ASSERT((!is_same<edge_index_type, 
sl@0
   253
                        boost::detail::error_property_not_found>::value));
sl@0
   254
sl@0
   255
    Graph m_graph;
sl@0
   256
    subgraph<Graph>* m_parent;
sl@0
   257
    edge_index_type m_edge_counter; // for generating unique edge indices
sl@0
   258
    ChildrenList m_children;
sl@0
   259
    std::vector<vertex_descriptor> m_global_vertex; // local -> global
sl@0
   260
    std::map<vertex_descriptor, vertex_descriptor> m_local_vertex;  // global -> local
sl@0
   261
    std::vector<edge_descriptor> m_global_edge;              // local -> global
sl@0
   262
    std::map<edge_index_type, edge_descriptor> m_local_edge; // global -> local
sl@0
   263
sl@0
   264
    edge_descriptor
sl@0
   265
    local_add_edge(vertex_descriptor u_local, vertex_descriptor v_local,
sl@0
   266
                   edge_descriptor e_global)
sl@0
   267
    {
sl@0
   268
      edge_descriptor e_local;
sl@0
   269
      bool inserted;
sl@0
   270
      tie(e_local, inserted) = add_edge(u_local, v_local, m_graph);
sl@0
   271
      put(edge_index, m_graph, e_local, m_edge_counter++);
sl@0
   272
      m_global_edge.push_back(e_global);
sl@0
   273
      m_local_edge[get(get(edge_index, this->root()), e_global)] = e_local;
sl@0
   274
      return e_local;
sl@0
   275
    }
sl@0
   276
sl@0
   277
  };
sl@0
   278
sl@0
   279
#ifndef BOOST_GRAPH_NO_BUNDLED_PROPERTIES
sl@0
   280
  template<typename Graph>
sl@0
   281
  struct vertex_bundle_type<subgraph<Graph> > : vertex_bundle_type<Graph> { };
sl@0
   282
sl@0
   283
  template<typename Graph>
sl@0
   284
  struct edge_bundle_type<subgraph<Graph> > : edge_bundle_type<Graph> { };
sl@0
   285
#endif // BOOST_GRAPH_NO_BUNDLED_PROPERTIES
sl@0
   286
sl@0
   287
  //===========================================================================
sl@0
   288
  // Functions special to the Subgraph Class
sl@0
   289
sl@0
   290
  template <typename G>
sl@0
   291
  typename subgraph<G>::vertex_descriptor
sl@0
   292
  add_vertex(typename subgraph<G>::vertex_descriptor u_global,
sl@0
   293
             subgraph<G>& g)
sl@0
   294
  {
sl@0
   295
    assert(!g.is_root());
sl@0
   296
    typename subgraph<G>::vertex_descriptor u_local, v_global, uu_global;
sl@0
   297
    typename subgraph<G>::edge_descriptor e_global;
sl@0
   298
sl@0
   299
    u_local = add_vertex(g.m_graph);
sl@0
   300
    g.m_global_vertex.push_back(u_global);
sl@0
   301
    g.m_local_vertex[u_global] = u_local;
sl@0
   302
sl@0
   303
    subgraph<G>& r = g.root();
sl@0
   304
sl@0
   305
    // remember edge global and local maps
sl@0
   306
    {
sl@0
   307
      typename subgraph<G>::out_edge_iterator ei, ei_end;
sl@0
   308
      for (tie(ei, ei_end) = out_edges(u_global, r);
sl@0
   309
           ei != ei_end; ++ei) {
sl@0
   310
        e_global = *ei;
sl@0
   311
        v_global = target(e_global, r);
sl@0
   312
        if (g.find_vertex(v_global).second == true)
sl@0
   313
          g.local_add_edge(u_local, g.global_to_local(v_global), e_global);
sl@0
   314
      }
sl@0
   315
    }
sl@0
   316
    if (is_directed(g)) { // not necessary for undirected graph
sl@0
   317
      typename subgraph<G>::vertex_iterator vi, vi_end;
sl@0
   318
      typename subgraph<G>::out_edge_iterator ei, ei_end;
sl@0
   319
      for (tie(vi, vi_end) = vertices(r); vi != vi_end; ++vi) {
sl@0
   320
        v_global = *vi;
sl@0
   321
        if (g.find_vertex(v_global).second)
sl@0
   322
          for (tie(ei, ei_end) = out_edges(*vi, r); ei != ei_end; ++ei) {
sl@0
   323
            e_global = *ei;
sl@0
   324
            uu_global = target(e_global, r);
sl@0
   325
            if (uu_global == u_global && g.find_vertex(v_global).second)
sl@0
   326
              g.local_add_edge(g.global_to_local(v_global), u_local, e_global);
sl@0
   327
          }
sl@0
   328
      }
sl@0
   329
    }
sl@0
   330
sl@0
   331
    return u_local;
sl@0
   332
  }
sl@0
   333
sl@0
   334
  //===========================================================================
sl@0
   335
  // Functions required by the IncidenceGraph concept
sl@0
   336
sl@0
   337
  template <typename G>
sl@0
   338
  std::pair<typename graph_traits<G>::out_edge_iterator,
sl@0
   339
            typename graph_traits<G>::out_edge_iterator>
sl@0
   340
  out_edges(typename graph_traits<G>::vertex_descriptor u_local,
sl@0
   341
            const subgraph<G>& g)
sl@0
   342
    { return out_edges(u_local, g.m_graph); }
sl@0
   343
sl@0
   344
  template <typename G>
sl@0
   345
  typename graph_traits<G>::degree_size_type
sl@0
   346
  out_degree(typename graph_traits<G>::vertex_descriptor u_local,
sl@0
   347
             const subgraph<G>& g)
sl@0
   348
    { return out_degree(u_local, g.m_graph); }
sl@0
   349
sl@0
   350
  template <typename G>
sl@0
   351
  typename graph_traits<G>::vertex_descriptor
sl@0
   352
  source(typename graph_traits<G>::edge_descriptor e_local,
sl@0
   353
         const subgraph<G>& g)
sl@0
   354
    { return source(e_local, g.m_graph); }
sl@0
   355
sl@0
   356
  template <typename G>
sl@0
   357
  typename graph_traits<G>::vertex_descriptor
sl@0
   358
  target(typename graph_traits<G>::edge_descriptor e_local,
sl@0
   359
         const subgraph<G>& g)
sl@0
   360
    { return target(e_local, g.m_graph); }
sl@0
   361
sl@0
   362
  //===========================================================================
sl@0
   363
  // Functions required by the BidirectionalGraph concept
sl@0
   364
sl@0
   365
  template <typename G>
sl@0
   366
  std::pair<typename graph_traits<G>::in_edge_iterator,
sl@0
   367
            typename graph_traits<G>::in_edge_iterator>
sl@0
   368
  in_edges(typename graph_traits<G>::vertex_descriptor u_local,
sl@0
   369
            const subgraph<G>& g)
sl@0
   370
    { return in_edges(u_local, g.m_graph); }
sl@0
   371
sl@0
   372
  template <typename G>
sl@0
   373
  typename graph_traits<G>::degree_size_type
sl@0
   374
  in_degree(typename graph_traits<G>::vertex_descriptor u_local,
sl@0
   375
             const subgraph<G>& g)
sl@0
   376
    { return in_degree(u_local, g.m_graph); }
sl@0
   377
sl@0
   378
  template <typename G>
sl@0
   379
  typename graph_traits<G>::degree_size_type
sl@0
   380
  degree(typename graph_traits<G>::vertex_descriptor u_local,
sl@0
   381
             const subgraph<G>& g)
sl@0
   382
    { return degree(u_local, g.m_graph); }
sl@0
   383
sl@0
   384
  //===========================================================================
sl@0
   385
  // Functions required by the AdjacencyGraph concept
sl@0
   386
sl@0
   387
  template <typename G>
sl@0
   388
  std::pair<typename subgraph<G>::adjacency_iterator,
sl@0
   389
            typename subgraph<G>::adjacency_iterator>
sl@0
   390
  adjacent_vertices(typename subgraph<G>::vertex_descriptor u_local,
sl@0
   391
                    const subgraph<G>& g)
sl@0
   392
    { return adjacent_vertices(u_local, g.m_graph); }
sl@0
   393
sl@0
   394
  //===========================================================================
sl@0
   395
  // Functions required by the VertexListGraph concept
sl@0
   396
sl@0
   397
  template <typename G>
sl@0
   398
  std::pair<typename subgraph<G>::vertex_iterator,
sl@0
   399
            typename subgraph<G>::vertex_iterator>
sl@0
   400
  vertices(const subgraph<G>& g)
sl@0
   401
    { return vertices(g.m_graph); }
sl@0
   402
sl@0
   403
  template <typename G>
sl@0
   404
  typename subgraph<G>::vertices_size_type
sl@0
   405
  num_vertices(const subgraph<G>& g)
sl@0
   406
    { return num_vertices(g.m_graph); }
sl@0
   407
sl@0
   408
  //===========================================================================
sl@0
   409
  // Functions required by the EdgeListGraph concept
sl@0
   410
sl@0
   411
  template <typename G>
sl@0
   412
  std::pair<typename subgraph<G>::edge_iterator,
sl@0
   413
            typename subgraph<G>::edge_iterator>
sl@0
   414
  edges(const subgraph<G>& g)
sl@0
   415
    { return edges(g.m_graph); }
sl@0
   416
sl@0
   417
  template <typename G>
sl@0
   418
  typename subgraph<G>::edges_size_type
sl@0
   419
  num_edges(const subgraph<G>& g)
sl@0
   420
    { return num_edges(g.m_graph); }
sl@0
   421
sl@0
   422
  //===========================================================================
sl@0
   423
  // Functions required by the AdjacencyMatrix concept
sl@0
   424
sl@0
   425
  template <typename G>
sl@0
   426
  std::pair<typename subgraph<G>::edge_descriptor, bool>
sl@0
   427
  edge(typename subgraph<G>::vertex_descriptor u_local,
sl@0
   428
       typename subgraph<G>::vertex_descriptor v_local,
sl@0
   429
       const subgraph<G>& g)
sl@0
   430
  {
sl@0
   431
    return edge(u_local, v_local, g.m_graph);
sl@0
   432
  }
sl@0
   433
sl@0
   434
  //===========================================================================
sl@0
   435
  // Functions required by the MutableGraph concept
sl@0
   436
sl@0
   437
  namespace detail {
sl@0
   438
sl@0
   439
    template <typename Vertex, typename Edge, typename Graph>
sl@0
   440
    void add_edge_recur_down
sl@0
   441
    (Vertex u_global, Vertex v_global, Edge e_global, subgraph<Graph>& g);
sl@0
   442
sl@0
   443
    template <typename Vertex, typename Edge, typename Children, typename G>
sl@0
   444
    void children_add_edge(Vertex u_global, Vertex v_global, Edge e_global,
sl@0
   445
                           Children& c, subgraph<G>* orig)
sl@0
   446
    {
sl@0
   447
      for (typename Children::iterator i = c.begin(); i != c.end(); ++i)
sl@0
   448
        if ((*i)->find_vertex(u_global).second
sl@0
   449
            && (*i)->find_vertex(v_global).second)
sl@0
   450
          add_edge_recur_down(u_global, v_global, e_global, **i, orig);
sl@0
   451
    }
sl@0
   452
sl@0
   453
    template <typename Vertex, typename Edge, typename Graph>
sl@0
   454
    void add_edge_recur_down
sl@0
   455
      (Vertex u_global, Vertex v_global, Edge e_global, subgraph<Graph>& g,
sl@0
   456
       subgraph<Graph>* orig)
sl@0
   457
    {
sl@0
   458
      if (&g != orig ) {
sl@0
   459
        // add local edge only if u_global and v_global are in subgraph g
sl@0
   460
        Vertex u_local, v_local;
sl@0
   461
        bool u_in_subgraph, v_in_subgraph;
sl@0
   462
        tie(u_local, u_in_subgraph) = g.find_vertex(u_global);
sl@0
   463
        tie(v_local, v_in_subgraph) = g.find_vertex(v_global);
sl@0
   464
        if (u_in_subgraph && v_in_subgraph)
sl@0
   465
          g.local_add_edge(u_local, v_local, e_global);
sl@0
   466
      }
sl@0
   467
      children_add_edge(u_global, v_global, e_global, g.m_children, orig);
sl@0
   468
    }
sl@0
   469
sl@0
   470
    template <typename Vertex, typename Graph>
sl@0
   471
    std::pair<typename subgraph<Graph>::edge_descriptor, bool>
sl@0
   472
    add_edge_recur_up(Vertex u_global, Vertex v_global,
sl@0
   473
                      const typename Graph::edge_property_type& ep,
sl@0
   474
                      subgraph<Graph>& g, subgraph<Graph>* orig)
sl@0
   475
    {
sl@0
   476
      if (g.is_root()) {
sl@0
   477
        typename subgraph<Graph>::edge_descriptor e_global;
sl@0
   478
        bool inserted;
sl@0
   479
        tie(e_global, inserted) = add_edge(u_global, v_global, ep, g.m_graph);
sl@0
   480
        put(edge_index, g.m_graph, e_global, g.m_edge_counter++);
sl@0
   481
        g.m_global_edge.push_back(e_global);
sl@0
   482
        children_add_edge(u_global, v_global, e_global, g.m_children, orig);
sl@0
   483
        return std::make_pair(e_global, inserted);
sl@0
   484
      } else
sl@0
   485
        return add_edge_recur_up(u_global, v_global, ep, *g.m_parent, orig);
sl@0
   486
    }
sl@0
   487
sl@0
   488
  } // namespace detail
sl@0
   489
sl@0
   490
  // Add an edge to the subgraph g, specified by the local vertex
sl@0
   491
  // descriptors u and v. In addition, the edge will be added to any
sl@0
   492
  // other subgraphs which contain vertex descriptors u and v.
sl@0
   493
sl@0
   494
  template <typename G>
sl@0
   495
  std::pair<typename subgraph<G>::edge_descriptor, bool>
sl@0
   496
  add_edge(typename subgraph<G>::vertex_descriptor u_local,
sl@0
   497
           typename subgraph<G>::vertex_descriptor v_local,
sl@0
   498
           const typename G::edge_property_type& ep,
sl@0
   499
           subgraph<G>& g)
sl@0
   500
  {
sl@0
   501
    if (g.is_root()) // u_local and v_local are really global
sl@0
   502
      return detail::add_edge_recur_up(u_local, v_local, ep, g, &g);
sl@0
   503
    else {
sl@0
   504
      typename subgraph<G>::edge_descriptor e_local, e_global;
sl@0
   505
      bool inserted;
sl@0
   506
      tie(e_global, inserted) = detail::add_edge_recur_up
sl@0
   507
        (g.local_to_global(u_local), g.local_to_global(v_local), ep, g, &g);
sl@0
   508
      e_local = g.local_add_edge(u_local, v_local, e_global);
sl@0
   509
      return std::make_pair(e_local, inserted);
sl@0
   510
    }
sl@0
   511
  }
sl@0
   512
sl@0
   513
  template <typename G>
sl@0
   514
  std::pair<typename subgraph<G>::edge_descriptor, bool>
sl@0
   515
  add_edge(typename subgraph<G>::vertex_descriptor u,
sl@0
   516
           typename subgraph<G>::vertex_descriptor v,
sl@0
   517
           subgraph<G>& g)
sl@0
   518
  {
sl@0
   519
    typename G::edge_property_type ep;
sl@0
   520
    return add_edge(u, v, ep, g);
sl@0
   521
  }
sl@0
   522
sl@0
   523
  namespace detail {
sl@0
   524
sl@0
   525
    //-------------------------------------------------------------------------
sl@0
   526
    // implementation of remove_edge(u,v,g)
sl@0
   527
sl@0
   528
    template <typename Vertex, typename Graph>
sl@0
   529
    void remove_edge_recur_down(Vertex u_global, Vertex v_global,
sl@0
   530
                                subgraph<Graph>& g);
sl@0
   531
sl@0
   532
    template <typename Vertex, typename Children>
sl@0
   533
    void children_remove_edge(Vertex u_global, Vertex v_global,
sl@0
   534
                              Children& c)
sl@0
   535
    {
sl@0
   536
      for (typename Children::iterator i = c.begin(); i != c.end(); ++i)
sl@0
   537
        if ((*i)->find_vertex(u_global).second
sl@0
   538
            && (*i)->find_vertex(v_global).second)
sl@0
   539
          remove_edge_recur_down(u_global, v_global, **i);
sl@0
   540
    }
sl@0
   541
sl@0
   542
    template <typename Vertex, typename Graph>
sl@0
   543
    void remove_edge_recur_down(Vertex u_global, Vertex v_global,
sl@0
   544
                                subgraph<Graph>& g)
sl@0
   545
    {
sl@0
   546
      Vertex u_local, v_local;
sl@0
   547
      u_local = g.m_local_vertex[u_global];
sl@0
   548
      v_local = g.m_local_vertex[v_global];
sl@0
   549
      remove_edge(u_local, v_local, g.m_graph);
sl@0
   550
      children_remove_edge(u_global, v_global, g.m_children);
sl@0
   551
    }
sl@0
   552
sl@0
   553
    template <typename Vertex, typename Graph>
sl@0
   554
    void remove_edge_recur_up(Vertex u_global, Vertex v_global,
sl@0
   555
                              subgraph<Graph>& g)
sl@0
   556
    {
sl@0
   557
      if (g.is_root()) {
sl@0
   558
        remove_edge(u_global, v_global, g.m_graph);
sl@0
   559
        children_remove_edge(u_global, v_global, g.m_children);
sl@0
   560
      } else
sl@0
   561
        remove_edge_recur_up(u_global, v_global, *g.m_parent);
sl@0
   562
    }
sl@0
   563
sl@0
   564
    //-------------------------------------------------------------------------
sl@0
   565
    // implementation of remove_edge(e,g)
sl@0
   566
sl@0
   567
    template <typename Edge, typename Graph>
sl@0
   568
    void remove_edge_recur_down(Edge e_global, subgraph<Graph>& g);
sl@0
   569
sl@0
   570
    template <typename Edge, typename Children>
sl@0
   571
    void children_remove_edge(Edge e_global, Children& c)
sl@0
   572
    {
sl@0
   573
      for (typename Children::iterator i = c.begin(); i != c.end(); ++i)
sl@0
   574
        if ((*i)->find_vertex(source(e_global, **i)).second
sl@0
   575
            && (*i)->find_vertex(target(e_global, **i)).second)
sl@0
   576
          remove_edge_recur_down(source(e_global, **i),
sl@0
   577
                                 target(e_global, **i), **i);
sl@0
   578
    }
sl@0
   579
sl@0
   580
    template <typename Edge, typename Graph>
sl@0
   581
    void remove_edge_recur_down(Edge e_global, subgraph<Graph>& g)
sl@0
   582
    {
sl@0
   583
      remove_edge(g.global_to_local(e_global), g.m_graph);
sl@0
   584
      children_remove_edge(e_global, g.m_children);
sl@0
   585
    }
sl@0
   586
sl@0
   587
    template <typename Edge, typename Graph>
sl@0
   588
    void remove_edge_recur_up(Edge e_global, subgraph<Graph>& g)
sl@0
   589
    {
sl@0
   590
      if (g.is_root()) {
sl@0
   591
        remove_edge(e_global, g.m_graph);
sl@0
   592
        children_remove_edge(e_global, g.m_children);
sl@0
   593
      } else
sl@0
   594
        remove_edge_recur_up(e_global, *g.m_parent);
sl@0
   595
    }
sl@0
   596
sl@0
   597
  } // namespace detail
sl@0
   598
sl@0
   599
  template <typename G>
sl@0
   600
  void
sl@0
   601
  remove_edge(typename subgraph<G>::vertex_descriptor u_local,
sl@0
   602
              typename subgraph<G>::vertex_descriptor v_local,
sl@0
   603
              subgraph<G>& g)
sl@0
   604
  {
sl@0
   605
    if (g.is_root())
sl@0
   606
      detail::remove_edge_recur_up(u_local, v_local, g);
sl@0
   607
    else
sl@0
   608
      detail::remove_edge_recur_up(g.local_to_global(u_local),
sl@0
   609
                                   g.local_to_global(v_local), g);
sl@0
   610
  }
sl@0
   611
sl@0
   612
  template <typename G>
sl@0
   613
  void
sl@0
   614
  remove_edge(typename subgraph<G>::edge_descriptor e_local,
sl@0
   615
              subgraph<G>& g)
sl@0
   616
  {
sl@0
   617
    if (g.is_root())
sl@0
   618
      detail::remove_edge_recur_up(e_local, g);
sl@0
   619
    else
sl@0
   620
      detail::remove_edge_recur_up(g.local_to_global(e_local), g);
sl@0
   621
  }
sl@0
   622
sl@0
   623
  template <typename Predicate, typename G>
sl@0
   624
  void
sl@0
   625
  remove_edge_if(Predicate p, subgraph<G>& g)
sl@0
   626
  {
sl@0
   627
    // This is wrong...
sl@0
   628
    remove_edge_if(p, g.m_graph);
sl@0
   629
  }
sl@0
   630
sl@0
   631
  template <typename G>
sl@0
   632
  void
sl@0
   633
  clear_vertex(typename subgraph<G>::vertex_descriptor v_local,
sl@0
   634
               subgraph<G>& g)
sl@0
   635
  {
sl@0
   636
    // this is wrong...
sl@0
   637
    clear_vertex(v_local, g.m_graph);
sl@0
   638
  }
sl@0
   639
sl@0
   640
  namespace detail {
sl@0
   641
sl@0
   642
    template <typename G>
sl@0
   643
    typename subgraph<G>::vertex_descriptor
sl@0
   644
    add_vertex_recur_up(subgraph<G>& g)
sl@0
   645
    {
sl@0
   646
      typename subgraph<G>::vertex_descriptor u_local, u_global;
sl@0
   647
      if (g.is_root()) {
sl@0
   648
        u_global = add_vertex(g.m_graph);
sl@0
   649
        g.m_global_vertex.push_back(u_global);
sl@0
   650
      } else {
sl@0
   651
        u_global = add_vertex_recur_up(*g.m_parent);
sl@0
   652
        u_local = add_vertex(g.m_graph);
sl@0
   653
        g.m_global_vertex.push_back(u_global);
sl@0
   654
        g.m_local_vertex[u_global] = u_local;
sl@0
   655
      }
sl@0
   656
      return u_global;
sl@0
   657
    }
sl@0
   658
sl@0
   659
  } // namespace detail
sl@0
   660
sl@0
   661
  template <typename G>
sl@0
   662
  typename subgraph<G>::vertex_descriptor
sl@0
   663
  add_vertex(subgraph<G>& g)
sl@0
   664
  {
sl@0
   665
    typename subgraph<G>::vertex_descriptor  u_local, u_global;
sl@0
   666
    if (g.is_root()) {
sl@0
   667
      u_global = add_vertex(g.m_graph);
sl@0
   668
      g.m_global_vertex.push_back(u_global);
sl@0
   669
      u_local = u_global;
sl@0
   670
    } else {
sl@0
   671
      u_global = detail::add_vertex_recur_up(g.parent());
sl@0
   672
      u_local = add_vertex(g.m_graph);
sl@0
   673
      g.m_global_vertex.push_back(u_global);
sl@0
   674
      g.m_local_vertex[u_global] = u_local;
sl@0
   675
    }
sl@0
   676
    return u_local;
sl@0
   677
  }
sl@0
   678
sl@0
   679
  template <typename G>
sl@0
   680
  void remove_vertex(typename subgraph<G>::vertex_descriptor u,
sl@0
   681
                     subgraph<G>& g)
sl@0
   682
  {
sl@0
   683
    // UNDER CONSTRUCTION
sl@0
   684
    assert(false);
sl@0
   685
  }
sl@0
   686
sl@0
   687
sl@0
   688
  //===========================================================================
sl@0
   689
  // Functions required by the PropertyGraph concept
sl@0
   690
sl@0
   691
  template <typename GraphPtr, typename PropertyMap, typename Tag>
sl@0
   692
  class subgraph_global_property_map
sl@0
   693
    : public put_get_helper<
sl@0
   694
        typename property_traits<PropertyMap>::reference,
sl@0
   695
        subgraph_global_property_map<GraphPtr, PropertyMap, Tag> >
sl@0
   696
  {
sl@0
   697
    typedef property_traits<PropertyMap> Traits;
sl@0
   698
  public:
sl@0
   699
    typedef typename Traits::category category;
sl@0
   700
    typedef typename Traits::value_type value_type;
sl@0
   701
    typedef typename Traits::key_type key_type;
sl@0
   702
    typedef typename Traits::reference reference;
sl@0
   703
sl@0
   704
    subgraph_global_property_map() { }
sl@0
   705
sl@0
   706
    subgraph_global_property_map(GraphPtr g)
sl@0
   707
      : m_g(g) { }
sl@0
   708
sl@0
   709
    inline reference operator[](key_type e_local) const {
sl@0
   710
      PropertyMap pmap = get(Tag(), m_g->root().m_graph);
sl@0
   711
      if (m_g->m_parent == 0)
sl@0
   712
        return pmap[e_local];
sl@0
   713
      else
sl@0
   714
        return pmap[m_g->local_to_global(e_local)];
sl@0
   715
    }
sl@0
   716
    GraphPtr m_g;
sl@0
   717
  };
sl@0
   718
sl@0
   719
  template <typename GraphPtr, typename PropertyMap, typename Tag>
sl@0
   720
  class subgraph_local_property_map
sl@0
   721
    : public put_get_helper<
sl@0
   722
        typename property_traits<PropertyMap>::reference,
sl@0
   723
        subgraph_local_property_map<GraphPtr, PropertyMap, Tag> >
sl@0
   724
  {
sl@0
   725
    typedef property_traits<PropertyMap> Traits;
sl@0
   726
  public:
sl@0
   727
    typedef typename Traits::category category;
sl@0
   728
    typedef typename Traits::value_type value_type;
sl@0
   729
    typedef typename Traits::key_type key_type;
sl@0
   730
    typedef typename Traits::reference reference;
sl@0
   731
sl@0
   732
    subgraph_local_property_map() { }
sl@0
   733
sl@0
   734
    subgraph_local_property_map(GraphPtr g)
sl@0
   735
      : m_g(g) { }
sl@0
   736
sl@0
   737
    inline reference operator[](key_type e_local) const {
sl@0
   738
      PropertyMap pmap = get(Tag(), *m_g);
sl@0
   739
      return pmap[e_local];
sl@0
   740
    }
sl@0
   741
    GraphPtr m_g;
sl@0
   742
  };
sl@0
   743
sl@0
   744
  namespace detail {
sl@0
   745
sl@0
   746
    struct subgraph_any_pmap {
sl@0
   747
      template <class Tag, class SubGraph, class Property>
sl@0
   748
      class bind_ {
sl@0
   749
        typedef typename SubGraph::graph_type Graph;
sl@0
   750
        typedef SubGraph* SubGraphPtr;
sl@0
   751
        typedef const SubGraph* const_SubGraphPtr;
sl@0
   752
        typedef typename property_map<Graph, Tag>::type PMap;
sl@0
   753
        typedef typename property_map<Graph, Tag>::const_type const_PMap;
sl@0
   754
      public:
sl@0
   755
        typedef subgraph_global_property_map<SubGraphPtr, PMap, Tag> type;
sl@0
   756
        typedef subgraph_global_property_map<const_SubGraphPtr, const_PMap, Tag>
sl@0
   757
          const_type;
sl@0
   758
      };
sl@0
   759
    };
sl@0
   760
    struct subgraph_id_pmap {
sl@0
   761
      template <class Tag, class SubGraph, class Property>
sl@0
   762
      struct bind_ {
sl@0
   763
        typedef typename SubGraph::graph_type Graph;
sl@0
   764
        typedef SubGraph* SubGraphPtr;
sl@0
   765
        typedef const SubGraph* const_SubGraphPtr;
sl@0
   766
        typedef typename property_map<Graph, Tag>::type PMap;
sl@0
   767
        typedef typename property_map<Graph, Tag>::const_type const_PMap;
sl@0
   768
      public:
sl@0
   769
        typedef subgraph_local_property_map<SubGraphPtr, PMap, Tag> type;
sl@0
   770
        typedef subgraph_local_property_map<const_SubGraphPtr, const_PMap, Tag>
sl@0
   771
          const_type;
sl@0
   772
      };
sl@0
   773
    };
sl@0
   774
    template <class Tag>
sl@0
   775
    struct subgraph_choose_pmap_helper {
sl@0
   776
      typedef subgraph_any_pmap type;
sl@0
   777
    };
sl@0
   778
    template <>
sl@0
   779
    struct subgraph_choose_pmap_helper<vertex_index_t> {
sl@0
   780
      typedef subgraph_id_pmap type;
sl@0
   781
    };
sl@0
   782
    template <class Tag, class Graph, class Property>
sl@0
   783
    struct subgraph_choose_pmap {
sl@0
   784
      typedef typename subgraph_choose_pmap_helper<Tag>::type Helper;
sl@0
   785
      typedef typename Helper::template bind_<Tag, Graph, Property> Bind;
sl@0
   786
      typedef typename Bind::type type;
sl@0
   787
      typedef typename Bind::const_type const_type;
sl@0
   788
    };
sl@0
   789
    struct subgraph_property_generator {
sl@0
   790
      template <class SubGraph, class Property, class Tag>
sl@0
   791
      struct bind_ {
sl@0
   792
        typedef subgraph_choose_pmap<Tag, SubGraph, Property> Choice;
sl@0
   793
        typedef typename Choice::type type;
sl@0
   794
        typedef typename Choice::const_type const_type;
sl@0
   795
      };
sl@0
   796
    };
sl@0
   797
sl@0
   798
  } // namespace detail
sl@0
   799
sl@0
   800
  template <>
sl@0
   801
  struct vertex_property_selector<subgraph_tag> {
sl@0
   802
    typedef detail::subgraph_property_generator type;
sl@0
   803
  };
sl@0
   804
sl@0
   805
  template <>
sl@0
   806
  struct edge_property_selector<subgraph_tag> {
sl@0
   807
    typedef detail::subgraph_property_generator type;
sl@0
   808
  };
sl@0
   809
sl@0
   810
  template <typename G, typename Property>
sl@0
   811
  typename property_map< subgraph<G>, Property>::type
sl@0
   812
  get(Property, subgraph<G>& g)
sl@0
   813
  {
sl@0
   814
    typedef typename property_map< subgraph<G>, Property>::type PMap;
sl@0
   815
    return PMap(&g);
sl@0
   816
  }
sl@0
   817
sl@0
   818
  template <typename G, typename Property>
sl@0
   819
  typename property_map< subgraph<G>, Property>::const_type
sl@0
   820
  get(Property, const subgraph<G>& g)
sl@0
   821
  {
sl@0
   822
    typedef typename property_map< subgraph<G>, Property>::const_type PMap;
sl@0
   823
    return PMap(&g);
sl@0
   824
  }
sl@0
   825
sl@0
   826
  template <typename G, typename Property, typename Key>
sl@0
   827
  typename property_traits<
sl@0
   828
    typename property_map< subgraph<G>, Property>::const_type
sl@0
   829
  >::value_type
sl@0
   830
  get(Property, const subgraph<G>& g, const Key& k)
sl@0
   831
  {
sl@0
   832
    typedef typename property_map< subgraph<G>, Property>::const_type PMap;
sl@0
   833
    PMap pmap(&g);
sl@0
   834
    return pmap[k];
sl@0
   835
  }
sl@0
   836
sl@0
   837
  template <typename G, typename Property, typename Key, typename Value>
sl@0
   838
  void
sl@0
   839
  put(Property, subgraph<G>& g, const Key& k, const Value& val)
sl@0
   840
  {
sl@0
   841
    typedef typename property_map< subgraph<G>, Property>::type PMap;
sl@0
   842
    PMap pmap(&g);
sl@0
   843
    pmap[k] = val;
sl@0
   844
  }
sl@0
   845
sl@0
   846
  template <typename G, typename Tag>
sl@0
   847
  inline
sl@0
   848
  typename graph_property<G, Tag>::type&
sl@0
   849
  get_property(subgraph<G>& g, Tag tag) {
sl@0
   850
    return get_property(g.m_graph, tag);
sl@0
   851
  }
sl@0
   852
sl@0
   853
  template <typename G, typename Tag>
sl@0
   854
  inline
sl@0
   855
  const typename graph_property<G, Tag>::type&
sl@0
   856
  get_property(const subgraph<G>& g, Tag tag) {
sl@0
   857
    return get_property(g.m_graph, tag);
sl@0
   858
  }
sl@0
   859
sl@0
   860
  //===========================================================================
sl@0
   861
  // Miscellaneous Functions
sl@0
   862
sl@0
   863
  template <typename G>
sl@0
   864
  typename subgraph<G>::vertex_descriptor
sl@0
   865
  vertex(typename subgraph<G>::vertices_size_type n, const subgraph<G>& g)
sl@0
   866
  {
sl@0
   867
    return vertex(n, g.m_graph);
sl@0
   868
  }
sl@0
   869
sl@0
   870
} // namespace boost
sl@0
   871
sl@0
   872
#endif // BOOST_SUBGRAPH_HPP