Question

I would like to get the VID (vertex ID) after I have added a single vertex to an existing graph. I current get a vertex_set after adding the new vertex and loop through to the end of the vertex set (assuming this is always the last added vertex even in the event of a earlier one being deleted?). I need to test if deleting a vertex from the middle of the set changes the VIDs still. But I am sure there must be a better (read more efficient way) of doing this.. The code below is what I currently use.

Any help appreciated as I am new to 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);
Was it helpful?

Solution

Vertex IDs (and also edge IDs) in igraph are integers from zero up to the number of vertices/edges minus one. Therefore, if you add a new vertex or edge, its ID will always be equal to the number of vertices/edges before the addition. Also, if you delete some edges, the IDs of existing edges will be re-arranged to make the edge ID range continuous again. Same applies for the deletion of vertices, and note that deleting some vertices will also re-arrange the edge IDs unless the deleted vertices were isolated.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top