【问题标题】:How to create weighted adjacency list/matrix from edge list?如何从边缘列表创建加权邻接列表/矩阵?
【发布时间】:2022-05-12 15:40:45
【问题描述】:

我的问题很简单:我需要从边列表创建一个邻接列表/矩阵。

我有一个存储在 csv 文档中的边列表,其中 column1 = node1 和 column2 = node2,我想将其转换为加权邻接列表或加权邻接矩阵。

更准确地说,数据如下所示 - 其中数字只是节点 ID:

node1,node2
551,548
510,512
548,553
505,504
510,512
552,543
512,510
512,510
551,548
548,543
543,547
543,548
548,543
548,542

关于如何实现从这个到加权邻接列表/矩阵的转换的任何提示? 这就是我之前决定这样做的方式,但没有成功(感谢Dai Shizuka):

dat=read.csv(file.choose(),header=TRUE) # choose an edgelist in .csv file format
el=as.matrix(dat) # coerces the data into a two-column matrix format that igraph likes
el[,1]=as.character(el[,1])
el[,2]=as.character(el[,2])
g=graph.edgelist(el,directed=FALSE) # turns the edgelist into a 'graph object'

谢谢!

【问题讨论】:

  • 您能否为我们提供一个可重现的小示例以及您可能尝试编写此代码的尝试?
  • This post 可能会有所帮助。
  • 感谢@Arun 将我指向该帖子。这确实很有用,但如果我没记错的话,他们的数据已经以矩阵方式排列,而从我的问题的编辑版本中可以看出,我有不同的输入。通过编辑帖子,我希望我也回复了 Roman。

标签: r igraph adjacency-list adjacency-matrix sna


【解决方案1】:

此响应仅使用基数 R。结果是一个标准矩阵,用来表示邻接矩阵。

 el  <- cbind(a=1:5, b=5:1) #edgelist (a=origin, b=destination)
 mat <- matrix(0, 5, 5)
 mat[el] <- 1
 mat
 #    [,1] [,2] [,3] [,4] [,5]
 #[1,]    0    0    0    0    1
 #[2,]    0    0    0    1    0
 #[3,]    0    0    1    0    0
 #[4,]    0    1    0    0    0
 #[5,]    1    0    0    0    0

这里的mat 是从边缘列表el 定义的邻接矩阵,它是向量1:55:1 的简单cbind

如果您的边缘列表包含权重,那么您需要稍微不同的解决方案。

el <- cbind(a=1:5, b=5:1, c=c(3,1,2,1,1)) # edgelist (a=origin, b=destination, c=weight)
mat<-matrix(0, 5, 5)
for(i in 1:NROW(el)) mat[ el[i,1], el[i,2] ] <- el[i,3]  # SEE UPDATE
mat
#     [,1] [,2] [,3] [,4] [,5]
#[1,]    0    0    0    0    3
#[2,]    0    0    0    1    0
#[3,]    0    0    2    0    0
#[4,]    0    1    0    0    0
#[5,]    1    0    0    0    0

更新

一段时间后,我意识到前面加权边缘列表示例中的 for 循环(第 3 行)是不必要的。您可以将其替换为以下矢量化操作:

mat[el[,1:2]] <- el[,3]

【讨论】:

【解决方案2】:

您在问题中提到的我网站上的帖子 (https://sites.google.com/site/daishizuka/toolkits/sna/sna_data) 使用 igraph 包,因此请确保已加载。

此外,我最近意识到 igraph 提供了一种更简单的方法,可以使用 graph.data.frame() 从边缘列表创建加权邻接矩阵。我已经在我的网站上更新了这个,但这里有一个简单的例子:

library(igraph)
el=matrix(c('a','b','c','d','a','d','a','b','c','d'),ncol=2,byrow=TRUE) #a sample edgelist
g=graph.data.frame(el)
get.adjacency(g,sparse=FALSE)

应该这样做。 sparse=FALSE 参数告诉它在邻接矩阵中显示 0。 如果您真的不想使用 igraph,我认为这是一种笨拙的方法:

el=matrix(c('a','b','c','d','a','d','a','b','c','d'),ncol=2,byrow=TRUE) #a sample edgelist
lab=names(table(el)) #extract the existing node IDs
mat=matrix(0,nrow=length(lab),ncol=length(lab),dimnames=list(lab,lab)) #create a matrix of 0s with the node IDs as rows and columns
for (i in 1:nrow(el)) mat[el[i,1],el[i,2]]=mat[el[i,1],el[i,2]]+1 #for each row in the edgelist, find the appropriate cell in the empty matrix and add 1.

【讨论】:

  • 请注意,对于加权网络,您需要在 get.adjacency() 调用中添加 attr='weight' 以使其返回加权邻接矩阵而不是未加权版本。
  • 很棒的链接,它还显示了使用框架导入边缘列表。我在 Python 中没有看到等效的 API:/
【解决方案3】:

从数据框边缘开始,使用 igraph 获取邻接矩阵:

头部(边缘)

  node1 node2
1   551   548
2   510   512
3   548   553
4   505   504
5   510   512
6   552   543

library(igraph)
as.matrix(get.adjacency(graph.data.frame(edges)))

    551 510 548 505 552 512 543 553 504 547 542
551   0   0   2   0   0   0   0   0   0   0   0
510   0   0   0   0   0   2   0   0   0   0   0
548   0   0   0   0   0   0   2   1   0   0   1
505   0   0   0   0   0   0   0   0   1   0   0
552   0   0   0   0   0   0   1   0   0   0   0
512   0   2   0   0   0   0   0   0   0   0   0
543   0   0   1   0   0   0   0   0   0   1   0
553   0   0   0   0   0   0   0   0   0   0   0
504   0   0   0   0   0   0   0   0   0   0   0
547   0   0   0   0   0   0   0   0   0   0   0
542   0   0   0   0   0   0   0   0   0   0   0

【讨论】:

    【解决方案4】:

    qdapTools 包的另一种可能性:

    library(qdapTools)
    
    el[rep(seq_len(nrow(el)), el[,'c']), c('a', 'b')] %>%
        {split(.[,'b'], .[,'a'])} %>%
        mtabulate()
    
    ##   1 2 3 4 5
    ## 1 0 0 0 0 3
    ## 2 0 0 0 1 0
    ## 3 0 0 2 0 0
    ## 4 0 1 0 0 0
    ## 5 1 0 0 0 0
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-25
      • 2014-09-04
      • 2018-06-21
      • 2016-10-15
      • 2017-08-06
      • 1970-01-01
      相关资源
      最近更新 更多