c++ - 如何在 C++ 中使用 igraph_add_vertices 添加顶点后获取 igraph 顶点 ID

标签 c++ igraph vertex

我想在将单个顶点添加到现有图形后获取 VID(顶点 ID)。我当前在添加新顶点后得到一个 vertex_set 并循环到顶点集的末尾(假设这始终是最后添加的顶点,即使在较早的顶点被删除的情况下也是如此?)。我需要测试从集合中间删除一个顶点是否仍然会改变 VID。但我确信必须有更好的(阅读更有效的方法)来做到这一点。下面的代码是我目前使用的。

感谢任何帮助,因为我是 iGraph 的新手。

// add into graph
igraph_integer_t t = 1;
if(igraph_add_vertices(user_graph, t, 0) != 0)
{
    ::MessageBoxW(NULL, L"Failed to add vertex to iGraph, vertex not added.", L"Network Model", MB_ICONSTOP);
    return false;
}

/* get all verticies */
igraph_vs_t vertex_set;
igraph_vit_t vit;
igraph_integer_t vid = 0;

igraph_vs_all(&vertex_set);
igraph_vit_create(user_graph, vertex_set, &vit);


// must be a better way - look for starting from end.
while (!IGRAPH_VIT_END(vit)) 
{
    vid = IGRAPH_VIT_GET(vit);
    IGRAPH_VIT_NEXT(vit);
}

// add vid to vertex ca
ca->graphid = (int)vid;

// Add new vertex to local store
vm->CreateVertex(ca);   

// cleanup
igraph_vit_destroy(&vit);
igraph_vs_destroy(&vertex_set);

最佳答案

igraph 中的顶点 ID(以及边 ID)是从零到顶点/边数减一的整数。因此,如果您添加新的顶点或边,其 ID 将始终等于添加之前的顶点/边数。另外,如果删除一些边,现有边的ID将重新排列,使边ID范围再次连续。同样适用于删除顶点,并注意删除一些顶点也会重新排列边 ID,除非删除的顶点被隔离。

关于c++ - 如何在 C++ 中使用 igraph_add_vertices 添加顶点后获取 igraph 顶点 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16148808/

相关文章:

c++ - 矩阵模板库矩阵求逆

c++ - 如何使 tab 键在非对话框的 win32 窗口中工作

c++ - 修改 2D Array 的 malloc 策略,使 malloc 成功

python - 删除两个顶点之间的边[igraph python]

java - 荣格图库: how to search vertices based on vertex property?

c++ - std::rethrow_exception(nullptr) 未定义行为或 bad_exception?

python - igraph 不能使用 GLPK

r - igraph + R 顶点按条件着色(最好是连续颜色)

OpenGL:在顶点着色器中使用高度图进行地形变形

c - 我应该为 Sprite 创建什么样的顶点?