简体   繁体   中英

How to permute nodes in a boost::adjacency_list?

Pretend the following typedefs and defintiontions:

#include <boost/graph/adjacency_list.hpp>
using namespace boost;

int main()
{
    typedef adjacency_list<vecS, vecS, directedS, property<vertex_index_t, int> > GraphTC;
    GraphTC g;

    typedef typename property_map<GraphTC, vertex_index_t>::const_type VertexIndexMap;
    VertexIndexMap index_map = get(vertex_index, g);

    typedef typename graph_traits<GraphTC>::vertex_descriptor tc_vertex;
    std::vector<tc_vertex> to_tc_vec(num_vertices(g));

    iterator_property_map < tc_vertex *, VertexIndexMap, tc_vertex, tc_vertex&>
    g_to_tc_map(&to_tc_vec[0], index_map);
}

I have an algorithm that outputs me g and g_to_tc_map (as above). Now, I need to permute the nodes by g_to_tc_map (which is, I think, something like an integer array or a std::map).

Note: I have found that there is a boost/graph/detail/permutation.hpp, but I have no idea how to use it (getting even bugs only including this file, conflicting with other headers).

Thanks for any idea/code how to do this permutation.

If you can live with creating a permuted copy of the graph, you can create the new graph using an iterator range:

struct permute_edge {
  iterator_property_map < tc_vertex *, VertexIndexMap, tc_vertex, tc_vertex&> g_to_tc_map;
  const GraphTC& g;
  permute_edge(iterator_property_map < tc_vertex *, VertexIndexMap, tc_vertex, tc_vertex&> g_to_tc_map, const GraphTC& g): g_to_tc_map(g_to_tc_map), g(g) {}
  std::pair<tc_vertex, tc_vertex> operator()(graph_traits<Graph>::edge_descriptor e) const {
    return std::make_pair(get(g_to_tc_map, source(e, g)), get(g_to_tc_map, target(e, g));
  }
};

permute_edge perm(g_to_tc_map, g);
typedef graph_traits<GraphTC>::edge_iterator edge_iterator;
std::pair<edge_iterator, edge_iterator> g_edges = edges(g);
GraphTC g_permuted(
          make_transform_iterator(g_edges.first, perm),
          make_transform_iterator(g_edges.second, perm),
          num_vertices(g), num_edges(g));

PS: you don't need a vertex_index_t property in a graph with vecS vertex container; it is created (and filled in) automatically.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM