5

问题:我想.csv使用 igraph 从存储在文件中的邻接矩阵制作加权无向图,然后对其进行最小生成树和其他一些算法。

我开始制作一个有 10 个顶点和 5 个边的有向图。默认情况下,igraph 不允许边缘的权重,您必须igraph_i_set_attribute_table在文档中使用一些对我没有意义的属性(类似于 )。

有人可以帮我解决这个问题。

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;
}
4

1 回答 1

3

首先,一般来说,使用 R 或 Python 中的 igraph 会更好,属性得到更好的支持。例如,您可以轻松地根据属性值等选择顶点或边。在 C 中,对属性的支持很少,而且在使用它们时,您主要靠自己。所以,除非你真的需要 C,否则我建议使用 R 或 Python。

如果你仍然想要 C,那么首先要记住的是你需要包含

igraph_i_set_attribute_table(&igraph_cattribute_table);

在您显式或隐式地对属性执行任何操作之前,在您的代码中。IE。即使您没有显式操作属性,而是创建一个可能具有某些属性的图形,例如。read a graph a GraphML file,之前需要这个调用,否则属性会被丢弃。最好在main()函数的开头包含调用。

您必须使用任何属性是不正确的,我不确定您的意思。

至于设置和查询属性,请看examples/simple目录下的文件:

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

这些示例很大程度上是人为的,因为它们主要用于测试目的,但它们显示了基本用法。

于 2013-03-27T11:30:03.510 回答