使用非连接存储(即!= vecs)和add_Edge()的最小boost aidgacency_list

Minimal boost adjacency_list with non-contiguous storage (i.e. != vecS) and add_edge()

本文关键字:Edge list aidgacency boost add 存储 vecs 连接      更新时间:2023-10-16

我想为算法创建图形,该算法需要仅由adjacency_list提供的图形概念。顶点ID本身是随机的size_t's且无连接,因此使用向量作为基础存储是不可能的,但这确实是不是 compile:

#include <boost/graph/adjacency_list.hpp>
int main()
{
  using namespace boost;
  using out_edge_storage = setS;
//  using vertex_storage = vecS; // compiles Ok
  using vertex_storage = setS; // error: no matching function for call to 'add_edge'
  using graph = adjacency_list<out_edge_storage, vertex_storage, undirectedS>;
  graph g;
  add_edge(3, 44, g);
  add_edge(1024102400, 3, g); // uses too much space (bad_alloc) with vecS
}

我不需要任何额外的自定义顶点属性,也不需要在创建图表后修改图形。阅读文档[1]我找不到add_edge()的额外要求的原因。

我将如何使用集合或哈希集合数据类型构造图形,并且在文档中可以找到我错过的详细信息?

/doc/adjacency_list.html

(有关Adjacency_list VEC的其他stackoverflow问题(例如,此处(远非最小,没有帮助。(

我不需要任何额外的自定义顶点属性,也不需要在创建图表后修改图形。

好吧,也许不是在您的脑海中,但是由于向量索引不再以顶点ID的形式"倍增",因此您希望某个地方将这些数字附加到顶点描述符。

恰好是您需要/渴望财产的理由。我建议内部属性如果您想要算法也会自动知道如何使用该数字来识别您的索引。

活在coliru

#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/graph_utility.hpp>
using graph = boost::adjacency_list<boost::setS, boost::setS, boost::undirectedS, 
    boost::property<boost::vertex_index_t, size_t> >;
int main() {
    graph g;
    auto A = add_vertex(3, g);
    auto B = add_vertex(44, g);
    auto C = add_vertex(1024102400, g);
    add_edge(A, B, g);
    add_edge(C, A, g);
    print_graph(g);
}

打印:

3 <--> 44 1024102400 
44 <--> 3 
1024102400 <--> 3