How to include edge attributes when calling add_edges()?

My program contains a dynamic graph, and edges of various kinds are added and removed over the lifecycle of the program. I would like to be able to include edge attributes in the same call as add_edges. I can see that this is possible from the documentation, however I can’t quite figure out how to do this. Of course, can add the attribute in after adding the edges, but I think it would streamline things considerable to do all at once

Could someone point me to an example or to the appropriate documentation?

Thanks!

The documentation is indeed quite sparse on using attributes. A partial rework of attribute handling is one of the big ticket items for version 0.11, though the work hasn’t started yet. The docs should be improved at the same time.

For now I recommend that you look at some of the import/export functions for usage examples, for example igraph_read_graph_gml(). While it’s not the simplest reader, here you can follow the relevant part of the code without having to look into Bison source files. igraph/gml.c at master · igraph/igraph · GitHub

In short:

  • attr must be a pointer to an igraph_vector_ptr_t, where each entry points to an attribute record.
  • Attribute records are represented as igraph_attribute_record_t, with the following fields:
    • name is a null-terminated string, the attribute name
    • type should be one of IGRAPH_ATTRIBUTE_NUMERIC, IGRAPH_ATTRIBUTE_BOOLEAN or IGRAPH_ATTRIBUTE_STRING
    • value will be a pointer to corresponding igraph vector type, i.e. igraph_vector_t, igraph_vector_bool_t or igraph_strvector_t. It must have an appropriate length, matching the number of vertices for vertex attribute, the number of edges for edge attributes, or 1 for graph attributes.
  • All the data passed in through attr will be copied. In other words, its ownership does not change. You still own the data and you are responsible for freeing it after the igraph_add_edges() call.

Thank you - this is extremely helpful! I will try to implement this later today, and will write back with any questions. I appreciate your time.