如何检查另一个网络中包含多少网络

时间:2014-04-02 08:10:05

标签: r igraph

我必须使用无向网络,g1和g2。这些网络具有相同的顶点,但具有不同的链接集。我想看看g2中有多少链接包含在g1中。什么是正确的策略?我在考虑:

g1 <- erdos.renyi.game(5, 0.8)
g2 <- erdos.renyi.game(5, 0.5)
adj1 <- get.adjacency(g1)
adj2 <- get.adjacency(g2)
summ <- adj1+adj2
similarity <- sum(summ == 2) / 2 / ecount(g2)
similarity

有更聪明的方法来实现这一目标吗?非常感谢你。

1 个答案:

答案 0 :(得分:1)

graph.intersection提供了一个完全包含公共边的图表:

library(igraph)
set.seed(42 * 42)
g1 <- erdos.renyi.game(5, 0.8)
g2 <- erdos.renyi.game(5, 0.5)

g1[]
# 5 x 5 sparse Matrix of class "dgCMatrix"
#               
# [1,] . 1 1 1 1
# [2,] 1 . 1 1 .
# [3,] 1 1 . 1 1
# [4,] 1 1 1 . 1
# [5,] 1 . 1 1 .

g2[]
# 5 x 5 sparse Matrix of class "dgCMatrix"
#               
# [1,] . . 1 1 .
# [2,] . . . 1 .
# [3,] 1 . . 1 1
# [4,] 1 1 1 . .
# [5,] . . 1 . .

gi <- graph.intersection(g1, g2)
gi[]
# 5 x 5 sparse Matrix of class "dgCMatrix"
#               
# [1,] . . 1 1 .
# [2,] . . . 1 .
# [3,] 1 . . 1 1
# [4,] 1 1 1 . .
# [5,] . . 1 . .

ecount(gi)
# [1] 5