创建第一个订单子图,边缘仅在种子节点和第一个交互器之间

时间:2015-02-18 01:36:18

标签: r graph igraph subgraph

我想使用更大的图作为源从一组种子节点生成第一个交互器子图。源图很大,有超过300,000个边,我的种子节点大约是300。子图(或多个子图)应包含每个种子节点的第一个交互器。

我已在此帖Creating Subgraph using igraph in R中应用了该代码 但它只给了我一个大的子图。我期待一个大的子图(大陆)和另一个子图与这个更大的图(岛)无关。

示例代码:

g <-erdos.renyi.game(50, 3/50)
seeds <- c(1,4,5,6,40,30)
sg <- decompose.graph(g)
neighverts <- unique(unlist(sapply(sg,FUN=function(s){if(any(V(s) %in% seeds)) V(s) else NULL})))
g1 <- induced.subgraph(graph=g,vids=neighverts)

g1为我提供了来自decompose.graph的最大组件。该图具有我的种子节点和其他节点,这些节点不是种子节点的第一个交互者。我正在寻找的是子图或子图列表,其中包含种子节点及其第一个交互作用者。我应该尝试使用neighborhood群组功能吗?

非常感谢提前

EDIT1:

根据Gabor的建议,我使用neigborhood函数与order=1。但是,我仍然没有得到我预期的结果。请在下面找到一个详细的例子:

mat <- structure(list(X1 = c("5203820", "9985655", "5203820", "1795907","2190697", "5203820", "3233174", "1773848", "892104", "9976862","9987167", "9987134", "1211741", "1722252", "9986835", "9986879","9986835", "61145", "3442328", "5203820", "9987075", "3442328","1773848", "119371", "9986887", "2190697", "9987032", "9974927","9986835", "892104", "3744713", "9974927", "5118892", "9987134","9987134", "3442328", "2190697", "5203820", "9985655", "9987167","9990684", "3233174", "5203820", "9990684", "3744713", "9990684","9990684", "61145", "9990684", "3442328", "5378006", "9987035","9987035", "9987088", "3442328", "5118895"), X2 = c("61145","9987167","4928047", "9975931", "3744713", "3744713", "9975931","3820326", "9987100", "9987035", "9990670", "9990670", "9984628","3588994", "9986879", "9987059", "9987059", "9978463", "9986877","1795907", "9987142", "3588994", "1211741", "2111600", "9987124","1773848", "9987228", "9987100", "3442328", "3820326", "9983385","9978897", "9987019", "9987059", "5143742", "5143742", "9983385","9978463", "9987134", "9986887", "9990684", "9990684", "9990684","5373267", "9990684", "5389588", "5389374", "9990684", "5393513","9987088", "9987088", "9987035", "5118895", "9987088", "9987035","9987088")), .Names = c("X1", "X2"), row.names = c(NA, -56L), class = "data.frame")

g <- graph.data.frame(mat, directed=F)
seeds <- c('1722252', '9990670', '3442328', '5378006', '9990684', '9978463')
g1 <- induced.subgraph(g, vids = unlist(neighborhood(g, 1, nodes = seeds)))

plot(g1)

image1

结果显示节点'5143742'和'9987134'之间的边缘。我的预期结果只是种子节点(种子)和它们与第一个交互者之间的边缘,不包括第一个交互者之间的边缘

再次感谢

1 个答案:

答案 0 :(得分:2)

要获得仅包含种子顶点与其第一个交互者之间边缘的诱导子图,您可以使用评论中指示的subgraph.edges

g1 <- subgraph.edges(g, unlist(get.adjedgelist(g)[seeds]) )