How can I query a vertex/edge according to its attribute?

Hello everyone! I’m eager to know how can I query a vertex/edge according to its attribute.
For detail, I try to module a communication network with igraph. I create a graph with several vertexes. But when I delete a vertex, some of other vertexes’ vid will be changed. So I must attach attribute to each vertex as its’ ID(Maybe there are methods can fix it up). I can attach the attribute, but I don’t know how to query a vertex/edge according to its attribute.
For help, thank you!

Based on the tag you used, I assume you are programming in C.

I would approach this by maintaining a mapping between your original IDs, and the IDs after some vertices of the graph have been deleted. It will be convenient to also maintain a reverse mapping from the new IDs to the original IDs.

The igraph_induced_subgraph_map() and igraph_delete_vertices_idx() functions use mappings like these, though currently they employ 1-based indexing. This will be changed to the more usual 0-based indexing in version 0.11 (see 0.11 changelog). You may be looking for these functions.

The direct mapping (original IDs to new IDs) is something you need to maintain manually. The inverse mapping can be stored as a vertex attribute (in which case vertex deletions are handled automatically), or you may opt to also maintain it manually.

It might also be interesting to look into implementing a custom attribute handler for your use-case by extending the existing C attribute handler functions. An attribute handler is essentially a set of “event listeners” that get called when a graph is created or its vertices or edges are updated, so you should be able to get all the information that you need to update your own mappings from the attribute handler functions themselves.

Thank you for your help!
There is still a question. How can i maintain the direct map manually?
For example, a graphy map with vertexes and vids like {a-0,b-1,c-2,d-3,e-4,f-5} {name-vid}, delete “b” and “e” vertex, the vertexes left are {a-0,c-1,d-2,f-3} ? Then if I add a new vertex “g”, the map will be {a-0,c-1,d-2,f-3,g-4}?
Furthemore, what the edge’s rules? If I add a new edge between 2 vertex, how can I get the new edge’s eid immediately after I create it? If I delete a edge, Are the other edges’ eid changed?

Based on your first post, I assumed you wanted to work with stable integer IDs that do not change during deletion, and that you did not need to deal with addition of new vertices.

Your new question is different, as you are asking to refer to vertices by a string, and you want to handle additions as well.

My advice for this situation would be to consider very carefully if you truly need to program igraph from C. One of the high high-level interfaces (in R, Python or Mathematica) would be a better fit.

If you need to use C, then frankly the only fully general solution is to implement your own attribute handler, as Tamás suggested, and add lookup functionality to it. This would not be a quick and easy task, and I would only recommend it if you are very comfortable with C. You will need some sort of map data structure for efficient name lookup. std::map would be the easiest but it requires C++.

If O(|V|) name lookup is good enough for your application, then you can use the standard C attribute handler and simply loop over the string attribute vector to find names.

If you have a more specific task in front of you, that does not require fully general lookup based on string names, including support for both deletion and addition as well as checking for duplicate names, then there may be easier solutions. To be able to give advice, you’d need to describe your specific problem though.