2014-04-02 123 views
0

我必須使用無向網絡g1和g2。這些網絡具有相同的頂點,但具有不同的鏈接集。我想看看g1中包含g2中的多少鏈接。什麼是正確的戰略?我在想:如何檢查另一個網絡中包含多少網絡

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

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