Pergunta

I have to undirected networks, g1 and g2. These networks have the same vertices, but different sets of links. I would like to see how much of the links in g2 are included in g1. What would be the right strategy? I was thinking about:

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

Are there more clever approaches to accomplish this? Thank you very much.

Foi útil?

Solução

graph.intersection gives a graph that contains exactly the common edges:

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
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top