2014-09-03 42 views
1

我有一個圖表列表(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" 

希望現在更清晰

+0

正如書面所述,這個問題在一般情況下是非常廣泛和難以回答的。請提供[可重現的示例](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example)。包括一些示例數據以及所需的輸出。這會讓你的問題更容易回答。 – MrFlick 2014-09-03 14:15:25

+0

你有沒有嘗試添加鄰接矩陣? – 2014-09-03 16:12:49

+0

不是@ WillBeason,也許我應該試試 – user2380782 2014-09-03 16:31:20

回答

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)) 
+0

當圖的頂點數相同時,您的方法@ WillBeason會起作用,但是圖的頂點數是否不同,鄰接矩陣的數目是不能減少的,對吧? – user2380782 2014-09-03 18:14:29

+0

只需將不在每個圖中的節點添加到圖中,並確保它們都正確排序。 – 2014-09-03 18:14:51

+0

謝謝@WillBeason,我會試試你的建議 – user2380782 2014-09-03 18:21:48