基于igraph的C函数的改进

发布于 2024-11-29 06:43:21 字数 1948 浏览 0 评论 0原文

与问题“边-权重关联”和 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文