删除C的图中的边 [英] Deleting an edge in graph for C

查看:9
本文介绍了删除C的图中的边的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从名为"g"的曲线图(http://igraph.org/c/)中删除随机选择的边。

IGRAPH_DELETE_EDGE函数的IGRAPH手册在这里:http://igraph.org/c/doc/igraph-Basic.html#igraph_delete_edges但我还是不能理解。

我有这个:

#include <stdio.h>
#include <igraph/igraph.h>

int main() {

igraph_t g;
igraph_vector_t v;

igraph_vector_t edges;
igraph_vector_init(&edges,0);

igraph_vector_t indices;
igraph_vector_init(&indices, 0);

igraph_erdos_renyi_game(&g, IGRAPH_ERDOS_RENYI_GNP, 100, .10,IGRAPH_UNDIRECTED, IGRAPH_NO_LOOPS); // Create a random graph

igraph_get_edgelist(&g, &edges, 0); // Put the graph's list of indices in "edges"
int r = rand() % igraph_vector_size(&edges); // Get a random edge index

igraph_vector_push_back(&indices, r); // create a vector of size 1 in which to put the random index found above

igraph_delete_edges(&g, igraph_es_vector(&indices)); // Delete that edge
}

这似乎不是使用iggraph_DELETE_EDGE的方法(对于如此简单的操作,它肯定非常冗长...)在用于C的iggraph中使用iggraph_DELETE_EDGE的正确方法是什么?

推荐答案

您的代码中有一些误解:

  • igraph_get_edgelist()不返回边缘索引列表。相反,它返回表示边的顶点索引对列表。因此,结果向量的长度是边数的两倍。
  • 如果要获取边数,请使用igraph_ecount()
  • igraph_es_vector()接受的不是一个参数,而是两个参数,并且需要一个类型为igraph_es_t的现有变量来构造边选择器。要在不需要预先存在的变量的情况下创建边选择器,请使用igraph_ess_vector()。这是igraph_es_vector()的即时版本(&Q;)。
  • 如果要删除单边,最简单的方法是使用igraph_es_1()

代码应如下所示(未经测试):

#include <igraph.h>

int main() {

  igraph_t g;

  igraph_erdos_renyi_game(&g, IGRAPH_ERDOS_RENYI_GNP, 100, .10, IGRAPH_UNDIRECTED, IGRAPH_NO_LOOPS); // Create a random graph

  igraph_delete_edges(&g, igraph_es_1(rand() % igraph_ecount(&g)); // Delete random edge

  // do something with g

  igraph_destroy(&g);
}

这篇关于删除C的图中的边的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆