基于igraph的C函数的改进
与问题“边-权重关联”和 Tamás 的答案相关,我编写了以下代码来获取从原始向量权重中提取的 mst 树的弧权重。当我迁移到 igraph 0.6 时,我将使用此代码。
有人在代码中看到了一些错误或改进,两者都像在 igraph 使用中一样。
谢谢,吉列尔莫。 代码:
int igraph_get_weight_subtree( igraph_t subtree, igraph_t grafo, igraph_vector_t vector_pesos_grafo, igraph_vector_t *vector_pesos_subtree){
igraph_vector_t vector_arcos_grafo, vector_arcos_subtree;
int i,j,k;
igraph_real_t sub_tree_nodo1, sub_tree_nodo2;
printf("\nigraph_get_weight_subtree: Imprimiendo arcos de subtree...\n");
igraph_write_graph_edgelist(&subtree, stdout);
printf("[OK]\n");
printf("\nigraph_get_weight_subtree: Imprimiendo arcos de grafo...\n");
igraph_write_graph_edgelist(&grafo, stdout);
printf("[OK]\n");
//Inicializa los vectores vector_arcos_subtree y vector_arcos_grafo
igraph_vector_init(&vector_arcos_subtree,1);
igraph_vector_init(&vector_arcos_grafo,1);
//Extrae los arcos desde los grafos de entrada. Los convierte en Vector
igraph_get_edgelist(&subtree, &vector_arcos_subtree, 0);
igraph_get_edgelist(&grafo, &vector_arcos_grafo, 0);
for (i=0; i< igraph_vector_size(&vector_arcos_subtree); i=i+2){
sub_tree_nodo1= VECTOR(vector_arcos_subtree)[i];
sub_tree_nodo2= VECTOR(vector_arcos_subtree)[i+1];
//Busca arco de subtree en grafo
j=0;
k=0;
while(j< igraph_vector_size(&vector_arcos_grafo)){
if( ((sub_tree_nodo1==VECTOR(vector_arcos_grafo)[j])&& (sub_tree_nodo2==VECTOR(vector_arcos_grafo)[j+1]))|| ((sub_tree_nodo2==VECTOR(vector_arcos_grafo)[j])&&(sub_tree_nodo1==VECTOR(vector_arcos_grafo)[j+1]))) {
igraph_vector_push_back (vector_pesos_subtree, VECTOR(vector_pesos_grafo)[k]);
break;
}//End if
j=j+2;
k=k+1;
}//End while
}
//Destruye los vectore en desuso
igraph_vector_destroy(&vector_arcos_subtree);
igraph_vector_destroy(&vector_arcos_grafo);
return 0;
}
Related with the question "Edge - weight association" and the Tamás's answer, I wrote the below code to get the arc weigth of the mst tree extracted from the original vector weigth. I will use this code while I move to igraph 0.6.
Some one see some mistake or improvement in the code, both general like in the igraph use.
Thanks, Guillermo.
The code:
int igraph_get_weight_subtree( igraph_t subtree, igraph_t grafo, igraph_vector_t vector_pesos_grafo, igraph_vector_t *vector_pesos_subtree){
igraph_vector_t vector_arcos_grafo, vector_arcos_subtree;
int i,j,k;
igraph_real_t sub_tree_nodo1, sub_tree_nodo2;
printf("\nigraph_get_weight_subtree: Imprimiendo arcos de subtree...\n");
igraph_write_graph_edgelist(&subtree, stdout);
printf("[OK]\n");
printf("\nigraph_get_weight_subtree: Imprimiendo arcos de grafo...\n");
igraph_write_graph_edgelist(&grafo, stdout);
printf("[OK]\n");
//Inicializa los vectores vector_arcos_subtree y vector_arcos_grafo
igraph_vector_init(&vector_arcos_subtree,1);
igraph_vector_init(&vector_arcos_grafo,1);
//Extrae los arcos desde los grafos de entrada. Los convierte en Vector
igraph_get_edgelist(&subtree, &vector_arcos_subtree, 0);
igraph_get_edgelist(&grafo, &vector_arcos_grafo, 0);
for (i=0; i< igraph_vector_size(&vector_arcos_subtree); i=i+2){
sub_tree_nodo1= VECTOR(vector_arcos_subtree)[i];
sub_tree_nodo2= VECTOR(vector_arcos_subtree)[i+1];
//Busca arco de subtree en grafo
j=0;
k=0;
while(j< igraph_vector_size(&vector_arcos_grafo)){
if( ((sub_tree_nodo1==VECTOR(vector_arcos_grafo)[j])&& (sub_tree_nodo2==VECTOR(vector_arcos_grafo)[j+1]))|| ((sub_tree_nodo2==VECTOR(vector_arcos_grafo)[j])&&(sub_tree_nodo1==VECTOR(vector_arcos_grafo)[j+1]))) {
igraph_vector_push_back (vector_pesos_subtree, VECTOR(vector_pesos_grafo)[k]);
break;
}//End if
j=j+2;
k=k+1;
}//End while
}
//Destruye los vectore en desuso
igraph_vector_destroy(&vector_arcos_subtree);
igraph_vector_destroy(&vector_arcos_grafo);
return 0;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论