R iGraph:如何从图获取加权邻接矩阵? [英] R iGraph: How to get weighted adjacency matrix from a graph?

查看:493
本文介绍了R iGraph:如何从图获取加权邻接矩阵?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尽管有一些关于从邻接矩阵创建图的问题,但我对从加权图提取 加权 邻接矩阵的了解不多. /p>

说我有以下图表:

library(igraph)
nodes <- data.frame(name=c("a","b", "c", "d", "f", "g"))

col1 <- c("a", "g", "f","f", "d","c")
col2 <- c("b", "f","c","d","a","a")
weight <- c(1,4,2,6,2,3)
edges <- cbind.data.frame(col1,col2,weight)

g <- graph.data.frame(edges, directed=F, vertices=nodes)
E(g)$weight <- weight

如何获得图g的加权邻接矩阵?

解决方案

看来,实际上有很多方法可以做到这一点. 也许显而易见,第一种方法是仔细查看文档 c0>并使用attr选项:

as_adjacency_matrix(g,attr = "weight",sparse = T)

6 x 6 sparse Matrix of class "dgCMatrix"
  a b c d f g
a . 1 3 2 . .
b 1 . . . . .
c 3 . . . 2 .
d 2 . . . 6 .
f . . 2 6 . 4
g . . . . 4 .

但是也可以键入

get.adjacency(g,attr = "weight",sparse = T)

或者只是

g[]

6 x 6 sparse Matrix of class "dgCMatrix"
  a b c d f g
a . 1 3 2 . .
b 1 . . . . .
c 3 . . . 2 .
d 2 . . . 6 .
f . . 2 6 . 4
g . . . . 4 .

即使我不确定最后一个选项的有效范围.

While there are some questions dealing with creating a graph from an adjacency matrix, I haven't found much about extracting the weighted adjacency matrix from a weighted graph.

Say I have the following graph:

library(igraph)
nodes <- data.frame(name=c("a","b", "c", "d", "f", "g"))

col1 <- c("a", "g", "f","f", "d","c")
col2 <- c("b", "f","c","d","a","a")
weight <- c(1,4,2,6,2,3)
edges <- cbind.data.frame(col1,col2,weight)

g <- graph.data.frame(edges, directed=F, vertices=nodes)
E(g)$weight <- weight

How do I get the weighted adjacency matrix of graph g?

解决方案

It appears there are actually quite a few ways to do this. Perhaps obvious, a first way to do it is to look carefully at the documentation of as_adjacency_matrix() and using the attr option:

as_adjacency_matrix(g,attr = "weight",sparse = T)

6 x 6 sparse Matrix of class "dgCMatrix"
  a b c d f g
a . 1 3 2 . .
b 1 . . . . .
c 3 . . . 2 .
d 2 . . . 6 .
f . . 2 6 . 4
g . . . . 4 .

But one can also type

get.adjacency(g,attr = "weight",sparse = T)

Or just

g[]

6 x 6 sparse Matrix of class "dgCMatrix"
  a b c d f g
a . 1 3 2 . .
b 1 . . . . .
c 3 . . . 2 .
d 2 . . . 6 .
f . . 2 6 . 4
g . . . . 4 .

Even if I'm not sure of the scope of validity of this last option.

这篇关于R iGraph:如何从图获取加权邻接矩阵?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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