I would like to generate a first-interactor subggraph from a group of seed nodes using a bigger graph as source. The source graph is huge with more than 300,000 edges, and my seed nodes are ~300. The subgraph (or multiple subgraphs) should contain the first interactors for each of the seed nodes.
I have applied the code in this post Creating Subgraph using igraph in R but it only gives me a big subgraph. I expect a big subgraph (continent) and another subgraphs with no connections to this bigger graph (islands).
Example code:
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)
The graph g1
gives me the largest component from decompose.graph
. This graph has my seed nodes and other nodes which are not first interactors for the seed nodes. What I am looking for is a subgraph, or a list of subgraphs, with the seed nodes and their first interactors. Maybe should I try with the neighborhood
group functions?
Many thanks in advance
EDIT1:
Following Gabor suggestion I have used neigborhood
function with order=1
. However, I still don't get my expected result. Please find below a detailed example:
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)
.
The result shows an edge between node '5143742' and '9987134'. My expected result is only the seed nodes(seeds) and the edges between them and the first interactors, not including those edges between first interactors
Thanks again