简体   繁体   English

生成igraph中每个顶点的边缘权重列表

[英]Generating the list of edge weights for each vertex in igraph

The igraph function: igraph函数:

nn.list <- adjacent_vertices(g, V(g))

outputs a list of adjacent vertices for each vertex. 输出每个顶点的相邻顶点列表。

This is very useful. 这是非常有用的。 But now I need a similar list containing the edge weights. 但是现在我需要包含边缘权重的类似列表。

For example, if vertex 600 has the follow adjacent vertices 597, 598, 599, 601 found from nn.list[[600]] , I now need a list where entry 600 gives the edge weights between vertex 600 and each of the adjacent vertices 597, 598, 599, 601 . 例如,如果顶点600具有遵循相邻顶点597, 598, 599, 601从找到的nn.list[[600]]我现在需要一个列表,其中条目600给出顶点之间的边的权值600与每个邻近顶点的597, 598, 599, 601

I can do this with a for loop - but it's slow. 我可以使用for循环执行此操作-但速度很慢。 My network has about 10^4 nodes and 10^5 edges. 我的网络大约有10^4节点和10^5边缘。 I'm looking for a vectorized approach. 我正在寻找一种矢量化方法。 Is there a built in functions in igraph would help me do this? igraph是否有内置函数可以帮助我做到这一点? Any suggestions? 有什么建议么?

library(igraph)
g<-make_empty_graph(directed = F)

g<-add.vertices(g,c(10))

g<-add_edges(g,c(3,5))
g<-add_edges(g,c(4,5))
g<-set_edge_attr(graph = g,name="weight",index = E(g)[1],value = 0.3)
g<-set_edge_attr(graph = g,name="weight",index = E(g)[2],value = 10)


gedges<-E(g)
gweights <- E(g)$weight

as_adj(g)


 as_adj(g,attr="weight")

as_adj(g)
as_adj(g,attr="weight")
as_adj(g,attr="weight", edges = T,sparse = F)

If you want to retrieve a vector of ALL edges, use E(g) , if you want a vector of ALL weights use E(g)$weight 如果要检索所有边的向量,请使用E(g) ,如果要获取所有权重的向量,请使用E(g)$weight

Then, as you seem to need an adjacency matrix you can use as_adj tweaking the params until you get all info you want to! 然后,由于您似乎需要一个adjacency matrix ,可以使用as_adj调整参数,直到获得所需的所有信息!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM