5
votes

Problem: I want to make a weighted undirected graph from adjacency matrix stored in a .csv file using igraph and then do the minimum spanning tree and some other algorithms on it.

I started with making a directed graph with 10 vertices and 5 edges. By default igraph does not allow weights for edges and you have to use some attributes which does not make sense to me (something like igraph_i_set_attribute_table) in the documentation.

Can someone please help me regarding this.

void print_vector(igraph_vector_t *v, FILE *f) {
  long int i;
  for (i=0; i<igraph_vector_size(v); i++) {
    fprintf(f, " %li", (long int) VECTOR(*v)[i]);
  }
  fprintf(f, "\n");
}

int main(int argc, char* argv[])
{
  igraph_t g;
  igraph_vector_t v;
  int ret;
  igraph_es_t es;

  /* Initialize the vector for edges */
  igraph_vector_init(&v,10);

  VECTOR(v)[0]=0;VECTOR(v)[1]=1;
  VECTOR(v)[2]=1;VECTOR(v)[3]=3;
  VECTOR(v)[4]=1;VECTOR(v)[5]=5;
  VECTOR(v)[6]=2;VECTOR(v)[7]=3;
  VECTOR(v)[8]=2;VECTOR(v)[9]=5;

  igraph_create(&g,&v,0,IGRAPH_DIRECTED);

  print_vector(&v,stdout);

  /* igraph_i_set_attribute_table(&igraph_cattribute_table); */

  igraph_vector_destroy(&v);
  igraph_destroy(&g);

  return 0;
}
1

1 Answers

3
votes

First of all, in general you are much better off using igraph from R or Python, attributes are much-much better supported. For example you can easily select vertices or edges based on attribute values, etc. In C, the support for attributes is minimal, and you are mostly on your own, when it comes to working with them. So, unless you really need C, I would suggest to use R or Python.

If you still want C, then the first thing to keep in mind is that you need to include

igraph_i_set_attribute_table(&igraph_cattribute_table);

in your code before you do anything with attribute explicitly or implicitly. Ie. even if you don't manipulate attributes explicitly, but create a graph that might have some attributes, eg. read a graph a GraphML file, you need this call before, otherwise the attributes are dropped. It is best to include the call at the beginning of your main() function.

It is not true that you have to use any attributes, I am not sure what you mean by that.

As for setting and querying the attributes, see the files in the examples/simple directory:

https://github.com/igraph/igraph/blob/master/examples/simple/cattributes.c https://github.com/igraph/igraph/blob/master/examples/simple/cattributes2.c https://github.com/igraph/igraph/blob/master/examples/simple/cattributes3.c https://github.com/igraph/igraph/blob/master/examples/simple/cattributes4.c

These examples are largely artificial, because they are mainly used for testing purposes, but they show the basic usage.