【问题标题】:How to permute nodes in a boost::adjacency_list?如何置换 boost::adjacency_list 中的节点?
【发布时间】:2012-01-22 12:12:57
【问题描述】:

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

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

感谢任何想法/代码如何进行此排列。

【问题讨论】:

    标签: c++ boost permutation boost-graph


    【解决方案1】:

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

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

    【讨论】:

    • 我已将您的代码复制到我的后面,修复了两个小错误。尽管如此,它还是没有找到 make_transform_iterator。所以我包括#include &lt;boost/iterator/transform_iterator.hpp&gt;。但现在我得到no matching function for call to make_transform_iterator(.......)。是不是标错了?
    • 你完成using namespace boost;了吗?如果没有,您将需要限定对make_transform_iterator 的调用。
    • 是的,我已经这样做了。我可以上传整个代码,但它需要超过 20 行...
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-12
    • 2012-08-13
    • 2021-06-13
    • 1970-01-01
    • 1970-01-01
    • 2011-11-23
    相关资源
    最近更新 更多