交叉图共享一定百分比的边和顶点

时间:2014-09-03 14:05:14

标签: r igraph

我有一个图表列表(igraph格式),我想获得一个合并图,它将是通过所有图形共享一定百分比的那些节点和顶点的交集。

我知道igraph库有函数graph.intersection()但是这个函数与所有图形中存在的所有顶点和节点相交。

非常感谢任何帮助

这是一个简短的例子

g1 <- graph.data.frame(df1, directed=F)
df2 <- data.frame(V1=c(1,2,2,3,4), V2=c(3,3,5,5,5))
g2 <- graph.data.frame(df2, directed=F)
df3 <- data.frame(V1=c(1,2,3,4), V2=c(3,3,5,5))
g3 <- graph.data.frame(df3, directed=F)
df4 <- data.frame(V1=c(1,1,2,3), V2=c(2,3,4,5))
g4 <- graph.data.frame(df4, directed=F)

get.edgelist(g1)
     [,1] [,2]
[1,] "1"  "3" 
[2,] "2"  "3" 
[3,] "2"  "4" 
[4,] "3"  "5" 
[5,] "4"  "5" 

get.edgelist(g2)
     [,1] [,2]
[1,] "1"  "3" 
[2,] "2"  "3" 
[3,] "2"  "5" 
[4,] "3"  "5" 
[5,] "4"  "5" 

get.edgelist(g3)
     [,1] [,2]
[1,] "1"  "3" 
[2,] "2"  "3" 
[3,] "3"  "5" 
[4,] "4"  "5" 

get.edgelist(g4)
     [,1] [,2]
[1,] "1"  "2" 
[2,] "1"  "3" 
[3,] "2"  "4" 
[4,] "3"  "5" 

如果我将所有图表放在一个列表中:

mylist <- list(g1,g2,g3,g4)

然后应用graph.intersection()函数:

g.int <- graph.intersection(mylist, keep.all.vertices=FALSE)

结果是包含以下节点和边的图:

V(g.int)
[1] "1" "2" "3" "4" "5"

get.edgelist(g.int)
     [,1] [,2]
[1,] "3"  "5" 
[2,] "1"  "3" 

我想要的是包含以某个百分比出现的那些顶点和边,在这个例子中我想包括75%的图中存在的边。因此,最佳结果将是:

V(g.int)
[1] "1" "2" "3" "4" "5"

get.edgelist(g.int)
     [,1] [,2]
[1,] "3"  "5" 
[2,] "1"  "3" 
[3,] "4"  "5"

希望现在更清楚了

1 个答案:

答案 0 :(得分:1)

您可以创建图表中所有边缘的图形,然后消除不经常出现的边缘。

library(igraph)

# generate graphs
edgeset <- combn(1:20, 2)

graphs <- list()
for (i in 1:10) {
  graphs[[i]] <- graph(i + edgeset[, sample(ncol(edgeset), 150)])
}

# Get a list of all edges in all graphs
myedges <- lapply(graphs, get.edgelist)

# Make a graph of all of the edges including overlap
uniongraph <- graph(do.call(rbind, myedges))

# Eliminate edges not overlapped enough
resultgraph <- graph.adjacency(get.adjacency(uniongraph) >= 0.75 * length(graphs))