首页 文章

如何在boost :: adjacency_list中置换节点?

提问于
浏览
1

假装以下typedef和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);
}

我有一个输出g和g_to_tc_map的算法(如上所述) . 现在,我需要通过g_to_tc_map来置换节点(我认为,这类似于整数数组或std :: map) .

注意:我发现有一个boost / graph / detail / permutation.hpp,但我不知道如何使用它(甚至只包含此文件的bug,与其他头文件冲突) .

感谢任何想法/代码如何做这个排列 .

1 回答

  • 0

    如果您可以使用创建图表的置换副本,则可以使用迭代器范围创建新图形:

    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:在 vecS 顶点容器的图形中不需要 vertex_index_t 属性;它是自动创建(并填写) .

相关问题