如何在boost::邻接列表中排列节点

How to permute nodes in a boost::adjacency_list?

本文关键字:列表 排列 节点 boost      更新时间:2023-10-16

假设以下类型定义和定义:

#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,与其他头文件冲突)。

感谢您提供任何关于如何进行此排列的想法/代码。

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

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属性;它是自动创建(并填充)的。