g.m_vertices.size()/4;是正确的解决方案吗?
这仅取决于您的要求。
如果最初我有 10 个顶点,那么我删除中间的一些顶点(例如 4 个),只剩下 6 个顶点(所以这是新大小),但顶点的索引从 0 到 5 或从 0 到9?
这取决于您的图形模型。您没有指定图表的类型(我知道,您确实说的是哪个模板,而不是模板参数)。假设顶点容器选择器为 vecS,那么是的,在 4 次删除后,顶点描述符(和索引)将为 [0,6)。
如何只将顶点的子集传递给vp而不是传递顶点(g)
很多方法。
- 您可以
std::for_each 使用并行执行策略
- 您可以使用 openmp 从普通循环创建并行部分
- 您可以使用
filtered_graph 适配器创建底层图形的 4 个“视图”并对其进行操作
- 您可以使用PBGL,它实际上是为处理大图而创建的。这具有额外的好处,它可以与线程/进程间/主机间通信一起使用,可以跨段协调算法等。
- 您可以使用
sub_graphs;如果您的图表构建方式具有自然分割,这主要(仅)有趣
没有一个解决方案是微不足道的。但是,这里是使用filtered_graph 的简单演示:
Live On Compiler Explorer
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/filtered_graph.hpp>
#include <boost/graph/random.hpp>
#include <iostream>
#include <random>
using G = boost::adjacency_list<>;
using V = G::vertex_descriptor;
G make_graph() {
G g;
std::mt19937 prng(std::random_device{}());
generate_random_graph(g, 32 * 1024 - (prng() % 37), 64 * 1024, prng);
return g;
}
template <int NSegments, int Segment> struct SegmentVertices {
std::hash<V> _h;
bool operator()(V vd) const { return (_h(vd) % NSegments) == Segment; }
};
template <int N>
using Quart = boost::filtered_graph<G, boost::keep_all, SegmentVertices<4, N>>;
template <typename Graph>
void the_function(Graph const& g, std::string_view name)
{
std::cout << name << " " << size(boost::make_iterator_range(vertices(g)))
<< " vertices\n";
}
int main()
{
G g = make_graph();
the_function(g, "full graph");
Quart<0> f0(g, {}, {});
Quart<1> f1(g, {}, {});
Quart<2> f2(g, {}, {});
Quart<3> f3(g, {}, {});
the_function(f0, "f0");
the_function(f1, "f1");
the_function(f2, "f2");
the_function(f3, "f3");
}
打印例如
full graph 32766 vertices
f0 8192 vertices
f1 8192 vertices
f2 8191 vertices
f3 8191 vertices