select subgraph of all connected nodes based on node id

一笑奈何 提交于 2021-01-28 14:23:43

问题


I have a graph composed of 3 subgraphs :

 person1 <- c(0, 0, 1, 3, 6)
 person2 <- c(1, 2, 4, 5, 7)


 id <- c(0,1, 2, 3, 4, 5, 6, 7)
 person <- c("Marc", "Marc","Eric", "Alan", "Henri", "Adele", "Wil", "Marc")

 nodes <- data.frame(id, person,  stringsAsFactors = FALSE)

 union_edges <- data.frame(person1, person2)


 library(igraph)
 family_tree <- graph_from_data_frame(union_edges)
 plot(family_tree)

How can I a select subgraph of all connected nodes based on node id ? For example, selecting one node ( 1, 2, 4 or 0) will gives me this subgraph :


回答1:


You look at connected clusters:

comps <- components(family_tree)
ids <- names(comps$membership)[comps$membership == comps$membership["1"]]
#[1] "0" "1" "2" "4"

plot(induced_subgraph(family_tree, V(family_tree)[ids]))



来源:https://stackoverflow.com/questions/49753508/select-subgraph-of-all-connected-nodes-based-on-node-id

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!