C++使用igraph 3.3j计算图中不同的属性值

        在我们接下来的例子中,我们会在友谊路径(是友谊路径吗?...)中以不同的方法量化向心性。"友谊路径"来自于著名的圣札空手道俱乐部的练习大纲中,(如果你想了解的更

多的话,可以在internet中搜索"Zachary karate",),向心性的计算将会量化各个孤立顶点与图中心的关系值。

#include 
  
  
   
   
#include "include\igraph.h"
#pragma comment(lib,"igraph.lib")
int main() 
{
	igraph_t graph;
	igraph_vector_t v;
	igraph_vector_t result;
	igraph_real_t edges[]={0,1,0,2,0,3,0,4,0,5,6,0,7,0,8,0,9,0,10,0,11,0,12,0,13,0,17,0,19,0,21,0,31,1,2,1,3,1,7,1,13,1,17,1,19,1,21,1,30,2,3,2,7,2,27,2,28,2,32,2,9,2,8,2,13,3,7,3,12,3,13,4,6,4,10,5,6,5,10,5,16,6,16,8,30,8,32,18,33,19,33,20,32,20,33,22,32,22,33,23,25,23,27,23,32,23,33,23,29,24,25,24,27,24,31,25,31,26,29,26,33,27,33,28,31,28,33,29,32,29,33,30,32,30,33,31,32,31,33,32,33
	};
	igraph_vector_view(&v,edges,sizeof(edges)/sizeof(double));
	igraph_create(&graph,&v,0,IGRAPH_UNDIRECTED);
	igraph_vector_init(&result,0);
	igraph_degree(&graph,&result,igraph_vss_all(),IGRAPH_ALL,IGRAPH_LOOPS);
	printf("maximum degree is %10i,vertex %2i.\n",(int)igraph_vector_max(&result),(int)igraph_vector_which_max(&result));
	igraph_closeness(&graph,&result,igraph_vss_all(),IGRAPH_ALL);
	printf("maximum closeness is %10f,vertex%2i.\n",(double)igraph_vector_max(&result),(int)igraph_vector_which_max(&result));
	igraph_betweenness(&graph,&result,igraph_vss_all(),IGRAPH_UNDIRECTED);
	printf("Maximum betweenness is %10f,vertex %2i.\n",(double)igraph_vector_max(&result),(int)igraph_vector_which_max(&result));

	igraph_vector_destroy(&result);
	igraph_destroy(&graph);
	return 0;
}

  
  

这个例子反映了一些新的特性,首先,它展示了如何仅仅使用使用C数组作为边去定义图的一种方式,其实这可以认为是一种类型转换。函数igraph_vector_view()创建了C数组的视图,但他并不拷贝任何数据,这也就意味着你不用去调用,igraph_vector_destroy()去销毁以这种方式创建的数组,它通常被用来创建无向图。图的层,耦合度,以及顶点的量化向心性,可以被计算出来,最高的那个值,会被打印出来,记住,数组(result)作为返回值返回的必须事先被初始化,同时,重新调整大小的函数也可以保存结果。内置函数igraph_vaa_all()返回值作为参数,告诉了我们,函数能够计算出图中每个顶点的属性值。你可以速记为顶点选择器,顶点选择器可以帮助我们实现得到顶点的子集,你会在下面的章节中有更多地了解。

igraph_degree — 计算出顶点的度:

        int igraph_degree(const igraph_t *graph, igraph_vector_t *res, const igraph_vs_t vids, igraph_neimode_t mode, igraph_bool_t loops);该函数计算出顶点的出度,入

度,或者是无向图的度

Arguments: graph:图对象

res:Vector,它包含了结果,同时应该被初始化,并调整至合适的大小

vids:Vector, 要进行计算的顶点的id值(每个顶点都有一个id值对吧)

mode:是要计算出度还是入度或者是无向图的度loops:布尔值,是否计算自环的顶点

Returns: Error code: 

IGRAPH_EINVVID: invalid vertex id.

 IGRAPH_EINVMODE: 

invalid mode argument.时间复杂度: O(v)如果计算自环的话, 时间复杂度是O(v*d)否则. v 是要计算的顶点的数目, and d 是顶点的度

int igraph_closeness(const igraph_t *graph, igraph_vector_t *res, const igraph_vs_t vids, igraph_neimode_t mode);

顶点的亲密度 指的是其他顶点到该顶点的容易程度

它被定义为给定顶点数的数量减一除以的长度和所有从/到给定测地线顶点.如果图是非连接的, 并且两点之间并没有路径项链,顶点的数量用于代替测地线的长度

Arguments:

graph:图对象

res:估算的结果, 在所给定的顶点中其向心性量化值的存放地

vids:要量化向心性的顶点的id

mode:在有向图中要计算的最短路径的类型 

Possible values:

IGRAPH_OUTthe lengths of the outgoing paths are calculated.

IGRAPH_INthe lengths of the incoming paths are calculated.

IGRAPH_ALLthe directed graph is considered as an undirected one for the computation.

Returns: Error code:IGRAPH_ENOMEM内存告急IGRAPH_EINVVID顶点不可用IGRAPH_EINVMODE最短路径的类型不对

    int igraph_betweenness(const igraph_t *graph, igraph_vector_t *res, const igraph_vs_t vids, igraph_bool_t directed);一个顶点向心性的中间性是经过该顶点的测地线的数量. 如果两个顶点之间有超过一条的测地线, 测地线的权重将会变成测地线的数量

Arguments: 

graph:The graph object.

res:The result of the computation, a vector containing the betweenness scores for the specified vertices.

vids:The vertices of which the betweenness centrality scores will be calculated.

directed:Logical, if true directed paths will be considered for directed graphs. 

It is ignored for undirected graphs.

Returns: Error code: IGRAPH_ENOMEM, not enough memory for temporary data. IGRAPH_EINVVID, invalid vertex id passed in vids.同上,我就不翻译了,这个和之前的两

个类似

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值