【问题标题】:Boost graph library: Get the order number of a specific vertex?Boost 图形库:获取特定顶点的序号?
【发布时间】:2015-01-29 01:22:39
【问题描述】:

Boost图形库有一个函数vertex_descriptor vertex(vertices_size_type n, const adjacency_list& g),它返回第n个vertex_descriptor。这个函数的反函数是什么?即获取给定vertex_descriptor的订单号?

【问题讨论】:

  • Vertex Order 表示图论中的其他含义:“在图中给定节点处相遇的图边的数量称为该图顶点的顺序。”
  • 你最好称它为 index 之类的。 “订单”已经被用来表示其他东西,而“订单号”从来都不是在集合中讨论某物索引的明确方式。
  • @Lightness Races in Orbit 谢谢。我之所以叫它这个名字,是因为这个库中有一个vertex_index_t 内部属性。

标签: boost


【解决方案1】:

一般来说,没有一种高效的方法,因为它取决于顶点的存储方式。

如果你不关心性能,你可以创建一个这样的函数:

template <typename Graph>
size_t index_of(Graph const& g, typename Graph::vertex_descriptor const& vd)
{
    auto vs = vertices(g);
    auto lookup = std::find(vs.first, vs.second, vd);
    assert(lookup != vs.second);

    return std::distance(vs.first, lookup);
}

在下面的“综合”测试中,它被证明适用于adjacency_list&lt;&gt; 图形类型的多种不同配置。

使用vecS 存储时,上述性能应该是合理的,否则可能会很糟糕。在这种情况下,最好提供自己的vertex_index 映射,因为许多其他算法确实需要(例如write_graphviz):


测试程序

Live On Coliru

#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/random.hpp>
#include <boost/random.hpp>

using namespace boost;

template <typename Graph>
size_t index_of(Graph const& g, typename Graph::vertex_descriptor const& vd)
{
    auto vs = vertices(g);
    auto lookup = std::find(vs.first, vs.second, vd);
    assert(lookup != vs.second);

    return std::distance(vs.first, lookup);
}

static mt19937 rnd(time(0));

template <typename Graph, size_t num_vertices = 200, size_t num_edges = 300>
void test_index_of()
{
    Graph g;
    generate_random_graph(g, 100, 300, rnd);

    for(auto const& v : make_iterator_range(vertices(g))) {
        assert(v == boost::vertex(index_of(g, v), g));
    }
}

int main()
{
    test_index_of<adjacency_list<vecS,  vecS,  undirectedS> >();
    test_index_of<adjacency_list<vecS,  setS,  undirectedS> >();
    test_index_of<adjacency_list<vecS,  listS, undirectedS> >();
    test_index_of<adjacency_list<setS,  vecS,  undirectedS> >();
    test_index_of<adjacency_list<setS,  setS,  undirectedS> >();
    test_index_of<adjacency_list<setS,  listS, undirectedS> >();
    test_index_of<adjacency_list<listS, vecS,  undirectedS> >();
    test_index_of<adjacency_list<listS, setS,  undirectedS> >();
    test_index_of<adjacency_list<listS, listS, undirectedS> >();
    test_index_of<adjacency_list<vecS,  vecS,  directedS>   >();
    test_index_of<adjacency_list<vecS,  setS,  directedS>   >();
    test_index_of<adjacency_list<vecS,  listS, directedS>   >();
    test_index_of<adjacency_list<setS,  vecS,  directedS>   >();
    test_index_of<adjacency_list<setS,  setS,  directedS>   >();
    test_index_of<adjacency_list<setS,  listS, directedS>   >();
    test_index_of<adjacency_list<listS, vecS,  directedS>   >();
    test_index_of<adjacency_list<listS, setS,  directedS>   >();
    test_index_of<adjacency_list<listS, listS, directedS>   >();
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-21
    相关资源
    最近更新 更多