2013-03-27 28 views
5

问题: 我想用的igraph使从存储在.csv文件邻接矩阵加权无向图,然后做最小生成树和它的一些其他算法。在“IGRAPH”创建一个加权无向图中的C/C++

我开始制作一个有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; 
} 

回答

3

首先,一般来说,使用R或Python的igraph要好得多,属性支持得更好。例如,您可以根据属性值等方便地选择顶点或边。在C中,对属性的支持很小,并且大多数情况下都是您自己操作的。所以,除非你真的需要C,否则我会建议使用R或Python。

如果你仍然想C,然后要记住的第一件事是,你需要包括

igraph_i_set_attribute_table(&igraph_cattribute_table); 

在你的代码,你做任何事情之前,属性明确或含蓄。 IE浏览器。即使您不明确操作属性,但创建了可能具有某些属性的图,例如。读一个图形GraphML文件,你需要这个调用之前,否则属性被删除。最好在您的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

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

+0

@Gabor非常有帮助。非常感谢你。 – NightFox 2013-03-28 06:37:38