如何排列 boost::adjacency_list 中的节点?

发布于 2024-12-28 08:11:55 字数 892 浏览 3 评论 0原文

假设以下 typedef 和定义:

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

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

三五鸿雁 2025-01-04 08:11:55

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

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文