c++ - 如何置换 boost::adjacency_list 中的节点?

标签 c++ boost permutation boost-graph

伪装以下类型定义和定义:

#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,但我不知道如何使用它(甚至只包含此文件的错误,与其他 header 冲突)。

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

最佳答案

如果您可以忍受创建图表的置换拷贝,则可以使用迭代器范围创建新图表:

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

关于c++ - 如何置换 boost::adjacency_list 中的节点?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8960949/

相关文章:

c++ - boost::asio 和同步读取导致编译错误

C++ Boost 和 Lzma 解压

c++ - 使用 Boost C++ 为嵌套标签解析 xml

algorithm - 在给定字符串排列的排序列表中查找给定排列的索引

python - np.random.permutation 与种子?

C++循环遍历目录中的文件

c++ - 将数据从一个类传递到另一个类而不传递对象

javascript - 为什么我的排列算法对所有排列给出相同的结果?

c++ - 右值引用转换后的地址变化

c++ - 通过指向派生类的指针访问派生成员变量